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

Unstrip fields for constants and new structs #162

Merged
merged 4 commits into from
Sep 14, 2024
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
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() && (processedType.OriginalType is not null || processedType.NewType.IsReferenceType()))
continue;// Instance fields are only supported on newly created value types.

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
Loading