Skip to content

Commit

Permalink
Support resolving a field's declaring type (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 authored Oct 19, 2024
1 parent 3161f03 commit a084026
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions LibCpp2IL/Reflection/LibCpp2IlReflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public static class LibCpp2IlReflection
private static readonly Dictionary<Il2CppFieldDefinition, int> FieldIndices = new();
private static readonly Dictionary<Il2CppPropertyDefinition, int> PropertyIndices = new();

private static readonly Dictionary<Il2CppFieldDefinition, Il2CppTypeDefinition> FieldDeclaringTypes = new();

private static readonly Dictionary<Il2CppTypeEnum, Il2CppType> PrimitiveTypeCache = new();
public static readonly Dictionary<Il2CppTypeEnum, Il2CppTypeDefinition> PrimitiveTypeDefinitions = new();
private static readonly Dictionary<long, Il2CppType> Il2CppTypeCache = new();
Expand All @@ -32,6 +34,7 @@ internal static void ResetCaches()
MethodIndices.Clear();
FieldIndices.Clear();
PropertyIndices.Clear();
FieldDeclaringTypes.Clear();
PrimitiveTypeCache.Clear();
PrimitiveTypeDefinitions.Clear();
Il2CppTypeCache.Clear();
Expand Down Expand Up @@ -197,6 +200,30 @@ public static int GetPropertyIndexFromProperty(Il2CppPropertyDefinition property
return PropertyIndices[propertyDefinition];
}

public static Il2CppTypeDefinition GetDeclaringTypeFromField(Il2CppFieldDefinition fieldDefinition)
{
if (LibCpp2IlMain.TheMetadata == null) return null!;

if (FieldDeclaringTypes.Count == 0)
{
lock (FieldDeclaringTypes)
{
if (FieldDeclaringTypes.Count == 0)
{
foreach (var declaringType in LibCpp2IlMain.TheMetadata.typeDefs)
{
foreach (var field in declaringType.Fields ?? [])
{
FieldDeclaringTypes[field] = declaringType;
}
}
}
}
}

return FieldDeclaringTypes[fieldDefinition];
}

public static Il2CppType? GetTypeFromDefinition(Il2CppTypeDefinition definition)
{
if (LibCpp2IlMain.Binary == null)
Expand Down

0 comments on commit a084026

Please sign in to comment.