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

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

--ControlVars
bFixEnabled = true
bFOV = true
bAspect = true
bDOF = true
bLensDistortion = true
bLensSoftness = true
bFringe = true
bFilmGrain = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "UnrealWindow"
Process_EXEName = "Hellblade2-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","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)
	FOVSlider:SetLabel1Text("-20")
	FOVSlider:SetLabel2Text("+50")

	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("CKLENSDISTORTIONFix_Enable","Lens distortion fix","CKLENSDISTORTIONFix_Changed",255,161,180,14)
	DefaultControls.AddFixToggle("CKLENSSOFTNESSFix_Enable","Lens softness fix","CKLENSSOFTNESSFix_Changed",255,181,180,14)
	DefaultControls.AddFixToggle("CKFRINGEFix_Enable","Chromatic aberrations fix","CKFRINGEFix_Changed",255,201,180,14)
	DefaultControls.AddFixToggle("CKFILMGRAINFix_Enable","Film grain fix","CKFILMGRAINFix_Changed",255,221,180,14)

end

function Configure_SignatureScan() 
	
	print ("test")
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("48 ?? ?? FF 90 ?? ?? ?? ?? F3 0F ?? ?? ?? 48 ?? ?? 48 ?? ?? 4C",tAddress,PAGE_EXECUTE_READ,0x09,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Hellblade2-Win64-Shipping.exe+3451CA4 - 48 8B 01              - mov rax,[rcx]
		--Hellblade2-Win64-Shipping.exe+3451CA7 - FF 90 58 07 00 00     - call qword ptr [rax+00000758]
		--Hellblade2-Win64-Shipping.exe+3451CAD - F3 0F 10 40 30        - movss xmm0,[rax+30]
		--Hellblade2-Win64-Shipping.exe+3451CB2 - 48 83 C4 28           - add rsp,28
		--Hellblade2-Win64-Shipping.exe+3451CB6 - C3                    - ret 
	end
	
	local tResultCount = HackTool:SignatureScanMulti("48 89 ?? ?? ?? 57 48 83 ?? ?? 0F ?? ?? 48 8B ?? 48 8B FA 0F ?? ?? F2 0F ?? ?? ?? F2 0F ?? ?? ??","ASPECTS",PAGE_EXECUTE_READ,0x59,Process_EXEName)
	
	if tResultCount ~= 3 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) )	
		--Hellblade2-Win64-Shipping.exe+2C63563 - 89 41 44              - mov [rcx+44],eax
		--Hellblade2-Win64-Shipping.exe+2C63566 - 8B 42 48              - mov eax,[rdx+48]
		--Hellblade2-Win64-Shipping.exe+2C63569 - 89 41 48              - mov [rcx+48],eax
		--Hellblade2-Win64-Shipping.exe+2C6356C - 48 8D 42 4C           - lea rax,[rdx+4C]
		--Hellblade2-Win64-Shipping.exe+2C63570 - 48 83 C1 4C           - add rcx,4C		
	end	

	local tAddress = HackTool:AddAddress("DOF")
	
	if HackTool:SignatureScan("75 ?? BF ?? ?? ?? ?? 8B ?? ?? 48 ?? ?? E8",tAddress,PAGE_EXECUTE_READ,0x07,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Hellblade2-Win64-Shipping.exe+1FD54C3 - 75 05                 - jne Hellblade2-Win64-Shipping.exe+1FD54CA
		--Hellblade2-Win64-Shipping.exe+1FD54C5 - BF 04 00 00 00        - mov edi,00000004
		--Hellblade2-Win64-Shipping.exe+1FD54CA - 8B 3C 37              - mov edi,[rdi+rsi]
		--Hellblade2-Win64-Shipping.exe+1FD54CD - 48 8B CB              - mov rcx,rbx
		--Hellblade2-Win64-Shipping.exe+1FD54D0 - E8 2B 05 55 01        - call Hellblade2-Win64-Shipping.exe+3525A00
	end

	local tAddress = HackTool:AddAddress("LENSDISTORTION")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? F3 41 0F ?? ?? ?? ?? ?? ?? 49 8B ?? ?? ?? ?? ?? 48",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Hellblade2-Win64-Shipping.exe+200F2AF - 48 8B 05 D2 8A 89 08     	  - mov rax,[Hellblade2-Win64-Shipping.exe+A8A7D88]
		--Hellblade2-Win64-Shipping.exe+200F2B6 - F3 41 0F 10 85 60 22 00 00  - movss xmm0,[r13+00002260]
		--Hellblade2-Win64-Shipping.exe+200F2BF - F3 0F 59 40 04         	  - mulss xmm0,[rax+04]
		--Hellblade2-Win64-Shipping.exe+200F2C4 - F3 41 0F 11 87 B8 00 00 00  - movss [r15+000000B8],xmm0
		--Hellblade2-Win64-Shipping.exe+200F2CD - 49 8B 8D 70 22 00 00     	  - mov rcx,[r13+00002270]
	end
	
	local tAddress = HackTool:AddAddress("LENSSOFTNESS")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? 8D ?? ?? ?? ?? F3 41 0F ?? ?? ?? ?? ?? ?? 41",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Hellblade2-Win64-Shipping.exe+200E3ED - F3 41 0F 10 86 18 23 00 00  - movss xmm0,[r14+00002318]
		--Hellblade2-Win64-Shipping.exe+200E3F6 - 8B B5 18 01 00 00        	  - mov esi,[rbp+00000118]
		--Hellblade2-Win64-Shipping.exe+200E3FC - F3 0F 59 40 04         	  - mulss xmm0,[rax+04]
		--Hellblade2-Win64-Shipping.exe+200E401 - 8D 1C 73              	  - lea ebx,[rbx+rsi*2]
		--Hellblade2-Win64-Shipping.exe+200E404 - 03 DE                 	  - add ebx,esi
	end
	
	local tAddress = HackTool:AddAddress("FRINGE")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? F3 41 0F ?? ?? ?? ?? ?? ?? 48 8B ?? ?? ?? ?? ?? E8",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Hellblade2-Win64-Shipping.exe+200D974 - 48 8B 05 F5 A3 89 08     	  - mov rax,[Hellblade2-Win64-Shipping.exe+A8A7D70]
		--Hellblade2-Win64-Shipping.exe+200D97B - F3 0F 10 86 E8 22 00 00     - movss xmm0,[rsi+000022E8]
		--Hellblade2-Win64-Shipping.exe+200D983 - F3 0F 59 40 04         	  - mulss xmm0,[rax+04]
		--Hellblade2-Win64-Shipping.exe+200D988 - F3 41 0F 11 87 D8 00 00 00  - movss [r15+000000D8],xmm0
		--Hellblade2-Win64-Shipping.exe+200D991 - 48 8B 9E 28 C2 00 00     	  - mov rbx,[rsi+0000C228]
	end
	
	local tAddress = HackTool:AddAddress("FILMGRAIN")
	
	if HackTool:SignatureScan("0F 84 ?? ?? ?? ?? 49 ?? ?? E8 ?? ?? ?? ?? 48 ?? ?? 48 8D ?? ?? ?? ?? ?? 0F B7",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Hellblade2-Win64-Shipping.exe+218DA65 - 48 8B 05 E4 E9 74 08  - mov rax,[Hellblade2-Win64-Shipping.exe+A8DC450]
		--Hellblade2-Win64-Shipping.exe+218DA6C - 83 38 00              - cmp dword ptr [rax],00
		--Hellblade2-Win64-Shipping.exe+218DA6F - 0F 84 BF 00 00 00     - je Hellblade2-Win64-Shipping.exe+218DB34
		--Hellblade2-Win64-Shipping.exe+218DA75 - 49 8B CC              - mov rcx,r12
		--Hellblade2-Win64-Shipping.exe+218DA78 - E8 53 6D 39 01        - call Hellblade2-Win64-Shipping.exe+35247D0
	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:PushInt("Aspect")
	
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["AspectRatio"]:WriteFloat(fDefaultAspectRatio)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["factor"]:WriteFloat(fFactor)
	
	ResolutionChanged()	
				
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			$$0 [(allocation)Variables->FOVIn],$$2					$ctx=1
			fld dword ptr [(allocation)Variables->FOVIn]
			cmp dword ptr [(allocation)Variables->Aspect],1
			jne regularFOV
			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]
		regularFOV:
			fadd dword ptr [(allocation)Variables->AdditionalFOV]
			fstp dword ptr [(allocation)Variables->FOVOut]
			$$0 $$2,[(allocation)Variables->FOVOut]					$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%

		(codecave:jmp)ASPECT,ASPECT_cc:
			$$0 $$2,[(allocation)Variables->ScreenRatio]			$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
			
		(codecave)DOF,DOF_cc:
			xor $$1,$$1												$ctx=1
			nop
			%end%
			
		(codecave)LENSDISTORTION,LENSDISTORTION_cc:
			xorps $$1,$$1											$ctx=1
			nop
			nop
			%end%
			
		(codecave)LENSSOFTNESS,LENSSOFTNESS_cc:
			xorps $$1,$$1											$ctx=1
			nop
			nop
			%end%
			
		(codecave)FRINGE,FRINGE_cc:
			xorps $$1,$$1											$ctx=1
			nop
			nop
			%end%
	
		(codecave)FILMGRAIN,FILMGRAIN_cc:
			jmp $$1
			nop
	
	]]

	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("LENSDISTORTION_cc",bLensDistortion)
		Toggle_CodeCave("LENSSOFTNESS_cc",bLensSoftness)
		Toggle_CodeCave("FRINGE_cc",bFringe)
		Toggle_CodeCave("FILMGRAIN_cc",bFilmGrain)
	end	
			
	Write_FOV()
	Write_AspectRatio()
	
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_AspectRatio()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AspectRatio"] and Variables["CompensatedFOV"] and Variables["FOVIn"] then
		if bAspect == false then
			local fFOVIn = Variables["FOVIn"]:ReadFloat()
			Variables["CompensatedFOV"]:WriteFloat(fFOVIn)
			Variables["Aspect"]:WriteInt(0)
		else
			Variables["Aspect"]:WriteInt(1)
		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 CKARFix_Changed(Sender)

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

function CKDOFFix_Changed(Sender)

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

end

function CKLENSDISTORTIONFix_Changed(Sender)

	bLensDistortion = Toggle_CheckFix(Sender)
	Toggle_CodeCave("LENSDISTORTION_cc",bLensDistortion)
	ForceUpdate()

end

function CKLENSSOFTNESSFix_Changed(Sender)

	bLensSoftness = Toggle_CheckFix(Sender)
	Toggle_CodeCave("LENSSOFTNESS_cc",bLensSoftness)
	ForceUpdate()

end

function CKFRINGEFix_Changed(Sender)

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

function CKFILMGRAINFix_Changed(Sender)

	bFilmGrain = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FILMGRAIN_cc",bFilmGrain)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
