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

--GAME VARS
fDefaultFOV = 80
fAdditionalFOV = 0
fAdditionalCinematicsFOV = 0
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fAspectRatio169 = 1.6 --1.77778
fFactor = 0.00872665

--ControlVars
bFixEnabled = true
bFOV = true
bAspect = true
bDOF = true
bFringe = true
bVignetting = true
bFog = true
bFPS = true

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

--INJECTION BEHAVIOUR
InjectDelay = 100
WriteInterval = 100
SearchInterval = 100
SuspendThread = false

--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","In game FOV fine adjustment",15,70,210,17)	
	DefaultControls.AddFixedFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35,0,90,0,1)
	FOVSlider:SetTickFrequency(10)

	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)
	DefaultControls.AddFixToggle("CKFRINGEFix_Enable","Chromatic aberrations fix","CKFRINGEFix_Changed",255,161,180,14)
	DefaultControls.AddFixToggle("CKVIGNETTINGFix_Enable","Vignetting fix","CKVIGNETTINGFix_Changed",255,181,180,14)
	DefaultControls.AddFixToggle("CKFOGFix_Enable","Fog fix","CKFOGFix_Changed",255,201,180,14)
	DefaultControls.AddFixToggle("CKFPSFix_Enable","Cinematics FPS fix","CKFPSFix_Changed",255,221,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("77 ?? 48 8B ?? FF 90 ?? ?? ?? ?? F3 0F ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x0b,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--SHProto-Win64-Shipping.exe+4E28404 - 48 8B 01              - mov rax,[rcx]
		--SHProto-Win64-Shipping.exe+4E28407 - FF 90 10 07 00 00     - call qword ptr [rax+00000710]
		--SHProto-Win64-Shipping.exe+4E2840D - F3 0F 10 40 30        - movss xmm0,[rax+30]
		--SHProto-Win64-Shipping.exe+4E28412 - 48 83 C4 28           - add rsp,28
		--SHProto-Win64-Shipping.exe+4E28416 - C3                    - ret 
	end
	
	local tAddress = HackTool:AddAddress("ASPECT")
	
	if HackTool:SignatureScan("C7 47 ?? ?? ?? ?? ?? C7 83 ?? ?? ?? ?? ?? ?? ?? ?? 48 8B ?? ?? ?? 48 83",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--SHProto-Win64-Shipping.exe+299861A - 83 4F 4C 01           		 - or dword ptr [rdi+4C],01
		--SHProto-Win64-Shipping.exe+299861E - 80 8B B4 02 00 00 01  		 - or byte ptr [rbx+000002B4],01
		--SHProto-Win64-Shipping.exe+2998625 - C7 47 48 39 8E E3 3F   		 - mov [rdi+48],3FE38E39
		--SHProto-Win64-Shipping.exe+299862C - C7 83 B0 02 00 00 39 8E E3 3F - mov [rbx+000002B0],3FE38E39
		--SHProto-Win64-Shipping.exe+2998636 - 48 8B 5C 24 48        		 - mov rbx,[rsp+48]
	end
	
	local tAddress = HackTool:AddAddress("DOF") -- r.DepthOfFieldQuality
	
	if HackTool:SignatureScan("48 ?? ?? 8B ?? ?? E8 ?? ?? ?? ?? 48 ?? ?? 48 6B ?? ?? 48 8D",tAddress,PAGE_EXECUTE_READ,0x03,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--SHProto-Win64-Shipping.exe+364DC34 - 48 8B 05 BD E6 14 05  - mov rax,[SHProto-Win64-Shipping.exe+879C2F8]
		--SHProto-Win64-Shipping.exe+364DC3B - 48 8B CB              - mov rcx,rbx
		--SHProto-Win64-Shipping.exe+364DC3E - 8B 78 04              - mov edi,[rax+04]
		--SHProto-Win64-Shipping.exe+364DC41 - E8 4A DF 87 01        - call SHProto-Win64-Shipping.exe+4ECBB90
		--SHProto-Win64-Shipping.exe+364DC46 - 48 63 C8              - movsxd  rcx,eax
	end
	
	local tAddress = HackTool:AddAddress("FRINGE") -- r.SceneColorFringeQuality
	
	if HackTool:SignatureScan("7F ?? 89 B3 ?? ?? ?? ?? 8B ?? ?? 39 05 ?? ?? ?? ?? 7E",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--SHProto-Win64-Shipping.exe+4EC9657 - 48 8B 05 82 23 9A 03  - mov rax,[SHProto-Win64-Shipping.exe+886B9E0]
		--SHProto-Win64-Shipping.exe+4EC965E - 39 30                 - cmp [rax],esi
		--SHProto-Win64-Shipping.exe+4EC9660 - 7F 06                 - jg SHProto-Win64-Shipping.exe+4EC9668
		--SHProto-Win64-Shipping.exe+4EC9662 - 89 B3 DC 1D 00 00     - mov [rbx+00001DDC],esi
		--SHProto-Win64-Shipping.exe+4EC9668 - 8B 04 2F              - mov eax,[rdi+rbp]
	end
	
	local tAddress = HackTool:AddAddress("VIGNETTING") -- r.Tonemapper.Quality
	
	if HackTool:SignatureScan("8B ?? 83 ?? ?? 7D ?? 89 B3 ?? ?? ?? ?? EB ?? 83",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--SHProto-Win64-Shipping.exe+4EC97AE - C7 83 B8 1F 00 00 00 00 80 3F - mov [rbx+00001FB8],3F800000
		--SHProto-Win64-Shipping.exe+4EC97B8 - 48 8B 05 81 1E 9A 03    		 - mov rax,[SHProto-Win64-Shipping.exe+886B640]
		--SHProto-Win64-Shipping.exe+4EC97BF - 8B 08                 		 - mov ecx,[rax]
		--SHProto-Win64-Shipping.exe+4EC97C1 - 83 F9 02              		 - cmp ecx,02
		--SHProto-Win64-Shipping.exe+4EC97C4 - 7D 08                 		 - jnl SHProto-Win64-Shipping.exe+4EC97CE
	end
	
	local tAddress = HackTool:AddAddress("FOG") -- r.fog
	
	if HackTool:SignatureScan("75 ?? B3 ?? EB ?? 32 ?? 48 8B ?? ?? ?? 48 ?? ?? 74 ?? E8 ?? ?? ?? ?? 0F ?? ?? 48 8B",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--SHProto-Win64-Shipping.exe+33636ED - 48 8B 05 84 3D 3E 05  - mov rax,[SHProto-Win64-Shipping.exe+8747478]
		--SHProto-Win64-Shipping.exe+33636F4 - 83 78 04 01           - cmp dword ptr [rax+04],01
		--SHProto-Win64-Shipping.exe+33636F8 - 75 04                 - jne SHProto-Win64-Shipping.exe+33636FE
		--SHProto-Win64-Shipping.exe+33636FA - B3 01                 - mov bl,01
		--SHProto-Win64-Shipping.exe+33636FC - EB 02                 - jmp SHProto-Win64-Shipping.exe+3363700
	end

	print ("here")

	local tAddress = HackTool:AddAddress("CINEMATICSFPS")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? EB ?? 0F ?? ?? 48 8B ?? ?? ?? 0F 28",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print ("here again")
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--SHProto-Win64-Shipping.exe+5048E78 - 74 02                 - je SHProto-Win64-Shipping.exe+5048E7C
		--SHProto-Win64-Shipping.exe+5048E7A - 33 DB                 - xor ebx,ebx
		--SHProto-Win64-Shipping.exe+5048E7C - F3 0F 10 04 1F        - movss xmm0,[rdi+rbx]
		--SHProto-Win64-Shipping.exe+5048E81 - EB 03                 - jmp SHProto-Win64-Shipping.exe+5048E86
		--SHProto-Win64-Shipping.exe+5048E83 - 0F 28 C6              - movaps xmm0,xmm6
	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%
			movss [(allocation)Variables->FOVIn],$$1						$ctx=1
			fld 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]					; calculate compensated FOV
			fstp dword ptr [(allocation)Variables->CompensatedFOV]			; and retrieve it			
			movss $$1,[(allocation)Variables->CompensatedFOV]				$ctx=1					
			addss $$1,[(allocation)Variables->AdditionalFOV]				$ctx=1
			movss [(allocation)Variables->FOVOut],$$1						$ctx=1			
			jmp %returnaddress%
			%end%
		
		(codecave:jmp)ASPECT,ASPECT_cc:
			fld dword ptr [(allocation)Variables->ScreenRatio]
			fstp dword ptr [$$1]											$ctx=1
			jmp %returnaddress%
			%end%
		
		
		(codecave:jmp)CINEMATICSFPS,CINEMATICSFPS_cc:
			$$0 $$1,[noFPSLimit]											$ctx=1
			jmp %returnaddress%
			%end%
			
		noFPSLimit:
			(float)0
						
		(codecave)FRINGE,FRINGE_cc:
			nop
			nop
			%end%
			
		(codecave)VIGNETTING,VIGNETTING_cc:
			xor $$1,$$1														$ctx=1
			%end%
			
		(codecave)DOF,DOF_cc:
			xor $$1,$$1														$ctx=1
			nop
			%end%
			
		(codecave)FOG,FOG_cc:
			jmp $$1															$ctx=1
			%end%
			
	]]
	
	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("ASPECT_cc",bAspect)
		Toggle_CodeCave("DOF_cc",bDOF)
		Toggle_CodeCave("FRINGE_cc",bFringe)
		Toggle_CodeCave("VIGNETTING_cc",bVignetting)
		Toggle_CodeCave("FOG_cc",bFog)
		Toggle_CodeCave("CINEMATICSFPS_cc",bFPS)
	end	
			
	Write_FOV()
	
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()
		local fCompensated = Variables["CompensatedFOV"]:ReadFloat()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %.2f, Compensated; %.2f, Out : %.2f", fFOVIn, fCompensated, fFOVOut))
	end
	
end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetPosition() - 20
	lblFOVSlider.Caption:SetCaption( string.format("World FOV: %.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 CKFOVFix_Changed(Sender)

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

function CKARFix_Changed(Sender)

	bAspect = Toggle_CheckFix(Sender)
	local Variables = HackTool:GetAllocation("Variables")
	Toggle_CodeCave("ASPECT_cc",bAspect)
	if Variables and Variables["AspectRatio"] then
		if not bAspect then
			Variables["AspectRatio"]:WriteFloat(fAspectRatio) 
		else
			Variables["AspectRatio"]:WriteFloat(fAspectRatio169) 
		end
	end
	ForceUpdate()
	
end

function CKDOFFix_Changed(Sender)

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

function CKFRINGEFix_Changed(Sender)

	bFringe = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FRINGE_cc",bFringe)
	ForceUpdate()

end

function CKVIGNETTINGFix_Changed(Sender)

	bVignetting = Toggle_CheckFix(Sender)
	Toggle_CodeCave("VIGNETTING_cc",bVignetting)
	ForceUpdate()
	
end

function CKFOGFix_Changed(Sender)

	bFog = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FOG_cc",bFog)
	ForceUpdate()
	
end

function CKFPSFix_Changed(Sender)

	bFPS = Toggle_CheckFix(Sender)
	Toggle_CodeCave("CINEMATICSFPS_cc",bFPS)
	ForceUpdate()

end

function Disable_Inject()
	
	CleanUp()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
