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

--GAME VARS
fDefaultAspectRatio = 1.78

--ControlVars
bFixEnabled = true
bMapAspectRatio = true
bHUDAspectRatio = true
bFOV = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "Spider-Man.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.AddFOVSlider("HUDSlider","HUDSlider_Changed",55,100,125,35)
	DefaultControls.AddFixToggle("CKHUDAspectFix_Enable","HUD aspect fix","CKCSAspectFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKMapAspectFix_Enable","City map fix","CKMapFix_Changed",255,101,180,54)

end

function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("MapAspectRatio")
	if HackTool:SignatureScan("4C 8B 00 F3 0F 10 1D ?? ?? ?? ?? F3 0F 11",tAddress,PAGE_EXECUTE_READ,0x03,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
		
	local tAddress = HackTool:AddAddress("GetGameRes")
	
	if HackTool:SignatureScan("44 0F 2E 8B ?? ?? ?? ?? 7A ?? 75 ?? 0F 2E",tAddress,PAGE_EXECUTE_READ,0x00,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end

	local tAddress = HackTool:AddAddress("HUDAspectRatio")
	if HackTool:SignatureScan("E8 ?? ?? ?? ?? F3 0F 2C C0 0F B6 C8 83 E9",tAddress,PAGE_EXECUTE_READ,0x0f,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end

	return true

end


function Enable_Inject() 
	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("HUD_Map_Aspect_Ratio")
	Variables:PushFloat("Map_Aspect_Ratio")
	Variables:PushFloat("Win_Width")
	Variables:PushFloat("Win_Height")
	Variables:Allocate()
	Variables["Map_Aspect_Ratio"]:WriteFloat(fDefaultAspectRatio)
	
	ResolutionChanged()	
	
	local asm = [[	
	
		(codecave:jmp)MapAspectRatio,MapAspectRatio_cc:
			movss xmm3,[(allocation)Variables->Map_Aspect_Ratio]
			jmp %returnaddress%
			%end%

		(codecave:jmp)GetGameRes,GetGameRes_cc:
			ucomiss xmm9,[$$2]								$ctx=1
			pushf
			cmp r10,0x438
			je return
			fld dword ptr [rbx+0xA8]
			fstp dword ptr [(allocation)Variables->Win_Width]
			fld dword ptr [rbx+0xAC]
			fstp dword ptr [(allocation)Variables->Win_Height]
			return:
			popf
			jmp %returnaddress%
			%end%
			
			;	Spider-Man.exe+31816CA - 75 2E                 - jne Spider-Man.exe+31816FA
			;	Spider-Man.exe+31816CC - 44 0F2E 8B A0000000   - ucomiss xmm9,[rbx+000000A0]
			;	Spider-Man.exe+31816D4 - 7A 24                 - jp Spider-Man.exe+31816FA
			
		(codecave:jmp)HUDAspectRatio,HUDAspectRatio_cc:
			movss xmm0,[(allocation)Variables->HUD_Map_Aspect_Ratio]
			movaps xmm6,[rsp+0x20]
			add rsp,0x30
			pop rbx
			ret
			%end%
		
	]]

	if HackTool:CompileAssembly(asm,"MapAspectRatio") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("MapAspectRatio_cc",bMapAspectRatio)
		Toggle_CodeCave("GetGameRes_cc",bMapAspectRatio)
		Toggle_CodeCave("HUDAspectRatio_cc",bHUDAspectRatio)
	end	
			
end

function Periodic()
	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["Win_Width"] and Variables["Map_Aspect_Ratio"] then
		local window_width = Variables["Win_Width"]:ReadFloat()
		local window_height = Variables["Win_Height"]:ReadFloat()
		local window_Map_Aspect_Ratio = window_width / window_height
		Variables["Map_Aspect_Ratio"]:WriteFloat(window_Map_Aspect_Ratio)
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nGame width : %.0f - height : %0.f - aspect ratio : %.2f", window_width, window_height, window_Map_Aspect_Ratio))
	end
end

function HUDSlider_Changed(Sender)

	fHUDAspectRatio = ((Sender:GetScaledFloat(2) + 50) / 10) + 1.78
	lblHUDSlider.Caption:SetCaption( string.format("HUD aspect ratio : %.2f",fHUDAspectRatio) )
	
	WriteHUDAR()	
	ForceUpdate()

end

function WriteHUDAR()

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

function Disable_Inject()
	
	CleanUp()
	
end

function CKCSAspectFix_Changed(Sender)

	bHUDAspectRatio = Toggle_CheckFix(Sender)
	Toggle_CodeCave("HUDAspectRatio_cc",bHUDAspectRatio)
	ForceUpdate()
	
end

function CKMapFix_Changed(Sender)

	bMapAspectRatio = Toggle_CheckFix(Sender)
	Toggle_CodeCave("MapAspectRatio_cc",bMapAspectRatio)
	Toggle_CodeCave("GetGameRes_cc",bMapAspectRatio)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
