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

--GAME VARS
fAdditionalFOV = 0
fDefaultAspectRatio = 1.777777777
fFOVMax = 110

--ControlVars
bFixEnabled = true
bFOVFix = true
bHUDFix = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "Killer Is Dead"
Process_ClassName = "LaunchUnrealUWindowsClient"
Process_EXEName = "*"

--INJECTION BEHAVIOUR
InjectDelay = 2000
WriteInterval = 100
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_FOV","FOV Fine adjustment",15,70,210,17)
	DefaultControls.AddFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35)
	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 Fix","CKHUDFix_Changed",255,120,180,14)
end


function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("FOVFix")
	if HackTool:SignatureScan("E8????????E9????????F30F1044????8B??24??8B??24??",tAddress,PAGE_EXECUTE_READ,0xA,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
	

	local tAddress = HackTool:AddAddress("HUDFix1")
	if HackTool:SignatureScan("D9E885??0F84????????8B????8B????8B",tAddress,PAGE_EXECUTE_READ,0xA,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:PushInt("HUD_X")
	Variables:PushInt("HUD_Y")
	Variables:PushInt("HUD_Width")
	Variables:PushInt("HUD_Height")	
	Variables:Allocate()	

	FOVCalculator = HackTool:InjectFOVCalculator("FOVCalculator")
	
	ResolutionChanged()
	
	local asm = [[	
		(codecave:jmp)FOVFix,FOVFix_cc:							
			fld dword ptr [$$2]					  $context=1
			call (allocation)FOVCalculator
			fstp dword ptr [$$2]                  $context=1
			%originalcode%
			jmp %returnaddress%
			%end%

		(codecave:jmp)HUDFix1,HUDFix1_cc:	
			%originalcode%
			push eax
			mov eax, [(allocation)Variables->HUD_X]
			mov [$$2], eax						  				$context=1
			mov eax, [(allocation)Variables->HUD_Y]
			mov [$$2+04h], eax					  				$context=1
			mov eax, [(allocation)Variables->HUD_Width]
			mov [$$2+08h], eax					  				$context=1
			mov eax, [(allocation)Variables->HUD_Height]
			mov [$$2+0Ch], eax					  				$context=1
			pop eax			
			jmp %returnaddress%
			%end%	

			
	]]

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

function Periodic()

	PluginViewport:AppendStatusMessage(	string.format("\r\n%s",FOVCalculatorSummary("FOVCalculator","InGame",bFOVFix)) )	
	
	local Variables = HackTool:GetAllocation("Variables")
	if Variables and bHUDFix == true then
		PluginViewport:AppendStatusMessage( string.format("\r\n     (HUD Position) HUD_X=%d, HUD_Y=%d, Width=%d, Height=%d",
											Variables["HUD_X"]:ReadInt(),Variables["HUD_Y"]:ReadInt(), Variables["HUD_Width"]:ReadInt(),Variables["HUD_Height"]:ReadInt()) )
	end	
	
end

function Disable_Inject()
	
	CleanUp()
	
end


function ResolutionChanged() 

	UpdateFOVCalculator("FOVCalculator",DisplayInfo:GetAspectRatio()/fDefaultAspectRatio,fAdditionalFOV)

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["HUD_X"] and Variables["HUD_Y"] and Variables["HUD_Width"] and Variables["HUD_Height"] then
		Variables["HUD_X"]:WriteInt( DisplayInfo:GetiOffsetX() )
		Variables["HUD_Y"]:WriteInt( DisplayInfo:GetiOffsetY() )
		Variables["HUD_Width"]:WriteInt( DisplayInfo:GetiOffsetWidth() )
		Variables["HUD_Height"]:WriteInt( DisplayInfo:GetiOffsetHeight() )
	end	
	
end

function HK_IncreaseFOV()	
	FOVSlider:OffsetPosition(1)
end

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

function FOVSlider_Changed(Sender)

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

function CKFOVFix_Changed(Sender)

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

function CKHUDFix_Changed(Sender)

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

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end