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

SamboyCoding / Cpp2IL / 15044082067

15 May 2025 11:43AM UTC coverage: 34.453% (-0.1%) from 34.593%
15044082067

Pull #452

github

web-flow
Merge 60e16a023 into b71542dec
Pull Request #452: Expand type system to support more types

1795 of 6522 branches covered (27.52%)

Branch coverage included in aggregate %.

22 of 101 new or added lines in 12 files covered. (21.78%)

4 existing lines in 3 files now uncovered.

4143 of 10713 relevant lines covered (38.67%)

160198.46 hits per line

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

32.14
/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 : HasCustomAttributesAndName, ITypeInfoProvider, ICSharpSourceToken
20
{
21
    internal const TypeAttributes DefaultTypeAttributes = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed;
22

23
    /// <summary>
24
    /// The context for the assembly this type was defined in.
25
    /// </summary>
26
    public readonly AssemblyAnalysisContext DeclaringAssembly;
27

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

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

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

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

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

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

58
    protected override int CustomAttributeIndex => Definition!.CustomAttributeIndex;
46,816✔
59

60
    public override AssemblyAnalysisContext CustomAttributeAssembly => DeclaringAssembly;
76,432✔
61

62
    public override string DefaultName => Definition?.Name! ?? throw new("Subclasses of TypeAnalysisContext must override DefaultName");
188,508!
63

64
    public virtual string DefaultNs => Definition?.Namespace ?? throw new("Subclasses of TypeAnalysisContext must override DefaultNs");
159,112!
65

66
    public string? OverrideNs { get; set; }
159,120✔
67

68
    public string Namespace => OverrideNs ?? DefaultNs;
159,120✔
69

70
    public TypeAnalysisContext? OverrideBaseType { get; protected set; }
559,395✔
71

72
    public TypeAnalysisContext? DeclaringType { get; protected internal set; }
134,179✔
73

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

76
    public TypeAnalysisContext? BaseType => OverrideBaseType ?? (Definition == null ? null : DeclaringAssembly.ResolveIl2CppType(Definition.RawBaseType));
276,321✔
77

78
    public TypeAnalysisContext[] InterfaceContexts => (Definition?.RawInterfaces.Select(DeclaringAssembly.ResolveIl2CppType).ToArray() ?? [])!;
×
79

80
    public virtual Il2CppTypeEnum Type
81
    {
82
        get
83
        {
84
            if (AppContext.SystemTypes.TryGetIl2CppTypeEnum(this, out var value))
×
85
                return value;
×
86
            
87
            if (Definition is { RawType: {} rawType })
×
88
                return rawType.Type;
×
89

90
            if (IsEnumType)
×
91
                return Il2CppTypeEnum.IL2CPP_TYPE_ENUM;
×
92

93
            if (IsValueType)
×
94
                return Il2CppTypeEnum.IL2CPP_TYPE_VALUETYPE;
×
95

96
            return Il2CppTypeEnum.IL2CPP_TYPE_CLASS;
×
97
        }
98
    }
99

100
    public string FullName
101
    {
102
        get
103
        {
104
            if (DeclaringType != null)
89,482✔
105
                return DeclaringType.FullName + "." + Name;
16,438✔
106

107
            if (string.IsNullOrEmpty(Namespace))
73,044✔
108
                return Name;
1,388✔
109

110
            return $"{Namespace}.{Name}";
71,656✔
111
        }
112
    }
113

114
    /// <summary>
115
    /// Returns the namespace of this type expressed as a folder hierarchy, with each sub-namespace becoming a sub-directory.
116
    /// If this type is in the global namespace, this will return an empty string.
117
    /// </summary>
118
    public string NamespaceAsSubdirs
119
    {
120
        get
121
        {
122
            var ns = Namespace;
×
123
            return string.IsNullOrEmpty(ns) ? "" : Path.Combine(MiscUtils.CleanPathElement(ns).Split('.'));
×
124
        }
125
    }
126

127
    /// <summary>
128
    /// Returns the top-level type this type is nested inside. If this type is not nested, will return this type.
129
    /// </summary>
130
    public TypeAnalysisContext UltimateDeclaringType => DeclaringType ?? this;
×
131

132
    public TypeAnalysisContext(Il2CppTypeDefinition? il2CppTypeDefinition, AssemblyAnalysisContext containingAssembly) : base(il2CppTypeDefinition?.Token ?? 0, containingAssembly.AppContext)
848,998✔
133
    {
134
        DeclaringAssembly = containingAssembly;
848,998✔
135
        Definition = il2CppTypeDefinition;
848,998✔
136

137
        if (Definition != null)
848,998✔
138
        {
139
            InitCustomAttributeData();
61,624✔
140

141
            Methods = Definition.Methods!.Select(m => new MethodAnalysisContext(m, this)).ToList();
414,574✔
142
            Properties = Definition.Properties!.Select(p => new PropertyAnalysisContext(p, this)).ToList();
122,236✔
143
            Events = Definition.Events!.Select(e => new EventAnalysisContext(e, this)).ToList();
61,894✔
144
            Fields = Definition.FieldInfos!.ToList().Select(f => new FieldAnalysisContext(f, this)).ToList();
303,050✔
145
        }
146
        else
147
        {
148
            Methods = [];
787,374✔
149
            Properties = [];
787,374✔
150
            Events = [];
787,374✔
151
            Fields = [];
787,374✔
152
        }
153
    }
787,374✔
154

155
    public MethodAnalysisContext? GetMethod(Il2CppMethodDefinition? methodDefinition)
156
    {
157
        if (methodDefinition == null)
350,844✔
158
            return null;
46,530✔
159

160
        return Methods.Find(m => m.Definition == methodDefinition);
5,823,046✔
161
    }
162

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

165
    public override string ToString() => $"Type: {Definition?.FullName}";
×
166

167
    public virtual string GetCSharpSourceString()
168
    {
169
        if (Definition != null)
×
170
            return Definition.FullName!;
×
171

172
        var ret = new StringBuilder();
×
173
        if (OverrideNs != null)
×
174
            ret.Append(OverrideNs).Append('.');
×
175

176
        ret.Append(Name);
×
177

178
        return ret.ToString();
×
179
    }
180

181
    public ArrayTypeAnalysisContext MakeArrayType(int rank)
182
    {
183
        return new(this, rank, DeclaringAssembly);
×
184
    }
185

186
    public ByRefTypeAnalysisContext MakeByReferenceType()
187
    {
188
        return new(this, DeclaringAssembly);
22,970✔
189
    }
190

191
    public GenericInstanceTypeAnalysisContext MakeGenericInstanceType(IEnumerable<TypeAnalysisContext> genericArguments)
192
    {
193
        return new(this, genericArguments, DeclaringAssembly);
330✔
194
    }
195

196
    public PointerTypeAnalysisContext MakePointerType()
197
    {
198
        return new(this, DeclaringAssembly);
×
199
    }
200

201
    public SzArrayTypeAnalysisContext MakeSzArrayType()
202
    {
203
        return new(this, DeclaringAssembly);
×
204
    }
205

206
    public PinnedTypeAnalysisContext MakePinnedType()
207
    {
NEW
208
        return new(this, DeclaringAssembly);
×
209
    }
210

211
    public BoxedTypeAnalysisContext MakeBoxedType()
212
    {
NEW
213
        return new(this, DeclaringAssembly);
×
214
    }
215

216
    public CustomModifierTypeAnalysisContext MakeCustomModifierType(TypeAnalysisContext modifierType, bool required)
217
    {
NEW
218
        return new(this, modifierType, required, DeclaringAssembly);
×
219
    }
220

221
    #region StableNameDotNet implementation
222

223
    public IEnumerable<ITypeInfoProvider> GetBaseTypeHierarchy()
224
    {
225
        if (OverrideBaseType != null)
×
226
            throw new("Type hierarchy for injected types is not supported");
×
227

228
        var baseType = Definition!.RawBaseType;
×
229
        while (baseType != null)
×
230
        {
231
            yield return GetSndnProviderForType(AppContext, baseType);
×
232

233
            baseType = baseType.CoerceToUnderlyingTypeDefinition().RawBaseType;
×
234
        }
235
    }
×
236

237
    public static ITypeInfoProvider GetSndnProviderForType(ApplicationAnalysisContext appContext, Il2CppType type)
238
    {
239
        if (type.Type == Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST)
×
240
        {
241
            var genericClass = type.GetGenericClass();
×
242
            var elementType = appContext.ResolveContextForType(genericClass.TypeDefinition)!;
×
243

244
            var genericParamTypes = genericClass.Context.ClassInst.Types;
×
245

246
            if (genericParamTypes.Any(t => t.Type is Il2CppTypeEnum.IL2CPP_TYPE_VAR or Il2CppTypeEnum.IL2CPP_TYPE_MVAR))
×
247
                //Discard non-fixed generic instances
248
                return elementType;
×
249

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

252
            return new GenericInstanceTypeInfoProviderWrapper(elementType, genericArguments);
×
253
        }
254

255
        if (type.Type is Il2CppTypeEnum.IL2CPP_TYPE_VAR or Il2CppTypeEnum.IL2CPP_TYPE_MVAR)
×
256
            return new GenericParameterTypeInfoProviderWrapper(type.GetGenericParameterDef().Name!);
×
257

258
        if (type.Type is Il2CppTypeEnum.IL2CPP_TYPE_SZARRAY or Il2CppTypeEnum.IL2CPP_TYPE_PTR)
×
259
            return GetSndnProviderForType(appContext, type.GetEncapsulatedType());
×
260

261
        if (type.Type is Il2CppTypeEnum.IL2CPP_TYPE_ARRAY)
×
262
            return GetSndnProviderForType(appContext, type.GetArrayElementType());
×
263

264
        if (type.Type.IsIl2CppPrimitive())
×
265
            return appContext.ResolveContextForType(LibCpp2IlReflection.PrimitiveTypeDefinitions[type.Type])!;
×
266

267
        return appContext.ResolveContextForType(type.AsClass())!;
×
268
    }
269

270
    public IEnumerable<ITypeInfoProvider> Interfaces => Definition!.RawInterfaces!.Select(t => GetSndnProviderForType(AppContext, t));
×
271
    public virtual TypeAttributes TypeAttributes => Definition?.Attributes ?? DefaultTypeAttributes;
14,754!
272
    public virtual int GenericParameterCount => Definition!.GenericContainer?.genericParameterCount ?? 0;
330!
273
    public string OriginalTypeName => DefaultName;
×
274
    public string RewrittenTypeName => Name;
×
275
    public string TypeNamespace => Namespace;
×
276
    public virtual bool IsGenericInstance => false;
×
277
    public virtual bool IsValueType => Definition?.IsValueType ?? BaseType is { Namespace: "System", Name: "ValueType" };
6,860!
278
    public bool IsEnumType => Definition?.IsEnumType ?? BaseType is { Namespace: "System", Name: "Enum" };
×
279
    public bool IsInterface => Definition?.IsInterface ?? ((TypeAttributes & TypeAttributes.Interface) != default);
3,166✔
280
    public IEnumerable<ITypeInfoProvider> GenericArgumentInfoProviders => Array.Empty<ITypeInfoProvider>();
×
281
    public IEnumerable<IFieldInfoProvider> FieldInfoProviders => Fields;
×
282
    public IEnumerable<IMethodInfoProvider> MethodInfoProviders => Methods;
×
283
    public IEnumerable<IPropertyInfoProvider> PropertyInfoProviders => Properties;
×
284
    public ITypeInfoProvider? DeclaringTypeInfoProvider => DeclaringType;
×
285

286
    #endregion
287
}
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