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

--GAME VARS
fDefaultFOV = 60
fDefaultAspectRatio = 1.777777791
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fAdditionalFOV = 0
fFOV = 60
fFactor = 0.00872665

--ControlVars
bFixEnabled = true
bFOV = true

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

--INJECTION BEHAVIOUR
InjectDelay = 100
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.AddHeader("Header_FOV","FOV fine adjustment",15,70,210,17)	
	DefaultControls.AddFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35)
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("EB ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? 8B 83",tAddress,PAGE_EXECUTE_READ,0x0a,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--TheInvincible-Win64-Shipping.exe+2A9F29A - EB 08                 		- jmp TheInvincible-Win64-Shipping.exe+2A9F2A4
		--TheInvincible-Win64-Shipping.exe+2A9F29C - F3 0F 10 83 F8 01 00 00   	- movss xmm0,[rbx+000001F8]
		--TheInvincible-Win64-Shipping.exe+2A9F2A4 - F3 0F 11 47 18         	- movss [rdi+18],xmm0
		--TheInvincible-Win64-Shipping.exe+2A9F2A9 - 8B 83 08 02 00 00        	- mov eax,[rbx+00000208]
		--TheInvincible-Win64-Shipping.exe+2A9F2AF - 89 47 2C              		- mov [rdi+2C],eax		
	end
	
	local tAddress = HackTool:AddAddress("RESOLUTION")
	
	if HackTool:SignatureScan("48 8B ?? ?? ?? F2 0F ?? ?? ?? ?? ?? ?? C6 05",tAddress,PAGE_EXECUTE_READ,0x05,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )

		--TheInvincible-Win64-Shipping.exe+2762D95 - F2 0F 11 03            	- movsd [rbx],xmm0 
		--TheInvincible-Win64-Shipping.exe+2762D99 - 48 8B 5C 24 38        		- mov rbx,[rsp+38] 
		--TheInvincible-Win64-Shipping.exe+2762D9E - F2 0F 11 05 02 65 DA 02   	- movsd [TheInvincible-Win64-Shipping.exe+55092A8],xmm0 <<==
		--TheInvincible-Win64-Shipping.exe+2762DA6 - C6 05 03 65 DA 02 01     	- mov byte ptr [TheInvincible-Win64-Shipping.exe+55092B0],01
		--TheInvincible-Win64-Shipping.exe+2762DAD - 48 83 C4 20           		- add rsp,20
	end
	
	return true

end


function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("CompensatedFOV")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("DefaultAspectRatio")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("factor")
	Variables:PushFloat("Width")
	Variables:PushFloat("Height")
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["AspectRatio"]:WriteFloat(fDefaultAspectRatio)
	Variables["DefaultAspectRatio"]:WriteFloat(fDefaultAspectRatio)
	Variables["factor"]:WriteFloat(fFactor)
	
	ResolutionChanged()	
	
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			movss [(allocation)Variables->FOVIn],$$2					$ctx=1
			
			fld dword ptr [(allocation)Variables->FOVIn]
			fmul dword ptr [(allocation)Variables->factor]
			fptan
			fstp st0
			fld dword ptr [(allocation)Variables->AspectRatio]
			fdiv dword ptr [(allocation)Variables->DefaultAspectRatio]
			fmulp st1,st0
		    fld1
		    fpatan
			fdiv dword ptr [(allocation)Variables->factor]						; calculate compensated FOV
			fstp dword ptr [(allocation)Variables->CompensatedFOV]				; and retrieve it
			$$0 $$2,[(allocation)Variables->CompensatedFOV]				$ctx=1
						
			addss $$2,[(allocation)Variables->AdditionalFOV]			$ctx=1
			movss [(allocation)Variables->FOVOut],$$2					$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%

		(codecave:jmp)RESOLUTION,RESOLUTION_cc:
			fld dword ptr [$$1]											$ctx=1
			fdiv dword ptr [$$1+0x4]									$ctx=1
			fstp dword ptr [(allocation)Variables->AspectRatio]
			%originalcode%
			jmp %returnaddress%
			%end%
		
	]]

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

function Periodic()
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		fFOVIn = Variables["FOVIn"]:ReadFloat()
		fFOVCompensated = Variables["CompensatedFOV"]:ReadFloat()
		fFOVOut = Variables["FOVOut"]:ReadFloat()
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %0.2f, FOV Compensated: %0.2f, FOV Out : %.02f", fFOVIn, fFOVCompensated, fFOVOut))
	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 Write_FOV()

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

end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
