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

--GAME VARS
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()

--ControlVars
bFixEnabled = true
bAspect=true
bDOF = true
bFrameGen = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "Outlaws.exe"
DLSS_G_DLL = "sl.dlss_g.dll"

--INJECTION BEHAVIOUR
InjectDelay = 3000
WriteInterval = 1000
SearchInterval = 1000
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.AddFixToggle("CKASPECTFix_Enable","Cinematics aspect fix","CKASPECTFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKDOFFix_Enable","Depth of field fix","CKDOFFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKFRAMEGENFix_Enable","Frame generation fix","CKFRAMEGENFix_Changed",255,141,180,14)

end

function Configure_SignatureScan() 
		
	local tAddress = HackTool:AddAddress("ASPECT")
	
	if HackTool:SignatureScan("48 83 ?? ?? ?? C3 ?? ?? ?? ?? 8B 02 89 01 C3 ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? 8B 02",tAddress,PAGE_EXECUTE_READ,0x1c,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Outlaws.exe+D8088F - CC                    - int 3 
		--Outlaws.exe+D80890 - 8B 02                 - mov eax,[rdx]
		--Outlaws.exe+D80892 - 89 01                 - mov [rcx],eax
		--Outlaws.exe+D80894 - C3                    - ret 
		--Outlaws.exe+D80895 - CC                    - int 3 
	end
	
	local tAddress = HackTool:AddAddress("DOF")
	
	if HackTool:SignatureScan("75 ?? 80 3D ?? ?? ?? ?? ?? 75 ?? 80 3D ?? ?? ?? ?? ?? 74 ?? 41 80 BC",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Outlaws.exe+18E6DDC - C5FE7F45 A0           - vmovdqu [rbp-60],ymm0
		--Outlaws.exe+18E6DE1 - 48 C7 45 C0 00000000  - mov qword ptr [rbp-40],00000000
		--Outlaws.exe+18E6DE9 - 75 66                 - jne Outlaws.exe+18E6E51
		--Outlaws.exe+18E6DEB - 80 3D 0927F706 00     - cmp byte ptr [Outlaws.exe+88594FB],00
		--Outlaws.exe+18E6DF2 - 75 09                 - jne Outlaws.exe+18E6DFD
	end
	
	local tAddress = HackTool:AddAddress("FRAMEGEN")
	
	if HackTool:SignatureScan("48 ?? ?? 74 ?? 48 8B ?? ?? ?? ?? ?? 48 ?? ?? ?? 0F 10 ?? ?? 0F 11 ?? 0F 10 ?? ?? 0F 11 ?? ?? 0F 10",tAddress,PAGE_EXECUTE_READ,0x1f,DLSS_G_DLL) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--sl.dlss_g.dll+3D325 - 0F 10 4A 28            - movups xmm1,[rdx+28]
		--sl.dlss_g.dll+3D329 - 0F 11 4F 10            - movups [rdi+10],xmm1
		--sl.dlss_g.dll+3D32D - 0F 10 42 38            - movups xmm0,[rdx+38]
		--sl.dlss_g.dll+3D331 - 0F 11 47 20            - movups [rdi+20],xmm0
		--sl.dlss_g.dll+3D335 - 0F 10 4A 48            - movups xmm1,[rdx+48]
	end
	
	return true
	
end

function Enable_Inject()

	local DLLAddr = HackTool:GetModuleAddress(DLSS_G_DLL)
	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("ScreenRatio")
	Variables:Allocate()
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	
	ResolutionChanged()	
		
	local asm = [[		
			
		(codecave:jmp)ASPECT,ASPECT_cc:
			cmp $$2,0x4018f5c3									$ctx=1
			jne exit
			mov $$2,[(allocation)Variables->ScreenRatio]		$ctx=1
		exit:
			%originalcode%
			%end%
			
			
		(codecave)DOF,DOF_cc:
			jmp $$1												$ctx=1
			%end%
					
	]]
	
	local asmdll = [[
	
		(codecave:jmp)FRAMEGEN,FRAMEGEN_cc:
			cmp r14,0xd
			jne next
			mov byte ptr [$$2],1								$ctx=1
		next:
			%originalcode%
			jmp %returnaddress%
			%end%
			
	]]
	
	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("DOF_cc",bDOF)
		Toggle_CodeCave("ASPECT_cc",bAspect)
	end	
			
	if HackTool:CompileAssembly(asmdll,"Framefix",DLLAddr) == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FRAMEGEN_cc",bFrameGen)
	end	
	
end

function Periodic()

end

function Disable_Inject()
	
	CleanUp()
	
end

function CKDOFFix_Changed(Sender)

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

end

function CKASPECTFix_Changed(Sender)

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

function CKFRAMEGENFix_Changed(Sender)

	bFrameGen = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FRAMEGEN_cc",bFrameGen)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
