require(GlobalDependencys:GetDependency("StandardBase"):GetPackageName())

--GAME VARS
fDefaultFOV = 85
fAdditionalFOV = 0
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fAspectRatio219 = 2.333333254
fAspectRatio169 = 1.777777791
fFactor = 0.00872665
fHUDSafeZOneLeft = 0
fHUDSafeZoneRight = 100

--ControlVars
bFixEnabled = true
bFOV = true
bAspect = true
bDOF = true
bHUDSafeZone = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "Dust-Win64-Shipping.exe"

--INJECTION BEHAVIOUR
InjectDelay = 500
WriteInterval = 100
SearchInterval = 100
SuspendThread = true

--Name                         Manual/Auto/Hybrid  		Steam/Origin/Any                IncludeFile:Configure;Enable;Periodic;Disable;
SupportedVersions = { 		
{"Automatically Detect",       "Hybrid",  			"Any",	                         "Configure_SignatureScan;Enable_Inject;Periodic;Disable_Inject;"},
}

function Init_Controls()

	DefaultControls.AddHeader("Header_FixesEnableDisable","Individual Fixes",245,70,210,17)
	
	DefaultControls.AddHeader("Header_FOV","FOV fine adjustment",15,70,210,17)	
	DefaultControls.AddFixedFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35,0,60,0,1)
	
	FOVSlider:SetTickFrequency(5)
	FOVSlider:SetLabel1Text("-20")
	FOVSlider:SetLabel2Text("+40")

	DefaultControls.AddHeader("Header_HUD_SZ_LEFT","HUD safe zone left",15,190,210,17)	
	DefaultControls.AddFixedFOVSlider("HUD_SZ_LEFT","HUDSZLSlider_Changed",55,220,125,35,0,40,0,1)
	HUD_SZ_LEFT:SetTickFrequency(5)
	HUD_SZ_LEFT:SetLabel1Text("0")
	HUD_SZ_LEFT:SetLabel2Text("40")
	DefaultControls.AddHeader("Header_HUDSZRIGHT","HUD safe zone right",245,190,210,17)	
	DefaultControls.AddFixedFOVSlider("HUD_SZ_RIGHT","HUDSZRSlider_Changed",285,220,125,35,0,40,0,1)
	HUD_SZ_RIGHT:SetTickFrequency(5)
	HUD_SZ_RIGHT:SetLabel1Text("0")
	HUD_SZ_RIGHT:SetLabel2Text("40")

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKARFix_Enable","Aspect ratio fix","CKARFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKDOFFix_Enable","Depth of field fix","CKDOFFix_Changed",255,141,180,14)
	DefaultControls.AddFixToggle("CKHUDFix_Enable","HUD safe zone fix","CKHUDFix_Changed",255,161,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("E8 ?? ?? ?? ?? 0F ?? ?? 48 8B ?? ?? ?? ?? ?? ?? 48 33 ?? E8 ?? ?? ?? ?? 0F 28 ?? ?? ?? ?? ?? ?? 48 81 ?? ?? ?? ?? ?? C3",tAddress,PAGE_EXECUTE_READ,0x05,Process_EXEName) == 0 then
	--if HackTool:SignatureScan("74 ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? EB ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? 8B 83",tAddress,PAGE_EXECUTE_READ,0x0a,Process_EXEName) == 0 then
	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Dust-Win64-Shipping.exe+2A4D18D - 74 05                 	- je Dust-Win64-Shipping.exe+2A4D194
		--Dust-Win64-Shipping.exe+2A4D18F - E8 BC A7 60 FE          - call Dust-Win64-Shipping.exe+1057950
		--Dust-Win64-Shipping.exe+2A4D194 - 0F 28 C6               	- movaps xmm0,xmm6
		--Dust-Win64-Shipping.exe+2A4D197 - 48 8B 8C 24 10 06 00 00 - mov rcx,[rsp+00000610]
		--Dust-Win64-Shipping.exe+2A4D19F - 48 33 CC              	- xor rcx,rsp
	end

	local tAddress = HackTool:AddAddress("HUDSAFEZONE")
	if HackTool:SignatureScan("48 ?? ?? 48 ?? ?? 66 48 ?? ?? ?? FF ?? 48 8B ?? ?? ?? 0F ?? ??",tAddress,PAGE_EXECUTE_READ,0x18,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Dust-Win64-Shipping.exe+233F811 - 0F 10 00              - movups xmm0,[rax]
		--Dust-Win64-Shipping.exe+233F814 - 48 8B C7              - mov rax,rdi
		--Dust-Win64-Shipping.exe+233F817 - 0F 11 07              - movups [rdi],xmm0
		--Dust-Win64-Shipping.exe+233F81A - 48 83 C4 30           - add rsp,30
		--Dust-Win64-Shipping.exe+233F81E - 5F                    - pop rdi
	end
	
	local tAddress = HackTool:AddAddress("ASPECT")
	
	if HackTool:SignatureScan("CC CC 48 ?? ?? ?? ?? 57 48 ?? ?? ?? F2 ?? ?? ?? 48 ?? ?? F2 ?? ?? ?? 48 ?? ?? 8B ?? ?? 89 ?? ?? F2 ?? ?? ?? ?? F2 ?? ?? ?? ?? 8B",tAddress,PAGE_EXECUTE_READ,0x51,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Dust-Win64-Shipping.exe+2A3E8C6 - 8B 42 28              - mov eax,[rdx+28]
		--Dust-Win64-Shipping.exe+2A3E8C9 - 89 41 28              - mov [rcx+28],eax
		--Dust-Win64-Shipping.exe+2A3E8CC - 8B 42 2C              - mov eax,[rdx+2C]
		--Dust-Win64-Shipping.exe+2A3E8CF - 89 41 2C              - mov [rcx+2C],eax
		--Dust-Win64-Shipping.exe+2A3E8D2 - 8B 41 30              - mov eax,[rcx+30]
	end

	local tAddress = HackTool:AddAddress("ASPECT2")
	
	if HackTool:SignatureScan("C3 CC 48 ?? ?? ?? ?? 57 48 ?? ?? ?? F2 ?? ?? ?? 48 ?? ?? F2 ?? ?? ?? 48 ?? ?? 8B ?? ?? 89 ?? ?? F2 ?? ?? ?? ?? F2 ?? ?? ?? ?? 8B",tAddress,PAGE_EXECUTE_READ,0x51,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Dust-Win64-Shipping.exe+2A3E8C6 - 8B 42 28              - mov eax,[rdx+28]
		--Dust-Win64-Shipping.exe+2A3E8C9 - 89 41 28              - mov [rcx+28],eax
		--Dust-Win64-Shipping.exe+2A3E8CC - 8B 42 2C              - mov eax,[rdx+2C]
		--Dust-Win64-Shipping.exe+2A3E8CF - 89 41 2C              - mov [rcx+2C],eax
		--Dust-Win64-Shipping.exe+2A3E8D2 - 8B 41 30              - mov eax,[rcx+30]
	end
	
	local tAddress = HackTool:AddAddress("DOF")
	
	if HackTool:SignatureScan("48 8B ?? ?? ?? ?? ?? 48 ?? ?? 8B ?? ?? E8 ?? ?? ?? ?? 8B",tAddress,PAGE_EXECUTE_READ,0x07,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Dust-Win64-Shipping.exe+18B9FB0 - 48 8B 05 C1 BB 0B 03  - mov rax,[Dust-Win64-Shipping.exe+4975B78]
		--Dust-Win64-Shipping.exe+18B9FB7 - 48 8B CB              - mov rcx,rbx
		--Dust-Win64-Shipping.exe+18B9FBA - 8B 78 04              - mov edi,[rax+04]
		--Dust-Win64-Shipping.exe+18B9FBD - E8 EE B2 21 01        - call Dust-Win64-Shipping.exe+2AD52B0
		--Dust-Win64-Shipping.exe+18B9FC2 - 8B C8                 - mov ecx,eax
	end

	return true
end

function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("CompensatedFOV")
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("factor")
	Variables:PushFloat("HUDSafeZoneLeft")
	Variables:PushFloat("HUDSafeZoneTop")
	Variables:PushFloat("HUDSafeZoneRight")
	Variables:PushFloat("HUDSafeZoneBottom")
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["AspectRatio"]:WriteFloat(fAspectRatio169)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["factor"]:WriteFloat(fFactor)
	Variables["HUDSafeZoneLeft"]:WriteFloat(0)
	Variables["HUDSafeZoneTop"]:WriteFloat(0)
	Variables["HUDSafeZoneRight"]:WriteFloat(1)
	Variables["HUDSafeZoneBottom"]:WriteFloat(1)
	
	ResolutionChanged()	

	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
		    movss [(allocation)Variables->FOVIn],$$2					$ctx=1
		    fld dword ptr [(allocation)Variables->FOVIn]
		    fmul dword ptr [(allocation)Variables->factor]
		    fptan
		    fstp st0
		    fld dword ptr [(allocation)Variables->ScreenRatio]			
		    fdiv dword ptr [(allocation)Variables->AspectRatio]
		    fmulp st1,st0
		    fld1
		    fpatan
		    fdiv dword ptr [(allocation)Variables->factor]
		    fst dword ptr [(allocation)Variables->CompensatedFOV]
		    fadd dword ptr [(allocation)Variables->AdditionalFOV]
		    fstp dword ptr [(allocation)Variables->FOVOut]
		    movss $$2,[(allocation)Variables->FOVOut]					$ctx=1
		    %originalcode%
		    jmp %returnaddress%
		    %end%
						
		(codecave:jmp)HUDSAFEZONE,HUDSAFEZONE_cc:
			$$0 $$2,[(allocation)Variables->HUDSafeZoneLeft]		$ctx=1 ; Will copy all safe zone offsets
			%originalcode%
			jmp %returnaddress%			
			%end%
			
		(codecave:jmp)ASPECT,ASPECT_cc:
			$$0 $$2,[(allocation)Variables->ScreenRatio]				$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
						
		(codecave:jmp)ASPECT2,ASPECT2_cc:
			%originalcode%
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)DOF,DOF_cc:
			%originalcode%
			$$0 edi,0													$ctx=1
			jmp %returnaddress%
			%end%
			
	]]

	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFixEnabled)
		Toggle_CodeCave("HUDSAFEZONE_cc",bHUDSafeZone)
		Toggle_CodeCave("ASPECT_cc",bAspect)
		Toggle_CodeCave("ASPECT2_cc",bAspect)
		Toggle_CodeCave("DOF_cc",bDOF)
	end	
			
	Write_FOV()
	Write_AR()
	Write_HUD_Safe_Zone_Left()
	Write_HUD_Safe_Zone_Right()
	
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		local fCompensatedFOV = Variables["CompensatedFOV"]:ReadFloat()
		local fFOVIn = Variables["FOVIn"]:ReadFloat()
		local fFOVOut = Variables["FOVOut"]:ReadFloat()

		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %0.2f, Compensated FOV : %0.2f, FOV Out : %.02f", fFOVIn, fCompensatedFOV, fFOVOut))
	end
	
end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetPosition() - 20
	lblFOVSlider.Caption:SetCaption( string.format("Value: +%.0f",fAdditionalFOV) )
		
	Write_FOV()
	
	ForceUpdate()

end

function HUDSZLSlider_Changed(Sender)
	
	fHUDSafeZOneLeft = Sender:GetPosition()
	lblHUD_SZ_LEFT.Caption:SetCaption( string.format("+ %.0f %%",fHUDSafeZOneLeft) )
	Write_HUD_Safe_Zone_Left()
	ForceUpdate()
	
end

function HUDSZRSlider_Changed(Sender)

	fHUDSafeZoneRight = Sender:GetPosition()
	lblHUD_SZ_RIGHT.Caption:SetCaption( string.format("+ %.0f %%",fHUDSafeZoneRight) )
	Write_HUD_Safe_Zone_Right()
	ForceUpdate()
	
end

function Write_FOV()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AdditionalFOV"] then
		if bFOV == true then
			Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
		else
			Variables["AdditionalFOV"]:WriteFloat(0)
		end
	end

end

function Write_AR()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AspectRatio"] then
		if bAspect == false then		
			Variables["AspectRatio"]:WriteFloat(fAspectRatio)
		else
			Variables["AspectRatio"]:WriteFloat(fAspectRatio169)
		end
	end
	
end

function Write_HUD_Safe_Zone_Left()
	
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["HUDSafeZoneLeft"] then
		Variables["HUDSafeZoneLeft"]:WriteFloat(fHUDSafeZOneLeft / 100)
	end
	
end

function Write_HUD_Safe_Zone_Right()
	
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["HUDSafeZoneRight"] then
		Variables["HUDSafeZoneRight"]:WriteFloat(1 - (fHUDSafeZoneRight / 100))
	end
	
end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

	bFOV = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FOV_cc",bFixEnabled)
	Write_FOV()
	ForceUpdate()
	
end

function CKARFix_Changed(Sender)

	bAspect = Toggle_CheckFix(Sender)
	Toggle_CodeCave("ASPECT_cc",bAspect)
	Toggle_CodeCave("ASPECT2_cc",bAspect)
	Write_AR()
	ForceUpdate()

end

function CKHUDFix_Changed(Sender)

	bHUDSafeZone = Toggle_CheckFix(Sender)
	Toggle_CodeCave("HUDSAFEZONE_cc",bHUDSafeZone)
	ForceUpdate()

end

function CKDOFFix_Changed(Sender)

	bDOF = Toggle_CheckFix(Sender)
	Toggle_CodeCave("DOF_cc",bDOF)
	ForceUpdate()

end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
