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

--GAME VARS
fDefaultFOV = 90
fAdditionalWorldFOV = 0
fAdditionalCinematicsFOV = 0
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fDefaultAspectRatio = 1.77777779
fFactor = 0.00872665

--ControlVars
bFixEnabled = true
bWorldFOV = true
bCinematicsFOV = true
bCinematicsFPS = true
bCinematicsFrameGen = true
bDOF = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "TheGreatCircle.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_World_FOV","In game FOV fine adjustment",15,70,210,17)	
	DefaultControls.AddFixedFOVSlider("WorldFOVSlider","WorldFOVSlider_Changed",55,100,125,35,0,70,0,1)
	WorldFOVSlider:SetTickFrequency(10)
	DefaultControls.AddHeader("Header_Cinematics_FOV","Cinematics FOV fine adjustment",15,160,210,17)	
	DefaultControls.AddFixedFOVSlider("CinematicsFOVSlider","CinematicsFOVSlider_Changed",55,190,125,35,0,70,0,1)
	CinematicsFOVSlider:SetTickFrequency(10)

	DefaultControls.AddFixToggle("CKWORLDFOVFix_Enable","World FOV fix","CKWorldFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKCINEMATICSFOVFix_Enable","Cinematics FOV fix","CKCinematicsFOVFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKDOFFix_Enable","Cinematics depth of field fix","CKDOFFix_Changed",255,141,180,14)
	DefaultControls.AddFixToggle("CKCINEMATICSFPSFix_Enable","Cinematics frametime fix","CKCINEMATICSFPSFix_Changed",255,161,180,14)
	DefaultControls.AddFixToggle("CKCINEMATICSFRAMEGENFix_Enable","Cinematics frame generation fix","CKCINEMATICSFRAMEGENFix_Changed",255,181,220,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("WORLDFOV")
	
	if HackTool:SignatureScan("F3 44 ?? ?? ?? ?? 48 8B ?? ?? ?? 4C ?? ?? 49 0F ?? ?? ?? ?? ?? ?? 4C 8D",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--TheGreatCircle.exe+18E7184 - 0F 84 5E 0A 00 00     - je TheGreatCircle.exe+18E7BE8
		--TheGreatCircle.exe+18E718A - 48 8B 05 FF 22 46 02  - mov rax,[TheGreatCircle.exe+3D49490]
		--TheGreatCircle.exe+18E7191 - F3 44 0F 10 58 0C     - movss xmm11,[rax+0C]
		--TheGreatCircle.exe+18E7197 - 48 8B 44 24 68        - mov rax,[rsp+68]
		--TheGreatCircle.exe+18E719C - 4C 8B 00              - mov r8,[rax]
	end
	
	local tAddress = HackTool:AddAddress("CINEMATICSFOV")
	
	if HackTool:SignatureScan("F3 0F ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 0F ?? ?? E8 ?? ?? ?? ?? F3 0F ?? ?? E8",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--TheGreatCircle.exe+1E210AA - 74 08                 	- je TheGreatCircle.exe+1E210B4
		--TheGreatCircle.exe+1E210AC - F3 0F 59 1D 0C AC AB 01  - mulss xmm3,[TheGreatCircle.exe+38DBCC0]
		--TheGreatCircle.exe+1E210B4 - F3 0F 11 18            	- movss [rax],xmm3
		--TheGreatCircle.exe+1E210B8 - F3 0F 59 1D 04 A5 AB 01  - mulss xmm3,[TheGreatCircle.exe+38DB5C4]
		--TheGreatCircle.exe+1E210C0 - 0F 28 C3               	- movaps xmm0,xmm3
	end
	
	local tAddress = HackTool:AddAddress("DOFS")
	
	if HackTool:SignatureScan("75 ?? 48 8B ?? ?? 80 B8 ?? ?? ?? ?? ?? 75 ?? 83 ?? ?? 75 ?? EB ?? C7 87",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		local DOF_1 = HackTool:AddAddress("DOF_1", tAddress)
		local DOF_2 = HackTool:AddAddress("DOF_2", tAddress)
		DOF_2:OffsetAddress(0x0D)

		print( DOF_1:GetInfo(TYPE_ADDRESS) )	
		print( DOF_2:GetInfo(TYPE_ADDRESS) )	
		--TheGreatCircle.exe+C800A7 - 80 BF 79 02 00 00 00  - cmp byte ptr [rdi+00000279],00
		--TheGreatCircle.exe+C800AE - 88 87 C5 02 00 00     - mov [rdi+000002C5],al
		--TheGreatCircle.exe+C800B4 - 75 14                 - jne TheGreatCircle.exe+C800CA <-- here
		--TheGreatCircle.exe+C800B6 - 48 8B 47 38           - mov rax,[rdi+38]
		--TheGreatCircle.exe+C800BA - 80 B8 93 05 00 00 00  - cmp byte ptr [rax+00000593],00
		--TheGreatCircle.exe+C800C1 - 75 07                 - jne TheGreatCircle.exe+C800CA <-- and here
		--TheGreatCircle.exe+C800C3 - 83 F9 02              - cmp ecx,02
		--TheGreatCircle.exe+C800C6 - 75 13                 - jne TheGreatCircle.exe+C800DB
	end

	local tAddress = HackTool:AddAddress("CINEMATICSFPS")
	
	if HackTool:SignatureScan("75 ?? 48 8B ?? ?? ?? ?? ?? 48 ?? ?? 74 ?? 39 ?? ?? 77",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		--TheGreatCircle.exe+184A5BC - 48 8B 41 28           - mov rax,[rcx+28]
		--TheGreatCircle.exe+184A5C0 - 48 39 98 08 03 00 00  - cmp [rax+00000308],rbx
		--TheGreatCircle.exe+184A5C7 - 75 1C                 - jne TheGreatCircle.exe+184A5E5
		--TheGreatCircle.exe+184A5C9 - 48 8B 80 00 03 00 00  - mov rax,[rax+00000300]
		--TheGreatCircle.exe+184A5D0 - 48 85 C0              - test rax,rax
	end
	
	local tAddress = HackTool:AddAddress("CINEMATICSFRAMEGEN")
	
	if HackTool:SignatureScan("38 ?? ?? 0F 85 ?? ?? ?? ?? 48 8B ?? ?? ?? ?? ?? 39 ?? ?? 74 ?? 33",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		--TheGreatCircle.exe+D2980A - 38 5F 5A              - cmp [rdi+5A],bl
		--TheGreatCircle.exe+D2980D - 0F 85 03 03 00 00     - jne TheGreatCircle.exe+D29B16
		--TheGreatCircle.exe+D29813 - 38 5F 5B              - cmp [rdi+5B],bl
		--TheGreatCircle.exe+D29816 - 0F 85 FA 02 00 00     - jne TheGreatCircle.exe+D29B16
		--TheGreatCircle.exe+D2981C - 48 8B 05 4D F0 61 03  - mov rax,[TheGreatCircle.exe+4348870]
	end

	return true
	
end

function Enable_Inject()
	local Variables = HackTool:AllocateMemory("Variables",50)
	Variables:PushFloat("WorldFOVIn")
	Variables:PushFloat("WorldFOVOut")
	Variables:PushFloat("CinematicsFOVIn")
	Variables:PushFloat("CinematicsFOVOut")
	Variables:PushFloat("CompensatedFOV")
	Variables:PushFloat("AdditionalWorldFOV")
	Variables:PushFloat("AdditionalCinematicsFOV")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("AspectRatio")	
	Variables:PushFloat("factor")
	
	Variables:Allocate()
	Variables["WorldFOVIn"]:WriteFloat(fDefaultFOV)
	Variables["WorldFOVOut"]:WriteFloat(fDefaultFOV)
	Variables["CinematicsFOVIn"]:WriteFloat(fDefaultFOV)
	Variables["CinematicsFOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalWorldFOV"]:WriteFloat(fAdditionalWorldFOV)
	Variables["AdditionalCinematicsFOV"]:WriteFloat(fAdditionalCinematicsFOV)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["AspectRatio"]:WriteFloat(fDefaultAspectRatio)
	Variables["factor"]:WriteFloat(fFactor)
	
	if fAspectRatio <= 2.34 then
		Variables["AspectRatio"]:WriteFloat(fAspectRatio)
	end
	
	ResolutionChanged()	
		
	local asm = [[		

		(codecave:jmp)WORLDFOV,WORLDFOV_cc:
			%originalcode%
			$$0 [(allocation)Variables->WorldFOVIn],$$1						$ctx=1
			addss $$1,[(allocation)Variables->AdditionalWorldFOV]			$ctx=1
			$$0 [(allocation)Variables->WorldFOVOut],$$1					$ctx=1
			jmp %returnaddress%
			%end%
								
		
		(codecave:jmp)CINEMATICSFOV,CINEMATICSFOV_cc:
			movss [(allocation)Variables->CinematicsFOVIn],$$2				$ctx=1
			fld dword ptr [(allocation)Variables->CinematicsFOVIn]
			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]					; calculate compensated FOV
			fstp dword ptr [(allocation)Variables->CompensatedFOV]			; and retrieve it			
			movss $$2,[(allocation)Variables->CompensatedFOV]				$ctx=1					
			addss $$2,[(allocation)Variables->AdditionalCinematicsFOV]		$ctx=1
			movss [(allocation)Variables->CinematicsFOVOut],$$2				$ctx=1			
			%originalcode%
			jmp %returnaddress%
			%end%
			
		(codecave)CINEMATICSFPS,CINEMATICSFPS_cc:
			nop
			nop
			%end%
			
		(codecave)CINEMATICSFRAMEGEN,CINEMATICSFRAMEGEN_cc:
			$$0 $$2,$$2														$ctx=1
			nop
			%end%
			
		(codecave)DOF_1,DOF_1_cc:
			nop
			nop
			%end%
		
		(codecave)DOF_2,DOF_2_cc:
			nop
			nop
			%end%
		
	]]
	
	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("WORLDFOV_cc",bWorldFOV)
		Toggle_CodeCave("CINEMATICSFOV_cc",bCinematicsFOV)
		Toggle_CodeCave("CINEMATICSFPS_cc",bCinematicsFPS)
		Toggle_CodeCave("CINEMATICSFRAMEGEN_cc",bCinematicsFrameGen)
		Toggle_CodeCave("DOF_1_cc",bDOF)
		Toggle_CodeCave("DOF_2_cc",bDOF)
	end	
			
	Write_World_FOV()
	Write_Cinematics_FOV()
	
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["WorldFOVIn"] and Variables["AdditionalWorldFOV"] then
		local fWorldFOVIn = Variables["WorldFOVIn"]:ReadFloat()
		local fWorldFOVOut = Variables["WorldFOVOut"]:ReadFloat()
		local fCinematicsFOVIn = Variables["CinematicsFOVIn"]:ReadFloat()
		local fCinematicsFOVOut = Variables["CinematicsFOVOut"]:ReadFloat()
		local fCompensatedCinematicsFOV = Variables["CompensatedFOV"]:ReadFloat()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nWorld FOV In: %.2f, Out : %.2f\r\nCinematics FOV In: %.2f, Compensated: %.2f, Out: %.2f", fWorldFOVIn, fWorldFOVOut, fCinematicsFOVIn, fCompensatedCinematicsFOV, fCinematicsFOVOut))
	end
	
end

function WorldFOVSlider_Changed(Sender)

	fAdditionalWorldFOV = Sender:GetPosition() - 20
	lblWorldFOVSlider.Caption:SetCaption( string.format("World FOV: %.0f",fAdditionalWorldFOV) )
		
	Write_World_FOV()	
	ForceUpdate()

end

function CinematicsFOVSlider_Changed(Sender)

	fAdditionalCinematicsFOV = Sender:GetPosition() - 20
	lblCinematicsFOVSlider.Caption:SetCaption( string.format("Cinematics FOV: %.0f",fAdditionalCinematicsFOV) )
		
	Write_Cinematics_FOV()	
	ForceUpdate()
	
end

function Write_World_FOV()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AdditionalWorldFOV"] then
		if bWorldFOV == true then
			Variables["AdditionalWorldFOV"]:WriteFloat(fAdditionalWorldFOV)
		else
			Variables["AdditionalWorldFOV"]:WriteFloat(0)
		end
	end

end

function Write_Cinematics_FOV()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AdditionalCinematicsFOV"] then
		if bCinematicsFOV == true then
			Variables["AdditionalCinematicsFOV"]:WriteFloat(fAdditionalCinematicsFOV)
		else
			Variables["AdditionalCinematicsFOV"]:WriteFloat(0)
		end
	end
	
end

function Disable_Inject()
	
	CleanUp()
	
end

function CKWorldFOVFix_Changed(Sender)

	bWorldFOV = Toggle_CheckFix(Sender)
	Write_World_FOV()
	Toggle_CodeCave("WORLDFOV_cc",bWorldFOV)
	ForceUpdate()
	
end

function CKCinematicsFOVFix_Changed(Sender)

	bCinematicsFOV = Toggle_CheckFix(Sender)
	Toggle_CodeCave("CINEMATICSFOV_cc",bCinematicsFOV)
	ForceUpdate()
	
end

function CKDOFFix_Changed(Sender)

	bDOF = Toggle_CheckFix(Sender)
	Toggle_CodeCave("DOF_1_cc",bDOF)
	Toggle_CodeCave("DOF_2_cc",bDOF)
	ForceUpdate()

end

function CKCINEMATICSFPSFix_Changed(Sender)

	bCinematicsFPS = Toggle_CheckFix(Sender)
	Toggle_CodeCave("CINEMATICSFPS_cc",bCinematicsFPS)
	ForceUpdate()
	
end

function CKCINEMATICSFRAMEGENFix_Changed(Sender)

	bCinematicsFrameGen = Toggle_CheckFix(Sender)
	Toggle_CodeCave("CINEMATICSFRAMEGEN_cc",bCinematicsFrameGen)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
