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

--GAME VARS
fDefaultFOV = 85
fAdditionalFOV = 0
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fAspectRatio169 = 1.777777791
fFactor = 0.00872665

--ControlVars
bFixEnabled = true
bFOV = true

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

--INJECTION BEHAVIOUR
InjectDelay = 500
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,0,70,0,1)
	
	FOVSlider:SetTickFrequency(5)
	FOVSlider:SetLabel1Text("-20")
	FOVSlider:SetLabel2Text("+50")

	DefaultControls.AddHeader("HeaderHUDSZ","HUD safe zone",15,160,210,17)	
	DefaultControls.AddFixedFOVSlider("HUD","HUDSZSlider_Changed",55,190,125,35,0,40,0,1)
	HUD:SetTickFrequency(5)
	HUD:SetLabel1Text("0")
	HUD:SetLabel2Text("40")

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKHUDFix_Enable","HUD safe zone fix","CKHUDFix_Changed",255,121,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")	
	if HackTool:SignatureScan("C6 84 ?? ?? ?? ?? ?? ?? 48 ?? ?? 74 ?? E8 ?? ?? ?? ?? 0F ?? ??",tAddress,PAGE_EXECUTE_READ,0x12,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--BrightMemoryInfinite-Win64-Shipping.exe+2CCE556 - 48 33 C4              	- xor rax,rsp
		--BrightMemoryInfinite-Win64-Shipping.exe+2CCE559 - 48 89 84 24 40 07 00 00 - mov [rsp+00000740],rax
		--BrightMemoryInfinite-Win64-Shipping.exe+2CCE561 - F3 0F 10 B1 3C 02 00 00 - movss xmm6,[rcx+0000023C]
		--BrightMemoryInfinite-Win64-Shipping.exe+2CCE569 - 33 C0                 	- xor eax,eax
		--BrightMemoryInfinite-Win64-Shipping.exe+2CCE56B - 0F 57 C9               	- xorps xmm1,xmm1
	end
	
	local tAddress = HackTool:AddAddress("HUDSAFEZONE")
	if HackTool:SignatureScan("48 8B ?? ?? ?? 0F ?? ?? 48 ?? ?? 0F ?? ?? 48 83 ?? ?? 5F",tAddress,PAGE_EXECUTE_READ,0x05,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--BrightMemoryInfinite-Win64-Shipping.exe+2571D91 - 0F 10 00              - movups xmm0,[rax]
		--BrightMemoryInfinite-Win64-Shipping.exe+2571D94 - 48 8B C7              - mov rax,rdi
		--BrightMemoryInfinite-Win64-Shipping.exe+2571D97 - 0F 11 07              - movups [rdi],xmm0
		--BrightMemoryInfinite-Win64-Shipping.exe+2571D9A - 48 83 C4 30           - add rsp,30
		--BrightMemoryInfinite-Win64-Shipping.exe+2571D9E - 5F                    - pop rdi
	end
	
	
	return true
end

function Enable_Inject()

	local TombRaiderDLL = HackTool:GetModuleAddress("tomb2.dll")


	local Variables = HackTool:AllocateMemory("Variables",256)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("CompensatedFOV")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("factor")
	Variables:PushFloat("HUDSafeZoneLeft")
	Variables:PushFloat("HUDSafeZoneTop")
	Variables:PushFloat("HUDSafeZoneRight")
	Variables:PushFloat("HUDSafeZoneBottom")
	Variables:Allocate(TombRaiderDLL)
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteInt(fAdditionalFOV)
	Variables["AspectRatio"]:WriteFloat(fAspectRatio169)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["factor"]:WriteFloat(fFactor)	
	Variables["HUDSafeZoneLeft"]:WriteFloat(0)
	Variables["HUDSafeZoneTop"]:WriteFloat(0)
	Variables["HUDSafeZoneRight"]:WriteFloat(1)
	Variables["HUDSafeZoneBottom"]:WriteFloat(1)
	
	ResolutionChanged()	
			
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			movss [(allocation)Variables->FOVIn],$$2					$ctx=1
			fld dword ptr [(allocation)Variables->FOVIn]
			test rcx,rcx
			jne next
			fmul dword ptr [(allocation)Variables->factor]
			fptan
			fstp st0
			fld dword ptr [(allocation)Variables->ScreenRatio]			
			fdiv dword ptr [(allocation)Variables->AspectRatio]
			fmulp st1,st0
			fld1
			fpatan
			fdiv dword ptr [(allocation)Variables->factor]
			next:
			fst dword ptr [(allocation)Variables->CompensatedFOV]
			fadd dword ptr [(allocation)Variables->AdditionalFOV]
			fstp dword ptr [(allocation)Variables->FOVOut]
			movss $$2,[(allocation)Variables->FOVOut]					$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
						
		(codecave:jmp)HUDSAFEZONE,HUDSAFEZONE_cc:
			%originalcode%
			$$0 $$1,[(allocation)Variables->HUDSafeZoneLeft]			$ctx=1
			jmp %returnaddress%
			%end%
						
	]]

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

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then

		local fFOVIn = Variables["FOVIn"]:ReadFloat()
		local fFOVOut = Variables["FOVOut"]:ReadFloat()
		local fCompensatedFOV = Variables["CompensatedFOV"]:ReadFloat()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %.2f, Compensated FOV : %.2f, FOV Out : %.2f", fFOVIn, fCompensatedFOV, fFOVOut))
	end
	
end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetPosition() - 20
	lblFOVSlider.Caption:SetCaption( string.format("Value: +%.0f",fAdditionalFOV) )
		
	Write_FOV()	
	ForceUpdate()

end

function HUDSZSlider_Changed(Sender)

	fHUDSafeZOneLeft = Sender:GetPosition()
	fHUDSafeZoneRight = fHUDSafeZOneLeft
	lblHUD.Caption:SetCaption( string.format("+ %.0f %%",fHUDSafeZOneLeft) )
	Write_HUD_Safe_Zone()
	ForceUpdate()
	
end

function Write_FOV()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AdditionalFOV"] then
		if bFOV == true then
			Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
		else
			Variables["AdditionalFOV"]:WriteFloat(0)
		end
	end

end

function Write_HUD_Safe_Zone()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["HUDSafeZoneLeft"] and Variables["HUDSafeZoneRight"] then
		Variables["HUDSafeZoneLeft"]:WriteFloat(fHUDSafeZOneLeft / 100)
		Variables["HUDSafeZoneRight"]:WriteFloat(1 - (fHUDSafeZoneRight / 100))
	end
	
end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKHUDFix_Changed(Sender)

	bHUDSafeZone = Toggle_CheckFix(Sender)
	Toggle_CodeCave("HUDSAFEZONE_cc",bHUDSafeZone)
	ForceUpdate()

end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
