-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathTypeDefinition.cs
More file actions
263 lines (210 loc) · 10.5 KB
/
TypeDefinition.cs
File metadata and controls
263 lines (210 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
namespace HackF5.UnitySpy.Detail
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
/// <summary>
/// Represents an unmanaged _MonoClass instance in a Mono process. This object describes the type of a class or
/// struct. The .NET equivalent is <see cref="System.Type"/>.
/// See: _MonoClass in https://github.com/Unity-Technologies/mono/blob/unity-master/mono/metadata/class-internals.h.
/// </summary>
[PublicAPI]
[DebuggerDisplay("Class: {" + nameof(TypeDefinition.Name) + "}")]
public class TypeDefinition : MemoryObject, ITypeDefinition
{
private readonly uint bitFields;
private readonly ConcurrentDictionary<(string @class, string name), FieldDefinition> fieldCache =
new ConcurrentDictionary<(string @class, string name), FieldDefinition>();
private readonly int fieldCount;
private readonly Lazy<IReadOnlyList<FieldDefinition>> lazyFields;
private readonly Lazy<string> lazyFullName;
private readonly Lazy<TypeDefinition> lazyNestedIn;
private readonly Lazy<TypeDefinition> lazyParent;
private readonly Lazy<TypeDefinition> lazyGeneric;
private readonly List<TypeInfo> genericTypeArguments;
public TypeDefinition([NotNull] AssemblyImage image, IntPtr address)
: base(image, address)
{
if (image == null)
{
throw new ArgumentNullException(nameof(image));
}
this.bitFields = this.ReadUInt32(image.Process.MonoLibraryOffsets.TypeDefinitionBitFields);
this.fieldCount = this.ReadInt32(image.Process.MonoLibraryOffsets.TypeDefinitionFieldCount);
this.lazyParent = new Lazy<TypeDefinition>(() => this.GetClassDefinition(image.Process.MonoLibraryOffsets.TypeDefinitionParent));
this.lazyNestedIn = new Lazy<TypeDefinition>(() => this.GetClassDefinition(image.Process.MonoLibraryOffsets.TypeDefinitionNestedIn));
this.lazyFullName = new Lazy<string>(this.GetFullName);
this.lazyFields = new Lazy<IReadOnlyList<FieldDefinition>>(this.GetFields);
this.lazyGeneric = new Lazy<TypeDefinition>(this.GetGeneric);
this.Name = this.ReadString(image.Process.MonoLibraryOffsets.TypeDefinitionName);
this.NamespaceName = this.ReadString(image.Process.MonoLibraryOffsets.TypeDefinitionNamespace);
this.Size = this.ReadInt32(image.Process.MonoLibraryOffsets.TypeDefinitionSize);
var vtablePtr = this.ReadPtr(image.Process.MonoLibraryOffsets.TypeDefinitionRuntimeInfo);
this.VTable = vtablePtr == IntPtr.Zero ? IntPtr.Zero : image.Process.ReadPtr(vtablePtr + image.Process.MonoLibraryOffsets.TypeDefinitionRuntimeInfoDomainVTables);
this.TypeInfo = new TypeInfo(image, this.Address + image.Process.MonoLibraryOffsets.TypeDefinitionByValArg);
this.VTableSize = vtablePtr == IntPtr.Zero ? 0 : this.ReadInt32(image.Process.MonoLibraryOffsets.TypeDefinitionVTableSize);
this.ClassKind = (MonoClassKind)(this.ReadByte(image.Process.MonoLibraryOffsets.TypeDefinitionClassKind) & 0x7);
// Get the generic type arguments
if (this.TypeInfo.TypeCode == TypeCode.GENERICINST)
{
this.fieldCount = this.ReadInt32(96);
var monoGenericClassAddress = this.TypeInfo.Data;
var monoClassAddress = this.Process.ReadPtr(monoGenericClassAddress);
this.Image.GetTypeDefinition(monoClassAddress);
var monoGenericContainerPtr = monoClassAddress + this.Process.MonoLibraryOffsets.TypeDefinitionGenericContainer;
var monoGenericContainerAddress = this.Process.ReadPtr(monoGenericContainerPtr);
var monoGenericContextPtr = monoGenericClassAddress + this.Process.SizeOfPtr;
var monoGenericInsPtr = this.Process.ReadPtr(monoGenericContextPtr);
// var argumentCount = this.Process.ReadInt32(monoGenericInsPtr + 0x4);
var argumentCount = this.Process.ReadInt32(monoGenericContainerAddress + (4 * this.Process.SizeOfPtr));
var typeArgVPtr = monoGenericInsPtr + 0x8;
this.genericTypeArguments = new List<TypeInfo>(argumentCount);
for (int i = 0; i < argumentCount; i++)
{
var genericTypeArgumentPtr = this.Process.ReadPtr(typeArgVPtr + (i * this.Process.SizeOfPtr));
this.genericTypeArguments.Add(new TypeInfo(this.Image, monoGenericInsPtr));
}
}
else
{
this.genericTypeArguments = null;
}
}
IReadOnlyList<IFieldDefinition> ITypeDefinition.Fields => this.Fields;
public string FullName => this.lazyFullName.Value;
public bool IsEnum => (this.bitFields & 0x8) == 0x8;
public bool IsValueType => (this.bitFields & 0x4) == 0x4;
public string Name { get; }
public string NamespaceName { get; }
ITypeInfo ITypeDefinition.TypeInfo => this.TypeInfo;
public IReadOnlyList<FieldDefinition> Fields => this.lazyFields.Value;
public TypeDefinition NestedIn => this.lazyNestedIn.Value;
public TypeDefinition Parent => this.lazyParent.Value;
public int Size { get; }
public TypeInfo TypeInfo { get; }
public IntPtr VTable { get; }
public int VTableSize { get; }
public MonoClassKind ClassKind { get; }
public List<TypeInfo> GenericTypeArguments
{
get
{
if (this.genericTypeArguments == null && this.NestedIn != null)
{
return this.NestedIn.GenericTypeArguments;
}
else
{
return this.genericTypeArguments;
}
}
}
public dynamic this[string fieldName] => this.GetStaticValue<dynamic>(fieldName);
IFieldDefinition ITypeDefinition.GetField(string fieldName, string typeFullName) =>
this.GetField(fieldName, typeFullName);
public TValue GetStaticValue<TValue>(string fieldName)
{
var field = this.GetField(fieldName, this.FullName)
?? throw new ArgumentException($"Field '{fieldName}' does not exist in class '{this.FullName}'.", nameof(fieldName));
if (!field.TypeInfo.IsStatic)
{
throw new InvalidOperationException($"Field '{fieldName}' is not static in class '{this.FullName}'.");
}
if (field.TypeInfo.IsConstant)
{
throw new InvalidOperationException($"Field '{fieldName}' is constant in class '{this.FullName}'.");
}
try
{
var vTableMemorySize = this.Process.SizeOfPtr * this.VTableSize;
var valuePtr = this.Process.ReadPtr(this.VTable + /*this.Process.MonoLibraryOffsets.VTable*/72 + vTableMemorySize);
return field.GetValue<TValue>(valuePtr);
}
catch (Exception e)
{
throw new Exception($"Exception received when trying to get static value for field '{fieldName}' in class '{this.FullName}': ${e.Message}.", e);
}
}
public FieldDefinition GetField(string fieldName, string typeFullName = default) =>
this.fieldCache.GetOrAdd(
(typeFullName, fieldName),
k => this.Fields
.FirstOrDefault(
f => (f.Name == k.name) && ((k.@class == default) || (k.@class == f.DeclaringType.FullName))));
public void Init()
{
this.NestedIn?.Init();
this.Parent?.Init();
}
private TypeDefinition GetClassDefinition(int address) =>
this.Image.GetTypeDefinition(this.ReadPtr(address));
private IReadOnlyList<FieldDefinition> GetFields()
{
var firstField = this.ReadPtr(this.Image.Process.MonoLibraryOffsets.TypeDefinitionFields);
if (firstField == IntPtr.Zero)
{
return this.Parent?.Fields ?? new List<FieldDefinition>();
}
var fields = new List<FieldDefinition>();
if (this.ClassKind is MonoClassKind.GInst or MonoClassKind.GParam)
{
fields.AddRange(this.GetGeneric().GetFields());
}
else
{
for (var fieldIndex = 0; fieldIndex < this.fieldCount; fieldIndex++)
{
var field = firstField + (fieldIndex * this.Process.MonoLibraryOffsets.TypeDefinitionFieldSize);
if (this.Process.ReadPtr(field) == IntPtr.Zero)
{
break;
}
fields.Add(new FieldDefinition(this, field));
}
}
fields.AddRange(this.Parent?.Fields ?? new List<FieldDefinition>());
return new ReadOnlyCollection<FieldDefinition>(fields.OrderBy(f => f.Name).ToArray());
}
private string GetFullName()
{
var builder = new StringBuilder();
var hierarchy = this.NestedHierarchy().Reverse().ToArray();
if (!string.IsNullOrWhiteSpace(this.NamespaceName))
{
builder.Append($"{hierarchy[0].NamespaceName}.");
}
foreach (var definition in hierarchy)
{
builder.Append($"{definition.Name}+");
}
return builder.ToString().TrimEnd('+');
}
private IEnumerable<TypeDefinition> NestedHierarchy()
{
yield return this;
var nested = this.NestedIn;
while (nested != default)
{
yield return nested;
nested = nested.NestedIn;
}
}
private TypeDefinition GetGeneric()
{
if (this.ClassKind is MonoClassKind.GInst or MonoClassKind.GParam)
{
var genericContainerPtr = this.ReadPtr(this.Process.MonoLibraryOffsets.TypeDefinitionMonoGenericClass);
return this.Image.GetTypeDefinition(this.Process.ReadPtr(genericContainerPtr));
}
else
{
return null;
}
}
}
}