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

--GAME VARS
fAdditionalFOV = 0
fDefaultAspectRatio = 1.777777791

--ControlVars
bFixEnabled = true
bCSAspectRatio = true
bFOV = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "Marvel's Guardians of the Galaxy"
Process_ClassName = "ZSystemClass000"
Process_EXEName = "gotg.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","FOV Fine adjustment",15,70,210,17)	
	DefaultControls.AddFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35)
	DefaultControls.AddFixToggle("CKCSAspectFix_Enable","Cutscene Aspect Fix","CKCSAspectFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV Fix","CKFOVFix_Changed",255,101,180,54)

end

function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("CSAspectConstrain")
	--if HackTool:SignatureScan("F3 48 0F 2A ?? F3 48 0F 2A ?? 0F 28 ?? F3 0F ?? ?? 0F ?? ?? 76 ?? 44",tAddress,PAGE_EXECUTE_READ,0x14,Process_EXEName) == 0 then
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? F3 48 ?? ?? ?? F3 48 ?? ?? ?? 0F 28",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
	
	local tAddress = HackTool:AddAddress("FOV")
	if HackTool:SignatureScan("0F 28 C1 F3 0F 5C 41 10",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
	--if HackTool:SignatureScan("0F 54 05 81 E4 16 02",tAddress,PAGE_EXECUTE_READ,0x0,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 EXEAddr = HackTool:GetModuleAddress(Process_EXEName)
	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOV")
	Variables:PushFloat("Aspect")
	Variables:Allocate(EXEAddr)
	Variables["Aspect"]:WriteFloat(3.2)
	ResolutionChanged()	
	
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			movss xmm1,[(allocation)Variables->FOV]
			%originalcode%
			jmp %returnaddress%
			%end%					

		(codecave)CSAspectConstrain,CSAspectConstrain_cc:
			movss xmm0,[(allocation)Variables->Aspect]
			%end%
			
	]]

	if HackTool:CompileAssembly(asm,"FOVFix",EXEAddr) == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("CSAspectConstrain_cc",bCSAspectRatio)
		Toggle_CodeCave("FOV_cc",bFOV)
		if bFOV then
			WriteFOV()
		end
	end	
		
	
end

function Periodic()


end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = ((Sender:GetScaledFloat(1) + 100) / 1.6) + 25
	lblFOVSlider.Caption:SetCaption( string.format("Value: %.0f",fAdditionalFOV) )
	
	WriteFOV()	
	ForceUpdate()

end

function WriteFOV()

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

function Disable_Inject()
	
	CleanUp()
	
end

function CKCSAspectFix_Changed(Sender)

	bCSAspectRatio = Toggle_CheckFix(Sender)
	Toggle_CodeCave("CSAspectConstrain_cc",bCSAspectRatio)
	ForceUpdate()
	
end

function CKFOVFix_Changed(Sender)

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

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
