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

--GAME VARS
fAdditionalFOV = 0
fAdditionalWeaponFOV = 0
fDefaultAspectRatio = 1.777777791
fFOVMax = 110

--ControlVars
bFOVFix = false
bWeaponFOVFix = true
bFixEnabled = true
bHUDFix = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "ALIEN RAGE"
Process_ClassName = "LaunchUnrealUWindowsClient"
Process_EXEName = "*"

--INJECTION BEHAVIOUR
InjectDelay = 2000
WriteInterval = 100
SearchInterval = 1000
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_FOV1","FOV Fine adjustment",15,70,210,17)
	DefaultControls.AddFOVSlider("FOVSlider1","FOVSlider1_Changed",55,100,125,35)
	DefaultControls.AddHeader("Header_FOV2","FOV Fine adjustment",15,170,210,17)
	DefaultControls.AddFOVSlider("FOVSlider2","FOVSlider2_Changed",55,200,125,35)	
	DefaultControls.AddHeader("Header_FixesEnableDisable","Individual Fixes",245,70,210,17)
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV Fix","CKFOVFix_Changed",255,101,180,14,false)	
	DefaultControls.AddFixToggle("CKWeaponFOVFix_Enable","Weapon FOV Fix","CKWeaponFOVFix_Changed",255,120,180,14)	
	DefaultControls.AddFixToggle("CKHUDFix_Enable","HUD Scaling","CKHUDFix_Changed",255,139,180,14)	
end


function Configure_SignatureScan() 

	FOVResultCount = HackTool:SignatureScanMulti("85??7D??D805????????F30F1044????DE","FOVFix",PAGE_EXECUTE_READ,0xA,Process_EXEName)

	if FOVResultCount == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		for i = 1,FOVResultCount,1 do
			local FOVFix = HackTool:GetAddress( string.format("FOVFix%d",i) )
			if FOVFix ~= nil then print( FOVFix:GetInfo(TYPE_ADDRESS) ) end
		end
		
	end	

	local tAddress = HackTool:AddAddress("HUDFix")
	if HackTool:SignatureScan("0F 28 ?? F3 0F 59 ?? F3 0F 10 ?? ?? ?? ?? 00 F3 0F 11 ?? ?? ?? 0F 28 ?? F3 0F 5C ?? F3 0F 10 ",tAddress,PAGE_EXECUTE_READ,0x7,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )		
	end			
	
	return true

end


function Enable_Inject() 

	FOVCalculator1 = HackTool:InjectFOVCalculator("FOVCalculator1")
	FOVCalculator2 = HackTool:InjectFOVCalculator("FOVCalculator2")
	
	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("SafeZoneX")
	Variables:PushFloat("SafeZoneX2")
	Variables:PushFloat("SafeZoneY")
	Variables:PushFloat("SafeZoneY2")
	Variables:Allocate()	

	ResolutionChanged()
	
	local asm = [[	
		(codecave:jmp)FOVFix1,FOVFix1_cc:							
			fld dword ptr [$$2]					  $context=1
			call (allocation)FOVCalculator1
			fstp dword ptr [$$2]                  $context=1
			%originalcode%
			jmp %returnaddress%
			%end%	
			
		(codecave:jmp)FOVFix2,FOVFix2_cc:	
			fld dword ptr [$$2]					  $context=1
			call (allocation)FOVCalculator2
			fstp dword ptr [$$2]                  $context=1
			%originalcode%
			jmp %returnaddress%
			%end%	
			
		(codecave:jmp)HUDFix,HUDFix_cc:
			%originalcode%
			push ecx
			mov ecx, [(allocation)Variables->SafeZoneX]
			mov [$$2], ecx						$context=1
			mov ecx, [(allocation)Variables->SafeZoneY]
			mov [$$2+04h], ecx					$context=1
			mov ecx, [(allocation)Variables->SafeZoneX2]
			mov [$$2+08h], ecx					$context=1
			mov ecx, [(allocation)Variables->SafeZoneY2]
			mov [$$2+0Ch], ecx				    $context=1			
			pop ecx
			
			jmp %returnaddress%
			%end%
	]]

	if HackTool:CompileAssembly(asm,"FOVFix") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOVFix1_cc",bFOVFix)
		Toggle_CodeCave("FOVFix2_cc",bWeaponFOVFix)
		Toggle_CodeCave("HUDFix_cc",bHUDFix)
	end	
		
end

function Periodic()

	PluginViewport:AppendStatusMessage(	string.format("\r\n%s%s",FOVCalculatorSummary("FOVCalculator1","InGame",bFOVFix),
																   FOVCalculatorSummary("FOVCalculator2","Weapons",bWeaponsFOVFix) ) )	
				
	local Variables = HackTool:GetAllocation("Variables")
	if Variables and bHUDFix == true then 								
		PluginViewport:AppendStatusMessage( string.format("\r\n     (HUDScaling) SafeZoneX=%.2f, SafeZoneX2=%.2f, SafeZoneY=%.2f, SafeZoneY2=%.2f",
											Variables["SafeZoneX"]:ReadFloat(),Variables["SafeZoneX2"]:ReadFloat(),Variables["SafeZoneY"]:ReadFloat(),Variables["SafeZoneY2"]:ReadFloat()) )		
	end																	   
	
end

function Disable_Inject()
	
	CleanUp()
	
end

function ResolutionChanged() 

	UpdateFOVCalculator("FOVCalculator1",DisplayInfo:GetAspectRatio()/fDefaultAspectRatio,fAdditionalFOV)	
	UpdateFOVCalculator("FOVCalculator2",DisplayInfo:GetAspectRatio()/fDefaultAspectRatio,fAdditionalWeaponFOV)	
	
	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["SafeZoneX"] and Variables["SafeZoneX2"] and Variables["SafeZoneY"] and Variables["SafeZoneY2"] then

		if DisplayInfo:GetfOffsetX() > 0 then
			Variables["SafeZoneX"]:WriteFloat( (DisplayInfo:GetWidth()  - DisplayInfo:GetfOffsetX2(-250)) / DisplayInfo:GetWidth())
		else
			Variables["SafeZoneX"]:WriteFloat( 0.82 )
		end
		
		if DisplayInfo:GetfOffsetX2() < DisplayInfo:GetWidth() then
			Variables["SafeZoneX2"]:WriteFloat( (DisplayInfo:GetWidth()  - DisplayInfo:GetfOffsetX2(-230)) / DisplayInfo:GetWidth() )
		else
			Variables["SafeZoneX2"]:WriteFloat( 0.87 )
		end	
	
		if DisplayInfo:GetfOffsetY() > 0 then
			Variables["SafeZoneY"]:WriteFloat( (DisplayInfo:GetHeight() - DisplayInfo:GetfOffsetY(170)) / DisplayInfo:GetWidth() )
		else
			Variables["SafeZoneY"]:WriteFloat( 0.9 )
		end
		if DisplayInfo:GetfOffsetY2() < DisplayInfo:GetHeight() then
			Variables["SafeZoneY2"]:WriteFloat( (DisplayInfo:GetHeight() - DisplayInfo:GetfOffsetY(170)) / DisplayInfo:GetWidth() )
		else
			Variables["SafeZoneY2"]:WriteFloat( 0.9 )
		end	
		
	end
	
	
	
	
end

function HK_IncreaseFOV()	
	FOVSlider1:OffsetPosition(1)
end

function HK_DecreaseFOV()	
	FOVSlider1:OffsetPosition(-1)
end

function FOVSlider1_Changed(Sender)

	fAdditionalFOV = Sender:GetScaledFloat(4)
	UpdateFOVCalculator_AdditionalFOV("FOVCalculator1",fAdditionalFOV)
	lblFOVSlider1.Caption:SetCaption( string.format("Value: %.2f",fAdditionalFOV) )	
	ForceUpdate()	
		
end

function FOVSlider2_Changed(Sender)

	fAdditionalWeaponFOV = Sender:GetScaledFloat(5)
	UpdateFOVCalculator_AdditionalFOV("FOVCalculator2",fAdditionalWeaponFOV)
	lblFOVSlider2.Caption:SetCaption( string.format("Value: %.2f",fAdditionalWeaponFOV) )	
	ForceUpdate()	
		
end

function CKFOVFix_Changed(Sender)

	bFOVFix = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FOVFix1_cc",bFOVFix)	
	ForceUpdate()		
	
end

function CKWeaponFOVFix_Changed(Sender)

	bWeaponFOVFix = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FOVFix2_cc",bWeaponFOVFix)	
	ForceUpdate()		
	
end

function CKHUDFix_Changed(Sender)

	bHUDFix = Toggle_CheckFix(Sender)
	Toggle_CodeCave("HUDFix_cc",bHUDFix)	
	ForceUpdate()		
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end