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

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

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

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "MetroExodus.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_FOV2","Cutscenes FOV fine adjustment",15,160,210,17)	
	DefaultControls.AddFixedFOVSlider("CinematicsFOVSlider","CinematicsFOVSlider_Changed",55,190,125,35,0,60,0,1)
	CinematicsFOVSlider:SetTickFrequency(5)
	CinematicsFOVSlider:SetLabel1Text("-20")
	CinematicsFOVSlider:SetLabel2Text("+40")
	

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKCinematicsFOVFix_Enable","Cutscenes FOV fix","CKCinematicsFOVFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKARFix_Enable","Aspect ratio fix","CKARFix_Changed",255,141,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("3C ?? 0F 86 ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x08,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--MetroExodus.exe+55AF38 - 3C 01                 	 - cmp al,01
		--MetroExodus.exe+55AF3A - 0F 86 65 01 00 00         - jbe MetroExodus.exe+55B0A5
		--MetroExodus.exe+55AF40 - F3 0F 10 15 CC FD F9 00   - movss xmm2,[MetroExodus.exe+14FAD14]
		--MetroExodus.exe+55AF48 - F3 0F 10 0D 84 9C 0F 01   - movss xmm1,[MetroExodus.exe+1654BD4]
		--MetroExodus.exe+55AF50 - F3 0F 59 0D C8 A0 FA 00   - mulss xmm1,[MetroExodus.exe+1505020]
	end

	local tAddress = HackTool:AddAddress("FOV2")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? E9 96 ?? ?? ?? 48 8B ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--MetroExodus.exe+55B0A3 - 5E                    	- pop rsi
		--MetroExodus.exe+55B0A4 - C3                    	- ret 
		--MetroExodus.exe+55B0A5 - F3 0F 10 15 B3 61 E7 00  - movss xmm2,[MetroExodus.exe+13D1260]
		--MetroExodus.exe+55B0AD - E9 96 FE FF FF           - jmp MetroExodus.exe+55AF48
		--MetroExodus.exe+55B0B2 - 48 8B CB              	- mov rcx,rbx
	end

	local tAddress = HackTool:AddAddress("ASPECT")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 89 84 ?? ?? ?? ?? ?? 8B",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--MetroExodus.exe+5D0EF1 - 4C 8B FA              	- mov r15,rdx
		--MetroExodus.exe+5D0EF4 - 44 8B 2D A1 EF 07 01     - mov r13d,[MetroExodus.exe+164FE9C]
		--MetroExodus.exe+5D0EFB - F3 0F 10 35 B9 EF 07 01  - movss xmm6,[MetroExodus.exe+164FEBC]
		--MetroExodus.exe+5D0F03 - F3 0F 10 3D B5 EF 07 01  - movss xmm7,[MetroExodus.exe+164FEC0]
		--MetroExodus.exe+5D0F0B - 89 84 24 A0 00 00 00     - mov [rsp+000000A0],eax
	end

	local tAddress = HackTool:AddAddress("ASPECT2")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? 0F ?? ?? 48 8D ?? ?? ?? ?? ?? F3 48 ?? ?? ?? 49 8D",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--MetroExodus.exe+DB0C9B - F3 0F 10 15 19 F2 89 00  - movss xmm2,[MetroExodus.exe+164FEBC]
		--MetroExodus.exe+DB0CA3 - EB 08                 	- jmp MetroExodus.exe+DB0CAD
		--MetroExodus.exe+DB0CA5 - F3 0F 10 15 0B F2 89 00  - movss xmm2,[MetroExodus.exe+164FEB8]
		--MetroExodus.exe+DB0CAD - 0F 57 C0               	- xorps xmm0,xmm0
		--MetroExodus.exe+DB0CB0 - 48 8D 35 B9 00 53 00     - lea rsi,[MetroExodus.exe+12E0D70]
	end
	
	return true
end

function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("CompensatedFOV")
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("CinematicsFOVIn")
	Variables:PushFloat("CinematicsFOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AdditionalCinematicsFOV")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("factor")
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["CinematicsFOVIn"]:WriteFloat(fDefaultFOV)
	Variables["CinematicsFOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["AdditionalCinematicsFOV"]:WriteFloat(fAdditionalCinematicsFOV)
	Variables["AspectRatio"]:WriteFloat(fAspectRatio169)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["factor"]:WriteFloat(fFactor)
	
	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)FOV2,FOV2_cc:
			%originalcode%
			$$0 [(allocation)Variables->CinematicsFOVIn],$$1					$ctx=1
			addss $$1,[(allocation)Variables->AdditionalCinematicsFOV]			$ctx=1
			$$0 [(allocation)Variables->CinematicsFOVOut],$$1					$ctx=1
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)ASPECT,ASPECT_cc:
			$$0 $$1,[(allocation)Variables->ScreenRatio]						$ctx=1
			jmp %returnaddress%
			%end%
						
		(codecave:jmp)ASPECT2,ASPECT2_cc:
			$$0 $$1,[(allocation)Variables->ScreenRatio]						$ctx=1
			jmp %returnaddress%
			%end%
			
	]]

	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("FOV2_cc",bCinematicsFOV)
		Toggle_CodeCave("ASPECT_cc",bAspect)
		Toggle_CodeCave("ASPECT2_cc",bAspect)
	end	
			
	Write_FOV()
	Write_CinematicsFOV()
	
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()
		local fCinematicsFOVIn = Variables["CinematicsFOVIn"]:ReadFloat()
		local fCinematicsFOVOut = Variables["CinematicsFOVOut"]:ReadFloat()

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

function FOVSlider_Changed(Sender)

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

end

function CinematicsFOVSlider_Changed(Sender)

	fAdditionalCinematicsFOV = Sender:GetPosition() - 20
	lblCinematicsFOVSlider.Caption:SetCaption( string.format("Value: +%.0f",fAdditionalCinematicsFOV) )
		
	Write_CinematicsFOV()
	
	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_CinematicsFOV()

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

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKCinematicsFOVFix_Changed(Sender)

	bCinematicsFOV = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FOV2_cc",bCinematicsFOV)
	Write_CinematicsFOV()
	ForceUpdate()
	
end

function CKARFix_Changed(Sender)

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