少年修仙传客户端基础资源
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/**
 * \file
 * System.Diagnostics.Process support
 *
 * Author:
 *    Dick Porter (dick@ximian.com)
 *
 * Copyright 2002 Ximian, Inc.
 * Copyright 2002-2006 Novell, Inc.
 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
 */
 
#include <config.h>
 
#include <glib.h>
#include <string.h>
 
#include <winsock2.h>
#include <windows.h>
 
#include <mono/metadata/object-internals.h>
#include <mono/metadata/w32process.h>
#include <mono/metadata/w32process-win32-internals.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/image.h>
#include <mono/metadata/cil-coff.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/threadpool-io.h>
#include <mono/utils/strenc.h>
#include <mono/utils/mono-proclib.h>
/* FIXME: fix this code to not depend so much on the internals */
#include <mono/metadata/class-internals.h>
#include <mono/metadata/w32handle.h>
#include <mono/utils/w32api.h>
 
#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
#include <shellapi.h>
#endif
 
void
mono_w32process_init (void)
{
}
 
void
mono_w32process_cleanup (void)
{
}
 
void
mono_w32process_signal_finished (void)
{
}
 
#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
HANDLE
ves_icall_System_Diagnostics_Process_GetProcess_internal (guint32 pid)
{
    HANDLE handle;
    
    /* GetCurrentProcess returns a pseudo-handle, so use
     * OpenProcess instead
     */
    handle = OpenProcess (PROCESS_ALL_ACCESS, TRUE, pid);
    if (handle == NULL)
        /* FIXME: Throw an exception */
        return NULL;
    return handle;
}
#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT) */
 
#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
MonoBoolean
ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoW32ProcessStartInfo *proc_start_info, MonoW32ProcessInfo *process_info)
{
    SHELLEXECUTEINFO shellex = {0};
    gboolean ret;
 
    shellex.cbSize = sizeof(SHELLEXECUTEINFO);
    shellex.fMask = (gulong)(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_UNICODE);
    shellex.nShow = (gulong)proc_start_info->window_style;
    shellex.nShow = (gulong)((shellex.nShow == 0) ? 1 : (shellex.nShow == 1 ? 0 : shellex.nShow));
 
    if (proc_start_info->filename != NULL) {
        shellex.lpFile = mono_string_chars (proc_start_info->filename);
    }
 
    if (proc_start_info->arguments != NULL) {
        shellex.lpParameters = mono_string_chars (proc_start_info->arguments);
    }
 
    if (proc_start_info->verb != NULL &&
        mono_string_length (proc_start_info->verb) != 0) {
        shellex.lpVerb = mono_string_chars (proc_start_info->verb);
    }
 
    if (proc_start_info->working_directory != NULL &&
        mono_string_length (proc_start_info->working_directory) != 0) {
        shellex.lpDirectory = mono_string_chars (proc_start_info->working_directory);
    }
 
    if (proc_start_info->error_dialog) {    
        shellex.hwnd = proc_start_info->error_dialog_parent_handle;
    } else {
        shellex.fMask = (gulong)(shellex.fMask | SEE_MASK_FLAG_NO_UI);
    }
 
    ret = ShellExecuteEx (&shellex);
    if (ret == FALSE) {
        process_info->pid = -GetLastError ();
    } else {
        process_info->process_handle = shellex.hProcess;
#if !defined(MONO_CROSS_COMPILE)
        process_info->pid = GetProcessId (shellex.hProcess);
#else
        process_info->pid = 0;
#endif
    }
 
    return ret;
}
#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
 
#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
static inline void
mono_process_init_startup_info (HANDLE stdin_handle, HANDLE stdout_handle, HANDLE stderr_handle, STARTUPINFO *startinfo)
{
    startinfo->cb = sizeof(STARTUPINFO);
    startinfo->dwFlags = STARTF_USESTDHANDLES;
    startinfo->hStdInput = stdin_handle;
    startinfo->hStdOutput = stdout_handle;
    startinfo->hStdError = stderr_handle;
    return;
}
 
static gboolean
mono_process_create_process (MonoW32ProcessInfo *mono_process_info, MonoString *cmd, guint32 creation_flags,
    gunichar2 *env_vars, gunichar2 *dir, STARTUPINFO *start_info, PROCESS_INFORMATION *process_info)
{
    gboolean result = FALSE;
 
    if (mono_process_info->username) {
        guint32 logon_flags = mono_process_info->load_user_profile ? LOGON_WITH_PROFILE : 0;
 
        result = CreateProcessWithLogonW (mono_string_chars (mono_process_info->username),
                          mono_process_info->domain ? mono_string_chars (mono_process_info->domain) : NULL,
                          (const gunichar2 *)mono_process_info->password,
                          logon_flags,
                          NULL,
                          cmd ? mono_string_chars (cmd) : NULL,
                          creation_flags,
                          env_vars, dir, start_info, process_info);
 
    } else {
 
        result = CreateProcessW (NULL,
                    cmd ? mono_string_chars (cmd): NULL,
                    NULL,
                    NULL,
                    TRUE,
                    creation_flags,
                    env_vars,
                    dir,
                    start_info,
                    process_info);
 
    }
 
    return result;
}
#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
 
static gchar*
process_unquote_application_name (gchar *appname)
{
    size_t len = strlen (appname);
    if (len) {
        if (appname[len-1] == '\"')
            appname[len-1] = '\0';
        if (appname[0] == '\"')
            appname++;
    }
 
    return appname;
}
 
static gchar*
process_quote_path (const gchar *path)
{
    gchar *res = g_shell_quote (path);
    gchar *q = res;
    while (*q) {
        if (*q == '\'')
            *q = '\"';
        q++;
    }
    return res;
}
 
/* Only used when UseShellExecute is false */
static gboolean
process_complete_path (const gunichar2 *appname, gchar **completed)
{
    gchar *utf8app, *utf8appmemory;
    gchar *found;
 
    utf8appmemory = g_utf16_to_utf8 (appname, -1, NULL, NULL, NULL);
    utf8app = process_unquote_application_name (utf8appmemory);
 
    if (g_path_is_absolute (utf8app)) {
        *completed = process_quote_path (utf8app);
        g_free (utf8appmemory);
        return TRUE;
    }
 
    if (g_file_test (utf8app, G_FILE_TEST_IS_EXECUTABLE) && !g_file_test (utf8app, G_FILE_TEST_IS_DIR)) {
        *completed = process_quote_path (utf8app);
        g_free (utf8appmemory);
        return TRUE;
    }
    
    found = g_find_program_in_path (utf8app);
    if (found == NULL) {
        *completed = NULL;
        g_free (utf8appmemory);
        return FALSE;
    }
 
    *completed = process_quote_path (found);
    g_free (found);
    g_free (utf8appmemory);
    return TRUE;
}
 
static gboolean
process_get_shell_arguments (MonoW32ProcessStartInfo *proc_start_info, MonoString **cmd)
{
    gchar        *spath = NULL;
    gchar        *new_cmd, *cmd_utf8;
    MonoError    mono_error;
 
    *cmd = proc_start_info->arguments;
 
    if (process_complete_path (mono_string_chars (proc_start_info->filename), &spath)) {
        /* Seems like our CreateProcess does not work as the windows one.
         * This hack is needed to deal with paths containing spaces */
        if (*cmd) {
            cmd_utf8 = mono_string_to_utf8_checked (*cmd, &mono_error);
            if (!mono_error_set_pending_exception (&mono_error)) {
                new_cmd = g_strdup_printf ("%s %s", spath, cmd_utf8);
                *cmd = mono_string_new_wrapper (new_cmd);
                g_free (cmd_utf8);
                g_free (new_cmd);
            } else {
                *cmd = NULL;
            }
        }
        else {
            *cmd = mono_string_new_wrapper (spath);
        }
 
        g_free (spath);
    }
 
    return (*cmd != NULL) ? TRUE : FALSE;
}
 
MonoBoolean
ves_icall_System_Diagnostics_Process_CreateProcess_internal (MonoW32ProcessStartInfo *proc_start_info, HANDLE stdin_handle,
                                 HANDLE stdout_handle, HANDLE stderr_handle, MonoW32ProcessInfo *process_info)
{
    gboolean ret;
    gunichar2 *dir;
    STARTUPINFO startinfo={0};
    PROCESS_INFORMATION procinfo;
    gunichar2 *env_vars = NULL;
    MonoString *cmd = NULL;
    guint32 creation_flags;
 
    mono_process_init_startup_info (stdin_handle, stdout_handle, stderr_handle, &startinfo);
 
    creation_flags = CREATE_UNICODE_ENVIRONMENT;
    if (proc_start_info->create_no_window)
        creation_flags |= CREATE_NO_WINDOW;
    
    if (process_get_shell_arguments (proc_start_info, &cmd) == FALSE) {
        process_info->pid = -ERROR_FILE_NOT_FOUND;
        return FALSE;
    }
 
    if (process_info->env_variables) {
        gint i, len;
        MonoString *var;
        gunichar2 *str, *ptr;
 
        len = 0;
 
        for (i = 0; i < mono_array_length (process_info->env_variables); i++) {
            var = mono_array_get (process_info->env_variables, MonoString*, i);
 
            len += mono_string_length (var) * sizeof (gunichar2);
 
            /* null-separated */
            len += sizeof (gunichar2);
        }
        /* null-terminated */
        len += sizeof (gunichar2);
 
        env_vars = ptr = g_new0 (gunichar2, len);
 
        for (i = 0; i < mono_array_length (process_info->env_variables); i++) {
            var = mono_array_get (process_info->env_variables, MonoString*, i);
 
            memcpy (ptr, mono_string_chars (var), mono_string_length (var) * sizeof (gunichar2));
            ptr += mono_string_length (var);
            ptr += 1; // Skip over the null-separator
        }
    }
    
    /* The default dir name is "".  Turn that into NULL to mean
     * "current directory"
     */
    if (proc_start_info->working_directory == NULL || mono_string_length (proc_start_info->working_directory) == 0)
        dir = NULL;
    else
        dir = mono_string_chars (proc_start_info->working_directory);
 
    ret = mono_process_create_process (process_info, cmd, creation_flags, env_vars, dir, &startinfo, &procinfo);
 
    g_free (env_vars);
 
    if (ret) {
        process_info->process_handle = procinfo.hProcess;
        /*process_info->thread_handle=procinfo.hThread;*/
        if (procinfo.hThread != NULL && procinfo.hThread != INVALID_HANDLE_VALUE)
            CloseHandle (procinfo.hThread);
        process_info->pid = procinfo.dwProcessId;
    } else {
        process_info->pid = -GetLastError ();
    }
    
    return ret;
}
 
#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
static inline gboolean
mono_process_win_enum_processes (DWORD *pids, DWORD count, DWORD *needed)
{
    return EnumProcesses (pids, count, needed);
}
#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
 
MonoArray *
ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
{
    MonoError error;
    MonoArray *procs;
    gboolean ret;
    DWORD needed;
    int count;
    DWORD *pids;
 
    count = 512;
    do {
        pids = g_new0 (DWORD, count);
        ret = mono_process_win_enum_processes (pids, count * sizeof (guint32), &needed);
        if (ret == FALSE) {
            MonoException *exc;
 
            g_free (pids);
            pids = NULL;
            exc = mono_get_exception_not_supported ("This system does not support EnumProcesses");
            mono_set_pending_exception (exc);
            return NULL;
        }
        if (needed < (count * sizeof (guint32)))
            break;
        g_free (pids);
        pids = NULL;
        count = (count * 3) / 2;
    } while (TRUE);
 
    count = needed / sizeof (guint32);
    procs = mono_array_new_checked (mono_domain_get (), mono_get_int32_class (), count, &error);
    if (mono_error_set_pending_exception (&error)) {
        g_free (pids);
        return NULL;
    }
 
    memcpy (mono_array_addr (procs, guint32, 0), pids, needed);
    g_free (pids);
    pids = NULL;
 
    return procs;
}
 
MonoBoolean
ves_icall_Microsoft_Win32_NativeMethods_CloseProcess (gpointer handle)
{
    return CloseHandle (handle);
}
 
MonoBoolean
ves_icall_Microsoft_Win32_NativeMethods_TerminateProcess (gpointer handle, gint32 exitcode)
{
    return TerminateProcess (handle, exitcode);
}
 
MonoBoolean
ves_icall_Microsoft_Win32_NativeMethods_GetExitCodeProcess (gpointer handle, gint32 *exitcode)
{
    return GetExitCodeProcess (handle, exitcode);
}
 
#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
static inline MonoBoolean
mono_icall_get_process_working_set_size (gpointer handle, gsize *min, gsize *max)
{
    return GetProcessWorkingSetSize (handle, min, max);
}
#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
 
MonoBoolean
ves_icall_Microsoft_Win32_NativeMethods_GetProcessWorkingSetSize (gpointer handle, gsize *min, gsize *max)
{
    return mono_icall_get_process_working_set_size (handle, min, max);
}
 
#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
static inline MonoBoolean
mono_icall_set_process_working_set_size (gpointer handle, gsize min, gsize max)
{
    return SetProcessWorkingSetSize (handle, min, max);
}
#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
 
MonoBoolean
ves_icall_Microsoft_Win32_NativeMethods_SetProcessWorkingSetSize (gpointer handle, gsize min, gsize max)
{
    return mono_icall_set_process_working_set_size (handle, min, max);
}
 
#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
static inline gint32
mono_icall_get_priority_class (gpointer handle)
{
    return GetPriorityClass (handle);
}
#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
 
gint32
ves_icall_Microsoft_Win32_NativeMethods_GetPriorityClass (gpointer handle)
{
    return mono_icall_get_priority_class (handle);
}
 
#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
static inline MonoBoolean
mono_icall_set_priority_class (gpointer handle, gint32 priorityClass)
{
    return SetPriorityClass (handle, (guint32) priorityClass);
}
#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
 
MonoBoolean
ves_icall_Microsoft_Win32_NativeMethods_SetPriorityClass (gpointer handle, gint32 priorityClass)
{
    return mono_icall_set_priority_class (handle, priorityClass);
}
 
MonoBoolean
ves_icall_Microsoft_Win32_NativeMethods_GetProcessTimes (gpointer handle, gint64 *creationtime, gint64 *exittime, gint64 *kerneltime, gint64 *usertime)
{
    return GetProcessTimes (handle, (LPFILETIME) creationtime, (LPFILETIME) exittime, (LPFILETIME) kerneltime, (LPFILETIME) usertime);
}
 
gpointer
ves_icall_Microsoft_Win32_NativeMethods_GetCurrentProcess (void)
{
    return GetCurrentProcess ();
}