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

--GAME VARS
fDefaultFOV = 80
fAdditionalFOV = 0
fScreenAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fScreenAspectRatio169 = 1.77778
fmenusFOV = 54.42170334
fMapFOV = 75.00003052

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

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "GoWR.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,90,0,1)
	
	FOVSlider:SetTickFrequency(10)
	FOVSlider:SetLabel1Text("-20")
	FOVSlider:SetLabel2Text("+70")

	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)

end

function Configure_SignatureScan()

	local tResultCount = HackTool:SignatureScanMulti("75 ?? F3 0F ?? ?? ?? ?? ?? ?? C3 48 ?? ?? ?? ?? ?? ?? F3 0F","FOVS",PAGE_EXECUTE_READ,0x02,Process_EXEName)
	
	if tResultCount ~= 2 then	
		return ErrorOccurred("Could not find FOVS injection point, " .. Process_FriendlyName .. 
							 " may have updated to a version that is no longer supported.\r\n\r\n" .. 
							 "Try selecting a different version and re-enable the fix."	)
	else
	
		local tAddress = HackTool:AddAddress("FOV", HackTool:GetAddress( string.format("FOVS%d",1) ))
		print( tAddress:GetInfo(TYPE_ADDRESS) )	
		--GoWR.exe+729FB0 - A8 01                 	- test al,01
		--GoWR.exe+729FB2 - 75 09                 	- jne GoWR.exe+729FBD
		--GoWR.exe+729FB4 - F3 0F 10 81 A0 01 00 00 - movss xmm0,[rcx+000001A0]
		--GoWR.exe+729FBC - C3                    	- ret 
		--GoWR.exe+729FBD - 48 8B 81 80 01 00 00    - mov rax,[rcx+00000180]
	end

	local tAddress = HackTool:AddAddress("ASPECT")
	
	if HackTool:SignatureScan("89 ?? 48 63 ?? ?? ?? ?? ?? F3 41",tAddress,PAGE_EXECUTE_READ,0x09,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--GoWR.exe+40F2C7 - 89 02                 		   - mov [rdx],eax
		--GoWR.exe+40F2C9 - 48 63 05 70 84 37 03     	   - movsxd  rax,dword ptr [GoWR.exe+3787740]
		--GoWR.exe+40F2D0 - F3 41 0F 10 84 80 B0 81 D6 01  - movss xmm0,[r8+rax*4+01D681B0]
		--GoWR.exe+40F2DA - C3                    		   - ret 
		--GoWR.exe+40F2DB - CC                    		   - int 3 
	end
	
	local tAddress = HackTool:AddAddress("DOF") --53.86122894
	
	if HackTool:SignatureScan("40 22 ?? ?? ?? ?? ?? F3 44 ?? ?? ?? C7 05",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--GoWR.exe+5758C8 - F3 44 0F 10 9E DC 03 00 00  	- movss xmm11,[rsi+000003DC]
		--GoWR.exe+5758D1 - 0F 28 E8               			- movaps xmm5,xmm0
		--GoWR.exe+5758D4 - 40 22 BB E5 02 00 00     		- and dil,[rbx+000002E5] one byte that is set to 0 (disabled dof) or 1
		--GoWR.exe+5758DB - F3 44 0F 5E DE         			- divss xmm11,xmm6
		--GoWR.exe+5758E0 - C7 05 56 E3 FA 01 00 00 80 3F 	- mov [GoWR.exe+2523C40],3F800000
	end

	return true
end

function Enable_Inject()

	local tAddress = HackTool:GetAddress("FOV")
	local NearTo = HackTool:GetBaseAddress() - tAddress:GetAddress() -- dirty trick to allocate fix nearby code to hack plus an injection delay
	local Variables = HackTool:AllocateMemory("Variables",100)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("FOVMenus")
	Variables:PushFloat("FOVMap")
	Variables:Allocate(NearTo)
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["ScreenRatio"]:WriteFloat(fScreenAspectRatio)
	Variables["FOVMenus"]:WriteFloat(fmenusFOV)
	Variables["FOVMap"]:WriteFloat(fMapFOV)
	
	ResolutionChanged()

	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			;comiss $$1,[(allocation)Variables->FOVMenus]			$ctx=1
			;jbe shortcutFOV
			;comiss $$1,[(allocation)Variables->FOVMap]				$ctx=1
			;je shortcutFOV
			movss [(allocation)Variables->FOVIn],$$1				$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]		$ctx=1
			movss [(allocation)Variables->FOVOut],$$1				$ctx=1
		shortcutFOV:
			jmp %returnaddress%
			%end%
		
		(codecave:jmp)ASPECT,ASPECT_cc:
			$$0 $$1,[(allocation)Variables->ScreenRatio]			$ctx=1
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)DOF,DOF_cc:
			mov byte ptr [$$2],0									$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
		
	]]

	if HackTool:CompileAssembly(asm,"FOV",NearTo) == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("ASPECT_cc",bAspect)
		Toggle_CodeCave("DOF_cc",bDOF)
	end	
			
	Write_FOV()

end

function Periodic()
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		local fFOVIn = Variables["FOVIn"]:ReadFloat()
		local fFOVOut = Variables["FOVOut"]:ReadFloat()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %0.2f, FOV Out : %.02f", fFOVIn, fFOVOut))
	end
end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetPosition() - 20
	lblFOVSlider.Caption:SetCaption( string.format("Value: +%.0f",fAdditionalFOV) )
		
	Write_FOV()
	
	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 Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKARFix_Changed(Sender)

	bAspect = Toggle_CheckFix(Sender)		
	Toggle_CodeCave("ASPECT_cc",bAspect)
	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
