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

--GAME VARS
fDefaultFOV = 0
fFOV = 0

--ControlVars
bFixEnabled = true
bFOV = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "Returnal-Win64-Shipping.exe"
Process_EngineDLL = "Returnal-Engine-Win64-Shipping.dll"

--INJECTION BEHAVIOUR
InjectDelay = 100
WriteInterval = 100
SearchInterval = 100
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.AddFixedFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35,70,160,0,1)
	FOVSlider:SetTickFrequency(5)
	FOVSlider:SetLabel1Text("70")
	FOVSlider:SetLabel2Text("160")	
	
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("89 ?? ?? ?? 8B ?? ?? C5 FA ?? ?? ?? ?? C5 FA ?? ?? ?? ?? 89 ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EngineDLL) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Returnal-Engine-Win64-Shipping.UCameraModifier::BlueprintModifyCamera+83 - 8B 45 08              - mov eax,[rbp+08]
		--Returnal-Engine-Win64-Shipping.UCameraModifier::BlueprintModifyCamera+86 - C5 FB 11 44 24 4C     - vmovsd [rsp+4C],xmm0
		--Returnal-Engine-Win64-Shipping.UCameraModifier::BlueprintModifyCamera+8C - C5 FA 10 07           - vmovss xmm0,[rdi]
		--Returnal-Engine-Win64-Shipping.UCameraModifier::BlueprintModifyCamera+90 - 89 44 24 48           - mov [rsp+48],eax
		--Returnal-Engine-Win64-Shipping.UCameraModifier::BlueprintModifyCamera+94 - 8B 46 08              - mov eax,[rsi+08]

	end
	
	return true

end


function Enable_Inject()
	
	local DLLAddr = HackTool:GetModuleAddress(Process_EngineDLL)
	local Variables = HackTool:AllocateMemory("Variables",50)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("DefaultFOV")
	Variables:PushFloat("FOV")
	Variables:Allocate(DLLAddr) -- Make sure variables won't be out of range
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["DefaultFOV"]:WriteFloat(fDefaultFOV)
	Variables["FOV"]:WriteFloat(fFOV)
	
	ResolutionChanged()	
	
	local asm = [[	

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

	]]

	if HackTool:CompileAssembly(asm,"Fixes",DLLAddr) == nil then -- Make sure injected code won't be out of range
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)		
	end	
			
	Write_FOV()
	
end

function Periodic()
end

function FOVSlider_Changed(Sender)

	fFOV = Sender:GetPosition()
	lblFOVSlider.Caption:SetCaption( string.format("%.0f",fFOV) )
	Write_FOV()
	ForceUpdate()
	
end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

	fFOV = Sender:GetPosition()
	lblFOVSlider.Caption:SetCaption( string.format("%.0f",fFOV) )
	
	if bFOV then
		Write_FOV()
	end
	
	ForceUpdate()
	
end

function Write_FOV()

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

end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
