Skip to content

Commit

Permalink
Allow constant fields on reference types to be unstripped
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 committed Sep 9, 2024
1 parent 7a56995 commit 1a33a5b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Il2CppInterop.Generator/Extensions/AsmResolverExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ private static Parameter GetArgument(this ILProcessor instructions, int argument
: method.Parameters[argumentIndex - 1];
}

public static bool IsReferenceType(this TypeDefinition type) => !type.IsValueType;

public static bool HasGenericParameters(this TypeDefinition type) => type.GenericParameters.Count > 0;

public static bool HasGenericParameters(this MethodDefinition method) => method.GenericParameters.Count > 0;

public static bool HasConstant(this FieldDefinition field) => field.Constant is not null;

public static bool IsInstance(this FieldDefinition field) => !field.IsStatic;

public static bool HasMethods(this TypeDefinition type) => type.Methods.Count > 0;

public static bool HasFields(this TypeDefinition type) => type.Fields.Count > 0;
Expand Down
10 changes: 6 additions & 4 deletions Il2CppInterop.Generator/Passes/Pass80UnstripFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ public static void DoPass(RewriteGlobalContext context)
var processedType = processedAssembly.TryGetTypeByName(unityType.FullName);
if (processedType == null) continue;

if (!unityType.IsValueType || unityType.IsEnum)
continue;
if (unityType.IsEnum)
continue;// Enum fields do not get stripped.

foreach (var unityField in unityType.Fields)
{
if (unityField.IsStatic && !unityField.HasConstant()) continue;
if (processedType.NewType.IsExplicitLayout && !unityField.IsStatic) continue;
if (unityField.IsStatic && !unityField.HasConstant())
continue;// Non-constant static fields might require initialization, which we can't do.
if (unityField.IsInstance() && (unityType.IsReferenceType() || processedType.NewType.IsExplicitLayout))
continue;

var processedField = processedType.TryGetFieldByUnityAssemblyField(unityField);
if (processedField != null) continue;
Expand Down
9 changes: 6 additions & 3 deletions Il2CppInterop.Generator/Utils/UnstripTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t
Pass80UnstripMethods.ResolveTypeInNewAssembliesRaw(globalContext, fieldArg.DeclaringType!.ToTypeSignature(), imports);
if (fieldDeclarer == null)
return false;
var newField = fieldDeclarer.Resolve()?.Fields.SingleOrDefault(it => it.Name == fieldArg.Name);
var fieldDeclarerDefinition = fieldDeclarer.Resolve();
if (fieldDeclarerDefinition == null)
return false;
var newField = fieldDeclarerDefinition.Fields.SingleOrDefault(it => it.Name == fieldArg.Name);
if (newField != null)
{
var newInstruction = targetBuilder.Add(bodyInstruction.OpCode, imports.Module.DefaultImporter.ImportField(newField));
Expand All @@ -112,7 +115,7 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t
{
if (bodyInstruction.OpCode == OpCodes.Ldfld || bodyInstruction.OpCode == OpCodes.Ldsfld)
{
var getterMethod = fieldDeclarer.Resolve()?.Properties
var getterMethod = fieldDeclarerDefinition.Properties
.SingleOrDefault(it => it.Name == fieldArg.Name)?.GetMethod;
if (getterMethod == null)
return false;
Expand All @@ -122,7 +125,7 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t
}
else if (bodyInstruction.OpCode == OpCodes.Stfld || bodyInstruction.OpCode == OpCodes.Stsfld)
{
var setterMethod = fieldDeclarer.Resolve()?.Properties
var setterMethod = fieldDeclarerDefinition.Properties
.SingleOrDefault(it => it.Name == fieldArg.Name)?.SetMethod;
if (setterMethod == null)
return false;
Expand Down

0 comments on commit 1a33a5b

Please sign in to comment.