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

--GAME VARS
fDefaultFOV = 55
fDefaultAspectRatio = 1.777777791
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fFOV = 55
fInventoryFOV = 0
fStoreFOV = 0

--ControlVars
bFixEnabled = true
bFOV = true
bInvFOV = true
bStoreFOV = true
bChromaticAberration = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "TheCallistoProtocol-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.AddFixedFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35,0,70,0,1)
	
	FOVSlider:SetTickFrequency(10)
	FOVSlider:SetLabel1Text("-20")
	FOVSlider:SetLabel2Text("+50")
	
	DefaultControls.AddHeader("UI_Scale","UI Inv. FOV fine adjustment",15,200,210,17)	
	DefaultControls.AddFixedFOVSlider("UISlider","UISlider_Changed",55,230,125,35,10,90,0,1)
	
	UISlider:SetTickFrequency(10)
	UISlider:SetLabel1Text("10")
	UISlider:SetLabel2Text("90")

	DefaultControls.AddHeader("UI_StoreScale","UI Store FOV fine adjustment",245,200,210,17)	
	DefaultControls.AddFixedFOVSlider("UISStorelider","UIStoreSlider_Changed",285,230,125,35,10,90,0,1)
	
	UISStorelider:SetTickFrequency(10)
	UISStorelider:SetLabel1Text("10")
	UISStorelider:SetLabel2Text("90")
	
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKUIInvFOVFix_Enable","Inventory FOV fix","CKUIInvFOVFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKUIStoreFOVFix_Enable","Store FOV fix","CKUIStoreFOVFix_Changed",255,141,180,14)
	DefaultControls.AddFixToggle("CKCHROMATICFix_Enable","Chromatic aberration fix","CKCHROMATICFix_Changed",255,161,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? 8B 83 ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--TheCallistoProtocol-Win64-Shipping.exe+D6962A: F6 83 10 02 00 00 10     - test byte ptr [rbx+00000210],10
		--TheCallistoProtocol-Win64-Shipping.exe+D69631: 0F 85 14 EC C5 01        - jne TheCallistoProtocol-Win64-Shipping.exe+29C824B
		--TheCallistoProtocol-Win64-Shipping.exe+D69637: F3 0F 10 83 F8 01 00 00  - movss xmm0,[rbx+000001F8]
		--TheCallistoProtocol-Win64-Shipping.exe+D6963F: F3 0F 11 47 18           - movss [rdi+18],xmm0
		--TheCallistoProtocol-Win64-Shipping.exe+D69644: F3 0F 58 83 FC 01 00 00  - addss xmm0,[rbx+000001FC]
	end
	
	local tAddress = HackTool:AddAddress("FOV2")
	
	if HackTool:SignatureScan("8B ?? ?? 4C 8D ?? ?? 89 ?? ?? 48 ?? ?? ?? ?? 48 ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--TheCallistoProtocol-Win64-Shipping.exe+951156 - 41 0F 2F F4           - comiss xmm6,xmm12
		--TheCallistoProtocol-Win64-Shipping.exe+95115A - 0F 83 66 FF FF FF     - jae TheCallistoProtocol-Win64-Shipping.exe+9510C6
		--TheCallistoProtocol-Win64-Shipping.exe+951160 - 8B 43 4C              - mov eax,[rbx+4C]
		--TheCallistoProtocol-Win64-Shipping.exe+951163 - 4C 8D 43 50           - lea r8,[rbx+50]
		--TheCallistoProtocol-Win64-Shipping.exe+951167 - 89 47 48              - mov [rdi+48],eax
	end	
	
	local tAddress = HackTool:AddAddress("FOV3")
	
	if HackTool:SignatureScan("74 ?? 8B ?? 89 ?? ?? F3 0F ?? ?? ?? 0F ?? ??",tAddress,PAGE_EXECUTE_READ,0x02,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--TheCallistoProtocol-Win64-Shipping.exe+B5A763 - 48 83 C0 08           - add rax,08
		--TheCallistoProtocol-Win64-Shipping.exe+B5A767 - 74 05                 - je TheCallistoProtocol-Win64-Shipping.exe+B5A76E
		--TheCallistoProtocol-Win64-Shipping.exe+B5A769 - 8B 00                 - mov eax,[rax]
		--TheCallistoProtocol-Win64-Shipping.exe+B5A76B - 89 43 48              - mov [rbx+48],eax
		--TheCallistoProtocol-Win64-Shipping.exe+B5A76E - F3 0F10 53 20         - movss xmm2,[rbx+20]
	end	
	
	local tAddress = HackTool:AddAddress("CHROMATIC")
	
	if HackTool:SignatureScan("7F ?? 89 B3 ?? ?? ?? ?? 8B ?? ?? 39",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--TheCallistoProtocol-Win64-Shipping.exe+3B4731A - 48 8B 05 D7 7D 35 03  - mov rax,[TheCallistoProtocol-Win64-Shipping.exe+6E9F0F8]
		--TheCallistoProtocol-Win64-Shipping.exe+3B47321 - 39 30                 - cmp [rax],esi
		--TheCallistoProtocol-Win64-Shipping.exe+3B47323 - 7F 06                 - jg TheCallistoProtocol-Win64-Shipping.exe+3B4732B
		--TheCallistoProtocol-Win64-Shipping.exe+3B47325 - 89 B3 A8 10 00 00     - mov [rbx+000010A8],esi
		--TheCallistoProtocol-Win64-Shipping.exe+3B4732B - 8B 04 2F              - mov eax,[rdi+rbp]
	end	
	
	return true

end


function Enable_Inject()

	local nearTo = 0
	local tAddress = HackTool:GetAddress("FOV")
	
	if HackTool:GetArchitecture() == 0 then
		nearTo = HackTool:GetBaseAddress()
	end


	local Variables = HackTool:AllocateMemory("Variables",0,50)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("FOV")
	Variables:PushFloat("InventoryFOV")
	Variables:PushFloat("StoreFOV")
	Variables:PushFloat("defaultInventoryFOV")
	Variables:PushFloat("defaultStoreFOV")
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOV"]:WriteFloat(fFOV)
	Variables["defaultInventoryFOV"]:WriteFloat(70)
	Variables["defaultStoreFOV"]:WriteFloat(59)
	
	ResolutionChanged()	
	
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
		    fld dword ptr [(allocation)Variables->StoreFOV]
			fstp dword ptr [UIstore]
			$$0 [(allocation)Variables->FOVIn],$$1							$ctx=1			
			addss $$1,[(allocation)Variables->FOV]							$ctx=1			
			$$0 [(allocation)Variables->FOVOut],$$1							$ctx=1
			jmp %returnaddress%
			%end%
		
		(codecave:jmp)FOV2,FOV2_cc:
			%originalcode%
			cmp $$1, dword ptr [(allocation)Variables->defaultInventoryFOV]	$ctx=1
			je setInvFOV
			cmp dword ptr ebx,[rbxPtr]
			jne exit
			
			setInvFOV:
			mov [rbxPtr],rbx
			fld dword ptr [(allocation)Variables->InventoryFOV]
			fstp dword ptr [$$2]											$ctx=1	

			exit:
			jmp %returnaddress%
			%end%
		
			rbxPtr:
			(float)0
			
			dummy:
			(float)0
			
		(codecave:jmp)FOV3,FOV3_cc:
			fld dword ptr [UIstore]
		    fstp dword ptr [$$2]											$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
			
			UIstore:
			(float)0f
			
		(codecave)CHROMATIC,CHROMATIC_cc:
			nop
			nop
			%end%
			
	]]

	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("FOV2_cc",bInvFOV)
		Toggle_CodeCave("FOV3_cc",bStoreFOV)
		Toggle_CodeCave("CHROMATIC_cc",bChromaticAberration)
	end	
			
	Write_FOV()
	Write_UIInv_Scale()
	Write_UIStore_Scale()
	
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["FOV"] then
		local fFOVIn = Variables["FOVIn"]:ReadFloat()
		local fFOVOut = Variables["FOVOut"]:ReadFloat()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %0.2f, FOV Out : %.02f", fFOVIn, fFOVOut))
	end
	
end

function FOVSlider_Changed(Sender)

	fFOV = Sender:GetPosition() - 20
	lblFOVSlider.Caption:SetCaption( string.format("FOV : %.2f",fFOV) )
	Write_FOV()
	ForceUpdate()
	
end

function UISlider_Changed(Sender)

	fInventoryFOV = Sender:GetPosition()
	lblUISlider.Caption:SetCaption( string.format("Inv. FOV : %.0f",fInventoryFOV) )
	Write_UIInv_Scale()
	ForceUpdate()

end

function UIStoreSlider_Changed(Sender)

	fStoreFOV = Sender:GetPosition()
	lblUISStorelider.Caption:SetCaption( string.format("Store FOV : %.0f",fStoreFOV) )
	Write_UIStore_Scale()
	ForceUpdate()

end

function Write_FOV()

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

end

function Write_UIInv_Scale()

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

end

function Write_UIStore_Scale()

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

end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKUIInvFOVFix_Changed(Sender)

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

function CKUIStoreFOVFix_Changed(Sender)

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

function CKCHROMATICFix_Changed(Sender)

	bChromaticAberration = Toggle_CheckFix(Sender)
	Toggle_CodeCave("CHROMATIC_cc",bChromaticAberration)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
