少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
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
#include "MethodBridge.h"
 
#include "vm/Object.h"
#include "vm/Class.h"
#include "metadata/GenericMetadata.h"
 
#include "../metadata/MetadataModule.h"
#include "../metadata/MetadataUtil.h"
 
#include "Interpreter.h"
#include "InterpreterModule.h"
#include "MemoryUtil.h"
 
namespace hybridclr
{
namespace interpreter
{
 
    void ConvertInvokeArgs(StackObject* resultArgs, const MethodInfo* method, MethodArgDesc* argDescs, void** args)
    {
        int32_t dstIdx = 0;
        for (uint8_t i = 0; i < method->parameters_count; i++)
        {
            StackObject* dst = resultArgs + dstIdx;
            MethodArgDesc& argDesc = argDescs[i];
            if (argDesc.passbyValWhenInvoke)
            {
                dst->ptr = args[i];
                ++dstIdx;
            }
            else
            {
#if SUPPORT_MEMORY_NOT_ALIGMENT_ACCESS
                CopyStackObject(dst, args[i], argDesc.stackObjectSize);
#else
                std::memcpy(dst, args[i], argDesc.stackObjectSize * sizeof(StackObject));
#endif
                dstIdx += argDesc.stackObjectSize;
            }
        }
    }
    
    static void AppendString(char* sigBuf, size_t bufSize, size_t& pos, const char* str)
    {
        size_t len = std::strlen(str);
        if (pos + len < bufSize)
        {
            std::strcpy(sigBuf + pos, str);
            pos += len;
        }
        else
        {
            RaiseExecutionEngineException("");
        }
    }
 
    inline void AppendSignatureObjOrRefOrPointer(char* sigBuf, size_t bufSize, size_t& pos)
    {
        AppendString(sigBuf, bufSize, pos, "u");
    }
 
    inline void AppendSignatureInterpreterValueType(char* sigBuf, size_t bufSize, size_t& pos)
    {
        AppendString(sigBuf, bufSize, pos, "$");
    }
 
    static void AppendSignature(const Il2CppType* type, char* sigBuf, size_t bufferSize, size_t& pos, bool convertTypeName2SigName = true);
 
    static bool IsSystemOrUnityAssembly(const Il2CppImage* image)
    {
        const char* assName = image->nameNoExt;
        if (std::strcmp(assName, "mscorlib") == 0)
        {
            return true;
        }
        if (std::strncmp(assName, "System.", 7) == 0)
        {
            return true;
        }
        if (std::strncmp(assName, "UnityEngine.", 12) == 0)
        {
            return true;
        }
        return false;
    }
 
    static void BuildValueTypeFullName(const Il2CppClass* klass, char* sigBuf, size_t bufferSize, size_t& pos)
    {
        if (klass->declaringType)
        {
            BuildValueTypeFullName(klass->declaringType, sigBuf, bufferSize, pos);
            AppendString(sigBuf, bufferSize, pos, "/");
            AppendString(sigBuf, bufferSize, pos, klass->name);
            return;
        }
        if (!IsSystemOrUnityAssembly(klass->image))
        {
            AppendString(sigBuf, bufferSize, pos, klass->image->nameNoExt);
            AppendString(sigBuf, bufferSize, pos, ":");
        }
        if (klass->namespaze[0])
        {
            AppendString(sigBuf, bufferSize, pos, klass->namespaze);
            AppendString(sigBuf, bufferSize, pos, ".");
        }
        AppendString(sigBuf, bufferSize, pos, klass->name);
    }
 
    static void BuildGenericValueTypeFullName(const Il2CppType* type, char* sigBuf, size_t bufferSize, size_t& pos)
    {
        const Il2CppType* underlyingGenericType = type->data.generic_class->type;
        const Il2CppClass* underlyingGenericClass = il2cpp::vm::Class::FromIl2CppType(underlyingGenericType);
        BuildValueTypeFullName(underlyingGenericClass, sigBuf, bufferSize, pos);
        AppendString(sigBuf, bufferSize, pos, "<");
        const Il2CppGenericInst* classInst = type->data.generic_class->context.class_inst;
        for (uint32_t i = 0 ; i < classInst->type_argc; ++i)
        {
            if (i != 0)
            {
                AppendString(sigBuf, bufferSize, pos, ",");
            }
            AppendSignature(classInst->type_argv[i], sigBuf, bufferSize, pos, false);
        }
        AppendString(sigBuf, bufferSize, pos, ">");
    }
 
    static void AppendSignature(const Il2CppType* type, char* sigBuf, size_t bufferSize, size_t& pos, bool convertTypeName2SigName)
    {
        if (type->byref)
        {
            AppendSignatureObjOrRefOrPointer(sigBuf, bufferSize, pos);
            return;
        }
        switch (type->type)
        {
        case IL2CPP_TYPE_VOID: AppendString(sigBuf, bufferSize, pos, "v"); break;
        case IL2CPP_TYPE_BOOLEAN: AppendString(sigBuf, bufferSize, pos, "u1"); break;
        case IL2CPP_TYPE_I1: AppendString(sigBuf, bufferSize, pos, "i1"); break;
        case IL2CPP_TYPE_U1: AppendString(sigBuf, bufferSize, pos, "u1"); break;
        case IL2CPP_TYPE_I2: AppendString(sigBuf, bufferSize, pos, "i2"); break;
        case IL2CPP_TYPE_U2:
        case IL2CPP_TYPE_CHAR: AppendString(sigBuf, bufferSize, pos, "u2"); break;
        case IL2CPP_TYPE_I4: AppendString(sigBuf, bufferSize, pos, "i4"); break;
        case IL2CPP_TYPE_U4: AppendString(sigBuf, bufferSize, pos, "u4"); break;
        case IL2CPP_TYPE_R4: AppendString(sigBuf, bufferSize, pos, "r4"); break;
        case IL2CPP_TYPE_R8: AppendString(sigBuf, bufferSize, pos, "r8"); break;
        case IL2CPP_TYPE_I8: AppendString(sigBuf, bufferSize, pos, "i8"); break;
        case IL2CPP_TYPE_U8: AppendString(sigBuf, bufferSize, pos, "u8"); break;
        case IL2CPP_TYPE_I: AppendString(sigBuf, bufferSize, pos, "i"); break;
        case IL2CPP_TYPE_U: AppendString(sigBuf, bufferSize, pos, "u"); break;
        case IL2CPP_TYPE_TYPEDBYREF:
        {
            IL2CPP_ASSERT(sizeof(Il2CppTypedRef) == sizeof(void*) * 3);
            AppendString(sigBuf, bufferSize, pos, "typedbyref");
            break;
        }
        case IL2CPP_TYPE_VALUETYPE:
        {
            const Il2CppTypeDefinition* typeDef = (const Il2CppTypeDefinition*)type->data.typeHandle;
            if (hybridclr::metadata::IsEnumType(typeDef))
            {
                AppendSignature(il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(typeDef->elementTypeIndex), sigBuf, bufferSize, pos);
                break;
            }
            if (hybridclr::metadata::IsInterpreterType(typeDef))
            {
                AppendSignatureInterpreterValueType(sigBuf, bufferSize, pos);
                break;
            }
            char tempFullName[1024];
            size_t fullNamePos = 0;
            Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(type);
            BuildValueTypeFullName(klass, tempFullName, sizeof(tempFullName) - 1, fullNamePos);
            tempFullName[fullNamePos] = 0;
            AppendString(sigBuf, bufferSize, pos, convertTypeName2SigName ? InterpreterModule::GetValueTypeSignature(tempFullName) : tempFullName);
            break;
        }
        case IL2CPP_TYPE_GENERICINST:
        {
            const Il2CppType* underlyingGenericType = type->data.generic_class->type;
            if (underlyingGenericType->type == IL2CPP_TYPE_CLASS)
            {
                AppendSignatureObjOrRefOrPointer(sigBuf, bufferSize, pos);
                break;
            }
            const Il2CppTypeDefinition* underlyingTypeDef = (const Il2CppTypeDefinition*)underlyingGenericType->data.typeHandle;
            if (hybridclr::metadata::IsEnumType(underlyingTypeDef))
            {
                AppendSignature(il2cpp::vm::GlobalMetadata::GetIl2CppTypeFromIndex(underlyingTypeDef->elementTypeIndex), sigBuf, bufferSize, pos);
                break;
            }
            IL2CPP_ASSERT(underlyingGenericType->type == IL2CPP_TYPE_VALUETYPE);
            if (hybridclr::metadata::IsInterpreterType(underlyingTypeDef))
            {
                AppendSignatureInterpreterValueType(sigBuf, bufferSize, pos);
                break;
            }
            
            char tempFullName[1024];
            size_t fullNamePos = 0;
            BuildGenericValueTypeFullName(type, tempFullName, sizeof(tempFullName) - 1, fullNamePos);
            tempFullName[fullNamePos] = 0;
            AppendString(sigBuf, bufferSize, pos, convertTypeName2SigName ? InterpreterModule::GetValueTypeSignature(tempFullName) : tempFullName);
            break;
        }
        case IL2CPP_TYPE_VAR:
        case IL2CPP_TYPE_MVAR:
        {
            AppendString(sigBuf, bufferSize, pos, "!");
            break;
        }
        default: AppendSignatureObjOrRefOrPointer(sigBuf, bufferSize, pos); break;
        }
    }
 
    bool ComputeSignature(const Il2CppType* ret, const Il2CppType* params, uint32_t paramCount, bool instanceCall, char* sigBuf, size_t bufferSize)
    {
        size_t pos = 0;
        AppendSignature(ret, sigBuf, bufferSize, pos);
 
        if (instanceCall)
        {
            AppendSignatureObjOrRefOrPointer(sigBuf, bufferSize, pos);
        }
 
        for (uint32_t i = 0; i < paramCount; i++)
        {
            AppendSignature(params + i, sigBuf, bufferSize, pos);
        }
        sigBuf[pos] = 0;
        return true;
    }
 
    bool ComputeSignature(const Il2CppMethodDefinition* method, bool call, char* sigBuf, size_t bufferSize)
    {
        size_t pos = 0;
        if (method->genericContainerIndex != kGenericContainerIndexInvalid)
        {
            AppendString(sigBuf, bufferSize, pos, "!");
            return true;
        }
 
        const Il2CppImage* image = hybridclr::metadata::MetadataModule::GetImage(method)->GetIl2CppImage();
 
        AppendSignature(hybridclr::metadata::MetadataModule::GetIl2CppTypeFromEncodeIndex(method->returnType), sigBuf, bufferSize, pos);
 
        if (call && metadata::IsInstanceMethod(method))
        {
            AppendSignatureObjOrRefOrPointer(sigBuf, bufferSize, pos);
        }
 
        for (uint8_t i = 0; i < method->parameterCount; i++)
        {
            TypeIndex paramTypeIndex = hybridclr::metadata::MetadataModule::GetParameterDefinitionFromIndex(image, method->parameterStart + i)->typeIndex;
            AppendSignature(hybridclr::metadata::MetadataModule::GetIl2CppTypeFromEncodeIndex(paramTypeIndex), sigBuf, bufferSize, pos);
        }
        sigBuf[pos] = 0;
        return true;
    }
 
    inline bool ContainsGenericParameters(const MethodInfo* method)
    {
        IL2CPP_ASSERT(method->is_inflated);
        auto& ctx = method->genericMethod->context;
        if (ctx.class_inst && il2cpp::metadata::GenericMetadata::ContainsGenericParameters(ctx.class_inst))
        {
            return true;
        }
        if (ctx.method_inst && il2cpp::metadata::GenericMetadata::ContainsGenericParameters(ctx.method_inst))
        {
            return true;
        }
        return false;
    }
 
    bool ComputeSignature(const MethodInfo* method, bool call, char* sigBuf, size_t bufferSize)
    {
        size_t pos = 0;
        if (method->is_generic || (method->is_inflated && ContainsGenericParameters(method)))
        {
            AppendString(sigBuf, bufferSize, pos, "!");
            return true;
        }
 
        AppendSignature(method->return_type, sigBuf, bufferSize, pos);
 
        if (call && metadata::IsInstanceMethod(method))
        {
            AppendSignatureObjOrRefOrPointer(sigBuf, bufferSize, pos);
        }
 
        for (uint8_t i = 0; i < method->parameters_count; i++)
        {
            AppendSignature(GET_METHOD_PARAMETER_TYPE(method->parameters[i]), sigBuf, bufferSize, pos);
        }
        sigBuf[pos] = 0;
        return true;
    }
 
}
}