-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathMetadataUsage.cs
181 lines (149 loc) · 5.97 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
using System;
using LibCpp2IL.BinaryStructures;
using LibCpp2IL.Metadata;
using LibCpp2IL.Reflection;
namespace LibCpp2IL;
public class MetadataUsage(MetadataUsageType type, ulong offset, uint value)
{
public readonly MetadataUsageType Type = type;
public readonly ulong Offset = offset;
private string? _cachedName;
private Il2CppType? _cachedType;
private Il2CppTypeReflectionData? _cachedTypeReflectionData;
private Il2CppMethodDefinition? _cachedMethod;
private Il2CppFieldDefinition? _cachedField;
private string? _cachedLiteral;
private Cpp2IlMethodRef? _cachedGenericMethod;
public uint RawValue => value;
public object Value =>
Type switch
{
MetadataUsageType.Type or MetadataUsageType.TypeInfo => AsType(),
MetadataUsageType.MethodDef => AsMethod(),
MetadataUsageType.FieldInfo => AsField(),
MetadataUsageType.StringLiteral => AsLiteral(),
MetadataUsageType.MethodRef => AsGenericMethodRef(),
_ => throw new ArgumentOutOfRangeException()
};
public bool IsValid =>
Type switch
{
MetadataUsageType.Type or MetadataUsageType.TypeInfo => value < LibCpp2IlMain.Binary!.NumTypes,
MetadataUsageType.MethodDef => value < LibCpp2IlMain.TheMetadata!.methodDefs.Length,
MetadataUsageType.FieldInfo => value < LibCpp2IlMain.TheMetadata!.fieldRefs.Length,
MetadataUsageType.StringLiteral => value < LibCpp2IlMain.TheMetadata!.stringLiterals.Length,
MetadataUsageType.MethodRef => value < LibCpp2IlMain.Binary!.AllGenericMethodSpecs.Length,
_ => false
};
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 Cpp2IlMethodRef AsGenericMethodRef()
{
if (_cachedGenericMethod == null)
{
switch (Type)
{
case MetadataUsageType.MethodRef:
var methodSpec = LibCpp2IlMain.Binary!.GetMethodSpec((int)value);
_cachedGenericMethod = new Cpp2IlMethodRef(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 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;
}
}