少年修仙传客户端基础资源
hch
2024-04-11 4c71d74b77c9eb62a0323698c9a0db3b641a917e
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#pragma once
#include "MetadataDef.h"
#include "BlobReader.h"
#include "MetadataUtil.h"
 
namespace hybridclr
{
namespace metadata
{
    enum class LoadImageErrorCode
    {
        OK = 0,
        BAD_IMAGE,
        NOT_IMPLEMENT,
        AOT_ASSEMBLY_NOT_FIND,
        HOMOLOGOUS_ONLY_SUPPORT_AOT_ASSEMBLY,
        HOMOLOGOUS_ASSEMBLY_HAS_BEEN_LOADED,
        INVALID_HOMOLOGOUS_MODE,
    };
 
    class RawImage
    {
    public:
        RawImage(): _ptrRawData(nullptr), _imageLength(0), _ptrRawDataEnd(nullptr),
            _isDll(false), _PEHeader(nullptr), _PESectionHeaders(nullptr), _ptrMetaData(nullptr), _ptrMetaRoot(nullptr),
            _streamStringHeap{}, _streamUS{}, _streamBlobHeap{}, _streamGuidHeap{}, _streamTables{},
            _stringHeapStrNum(0), _userStringStrNum(0), _blobNum(0),
            _4byteStringIndex(false), _4byteGUIDIndex(false), _4byteBlobIndex(false)
        {
 
        }
 
 
        LoadImageErrorCode Load(const void* imageData, size_t length);
 
        uint32_t GetTypeCount() const
        {
            return _tables[(int)TableType::TYPEDEF].rowNum;
        }
 
        uint32_t GetExportedTypeCount() const
        {
            return _tables[(int)TableType::EXPORTEDTYPE].rowNum;
        }
 
        const char* GetStringFromRawIndex(StringIndex index) const
        {
            IL2CPP_ASSERT(DecodeImageIndex(index) == 0);
            IL2CPP_ASSERT(index >= 0 && (uint32_t)index < _streamStringHeap.size);
            return (const char*)(_streamStringHeap.data + index);
        }
 
        const byte* GetBlobFromRawIndex(StringIndex index) const
        {
            IL2CPP_ASSERT(DecodeImageIndex(index) == 0);
            IL2CPP_ASSERT(index == 0 || (index > 0 && (size_t)index < _streamBlobHeap.size));
            return _streamBlobHeap.data + index;
        }
 
        const uint8_t* GetFieldOrParameterDefalutValueByRawIndex(uint32_t index) const
        {
            return _ptrRawData + index;
        }
 
        static BlobReader DecodeBlob(const byte* buf)
        {
            uint32_t sizeLength;
            uint32_t length = BlobReader::ReadCompressedUint32(buf, sizeLength);
            return BlobReader(buf + sizeLength, length);
        }
 
        BlobReader GetBlobReaderByRawIndex(uint32_t rawIndex) const
        {
            IL2CPP_ASSERT(DecodeImageIndex(rawIndex) == 0);
            const byte* buf = _streamBlobHeap.data + rawIndex;
            return DecodeBlob(buf);
        }
 
        uint32_t GetImageOffsetOfBlob(Il2CppTypeEnum type, uint32_t index) const
        {
            if (type != IL2CPP_TYPE_STRING)
            {
                return (uint32_t)(GetBlobReaderByRawIndex(index).GetData() - _ptrRawData);
            }
            else
            {
                return (uint32_t)(_streamBlobHeap.data + index - _ptrRawData);
            }
        }
 
        const byte* GetDataPtrByImageOffset(uint32_t imageOffset) const
        {
            IL2CPP_ASSERT(imageOffset < _imageLength);
            return _ptrRawData + imageOffset;
        }
 
        const byte* GetTableRowPtr(TableType type, uint32_t rawIndex) const
        {
            auto& tb = _tables[(int)type];
            IL2CPP_ASSERT(rawIndex > 0 && rawIndex <= tb.rowNum);
            return tb.data + tb.rowMetaDataSize * (rawIndex - 1);
        }
 
        const Table& GetTable(TableType type) const
        {
            return _tables[(int)type];
        }
 
        const std::vector<ColumnOffsetSize>& GetRowSchema(TableType type) const
        {
            return _tableRowMetas[(int)type];
        }
 
        bool TranslateRVAToImageOffset(uint32_t rvaOffset, uint32_t& imageOffset);
 
        LoadImageErrorCode LoadStreams();
 
        LoadImageErrorCode LoadTables();
 
        void BuildTableRowMetas();
 
        uint32_t ComputTableRowMetaDataSize(TableType tableIndex) const;
 
        uint32_t ComputStringIndexByte() const
        {
            return _4byteStringIndex ? 4 : 2;
        }
 
        uint32_t ComputGUIDIndexByte() const
        {
            return _4byteGUIDIndex ? 4 : 2;
        }
 
        uint32_t ComputBlobIndexByte() const
        {
            return _4byteBlobIndex ? 4 : 2;
        }
 
        uint32_t ComputTableIndexByte(TableType tableIndex) const
        {
            return _tables[(int)tableIndex].rowNum < 65536 ? 2 : 4;
        }
 
        uint32_t ComputIndexByte(uint32_t maxRowNum, uint32_t tagBitNum) const
        {
            return (maxRowNum << tagBitNum) < 65536 ? 2 : 4;
        }
 
        uint32_t GetTableRowNum(TableType tableIndex) const
        {
            return _tables[(int)tableIndex].rowNum;
        }
 
        uint32_t ComputTableIndexByte(TableType t1, TableType t2, uint32_t tagBitNum) const
        {
            uint32_t n = GetTableRowNum(t1);
            n = std::max(n, GetTableRowNum(t2));
            return ComputIndexByte(n, tagBitNum);
        }
 
        uint32_t ComputTableIndexByte(TableType t1, TableType t2, TableType t3, uint32_t tagBitNum) const
        {
            uint32_t n = GetTableRowNum(t1);
            n = std::max(n, GetTableRowNum(t2));
            n = std::max(n, GetTableRowNum(t3));
            return ComputIndexByte(n, tagBitNum);
        }
 
        uint32_t ComputTableIndexByte(TableType t1, TableType t2, TableType t3, TableType t4, uint32_t tagBitNum) const
        {
            uint32_t n = GetTableRowNum(t1);
            n = std::max(n, GetTableRowNum(t2));
            n = std::max(n, GetTableRowNum(t3));
            n = std::max(n, GetTableRowNum(t4));
            return ComputIndexByte(n, tagBitNum);
        }
 
        uint32_t ComputTableIndexByte(TableType t1, TableType t2, TableType t3, TableType t4, TableType t5, uint32_t tagBitNum) const
        {
            uint32_t n = GetTableRowNum(t1);
            n = std::max(n, GetTableRowNum(t2));
            n = std::max(n, GetTableRowNum(t3));
            n = std::max(n, GetTableRowNum(t4));
            n = std::max(n, GetTableRowNum(t5));
            return ComputIndexByte(n, tagBitNum);
        }
 
        uint32_t ComputTableIndexByte(const TableType* ts, int num, uint32_t tagBitNum) const
        {
            uint32_t n = 0;
            for (int i = 0; i < num; i++)
            {
                n = std::max(n, GetTableRowNum(ts[i]));
            }
            return ComputIndexByte(n, tagBitNum);
        }
 
        uint32_t GetEntryPointToken() const
        {
            return _CLIHeader->entryPointToken;
        }
 
        const byte* GetUserStringBlogByIndex(uint32_t index) const
        {
            IL2CPP_ASSERT(index >= 0 && (uint32_t)index < _streamUS.size);
            return _streamUS.data + index;
        }
private:
    uint32_t ReadColumn(const byte* rowPtr, const ColumnOffsetSize& columnMt) const
    {
        return ReadColumn(rowPtr, columnMt.offset, columnMt.size);
    }
 
    uint32_t ReadColumn(const byte* data, uint32_t offset, uint32_t size) const
    {
        IL2CPP_ASSERT(size == 2 || size == 4);
        return (size == 2 ? *(uint16_t*)(data + offset) : *(uint32_t*)(data + offset));
    }
 
    public:
 
 
#define TABLE_BEGIN(name, tableType) Tb##name Read##name(uint32_t rawIndex) const \
        { \
        IL2CPP_ASSERT(rawIndex > 0 && rawIndex <= GetTable(tableType).rowNum); \
        const byte* rowPtr = GetTableRowPtr(tableType, rawIndex); \
        auto& rowSchema = GetRowSchema(tableType); \
        uint32_t __fieldIndex = 0; \
        Tb##name __r = {};
 
#define __F(fieldName) const ColumnOffsetSize& col_##fieldName = rowSchema[__fieldIndex++]; \
        __r.fieldName = ReadColumn(rowPtr, col_##fieldName);
 
#define TABLE_END return __r; \
        }
 
#define TABLE1(name, tableType, f1) TABLE_BEGIN(name, tableType) \
__F(f1) \
TABLE_END
 
#define TABLE2(name, tableType, f1, f2) TABLE_BEGIN(name, tableType) \
__F(f1) \
__F(f2) \
TABLE_END
 
#define TABLE3(name, tableType, f1, f2, f3) TABLE_BEGIN(name, tableType) \
__F(f1) \
__F(f2) \
__F(f3) \
TABLE_END
 
#define TABLE4(name, tableType, f1, f2, f3, f4) TABLE_BEGIN(name, tableType) \
__F(f1) \
__F(f2) \
__F(f3) \
__F(f4) \
TABLE_END
 
#define TABLE5(name, tableType, f1, f2, f3, f4, f5) TABLE_BEGIN(name, tableType) \
__F(f1) \
__F(f2) \
__F(f3) \
__F(f4) \
__F(f5) \
TABLE_END
 
#define TABLE6(name, tableType, f1, f2, f3, f4, f5, f6) TABLE_BEGIN(name, tableType) \
__F(f1) \
__F(f2) \
__F(f3) \
__F(f4) \
__F(f5) \
__F(f6) \
TABLE_END
 
        TABLE5(Module, TableType::MODULE, generation, name, mvid, encid, encBaseId);
        TABLE3(TypeRef, TableType::TYPEREF, resolutionScope, typeName, typeNamespace)
            TABLE6(TypeDef, TableType::TYPEDEF, flags, typeName, typeNamespace, extends, fieldList, methodList)
            TABLE1(TypeSpec, TableType::TYPESPEC, signature);
        TABLE3(Field, TableType::FIELD, flags, name, signature)
            TABLE4(GenericParam, TableType::GENERICPARAM, number, flags, owner, name)
            TABLE2(GenericParamConstraint, TableType::GENERICPARAMCONSTRAINT, owner, constraint)
            TABLE3(MemberRef, TableType::MEMBERREF, classIdx, name, signature)
            TABLE1(StandAloneSig, TableType::STANDALONESIG, signature)
            TABLE3(MethodImpl, TableType::METHODIMPL, classIdx, methodBody, methodDeclaration)
            TABLE2(FieldRVA, TableType::FIELDRVA, rva, field)
            TABLE2(FieldLayout, TableType::FIELDLAYOUT, offset, field)
            TABLE3(Constant, TableType::CONSTANT, type, parent, value)
            TABLE2(MethodSpec, TableType::METHODSPEC, method, instantiation)
            TABLE3(CustomAttribute, TableType::CUSTOMATTRIBUTE, parent, type, value)
            TABLE2(PropertyMap, TableType::PROPERTYMAP, parent, propertyList)
            TABLE3(Property, TableType::PROPERTY, flags, name, type)
            TABLE2(EventMap, TableType::EVENTMAP, parent, eventList)
            TABLE3(Event, TableType::EVENT, eventFlags, name, eventType)
            TABLE3(MethodSemantics, TableType::METHODSEMANTICS, semantics, method, association)
 
            TABLE2(NestedClass, TableType::NESTEDCLASS, nestedClass, enclosingClass)
            TABLE6(Method, TableType::METHOD, rva, implFlags, flags, name, signature, paramList)
            TABLE3(Param, TableType::PARAM, flags, sequence, name)
 
            TABLE3(ClassLayout, TableType::CLASSLAYOUT, packingSize, classSize, parent)
            TABLE2(InterfaceImpl, TableType::INTERFACEIMPL, classIdx, interfaceIdx)
 
            TABLE_BEGIN(Assembly, TableType::ASSEMBLY)
            __F(hashAlgId)
            __F(majorVersion)
            __F(minorVersion)
            __F(buildNumber)
            __F(revisionNumber)
            __F(flags)
            __F(publicKey)
            __F(name)
            __F(culture)
            TABLE_END
 
 
            TABLE_BEGIN(AssemblyRef, TableType::ASSEMBLYREF)
            __F(majorVersion)
            __F(minorVersion)
            __F(buildNumber)
            __F(revisionNumber)
            __F(flags)
            __F(publicKeyOrToken)
            __F(name)
            __F(culture)
            __F(hashValue)
            TABLE_END
 
    private:
        const byte* _ptrRawData;
        uint32_t _imageLength;
        const byte* _ptrRawDataEnd;
 
        bool _isDll;
        const PEHeader* _PEHeader;
        const CLIHeader* _CLIHeader;
        const PESectionHeader* _PESectionHeaders;
        const MetadataRootPartial* _ptrMetaRoot;
 
        const byte* _ptrMetaData;
 
        CliStream _streamStringHeap;
        CliStream _streamUS;
        CliStream _streamBlobHeap;
        CliStream _streamGuidHeap;
        CliStream _streamTables;
 
        uint32_t _stringHeapStrNum;
 
        uint32_t _userStringStrNum;
        uint32_t _blobNum;
 
 
        bool _4byteStringIndex;
        bool _4byteGUIDIndex;
        bool _4byteBlobIndex;
 
        Table _tables[TABLE_NUM];
        std::vector<ColumnOffsetSize> _tableRowMetas[TABLE_NUM];
    };
}
}