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

--GAME VARS
fDefaultFOV = 85
fAdditionalWorldFOV = 0
fAdditionalAimFOV = 0
fAdditionalCinematicsFOV = 0
fHUDScaling = 0
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fAspectRatio169 = 1.778

--ControlVars
bFixEnabled = true
bFOV = true
bHUD = true

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

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

--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_World_FOV","World FOV fine adjustment",15,70,210,17)		
	DefaultControls.AddFixedFOVSlider("WorldFOVSlider","WorldFOVSlider_Changed",55,100,125,35,0,60,0,1)
	WorldFOVSlider:SetTickFrequency(5)
	WorldFOVSlider:SetLabel1Text("0")
	WorldFOVSlider:SetLabel2Text("+60")
	
	DefaultControls.AddHeader("Header_Aim_FOV","Aim FOV fine adjustment",15,160,210,17)
	DefaultControls.AddFixedFOVSlider("AimFOVSlider","AimFOVSlider_Changed",55,190,125,35,0,80,0,1)
	AimFOVSlider:SetTickFrequency(5)
	AimFOVSlider:SetLabel1Text("-20")
	AimFOVSlider:SetLabel2Text("+60")
		
	DefaultControls.AddHeader("Header_Cinematics_FOV","Cinematics FOV fine adjustment",15,250,210,17)
	DefaultControls.AddFixedFOVSlider("CinematicsFOVSlider","CinematicsFOVSlider_Changed",55,280,125,35,0,60,0,1)
	CinematicsFOVSlider:SetTickFrequency(5)
	CinematicsFOVSlider:SetLabel1Text("0")
	CinematicsFOVSlider:SetLabel2Text("+60")
		
	DefaultControls.AddHeader("Header_HUD_Scaling","HUD scaling fine adjustment",15,340,210,17)
	DefaultControls.AddFixedFOVSlider("HUDScalingSlider","HUDScalingSlider_Changed",55,370,125,35,0,40,0,1)
	HUDScalingSlider:SetTickFrequency(5)
	HUDScalingSlider:SetLabel1Text("0%")
	HUDScalingSlider:SetLabel2Text("40%")
	
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKHUDFix_Enable","HUD Scaling fine fix","CKHUDFix_Changed",255,121,180,14)
	
end

function Configure_SignatureScan() 
	
	local tResultCount = HackTool:SignatureScanMulti("C5 FA 10 81 ?? ?? ?? ?? C3 CC CC CC CC CC CC CC C5 FA 10 81","AOB",PAGE_EXECUTE_READ,0x08,Process_EXEName)
	
	if tResultCount ~= 8 then	
		return ErrorOccurred("Could not find AOB 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("CinematicsFOV", HackTool:GetAddress( string.format("AOB%d",4) ))
		print( tAddress:GetInfo(TYPE_ADDRESS) )	
		--HorizonForbiddenWest.exe+99168E - CC                    	- int 3 
		--HorizonForbiddenWest.exe+99168F - CC                    	- int 3 
		--HorizonForbiddenWest.exe+991690 - C5 FA 10 81 64 02 00 00 - vmovss xmm0,[rcx+00000264]
		--HorizonForbiddenWest.exe+991698 - C3                    	- ret 
		--HorizonForbiddenWest.exe+991699 - CC                    	- int 3 
	end	
	
	local tAddress = HackTool:AddAddress("FOVS")
	
	if HackTool:SignatureScan("44 39 ?? ?? ?? ?? ?? 74 ?? C5 FA ?? ?? ?? ?? ?? ?? C5 F8",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--HorizonForbiddenWest.exe+118CB10 - EB 03                 	  - jmp HorizonForbiddenWest.exe+118CB15
		--HorizonForbiddenWest.exe+118CB12 - 49 8B C1              	  - mov rax,r9
		--HorizonForbiddenWest.exe+118CB15 - C5 FA 10 80 14 01 00 00  - vmovss xmm0,[rax+00000114]
		--HorizonForbiddenWest.exe+118CB1D - 44 39 8B D8 02 00 00     - cmp [rbx+000002D8],r9d
		--HorizonForbiddenWest.exe+118CB24 - 74 1A                 	  - je HorizonForbiddenWest.exe+118CB40
	end
 
	local tAddress = HackTool:AddAddress("HUD")
	
	if HackTool:SignatureScan("48 8B ?? ?? ?? ?? ?? 48 8D ?? ?? ?? ?? ?? 48 8B ?? FF ?? ?? C5 F8 ?? ?? ?? ?? ?? ?? C4",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--HorizonForbiddenWest.exe+143162F - C5 FA 10 3D 09 49 58 00    - vmovss xmm7,[HorizonForbiddenWest.exe+19B5F40]
		--HorizonForbiddenWest.exe+1431637 - EB 05                 		- jmp HorizonForbiddenWest.exe+143163E
		--HorizonForbiddenWest.exe+1431639 - C4 C1 78 28 F8            	- vmovaps xmm7,xmm8
		--HorizonForbiddenWest.exe+143163E - 48 8B 0D 2B 52 F0 00     	- mov rcx,[HorizonForbiddenWest.exe+2336870]
		--HorizonForbiddenWest.exe+1431645 - 48 8D 15 94 2B 3E 00     	- lea rdx,[HorizonForbiddenWest.exe+18141E0]
		--HorizonForbiddenWest.exe+143164C - 48 8B 01              		- mov rax,[rcx]
		--HorizonForbiddenWest.exe+143164F - FF 50 70              		- call qword ptr [rax+70]
	end

	return true
	
end

function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("WorldFOV")
	Variables:PushFloat("WorldFOVIn")
	Variables:PushFloat("WorldFOVOut")
	Variables:PushFloat("AimFOVIn")
	Variables:PushFloat("AimFOVOut")
	Variables:PushFloat("CinematicsFOVIn")
	Variables:PushFloat("CinematicsFOVOut")
	Variables:PushFloat("AdditionalWorldFOV")
	Variables:PushFloat("AdditionalAimFOV")
	Variables:PushFloat("AdditionalCinematicsFOV")
	Variables:PushFloat("HUDAspectRatio")
	Variables:Allocate()
	Variables["WorldFOV"]:WriteFloat(70)
	Variables["WorldFOVIn"]:WriteFloat(fDefaultFOV)
	Variables["WorldFOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AimFOVIn"]:WriteFloat(fDefaultFOV)
	Variables["AimFOVOut"]:WriteFloat(fDefaultFOV)
	Variables["CinematicsFOVIn"]:WriteFloat(fDefaultFOV)
	Variables["CinematicsFOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalWorldFOV"]:WriteFloat(fAdditionalWorldFOV)
	Variables["AdditionalAimFOV"]:WriteFloat(fAdditionalAimFOV)
	Variables["AdditionalCinematicsFOV"]:WriteFloat(fAdditionalCinematicsFOV)
	
	ResolutionChanged()	
				
	local asm = [[	

		(codecave:jmp)FOVS,FOVS_cc:
			comiss xmm0,[(allocation)Variables->WorldFOV]
			je worldFOV
			movss [(allocation)Variables->AimFOVIn],xmm0
			addss xmm0,[(allocation)Variables->AdditionalAimFOV]
			movss [(allocation)Variables->AimFOVOut],xmm0
			jmp exitFOVS
		worldFOV:
			movss [(allocation)Variables->WorldFOVIn],xmm0
			addss xmm0,[(allocation)Variables->AdditionalWorldFOV]
			movss [(allocation)Variables->WorldFOVOut],xmm0
		exitFOVS:
			%originalcode%
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)CinematicsFOV,CinematicsFOV_cc:
			test r13,r13
			jne exit
			movss [(allocation)Variables->CinematicsFOVIn],xmm0
			addss xmm0,[(allocation)Variables->AdditionalCinematicsFOV]
			movss [(allocation)Variables->CinematicsFOVOut],xmm0
		  exit:
			%originalcode%
			%end%
			
		(codecave:jmp)HUD,HUD_cc:
			movss xmm7,[(allocation)Variables->HUDAspectRatio]
			%originalcode%
			jmp %returnaddress%
			%end%
	]]

	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("CinematicsFOV_cc",bFOV)
		Toggle_CodeCave("FOVS_cc",bFOV)
		Toggle_CodeCave("HUD_cc",bHUD)
	end	
			
	Write_FOV()
	Write_HUD_Scaling()
	
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["CinematicsFOVIn"] and Variables["AdditionalWorldFOV"] then
		local fWorldFOVIn = Variables["WorldFOVIn"]:ReadFloat()
		local fWorldFOVOut = Variables["WorldFOVOut"]:ReadFloat()
		local fAimFOVIn = Variables["AimFOVIn"]:ReadFloat()
		local fAimFOVOut = Variables["AimFOVOut"]:ReadFloat()
		local fCinematicsFOVIn = Variables["CinematicsFOVIn"]:ReadFloat()
		local fCinematicsFOVOut = Variables["CinematicsFOVOut"]:ReadFloat()
		local HUDScalingAspect = Variables["HUDAspectRatio"]:ReadFloat()
		PluginViewport:AppendStatusMessage( string.format("===== Fix informations =====\r\n(World) FOV In: %.2f, FOV Out: %.2f -- (Aim) FOV In: %.2f, FOV Out: %.2f -- (Cinematics) FOV In: %.2f, FOV Out: %.2f\r\nHUD scaling aspect : %.2f", fWorldFOVIn, fWorldFOVOut, fAimFOVIn, fAimFOVOut, fCinematicsFOVIn, fCinematicsFOVOut, HUDScalingAspect))
	end
	
end

function WorldFOVSlider_Changed(Sender)

	fAdditionalWorldFOV = Sender:GetPosition()
	lblWorldFOVSlider.Caption:SetCaption( string.format("Value: +%.0f",fAdditionalWorldFOV) )
	Write_FOV()
	ForceUpdate()
	
end

function AimFOVSlider_Changed(Sender)

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

end

function CinematicsFOVSlider_Changed(Sender)

	fAdditionalCinematicsFOV = Sender:GetPosition()
	lblCinematicsFOVSlider.Caption:SetCaption( string.format("Value: +%.0f",fAdditionalCinematicsFOV) )
	Write_FOV()
	ForceUpdate()

end

function HUDScalingSlider_Changed(Sender)

	fHUDScaling = Sender:GetPosition()
	lblHUDScalingSlider.Caption:SetCaption( string.format("Value: %.0f%%",fHUDScaling) )
	Write_HUD_Scaling()
	ForceUpdate()

end

function Write_FOV()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AdditionalWorldFOV"] then
		if bFOV == true then
			Variables["AdditionalWorldFOV"]:WriteFloat(fAdditionalWorldFOV)
			Variables["AdditionalAimFOV"]:WriteFloat(fAdditionalAimFOV)
			Variables["AdditionalCinematicsFOV"]:WriteFloat(fAdditionalCinematicsFOV)
		else
			Variables["AdditionalWorldFOV"]:WriteFloat(0)
			Variables["AdditionalAimFOV"]:WriteFloat(0)
			Variables["AdditionalCinematicsFOV"]:WriteFloat(0)
		end
	end

end

function Write_HUD_Scaling()

	local width = DisplayInfo:GetWidth()
	local height = DisplayInfo:GetHeight()
	local HUDLeft = width * (fHUDScaling / 100)
	local HUDRight = width * (1 - (fHUDScaling / 100))
	local HUDWidth = HUDRight - HUDLeft
	local HUDAspectRatio = HUDWidth / height
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["HUDAspectRatio"] then
		Variables["HUDAspectRatio"]:WriteFloat(HUDAspectRatio)
	end
	
end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKHUDFix_Changed(Sender)

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

end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
