Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of the Thread.Sleep(1) workaround in InjectorHelpers #56

Merged
merged 2 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Il2CppInterop.Runtime/Injection/DetourProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;
using Il2CppInterop.Runtime.Startup;

namespace Il2CppInterop.Runtime.Injection;
Expand All @@ -11,6 +10,7 @@ public interface IDetour : IDisposable
nint OriginalTrampoline { get; }

void Apply();
T GenerateTrampoline<T>() where T : Delegate;
}

public interface IDetourProvider
Expand All @@ -20,10 +20,11 @@ public interface IDetourProvider

internal static class Detour
{
public static T Apply<T>(nint original, T target) where T : Delegate
public static IDetour Apply<T>(nint original, T target, out T trampoline) where T : Delegate
{
var detour = Il2CppInteropRuntime.Instance.DetourProvider.Create(original, target);
trampoline = detour.GenerateTrampoline<T>();
detour.Apply();
return Marshal.GetDelegateForFunctionPointer<T>(detour.OriginalTrampoline);
return detour;
}
}
16 changes: 7 additions & 9 deletions Il2CppInterop.Runtime/Injection/InjectorHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private static d_GenericMethodGetMethod FindGenericMethodGetMethod()
}

Logger.Instance.LogTrace("GenericMethod::GetMethod: 0x{GenericMethodGetMethodAddress}", genericMethodGetMethod.ToString("X2"));
GenericMethodGetMethodOriginal = Detour.Apply(genericMethodGetMethod, GenericMethodGetMethodDetour);
Detour.Apply(genericMethodGetMethod, GenericMethodGetMethodDetour, out GenericMethodGetMethodOriginal);
return Marshal.GetDelegateForFunctionPointer<d_GenericMethodGetMethod>(genericMethodGetMethod);
}
#endregion
Expand All @@ -180,7 +180,6 @@ private static d_GenericMethodGetMethod FindGenericMethodGetMethod()
internal delegate Il2CppClass* d_ClassFromName(Il2CppImage* image, IntPtr _namespace, IntPtr name);
private static Il2CppClass* hkClassFromName(Il2CppImage* image, IntPtr _namespace, IntPtr name)
{
while (ClassFromNameOriginal == null) Thread.Sleep(1);
Il2CppClass* classPtr = ClassFromNameOriginal(image, _namespace, name);

if (classPtr == null)
Expand All @@ -204,7 +203,7 @@ private static d_ClassFromName FindClassFromName()
var classFromName = XrefScannerLowLevel.JumpTargets(classFromNameAPI).Single();
Logger.Instance.LogTrace("Class::FromName: 0x{ClassFromNameAddress}", classFromName.ToInt64().ToString("X2"));

ClassFromNameOriginal = Detour.Apply(classFromName, ClassFromNameDetour);
Detour.Apply(classFromName, ClassFromNameDetour, out ClassFromNameOriginal);
return Marshal.GetDelegateForFunctionPointer<d_ClassFromName>(classFromName);
}
#endregion
Expand All @@ -217,7 +216,6 @@ private static d_ClassFromName FindClassFromName()
if (s_InjectedClasses.TryGetValue(index, out IntPtr classPtr))
return (Il2CppClass*)classPtr;

while (GetTypeInfoFromTypeDefinitionIndexOriginal == null) Thread.Sleep(1);
return GetTypeInfoFromTypeDefinitionIndexOriginal(index);
}
private static readonly d_GetTypeInfoFromTypeDefinitionIndex GetTypeInfoFromTypeDefinitionIndexDetour = new(hkGetTypeInfoFromTypeDefinitionIndex);
Expand Down Expand Up @@ -294,9 +292,10 @@ private static d_GetTypeInfoFromTypeDefinitionIndex FindGetTypeInfoFromTypeDefin

Logger.Instance.LogTrace("MetadataCache::GetTypeInfoFromTypeDefinitionIndex: 0x{GetTypeInfoFromTypeDefinitionIndexAddress}", getTypeInfoFromTypeDefinitionIndex.ToInt64().ToString("X2"));

GetTypeInfoFromTypeDefinitionIndexOriginal = Detour.Apply(
Detour.Apply(
getTypeInfoFromTypeDefinitionIndex,
GetTypeInfoFromTypeDefinitionIndexDetour
GetTypeInfoFromTypeDefinitionIndexDetour,
out GetTypeInfoFromTypeDefinitionIndexOriginal
);
return Marshal.GetDelegateForFunctionPointer<d_GetTypeInfoFromTypeDefinitionIndex>(getTypeInfoFromTypeDefinitionIndex);
}
Expand Down Expand Up @@ -336,7 +335,7 @@ private static d_ClassFromIl2CppType FindClassFromIl2CppType()
var classFromType = XrefScannerLowLevel.JumpTargets(classFromTypeAPI).Single();
Logger.Instance.LogTrace("Class::FromIl2CppType: 0x{ClassFromTypeAddress}", classFromType.ToInt64().ToString("X2"));

ClassFromIl2CppTypeOriginal = Detour.Apply(classFromType, ClassFromIl2CppTypeDetour);
Detour.Apply(classFromType, ClassFromIl2CppTypeDetour, out ClassFromIl2CppTypeOriginal);
return Marshal.GetDelegateForFunctionPointer<d_ClassFromIl2CppType>(classFromType);
}
#endregion
Expand All @@ -354,7 +353,6 @@ private static d_ClassFromIl2CppType FindClassFromIl2CppType()
type = wrappedElementClass.ByValArg.TypePointer;
return (byte*)newDefaultPtr;
}
while (ClassGetFieldDefaultValueOriginal == null) Thread.Sleep(1);
return ClassGetFieldDefaultValueOriginal(field, out type);
}
private static d_ClassGetFieldDefaultValue ClassGetFieldDefaultValueDetour = new(hkClassGetFieldDefaultValue);
Expand Down Expand Up @@ -446,7 +444,7 @@ private static d_ClassGetFieldDefaultValue FindClassGetFieldDefaultValue(bool fo
}
Logger.Instance.LogTrace("Class::GetDefaultFieldValue: 0x{ClassGetDefaultFieldValueAddress}", classGetDefaultFieldValue.ToString("X2"));

ClassGetFieldDefaultValueOriginal = Detour.Apply(classGetDefaultFieldValue, ClassGetFieldDefaultValueDetour);
Detour.Apply(classGetDefaultFieldValue, ClassGetFieldDefaultValueDetour, out ClassGetFieldDefaultValueOriginal);
return Marshal.GetDelegateForFunctionPointer<d_ClassGetFieldDefaultValue>(classGetDefaultFieldValue);
}
#endregion
Expand Down