-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/CamelCaseName/Il2CppInterop
- Loading branch information
Showing
13 changed files
with
180 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
Il2CppInterop.Runtime/Injection/Hooks/GarbageCollector_RunFinalizer_Patch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using Il2CppInterop.Common; | ||
using Il2CppInterop.Runtime.Runtime; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Il2CppInterop.Runtime.Injection.Hooks; | ||
|
||
internal class GarbageCollector_RunFinalizer_Patch : Hook<GarbageCollector_RunFinalizer_Patch.MethodDelegate> | ||
{ | ||
public override string TargetMethodName => "GarbageCollector::RunFinalizer"; | ||
public override MethodDelegate GetDetour() => Hook; | ||
|
||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] | ||
public delegate void MethodDelegate(IntPtr obj, IntPtr data); | ||
|
||
private void Hook(IntPtr obj, IntPtr data) | ||
{ | ||
unsafe | ||
{ | ||
var nativeClassStruct = UnityVersionHandler.Wrap((Il2CppClass*)IL2CPP.il2cpp_object_get_class(obj)); | ||
if (nativeClassStruct.HasFinalize) | ||
{ | ||
Original(obj, data); | ||
} | ||
} | ||
Il2CppObjectPool.Remove(obj); | ||
} | ||
|
||
private static readonly MemoryUtils.SignatureDefinition[] s_signatures = | ||
{ | ||
new() | ||
{ | ||
// Among Us - 2020.3.22 (x86) | ||
pattern = "\x55\x8B\xEC\x51\x56\x8B\x75\x08\xC7\x45\x00\x00\x00\x00\x00", | ||
mask = "xxxxxxxxxx?????", | ||
xref = false | ||
}, | ||
new() | ||
{ | ||
// Test Game - 2021.3.22 (x64) | ||
pattern = "\x40\x53\x48\x83\xEC\x20\x48\x8B\xD9\x48\xC7\x44\x24\x30\x00\x00\x00\x00\x48\x8B", | ||
mask = "xxxxxxxxxxxxxxxxxxxx", | ||
xref = false, | ||
} | ||
}; | ||
|
||
public override IntPtr FindTargetMethod() | ||
{ | ||
return s_signatures | ||
.Select(s => MemoryUtils.FindSignatureInModule(InjectorHelpers.Il2CppModule, s)) | ||
.FirstOrDefault(p => p != 0); | ||
} | ||
|
||
public override void TargetMethodNotFound() | ||
{ | ||
Il2CppObjectPool.DisableCaching = true; | ||
Logger.Instance.LogWarning("{MethodName} not found, disabling Il2CppObjectPool", TargetMethodName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Runtime.CompilerServices; | ||
using Il2CppInterop.Runtime.InteropTypes; | ||
using Object = Il2CppSystem.Object; | ||
|
||
namespace Il2CppInterop.Runtime.Runtime; | ||
|
||
public static class Il2CppObjectPool | ||
{ | ||
internal static bool DisableCaching { get; set; } | ||
|
||
private static readonly ConcurrentDictionary<IntPtr, WeakReference<Il2CppObjectBase>> s_cache = new(); | ||
|
||
internal static void Remove(IntPtr ptr) | ||
{ | ||
s_cache.TryRemove(ptr, out _); | ||
} | ||
|
||
public static T Get<T>(IntPtr ptr) | ||
{ | ||
if (ptr == IntPtr.Zero) return default; | ||
|
||
var ownClass = IL2CPP.il2cpp_object_get_class(ptr); | ||
if (RuntimeSpecificsStore.IsInjected(ownClass)) | ||
{ | ||
var monoObject = ClassInjectorBase.GetMonoObjectFromIl2CppPointer(ptr); | ||
if (monoObject is T monoObjectT) return monoObjectT; | ||
} | ||
|
||
if (DisableCaching) return Il2CppObjectBase.InitializerStore<T>.Initializer(ptr); | ||
|
||
if (s_cache.TryGetValue(ptr, out var reference) && reference.TryGetTarget(out var cachedObject)) | ||
{ | ||
if (cachedObject is T cachedObjectT) return cachedObjectT; | ||
cachedObject.pooledPtr = IntPtr.Zero; | ||
// This leaves the case when you cast to an interface handled as if nothing was cached | ||
} | ||
|
||
var newObj = Il2CppObjectBase.InitializerStore<T>.Initializer(ptr); | ||
unsafe | ||
{ | ||
var nativeClassStruct = UnityVersionHandler.Wrap((Il2CppClass*)Il2CppClassPointerStore<T>.NativeClassPtr); | ||
if (!nativeClassStruct.HasFinalize) | ||
{ | ||
Il2CppSystem.GC.ReRegisterForFinalize(newObj as Object ?? new Object(ptr)); | ||
} | ||
} | ||
|
||
var il2CppObjectBase = Unsafe.As<T, Il2CppObjectBase>(ref newObj); | ||
s_cache[ptr] = new WeakReference<Il2CppObjectBase>(il2CppObjectBase); | ||
il2CppObjectBase.pooledPtr = ptr; | ||
return newObj; | ||
} | ||
} |