Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ottermandias committed Jul 22, 2024
1 parent 528e322 commit df33557
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Penumbra.Api
22 changes: 10 additions & 12 deletions Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou
{
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 = [];
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)
{
Expand All @@ -26,13 +26,13 @@ public void Rewrite(string sig, string name)

foreach (var hookPoint in hookPoints)
{
var stackAccesses = ScanStackAccesses(funcInstructions, hookPoint).ToList();
var stackAccesses = ScanStackAccesses(funcInstructions, hookPoint).ToList();
var stringAllocation = NativeAllocPath(
address, hookPoint.MemoryBase, hookPoint.MemoryDisplacement64,
Utf8GamePath.MaxGamePathLength
);
);
WriteToAlloc(stringAllocation, Utf8GamePath.MaxGamePathLength, name);

// We'll need to grab our true hook point; the location where we can change the path at our leisure.
// This is going to be the first call instruction after our 'hookPoint', so, we'll find that.
// Pretty scuffed, this might need a refactoring at some point.
Expand Down Expand Up @@ -150,17 +150,15 @@ private unsafe nint NativeAllocCave(nuint size)

return caveLoc;
}

// This is a bit conked but, if we identify a path by:
// 1) The function it belongs to (starting address, 'funcAddress')
// 2) The stack register (not strictly necessary - should always be rbp - but abundance of caution, so I don't hit myself in the future)
// 3) The displacement on the stack
// Then we ensure we have a unique identifier for the specific variable location of that specific function
// This is useful because sometimes the stack address is reused within the same function for different GetResourceAsync calls
private unsafe nint NativeAllocPath(nint funcAddress, Register stackRegister, ulong stackDisplacement, nuint size)
{
return _nativeAllocPaths.GetOrAdd((funcAddress, stackRegister, stackDisplacement), _ => (nint)NativeMemory.Alloc(size));
}
=> _nativeAllocPaths.GetOrAdd((funcAddress, stackRegister, stackDisplacement), _ => (nint)NativeMemory.Alloc(size));

private static unsafe void NativeFree(nint mem)
=> NativeMemory.Free((void*)mem);
Expand All @@ -181,7 +179,7 @@ public void Dispose()
NativeFree(mem);

_nativeAllocCaves.Clear();

foreach (var mem in _nativeAllocPaths.Values)
NativeFree(mem);

Expand Down

0 comments on commit df33557

Please sign in to comment.