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

--ControlVars
bFixEnabled = true
bFOV = true

--GAME VARS
fFOV = 1.0
fAdditionalFOV = 0

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "Gylt  "
Process_ClassName = "UnrealWindow"
Process_EXEName = "*"

--INJECTION BEHAVIOUR
InjectDelay = 500 
WriteInterval = 500
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_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("0")
	FOVSlider:SetLabel2Text("70")

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)

end

function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("FOV")
	if HackTool:SignatureScan("C6 84 ?? ?? ?? ?? ?? ?? 48 ?? ?? 74 ?? E8 ?? ?? ?? ?? 0F ?? ?? 48 8B ?? ?? ?? ?? ?? ?? ",tAddress,PAGE_EXECUTE_READ,0x12,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Gylt-Win64-Shipping.exe+2C5F916: 48 33 C4                 - xor rax,rsp
		--Gylt-Win64-Shipping.exe+2C5F919: 48 89 84 24 20 06 00 00  - mov [rsp+00000620],rax
		--Gylt-Win64-Shipping.exe+2C5F921: F3 0F 10 B1 3C 02 00 00  - movss xmm6,[rcx+0000023C]
		--Gylt-Win64-Shipping.exe+2C5F929: 33 C0                    - xor eax,eax
		--Gylt-Win64-Shipping.exe+2C5F92B: 0F 57 C9                 - xorps xmm1,xmm1
	end
	
	return true
	
end



function Enable_Inject() 

	local EXEAddr = HackTool:GetModuleAddress(Process_EXEName)
	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("Aspect")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("Sharpening")
	Variables:Allocate(EXEAddr)
	
	asm = [[	
			
		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			movss [(allocation)Variables->FOVIn],$$1			$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]	$ctx=1
			movss [(allocation)Variables->FOVOut],$$1			$ctx=1
			jmp %returnaddress%			
			%end%
					
	]]

	if HackTool:CompileAssembly(asm,"Fixes",EXEAddr) == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		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
	
	if bAspectFix and Variables["Aspect"] then
		fAspectRatio = DisplayInfo:GetfOffsetWidth() / DisplayInfo:GetfOffsetHeight()
		Variables["Aspect"]:WriteFloat(fAspectRatio)
	end
	
end

function Disable_Inject()

	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function FOVSlider_Changed(Sender)

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

function Write_FOV()

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

end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end