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

--GAME VARS
fDefaultFOV = 85
fAdditionalFOV = 0
fAdditionalOtherFOV = 0
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fAspectRatio169 = 1.778
fFactor = 0.00872665

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

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "TheElysianWoods-Win64-Shipping.exe"
-- LOTF2-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)
	FOVSlider:SetLabel1Text("-20")
	FOVSlider:SetLabel2Text("+50")

	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 disable","CKDOFFix_Changed",255,141,180,14)
	DefaultControls.AddFixToggle("CKFRINGEFix_Enable","Chromatic aberration fix","CKFRINGEFix_Changed",255,161,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? 48 ?? ?? ?? 48 ?? ?? 4C ?? ?? ?? 48 ?? ?? FF",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--TheElysianWoods-Win64-Shipping.exe+3A670DB - 48 8B 01              - mov rax,[rcx]
		--TheElysianWoods-Win64-Shipping.exe+3A670DE - FF 90 80 07 00 00     - call qword ptr [rax+00000780]
		--TheElysianWoods-Win64-Shipping.exe+3A670E4 - F3 0F 11 47 30        - movss [rdi+30],xmm0
		--TheElysianWoods-Win64-Shipping.exe+3A670E9 - 48 8B 4D 30           - mov rcx,[rbp+30]
		--TheElysianWoods-Win64-Shipping.exe+3A670ED - 48 8B 01              - mov rax,[rcx]
	end
	
	local tAddress = HackTool:AddAddress("ASPECT")
	
	if HackTool:SignatureScan("8B 83 ?? ?? ?? ?? 89 ?? ?? 0F B6 ?? ?? ?? ?? ?? 33 ?? ?? 83",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--TheElysianWoods-Win64-Shipping.exe+36FAB57 - F3 0F 11 47 30        - movss [rdi+30],xmm0
		--TheElysianWoods-Win64-Shipping.exe+36FAB5C - 0F 57 C0              - xorps xmm0,xmm0
		--TheElysianWoods-Win64-Shipping.exe+36FAB5F - 8B 83 B0 02 00 00     - mov eax,[rbx+000002B0]
		--TheElysianWoods-Win64-Shipping.exe+36FAB65 - 89 47 48              - mov [rdi+48],eax
		--TheElysianWoods-Win64-Shipping.exe+36FAB68 - 0F B6 83 B4 02 00 00  - movzx eax,byte ptr [rbx+000002B4]
	end
	
	local tAddress = HackTool:AddAddress("DOF")
	
	if HackTool:SignatureScan("48 8B ?? 8B ?? ?? E8 ?? ?? ?? ?? 48 ?? ?? 48 ?? ?? ?? 48 8D",tAddress,PAGE_EXECUTE_READ,0x03,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--TheElysianWoods-Win64-Shipping.exe+25079E4 - 48 8B 05 FD ED F0 03  - mov rax,[TheElysianWoods-Win64-Shipping.exe+64167E8]
		--TheElysianWoods-Win64-Shipping.exe+25079EB - 48 8B CB              - mov rcx,rbx
		--TheElysianWoods-Win64-Shipping.exe+25079EE - 8B 78 04              - mov edi,[rax+04]
		--TheElysianWoods-Win64-Shipping.exe+25079F1 - E8 AA 68 80 01        - call TheElysianWoods-Win64-Shipping.exe+3D0E2A0
		--TheElysianWoods-Win64-Shipping.exe+25079F6 - 48 63 C8              - movsxd  rcx,eax
	end
		
	local tAddress = HackTool:AddAddress("FRINGE")
	
	if HackTool:SignatureScan("7F ?? 89 B3 ?? ?? ?? ?? 8B ?? ?? 39 05",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--TheElysianWoods-Win64-Shipping.exe+3D0BD85 - 48 8B 05 54 98 7D 02  - mov rax,[TheElysianWoods-Win64-Shipping.exe+64E55E0]
		--TheElysianWoods-Win64-Shipping.exe+3D0BD8C - 39 30                 - cmp [rax],esi
		--TheElysianWoods-Win64-Shipping.exe+3D0BD8E - 7F 06                 - jg TheElysianWoods-Win64-Shipping.exe+3D0BD96
		--TheElysianWoods-Win64-Shipping.exe+3D0BD90 - 89 B3 DC 1D 00 00     - mov [rbx+00001DDC],esi
		--TheElysianWoods-Win64-Shipping.exe+3D0BD96 - 8B 04 2F              - mov eax,[rdi+rbp]
	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("AspectRatio")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("factor")
	
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["AspectRatio"]:WriteFloat(fAspectRatio169)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["factor"]:WriteFloat(fFactor)
	
	ResolutionChanged()	
				
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			$$0 [(allocation)Variables->FOVIn],$$2					$ctx=1
			fld dword ptr [(allocation)Variables->FOVIn]
			fmul dword ptr [(allocation)Variables->factor]
			fptan
			fstp st0
			fld dword ptr [(allocation)Variables->ScreenRatio]
			fdiv dword ptr [(allocation)Variables->AspectRatio]
			fmulp st1,st0
		    fld1
		    fpatan
			fdiv dword ptr [(allocation)Variables->factor]						; calculate compensated FOV
			fstp dword ptr [(allocation)Variables->CompensatedFOV]				; and retrieve it			
			movss $$2,[(allocation)Variables->CompensatedFOV]		$ctx=1
			addss $$2,[(allocation)Variables->AdditionalFOV]		$ctx=1
			movss [(allocation)Variables->FOVOut],$$2				$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
		
		(codecave:jmp)ASPECT,ASPECT_cc:
			fld dword ptr [$$2]										$ctx=1
			fstp dword ptr[(allocation)Variables->AspectRatio]
			$$0 $$1,[(allocation)Variables->ScreenRatio]			$ctx=1
			jmp %returnaddress%
			%end%
			
		(codecave)DOF,DOF_cc:
			xor $$1,$$1												$ctx=1
			nop
			%end%
			
		(codecave)FRINGE,FRINGE_cc:
			nop
			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)
		Toggle_CodeCave("FRINGE_cc",bFringe)
	end	
			
	Write_FOV()
	Write_AR()
	
end

function Periodic()
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		local fFOVIn = Variables["FOVIn"]:ReadFloat()
		local fCompensatedFOV = Variables["CompensatedFOV"]:ReadFloat()
		local fFOVOut = Variables["FOVOut"]:ReadFloat()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %.2f, Compensated FOV : %.2f, FOV Out : %.2f", fFOVIn, fCompensatedFOV, 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 Write_AR()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AspectRatio"] then
		if bAspect ~= true then
			Variables["AspectRatio"]:WriteFloat(fAspectRatio)
		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 CKARFix_Changed(Sender)

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

function CKDOFFix_Changed(Sender)

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

end

function CKFRINGEFix_Changed(Sender)

	bFringe = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FRINGE_cc",bFringe)
	ForceUpdate()

end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
