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

--GAME VARS
fAdditionalFOV = 0

--ControlVars
bFixEnabled = true
bFOV = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "Biomutant-Win64-Shipping.exe"

--INJECTION BEHAVIOUR
InjectDelay = 1500
WriteInterval = 500
SearchInterval = 500
SuspendThread = true

--Name                         Manual/Auto/Hybrid  		Steam/Origin/Any                IncludeFile:Configure;Enable;Periodic;Disable;
SupportedVersions = { 		
{"Automatically Detect",       "Auto",  			"Any",	                         "Configure_SignatureScan;Enable_Inject;Periodic;Disable_Inject;"},
}

function Init_Controls()

	DefaultControls.AddHeader("Header_FOV","Additional FOV Fine adjustment",15,70,210,17)	
	DefaultControls.AddFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("VerticalFOV")
	
	if HackTool:SignatureScan("F3 0F 11 83 EC 04 00 00",tAddress,PAGE_EXECUTE_READ,0x00,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("FOV")
	Variables:PushFloat("InGameFOV")
	Variables:Allocate()
	
	local asm = [[
				
		(codecave:jmp)VerticalFOV,VerticalFOV_cc:
			movss xmm0,[rbx+0x000004EC]						; Retrieve in game FOV
			movss [(allocation)Variables->InGameFOV],xmm0	; Store in game FOV
			movss xmm0,[rbx+0x000004C8]						; Retrieve Saved FOV
			addss xmm0,[(allocation)Variables->FOV]			; Add user FOV value
			%originalcode%
			jmp %returnaddress%
			%end%					

	]]
	
	if HackTool:CompileAssembly(asm,"VerticalFOV") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("VerticalFOV_cc",bFOV)
		if bFOV then
			WriteFOV()
		end
	end	
	
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["InGameFOV"] then
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nVertical FOV %.2f\r\n", Variables["InGameFOV"]:ReadFloat() ) )
	end
	
end

function Disable_Inject()
	
	CleanUp()
	
end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetScaledFloat(2) - 20
	lblFOVSlider.Caption:SetCaption( string.format("Value: %.0f",fAdditionalFOV) )
	
	if bFixEnabled then
		WriteFOV()
	end
	
	ForceUpdate()

end

function WriteFOV()

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

function ResolutionChanged() 

end

function Init()	

	Init_BaseControls()
	Init_Controls()
	
end

function DeInit()

	DisableFix()
	
end
