Skip to content

Commit e93c0fe

Browse files
authored
Implement some helpers to improve the debugging experience (SamboyCoding#437)
* Implement some helpers to improve the debugging experience * Revisions
1 parent d8ceca2 commit e93c0fe

File tree

8 files changed

+95
-36
lines changed

8 files changed

+95
-36
lines changed

Cpp2IL.Core/Model/Contexts/GenericParameterTypeAnalysisContext.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Diagnostics;
32
using LibCpp2IL.BinaryStructures;
43
using LibCpp2IL.Metadata;
54

@@ -18,7 +17,6 @@ public class GenericParameterTypeAnalysisContext : ReferencedTypeAnalysisContext
1817
public GenericParameterTypeAnalysisContext(Il2CppType rawType, AssemblyAnalysisContext referencedFrom)
1918
: this(rawType.GetGenericParameterDef(), referencedFrom)
2019
{
21-
Debug.Assert(Type == rawType.Type);
2220
}
2321

2422
public GenericParameterTypeAnalysisContext(Il2CppGenericParameter genericParameter, AssemblyAnalysisContext referencedFrom)

Cpp2IL.Core/Utils/AsmResolver/AsmResolverAssemblyPopulator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private static void PopulateGenericParamsForType(Il2CppTypeDefinition cppTypeDef
6666

6767
if (!AsmResolverUtils.GenericParamsByIndexNew.TryGetValue(param.Index, out var p))
6868
{
69-
p = new GenericParameter(param.Name, (GenericParameterAttributes)param.flags);
69+
p = new GenericParameter(param.Name, (GenericParameterAttributes)param.Attributes);
7070
AsmResolverUtils.GenericParamsByIndexNew[param.Index] = p;
7171

7272
ilTypeDefinition.GenericParameters.Add(p);
@@ -450,7 +450,7 @@ private static void CopyMethodsInType(ReferenceImporter importer, TypeAnalysisCo
450450
return;
451451
}
452452

453-
gp = new(p.Name, (GenericParameterAttributes)p.flags);
453+
gp = new(p.Name, (GenericParameterAttributes)p.Attributes);
454454

455455
if (!managedMethod.GenericParameters.Contains(gp))
456456
managedMethod.GenericParameters.Add(gp);

Cpp2IL.Core/Utils/MiscUtils.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
//#define NOT_REALLY_PARALLEL_FOR_TESTING
2-
31
using System;
4-
using System.Collections;
52
using System.Collections.Generic;
3+
using System.Diagnostics;
64
using System.Linq;
75
using System.Text;
86
using Cpp2IL.Core.Model.Contexts;
@@ -248,10 +246,16 @@ bool F2(T t)
248246
return true;
249247
}
250248

249+
#if DEBUG
250+
if (Debugger.IsAttached)
251+
{
252+
ExecuteSerial(enumerable, what);
253+
return;
254+
}
255+
#endif
256+
251257
enumerable
252-
#if !NOT_REALLY_PARALLEL_FOR_TESTING
253258
.AsParallel()
254-
#endif
255259
.Select((Func<T, bool>)F2)
256260
.ToList();
257261
}

LibCpp2IL/BinaryStructures/Il2CppArrayType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public class Il2CppArrayType : ReadableClass
99
public ulong sizes;
1010
public ulong lobounds;
1111

12+
public Il2CppType ElementType => LibCpp2IlMain.Binary!.GetIl2CppTypeFromPointer(etype);
13+
1214
public override void Read(ClassReadingBinaryReader reader)
1315
{
1416
etype = reader.ReadNUint();

LibCpp2IL/BinaryStructures/Il2CppType.cs

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using System.Reflection;
2+
using System.Diagnostics;
33
using LibCpp2IL.Metadata;
44
using LibCpp2IL.Reflection;
55

@@ -52,48 +52,85 @@ public class Union
5252
public ulong GenericClass => Dummy;
5353
}
5454

55+
private Il2CppTypeDefinition? Class
56+
{
57+
get
58+
{
59+
if (Type is not Il2CppTypeEnum.IL2CPP_TYPE_CLASS and not Il2CppTypeEnum.IL2CPP_TYPE_VALUETYPE)
60+
return null;
61+
return LibCpp2IlMain.TheMetadata!.typeDefs[Data.ClassIndex];
62+
}
63+
}
64+
5565
public Il2CppTypeDefinition AsClass()
5666
{
57-
if (Type is not Il2CppTypeEnum.IL2CPP_TYPE_CLASS and not Il2CppTypeEnum.IL2CPP_TYPE_VALUETYPE)
58-
throw new Exception("Type is not a class, but a " + Type);
67+
return Class ?? throw new Exception("Type is not a class, but a " + Type);
68+
}
5969

60-
return LibCpp2IlMain.TheMetadata!.typeDefs[Data.ClassIndex];
70+
private Il2CppType? EncapsulatedType
71+
{
72+
get
73+
{
74+
if (Type is not Il2CppTypeEnum.IL2CPP_TYPE_PTR and not Il2CppTypeEnum.IL2CPP_TYPE_SZARRAY)
75+
return null;
76+
return LibCpp2IlMain.Binary!.GetIl2CppTypeFromPointer(Data.Type);
77+
}
6178
}
6279

6380
public Il2CppType GetEncapsulatedType()
6481
{
65-
if (Type is not Il2CppTypeEnum.IL2CPP_TYPE_PTR and not Il2CppTypeEnum.IL2CPP_TYPE_SZARRAY)
66-
throw new Exception("Type does not have a encapsulated type - it is not a pointer or an szarray");
82+
return EncapsulatedType ?? throw new Exception("Type does not have a encapsulated type - it is not a pointer or an szarray");
83+
}
6784

68-
return LibCpp2IlMain.Binary!.GetIl2CppTypeFromPointer(Data.Type);
85+
private Il2CppArrayType? ArrayType
86+
{
87+
get
88+
{
89+
if (Type is not Il2CppTypeEnum.IL2CPP_TYPE_ARRAY)
90+
return null;
91+
return LibCpp2IlMain.Binary!.ReadReadableAtVirtualAddress<Il2CppArrayType>(Data.Array);
92+
}
6993
}
7094

7195
public Il2CppArrayType GetArrayType()
7296
{
73-
if (Type is not Il2CppTypeEnum.IL2CPP_TYPE_ARRAY)
74-
throw new Exception("Type is not an array");
75-
76-
return LibCpp2IlMain.Binary!.ReadReadableAtVirtualAddress<Il2CppArrayType>(Data.Array);
97+
return ArrayType ?? throw new Exception("Type is not an array");
7798
}
7899

79-
public Il2CppType GetArrayElementType() => LibCpp2IlMain.Binary!.GetIl2CppTypeFromPointer(GetArrayType().etype);
100+
public Il2CppType GetArrayElementType() => GetArrayType().ElementType;
80101

81102
public int GetArrayRank() => GetArrayType().rank;
82103

104+
private Il2CppGenericParameter? GenericParameter
105+
{
106+
get
107+
{
108+
if (Type is not Il2CppTypeEnum.IL2CPP_TYPE_VAR and not Il2CppTypeEnum.IL2CPP_TYPE_MVAR)
109+
return null;
110+
return LibCpp2IlMain.TheMetadata!.genericParameters[Data.GenericParameterIndex];
111+
}
112+
}
113+
83114
public Il2CppGenericParameter GetGenericParameterDef()
84115
{
85-
if (Type is not Il2CppTypeEnum.IL2CPP_TYPE_VAR and not Il2CppTypeEnum.IL2CPP_TYPE_MVAR)
86-
throw new Exception("Type is not a generic parameter");
116+
var result = GenericParameter ?? throw new Exception("Type is not a generic parameter");
117+
Debug.Assert(result.Type == Type);
118+
return result;
119+
}
87120

88-
return LibCpp2IlMain.TheMetadata!.genericParameters[Data.GenericParameterIndex];
121+
private Il2CppGenericClass? GenericClass
122+
{
123+
get
124+
{
125+
if (Type is not Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST)
126+
return null;
127+
return LibCpp2IlMain.Binary!.ReadReadableAtVirtualAddress<Il2CppGenericClass>(Data.GenericClass);
128+
}
89129
}
90130

91131
public Il2CppGenericClass GetGenericClass()
92132
{
93-
if (Type is not Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST)
94-
throw new Exception("Type is not a generic class");
95-
96-
return LibCpp2IlMain.Binary!.ReadReadableAtVirtualAddress<Il2CppGenericClass>(Data.GenericClass);
133+
return GenericClass ?? throw new Exception("Type is not a generic class");
97134
}
98135

99136
public override void Read(ClassReadingBinaryReader reader)

LibCpp2IL/LibCpp2IlUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static string GetTypeName(Il2CppMetadata metadata, Il2CppBinary cppAssemb
143143
case Il2CppTypeEnum.IL2CPP_TYPE_ARRAY:
144144
{
145145
var arrayType = cppAssembly.ReadReadableAtVirtualAddress<Il2CppArrayType>(type.Data.Array);
146-
var oriType = cppAssembly.GetIl2CppTypeFromPointer(arrayType.etype);
146+
var oriType = arrayType.ElementType;
147147
ret = $"{GetTypeName(metadata, cppAssembly, oriType)}[{new string(',', arrayType.rank - 1)}]";
148148
break;
149149
}
@@ -351,7 +351,7 @@ public static Il2CppTypeReflectionData GetTypeReflectionData(Il2CppType forWhat)
351351
case Il2CppTypeEnum.IL2CPP_TYPE_ARRAY:
352352
{
353353
var arrayType = LibCpp2IlMain.Binary.ReadReadableAtVirtualAddress<Il2CppArrayType>(forWhat.Data.Array);
354-
var oriType = LibCpp2IlMain.Binary.GetIl2CppTypeFromPointer(arrayType.etype);
354+
var oriType = arrayType.ElementType;
355355
return new()
356356
{
357357
baseType = null,

LibCpp2IL/Metadata/Il2CppGenericContainer.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Il2CppGenericContainer : ReadableClass
1111
public int genericParameterCount;
1212

1313
/* If true, we're a generic method, otherwise a generic type definition. */
14-
public int isGenericMethod;
14+
public bool isGenericMethod; // actually an int, but we treat it as a bool
1515

1616
/* Our type parameters. */
1717
public int genericParameterStart;
@@ -33,11 +33,29 @@ public IEnumerable<Il2CppGenericParameter> GenericParameters
3333
}
3434
}
3535

36+
public Il2CppTypeDefinition? TypeOwner
37+
{
38+
get
39+
{
40+
return isGenericMethod ? null : LibCpp2IlMain.TheMetadata!.typeDefs[ownerIndex];
41+
}
42+
}
43+
44+
public Il2CppMethodDefinition? MethodOwner
45+
{
46+
get
47+
{
48+
return isGenericMethod ? LibCpp2IlMain.TheMetadata!.methodDefs[ownerIndex] : null;
49+
}
50+
}
51+
52+
public Il2CppTypeDefinition? DeclaringType => TypeOwner ?? MethodOwner?.DeclaringType;
53+
3654
public override void Read(ClassReadingBinaryReader reader)
3755
{
3856
ownerIndex = reader.ReadInt32();
3957
genericParameterCount = reader.ReadInt32();
40-
isGenericMethod = reader.ReadInt32();
58+
isGenericMethod = reader.ReadInt32() != 0;
4159
genericParameterStart = reader.ReadInt32();
4260
}
4361
}

LibCpp2IL/Metadata/Il2CppGenericParameter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System;
21
using System.Linq;
2+
using System.Reflection;
33
using LibCpp2IL.BinaryStructures;
44

55
namespace LibCpp2IL.Metadata;
@@ -13,6 +13,8 @@ public class Il2CppGenericParameter : ReadableClass
1313
public ushort genericParameterIndexInOwner;
1414
public ushort flags;
1515

16+
public GenericParameterAttributes Attributes => (GenericParameterAttributes)flags;
17+
1618
public string? Name => LibCpp2IlMain.TheMetadata?.GetStringFromIndex(nameIndex);
1719

1820
public Il2CppType[]? ConstraintTypes => constraintsCount == 0
@@ -27,9 +29,7 @@ public class Il2CppGenericParameter : ReadableClass
2729

2830
public Il2CppGenericContainer Owner => LibCpp2IlMain.TheMetadata!.genericContainers[ownerIndex];
2931

30-
private bool IsOwnedByMethod => Owner.isGenericMethod != 0;
31-
32-
public Il2CppTypeEnum Type => IsOwnedByMethod ? Il2CppTypeEnum.IL2CPP_TYPE_MVAR : Il2CppTypeEnum.IL2CPP_TYPE_VAR;
32+
public Il2CppTypeEnum Type => Owner.isGenericMethod ? Il2CppTypeEnum.IL2CPP_TYPE_MVAR : Il2CppTypeEnum.IL2CPP_TYPE_VAR;
3333

3434
public override void Read(ClassReadingBinaryReader reader)
3535
{

0 commit comments

Comments
 (0)