Skip to content
Merged
Changes from 1 commit
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
37 changes: 27 additions & 10 deletions Il2CppInterop.Generator/Passes/Pass79UnstripTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,20 @@
if (unityType.BaseType != null && unityType.BaseType.FullName == "System.MulticastDelegate")
return;
var newModule = processedAssembly.NewAssembly.ManifestModule!;
var processedType = enclosingNewType == null
? processedAssembly.TryGetTypeByName(unityType.FullName)?.NewType
: enclosingNewType.NestedTypes.SingleOrDefault(it => it.Name == unityType.Name);
var processedType = processedAssembly.TryGetTypeByName(unityType.FullName)?.NewType;
var convertedTypeName = GetConvertedUnityTypeName(processedAssembly.GlobalContext, unityType);

// If the parent type does not exist in the rewritten assembly, this nested type cannot be emitted safely.
// Promoting it to a top-level type creates orphan compiler-generated types such as __O/__c.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is so cursed that we were doing this before.

if (unityType.DeclaringType != null && enclosingNewType == null && processedType == null)
Comment thread
acan1980728690 marked this conversation as resolved.
Outdated
return;

if (unityType.IsEnum)
{
if (processedType != null) return;

typesUnstripped++;
var clonedType = CloneEnum(unityType, imports);
var clonedType = CloneEnum(unityType, convertedTypeName, imports);
if (enclosingNewType == null)
{
newModule.TopLevelTypes.Add(clonedType);
Expand All @@ -74,7 +79,8 @@
!unityType.HasGenericParameters()) // restore all types even if it would be not entirely correct
{
typesUnstripped++;
var clonedType = new TypeDefinition(unityType.Namespace, unityType.Name, ForcePublic(unityType.Attributes), unityType.BaseType == null ? null : newModule.DefaultImporter.ImportType(unityType.BaseType));
var clonedType = new TypeDefinition(unityType.Namespace, convertedTypeName, ForcePublic(unityType.Attributes),
unityType.BaseType == null ? null : newModule.DefaultImporter.ImportType(unityType.BaseType));
if (enclosingNewType == null)
{
newModule.TopLevelTypes.Add(clonedType);
Expand All @@ -100,9 +106,9 @@
ProcessType(processedAssembly, nestedUnityType, processedType, imports, ref typesUnstripped);
}

private static TypeDefinition CloneEnum(TypeDefinition sourceEnum, RuntimeAssemblyReferences imports)
private static TypeDefinition CloneEnum(TypeDefinition sourceEnum, string convertedTypeName, RuntimeAssemblyReferences imports)
Comment thread
acan1980728690 marked this conversation as resolved.
Outdated
{
var newType = new TypeDefinition(sourceEnum.Namespace, sourceEnum.Name, ForcePublic(sourceEnum.Attributes),
var newType = new TypeDefinition(sourceEnum.Namespace, convertedTypeName, ForcePublic(sourceEnum.Attributes),
imports.Module.Enum().ToTypeDefOrRef());
foreach (var sourceEnumField in sourceEnum.Fields)
{
Expand Down Expand Up @@ -130,14 +136,25 @@
if (!fieldDefinition.Signature!.FieldType.IsValueType())
return true;

if (fieldDefinition.Signature.FieldType.Namespace?.StartsWith("System") ?? false &&
HasNonBlittableFields(fieldDefinition.Signature.FieldType.Resolve()))
return true;
if (fieldDefinition.Signature.FieldType.Namespace?.StartsWith("System") ?? false)
{
var resolved = fieldDefinition.Signature.FieldType.Resolve();
if (resolved != null && HasNonBlittableFields(resolved))
return true;
}
Comment on lines +140 to +145

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cursed, but it's not your fault. I see no reason for:

  • A System namespace check
  • Resolving a TypeSignature to a TypeDefinition, which removes the type arguments if the signature is a generic instance type

I'm not asking you to change anything, except maybe returning true for null resolved; I'm just saying that the preexisting code is extremely flawed.

}

return false;
}

private static string GetConvertedUnityTypeName(RewriteGlobalContext context, TypeDefinition unityType)
Comment thread
acan1980728690 marked this conversation as resolved.
Outdated
{
if (context.Options.PassthroughNames)
return unityType.Name;

Check warning on line 153 in Il2CppInterop.Generator/Passes/Pass79UnstripTypes.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 153 in Il2CppInterop.Generator/Passes/Pass79UnstripTypes.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
Comment thread
acan1980728690 marked this conversation as resolved.
Outdated

return unityType.Name.MakeValidInSource();
}

private static TypeAttributes ForcePublic(TypeAttributes typeAttributes)
{
var visibility = typeAttributes & TypeAttributes.VisibilityMask;
Expand Down
Loading