Potentially more secure version for mod loading.

This commit is contained in:
Ottermandias 2022-07-02 00:31:27 +02:00
parent 958ff5d803
commit 57a38aeb94
2 changed files with 11 additions and 8 deletions

View file

@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -114,7 +116,7 @@ public unsafe partial class ResourceLoader
// Try all resolve path subscribers or use the default replacer. // Try all resolve path subscribers or use the default replacer.
private (FullPath?, object?) ResolvePath( Utf8GamePath path, ResourceCategory category, ResourceType resourceType, int resourceHash ) private (FullPath?, object?) ResolvePath( Utf8GamePath path, ResourceCategory category, ResourceType resourceType, int resourceHash )
{ {
if( !DoReplacements || _isInIncRef > 0 ) if( !DoReplacements || _incMode.Value )
{ {
return ( null, null ); return ( null, null );
} }
@ -270,17 +272,19 @@ public unsafe partial class ResourceLoader
// This means, that if the path determined from that is different than the resources path, // This means, that if the path determined from that is different than the resources path,
// a different resource gets loaded or incremented, while the IncRef'd resource stays at 0. // a different resource gets loaded or incremented, while the IncRef'd resource stays at 0.
// This causes some problems and is hopefully prevented with this. // This causes some problems and is hopefully prevented with this.
private int _isInIncRef = 0; private readonly ThreadLocal< bool > _incMode = new();
public int IsInIncRef
=> _isInIncRef;
private readonly Hook< ResourceHandleDestructor > _incRefHook; private readonly Hook< ResourceHandleDestructor > _incRefHook;
private IntPtr ResourceHandleIncRefDetour( ResourceHandle* handle ) private IntPtr ResourceHandleIncRefDetour( ResourceHandle* handle )
{ {
Interlocked.Increment(ref _isInIncRef); if( handle->RefCount > 0 )
{
return _incRefHook.Original( handle );
}
_incMode.Value = true;
var ret = _incRefHook.Original( handle ); var ret = _incRefHook.Original( handle );
Interlocked.Decrement(ref _isInIncRef); _incMode.Value = false;
return ret; return ret;
} }
} }

View file

@ -153,7 +153,6 @@ public partial class ConfigWindow
return; return;
} }
ImGui.TextUnformatted( $"In Increment RefCounter Mode: {Penumbra.ResourceLoader.IsInIncRef}" );
using var drawTree = ImRaii.TreeNode( "Draw Object to Object" ); using var drawTree = ImRaii.TreeNode( "Draw Object to Object" );
if( drawTree ) if( drawTree )
{ {