少年修仙传客户端基础资源
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
#include "il2cpp-config.h"
 
#if IL2CPP_TARGET_POSIX
 
//#define VERBOSE_OUTPUT
 
#ifdef VERBOSE_OUTPUT
#include <stdio.h>
#endif
 
#include <dlfcn.h>
#include <string>
#include <set>
#if IL2CPP_TARGET_LINUX
#include <unistd.h>
#include <gnu/lib-names.h>
#endif
 
#include "il2cpp-runtime-metadata.h"
#include "os/LibraryLoader.h"
#include "os/Mutex.h"
#include "utils/PathUtils.h"
#include "utils/StringUtils.h"
#include "utils/Environment.h"
 
namespace il2cpp
{
namespace os
{
#if !IL2CPP_TINY_WITHOUT_DEBUGGER
    static std::set<void*> s_NativeHandlesOpen;
    typedef std::set<void*>::const_iterator OpenHandleIterator;
    os::FastMutex s_NativeHandlesOpenMutex;
#endif
 
    struct LibraryNamePrefixAndSuffix
    {
        LibraryNamePrefixAndSuffix(const char* prefix_, const char* suffix_)
        {
            prefix = std::string(prefix_);
            suffix = std::string(suffix_);
        }
 
        std::string prefix;
        std::string suffix;
    };
 
    static LibraryNamePrefixAndSuffix LibraryNamePrefixAndSuffixVariations[8] =
    {
        LibraryNamePrefixAndSuffix("", ".so"),
        LibraryNamePrefixAndSuffix("", ".dll"),
        LibraryNamePrefixAndSuffix("", ".dylib"),
        LibraryNamePrefixAndSuffix("", ".bundle"),
        LibraryNamePrefixAndSuffix("lib", ".so"),
        LibraryNamePrefixAndSuffix("lib", ".dll"),
        LibraryNamePrefixAndSuffix("lib", ".dylib"),
        LibraryNamePrefixAndSuffix("lib", ".bundle")
    };
 
// Note that testing this code can be a bit difficult, since dlopen will cache
// the values it returns, and we don't call dlcose from the C# level. See the
// comments in the integration test code for more details.
 
    static void* LoadLibraryWithName(const char* name, int flags)
    {
#ifdef VERBOSE_OUTPUT
        printf("Trying name: %s\n", name);
#endif
 
        void* handle = NULL;
#if IL2CPP_TARGET_IOS
        std::string dirName;
        if (utils::Environment::GetNumMainArgs() > 0)
        {
            std::string main = utils::StringUtils::Utf16ToUtf8(utils::Environment::GetMainArgs()[0]);
            dirName = utils::PathUtils::DirectoryName(main);
        }
 
        std::string libPath = utils::StringUtils::Printf("%s/%s", dirName.c_str(), name);
        handle = dlopen(libPath.c_str(), flags);
 
        // Fallback to just using the name. This might be a system dylib.
        if (handle == NULL)
            handle = dlopen(name, flags);
#else
        handle = dlopen(name, flags);
#endif
 
        if (handle != NULL)
            return handle;
 
#ifdef VERBOSE_OUTPUT
        printf("Error: %s\n", dlerror());
#endif
 
        return NULL;
    }
 
#if IL2CPP_TARGET_LINUX
    static void* LoadLibraryRelativeToExecutableDirectory(const char* name, int flags)
    {
        if (name == NULL || name[0] == '/')
            return NULL;
 
        char exePath[PATH_MAX + 1];
        int len;
 
        if ((len = readlink("/proc/self/exe", exePath, sizeof(exePath))) == -1)
        {
#ifdef VERBOSE_OUTPUT
            printf("Error: %s\n", perror());
#endif
            return NULL;
        }
        exePath[len] = '\0'; // readlink does not terminate buffer
        while (len > 0 && exePath[len] != '/')
            len--;
        exePath[len] = '\0';
        std::string libPath = utils::StringUtils::Printf("%s/%s", exePath, name);
        return dlopen(libPath.c_str(), flags);
    }
 
#endif
 
    static void* CheckLibraryVariations(const char* name, int flags)
    {
        int numberOfVariations = sizeof(LibraryNamePrefixAndSuffixVariations) / sizeof(LibraryNamePrefixAndSuffixVariations[0]);
        for (int i = 0; i < numberOfVariations; ++i)
        {
            std::string libraryName = LibraryNamePrefixAndSuffixVariations[i].prefix + name + LibraryNamePrefixAndSuffixVariations[i].suffix;
            void* handle = LoadLibraryWithName(libraryName.c_str(), flags);
            if (handle != NULL)
                return handle;
#if IL2CPP_TARGET_LINUX
            // Linux does not search current directory by default
            handle = LoadLibraryRelativeToExecutableDirectory(libraryName.c_str(), flags);
            if (handle != NULL)
                return handle;
#endif
        }
 
        return NULL;
    }
 
    Il2CppMethodPointer LibraryLoader::GetHardcodedPInvokeDependencyFunctionPointer(const il2cpp::utils::StringView<Il2CppNativeChar>& nativeDynamicLibrary, const il2cpp::utils::StringView<char>& entryPoint)
    {
        return NULL;
    }
 
    void* LibraryLoader::LoadDynamicLibrary(const utils::StringView<Il2CppNativeChar>& nativeDynamicLibrary)
    {
        return LoadDynamicLibrary(nativeDynamicLibrary, RTLD_LAZY);
    }
 
    void* LibraryLoader::LoadDynamicLibrary(const utils::StringView<Il2CppNativeChar>& nativeDynamicLibrary, int flags)
    {
#ifdef VERBOSE_OUTPUT
        printf("Attempting to load dynamic library: %s\n", nativeDynamicLibrary.Str());
#endif
 
        if (nativeDynamicLibrary.IsEmpty())
            return LoadLibraryWithName(NULL, flags);
 
#if IL2CPP_TARGET_LINUX
        // Workaround the fact that on Linux, libc is actually named libc.so.6 instead of libc.so.
        // mscorlib P/Invokes into plain libc, so we need this for those P/Invokes to succeed
        if (strncasecmp(nativeDynamicLibrary.Str(), "libc", nativeDynamicLibrary.Length()) == 0)
            return LoadLibraryWithName(LIBC_SO, flags);
#endif
 
        StringViewAsNullTerminatedStringOf(char, nativeDynamicLibrary, libraryName);
        void* handle = LoadLibraryWithName(libraryName, flags);
 
        if (handle == NULL)
            handle = CheckLibraryVariations(libraryName, flags);
 
        if (handle == NULL)
        {
            size_t lengthWithoutDotDll = nativeDynamicLibrary.Length() - 4;
            if (strncmp(libraryName + lengthWithoutDotDll, ".dll", 4) == 0)
            {
                char* nativeDynamicLibraryWithoutExtension = static_cast<char*>(alloca((lengthWithoutDotDll + 1) * sizeof(char)));
                memcpy(nativeDynamicLibraryWithoutExtension, libraryName, lengthWithoutDotDll);
                nativeDynamicLibraryWithoutExtension[lengthWithoutDotDll] = 0;
 
                handle = CheckLibraryVariations(nativeDynamicLibraryWithoutExtension, flags);
            }
        }
 
#if !IL2CPP_TINY_WITHOUT_DEBUGGER
        os::FastAutoLock lock(&s_NativeHandlesOpenMutex);
        if (handle != NULL)
            s_NativeHandlesOpen.insert(handle);
#endif
 
        return handle;
    }
 
    Il2CppMethodPointer LibraryLoader::GetFunctionPointer(void* dynamicLibrary, const PInvokeArguments& pinvokeArgs)
    {
        StringViewAsNullTerminatedStringOf(char, pinvokeArgs.entryPoint, entryPoint);
#if IL2CPP_TINY_WITHOUT_DEBUGGER
        return reinterpret_cast<Il2CppMethodPointer>(dlsym(dynamicLibrary, entryPoint));
#else
 
        if (pinvokeArgs.isNoMangle)
            return reinterpret_cast<Il2CppMethodPointer>(dlsym(dynamicLibrary, entryPoint));
 
        const size_t kBufferOverhead = 10;
        void* functionPtr = NULL;
        size_t originalFuncNameLength = strlen(entryPoint) + 1;
        std::string functionName;
 
        functionName.resize(originalFuncNameLength + kBufferOverhead + 1); // Let's index the string from '1', because we might have to prepend an underscore in case of stdcall mangling
        memcpy(&functionName[1], entryPoint, originalFuncNameLength);
        memset(&functionName[1] + originalFuncNameLength, 0, kBufferOverhead);
 
        // If there's no 'dont mangle' flag set, 'W' function takes priority over original name, but 'A' function does not (yes, really)
        if (pinvokeArgs.charSet == CHARSET_UNICODE)
        {
            functionName[originalFuncNameLength] = 'W';
            functionPtr = dlsym(dynamicLibrary, functionName.c_str() + 1);
            if (functionPtr != NULL)
                return reinterpret_cast<Il2CppMethodPointer>(functionPtr);
 
            // If charset specific function lookup failed, try with original name
            functionPtr = dlsym(dynamicLibrary, entryPoint);
        }
        else
        {
            functionPtr = dlsym(dynamicLibrary, entryPoint);
            if (functionPtr != NULL)
                return reinterpret_cast<Il2CppMethodPointer>(functionPtr);
 
            // If original name function lookup failed, try with mangled name
            functionName[originalFuncNameLength] = 'A';
            functionPtr = dlsym(dynamicLibrary, functionName.c_str() + 1);
        }
 
        return reinterpret_cast<Il2CppMethodPointer>(functionPtr);
#endif
    }
 
    Il2CppMethodPointer LibraryLoader::GetFunctionPointer(void* dynamicLibrary, const char* functionName)
    {
#ifdef VERBOSE_OUTPUT
        printf("Attempting to load method at entry point: %s\n", functionName);
#endif
 
        Il2CppMethodPointer method = reinterpret_cast<Il2CppMethodPointer>(dlsym(dynamicLibrary, functionName));
 
#ifdef VERBOSE_OUTPUT
        if (method == NULL)
            printf("Error: %s\n", dlerror());
#endif
 
        return method;
    }
 
    void LibraryLoader::CleanupLoadedLibraries()
    {
#if !IL2CPP_TINY_WITHOUT_DEBUGGER
        os::FastAutoLock lock(&s_NativeHandlesOpenMutex);
        for (OpenHandleIterator it = s_NativeHandlesOpen.begin(); it != s_NativeHandlesOpen.end(); it++)
        {
            dlclose(*it);
        }
#endif
    }
 
    bool LibraryLoader::CloseLoadedLibrary(void*& dynamicLibrary)
    {
        if (dynamicLibrary == NULL)
            return false;
 
#if !IL2CPP_TINY_WITHOUT_DEBUGGER
        os::FastAutoLock lock(&s_NativeHandlesOpenMutex);
        OpenHandleIterator it = s_NativeHandlesOpen.find(dynamicLibrary);
        if (it != s_NativeHandlesOpen.end())
        {
            dlclose(*it);
            s_NativeHandlesOpen.erase(it);
            return true;
        }
        return false;
#else
        return true;
#endif
    }
}
}
 
#endif