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

--ControlVars
bFixEnabled = true
bFOV = true
bAspectRatio = true
bAspectFix = true
bSharpening = true

--GAME VARS
fFOV = 1.0
fAdditionalFOV = 0
fAspectRatio = 1.777777791
fSharpening = 0

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "Hogwarts Legacy  "
Process_ClassName = "UnrealWindow"
Process_EXEName = "*"

--INJECTION BEHAVIOUR
InjectDelay = 500 
WriteInterval = 500
SearchInterval = 1000
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.AddHeader("Header_Sharp","Sharpening fine adjustment",15,150,210,17)
	DefaultControls.AddFOVSlider("SharpSlider","SharpSlider_Changed",55,180,125,35)

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKARatioFix_Enable","Cinematics aspect fix","CKARatioFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKSharpFix_Enable","Tone Mapping Sharpening fix","CKSharpFix_Changed",255,141,180,14)

end

function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("Sharpening")
	if HackTool:SignatureScan("EB ?? 48 ?? ?? E8 ?? ?? ?? ?? F3 0F ?? ?? ?? ?? 48 8B ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ",tAddress,PAGE_EXECUTE_READ,0x17,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--HogwartsLegacy.exe+3EDE648: F3 0F 11 44 24 38           - movss [rsp+38],xmm0
		--HogwartsLegacy.exe+3EDE64E: 48 8B 05 D3 DC 27 05        - mov rax,[HogwartsLegacy.exe+915C328]
		--HogwartsLegacy.exe+3EDE655: F3 0F 10 78 04              - movss xmm7,[rax+04]
		--HogwartsLegacy.exe+3EDE65A: 0F 2F FE                    - comiss xmm7,xmm6
		--HogwartsLegacy.exe+3EDE65D: 73 05                       - jae HogwartsLegacy.exe+3EDE664
	end

	local tAddress = HackTool:AddAddress("FOV")
	if HackTool:SignatureScan("F6 83 ?? ?? ?? ?? ?? 74 ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? EB ?? F3 0F ?? ?? ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x1B,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--HogwartsLegacy.exe+522F9D5 - F3 0F 10 83 64 02 00 00  - movss xmm0,[rbx+00000264]
		--HogwartsLegacy.exe+522F9DD - F3 0F 58 83 18 02 00 00  - addss xmm0,[rbx+00000218]
		--HogwartsLegacy.exe+522F9E5 - EB 08                 	- jmp HogwartsLegacy.exe+522F9EF
		--HogwartsLegacy.exe+522F9E7 - F3 0F 10 83 18 02 00 00  - movss xmm0,[rbx+00000218]
		--HogwartsLegacy.exe+522F9EF - F3 0F 11 47 18         	- movss [rdi+18],xmm0
		--HogwartsLegacy.exe+522F9F4 - 8B 83 28 02 00 00        - mov eax,[rbx+00000228]
	end

	local tAddress = HackTool:AddAddress("Aspect")
	if HackTool:SignatureScan("76 ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 0f 28 ?? ?? ?? 48",tAddress,PAGE_EXECUTE_READ,0x0E,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--HogwartsLegacy.exe+4F755F4 - F3 0F 10 83 8C 08 00 00  - movss xmm0,[rbx+0000088C]
		--HogwartsLegacy.exe+4F755FC - F3 0F 5E C1            	- divss xmm0,xmm1
		--HogwartsLegacy.exe+4F75600 - F3 0F 11 83 94 08 00 00  - movss [rbx+00000894],xmm0
		--HogwartsLegacy.exe+4F75608 - F3 0F 11 83 28 02 00 00  - movss [rbx+00000228],xmm0
		--HogwartsLegacy.exe+4F75610 - 0F 28 74 24 20         	- movaps xmm6,[rsp+20]
	end
	
	return true
	
end



function Enable_Inject() 

	local EXEAddr = HackTool:GetModuleAddress(Process_EXEName)
	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("Aspect")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("Sharpening")
	Variables:Allocate(EXEAddr)
	
	asm = [[	

		(codecave:jmp)Sharpening,Sharpening_cc:
			$$0 $$1,[(allocation)Variables->Sharpening]			$ctx=1
			jmp %returnaddress%			
			%end%
			
		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			movss [(allocation)Variables->FOVIn],$$1			$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]	$ctx=1
			movss [(allocation)Variables->FOVOut],$$1			$ctx=1
			jmp %returnaddress%			
			%end%
		
		(codecave:jmp)Aspect,Aspect_cc:
			$$0 $$2,[(allocation)Variables->Aspect]				$ctx=1
			%originalcode%
			jmp %returnaddress%			
			%end%
			
	]]

	if HackTool:CompileAssembly(asm,"Fixes",EXEAddr) == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("Sharpening_cc",bSharpening)
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("Aspect_cc",bAspectFix)
	end

	Write_FOV()
	Write_Sharpening()

end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		fFOVIn = Variables["FOVIn"]:ReadFloat()
		fFOVOut = Variables["FOVOut"]:ReadFloat()		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %0.f, FOV Out : %.0f", fFOVIn, fFOVOut))
	end
	
	if bAspectFix and Variables["Aspect"] then
		fAspectRatio = DisplayInfo:GetfOffsetWidth() / DisplayInfo:GetfOffsetHeight()
		Variables["Aspect"]:WriteFloat(fAspectRatio)
	end
	
end

function Disable_Inject()

	CleanUp()
	
end

function CKSharpFix_Changed(Sender)

	bSharpening = Toggle_CheckFix(Sender)
	Toggle_CodeCave("Sharpening_cc",bSharpening)
	ForceUpdate()
	
end

function SharpSlider_Changed(Sender)

	fSharpening = (Sender:GetScaledFloat(2) + 50) / 10
	lblSharpSlider.Caption:SetCaption( string.format("%.2f",fSharpening))	
	Write_Sharpening()
	ForceUpdate()	
		
end

function Write_Sharpening()

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

end

function CKFOVFix_Changed(Sender)

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

function FOVSlider_Changed(Sender)

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

function Write_FOV()

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

end

function CKARatioFix_Changed(Sender)

	bAspectFix = Toggle_CheckFix(Sender)
	Toggle_CodeCave("Aspect_cc",bAspectFix)
	ForceUpdate()		
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end