Skip to content

Commit

Permalink
Share PeSigScanner and use in RenderTargetHdrEnabler because of ReShade.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ottermandias committed Jan 11, 2025
1 parent 7f52777 commit 6ea38ea
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
33 changes: 19 additions & 14 deletions Penumbra/Interop/Hooks/PostProcessing/RenderTargetHdrEnabler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using FFXIVClientStructs.FFXIV.Client.Graphics.Render;
using OtterGui.Services;
using Penumbra.GameData;
using Penumbra.Interop.Hooks.ResourceLoading;
using Penumbra.Services;

namespace Penumbra.Interop.Hooks.PostProcessing;
Expand All @@ -31,19 +32,23 @@ private static readonly IComparer<ForcedTextureConfig> ForcedTextureConfigCompar

public TextureReportRecord[]? TextureReport { get; private set; }

[Signature(Sigs.RenderTargetManagerInitialize, DetourName = nameof(RenderTargetManagerInitializeDetour))]
private readonly Hook<RenderTargetManagerInitializeFunc> _renderTargetManagerInitialize = null!;

[Signature(Sigs.DeviceCreateTexture2D, DetourName = nameof(CreateTexture2DDetour))]
private readonly Hook<CreateTexture2DFunc> _createTexture2D = null!;
private readonly Hook<RenderTargetManagerInitializeFunc>? _renderTargetManagerInitialize;
private readonly Hook<CreateTexture2DFunc>? _createTexture2D;

public RenderTargetHdrEnabler(IGameInteropProvider interop, Configuration config, IDalamudPluginInterface pi,
DalamudConfigService dalamudConfig)
DalamudConfigService dalamudConfig, PeSigScanner peScanner)
{
_config = config;
interop.InitializeFromAttributes(this);
if (config.HdrRenderTargets && !HookOverrides.Instance.PostProcessing.RenderTargetManagerInitialize)
_renderTargetManagerInitialize.Enable();
if (peScanner.TryScanText(Sigs.RenderTargetManagerInitialize, out var initializeAddress)
&& peScanner.TryScanText(Sigs.DeviceCreateTexture2D, out var createAddress))
{
_renderTargetManagerInitialize =
interop.HookFromAddress<RenderTargetManagerInitializeFunc>(initializeAddress, RenderTargetManagerInitializeDetour);
_createTexture2D = interop.HookFromAddress<CreateTexture2DFunc>(createAddress, CreateTexture2DDetour);

if (config.HdrRenderTargets && !HookOverrides.Instance.PostProcessing.RenderTargetManagerInitialize)
_renderTargetManagerInitialize.Enable();
}

_share = pi.GetOrCreateData("Penumbra.RenderTargetHDR.V1", () =>
{
Expand Down Expand Up @@ -87,19 +92,19 @@ public void Dispose()

private void Dispose(bool _)
{
_createTexture2D.Dispose();
_renderTargetManagerInitialize.Dispose();
_createTexture2D?.Dispose();
_renderTargetManagerInitialize?.Dispose();
}

private nint RenderTargetManagerInitializeDetour(RenderTargetManager* @this)
{
_createTexture2D.Enable();
_createTexture2D!.Enable();
_share.Item5[0] = true;
_textureIndices.Value = new TextureIndices(0, 0);
_textures.Value = _config.DebugMode ? [] : null;
try
{
return _renderTargetManagerInitialize.Original(@this);
return _renderTargetManagerInitialize!.Original(@this);
}
finally
{
Expand Down Expand Up @@ -133,7 +138,7 @@ private nint RenderTargetManagerInitializeDetour(RenderTargetManager* @this)
_textureIndices.Value = indices;
}

var texture = _createTexture2D.Original(@this, size, mipLevel, textureFormat, flags, unk);
var texture = _createTexture2D!.Original(@this, size, mipLevel, textureFormat, flags, unk);
if (_textures.IsValueCreated)
_textures.Value?.Add((nint)texture, (indices.CreationOrder - 1, originalTextureFormat));
return texture;
Expand Down
4 changes: 2 additions & 2 deletions Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Penumbra.Interop.Hooks.ResourceLoading;

public sealed class PapHandler(PapRewriter.PapResourceHandlerPrototype papResourceHandler) : IDisposable
public sealed class PapHandler(PeSigScanner sigScanner, PapRewriter.PapResourceHandlerPrototype papResourceHandler) : IDisposable
{
private readonly PapRewriter _papRewriter = new(papResourceHandler);
private readonly PapRewriter _papRewriter = new(sigScanner, papResourceHandler);

public void Enable()
{
Expand Down
9 changes: 3 additions & 6 deletions Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@

namespace Penumbra.Interop.Hooks.ResourceLoading;

public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResourceHandler) : IDisposable
public sealed class PapRewriter(PeSigScanner sigScanner, PapRewriter.PapResourceHandlerPrototype papResourceHandler) : IDisposable
{
public unsafe delegate int PapResourceHandlerPrototype(void* self, byte* path, int length);

private readonly PeSigScanner _scanner = new();
private readonly Dictionary<nint, AsmHook> _hooks = [];
private readonly Dictionary<(nint, Register, ulong), nint> _nativeAllocPaths = [];
private readonly List<nint> _nativeAllocCaves = [];

public void Rewrite(string sig, string name)
{
if (!_scanner.TryScanText(sig, out var address))
if (!sigScanner.TryScanText(sig, out var address))
throw new Exception($"Signature for {name} [{sig}] could not be found.");

var funcInstructions = _scanner.GetFunctionInstructions(address).ToArray();
var funcInstructions = sigScanner.GetFunctionInstructions(address).ToArray();
var hookPoints = ScanPapHookPoints(funcInstructions).ToList();

foreach (var hookPoint in hookPoints)
Expand Down Expand Up @@ -165,8 +164,6 @@ private static unsafe void NativeFree(nint mem)

public void Dispose()
{
_scanner.Dispose();

foreach (var hook in _hooks.Values)
{
hook.Disable();
Expand Down
3 changes: 2 additions & 1 deletion Penumbra/Interop/Hooks/ResourceLoading/PeSigScanner.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.IO.MemoryMappedFiles;
using Iced.Intel;
using OtterGui.Services;
using PeNet;
using Decoder = Iced.Intel.Decoder;

namespace Penumbra.Interop.Hooks.ResourceLoading;

// A good chunk of this was blatantly stolen from Dalamud's SigScanner 'cause Winter could not be faffed, Winter will definitely not rewrite it later
public unsafe class PeSigScanner : IDisposable
public unsafe class PeSigScanner : IDisposable, IService
{
private readonly MemoryMappedFile _file;
private readonly MemoryMappedViewAccessor _textSection;
Expand Down
4 changes: 2 additions & 2 deletions Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public unsafe class ResourceLoader : IDisposable, IService
private ResolveData _resolvedData = ResolveData.Invalid;
public event Action<Utf8GamePath, FullPath?, ResolveData>? PapRequested;

public ResourceLoader(ResourceService resources, FileReadService fileReadService, RsfService rsfService, Configuration config)
public ResourceLoader(ResourceService resources, FileReadService fileReadService, RsfService rsfService, Configuration config, PeSigScanner sigScanner)
{
_resources = resources;
_fileReadService = fileReadService;
Expand All @@ -35,7 +35,7 @@ public ResourceLoader(ResourceService resources, FileReadService fileReadService
_resources.ResourceHandleDecRef += DecRefProtection;
_fileReadService.ReadSqPack += ReadSqPackDetour;

_papHandler = new PapHandler(PapResourceHandler);
_papHandler = new PapHandler(sigScanner, PapResourceHandler);
_papHandler.Enable();
}

Expand Down

0 comments on commit 6ea38ea

Please sign in to comment.