Skip to content

Commit

Permalink
Hacky/example fix for SystemTypeFromIl2CppType for Il2Cpp prefixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
WNP78 committed Jun 20, 2024
1 parent 45e22c2 commit 0b5f5ed
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Il2CppInterop.Runtime/Injection/ClassInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ private static Type RewriteType(Type type)
return type;
}

private static string GetIl2CppTypeFullName(Il2CppTypeStruct* typePointer)
private static string GetIl2CppTypeFullName(Il2CppTypeStruct* typePointer, bool prefix = false)
{
var klass = UnityVersionHandler.Wrap((Il2CppClass*)IL2CPP.il2cpp_class_from_type((IntPtr)typePointer));
var assembly = UnityVersionHandler.Wrap(UnityVersionHandler.Wrap(klass.Image).Assembly);
Expand All @@ -1122,9 +1122,11 @@ private static string GetIl2CppTypeFullName(Il2CppTypeStruct* typePointer)
var namespaceName = Marshal.PtrToStringAnsi(klass.Namespace);
if (!string.IsNullOrEmpty(namespaceName))
{
if (prefix) fullName.Append("Il2Cpp");
fullName.Append(namespaceName);
fullName.Append('.');
}
else if (prefix) fullName.Append("Il2Cpp.");

var declaringType = klass;
while ((declaringType = UnityVersionHandler.Wrap(declaringType.DeclaringType)) != default)
Expand All @@ -1139,6 +1141,7 @@ private static string GetIl2CppTypeFullName(Il2CppTypeStruct* typePointer)
if (assemblyName != "mscorlib")
{
fullName.Append(", ");
if (prefix) fullName.Append("Il2Cpp");
fullName.Append(assemblyName);
}

Expand All @@ -1148,7 +1151,13 @@ private static string GetIl2CppTypeFullName(Il2CppTypeStruct* typePointer)
internal static Type SystemTypeFromIl2CppType(Il2CppTypeStruct* typePointer)
{
var fullName = GetIl2CppTypeFullName(typePointer);
var type = Type.GetType(fullName) ?? throw new NullReferenceException($"Couldn't find System.Type for Il2Cpp type: {fullName}");
var type = Type.GetType(fullName);

if (type == null)
{
fullName = GetIl2CppTypeFullName(typePointer, prefix: true);
type = Type.GetType(fullName) ?? throw new NullReferenceException($"Couldn't find System.Type for Il2Cpp type: {fullName}");
}

INativeTypeStruct wrappedType = UnityVersionHandler.Wrap(typePointer);
if (wrappedType.Type == Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST)
Expand Down

0 comments on commit 0b5f5ed

Please sign in to comment.