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

--GAME VARS
fDefaultFOV = 85
fAdditionalFOV = 0
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fAspectRatio169 = 1.777777791

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

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "Empire-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("CKDOFFix_Enable","Depth of field fix","CKDOFFix_Changed",255,141,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("77 ?? 48 ?? ?? FF 90 ?? ?? ?? ?? F3 0F ?? ?? ?? 48 83",tAddress,PAGE_EXECUTE_READ,0x0b,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Empire-Win64-Shipping.exe+39E1F14 - 48 8B 01              - mov rax,[rcx]
		--Empire-Win64-Shipping.exe+39E1F17 - FF 90 50 07 00 00     - call qword ptr [rax+00000750]
		--Empire-Win64-Shipping.exe+39E1F1D - F3 0F 10 40 30        - movss xmm0,[rax+30]
		--Empire-Win64-Shipping.exe+39E1F22 - 48 83 C4 28           - add rsp,28
		--Empire-Win64-Shipping.exe+39E1F26 - C3                    - ret 
	end
	
	local tAddress = HackTool:AddAddress("ASPECT")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? 76 ?? F3 0F ?? ?? ?? ?? ?? ?? 48 83 ?? ?? 5B",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Empire-Win64-Shipping.exe+3033DB0 - F3 0F 10 8B 78 0A 00 00   - movss xmm1,[rbx+00000A78]
		--Empire-Win64-Shipping.exe+3033DB8 - 0F 2F CA               	- comiss xmm1,xmm2
		--Empire-Win64-Shipping.exe+3033DBB - F3 0F 11 83 4C 02 00 00  	- movss [rbx+0000024C],xmm0
		--Empire-Win64-Shipping.exe+3033DC3 - 76 08                 	- jna Empire-Win64-Shipping.exe+3033DCD
		--Empire-Win64-Shipping.exe+3033DC5 - F3 0F 11 8B 4C 02 00 00   - movss [rbx+0000024C],xmm1
	end
	
	local tAddress = HackTool:AddAddress("DOF")
	
	if HackTool:SignatureScan("8B ?? ?? 48 ?? ?? E8 ?? ?? ?? ?? 0F ?? ?? 48 6B ?? ?? 48 8D",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Empire-Win64-Shipping.exe+2464132 - 75 05                 - jne Empire-Win64-Shipping.exe+2464139
		--Empire-Win64-Shipping.exe+2464134 - BF 04 00 00 00        - mov edi,00000004
		--Empire-Win64-Shipping.exe+2464139 - 8B 3C 37              - mov edi,[rdi+rsi]
		--Empire-Win64-Shipping.exe+246413C - 48 8B CB              - mov rcx,rbx
		--Empire-Win64-Shipping.exe+246413F - E8 DC 5A 66 01        - call Empire-Win64-Shipping.exe+3AC9C20
	end
	
	return true
	
end

function Enable_Inject()
	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("CompensatedFOV")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("factor")
	
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["AspectRatio"]:WriteFloat(fAspectRatio169)
	Variables["factor"]:WriteFloat(fFactor)
	
	FOVCalculator = HackTool:InjectFOVCalculator("FOVCalculator")	
	
	ResolutionChanged()	
		
	local asm = [[		

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			$$0 [(allocation)Variables->FOVIn], $$1							$ctx=1
			fld dword [(allocation)Variables->FOVIn]
			call (allocation)FOVCalculator
			fstp dword ptr [(allocation)Variables->CompensatedFOV]
			$$0 $$1,[(allocation)Variables->CompensatedFOV]					$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]				$ctx=1
			$$0 [(allocation)Variables->FOVOut],$$1							$ctx=1			
			jmp %returnaddress%
			%end%
								
		(codecave:jmp)ASPECT,ASPECT_cc:
			$$0 $$2,[(allocation)Variables->ScreenRatio]					$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
						
		(codecave)DOF,DOF_cc:
			xor $$1,$$1														$ctx=1
			nop
			%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()
	
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()
		local fFOVCompensated = Variables["CompensatedFOV"]:ReadFloat()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %.2f, Compensated : %.2f, Out : %.2f", fFOVIn, fFOVCompensated, 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 CKDOFFix_Changed(Sender)

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

function ResolutionChanged() 

	SyncDisplayDetection()
	
	local CurrentAspectRatio = DisplayInfo:GetAspectRatio()
	local AspectDevisional = CurrentAspectRatio/fAspectRatio169

	if CurrentAspectRatio < 1.78 then
		AspectDevisional = 1.0
	end

	UpdateFOVCalculator("FOVCalculator",AspectDevisional,0.0)
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
