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

--GAME VARS
fAdditionalFOV = 0
fDefaultAspectRatio = 1.777777791
fFOV = 90
fDefaultFOV = 75

--ControlVars
bFixEnabled = true
bFOV = true
bAspectRatio = true

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

--INJECTION BEHAVIOUR
InjectDelay = 500
WriteInterval = 500
SearchInterval = 500
SuspendThread = true

NearToAddress = 0

--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.AddFOVSlider("FOVSlider","FOVSlider_Changed",55,90,125,35)
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKARFix_Enable","Aspect ratio fix","CKARFix_Changed",255,121,180,14)

end

function Configure_SignatureScan() 

	NearToAddress = HackTool:GetBaseAddress()

	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("F3 0F 10 83 ?? ?? ?? ?? F3 0F ?? ?? ?? 8B 83",tAddress,PAGE_EXECUTE_READ,0x08,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Chant-Win64-Shipping.UCameraComponent::GetCameraView+EE8 - addss xmm0,[rbx+000001F8]
		--Chant-Win64-Shipping.UCameraComponent::GetCameraView+EF0 - jmp Chant-Win64-Shipping.UCameraComponent::GetCameraView+EFA
		--Chant-Win64-Shipping.UCameraComponent::GetCameraView+EF2 - movss xmm0,[rbx+000001F8]
		--Chant-Win64-Shipping.UCameraComponent::GetCameraView+EFA - movss [rdi+18],xmm0
		--Chant-Win64-Shipping.UCameraComponent::GetCameraView+EFF - mov eax,[rbx+00000208]		
	end	

	local tAddress = HackTool:AddAddress("ARatio")
	
	if HackTool:SignatureScan("F3 0F 11 83 ?? ?? ?? ?? F3 0F 11 83 ?? ?? ?? ?? 0F 28",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Chant-Win64-Shipping.UCineCameraComponent::RecalcDerivedData+114 - movss xmm0,[rbx+000007DC]
		--Chant-Win64-Shipping.UCineCameraComponent::RecalcDerivedData+11C - divss xmm0,xmm1
		--Chant-Win64-Shipping.UCineCameraComponent::RecalcDerivedData+120 - movss [rbx+000007E4],xmm0
		--Chant-Win64-Shipping.UCineCameraComponent::RecalcDerivedData+128 - movss [rbx+00000208],xmm0
		--Chant-Win64-Shipping.UCineCameraComponent::RecalcDerivedData+130 - movaps xmm6,[rsp+20]
	end

	return true

end

function Enable_Inject() 

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AspectRatio")
	Variables:Allocate(NearToAddress)
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["AspectRatio"]:WriteFloat(fDefaultAspectRatio)

	local asm = [[
	
		(codecave:jmp)FOV,FOV_cc:
		movss [(allocation)Variables->FOVIn],$$2			$ctx=1
		addss $$2,[(allocation)Variables->AdditionalFOV]	$ctx=1
		movss [(allocation)Variables->FOVOut],$$2			$ctx=1
		%originalcode%
		jmp %returnaddress%
		%end%
		
		(codecave:jmp)ARatio,ARatio_cc:
		movss $$2,[(allocation)Variables->AspectRatio]		$ctx=1
		%originalcode%
		jmp %returnaddress%
		%end%
		
	]]
	

	if HackTool:CompileAssembly(asm,"Fixes",NearToAddress) == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("ARatio_cc",bAspectRatio)
	end
	
	Write_FOV()
	
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["FOVOut"] and Variables["AdditionalFOV"] and Variables["AspectRatio"] then
		Variables["AspectRatio"]:WriteFloat(DisplayInfo:GetAspectRatio())
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In : %0.f, FOV Out : %.0f",Variables["FOVIn"]:ReadFloat(), Variables["FOVOut"]:ReadFloat()))
	end

end

function Write_FOV()

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

end

function FOVSlider_Changed(Sender)

	fFOV = (Sender:GetScaledFloat(2)) + 25
	lblFOVSlider.Caption:SetCaption( string.format("Additional FOV : %.2f",fFOV) )
	Write_FOV()
	ForceUpdate()
	
end

function CKFOVFix_Changed(Sender)

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

function CKARFix_Changed(Sender)

	bAspectRatio = Toggle_CheckFix(Sender)
	Toggle_CodeCave("ARatio_cc",bAspectRatio)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Disable_Inject()
	
	CleanUp()
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
