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

--GAME VARS
fAdditionalCamFOV = 0
fAdditionalCinematicsFOV = 0
fDefaultAspectRatio = 2.4

--ControlVars
bFixEnabled = true
bCSAspectRatio = true
bCamFOV = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "TheQuarry-Win64-Shipping.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_FOV1","FOV camera adjustment",15,70,210,17)	
	DefaultControls.AddFOVSlider("FOVSlider1","FOVCamSlider_Changed",55,100,125,35)
	DefaultControls.AddHeader("Header_FOV2","FOV cinematics adjustment",15,160,210,17)	
	DefaultControls.AddFOVSlider("FOVSlider2","FOVcinematicsSlider_Changed",55,190,125,35)
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV Fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKCSAspectFix_Enable","Black bars removal","CKCSAspectFix_Changed",255,101,180,54)

end

function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("CSARatio1")
	if HackTool:SignatureScan("E9 ?? ?? ?? ?? F3 0F 10 41 ?? EB",tAddress,PAGE_EXECUTE_READ,0x5,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
	
	local tAddress = HackTool:AddAddress("CSARatio2")
	if HackTool:SignatureScan("F3 0F 10 83 ?? ?? ?? ?? F3 0F 5E C1 F3 0F 11 83 ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x0c,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
	
	
	local tAddress = HackTool:AddAddress("CSARatio3")
	if HackTool:SignatureScan("75 ?? 48 FF C2 48 83 C1 28 ",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("FOV")
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? EB ?? 0F ?? ?? F3 0F 10 8B ?? ?? ?? ??",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 Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("CamFOV")
	Variables:PushFloat("CinematicsFOV")
	Variables:PushFloat("AR")
	Variables:PushFloat("FOVState")
	Variables:PushFloat("SavedAR")
	Variables:Allocate()
	Variables["SavedAR"]:WriteFloat(fDefaultAspectRatio)
	
	ResolutionChanged()	
	
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			pushf
			mov [(allocation)Variables->FOVState],r14
			cmp r14,0x2
			je cinematics
			cmp r14,0x1
			je camera
			jmp exit
			
			cinematics:
			addss xmm0,[(allocation)Variables->CinematicsFOV]
			jmp exit

			camera:
			addss xmm0,[(allocation)Variables->CamFOV]
			
			exit:
			popf
			jmp %returnaddress%
			%end%					
			
			;	TheQuarry-Win64-Shipping.exe+388D32A: F3 0F 59 83 3C 08 00 00  - mulss xmm0,[rbx+0000083C]
			;	TheQuarry-Win64-Shipping.exe+388D332: E8 A4 DD 6B 01           - call TheQuarry-Win64-Shipping.exe+4F4B0DB
			; ---------- INJECTING HERE ----------
			;	TheQuarry-Win64-Shipping.exe+388D337: F3 0F 59 05 C5 52 7A 02  - mulss xmm0,[TheQuarry-Win64-Shipping.exe+6032604]
			; ---------- DONE INJECTING  ----------
			;	TheQuarry-Win64-Shipping.exe+388D33F: EB 03                    - jmp TheQuarry-Win64-Shipping.exe+388D344
			;	TheQuarry-Win64-Shipping.exe+388D341: 0F 57 C0                 - xorps xmm0,xmm0
			
		(codecave:jmp)CSARatio1,CSARatio1_cc:
			movss xmm0,[(allocation)Variables->AR]
			jmp %returnaddress%
			%end%
			
			;	TheQuarry-Win64-Shipping.exe+3E42A2D - mov rsi,[rsi+00000140]
			;	TheQuarry-Win64-Shipping.exe+3E42A34 - jmp TheQuarry-Win64-Shipping.exe+3E427E0
			; ---------- INJECTING HERE ----------
			;	TheQuarry-Win64-Shipping.exe+3E42A39 - movss xmm0,[rcx+10]
			; ---------- DONE INJECTING  ----------
			;	TheQuarry-Win64-Shipping.exe+3E42A3E - jmp TheQuarry-Win64-Shipping.exe+3E42A45
			;	TheQuarry-Win64-Shipping.exe+3E42A40 - movss xmm0,[rdi+14]
		
		(codecave:jmp)CSARatio2,CSARatio2_cc:
			movss xmm0,[(allocation)Variables->AR]
			%originalcode%
			jmp %returnaddress%
			%end%							

			;	TheQuarry-Win64-Shipping.exe+388D359 - movss xmm0,[rbx+0000083C]
			;	TheQuarry-Win64-Shipping.exe+388D361 - divss xmm0,xmm1
			; ---------- INJECTING HERE ----------
			;	TheQuarry-Win64-Shipping.exe+388D365 - movss [rbx+00000844],xmm0
			; ---------- DONE INJECTING  ----------
			;	TheQuarry-Win64-Shipping.exe+388D36D - movss [rbx+00000218],xmm0
			;	TheQuarry-Win64-Shipping.exe+388D375 - movaps xmm6,[rsp+20]
			
		(codecave)CSARatio3,CSARatio3_cc:
			jmp $$1									$ctx=1
			%end%
			
			;	TheQuarry-Win64-Shipping.exe+3E42A1B - xor al,al
			;	TheQuarry-Win64-Shipping.exe+3E42A1D - test al,al
			; ---------- INJECTING HERE ----------
			;	TheQuarry-Win64-Shipping.exe+3E42A1F - jne TheQuarry-Win64-Shipping.exe+3E42A39
			; ---------- DONE INJECTING  ----------
			;	TheQuarry-Win64-Shipping.exe+3E42A21 - inc rdx
			;	TheQuarry-Win64-Shipping.exe+3E42A24 - add rcx,28
			
	]]

	if HackTool:CompileAssembly(asm,"FOVFix") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("CSARatio1_cc",bCSAspectRatio)
		Toggle_CodeCave("CSARatio2_cc",bCSAspectRatio)
		Toggle_CodeCave("CSARatio3_cc",bCSAspectRatio)
		Toggle_CodeCave("FOV_cc",bCamFOV)
		if bCamFOV then
			WriteCamFOV()
			WriteCinematicsFOV()
		end
	end	
		
	
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	local RegisterState
	
	if Variables and Variables["AR"] and Variables["FOVState"] then
		Variables["AR"]:WriteFloat(DisplayInfo:GetAspectRatio())
		RegisterState = Variables["FOVState"]:ReadInt()
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nBB toggle %s", bCSAspectRatio ) )
		--if RegisterState == 1 then
			--PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV fix on camera" ) )
		--else
			--PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV fix on cinematics" ) )
		--end
	end
	
end

function FOVCamSlider_Changed(Sender)

	fAdditionalCamFOV = Sender:GetScaledFloat(4) + 35
	lblFOVSlider1.Caption:SetCaption( string.format("Value: %.0f",fAdditionalCamFOV) )
	
	WriteCamFOV()	
	ForceUpdate()

end

function FOVcinematicsSlider_Changed(Sender)

	fAdditionalCinematicsFOV = Sender:GetScaledFloat(4) + 35
	lblFOVSlider2.Caption:SetCaption( string.format("Value: %.0f",fAdditionalCinematicsFOV) )
	
	WriteCinematicsFOV()
	ForceUpdate()
	
end

function WriteCamFOV()

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

function WriteCinematicsFOV()

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

end

function Disable_Inject()
	
	RestoreAspectDefault()
	CleanUp()
	
end

function CKCSAspectFix_Changed(Sender)

	bCSAspectRatio = Toggle_CheckFix(Sender)
	if bCSAspectRatio == false then
		RestoreAspectDefault()
	end
	Toggle_CodeCave("CSARatio1_cc",bCSAspectRatio)
	Toggle_CodeCave("CSARatio2_cc",bCSAspectRatio)
	Toggle_CodeCave("CSARatio3_cc",bCSAspectRatio)
	ForceUpdate()
	
end

function CKFOVFix_Changed(Sender)

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

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function RestoreAspectDefault()

end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
