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

--GAME VARS
fDefaultFOV = 90
fDefaultAspectRatio = 1.7777778
fAdditionalFOV = 0
fFOV = 90

--ControlVars
bFixEnabled = true
bAspectRatio = true
bFOV = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "BrokenPieces-Win64-Shipping.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.AddFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35)
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKARatioFix_Enable","Aspect Ratio fix","CKARatioFix_Changed",255,121,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? 33 C0 0F 57",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	

		-- BrokenPieces-Win64-Shipping.exe+2E90CC6: 48 33 C4                 - xor rax,rsp
		-- BrokenPieces-Win64-Shipping.exe+2E90CC9: 48 89 84 24 20 06 00 00  - mov [rsp+00000620],rax
		-- BrokenPieces-Win64-Shipping.exe+2E90CD1: F3 0F 10 B1 3C 02 00 00  - movss xmm6,[rcx+0000023C]
		-- BrokenPieces-Win64-Shipping.exe+2E90CD9: 33 C0                    - xor eax,eax
		-- BrokenPieces-Win64-Shipping.exe+2E90CDB: 0F 57 C0                 - xorps xmm0,xmm0
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
	
	local tAddress = HackTool:AddAddress("ARatio")
	
	if HackTool:SignatureScan("89 41 ?? 8B 41 ?? 33 42 ?? 83 E0 ?? 31 41 ?? 8B 4A ?? 33 4F",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		-- BrokenPieces-Win64-Shipping.exe+F1A0A9: 89 41 28     - mov [rcx+28],eax
		-- BrokenPieces-Win64-Shipping.exe+F1A0AC: 8B 42 2C     - mov eax,[rdx+2C]
		-- BrokenPieces-Win64-Shipping.exe+F1A0AF: 89 41 2C     - mov [rcx+2C],eax
		-- BrokenPieces-Win64-Shipping.exe+F1A0B2: 8B 41 30     - mov eax,[rcx+30]
		-- BrokenPieces-Win64-Shipping.exe+F1A0B5: 33 42 30     - xor eax,[rdx+30]
		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("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("ScreenAspect")
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	
	ResolutionChanged()	
	
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			movss xmm6,[rcx+0x00000238]
			movss [(allocation)Variables->FOVIn],$$1				$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]		$ctx=1
			movss [(allocation)Variables->FOVOut],$$1				$ctx=1
			jmp %returnaddress%
			%end%			

		(codecave:jmp)ARatio,ARatio_cc:
			fld dword ptr [(allocation)Variables->ScreenAspect]
			fstp dword ptr [$$1]									$ctx=1
			jmp %returnaddress%
			%end%
	]]

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

function Periodic()
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		fFOVIn = Variables["FOVIn"]:ReadFloat()
		fFOVOut = Variables["FOVOut"]:ReadFloat()		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %0.f, FOV Out : %.0f", fFOVIn, fFOVOut))
	end
end

function FOVSlider_Changed(Sender)

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

function Write_FOV()

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

end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKARatioFix_Changed(Sender)

	bAspectRatio = Toggle_CheckFix(Sender)
	Toggle_CodeCave("ARatio_cc",bAspectRatio)
	ForceUpdate()

end

function ResolutionChanged() 

	SyncDisplayDetection()
	
	local ScreenAspect = DisplayInfo:GetAspectRatio()
	
	local Variables = HackTool:GetAllocation("Variables")
	if Variables then
		Variables["ScreenAspect"]:WriteFloat(ScreenAspect)
	end		
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
