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

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

--ControlVars
bFixEnabled = true
bFOV = true
bPillarBoxing = true
bAspect = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "SHCO.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,70,0,1)
	
	FOVSlider:SetTickFrequency(5)
	FOVSlider:SetLabel1Text("0")
	FOVSlider:SetLabel2Text("70")

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKPBFix_Enable","Pillar boxing fix","CKPBFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKARFix_Enable","Aspect ratio fix","CKARFix_Changed",255,141,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("E9 ?? ?? ?? ?? F3 ?? ?? ?? ?? F3 ?? ?? ?? ?? ?? ?? ?? 41 8B ?? ??",tAddress,PAGE_EXECUTE_READ,0x05,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--SHCO.exe+22E376C - 44 0F 29 40 C8         - movaps [rax-38],xmm8
		--SHCO.exe+22E3771 - 44 0F 29 48 B8         - movaps [rax-48],xmm9
		--SHCO.exe+22E3776 - 0F 84 1F 02 00 00      - je SHCO.exe+22E399B
		--SHCO.exe+22E377C - F3 0F 10 51 2C         - movss xmm2,[rcx+2C]
		--SHCO.exe+22E3781 - 48 8D 55 A0            - lea rdx,[rbp-60]
	end
	
	local tAddress = HackTool:AddAddress("FOV2")
	
	if HackTool:SignatureScan("F3 ?? ?? ?? ?? E9 ?? ?? ?? ?? F3 0F ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? ",tAddress,PAGE_EXECUTE_READ,0x0a,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--SHCO.exe+22E3877 - F3 44 0F 10 C0         	- movss xmm8,xmm0
		--SHCO.exe+22E387C - E9 83 00 00 00           	- jmp SHCO.exe+22E3904
		--SHCO.exe+22E3881 - F3 0F 10 43 18         	- movss xmm0,[rbx+18] <<==
		--SHCO.exe+22E3886 - F3 0F 5F 05 5A 03 F5 00   	- maxss xmm0,[SHCO.exe+3233BE8]
		--SHCO.exe+22E388E - F3 0F 10 35 3E DE EC 01   	- movss xmm6,[SHCO.exe+41B16D4]
	end
	
	local tAddress = HackTool:AddAddress("PILLARBOXING")
	
	if HackTool:SignatureScan("F6 41 ?? 01 48 8B ?? F3 44",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--SHCO.exe+22E373E - 44 0F 29 50 A8         	- movaps [rax-58],xmm10
		--SHCO.exe+22E3743 - 49 81 C1 90 00 00 00     	- add r9,00000090
		--SHCO.exe+22E374A - F6 41 30 01           		- test byte ptr [rcx+30],01 <<==
		--SHCO.exe+22E374E - 48 8B D9              		- mov rbx,rcx
		--SHCO.exe+22E3751 - F3 44 0F 10 15 42 7E F0 00 - movss xmm10,[SHCO.exe+31EB59C]
	end
	
	local tAddress = HackTool:AddAddress("ASPECT")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? 0F ?? ?? 76 ?? 0F ?? ?? 76 ?? F3 0F ?? ?? F3 0F ?? ?? F3 0F ?? ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--SHCO.exe+6438B1 - F3 0F 11 4B 10         	- movss [rbx+10],xmm1
		--SHCO.exe+6438B6 - EB 38                 	- jmp SHCO.exe+6438F0
		--SHCO.exe+6438B8 - F3 0F 10 95 CC 07 00 00 - movss xmm2,[rbp+000007CC] <<==
		--SHCO.exe+6438C0 - 0F 2F D4               	- comiss xmm2,xmm4
		--SHCO.exe+6438C3 - 76 29                 	- jna SHCO.exe+6438EE
	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:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["DefaultAspectRatio"]:WriteFloat(fDefaultAspectRatio)
	Variables["AspectRatio"]:WriteFloat(fAspectRatio)
	Variables["factor"]:WriteFloat(fFactor)
	
	ResolutionChanged()	
	
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			$$0 [(allocation)Variables->FOVIn],$$1 						$ctx=1	; retrieves FOV from xmm register
			fld dword ptr [$$2]											$ctx=1
			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 $$1,[(allocation)Variables->CompensatedFOV]				$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]			$ctx=1
			$$0 [(allocation)Variables->FOVOut],$$1						$ctx=1
			jmp %returnaddress%
			%end%			

		(codecave:jmp)FOV2,FOV2_cc:
			%originalcode%
			$$0 [(allocation)Variables->FOVIn],$$1						$ctx=1
			$$0 [(allocation)Variables->CompensatedFOV],$$1				$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]			$ctx=1
			$$0 [(allocation)Variables->FOVOut],$$1						$ctx=1
			jmp %returnaddress%
			%end%		

		(codecave:jmp)ASPECT,ASPECT_cc:
			$$0 $$1,[(allocation)Variables->AspectRatio]				$ctx=1
			jmp %returnaddress%
			%end%

		(codecave)PILLARBOXING,PILLARBOXING_cc:
			test byte ptr [$$1],0										$ctx=1

	]]

	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)	
		Toggle_CodeCave("FOV2_cc",bFOV)	
		Toggle_CodeCave("PILLARBOXING_cc",bPillarBoxing)
		Toggle_CodeCave("ASPECT_cc",bAspect)
	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)

	fAdditionalFOV = Sender:GetPosition()
	lblFOVSlider.Caption:SetCaption( string.format("Value: +%.0f",fAdditionalFOV) )
		
	if bFOV == true then
		Write_FOV()
	end
	
	ForceUpdate()

end

function Write_FOV()

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

end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKPBFix_Changed(Sender)

	bPillarBoxing = Toggle_CheckFix(Sender)
	Toggle_CodeCave("PILLARBOXING_cc",bPillarBoxing)
	ForceUpdate()

end

function CKARFix_Changed(Sender)

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

end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
