少年修仙传客户端基础资源
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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "il2cpp-config.h"
 
#if IL2CPP_TARGET_WINDOWS && !IL2CPP_USE_GENERIC_WINDOWSRUNTIME
 
#include "il2cpp-class-internals.h"
#include "il2cpp-string-types.h"
#include "il2cpp-vm-support.h"
#include "os/WindowsRuntime.h"
#include "utils/Il2CppHStringReference.h"
#include "utils/StringUtils.h"
#include "vm/CCW.h"
#include "WindowsHeaders.h"
 
#include <roerrorapi.h>
 
namespace il2cpp
{
namespace os
{
#if !LINK_TO_WINDOWSRUNTIME_LIBS
    template<typename FunctionType>
    FunctionType ResolveAPI(const wchar_t* moduleName, const char* functionName)
    {
        HMODULE module = GetModuleHandleW(moduleName);
 
        if (module == NULL)
            return NULL;
 
        return reinterpret_cast<FunctionType>(GetProcAddress(module, functionName));
    }
 
#endif
 
    il2cpp_hresult_t WindowsRuntime::GetActivationFactory(Il2CppHString className, Il2CppIActivationFactory** activationFactory)
    {
        IL2CPP_ASSERT(className != NULL);
        IL2CPP_ASSERT(activationFactory != NULL);
 
#if LINK_TO_WINDOWSRUNTIME_LIBS
        return RoGetActivationFactory(reinterpret_cast<HSTRING>(className), reinterpret_cast<const IID&>(Il2CppIActivationFactory::IID), reinterpret_cast<void**>(activationFactory));
#else
        typedef HRESULT(WINAPI* RoGetActivationFactoryFunc)(void* activatableClassId, const Il2CppGuid& iid, Il2CppIActivationFactory** factory);
        static RoGetActivationFactoryFunc RoGetActivationFactory = NULL;
 
        if (RoGetActivationFactory == NULL)
        {
            RoGetActivationFactory = ResolveAPI<RoGetActivationFactoryFunc>(L"api-ms-win-core-winrt-l1-1-0.dll", "RoGetActivationFactory");
 
            if (RoGetActivationFactory == NULL)
                return IL2CPP_REGDB_E_CLASSNOTREG;
        }
 
        return RoGetActivationFactory(className, Il2CppIActivationFactory::IID, activationFactory);
#endif
    }
 
    il2cpp_hresult_t WindowsRuntime::CreateHStringReference(const utils::StringView<Il2CppNativeChar>& str, Il2CppHStringHeader* header, Il2CppHString* hstring)
    {
        IL2CPP_ASSERT(header != NULL);
        IL2CPP_ASSERT(hstring != NULL);
 
        if (str.Length() == 0)
        {
            *hstring = NULL;
            return S_OK;
        }
 
#if LINK_TO_WINDOWSRUNTIME_LIBS
        return WindowsCreateStringReference(str.Str(), static_cast<uint32_t>(str.Length()), reinterpret_cast<HSTRING_HEADER*>(header), reinterpret_cast<HSTRING*>(hstring));
#else
        typedef HRESULT(STDAPICALLTYPE * WindowsCreateStringReferenceFunc)(const wchar_t* sourceString, uint32_t length, Il2CppHStringHeader* hstringHeader, Il2CppHString* hstring);
        static WindowsCreateStringReferenceFunc WindowsCreateStringReference = NULL;
 
        if (WindowsCreateStringReference == NULL)
        {
            WindowsCreateStringReference = ResolveAPI<WindowsCreateStringReferenceFunc>(L"api-ms-win-core-winrt-string-l1-1-0.dll", "WindowsCreateStringReference");
 
            if (WindowsCreateStringReference == NULL)
                return IL2CPP_COR_E_PLATFORMNOTSUPPORTED;
        }
 
        return WindowsCreateStringReference(str.Str(), static_cast<uint32_t>(str.Length()), header, hstring);
#endif
    }
 
    il2cpp_hresult_t WindowsRuntime::CreateHString(const utils::StringView<Il2CppChar>& str, Il2CppHString* hstring)
    {
        IL2CPP_ASSERT(str.Str() != NULL || str.Length() == 0);
 
        if (str.Length() == 0)
        {
            *hstring = NULL;
            return S_OK;
        }
 
#if LINK_TO_WINDOWSRUNTIME_LIBS
        return WindowsCreateString(str.Str(), static_cast<uint32_t>(str.Length()), reinterpret_cast<HSTRING*>(hstring));
#else
        typedef il2cpp_hresult_t (STDAPICALLTYPE * WindowsCreateStringFunc)(const wchar_t* sourceString, uint32_t length, Il2CppHString* hstring);
        static WindowsCreateStringFunc WindowsCreateString = NULL;
 
        if (WindowsCreateString == NULL)
        {
            WindowsCreateString = ResolveAPI<WindowsCreateStringFunc>(L"api-ms-win-core-winrt-string-l1-1-0.dll", "WindowsCreateString");
 
            if (WindowsCreateString == NULL)
                return IL2CPP_COR_E_PLATFORMNOTSUPPORTED;
        }
 
        return WindowsCreateString(str.Str(), static_cast<uint32_t>(str.Length()), hstring);
#endif
    }
 
    il2cpp_hresult_t WindowsRuntime::DuplicateHString(Il2CppHString hstring, Il2CppHString* duplicated)
    {
#if LINK_TO_WINDOWSRUNTIME_LIBS
        return WindowsDuplicateString(reinterpret_cast<HSTRING>(hstring), reinterpret_cast<HSTRING*>(duplicated));
#else
        typedef il2cpp_hresult_t(STDAPICALLTYPE* WindowsDuplicateStringFunc)(Il2CppHString hstring, Il2CppHString* duplicated);
        static WindowsDuplicateStringFunc WindowsDuplicateString = NULL;
 
        if (WindowsDuplicateString == NULL)
        {
            WindowsDuplicateString = ResolveAPI<WindowsDuplicateStringFunc>(L"api-ms-win-core-winrt-string-l1-1-0.dll", "WindowsDuplicateString");
 
            if (WindowsDuplicateString == NULL)
                return IL2CPP_COR_E_PLATFORMNOTSUPPORTED;
        }
 
        return WindowsDuplicateString(hstring, duplicated);
#endif
    }
 
    il2cpp_hresult_t WindowsRuntime::DeleteHString(Il2CppHString hstring)
    {
        if (hstring == NULL)
            return IL2CPP_S_OK;
 
#if LINK_TO_WINDOWSRUNTIME_LIBS
        return WindowsDeleteString(reinterpret_cast<HSTRING>(hstring));
#else
        typedef il2cpp_hresult_t (STDAPICALLTYPE * WindowsDeleteStringFunc)(Il2CppHString hstring);
        static WindowsDeleteStringFunc WindowsDeleteString = NULL;
 
        if (WindowsDeleteString == NULL)
        {
            WindowsDeleteString = ResolveAPI<WindowsDeleteStringFunc>(L"api-ms-win-core-winrt-string-l1-1-0.dll", "WindowsDeleteString");
 
            if (WindowsDeleteString == NULL)
                return IL2CPP_COR_E_PLATFORMNOTSUPPORTED;
        }
 
        return WindowsDeleteString(hstring);
#endif
    }
 
    const Il2CppChar* WindowsRuntime::GetHStringBuffer(Il2CppHString hstring, uint32_t* length)
    {
#if LINK_TO_WINDOWSRUNTIME_LIBS
        return WindowsGetStringRawBuffer(reinterpret_cast<HSTRING>(hstring), length);
#else
        typedef const wchar_t* (STDAPICALLTYPE * WindowsGetStringRawBufferFunc)(Il2CppHString hstring, uint32_t* length);
        static WindowsGetStringRawBufferFunc WindowsGetStringRawBuffer = NULL;
 
        if (WindowsGetStringRawBuffer == NULL)
        {
            WindowsGetStringRawBuffer = ResolveAPI<WindowsGetStringRawBufferFunc>(L"api-ms-win-core-winrt-string-l1-1-0.dll", "WindowsGetStringRawBuffer");
 
            if (WindowsGetStringRawBuffer == NULL)
                IL2CPP_VM_NOT_SUPPORTED(GetHStringBuffer, "Marshaling HSTRINGs is not supported on current platform.");
        }
 
        return WindowsGetStringRawBuffer(hstring, length);
#endif
    }
 
    const Il2CppNativeChar* WindowsRuntime::GetNativeHStringBuffer(Il2CppHString hstring, uint32_t* length)
    {
        return GetHStringBuffer(hstring, length);
    }
 
    Il2CppString* WindowsRuntime::HStringToManagedString(Il2CppHString hstring)
    {
        if (hstring == NULL)
            return IL2CPP_VM_STRING_EMPTY();
 
        uint32_t length;
        const wchar_t* ptr = GetHStringBuffer(hstring, &length);
        return IL2CPP_VM_STRING_NEW_UTF16(ptr, length);
    }
 
    il2cpp_hresult_t WindowsRuntime::PreallocateHStringBuffer(uint32_t length, Il2CppNativeChar** mutableBuffer, void** bufferHandle)
    {
#if LINK_TO_WINDOWSRUNTIME_LIBS
        return WindowsPreallocateStringBuffer(length, mutableBuffer, reinterpret_cast<HSTRING_BUFFER*>(bufferHandle));
#else
        typedef il2cpp_hresult_t (STDAPICALLTYPE * WindowsPreallocateStringBufferFunc)(uint32_t length, Il2CppNativeChar** mutableBuffer, void** bufferHandle);
        static WindowsPreallocateStringBufferFunc WindowsPreallocateStringBuffer = NULL;
 
        if (WindowsPreallocateStringBuffer == NULL)
        {
            WindowsPreallocateStringBuffer = ResolveAPI<WindowsPreallocateStringBufferFunc>(L"api-ms-win-core-winrt-string-l1-1-0.dll", "WindowsPreallocateStringBuffer");
 
            if (WindowsPreallocateStringBuffer == NULL)
                IL2CPP_VM_NOT_SUPPORTED(PreallocateHStringBuffer, "Marshaling HSTRINGs is not supported on current platform.");
        }
 
        return WindowsPreallocateStringBuffer(length, mutableBuffer, bufferHandle);
#endif
    }
 
    il2cpp_hresult_t WindowsRuntime::PromoteHStringBuffer(void* bufferHandle, Il2CppHString* hstring)
    {
#if LINK_TO_WINDOWSRUNTIME_LIBS
        return WindowsPromoteStringBuffer(static_cast<HSTRING_BUFFER>(bufferHandle), reinterpret_cast<HSTRING*>(hstring));
#else
        typedef il2cpp_hresult_t (STDAPICALLTYPE * WindowsPromoteStringBufferFunc)(void* bufferHandle, Il2CppHString* hstring);
        static WindowsPromoteStringBufferFunc WindowsPromoteStringBuffer = NULL;
 
        if (WindowsPromoteStringBuffer == NULL)
        {
            WindowsPromoteStringBuffer = ResolveAPI<WindowsPromoteStringBufferFunc>(L"api-ms-win-core-winrt-string-l1-1-0.dll", "WindowsPromoteStringBuffer");
 
            if (WindowsPromoteStringBuffer == NULL)
                IL2CPP_VM_NOT_SUPPORTED(PromoteHStringBuffer, "Marshaling HSTRINGs is not supported on current platform.");
        }
 
        return WindowsPromoteStringBuffer(bufferHandle, hstring);
#endif
    }
 
    il2cpp_hresult_t WindowsRuntime::DeleteHStringBuffer(void* bufferHandle)
    {
#if LINK_TO_WINDOWSRUNTIME_LIBS
        return WindowsDeleteStringBuffer(static_cast<HSTRING_BUFFER>(bufferHandle));
#else
        typedef il2cpp_hresult_t (STDAPICALLTYPE * WindowsDeleteStringBufferFunc)(void* bufferHandle);
        static WindowsDeleteStringBufferFunc WindowsDeleteStringBuffer = NULL;
 
        if (WindowsDeleteStringBuffer == NULL)
        {
            WindowsDeleteStringBuffer = ResolveAPI<WindowsDeleteStringBufferFunc>(L"api-ms-win-core-winrt-string-l1-1-0.dll", "WindowsDeleteStringBuffer");
 
            if (WindowsDeleteStringBuffer == NULL)
                IL2CPP_VM_NOT_SUPPORTED(DeleteHStringBuffer, "Marshaling HSTRINGs is not supported on current platform.");
        }
 
        return WindowsDeleteStringBuffer(bufferHandle);
#endif
    }
 
    Il2CppIRestrictedErrorInfo* WindowsRuntime::GetRestrictedErrorInfo()
    {
        Il2CppIRestrictedErrorInfo* errorInfo;
        HRESULT hr;
 
#if LINK_TO_WINDOWSRUNTIME_LIBS
        hr = ::GetRestrictedErrorInfo(reinterpret_cast<IRestrictedErrorInfo**>(&errorInfo));
#else
        typedef HRESULT (STDAPICALLTYPE * GetRestrictedErrorInfoFunc)(Il2CppIRestrictedErrorInfo** ppRestrictedErrorInfo);
        static GetRestrictedErrorInfoFunc getRestrictedErrorInfo = NULL;
 
        if (getRestrictedErrorInfo == NULL)
        {
            getRestrictedErrorInfo = ResolveAPI<GetRestrictedErrorInfoFunc>(L"api-ms-win-core-winrt-error-l1-1-1.dll", "GetRestrictedErrorInfo");
 
            if (getRestrictedErrorInfo == NULL)
                return NULL;
        }
 
        hr = getRestrictedErrorInfo(&errorInfo);
#endif
 
        if (FAILED(hr))
            return NULL;
 
        return errorInfo;
    }
 
// Fallback path for desktop in case we're running on below Windows 8.1
// Also used for Xbox One as it has no RoOriginateLanguageException
    static inline void OriginateErrorNoLanguageException(Il2CppException* ex, Il2CppHString message)
    {
#if !LINK_TO_WINDOWSRUNTIME_LIBS
        typedef BOOL(STDAPICALLTYPE * RoOriginateErrorFunc)(il2cpp_hresult_t error, Il2CppHString message);
        static RoOriginateErrorFunc RoOriginateError = NULL;
 
        if (RoOriginateError == NULL)
        {
            RoOriginateError = ResolveAPI<RoOriginateErrorFunc>(L"api-ms-win-core-winrt-error-l1-1-0.dll", "RoOriginateError");
 
            if (RoOriginateError == NULL)
            {
                // We're running on Win7 or below. Give up.
                return;
            }
        }
 
        RoOriginateError(ex->hresult, message);
#else
        RoOriginateError(ex->hresult, reinterpret_cast<HSTRING>(message));
#endif
    }
 
#if !IL2CPP_TARGET_XBOXONE
 
    inline void OriginateLanguageExceptionWithFallback(Il2CppException* ex, Il2CppHString message)
    {
#if !LINK_TO_WINDOWSRUNTIME_LIBS
        typedef BOOL(STDAPICALLTYPE * RoOriginateLanguageExceptionFunc)(il2cpp_hresult_t error, Il2CppHString message, Il2CppIUnknown* languageException);
        static RoOriginateLanguageExceptionFunc RoOriginateLanguageException = NULL;
 
        if (RoOriginateLanguageException == NULL)
        {
            RoOriginateLanguageException = ResolveAPI<RoOriginateLanguageExceptionFunc>(L"api-ms-win-core-winrt-error-l1-1-1.dll", "RoOriginateLanguageException");
 
            if (RoOriginateLanguageException == NULL)
            {
                // We're running on Win8 or below. Fallback to RoOriginateError
                OriginateErrorNoLanguageException(ex, message);
                return;
            }
        }
#endif
 
        Il2CppIUnknown* exceptionCCW = IL2CPP_VM_GET_CREATE_CCW_EXCEPTION(ex);
 
#if LINK_TO_WINDOWSRUNTIME_LIBS
        RoOriginateLanguageException(ex->hresult, reinterpret_cast<HSTRING>(static_cast<Il2CppHString>(message)), reinterpret_cast<IUnknown*>(exceptionCCW));
#else
        RoOriginateLanguageException(ex->hresult, message, exceptionCCW);
#endif
 
        exceptionCCW->Release();
    }
 
#endif // !IL2CPP_TARGET_XBOXONE
 
    void WindowsRuntime::OriginateLanguageException(Il2CppException* ex, Il2CppString* exceptionString)
    {
        utils::StringView<Il2CppNativeChar> message(utils::StringUtils::GetChars(exceptionString), utils::StringUtils::GetLength(exceptionString));
        utils::Il2CppHStringReference messageHString(message);
 
#if IL2CPP_TARGET_XBOXONE
        OriginateErrorNoLanguageException(ex, messageHString);
#else
        OriginateLanguageExceptionWithFallback(ex, messageHString);
#endif
    }
 
    void WindowsRuntime::EnableErrorReporting()
    {
#if !LINK_TO_WINDOWSRUNTIME_LIBS
        typedef il2cpp_hresult_t (STDCALL * RoSetErrorReportingFlagsFunc)(uint32_t flags);
        static RoSetErrorReportingFlagsFunc RoSetErrorReportingFlags = NULL;
 
        if (RoSetErrorReportingFlags == NULL)
        {
            RoSetErrorReportingFlags = ResolveAPI<RoSetErrorReportingFlagsFunc>(L"api-ms-win-core-winrt-error-l1-1-0.dll", "RoSetErrorReportingFlags");
 
            // We're running on Win7 or below. Do nothing
            if (RoSetErrorReportingFlags == NULL)
                return;
        }
 
        const int RO_ERROR_REPORTING_USESETERRORINFO = 0x00000004;
#endif
 
        il2cpp_hresult_t hr = RoSetErrorReportingFlags(RO_ERROR_REPORTING_USESETERRORINFO);
        IL2CPP_ASSERT(IL2CPP_HR_SUCCEEDED(hr) && "RoSetErrorReportingFlags failed");
    }
}
}
 
#endif