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

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

--ControlVars
bFixEnabled = true
bFOV = true
bHUDSafeZone = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "UnrealWindow"
Process_EXEName = "PenDriverPro-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.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)

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

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	--if HackTool:SignatureScan("0F 28 ?? 48 8B ?? ?? ?? ?? ?? ?? 48 ?? ?? E8 ?? ?? ?? ?? 0F 28 ?? ?? ?? ?? ?? ?? 48 81",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
	if HackTool:SignatureScan("F3 0F ?? ?? 89 ?? ?? ?? 8B ?? ?? F3 0F ?? ?? ?? ?? F3 0F ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--PenDriverPro-Win64-Shipping.exe+2D87EC3 - 8B 45 08              - mov eax,[rbp+08]
		--PenDriverPro-Win64-Shipping.exe+2D87EC6 - F2 0F 11 44 24 4C     - movsd [rsp+4C],xmm0
		--PenDriverPro-Win64-Shipping.exe+2D87ECC - F3 0F 10 07           - movss xmm0,[rdi]
		--PenDriverPro-Win64-Shipping.exe+2D87ED0 - 89 44 24 48           - mov [rsp+48],eax
		--PenDriverPro-Win64-Shipping.exe+2D87ED4 - 8B 46 08              - mov eax,[rsi+08]
	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) )
		--PenDriverPro-Win64-Shipping.exe+23E782A - FF D0                 - call rax
		--PenDriverPro-Win64-Shipping.exe+23E782C - 48 8B 5C 24 40        - mov rbx,[rsp+40]
		--PenDriverPro-Win64-Shipping.exe+23E7831 - 0F 10 00              - movups xmm0,[rax]
		--PenDriverPro-Win64-Shipping.exe+23E7834 - 48 8B C7              - mov rax,rdi
		--PenDriverPro-Win64-Shipping.exe+23E7837 - 0F 11 07              - movups [rdi],xmm0
	end

	return true
end

function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("MinFOV")
	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:PushFloat("HUDSafeZoneDefRight")
	Variables:PushFloat("HUDSafeZoneDefBottom")
	
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["MinFOV"]:WriteFloat(10)
	Variables["AdditionalFOV"]:WriteFloat(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)
	Variables["HUDSafeZoneDefRight"]:WriteFloat(1)
	Variables["HUDSafeZoneDefBottom"]:WriteFloat(1)
	
	ResolutionChanged()	

	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			$$0 [(allocation)Variables->FOVIn],$$1						$ctx=1
			fld dword ptr [(allocation)Variables->FOVIn]
			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]
			fstp dword ptr [(allocation)Variables->FOVOut]
			movss $$1,[(allocation)Variables->FOVOut]					$ctx=1
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)HUDSAFEZONE,HUDSAFEZONE_cc:
			movsd xmm14,[$$2+8]											$ctx=1
			ucomisd xmm14,[(allocation)Variables->HUDSafeZoneDefRight]
			jne next			
			$$0 xmm14,[(allocation)Variables->HUDSafeZoneLeft]			$ctx=1
			$$0 [$$2],xmm14												$ctx=1
			
			next:
			%originalcode%			
			jmp %returnaddress%
			%end%
			
	]]

	if HackTool:CompileAssembly(asm,"Fixes") == 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()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %0.2f, FOV Out : %.02f", fFOVIn, fFOVOut))
	end
end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetPosition() - 20
	lblFOVSlider.Caption:SetCaption( string.format("Value: +%.0f",fAdditionalFOV) )
		
	Write_FOV()	
	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 HUDSZSlider_Changed(Sender)

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

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
