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

--GAME VARS
fDefaultFOV = 55
fDefaultAspectRatio = 1.777777791
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fFOV = 0
fFOV = 55

--ControlVars
bFixEnabled = true
bFOV = true

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

--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,55,140,0,1)
	
	FOVSlider:SetTickFrequency(10)
	FOVSlider:SetLabel1Text("55")
	FOVSlider:SetLabel2Text("140")
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("F3 0F 10 80 84 02 00 00",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Firewatch.Matrix4x4f::Invert_Full+B67D - 48 85 C0              	- test rax,rax
		--Firewatch.Matrix4x4f::Invert_Full+B680 - 74 0D                 	- je Firewatch.Matrix4x4f::Invert_Full+B68F
		--Firewatch.Matrix4x4f::Invert_Full+B682 - F3 0F 10 80 84 02 00 00  - movss xmm0,[rax+00000284]
		--Firewatch.Matrix4x4f::Invert_Full+B68A - 48 83 C4 28           	- add rsp,28
		--Firewatch.Matrix4x4f::Invert_Full+B68E - C3                    	- ret 
	end
	
	local tAddress = HackTool:AddAddress("FOV2")
	
	if HackTool:SignatureScan("C3 CC CC F3 0F ?? ?? ?? ?? ?? ?? 66 C7 81 ?? ?? ?? ?? ?? ?? C3 CC",tAddress,PAGE_EXECUTE_READ,0x03,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Firewatch.Camera::SetTemporarySettings+2E - CC                    		- int 3 
		--Firewatch.Camera::SetTemporarySettings+2F - CC                    		- int 3 
		--Firewatch.Camera::SetFov                  - F3 0F 11 89 84 02 00 00   	- movss [rcx+00000284],xmm1
		--Firewatch.Camera::SetFov+8                - 66 C7 81 25 03 00 00 01 01 	- mov word ptr [rcx+00000325],0101
		--Firewatch.Camera::SetFov+11               - C3                    		- ret 
	end
	
	return true

end


function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("FOV")
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOV"]:WriteFloat(fFOV)
	
	ResolutionChanged()	
	
	local asm = [[	

		(codecave:jmp)FOV2,FOV2_cc:
			$$0 $$2,[(allocation)Variables->FOV]		$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
		
		(codecave)FOV,FOV_cc:
			$$0 $$1,[(allocation)Variables->FOV]		$ctx=1
			%end%
			
	]]

	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("FOV2_cc",bFOV)
	end	
			
	Write_FOV()
	
end

function Periodic()
end

function FOVSlider_Changed(Sender)

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

function Write_FOV()

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

end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
