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

--GAME VARS
fDefaultFOV = 80
fAdditionalFOV = 0
fScreenAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fScreenAspectRatio169 = 1.77778
fFactor = 0.00872665

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

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "Gow.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 tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? C6 80 ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 48 83 ?? ??",tAddress,PAGE_EXECUTE_READ,0x0f,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--GoW.exe+4CB9F7 - F3 0F 11 80 90 00 00 00   - movss [rax+00000090],xmm0
		--GoW.exe+4CB9FF - C6 80 94 00 00 00 00      - mov byte ptr [rax+00000094],00
		--GoW.exe+4CBA06 - F3 0F 11 83 A8 01 00 00   - movss [rbx+000001A8],xmm0
		--GoW.exe+4CBA0E - 48 83 C4 20           	 - add rsp,20
		--GoW.exe+4CBA12 - 5B                        - pop rbx
	end

	local tAddress = HackTool:AddAddress("ASPECT")
	
	if HackTool:SignatureScan("7D ?? F3 0F ?? ?? ?? ?? ?? ?? ?? EB ?? 41 0F ?? ??",tAddress,PAGE_EXECUTE_READ,0x02,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--GoW.exe+991340 - 7D 0B                 	   - jnl GoW.exe+99134D
		--GoW.exe+991342 - F3 0F 10 B4 86 38 0E D6 00  - movss xmm6,[rsi+rax*4+00D60E38]
		--GoW.exe+99134B - EB 04                 	   - jmp GoW.exe+991351
		--GoW.exe+99134D - 41 0F 28 F0            	   - movaps xmm6,xmm8
		--GoW.exe+991351 - F3 0F 10 05 B3 B8 6D 04     - movss xmm0,[GoW.exe+506CC0C]
	end

	local tAddress = HackTool:AddAddress("DOF")
	
	if HackTool:SignatureScan("74 ?? F3 0F ?? ?? ?? ?? ?? ?? F3 41 ?? ?? ?? F3 0F ?? ?? ?? ?? EB",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--GoW.exe+4116F4 - F3 0F 11 15 60 74 D7 00  - movss [GoW.exe+1188B5C],xmm2
		--GoW.exe+4116FC - 40 84 FF              	- test dil,dil
		--GoW.exe+4116FF - 74 15                 	- je GoW.exe+411716
		--GoW.exe+411701 - F3 0F 10 05 1F 4C AE 00  - movss xmm0,[GoW.exe+EF6328]
		--GoW.exe+411709 - F3 41 0F 5E C6         	- divss xmm0,xmm14
	end

	return true
end

function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("ScreenRatio")
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["ScreenRatio"]:WriteFloat(fScreenAspectRatio)
	
	ResolutionChanged()	

	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			$$0 [(allocation)Variables->FOVIn],$$2					$ctx=1
			addss $$2,[(allocation)Variables->AdditionalFOV]		$ctx=1
			$$0 [(allocation)Variables->FOVOut],$$2					$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
		
		(codecave:jmp)ASPECT,ASPECT_cc:
			$$0 $$1,[(allocation)Variables->ScreenRatio]				$ctx=1
			jmp %returnaddress%
			%end%
						
		(codecave)DOF,DOF_cc:
			jne $$1														$ctx=1
			%end%
			
	]]

	if HackTool:CompileAssembly(asm,"Fixes") == 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()
	WriteAR()

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 WriteAR()	
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)		
	WriteAR()
	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
