forked from SamboyCoding/Cpp2IL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetadataUsage.cs
206 lines (175 loc) · 6.74 KB
/
MetadataUsage.cs
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
using System;
using LibCpp2IL.BinaryStructures;
using LibCpp2IL.Metadata;
using LibCpp2IL.Reflection;
namespace LibCpp2IL
{
public class MetadataUsage
{
public readonly MetadataUsageType Type;
public readonly ulong Offset;
private readonly uint _value;
private string? _cachedName;
private Il2CppType? _cachedType;
private Il2CppTypeReflectionData? _cachedTypeReflectionData;
private Il2CppMethodDefinition? _cachedMethod;
private Il2CppFieldDefinition? _cachedField;
private string? _cachedLiteral;
private Il2CppGenericMethodRef? _cachedGenericMethod;
public MetadataUsage(MetadataUsageType type, ulong offset, uint value)
{
Type = type;
_value = value;
Offset = offset;
}
public uint RawValue => _value;
public object Value
{
get
{
switch (Type)
{
case MetadataUsageType.Type:
case MetadataUsageType.TypeInfo:
return AsType();
case MetadataUsageType.MethodDef:
return AsMethod();
case MetadataUsageType.FieldInfo:
return AsField();
case MetadataUsageType.StringLiteral:
return AsLiteral();
case MetadataUsageType.MethodRef:
return AsGenericMethodRef();
default:
throw new ArgumentOutOfRangeException();
}
}
}
public Il2CppTypeReflectionData AsType()
{
if (_cachedTypeReflectionData == null)
{
switch (Type)
{
case MetadataUsageType.Type:
case MetadataUsageType.TypeInfo:
try
{
_cachedType = LibCpp2IlMain.Binary!.GetType((int) _value);
_cachedTypeReflectionData = LibCpp2ILUtils.GetTypeReflectionData(_cachedType)!;
_cachedName = LibCpp2ILUtils.GetTypeReflectionData(_cachedType)?.ToString();
}
catch (Exception e)
{
throw new Exception($"Failed to convert this metadata usage to a type, but it is of type {Type}, with a value of {_value} (0x{_value:X}). There are {LibCpp2IlMain.Binary!.NumTypes} types", e);
}
break;
default:
throw new Exception($"Cannot cast metadata usage of kind {Type} to a Type");
}
}
return _cachedTypeReflectionData!;
}
public Il2CppMethodDefinition AsMethod()
{
if (_cachedMethod == null)
{
switch (Type)
{
case MetadataUsageType.MethodDef:
_cachedMethod = LibCpp2IlMain.TheMetadata!.methodDefs[_value];
_cachedName = _cachedMethod.GlobalKey;
break;
default:
throw new Exception($"Cannot cast metadata usage of kind {Type} to a Method Def");
}
}
return _cachedMethod!;
}
public Il2CppFieldDefinition AsField()
{
if (_cachedField == null)
{
switch (Type)
{
case MetadataUsageType.FieldInfo:
var fieldRef = LibCpp2IlMain.TheMetadata!.fieldRefs[_value];
_cachedField = fieldRef.FieldDefinition;
_cachedName = fieldRef.DeclaringTypeDefinition!.FullName + "." + _cachedField!.Name;
break;
default:
throw new Exception($"Cannot cast metadata usage of kind {Type} to a Field");
}
}
return _cachedField;
}
public string AsLiteral()
{
if (_cachedLiteral == null)
{
switch (Type)
{
case MetadataUsageType.StringLiteral:
_cachedName = _cachedLiteral = LibCpp2IlMain.TheMetadata!.GetStringLiteralFromIndex(_value);
break;
default:
throw new Exception($"Cannot cast metadata usage of kind {Type} to a String Literal");
}
}
return _cachedLiteral;
}
public Il2CppGenericMethodRef AsGenericMethodRef()
{
if (_cachedGenericMethod == null)
{
switch (Type)
{
case MetadataUsageType.MethodRef:
var methodSpec = LibCpp2IlMain.Binary!.GetMethodSpec((int) _value);
_cachedGenericMethod = new Il2CppGenericMethodRef(methodSpec);
_cachedName = _cachedGenericMethod.ToString();
break;
default:
throw new Exception($"Cannot cast metadata usage of kind {Type} to a Generic Method Ref");
}
}
return _cachedGenericMethod;
}
public override string ToString()
{
return $"Metadata Usage {{type={Type}, Value={Value}}}";
}
public bool IsValid
{
get
{
try
{
var _ = Value;
return true;
}
catch (Exception)
{
return false;
}
}
}
public static MetadataUsage? DecodeMetadataUsage(ulong encoded, ulong address)
{
var encodedType = encoded & 0xE000_0000;
var type = (MetadataUsageType) (encodedType >> 29);
if (type <= MetadataUsageType.MethodRef && type >= MetadataUsageType.TypeInfo)
{
var index = (uint) (encoded & 0x1FFF_FFFF);
if (LibCpp2IlMain.MetadataVersion >= 27)
index >>= 1;
if (type is MetadataUsageType.Type or MetadataUsageType.TypeInfo && index > LibCpp2IlMain.Binary!.NumTypes)
return null;
if (type == MetadataUsageType.MethodDef && index > LibCpp2IlMain.TheMetadata!.methodDefs.Length)
return null;
return new MetadataUsage(type, address, index);
}
return null;
}
}
}