Skip to content

Commit

Permalink
Special-case System.Enum as a generic constraint (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 authored Sep 10, 2024
1 parent 7a56995 commit 1eacadb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
11 changes: 9 additions & 2 deletions Il2CppInterop.Generator/Contexts/MethodRewriteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,15 @@ public void CtorPhase2()

foreach (var oldConstraint in oldParameter.Constraints)
{
if (oldConstraint.Constraint?.FullName == "System.ValueType" ||
oldConstraint.Constraint?.Resolve()?.IsInterface == true) continue;
if (oldConstraint.IsSystemValueType() || oldConstraint.IsInterface())
continue;

if (oldConstraint.IsSystemEnum())
{
newParameter.Constraints.Add(new GenericParameterConstraint(
DeclaringType.AssemblyContext.Imports.Module.Enum().ToTypeDefOrRef()));
continue;
}

newParameter.Constraints.Add(new GenericParameterConstraint(
DeclaringType.AssemblyContext.RewriteTypeRef(oldConstraint.Constraint?.ToTypeSignature()).ToTypeDefOrRef()));
Expand Down
6 changes: 6 additions & 0 deletions Il2CppInterop.Generator/Extensions/AsmResolverExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ private static Parameter GetArgument(this ILProcessor instructions, int argument

public static bool IsNested(this TypeSignature type) => type.DeclaringType is not null;

public static bool IsSystemEnum(this GenericParameterConstraint constraint) => constraint.Constraint?.FullName is "System.Enum";

public static bool IsSystemValueType(this GenericParameterConstraint constraint) => constraint.Constraint?.FullName is "System.ValueType";

public static bool IsInterface(this GenericParameterConstraint constraint) => constraint.Constraint?.Resolve()?.IsInterface == true;

public static ITypeDefOrRef? AttributeType(this CustomAttribute attribute) => attribute.Constructor?.DeclaringType;

public static Parameter AddParameter(this MethodDefinition method, TypeSignature parameterSignature, string parameterName, ParameterAttributes parameterAttributes = default)
Expand Down
12 changes: 10 additions & 2 deletions Il2CppInterop.Generator/Passes/Pass13FillGenericConstraints.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using AsmResolver.DotNet;
using Il2CppInterop.Generator.Contexts;
using Il2CppInterop.Generator.Extensions;
using Il2CppInterop.Generator.Utils;

namespace Il2CppInterop.Generator.Passes;

Expand All @@ -17,10 +19,16 @@ public static void DoPass(RewriteGlobalContext context)
var newParameter = typeContext.NewType.GenericParameters[i];
foreach (var originalConstraint in originalParameter.Constraints)
{
if (originalConstraint.Constraint?.FullName is "System.ValueType" ||
originalConstraint.Constraint?.Resolve()?.IsInterface == true)
if (originalConstraint.IsSystemValueType() || originalConstraint.IsInterface())
continue;

if (originalConstraint.IsSystemEnum())
{
newParameter.Constraints.Add(new GenericParameterConstraint(
typeContext.AssemblyContext.Imports.Module.Enum().ToTypeDefOrRef()));
continue;
}

newParameter.Constraints.Add(
new GenericParameterConstraint(
assemblyContext.RewriteTypeRef(originalConstraint.Constraint!)));
Expand Down
10 changes: 8 additions & 2 deletions Il2CppInterop.Generator/Passes/Pass80UnstripMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ public static void DoPass(RewriteGlobalContext context)
newParameter.Attributes = unityMethodGenericParameter.Attributes;
foreach (var genericParameterConstraint in unityMethodGenericParameter.Constraints)
{
if (genericParameterConstraint.Constraint?.FullName == "System.ValueType") continue;
if (genericParameterConstraint.Constraint?.Resolve()?.IsInterface ?? false) continue;
if (genericParameterConstraint.IsSystemValueType() || genericParameterConstraint.IsInterface())
continue;

if (genericParameterConstraint.IsSystemEnum())
{
newParameter.Constraints.Add(new GenericParameterConstraint(imports.Module.Enum().ToTypeDefOrRef()));
continue;
}

var newType = ResolveTypeInNewAssemblies(context, genericParameterConstraint.Constraint?.ToTypeSignature(),
imports);
Expand Down

0 comments on commit 1eacadb

Please sign in to comment.