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

--GAME VARS
fDefaultFOV = 85
fAdditionalFOV = 0
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fAspectRatio219 = 2.333333254
fAspectRatio169 = 1.777777791
fFactor = 0.00872665

--ControlVars
bFixEnabled = true
bFOV = true
bAspect = true
bDOF = true

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

--INJECTION BEHAVIOUR
InjectDelay = 500
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,60,0,1)
	
	FOVSlider:SetTickFrequency(5)
	FOVSlider:SetLabel1Text("-20")
	FOVSlider:SetLabel2Text("+40")

	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)
	DefaultControls.AddFixToggle("CKDOFFix_Enable","Depth of field fix","CKDOFFix_Changed",255,141,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("CC CC F3 0F ?? ?? ?? ?? ?? ?? 0F ?? ?? 0F ?? ?? 77 ?? F3 0F 10 81 ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x12,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--ctts-Win64-Shipping.exe+11BC6BB - 0F 2F C1               	  - comiss xmm0,xmm1
		--ctts-Win64-Shipping.exe+11BC6BE - 77 08                 	  - ja ctts-Win64-Shipping.exe+11BC6C8
		--ctts-Win64-Shipping.exe+11BC6C0 - F3 0F 10 81 08 04 00 00   - movss xmm0,[rcx+00000408]
		--ctts-Win64-Shipping.exe+11BC6C8 - C3                    	  - ret 
		--ctts-Win64-Shipping.exe+11BC6C9 - CC                    	  - int 3 
	end

	local tAddress = HackTool:AddAddress("ASPECT")
	
	if HackTool:SignatureScan("CC CC CC CC CC 48 ?? ?? ?? ?? 57 48 ?? ?? ?? F2 ?? ?? ?? 48 ?? ?? F2 ?? ?? ?? 48 ?? ?? 8B ?? ?? 89 ?? ?? F2 ?? ?? ?? ?? F2 ?? ?? ?? ?? 8B",tAddress,PAGE_EXECUTE_READ,0x4e,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--ctts-Win64-Shipping.exe+1062693 - 89 41 24              - mov [rcx+24],eax
		--ctts-Win64-Shipping.exe+1062696 - 8B 42 28              - mov eax,[rdx+28]
		--ctts-Win64-Shipping.exe+1062699 - 89 41 28              - mov [rcx+28],eax
		--ctts-Win64-Shipping.exe+106269C - 8B 41 2C              - mov eax,[rcx+2C]
		--ctts-Win64-Shipping.exe+106269F - 33 42 2C              - xor eax,[rdx+2C]
	end

	local tAddress = HackTool:AddAddress("DOF")
	
	if HackTool:SignatureScan("48 ?? ?? ?? ?? ?? ?? 32 ?? 8B ?? ?? 66 C7",tAddress,PAGE_EXECUTE_READ,0x09,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--ctts-Win64-Shipping.exe+82BBC9 - 48 8B 05 90 A9 2E 02  - mov rax,[ctts-Win64-Shipping.exe+2B16560]
		--ctts-Win64-Shipping.exe+82BBD0 - 32 DB                 - xor bl,bl
		--ctts-Win64-Shipping.exe+82BBD2 - 8B 78 04              - mov edi,[rax+04]
		--ctts-Win64-Shipping.exe+82BBD5 - 66 C7 44 24 64 01 01  - mov word ptr [rsp+64],0101
		--ctts-Win64-Shipping.exe+82BBDC - 85 FF                 - test edi,edi
	end

	return true
end

function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("CompensatedFOV")
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("factor")
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["AspectRatio"]:WriteFloat(fAspectRatio169)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["factor"]:WriteFloat(fFactor)
	
	ResolutionChanged()	
	
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:			
			%originalcode%
			fld dword ptr [$$2]											$ctx=1
			fst dword ptr [(allocation)Variables->FOVIn]
			fmul dword ptr [(allocation)Variables->factor]
			fptan
			fstp st0
			fld dword ptr [(allocation)Variables->ScreenRatio]			
			fdiv dword ptr [(allocation)Variables->AspectRatio]
			fmulp st1,st0
			fld1
			fpatan
			fdiv dword ptr [(allocation)Variables->factor]
			fst dword ptr [(allocation)Variables->CompensatedFOV]
			fadd dword ptr [(allocation)Variables->AdditionalFOV]
			fstp dword ptr [(allocation)Variables->FOVOut]
			$$0 $$1,[(allocation)Variables->FOVOut]						$ctx=1
			jmp %returnaddress%
			%end%
						
		(codecave:jmp)ASPECT,ASPECT_cc:
			$$0 $$2,[(allocation)Variables->ScreenRatio]				$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
						
		(codecave:jmp)DOF,DOF_cc:
			%originalcode%
			$$0 $$1,0													$ctx=1
			jmp %returnaddress%
			%end%
			
	]]

	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFixEnabled)
		Toggle_CodeCave("ASPECT_cc",bAspect)
		Toggle_CodeCave("DOF_cc",bDOF)
	end	
			
	Write_FOV()
	Write_AR()
	
end

function Periodic()
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		local fCompensatedFOV = Variables["CompensatedFOV"]:ReadFloat()
		local fFOVIn = Variables["FOVIn"]:ReadFloat()
		local fFOVOut = Variables["FOVOut"]:ReadFloat()

		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %0.2f, Compensated FOV : %0.2f, FOV Out : %.02f", fFOVIn, fCompensatedFOV, fFOVOut))
	end
end

function FOVSlider_Changed(Sender)

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

end

function Write_FOV()

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

end

function Write_AR()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AspectRatio"] then
		if bAspect == false then		
			Variables["AspectRatio"]:WriteFloat(fAspectRatio)
		else
			Variables["AspectRatio"]:WriteFloat(fAspectRatio169)
		end
	end
	
end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKARFix_Changed(Sender)

	bAspect = Toggle_CheckFix(Sender)
	Toggle_CodeCave("ASPECT_cc",bAspect)
	Write_AR()
	ForceUpdate()

end

function CKDOFFix_Changed(Sender)

	bDOF = Toggle_CheckFix(Sender)
	Toggle_CodeCave("DOF_cc",bDOF)
	ForceUpdate()

end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
