diff --git a/Il2CppInterop.Generator/Contexts/RewriteGlobalContext.cs b/Il2CppInterop.Generator/Contexts/RewriteGlobalContext.cs index bbf503f7..c6cb9e46 100644 --- a/Il2CppInterop.Generator/Contexts/RewriteGlobalContext.cs +++ b/Il2CppInterop.Generator/Contexts/RewriteGlobalContext.cs @@ -120,6 +120,11 @@ public AssemblyRewriteContext GetAssemblyByName(string name) return null; } + public TypeRewriteContext GetContextForNewType(TypeDefinition type) + { + return GetAssemblyByName(type.Module!.Assembly!.Name!).GetContextForNewType(type); + } + public MethodDefinition? CreateParamsMethod(MethodDefinition originalMethod, MethodDefinition newMethod, RuntimeAssemblyReferences imports, Func resolve) { diff --git a/Il2CppInterop.Generator/Utils/UnstripTranslator.cs b/Il2CppInterop.Generator/Utils/UnstripTranslator.cs index 6c20ced1..2053acd6 100644 --- a/Il2CppInterop.Generator/Utils/UnstripTranslator.cs +++ b/Il2CppInterop.Generator/Utils/UnstripTranslator.cs @@ -105,7 +105,12 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t var fieldDeclarerDefinition = fieldDeclarer.Resolve(); if (fieldDeclarerDefinition == null) return false; - var newField = fieldDeclarerDefinition.Fields.SingleOrDefault(it => it.Name == fieldArg.Name); + + var fieldDeclarerContext = globalContext.GetContextForNewType(fieldDeclarerDefinition); + var propertyName = fieldDeclarerContext.Fields.SingleOrDefault(it => it.OriginalField.Name == fieldArg.Name)?.UnmangledName; + + var newField = fieldDeclarerDefinition.Fields.SingleOrDefault(it => it.Name == fieldArg.Name) + ?? fieldDeclarerDefinition.Fields.SingleOrDefault(it => it.Name == propertyName); if (newField != null) { var newInstruction = targetBuilder.Add(bodyInstruction.OpCode, imports.Module.DefaultImporter.ImportField(newField)); @@ -113,10 +118,14 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t } else { - if (bodyInstruction.OpCode == OpCodes.Ldfld || bodyInstruction.OpCode == OpCodes.Ldsfld) + if (propertyName == null) + { + return false; + } + else if (bodyInstruction.OpCode == OpCodes.Ldfld || bodyInstruction.OpCode == OpCodes.Ldsfld) { var getterMethod = fieldDeclarerDefinition.Properties - .SingleOrDefault(it => it.Name == fieldArg.Name)?.GetMethod; + .SingleOrDefault(it => it.Name == propertyName)?.GetMethod; if (getterMethod == null) return false; @@ -126,7 +135,7 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t else if (bodyInstruction.OpCode == OpCodes.Stfld || bodyInstruction.OpCode == OpCodes.Stsfld) { var setterMethod = fieldDeclarerDefinition.Properties - .SingleOrDefault(it => it.Name == fieldArg.Name)?.SetMethod; + .SingleOrDefault(it => it.Name == propertyName)?.SetMethod; if (setterMethod == null) return false;