少年修仙传客户端基础资源
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
#include "ConsistentAOTHomologousImage.h"
 
#include "vm/MetadataLock.h"
#include "vm/GlobalMetadata.h"
#include "vm/Class.h"
#include "vm/Image.h"
#include "vm/Exception.h"
#include "vm/MetadataCache.h"
#include "metadata/GenericMetadata.h"
 
namespace hybridclr
{
namespace metadata
{
 
    void ConsistentAOTHomologousImage::InitRuntimeMetadatas()
    {
        InitTypes();
        InitMethods();
        InitFields();
    }
 
    void ConsistentAOTHomologousImage::InitTypes()
    {
        const Table& typeDefTb = _rawImage.GetTable(TableType::TYPEDEF);
        uint32_t typeCount = typeDefTb.rowNum;
        _il2cppTypeForTypeDefs.resize(typeCount);
        _typeDefs.resize(typeCount);
 
        Il2CppImage* image = _aotAssembly->image;
        //if (image->typeCount != typeCount)
        //{
        //    RaiseExecutionEngineException("image metadata not match");
        //}
        for (uint32_t index = 0; index < image->typeCount; index++)
        {
            Il2CppTypeDefinition* typeDef = (Il2CppTypeDefinition*)il2cpp::vm::MetadataCache::GetAssemblyTypeHandle(image, index);
            uint32_t rowIndex = DecodeTokenRowIndex(typeDef->token);
            IL2CPP_ASSERT(rowIndex > 0);
            if (rowIndex > typeCount)
            {
                continue;
            }
            TbTypeDef data = _rawImage.ReadTypeDef(rowIndex);
            uint32_t typeIndex = rowIndex - 1;
            _typeDefs[typeIndex] = typeDef;
            const Il2CppType* il2cppType = il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(typeDef->byvalTypeIndex);
            _il2cppTypeForTypeDefs[typeIndex] = il2cppType;
 
            const char* name1 = _rawImage.GetStringFromRawIndex(data.typeName);
            const char* name2 = il2cpp::vm::GlobalMetadata::GetStringFromIndex(typeDef->nameIndex);
            if (std::strcmp(name1, name2))
            {
                RaiseExecutionEngineException("metadata type not match");
            }
            const char* namespaze1 = _rawImage.GetStringFromRawIndex(data.typeNamespace);
            const char* namespaze2 = il2cpp::vm::GlobalMetadata::GetStringFromIndex(typeDef->namespaceIndex);
            if (std::strcmp(namespaze1, namespaze2))
            {
                RaiseExecutionEngineException("metadata type not match");
            }
        }
    }
 
    void ConsistentAOTHomologousImage::InitMethods()
    {
        const Table& methodTb = _rawImage.GetTable(TableType::METHOD);
        _methodDefs.resize(methodTb.rowNum);
 
        for (Il2CppTypeDefinition* type : _typeDefs)
        {
            for (uint16_t i = 0; i < type->method_count; i++)
            {
                const Il2CppMethodDefinition* methodDef = il2cpp::vm::GlobalMetadata::GetMethodDefinitionFromIndex(type->methodStart + i);
                uint32_t rowIndex = DecodeTokenRowIndex(methodDef->token);
                IL2CPP_ASSERT(rowIndex > 0 && rowIndex <= methodTb.rowNum);
                uint32_t methodIndex = rowIndex - 1;
                IL2CPP_ASSERT(_methodDefs[methodIndex] == nullptr);
                _methodDefs[methodIndex] = methodDef;
 
                TbMethod methodData = _rawImage.ReadMethod(rowIndex);
                const char* name1 = _rawImage.GetStringFromRawIndex(methodData.name);
                const char* name2 = il2cpp::vm::GlobalMetadata::GetStringFromIndex(methodDef->nameIndex);
                if (std::strcmp(name1, name2))
                {
                    RaiseExecutionEngineException("metadata method not match");
                }
            }
        }
    }
 
    void ConsistentAOTHomologousImage::InitFields()
    {
        const Table& fieldTb = _rawImage.GetTable(TableType::FIELD);
        _fields.resize(fieldTb.rowNum);
 
        for (size_t i = 0; i < _typeDefs.size(); i++)
        {
            Il2CppTypeDefinition* type = _typeDefs[i];
            for (uint16_t j = 0; j < type->field_count; j++)
            {
                const Il2CppFieldDefinition* fieldDef = il2cpp::vm::GlobalMetadata::GetFieldDefinitionFromTypeDefAndFieldIndex(type, j);
                uint32_t rowIndex = DecodeTokenRowIndex(fieldDef->token);
                IL2CPP_ASSERT(rowIndex > 0);
                uint32_t fieldIndex = rowIndex - 1;
                IL2CPP_ASSERT(_fields[fieldIndex].fieldDef == nullptr);
                if (rowIndex >= fieldTb.rowNum)
                {
                    continue;
                }
                _fields[fieldIndex] = { (uint32_t)i, fieldDef };
 
                TbField fieldData = _rawImage.ReadField(rowIndex);
                const char* name1 = _rawImage.GetStringFromRawIndex(fieldData.name);
                const char* name2 = il2cpp::vm::GlobalMetadata::GetStringFromIndex(fieldDef->nameIndex);
                if (std::strcmp(name1, name2))
                {
                    RaiseExecutionEngineException("metadata field not match");
                }
            }
        }
    }
 
    MethodBody* ConsistentAOTHomologousImage::GetMethodBody(uint32_t token)
    {
        auto it = _token2MethodBodies.find(token);
        if (it != _token2MethodBodies.end())
        {
            return it->second;
        }
        uint32_t rowIndex = DecodeTokenRowIndex(token);
        IL2CPP_ASSERT(rowIndex > 0);
        TbMethod methodData = _rawImage.ReadMethod(rowIndex);
        MethodBody* body = new (HYBRIDCLR_MALLOC_ZERO(sizeof(MethodBody))) MethodBody();
        ReadMethodBody(*_methodDefs[rowIndex - 1], methodData, *body);
        _token2MethodBodies.insert({ token, body });
        return body;
    }
 
    const Il2CppType* ConsistentAOTHomologousImage::GetIl2CppTypeFromRawTypeDefIndex(uint32_t index)
    {
        IL2CPP_ASSERT((size_t)index < _il2cppTypeForTypeDefs.size());
        return _il2cppTypeForTypeDefs[index];
    }
 
    Il2CppGenericContainer* ConsistentAOTHomologousImage::GetGenericContainerByRawIndex(uint32_t index)
    {
        return (Il2CppGenericContainer*)il2cpp::vm::GlobalMetadata::GetGenericContainerFromIndex(index);
    }
 
    Il2CppGenericContainer* ConsistentAOTHomologousImage::GetGenericContainerByTypeDefRawIndex(int32_t typeDefIndex)
    {
        Il2CppTypeDefinition* type = (Il2CppTypeDefinition*)il2cpp::vm::GlobalMetadata::GetTypeHandleFromIndex(typeDefIndex);
        return (Il2CppGenericContainer*)il2cpp::vm::GlobalMetadata::GetGenericContainerFromIndex(type->genericContainerIndex);
    }
 
    const Il2CppMethodDefinition* ConsistentAOTHomologousImage::GetMethodDefinitionFromRawIndex(uint32_t index)
    {
        IL2CPP_ASSERT((size_t)index < _methodDefs.size());
        return _methodDefs[index];
    }
 
    void ConsistentAOTHomologousImage::ReadFieldRefInfoFromFieldDefToken(uint32_t rowIndex, FieldRefInfo& ret)
    {
        IL2CPP_ASSERT(rowIndex > 0);
        AOTFieldData& fd = _fields[rowIndex - 1];
        ret.containerType = *_il2cppTypeForTypeDefs[fd.typeDefIndex];
        ret.field = fd.fieldDef;
    }
}
}