• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SamboyCoding / Cpp2IL / 15159531087

21 May 2025 10:13AM UTC coverage: 34.289% (+0.2%) from 34.047%
15159531087

Pull #462

github

web-flow
Merge c760cddbe into 5807d2b6c
Pull Request #462: Support overriding member types

1801 of 6650 branches covered (27.08%)

Branch coverage included in aggregate %.

121 of 213 new or added lines in 33 files covered. (56.81%)

22 existing lines in 6 files now uncovered.

4201 of 10854 relevant lines covered (38.7%)

186268.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.64
/Cpp2IL.Core/Model/Contexts/TypeAnalysisContext.cs
1
using System;
2
using System.Collections.Generic;
3
using System.IO;
4
using System.Linq;
5
using System.Reflection;
6
using System.Text;
7
using Cpp2IL.Core.Api;
8
using Cpp2IL.Core.Utils;
9
using LibCpp2IL.BinaryStructures;
10
using LibCpp2IL.Metadata;
11
using LibCpp2IL.Reflection;
12
using StableNameDotNet.Providers;
13

14
namespace Cpp2IL.Core.Model.Contexts;
15

16
/// <summary>
17
/// Represents one managed type in the application.
18
/// </summary>
19
public class TypeAnalysisContext : HasGenericParameters, ITypeInfoProvider, ICSharpSourceToken
20
{
21
    /// <summary>
22
    /// The context for the assembly this type was defined in.
23
    /// </summary>
24
    public readonly AssemblyAnalysisContext DeclaringAssembly;
25

26
    /// <summary>
27
    /// The underlying metadata for this type. Allows access to RGCTX data, the raw bitfield properties, interfaces, etc.
28
    /// </summary>
29
    public readonly Il2CppTypeDefinition? Definition;
30

31
    /// <summary>
32
    /// The analysis contexts for methods contained within this type.
33
    /// </summary>
34
    public readonly List<MethodAnalysisContext> Methods;
35

36
    /// <summary>
37
    /// The analysis contexts for properties contained within this type.
38
    /// </summary>
39
    public readonly List<PropertyAnalysisContext> Properties;
40

41
    /// <summary>
42
    /// The analysis contexts for events contained within this type.
43
    /// </summary>
44
    public readonly List<EventAnalysisContext> Events;
45

46
    /// <summary>
47
    /// The analysis contexts for fields contained within this type.
48
    /// </summary>
49
    public readonly List<FieldAnalysisContext> Fields;
50

51
    /// <summary>
52
    /// The analysis contexts for nested types within this type.
53
    /// </summary>
54
    public List<TypeAnalysisContext> NestedTypes { get; internal set; } = [];
739,310✔
55

56
    protected override int CustomAttributeIndex => Definition!.CustomAttributeIndex;
61,446✔
57

58
    public override AssemblyAnalysisContext CustomAttributeAssembly => DeclaringAssembly;
173,210✔
59

60
    public override string DefaultName => Definition?.Name! ?? throw new("Subclasses of TypeAnalysisContext must override DefaultName");
188,514!
61

62
    public virtual string DefaultNamespace => Definition?.Namespace ?? throw new("Subclasses of TypeAnalysisContext must override DefaultNs");
159,124!
63

64
    public string? OverrideNamespace { get; set; }
159,895✔
65

66
    public string Namespace => OverrideNamespace ?? DefaultNamespace;
159,895✔
67

68
    public virtual TypeAttributes DefaultAttributes => Definition?.Attributes ?? TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed;
17,593!
69

70
    public virtual TypeAttributes? OverrideAttributes { get; set; }
17,593✔
71

72
    public TypeAttributes Attributes => OverrideAttributes ?? DefaultAttributes;
17,593!
73

74
    public virtual TypeAnalysisContext? DefaultBaseType => Definition == null ? null : DeclaringAssembly.ResolveIl2CppType(Definition.RawBaseType);
449,149!
75

76
    public TypeAnalysisContext? OverrideBaseType { get; set; }
449,215✔
77

78
    public TypeAnalysisContext? BaseType => OverrideBaseType ?? DefaultBaseType;
449,215✔
79

80
    public TypeAnalysisContext? DeclaringType { get; protected internal set; }
792,141✔
81

82
    public TypeAnalysisContext? EnumUnderlyingType => Definition == null ? null : DeclaringAssembly.ResolveIl2CppType(Definition.EnumUnderlyingType);
×
83

84
    private List<TypeAnalysisContext>? _interfaceContexts;
85
    public List<TypeAnalysisContext> InterfaceContexts
86
    {
87
        get
88
        {
89
            // Lazy load the interface contexts
90
            _interfaceContexts ??= (Definition?.RawInterfaces.Select(DeclaringAssembly.ResolveIl2CppType).ToList() ?? [])!;
14,420!
91
            return _interfaceContexts;
14,420✔
92
        }
93
    }
94

95
    private List<GenericParameterTypeAnalysisContext>? _genericParameters;
96
    public override List<GenericParameterTypeAnalysisContext> GenericParameters
97
    {
98
        get
99
        {
100
            // Lazy load the generic parameters
101
            _genericParameters ??= Definition?.GenericContainer?.GenericParameters.Select(g => new GenericParameterTypeAnalysisContext(g, this)).ToList() ?? [];
429,588!
102
            return _genericParameters;
423,694✔
103
        }
104
    }
105

106
    public virtual Il2CppTypeEnum Type
107
    {
108
        get
109
        {
110
            if (AppContext.SystemTypes.TryGetIl2CppTypeEnum(this, out var value))
×
111
                return value;
×
112

113
            if (IsEnumType)
×
114
                return Il2CppTypeEnum.IL2CPP_TYPE_ENUM;
×
115

116
            if (IsValueType)
×
117
                return Il2CppTypeEnum.IL2CPP_TYPE_VALUETYPE;
×
118

119
            return Il2CppTypeEnum.IL2CPP_TYPE_CLASS;
×
120
        }
121
    }
122

123
    public string FullName
124
    {
125
        get
126
        {
127
            if (DeclaringType != null)
89,912✔
128
                return DeclaringType.FullName + "." + Name;
16,481✔
129

130
            if (string.IsNullOrEmpty(Namespace))
73,431✔
131
                return Name;
1,388✔
132

133
            return $"{Namespace}.{Name}";
72,043✔
134
        }
135
    }
136

137
    public TypeAttributes Visibility
138
    {
139
        get
140
        {
141
            return Attributes & TypeAttributes.VisibilityMask;
7✔
142
        }
143
        set
144
        {
NEW
145
            OverrideAttributes = (Attributes & ~TypeAttributes.VisibilityMask) | (value & TypeAttributes.VisibilityMask);
×
NEW
146
        }
×
147
    }
148

149
    public bool IsInterface => (Attributes & TypeAttributes.Interface) != default;
3,166✔
NEW
150
    public bool IsAbstract => (Attributes & TypeAttributes.Abstract) != default;
×
NEW
151
    public bool IsSealed => (Attributes & TypeAttributes.Sealed) != default;
×
NEW
152
    public bool IsStatic => IsAbstract && IsSealed;
×
153

154
    /// <summary>
155
    /// Returns the namespace of this type expressed as a folder hierarchy, with each sub-namespace becoming a sub-directory.
156
    /// If this type is in the global namespace, this will return an empty string.
157
    /// </summary>
158
    public string NamespaceAsSubdirs
159
    {
160
        get
161
        {
162
            var ns = Namespace;
×
163
            return string.IsNullOrEmpty(ns) ? "" : Path.Combine(MiscUtils.CleanPathElement(ns).Split('.'));
×
164
        }
165
    }
166

167
    /// <summary>
168
    /// Returns the top-level type this type is nested inside. If this type is not nested, will return this type.
169
    /// </summary>
170
    public TypeAnalysisContext UltimateDeclaringType => DeclaringType ?? this;
×
171

172
    public TypeAnalysisContext(Il2CppTypeDefinition? il2CppTypeDefinition, AssemblyAnalysisContext containingAssembly) : base(il2CppTypeDefinition?.Token ?? 0, containingAssembly.AppContext)
718,429✔
173
    {
174
        DeclaringAssembly = containingAssembly;
718,429✔
175
        Definition = il2CppTypeDefinition;
718,429✔
176

177
        if (Definition != null)
718,429✔
178
        {
179
            InitCustomAttributeData();
76,254✔
180

181
            Methods = Definition.Methods!.Select(m => new MethodAnalysisContext(m, this)).ToList();
516,234✔
182
            Properties = Definition.Properties!.Select(p => new PropertyAnalysisContext(p, this)).ToList();
152,226✔
183
            Events = Definition.Events!.Select(e => new EventAnalysisContext(e, this)).ToList();
76,599✔
184
            Fields = Definition.FieldInfos!.ToList().Select(f => new FieldAnalysisContext(f, this)).ToList();
376,575✔
185
        }
186
        else
187
        {
188
            Methods = [];
642,175✔
189
            Properties = [];
642,175✔
190
            Events = [];
642,175✔
191
            Fields = [];
642,175✔
192
        }
193
    }
642,175✔
194

195
    public MethodAnalysisContext? GetMethod(Il2CppMethodDefinition? methodDefinition)
196
    {
197
        if (methodDefinition == null)
451,299✔
198
            return null;
57,795✔
199

200
        return Methods.Find(m => m.Definition == methodDefinition);
7,564,371✔
201
    }
202

203
    public List<MethodAnalysisContext> GetConstructors() => Methods.Where(m => m.Definition!.Name == ".ctor").ToList();
×
204

NEW
205
    public override string ToString() => $"Type: {FullName}";
×
206

207
    public virtual string GetCSharpSourceString()
208
    {
209
        if (Definition != null)
×
210
            return Definition.FullName!;
×
211

212
        var ret = new StringBuilder();
×
NEW
213
        if (OverrideNamespace != null)
×
NEW
214
            ret.Append(OverrideNamespace).Append('.');
×
215

216
        ret.Append(Name);
×
217

218
        return ret.ToString();
×
219
    }
220

221
    public ArrayTypeAnalysisContext MakeArrayType(int rank)
222
    {
223
        return new(this, rank, DeclaringAssembly);
×
224
    }
225

226
    public ByRefTypeAnalysisContext MakeByReferenceType()
227
    {
228
        return new(this, DeclaringAssembly);
16,856✔
229
    }
230

231
    public GenericInstanceTypeAnalysisContext MakeGenericInstanceType(IEnumerable<TypeAnalysisContext> genericArguments)
232
    {
233
        return new(this, genericArguments, DeclaringAssembly);
77,948✔
234
    }
235

236
    public PointerTypeAnalysisContext MakePointerType()
237
    {
238
        return new(this, DeclaringAssembly);
×
239
    }
240

241
    public SzArrayTypeAnalysisContext MakeSzArrayType()
242
    {
243
        return new(this, DeclaringAssembly);
×
244
    }
245

246
    public PinnedTypeAnalysisContext MakePinnedType()
247
    {
248
        return new(this, DeclaringAssembly);
×
249
    }
250

251
    public BoxedTypeAnalysisContext MakeBoxedType()
252
    {
253
        return new(this, DeclaringAssembly);
×
254
    }
255

256
    public CustomModifierTypeAnalysisContext MakeCustomModifierType(TypeAnalysisContext modifierType, bool required)
257
    {
258
        return new(this, modifierType, required, DeclaringAssembly);
×
259
    }
260

261
    public InjectedTypeAnalysisContext InjectNestedType(string name, TypeAnalysisContext? baseType, TypeAttributes typeAttributes = TypeAttributes.NestedPublic | TypeAttributes.Sealed)
262
    {
263
        if (this is ReferencedTypeAnalysisContext)
43!
264
            throw new InvalidOperationException("Cannot inject nested types into a non type definition");
×
265

266
        var ret = new InjectedTypeAnalysisContext(DeclaringAssembly, "", name, baseType, typeAttributes);
43✔
267
        ret.DeclaringType = this;
43✔
268
        NestedTypes.Add(ret);
43✔
269
        DeclaringAssembly.InjectType(ret);
43✔
270
        return ret;
43✔
271
    }
272

273
    #region StableNameDotNet implementation
274

275
    public IEnumerable<ITypeInfoProvider> GetBaseTypeHierarchy()
276
    {
NEW
277
        if (IsInjected)
×
278
            throw new("Type hierarchy for injected types is not supported");
×
279

280
        var baseType = Definition!.RawBaseType;
×
281
        while (baseType != null)
×
282
        {
283
            yield return GetSndnProviderForType(AppContext, baseType);
×
284

285
            baseType = baseType.CoerceToUnderlyingTypeDefinition().RawBaseType;
×
286
        }
287
    }
×
288

289
    public static ITypeInfoProvider GetSndnProviderForType(ApplicationAnalysisContext appContext, Il2CppType type)
290
    {
291
        if (type.Type == Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST)
×
292
        {
293
            var genericClass = type.GetGenericClass();
×
294
            var elementType = appContext.ResolveContextForType(genericClass.TypeDefinition)!;
×
295

296
            var genericParamTypes = genericClass.Context.ClassInst.Types;
×
297

298
            if (genericParamTypes.Any(t => t.Type is Il2CppTypeEnum.IL2CPP_TYPE_VAR or Il2CppTypeEnum.IL2CPP_TYPE_MVAR))
×
299
                //Discard non-fixed generic instances
300
                return elementType;
×
301

302
            var genericArguments = genericParamTypes.Select(t => GetSndnProviderForType(appContext, t)).ToArray();
×
303

304
            return new GenericInstanceTypeInfoProviderWrapper(elementType, genericArguments);
×
305
        }
306

307
        if (type.Type is Il2CppTypeEnum.IL2CPP_TYPE_VAR or Il2CppTypeEnum.IL2CPP_TYPE_MVAR)
×
308
            return new GenericParameterTypeInfoProviderWrapper(type.GetGenericParameterDef().Name!);
×
309

310
        if (type.Type is Il2CppTypeEnum.IL2CPP_TYPE_SZARRAY or Il2CppTypeEnum.IL2CPP_TYPE_PTR)
×
311
            return GetSndnProviderForType(appContext, type.GetEncapsulatedType());
×
312

313
        if (type.Type is Il2CppTypeEnum.IL2CPP_TYPE_ARRAY)
×
314
            return GetSndnProviderForType(appContext, type.GetArrayElementType());
×
315

316
        if (type.Type.IsIl2CppPrimitive())
×
317
            return appContext.ResolveContextForType(LibCpp2IlReflection.PrimitiveTypeDefinitions[type.Type])!;
×
318

319
        return appContext.ResolveContextForType(type.AsClass())!;
×
320
    }
321

NEW
322
    IEnumerable<ITypeInfoProvider> ITypeInfoProvider.Interfaces => Definition!.RawInterfaces!.Select(t => GetSndnProviderForType(AppContext, t));
×
NEW
323
    TypeAttributes ITypeInfoProvider.TypeAttributes => Attributes;
×
NEW
324
    int ITypeInfoProvider.GenericParameterCount => GenericParameters.Count;
×
NEW
325
    string ITypeInfoProvider.OriginalTypeName => DefaultName;
×
NEW
326
    string ITypeInfoProvider.RewrittenTypeName => Name;
×
NEW
327
    string ITypeInfoProvider.TypeNamespace => Namespace;
×
UNCOV
328
    public virtual bool IsGenericInstance => false;
×
329
    public virtual bool IsValueType => Definition?.IsValueType ?? BaseType is { Namespace: "System", Name: "ValueType" };
8,920!
330
    public bool IsEnumType => Definition?.IsEnumType ?? BaseType is { Namespace: "System", Name: "Enum" };
×
NEW
331
    IEnumerable<ITypeInfoProvider> ITypeInfoProvider.GenericArgumentInfoProviders => [];
×
NEW
332
    IEnumerable<IFieldInfoProvider> ITypeInfoProvider.FieldInfoProviders => Fields;
×
NEW
333
    IEnumerable<IMethodInfoProvider> ITypeInfoProvider.MethodInfoProviders => Methods;
×
NEW
334
    IEnumerable<IPropertyInfoProvider> ITypeInfoProvider.PropertyInfoProviders => Properties;
×
NEW
335
    ITypeInfoProvider? ITypeInfoProvider.DeclaringTypeInfoProvider => DeclaringType;
×
336

337
    #endregion
338
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc