2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* godot_windows.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "os_windows.h"
|
2019-02-12 15:43:54 +01:00
|
|
|
|
2023-06-08 14:51:32 +02:00
|
|
|
#include "main/main.h"
|
|
|
|
|
2014-02-21 12:00:23 +01:00
|
|
|
#include <locale.h>
|
2017-03-05 16:44:50 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2019-03-25 20:56:33 +01:00
|
|
|
// For export templates, add a section; the exporter will patch it to enclose
|
2024-01-22 09:47:42 +01:00
|
|
|
// the data appended to the executable (bundled PCK).
|
2019-03-25 20:56:33 +01:00
|
|
|
#ifndef TOOLS_ENABLED
|
|
|
|
#if defined _MSC_VER
|
|
|
|
#pragma section("pck", read)
|
2021-12-20 10:28:54 +01:00
|
|
|
__declspec(allocate("pck")) static char dummy[8] = { 0 };
|
|
|
|
|
|
|
|
// Dummy function to prevent LTO from discarding "pck" section.
|
|
|
|
extern "C" char *__cdecl pck_section_dummy_call() {
|
|
|
|
return &dummy[0];
|
2024-01-22 09:47:42 +01:00
|
|
|
}
|
2021-12-20 10:28:54 +01:00
|
|
|
#if defined _AMD64_
|
|
|
|
#pragma comment(linker, "/include:pck_section_dummy_call")
|
|
|
|
#elif defined _X86_
|
|
|
|
#pragma comment(linker, "/include:_pck_section_dummy_call")
|
|
|
|
#endif
|
|
|
|
|
2019-03-25 20:56:33 +01:00
|
|
|
#elif defined __GNUC__
|
|
|
|
static const char dummy[8] __attribute__((section("pck"), used)) = { 0 };
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
PCHAR *
|
|
|
|
CommandLineToArgvA(
|
|
|
|
PCHAR CmdLine,
|
|
|
|
int *_argc) {
|
|
|
|
PCHAR *argv;
|
|
|
|
PCHAR _argv;
|
|
|
|
ULONG len;
|
|
|
|
ULONG argc;
|
|
|
|
CHAR a;
|
|
|
|
ULONG i, j;
|
|
|
|
|
|
|
|
BOOLEAN in_QM;
|
|
|
|
BOOLEAN in_TEXT;
|
|
|
|
BOOLEAN in_SPACE;
|
|
|
|
|
|
|
|
len = strlen(CmdLine);
|
|
|
|
i = ((len + 2) / 2) * sizeof(PVOID) + sizeof(PVOID);
|
|
|
|
|
|
|
|
argv = (PCHAR *)GlobalAlloc(GMEM_FIXED,
|
|
|
|
i + (len + 2) * sizeof(CHAR));
|
|
|
|
|
|
|
|
_argv = (PCHAR)(((PUCHAR)argv) + i);
|
|
|
|
|
|
|
|
argc = 0;
|
|
|
|
argv[argc] = _argv;
|
|
|
|
in_QM = FALSE;
|
|
|
|
in_TEXT = FALSE;
|
|
|
|
in_SPACE = TRUE;
|
|
|
|
i = 0;
|
|
|
|
j = 0;
|
|
|
|
|
2022-09-28 15:59:08 +02:00
|
|
|
a = CmdLine[i];
|
|
|
|
while (a) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (in_QM) {
|
|
|
|
if (a == '\"') {
|
|
|
|
in_QM = FALSE;
|
|
|
|
} else {
|
|
|
|
_argv[j] = a;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (a) {
|
|
|
|
case '\"':
|
|
|
|
in_QM = TRUE;
|
|
|
|
in_TEXT = TRUE;
|
|
|
|
if (in_SPACE) {
|
|
|
|
argv[argc] = _argv + j;
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
in_SPACE = FALSE;
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
if (in_TEXT) {
|
|
|
|
_argv[j] = '\0';
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
in_TEXT = FALSE;
|
|
|
|
in_SPACE = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
in_TEXT = TRUE;
|
|
|
|
if (in_SPACE) {
|
|
|
|
argv[argc] = _argv + j;
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
_argv[j] = a;
|
|
|
|
j++;
|
|
|
|
in_SPACE = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
2022-09-28 15:59:08 +02:00
|
|
|
a = CmdLine[i];
|
2017-03-05 16:44:50 +01:00
|
|
|
}
|
|
|
|
_argv[j] = '\0';
|
2020-04-02 01:20:12 +02:00
|
|
|
argv[argc] = nullptr;
|
2017-03-05 16:44:50 +01:00
|
|
|
|
|
|
|
(*_argc) = argc;
|
|
|
|
return argv;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
char *wc_to_utf8(const wchar_t *wc) {
|
2020-04-02 01:20:12 +02:00
|
|
|
int ulen = WideCharToMultiByte(CP_UTF8, 0, wc, -1, nullptr, 0, nullptr, nullptr);
|
2017-03-05 16:44:50 +01:00
|
|
|
char *ubuf = new char[ulen + 1];
|
2020-04-02 01:20:12 +02:00
|
|
|
WideCharToMultiByte(CP_UTF8, 0, wc, -1, ubuf, ulen, nullptr, nullptr);
|
2014-02-21 12:00:23 +01:00
|
|
|
ubuf[ulen] = 0;
|
|
|
|
return ubuf;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int widechar_main(int argc, wchar_t **argv) {
|
2020-04-02 01:20:12 +02:00
|
|
|
OS_Windows os(nullptr);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-02-21 12:00:23 +01:00
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
char **argv_utf8 = new char *[argc];
|
2015-05-04 20:26:49 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < argc; ++i) {
|
2015-05-04 20:26:49 +02:00
|
|
|
argv_utf8[i] = wc_to_utf8(argv[i]);
|
2014-02-21 12:00:23 +01:00
|
|
|
}
|
|
|
|
|
2020-07-20 18:35:34 +02:00
|
|
|
TEST_MAIN_PARAM_OVERRIDE(argc, argv_utf8)
|
|
|
|
|
2015-05-17 19:17:57 +02:00
|
|
|
Error err = Main::setup(argv_utf8[0], argc - 1, &argv_utf8[1]);
|
|
|
|
|
2017-08-24 02:54:02 +02:00
|
|
|
if (err != OK) {
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
delete[] argv_utf8[i];
|
|
|
|
}
|
|
|
|
delete[] argv_utf8;
|
2022-07-02 01:05:20 +02:00
|
|
|
|
|
|
|
if (err == ERR_HELP) { // Returned by --help and --version, so success.
|
2024-03-06 18:50:35 +01:00
|
|
|
return EXIT_SUCCESS;
|
2022-07-02 01:05:20 +02:00
|
|
|
}
|
2024-03-06 18:50:35 +01:00
|
|
|
return EXIT_FAILURE;
|
2017-08-24 02:54:02 +02:00
|
|
|
}
|
2015-05-17 19:17:57 +02:00
|
|
|
|
2024-03-06 18:50:35 +01:00
|
|
|
if (Main::start() == EXIT_SUCCESS) {
|
2014-02-10 02:10:30 +01:00
|
|
|
os.run();
|
2024-03-06 18:50:35 +01:00
|
|
|
} else {
|
|
|
|
os.set_exit_code(EXIT_FAILURE);
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
Main::cleanup();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < argc; ++i) {
|
2014-02-21 12:00:23 +01:00
|
|
|
delete[] argv_utf8[i];
|
|
|
|
}
|
|
|
|
delete[] argv_utf8;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return os.get_exit_code();
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-09-18 21:29:23 +02:00
|
|
|
int _main() {
|
|
|
|
LPWSTR *wc_argv;
|
|
|
|
int argc;
|
|
|
|
int result;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-09-18 21:29:23 +02:00
|
|
|
wc_argv = CommandLineToArgvW(GetCommandLineW(), &argc);
|
|
|
|
|
2020-04-02 14:41:58 +02:00
|
|
|
if (nullptr == wc_argv) {
|
2017-09-18 21:29:23 +02:00
|
|
|
wprintf(L"CommandLineToArgvW failed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-09-18 21:29:23 +02:00
|
|
|
result = widechar_main(argc, wc_argv);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-09-18 21:29:23 +02:00
|
|
|
LocalFree(wc_argv);
|
|
|
|
return result;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-07-20 18:35:34 +02:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
// override the arguments for the test handler / if symbol is provided
|
|
|
|
// TEST_MAIN_OVERRIDE
|
|
|
|
|
2018-07-18 16:24:33 +02:00
|
|
|
// _argc and _argv are ignored
|
|
|
|
// we are going to use the WideChar version of them instead
|
2022-05-13 14:42:07 +02:00
|
|
|
#if defined(CRASH_HANDLER_EXCEPTION) && defined(_MSC_VER)
|
2017-09-18 21:29:23 +02:00
|
|
|
__try {
|
|
|
|
return _main();
|
2017-09-08 03:01:49 +02:00
|
|
|
} __except (CrashHandlerException(GetExceptionInformation())) {
|
|
|
|
return 1;
|
|
|
|
}
|
2017-09-18 21:29:23 +02:00
|
|
|
#else
|
|
|
|
return _main();
|
2017-09-08 03:01:49 +02:00
|
|
|
#endif
|
2015-05-04 20:26:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
HINSTANCE godot_hinstance = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
|
2015-05-04 20:26:49 +02:00
|
|
|
godot_hinstance = hInstance;
|
2020-04-02 01:20:12 +02:00
|
|
|
return main(0, nullptr);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|