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

--GAME VARS
fDefaultFOV = 0
fDefaultAspectRatio = 1.7777778
fAdditionalFOV = 0
fFOV = 0
fSharpening = 0

--ControlVars
bFixEnabled = true
bFOV = true
bTAA = true
bSharpening = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "tlou-i.exe;tlou-i-l.exe"

--INJECTION BEHAVIOUR
InjectDelay = 100
WriteInterval = 500
SearchInterval = 500
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.AddFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35)
	DefaultControls.AddFixedFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35,0,60,0,1)
	FOVSlider:SetTickFrequency(5)
	FOVSlider:SetLabel1Text("-20")
	FOVSlider:SetLabel2Text("+40")

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKTAAFix_Enable","TAA disabling","CKTAAFix_Changed",255,121,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("C5 F8 ?? ?? ?? ?? ?? ?? ?? 88 83 ?? ?? ?? ?? C5 F8",tAddress,PAGE_EXECUTE_READ,0x09,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		--tlou-i.exe+59EDFF - C5 FC 11 83 D0 02 00 00    	- vmovups [rbx+000002D0],ymm0
		--tlou-i.exe+59EE07 - C5 F8 10 84 24 A4 00 00 00 	- vmovups xmm0,[rsp+000000A4]
		--tlou-i.exe+59EE10 - 88 83 F0 02 00 00        		- mov [rbx+000002F0],al
		--tlou-i.exe+59EE16 - C5 F8 11 83 F4 02 00 00    	- vmovups [rbx+000002F4],xmm0
		--tlou-i.exe+59EE1E - C5 F8 10 05 EA 02 8D 02    	- vmovups xmm0,[tlou-i.exe+2E6F110]
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
	
	local tAddress = HackTool:AddAddress("TAA1")
	
	if HackTool:SignatureScan("41 ?? ?? E8 ?? ?? ?? ?? 4C ?? ?? ?? 38 98 ?? ?? ?? ?? 74 ?? 49 8D ?? ?? ?? ?? ?? E8 ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x22,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		--tlou-i.exe+1193323 - E8 FC 45 FF FF           - call tlou-i.exe+1187924
		--tlou-i.exe+1193328 - 84 C0                 	- test al,al
		--tlou-i.exe+119332A - 74 08                 	- je tlou-i.exe+1193334
		--tlou-i.exe+119332C - 41 8A C7              	- mov al,r15l
		--tlou-i.exe+119332F - EB 05                 	- jmp tlou-i.exe+1193336
		print( tAddress:GetInfo(TYPE_ADDRESS) )	
	end
	
	local tAddress = HackTool:AddAddress("TAA2")
	
	if HackTool:SignatureScan("41 ?? ?? E8 ?? ?? ?? ?? 4C ?? ?? ?? 38 98 ?? ?? ?? ?? 74 ?? 49 8D ?? ?? ?? ?? ?? E8 ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x24,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		--tlou-i.exe+119332C - 41 8A C7              	- mov al,r15l
		--tlou-i.exe+119332F - EB 05                 	- jmp tlou-i.exe+1193336
		--tlou-i.exe+1193331 - 44 8A FB              	- mov r15l,bl
		--tlou-i.exe+1193334 - 8A C3                 	- mov al,bl
		--tlou-i.exe+1193336 - 41 88 86 91 5A 01 00     - mov [r14+00015A91],al
		print( tAddress:GetInfo(TYPE_ADDRESS) )	
	end
	
	local tAddress = HackTool:AddAddress("TAA3")
	
	if HackTool:SignatureScan("C4 63 ?? ?? ?? ?? C5 ?? ?? ?? E8 ?? ?? ?? ?? 44 0F ?? ??",tAddress,PAGE_EXECUTE_READ,0x0f,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		--tlou-i.exe+402385 - C5 C0 57 FF            - vxorps xmm7,xmm7,xmm7
		--tlou-i.exe+402389 - E8 F2 28 AD 01         - call tlou-i.exe+1ED4C80
		--tlou-i.exe+40238E - 44 0F B6 F8            - movzx r15d,al
		--tlou-i.exe+402392 - C5 C8 57 F6            - vxorps xmm6,xmm6,xmm6
		--tlou-i.exe+402396 - 84 C0                  - test al,al
		print( tAddress:GetInfo(TYPE_ADDRESS) )	
	end
	
	return true
end


function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("Sharpening")
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	
	ResolutionChanged()	
	
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			pushf
			vmovss xmm15,xmm0,xmm0									; Save FOV In
			vaddss xmm0,xmm0,[(allocation)Variables->AdditionalFOV]
			cmp r14,1
			jne exit
			vmovss [(allocation)Variables->FOVIn],xmm15
			vmovss [(allocation)Variables->FOVOut],xmm0
			
		exit:
			popf
			jmp %returnaddress%
			%end%			
			
		(codecave)TAA1,TAA1_cc:
			nop
			nop
			%end%	
			
		(codecave)TAA2,TAA2_cc:
			xor al,al
			nop
			%end%
			
		(codecave)TAA3,TAA3_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("TAA1_cc",bTAA)
		Toggle_CodeCave("TAA2_cc",bTAA)
		Toggle_CodeCave("TAA3_cc",bTAA)
	end	
			
	Write_FOV()
	
end

function Periodic()
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		fFOVIn = Variables["FOVIn"]:ReadFloat()
		fFOVOut = Variables["FOVOut"]:ReadFloat()
		if fFOVIn < 160 then
			PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %.2f, FOV Out : %.2f", fFOVIn, fFOVOut))
		else
			PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: 0, FOV Out : 0", fFOVIn, fFOVOut))
		end
	end
end

function FOVSlider_Changed(Sender)

	fFOV = Sender:GetPosition() - 20 --(Sender:GetScaledFloat(3)) + 18.33
	if fFOV > 0 then
		lblFOVSlider.Caption:SetCaption( string.format("Value: +%.2f",fFOV) )
	else
		lblFOVSlider.Caption:SetCaption( string.format("Value: %.2f",fFOV) )	
	end
	Write_FOV()
	ForceUpdate()
	
end

function Write_FOV()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AdditionalFOV"] then
		Variables["AdditionalFOV"]:WriteFloat(fFOV)
	end

end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKTAAFix_Changed(Sender)

	bTAA = Toggle_CheckFix(Sender)
	Toggle_CodeCave("TAA1_cc",bTAA)
	Toggle_CodeCave("TAA2_cc",bTAA)
	Toggle_CodeCave("TAA3_cc",bTAA)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
