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

--GAME VARS
fDefaultFOV = 85
fAdditionalFOV = 0
fAdditionalCinematicsFOV = 0
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fFactor = 0.00872665
fSharpening = 0

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

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "b1-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","In game FOV fine adjustment",15,70,210,17)	
	DefaultControls.AddFixedFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35,0,70,0,1)
	FOVSlider:SetTickFrequency(10)
	DefaultControls.AddHeader("Header_Sharpening","Image sharpening adjustment",15,165,210,17)
	DefaultControls.AddFixedFOVSlider("SharpeningSlider","SharpeningSlider_Changed",55,195,125,35,0,100,0,1)
	SharpeningSlider:SetTickFrequency(10)
	SharpeningSlider:SetLabel1Text("0")
	SharpeningSlider:SetLabel2Text("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("CKFRINGEFOVFix_Enable","Chromatic aberrations fix","CKFRINGEFix_Changed",255,161,180,14)
	DefaultControls.AddFixToggle("CKSHARPENINGFix_Enable","Sharpening fix","CKSHARPENINGFix_Changed",255,181,180,14)
	DefaultControls.AddFixToggle("CKVIGNETTINGFix_Enable","Vignetting fix","CKVIGNETTINGFix_Changed",255,201,180,14)
	DefaultControls.AddFixToggle("CKFOGGFix_Enable","Fog fix","CKFOGFix_Changed",255,221,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("C6 84 ?? ?? ?? ?? ?? ?? 48 ?? ?? 74 ?? E8 ?? ?? ?? ?? 0F ?? ?? 48 8B",tAddress,PAGE_EXECUTE_READ,0x12,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--b1-Win64-Shipping.exe+E9F874D - 74 05                    - je b1-Win64-Shipping.exe+E9F8754
		--b1-Win64-Shipping.exe+E9F874F - E8 2C FD 27 FE           - call b1-Win64-Shipping.exe+CC78480
		--b1-Win64-Shipping.exe+E9F8754 - 0F 28 C6                 - movaps xmm0,xmm6
		--b1-Win64-Shipping.exe+E9F8757 - 48 8B 8C 24 C0 08 00 00  - mov rcx,[rsp+000008C0]
		--b1-Win64-Shipping.exe+E9F875F - 48 33 CC                 - xor rcx,rsp
	end
	
	local tResultCount = HackTool:SignatureScanMulti("48 89 ?? ?? ?? 57 48 83 ?? ?? 0F ?? ?? 48 ?? ?? 48 ?? ?? 0F ?? ?? F2 0F ?? ?? ?? F2 0F ?? ?? ?? 0F 10 ?? ?? 0F 11 ?? ?? F2 0F","ASPECTS",PAGE_EXECUTE_READ,0x53,Process_EXEName)
	
	if tResultCount ~= 2 then
		return ErrorOccurred("Could not find ASPECTs injection point, " .. Process_FriendlyName .. 
							 " may have updated to a version that is no longer supported.\r\n\r\n" .. 
							 "Try selecting a different version and re-enable the fix."	)
	else
		local tAddress = HackTool:AddAddress("ASPECT", HackTool:GetAddress( string.format("ASPECTS%d",1) ))
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--b1-Win64-Shipping.exe+B5B670D - 89 41 40              - mov [rcx+40],eax
		--b1-Win64-Shipping.exe+B5B6710 - 8B 42 44              - mov eax,[rdx+44]
		--b1-Win64-Shipping.exe+B5B6713 - 89 41 44              - mov [rcx+44],eax
		--b1-Win64-Shipping.exe+B5B6716 - 8B 49 48              - mov ecx,[rcx+48]
		--b1-Win64-Shipping.exe+B5B6719 - 33 4A 48              - xor ecx,[rdx+48]
	end
	
	local tAddress = HackTool:AddAddress("DOF")
	
	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) )
		--b1-Win64-Shipping.exe+D6E4FE0 - 48 8B 05 91 BC 11 10  - mov rax,[b1-Win64-Shipping.exe+1D800C78]
		--b1-Win64-Shipping.exe+D6E4FE7 - 48 8B CB              - mov rcx,rbx
		--b1-Win64-Shipping.exe+D6E4FEA - 8B 78 04              - mov edi,[rax+04]
		--b1-Win64-Shipping.exe+D6E4FED - E8 2E 86 3B 01        - call b1-Win64-Shipping.exe+EA9D620
		--b1-Win64-Shipping.exe+D6E4FF2 - 48 63 C8              - movsxd  rcx,eax
	end
	
	local tAddress = HackTool:AddAddress("FRINGE")
	
	if HackTool:SignatureScan("7F ?? 89 B3 ?? ?? ?? ?? 8B ?? ?? 39 05 ?? ?? ?? ?? 0F",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--b1-Win64-Shipping.exe+EA98935 - 48 8B 05 6C 0A E3 0E  - mov rax,[b1-Win64-Shipping.exe+1D8C93A8]
		--b1-Win64-Shipping.exe+EA9893C - 39 30                 - cmp [rax],esi
		--b1-Win64-Shipping.exe+EA9893E - 7F 06                 - jg b1-Win64-Shipping.exe+EA98946
		--b1-Win64-Shipping.exe+EA98940 - 89 B3 AC 1D 00 00     - mov [rbx+00001DAC],esi
		--b1-Win64-Shipping.exe+EA98946 - 8B 04 2F              - mov eax,[rdi+rbp]
	end
	
	local tAddress = HackTool:AddAddress("SHARPENING")
	
	if HackTool:SignatureScan("F3 44 ?? ?? ?? ?? 44 0F ?? ?? 73 ?? 45 0F ?? ?? EB",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--b1-Win64-Shipping.exe+D767FBA - 44 0F 28 F8           - movaps xmm15,xmm0
		--b1-Win64-Shipping.exe+D767FBE - 48 8B 05 93 50 0A 10  - mov rax,[b1-Win64-Shipping.exe+1D80D058]
		--b1-Win64-Shipping.exe+D767FC5 - F3 44 0F10 60 04      - movss xmm12,[rax+04]
		--b1-Win64-Shipping.exe+D767FCB - 44 0F 2F E7           - comiss xmm12,xmm7
		--b1-Win64-Shipping.exe+D767FCF - 73 06                 - jae b1-Win64-Shipping.exe+D767FD7
	end
	
	local tAddress = HackTool:AddAddress("VIGNETTING")
	
	if HackTool:SignatureScan("8B ?? 83 ?? ?? 7D ?? 89 B3 ?? ?? ?? ?? EB",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--b1-Win64-Shipping.exe+EA989B3 - C7 83 80 1F 00 00 00 00 80 3F - mov [rbx+00001F80],3F800000
		--b1-Win64-Shipping.exe+EA989BD - 48 8B 05 44 FB E2 0E     		- mov rax,[b1-Win64-Shipping.exe+1D8C8508]
		--b1-Win64-Shipping.exe+EA989C4 - 8B 08                 		- mov ecx,[rax]
		--b1-Win64-Shipping.exe+EA989C6 - 83 F9 02              		- cmp ecx,02
		--b1-Win64-Shipping.exe+EA989C9 - 7D 08                 		- jnl b1-Win64-Shipping.exe+EA989D3
	end
	
	local tAddress = HackTool:AddAddress("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) )
		--b1-Win64-Shipping.exe+D40D79D - 48 8B 05 A4 08 3A 10  - mov rax,[b1-Win64-Shipping.exe+1D7AE048]
		--b1-Win64-Shipping.exe+D40D7A4 - 83 78 04 01           - cmp dword ptr [rax+04],01
		--b1-Win64-Shipping.exe+D40D7A8 - 75 04                 - jne b1-Win64-Shipping.exe+D40D7AE
		--b1-Win64-Shipping.exe+D40D7AA - B3 01                 - mov bl,01
		--b1-Win64-Shipping.exe+D40D7AC - EB 02                 - jmp b1-Win64-Shipping.exe+D40D7B0
	end

	return true
	
end

function Enable_Inject()
	local NearTo = HackTool:GetModuleAddress(Process_EXEName)
	local Variables = HackTool:AllocateMemory("Variables", 0)
	Variables:PushFloat("CompensatedFOV")
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("CinematicsFOVIn")
	Variables:PushFloat("CinematicsFOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("Sharpening")
	Variables:PushFloat("factor")
	
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["AspectRatio"]:WriteFloat(fAspectRatio)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["Sharpening"]:WriteFloat(fSharpening)
	Variables["factor"]:WriteFloat(fFactor)
	
	ResolutionChanged()	

	local asm = [[		

		(codecave:jmp)FOV,FOV_cc:		
			movss [(allocation)Variables->FOVIn],$$2						$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 $$2,[(allocation)Variables->CompensatedFOV]				$ctx=1					
			addss $$2,[(allocation)Variables->AdditionalFOV]				$ctx=1
			movss [(allocation)Variables->FOVOut],$$2						$ctx=1			
			%originalcode%
			jmp %returnaddress%
			%end%
		
		(codecave:jmp)ASPECT,ASPECT_cc:
			fld dword ptr [rdx+0x44]
			fstp dword ptr [(allocation)Variables->AspectRatio]
			$$0 $$2,[(allocation)Variables->ScreenRatio]					$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
						
		(codecave:jmp)SHARPENING,SHARPENING_cc:
			$$0 $$1,[(allocation)Variables->Sharpening]						$ctx=1
			jmp %returnaddress%
			%end%
			
		(codecave)FRINGE,FRINGE_cc:
			nop
			nop
			%end%
			
		(codecave)DOF,DOF_cc:
			xor $$1,$$1														$ctx=1
			nop
			%end%
			
		(codecave)VIGNETTING,VIGNETTING_cc:
			xor $$1,$$1														$ctx=1
			%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("SHARPENING_cc",bSharpening)
		Toggle_CodeCave("VIGNETTING_cc",bVignetting)
		Toggle_CodeCave("FOG_cc",bFog)
	end	
			
	Write_FOV()
	Write_Sharpening()
	
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 CinematicsFOVSlider_Changed(Sender)
	
	fAdditionalCinematicsFOV = Sender:GetPosition() - 20
	lblCinematicsFOVSlider.Caption:SetCaption( string.format("Cinematics FOV: %.0f",fAdditionalCinematicsFOV) )
	Write_Cinematics_FOV()	
	ForceUpdate()

end

function FRINGESlider_Changed(Sender)

	fFRINGE = Sender:GetPosition()
	lblFRINGESlider.Caption:SetCaption( string.format("Value: %.2f",fFRINGE / 100) )
	Write_FRINGE()
	ForceUpdate()
	
end

function SharpeningSlider_Changed(Sender)

	fSharpening = Sender:GetPosition() / 10
	lblSharpeningSlider.Caption:SetCaption( string.format("Value: %.2f",fSharpening) )
	Write_Sharpening()
	ForceUpdate()
	
end

function Write_Sharpening()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["Sharpening"] then
		if bSharpening == true then
			Variables["Sharpening"]:WriteFloat(fSharpening)
		else
			Variables["Sharpening"]:WriteFloat(0)
		end
	end
	
end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKFRINGEFix_Changed(Sender)

	bFringe = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FRINGE_cc",bFringe)
	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"] and not bAspect then	
		Variables["AspectRatio"]:WriteFloat(fAspectRatio) 
	end
	ForceUpdate()
	
end

function CKDOFFix_Changed(Sender)

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

function CKSHARPENINGFix_Changed(Sender)

	bSharpening = Toggle_CheckFix(Sender)
	Write_Sharpening()
	Toggle_CodeCave("SHARPENING_cc",bSharpening)
	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 ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
