-
Notifications
You must be signed in to change notification settings - Fork 134
Fix duplicate types in generated assemblies on Unity 6.4+ #266
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
Open
acan1980728690
wants to merge
2
commits into
BepInEx:master
Choose a base branch
from
acan1980728690:fix/pass79-duplicate-types
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+28
−10
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| using AsmResolver; | ||
| using AsmResolver.DotNet; | ||
| using AsmResolver.DotNet.Signatures; | ||
| using AsmResolver.PE.DotNet.Metadata.Tables; | ||
|
|
@@ -47,15 +48,20 @@ private static void ProcessType(AssemblyRewriteContext processedAssembly, TypeDe | |
| 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. | ||
| if ((unityType.DeclaringType is null) != (enclosingNewType is null)) | ||
| 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); | ||
|
|
@@ -74,7 +80,8 @@ private static void ProcessType(AssemblyRewriteContext processedAssembly, TypeDe | |
| !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); | ||
|
|
@@ -100,9 +107,9 @@ private static void ProcessType(AssemblyRewriteContext processedAssembly, TypeDe | |
| ProcessType(processedAssembly, nestedUnityType, processedType, imports, ref typesUnstripped); | ||
| } | ||
|
|
||
| private static TypeDefinition CloneEnum(TypeDefinition sourceEnum, RuntimeAssemblyReferences imports) | ||
| private static TypeDefinition CloneEnum(TypeDefinition sourceEnum, Utf8String convertedTypeName, RuntimeAssemblyReferences imports) | ||
| { | ||
| 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) | ||
| { | ||
|
|
@@ -130,14 +137,25 @@ private static bool HasNonBlittableFields(TypeDefinition type) | |
| 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
I'm not asking you to change anything, except maybe returning true for null |
||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static Utf8String GetConvertedUnityTypeName(RewriteGlobalContext context, TypeDefinition unityType) | ||
| { | ||
| if (context.Options.PassthroughNames) | ||
| return unityType.Name ?? Utf8String.Empty; | ||
|
|
||
| return unityType.Name.MakeValidInSource(); | ||
| } | ||
|
|
||
| private static TypeAttributes ForcePublic(TypeAttributes typeAttributes) | ||
| { | ||
| var visibility = typeAttributes & TypeAttributes.VisibilityMask; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.