少年修仙传客户端基础资源
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
#include "MetadataModule.h"
 
#include "Baselib.h"
#include "os/Atomic.h"
#include "os/Mutex.h"
#include "os/File.h"
#include "vm/Exception.h"
#include "vm/String.h"
#include "vm/Assembly.h"
#include "vm/Class.h"
#include "vm/Object.h"
#include "vm/Image.h"
#include "vm/MetadataLock.h"
#include "utils/Logging.h"
#include "utils/MemoryMappedFile.h"
#include "utils/Memory.h"
 
#include "../interpreter/InterpreterModule.h"
 
#include "Assembly.h"
#include "InterpreterImage.h"
#include "ConsistentAOTHomologousImage.h"
#include "SuperSetAOTHomologousImage.h"
#include "ReversePInvokeMethodStub.h"
 
using namespace il2cpp;
 
namespace hybridclr
{
 
namespace metadata
{
 
    Il2CppHashMap<const MethodInfo*, const ReversePInvokeInfo*, il2cpp::utils::PointerHash<MethodInfo>> MetadataModule::s_methodInfo2ReverseInfos;
    Il2CppHashMap<Il2CppMethodPointer, const ReversePInvokeInfo*, il2cpp::utils::PassThroughHash<Il2CppMethodPointer>> MetadataModule::s_methodPointer2ReverseInfos;
    Il2CppHashMap<const char*, int32_t, CStringHash, CStringEqualTo> MetadataModule::s_methodSig2Indexs;
    std::vector<ReversePInvokeInfo> MetadataModule::s_reverseInfos;
 
    static baselib::ReentrantLock g_reversePInvokeMethodLock;
 
    void MetadataModule::InitReversePInvokeInfo()
    {
        for (int32_t i = 0; ; i++)
        {
            ReversePInvokeMethodData& data = g_reversePInvokeMethodStub[i];
            if (data.methodPointer == nullptr)
            {
                break;
            }
            s_reverseInfos.push_back({ i, data.methodPointer, nullptr });
            auto it = s_methodSig2Indexs.find(data.methodSig);
            if (it == s_methodSig2Indexs.end())
            {
                s_methodSig2Indexs.insert({ data.methodSig, i });
            }
        }
        s_methodInfo2ReverseInfos.resize(s_reverseInfos.size() * 2);
        s_methodPointer2ReverseInfos.resize(s_reverseInfos.size() * 2);
        for (ReversePInvokeInfo& rpi : s_reverseInfos)
        {
            s_methodPointer2ReverseInfos.insert({ rpi.methodPointer, &rpi });
        }
    }
 
    void MetadataModule::Initialize()
    {
        InitReversePInvokeInfo();
        InterpreterImage::Initialize();
        Assembly::InitializePlaceHolderAssemblies();
    }
 
    Il2CppMethodPointer MetadataModule::GetReversePInvokeWrapper(const Il2CppImage* image, const MethodInfo* method)
    {
        if (!hybridclr::metadata::IsStaticMethod(method))
        {
            return nullptr;
        }
        il2cpp::os::FastAutoLock lock(&g_reversePInvokeMethodLock);
        auto it = s_methodInfo2ReverseInfos.find(method);
        if (it != s_methodInfo2ReverseInfos.end())
        {
            return it->second->methodPointer;
        }
 
 
        char sigName[1000];
        interpreter::ComputeSignature(method, false, sigName, sizeof(sigName) - 1);
        auto it2 = s_methodSig2Indexs.find(sigName);
        if (it2 == s_methodSig2Indexs.end())
        {
            TEMP_FORMAT(methodSigBuf, "GetReversePInvokeWrapper fail. not find wrapper of method:%s", GetMethodNameWithSignature(method).c_str());
            RaiseExecutionEngineException(methodSigBuf);
        }
        int32_t wrapperIndex = it2->second;
        ReversePInvokeMethodData& data = g_reversePInvokeMethodStub[wrapperIndex];
        if (data.methodPointer == nullptr || std::strcmp(data.methodSig, sigName))
        {
            TEMP_FORMAT(methodSigBuf, "GetReversePInvokeWrapper fail. exceed max wrapper num of method:%s", GetMethodNameWithSignature(method).c_str());
            RaiseExecutionEngineException(methodSigBuf);
        }
 
        s_methodSig2Indexs[sigName] = wrapperIndex + 1;
 
        ReversePInvokeInfo& rpi = s_reverseInfos[wrapperIndex];
        rpi.methodInfo = method;
        s_methodInfo2ReverseInfos.insert({ method, &rpi });
        return rpi.methodPointer;
    }
 
 
    LoadImageErrorCode MetadataModule::LoadMetadataForAOTAssembly(const void* dllBytes, uint32_t dllSize, HomologousImageMode mode)
    {
        il2cpp::os::FastAutoLock lock(&il2cpp::vm::g_MetadataLock);
 
        AOTHomologousImage* image = nullptr;
        switch (mode)
        {
        case HomologousImageMode::CONSISTENT: image = new ConsistentAOTHomologousImage(); break;
        case HomologousImageMode::SUPERSET: image = new SuperSetAOTHomologousImage(); break;
        default: return LoadImageErrorCode::INVALID_HOMOLOGOUS_MODE;
        }
 
        LoadImageErrorCode err = image->Load((byte*)CopyBytes(dllBytes, dllSize), dllSize);
        if (err != LoadImageErrorCode::OK)
        {
            return err;
        }
        if (AOTHomologousImage::FindImageByAssemblyLocked(image->GetAOTAssembly(), lock))
        {
            return LoadImageErrorCode::HOMOLOGOUS_ASSEMBLY_HAS_BEEN_LOADED;
        }
        image->InitRuntimeMetadatas();
        AOTHomologousImage::RegisterLocked(image, lock);
        return LoadImageErrorCode::OK;
    }
}
}