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

--GAME VARS
fDefaultFOV = 85
fAdditionalFOV = 0
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fHUD_Left_Max = (DisplayInfo:GetWidth() - ((DisplayInfo:GetHeight() * 16) / 9)) / 2
fHUD_Top_Max = 200
fHUD_Right_Max = fHUD_Left_Max
fHUD_Bottom_Max = 200
fHUD_Left_Min = -250
fHUD_Top_Min = -60
fHUD_Right_Min = -250
fHUD_Bottom_Min = -60

--ControlVars
bFixEnabled = true
bFOV = true
bAspect = true
bHUD = true
bDOF = true
bFog = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "U9-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","In game FOV fine adjustment",15,70,210,17)	
	DefaultControls.AddFixedFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35,0,70,0,1)
	FOVSlider:SetTickFrequency(10)

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKASPECTFix_Enable","Aspect ratio fix","CKASPECTFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKHUDFix_Enable","Unlock HUD scaling","CKHUDFix_Changed",255,141,180,14)
	DefaultControls.AddFixToggle("CKDOFFix_Enable","Depth of field fix","CKDOFFix_Changed",255,161,180,14)
	DefaultControls.AddFixToggle("CKFOGFix_Enable","Fog removal fix","CKFOGFix_Changed",255,181,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("77 ?? 48 ?? ?? FF 90 ?? ?? ?? ?? F3 0F ?? ?? ?? 48",tAddress,PAGE_EXECUTE_READ,0x0b,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--U9-Win64-Shipping.exe+3285704 - 48 8B 01              - mov rax,[rcx]
		--U9-Win64-Shipping.exe+3285707 - FF 90 80 06 00 00     - call qword ptr [rax+00000680]
		--U9-Win64-Shipping.exe+328570D - F3 0F 10 40 18        - movss xmm0,[rax+18]
		--U9-Win64-Shipping.exe+3285712 - 48 83 C4 28           - add rsp,28
		--U9-Win64-Shipping.exe+3285716 - C3                    - ret 
	end
	
	local tAddress = HackTool:AddAddress("ASPECT")
	
	if HackTool:SignatureScan("8B 83 ?? ?? ?? ?? 89 ?? ?? 0F B6 ?? ?? ?? ?? ?? 33 ?? ?? 83 ?? ?? 31 ?? ?? 0F B6",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--U9-Win64-Shipping.exe+2EA8E22 - F3 0F 10 83 F8 01 00 00   - movss xmm0,[rbx+000001F8]
		--U9-Win64-Shipping.exe+2EA8E2A - F3 0F 11 47 18         	- movss [rdi+18],xmm0
		--U9-Win64-Shipping.exe+2EA8E2F - 8B 83 08 02 00 00        	- mov eax,[rbx+00000208]
		--U9-Win64-Shipping.exe+2EA8E35 - 89 47 2C              	- mov [rdi+2C],eax
		--U9-Win64-Shipping.exe+2EA8E38 - 0F B6 83 0C 02 00 00      - movzx eax,byte ptr [rbx+0000020C]
	end
	
	local tResultCount = HackTool:SignatureScanMulti("F3 0F ?? ?? ?? ?? ?? ?? 73 ?? F3 0F ?? ?? ?? ?? 0F ?? ?? EB ?? 0F ?? ?? 72 ?? F3 0F ?? ?? ?? ?? 0F ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 0F ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 73 ?? F3 0F","HUDS",PAGE_EXECUTE_READ,0x0,Process_EXEName)
	
	if tResultCount ~= 4 then
		return ErrorOccurred("Could not find HUDS 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("HUD", HackTool:GetAddress( string.format("HUDS%d",3) ))
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--U9-Win64-Shipping.exe+3999866 - F3 0F 10 99 38 04 00 00   - movss xmm3,[rcx+00000438]
		--U9-Win64-Shipping.exe+399986E - 0F 2F C3               	- comiss xmm0,xmm3
		--U9-Win64-Shipping.exe+3999871 - F3 0F 10 91 28 04 00 00   - movss xmm2,[rcx+00000428]
		--U9-Win64-Shipping.exe+3999879 - 73 0B                 	- jae U9-Win64-Shipping.exe+3999886
		--U9-Win64-Shipping.exe+399987B - F3 0F 11 5C 24 20      	- movss [rsp+20],xmm3
	end
	
	local tAddress = HackTool:AddAddress("DOF") -- r.DepthOfFieldQuality
	
	if HackTool:SignatureScan("8B ?? ?? E8 ?? ?? ?? ?? 8B ?? E8 ?? ?? ?? ?? 84 ?? 74 ?? 48 ?? ?? F7",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--U9-Win64-Shipping.exe+1E8FF20 - 48 8B 05 C1 35 5B 04  - mov rax,[U9-Win64-Shipping.exe+64434E8]
		--U9-Win64-Shipping.exe+1E8FF27 - 48 8B CB              - mov rcx,rbx
		--U9-Win64-Shipping.exe+1E8FF2A - 8B 78 04              - mov edi,[rax+04]
		--U9-Win64-Shipping.exe+1E8FF2D - E8 3E 4E 48 01        - call U9-Win64-Shipping.exe+3314D70
		--U9-Win64-Shipping.exe+1E8FF32 - 8B C8                 - mov ecx,eax
	end
	
	local tAddress = HackTool:AddAddress("FOG") -- r.fog
	
	if HackTool:SignatureScan("75 ?? B3 ?? EB ?? 32 ?? 48 ?? ?? ?? ?? 48 ?? ?? 74 ?? E8 ?? ?? ?? ?? 0F",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--U9-Win64-Shipping.exe+1CFC14D - 48 8B 05 24 17 6F 04  - mov rax,[U9-Win64-Shipping.exe+63ED878]
		--U9-Win64-Shipping.exe+1CFC154 - 83 78 04 01           - cmp dword ptr [rax+04],01
		--U9-Win64-Shipping.exe+1CFC158 - 75 04                 - jne U9-Win64-Shipping.exe+1CFC15E
		--U9-Win64-Shipping.exe+1CFC15A - B3 01                 - mov bl,01
		--U9-Win64-Shipping.exe+1CFC15C - EB 02                 - jmp U9-Win64-Shipping.exe+1CFC160
	end

	return true
	
end

function Enable_Inject()
	local Variables = HackTool:AllocateMemory("Variables",50)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("ScreenRatio")

	Variables:PushFloat("Hud_Left_Max")
	Variables:PushFloat("Hud_Top_Max")
	Variables:PushFloat("Hud_Right_Max")
	Variables:PushFloat("Hud_Bottom_Max")
	Variables:PushFloat("Hud_Left_Min")
	Variables:PushFloat("Hud_Top_Min")
	Variables:PushFloat("Hud_Right_Min")
	Variables:PushFloat("Hud_Bottom_Min")
	
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["Hud_Left_Max"]:WriteFloat(fHUD_Left_Max)
	Variables["Hud_Top_Max"]:WriteFloat(fHUD_Top_Max)
	Variables["Hud_Right_Max"]:WriteFloat(fHUD_Right_Max)
	Variables["Hud_Bottom_Max"]:WriteFloat(fHUD_Bottom_Max)
	Variables["Hud_Left_Min"]:WriteFloat(fHUD_Left_Min)
	Variables["Hud_Top_Min"]:WriteFloat(fHUD_Top_Min)
	Variables["Hud_Right_Min"]:WriteFloat(fHUD_Right_Min)
	Variables["Hud_Bottom_Min"]:WriteFloat(fHUD_Bottom_Min)
	
	ResolutionChanged()	
		
	local asm = [[		

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			$$0 [(allocation)Variables->FOVIn],$$1							$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]				$ctx=1
			$$0 [(allocation)Variables->FOVOut],$$1							$ctx=1
			jmp %returnaddress%
			%end%
								
		(codecave:jmp)HUD,HUD_cc:
			movups $$1,[(allocation)Variables->Hud_Left_Max]				$ctx=1
			movups [$$2],$$1												$ctx=1
			movups $$1,[(allocation)Variables->Hud_Left_Min]				$ctx=1
			movups [$$2+0x10],$$1											$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
		
		(codecave)ASPECT,ASPECT_cc:
			$$0 $$1,[(allocation)Variables->ScreenRatio]					$ctx=1
			%end%
			
		(codecave)DOF,DOF_cc:
			xor $$1,$$1														$ctx=1
			nop
			%end%
			
		(codecave)FOG,FOG_cc:
			jmp $$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("HUD_cc",bHUD)
		Toggle_CodeCave("DOF_cc",bDOF)
		Toggle_CodeCave("FOG_cc",bFog)
	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: %.2f, Out : %.2f", fFOVIn, fFOVOut))
	end
	
end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetPosition() - 20
	lblFOVSlider.Caption:SetCaption( string.format("World FOV: %.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 CKASPECTFix_Changed(Sender)

	bAspect = Toggle_CheckFix(Sender)
	Toggle_CodeCave("ASPECT_cc",bAspect)
	ForceUpdate()
	
end

function CKHUDFix_Changed(Sender)

	bHUD = Toggle_CheckFix(Sender)
	Toggle_CodeCave("HUD_cc",bHUD)
	ForceUpdate()

end

function CKDOFFix_Changed(Sender)

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

function CKFOGFix_Changed(Sender)

	bFog = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FOG_cc",bFog)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
