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

--GAME VARS
fDefaultAspectRatio = 1.78

--ControlVars
bFixEnabled = true
bDOF = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "Spider-Man2.exe"

--INJECTION BEHAVIOUR
InjectDelay = 500
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","HUD ar fine adjustment",15,70,210,17)	
	DefaultControls.AddFixToggle("CKDOFFix_Enable","Depth of field fix","CKDOFFix_Changed",255,101,180,14)

end

function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("DOF")
	if HackTool:SignatureScan("C5 FA ?? ?? ?? ?? ?? ?? C4 C1 ?? ?? ?? 0F 86 ?? ?? ?? ?? 44",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--"Spider-Man2.exe"+2CFED7F - 0F 8D 2F 01 00 00        - jnl "Spider-Man2.exe"+2CFEEB4
		--"Spider-Man2.exe"+2CFED85 - C6 86 3A 09 00 00 01     - mov byte ptr [rsi+0000093A],01
		--"Spider-Man2.exe"+2CFED8C - C5 FA 10 B7 D0 1B 00 00  - vmovss xmm6,[rdi+00001BD0]
		--"Spider-Man2.exe"+2CFED94 - C4 C1 78 2F F2           - vcomiss xmm6,xmm10
		--"Spider-Man2.exe"+2CFED99 - 0F 86 8D 00 00 00        - jbe "Spider-Man2.exe"+2CFEE2C
	end
		
	return true

end


function Enable_Inject() 
	
	ResolutionChanged()	
	
	local asm = [[	
	
		(codecave:jmp)DOF,DOF_cc:
			movss xmm6,[disabledDOF]
			jmp %returnaddress%
			%end%
			
		disabledDOF: (float)0
		
	]]

	RemoveAVXInstructions()
	
	if HackTool:CompileAssembly(asm,"MapAspectRatio") == nil then
		RestoreAVXInstructions()
		return ErrorOccurred("Assembly compilation failed...")
	else
		RestoreAVXInstructions()
		Toggle_CodeCave("DOF_cc",bDOF)
	end	
			
end

function Periodic()
end

function CKDOFFix_Changed(Sender)

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

end

function Disable_Inject()
	
	CleanUp()
	
end

function RemoveAVXInstructions()

	local tSuspendedThreadID = HackTool:SuspendAttachedThread() 						

	local tAddress = HackTool:GetAddress("DOF")
	tAddress:WriteByte(0xF3,0)
	tAddress:WriteByte(0x0F,1)

	HackTool:ResumeThread(tSuspendedThreadID)

end

function RestoreAVXInstructions()

	local tSuspendedThreadID = HackTool:SuspendAttachedThread()
	
	local tAddress = HackTool:GetAddress("DOF")
	tAddress:WriteByte(0xC5,0)
	tAddress:WriteByte(0xFA,1)

	
	HackTool:ResumeThread(tSuspendedThreadID)

end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
