From d03ec7dd9dce3ae80424a1e937e78f1b8700cdb8 Mon Sep 17 00:00:00 2001 From: ds5678 <49847914+ds5678@users.noreply.github.com> Date: Sun, 17 May 2026 16:32:01 -0700 Subject: [PATCH] Fix name overrides for constructed types * Ensure that DefaultName only calls DefaultName and DefaultFullName, not Name or FullName * Remove unnecessary ReferenceTypeAnalysisContext::ToString override * Seal Type and name properties * Use DefaultFullName and FullName for arguments of GenericInstanceTypeAnalysisContext to ensure expected output from its own DefaultFullName and FullName --- .../Contexts/ArrayTypeAnalysisContext.cs | 11 ++++++++-- .../Contexts/BoxedTypeAnalysisContext.cs | 10 +++++++-- .../Contexts/ByRefTypeAnalysisContext.cs | 11 ++++++++-- .../CustomModifierTypeAnalysisContext.cs | 17 ++++++++++---- .../GenericInstanceTypeAnalysisContext.cs | 22 ++++++++++++++----- .../GenericParameterTypeAnalysisContext.cs | 10 ++++++++- .../Contexts/PinnedTypeAnalysisContext.cs | 10 +++++++-- .../Contexts/PointerTypeAnalysisContext.cs | 11 ++++++++-- .../Contexts/ReferencedTypeAnalysisContext.cs | 5 ----- .../Contexts/SentinelTypeAnalysisContext.cs | 15 +++++++++++++ .../Contexts/SzArrayTypeAnalysisContext.cs | 11 ++++++++-- .../Model/Contexts/TypeAnalysisContext.cs | 2 +- .../Contexts/WrappedTypeAnalysisContext.cs | 8 ++++++- 13 files changed, 114 insertions(+), 29 deletions(-) diff --git a/Cpp2IL.Core/Model/Contexts/ArrayTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/ArrayTypeAnalysisContext.cs index d880e52c5..bdc593f17 100644 --- a/Cpp2IL.Core/Model/Contexts/ArrayTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/ArrayTypeAnalysisContext.cs @@ -1,3 +1,4 @@ +using System; using Cpp2IL.Core.Utils; using LibCpp2IL.BinaryStructures; @@ -11,9 +12,15 @@ public ArrayTypeAnalysisContext(Il2CppType rawType, AssemblyAnalysisContext refe { } - public override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_ARRAY; + public sealed override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_ARRAY; - public override string DefaultName => $"{ElementType.Name}[{Rank}]"; + public sealed override string DefaultName => $"{ElementType.DefaultName}[{Rank}]"; + + public sealed override string? OverrideName + { + get => $"{ElementType.Name}[{Rank}]"; + set => throw new NotSupportedException(); + } public sealed override bool IsValueType => false; diff --git a/Cpp2IL.Core/Model/Contexts/BoxedTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/BoxedTypeAnalysisContext.cs index 1c950f450..0cbac5292 100644 --- a/Cpp2IL.Core/Model/Contexts/BoxedTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/BoxedTypeAnalysisContext.cs @@ -11,9 +11,15 @@ public BoxedTypeAnalysisContext(Il2CppType rawType, AssemblyAnalysisContext refe { } - public override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_BOXED; + public sealed override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_BOXED; - public override string DefaultName => ElementType.Name; + public sealed override string DefaultName => ElementType.DefaultName; + + public sealed override string? OverrideName + { + get => ElementType.OverrideName; + set => ElementType.OverrideName = value; + } public sealed override bool IsValueType => false; } diff --git a/Cpp2IL.Core/Model/Contexts/ByRefTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/ByRefTypeAnalysisContext.cs index ebb2e15dd..1a83d1492 100644 --- a/Cpp2IL.Core/Model/Contexts/ByRefTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/ByRefTypeAnalysisContext.cs @@ -1,3 +1,4 @@ +using System; using LibCpp2IL.BinaryStructures; namespace Cpp2IL.Core.Model.Contexts; @@ -10,9 +11,15 @@ public ByRefTypeAnalysisContext(Il2CppType rawType, AssemblyAnalysisContext refe { } - public override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_BYREF; + public sealed override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_BYREF; - public override string DefaultName => $"{ElementType.Name}&"; + public sealed override string DefaultName => $"{ElementType.DefaultName}&"; + + public sealed override string? OverrideName + { + get => $"{ElementType.Name}&"; + set => throw new NotSupportedException(); + } public sealed override bool IsValueType => false; diff --git a/Cpp2IL.Core/Model/Contexts/CustomModifierTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/CustomModifierTypeAnalysisContext.cs index 0bdff7770..90f823ea1 100644 --- a/Cpp2IL.Core/Model/Contexts/CustomModifierTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/CustomModifierTypeAnalysisContext.cs @@ -1,3 +1,4 @@ +using System; using LibCpp2IL.BinaryStructures; namespace Cpp2IL.Core.Model.Contexts; @@ -9,11 +10,19 @@ public class CustomModifierTypeAnalysisContext(TypeAnalysisContext elementType, public bool Required { get; } = required; - public override Il2CppTypeEnum Type => Required ? Il2CppTypeEnum.IL2CPP_TYPE_CMOD_REQD : Il2CppTypeEnum.IL2CPP_TYPE_CMOD_OPT; + public sealed override Il2CppTypeEnum Type => Required ? Il2CppTypeEnum.IL2CPP_TYPE_CMOD_REQD : Il2CppTypeEnum.IL2CPP_TYPE_CMOD_OPT; - public override string DefaultName => Required - ? $"{ElementType.Name} modreq({ModifierType.Name})" - : $"{ElementType.Name} modopt({ModifierType.Name})"; + public sealed override string DefaultName => Required + ? $"{ElementType.DefaultName} modreq({ModifierType.DefaultFullName})" + : $"{ElementType.DefaultName} modopt({ModifierType.DefaultFullName})"; + + public sealed override string? OverrideName + { + get => Required + ? $"{ElementType.Name} modreq({ModifierType.FullName})" + : $"{ElementType.Name} modopt({ModifierType.FullName})"; + set => throw new NotSupportedException(); + } public sealed override bool IsValueType => ElementType.IsValueType; } diff --git a/Cpp2IL.Core/Model/Contexts/GenericInstanceTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/GenericInstanceTypeAnalysisContext.cs index 1cf646a00..fe483eec6 100644 --- a/Cpp2IL.Core/Model/Contexts/GenericInstanceTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/GenericInstanceTypeAnalysisContext.cs @@ -15,15 +15,27 @@ public class GenericInstanceTypeAnalysisContext : ReferencedTypeAnalysisContext public List GenericArguments { get; } = []; - public override TypeAttributes DefaultAttributes => GenericType.DefaultAttributes; + public sealed override TypeAttributes DefaultAttributes => GenericType.DefaultAttributes; - public override TypeAttributes? OverrideAttributes { get => GenericType.OverrideAttributes; set => GenericType.OverrideAttributes = value; } + public sealed override TypeAttributes? OverrideAttributes { get => GenericType.OverrideAttributes; set => GenericType.OverrideAttributes = value; } - public override string DefaultName => $"{GenericType.Name}<{string.Join(", ", GenericArguments.Select(a => a.Name))}>"; + public sealed override string DefaultName => $"{GenericType.DefaultName}<{string.Join(", ", GenericArguments.Select(a => a.DefaultFullName))}>"; - public override string DefaultNamespace => GenericType.Namespace; + public sealed override string? OverrideName + { + get => $"{GenericType.Name}<{string.Join(", ", GenericArguments.Select(a => a.FullName))}>"; + set => throw new NotSupportedException(); + } + + public sealed override string DefaultNamespace => GenericType.DefaultNamespace; + + public sealed override string? OverrideNamespace + { + get => GenericType.OverrideNamespace; + set => GenericType.OverrideNamespace = value; + } - public override TypeAnalysisContext? DefaultBaseType { get; } + public sealed override TypeAnalysisContext? DefaultBaseType { get; } public sealed override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST; diff --git a/Cpp2IL.Core/Model/Contexts/GenericParameterTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/GenericParameterTypeAnalysisContext.cs index 3a850ad74..776532443 100644 --- a/Cpp2IL.Core/Model/Contexts/GenericParameterTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/GenericParameterTypeAnalysisContext.cs @@ -16,9 +16,17 @@ public class GenericParameterTypeAnalysisContext : ReferencedTypeAnalysisContext public sealed override string DefaultNamespace => ""; + public sealed override string? OverrideNamespace + { + get => null; + set + { + } + } + public int Index { get; } - public override Il2CppTypeEnum Type { get; } + public sealed override Il2CppTypeEnum Type { get; } public new GenericParameterAttributes DefaultAttributes { get; } public new GenericParameterAttributes? OverrideAttributes { get; set; } diff --git a/Cpp2IL.Core/Model/Contexts/PinnedTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/PinnedTypeAnalysisContext.cs index d3e2759b9..a00aeeee3 100644 --- a/Cpp2IL.Core/Model/Contexts/PinnedTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/PinnedTypeAnalysisContext.cs @@ -11,9 +11,15 @@ public PinnedTypeAnalysisContext(Il2CppType rawType, AssemblyAnalysisContext ref { } - public override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_PINNED; + public sealed override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_PINNED; - public override string DefaultName => ElementType.Name; + public sealed override string DefaultName => ElementType.DefaultName; + + public sealed override string? OverrideName + { + get => ElementType.OverrideName; + set => ElementType.OverrideName = value; + } public sealed override bool IsValueType => false; } diff --git a/Cpp2IL.Core/Model/Contexts/PointerTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/PointerTypeAnalysisContext.cs index 7f6823480..91a295f91 100644 --- a/Cpp2IL.Core/Model/Contexts/PointerTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/PointerTypeAnalysisContext.cs @@ -1,3 +1,4 @@ +using System; using Cpp2IL.Core.Utils; using LibCpp2IL.BinaryStructures; @@ -11,9 +12,15 @@ public PointerTypeAnalysisContext(Il2CppType rawType, AssemblyAnalysisContext re { } - public override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_PTR; + public sealed override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_PTR; - public override string DefaultName => $"{ElementType.Name}*"; + public sealed override string DefaultName => $"{ElementType.DefaultName}*"; + + public sealed override string? OverrideName + { + get => $"{ElementType.Name}*"; + set => throw new NotSupportedException(); + } public sealed override bool IsValueType => false; } diff --git a/Cpp2IL.Core/Model/Contexts/ReferencedTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/ReferencedTypeAnalysisContext.cs index d0a8a9462..44dd52505 100644 --- a/Cpp2IL.Core/Model/Contexts/ReferencedTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/ReferencedTypeAnalysisContext.cs @@ -15,11 +15,6 @@ public abstract class ReferencedTypeAnalysisContext(AssemblyAnalysisContext refe public override AssemblyAnalysisContext CustomAttributeAssembly => DeclaringAssembly; - public override string ToString() - { - return DefaultName; - } - public override string GetCSharpSourceString() { return Name; diff --git a/Cpp2IL.Core/Model/Contexts/SentinelTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/SentinelTypeAnalysisContext.cs index ac69b1e26..8cdb101ff 100644 --- a/Cpp2IL.Core/Model/Contexts/SentinelTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/SentinelTypeAnalysisContext.cs @@ -6,5 +6,20 @@ public sealed class SentinelTypeAnalysisContext(AssemblyAnalysisContext referenc { public override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_SENTINEL; public override string DefaultName => "<>"; + public override string? OverrideName + { + get => null; + set + { + } + } + public override string DefaultNamespace => ""; + public override string? OverrideNamespace + { + get => null; + set + { + } + } public override bool IsValueType => false; } diff --git a/Cpp2IL.Core/Model/Contexts/SzArrayTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/SzArrayTypeAnalysisContext.cs index 72aad8b58..5e1c8b89b 100644 --- a/Cpp2IL.Core/Model/Contexts/SzArrayTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/SzArrayTypeAnalysisContext.cs @@ -1,3 +1,4 @@ +using System; using Cpp2IL.Core.Utils; using LibCpp2IL.BinaryStructures; @@ -11,9 +12,15 @@ public SzArrayTypeAnalysisContext(Il2CppType rawType, AssemblyAnalysisContext re { } - public override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_SZARRAY; + public sealed override Il2CppTypeEnum Type => Il2CppTypeEnum.IL2CPP_TYPE_SZARRAY; - public override string DefaultName => $"{ElementType.Name}[]"; + public sealed override string DefaultName => $"{ElementType.DefaultName}[]"; + + public sealed override string? OverrideName + { + get => $"{ElementType.Name}[]"; + set => throw new NotSupportedException(); + } public sealed override bool IsValueType => false; } diff --git a/Cpp2IL.Core/Model/Contexts/TypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/TypeAnalysisContext.cs index d11dfb91b..e05ab7501 100644 --- a/Cpp2IL.Core/Model/Contexts/TypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/TypeAnalysisContext.cs @@ -61,7 +61,7 @@ public class TypeAnalysisContext : HasGenericParameters, ITypeInfoProvider, ICSh public virtual string DefaultNamespace => Definition?.Namespace ?? throw new("Subclasses of TypeAnalysisContext must override DefaultNs"); - public string? OverrideNamespace { get; set; } + public virtual string? OverrideNamespace { get; set; } public string Namespace { diff --git a/Cpp2IL.Core/Model/Contexts/WrappedTypeAnalysisContext.cs b/Cpp2IL.Core/Model/Contexts/WrappedTypeAnalysisContext.cs index 44278f84c..50491eea7 100644 --- a/Cpp2IL.Core/Model/Contexts/WrappedTypeAnalysisContext.cs +++ b/Cpp2IL.Core/Model/Contexts/WrappedTypeAnalysisContext.cs @@ -12,7 +12,13 @@ public abstract class WrappedTypeAnalysisContext( { public virtual TypeAnalysisContext ElementType { get; } = elementType; - public override string DefaultNamespace => ElementType.Namespace; + public sealed override string DefaultNamespace => ElementType.DefaultNamespace; + + public sealed override string? OverrideNamespace + { + get => ElementType.OverrideNamespace; + set => ElementType.OverrideNamespace = value; + } public static WrappedTypeAnalysisContext Create(Il2CppType rawType, AssemblyAnalysisContext referencedFrom) {