godot_windows.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**************************************************************************/
  2. /* godot_windows.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "os_windows.h"
  31. #include "main/main.h"
  32. #include <locale.h>
  33. #include <stdio.h>
  34. // For export templates, add a section; the exporter will patch it to enclose
  35. // the data appended to the executable (bundled PCK).
  36. #ifndef TOOLS_ENABLED
  37. #if defined _MSC_VER
  38. #pragma section("pck", read)
  39. __declspec(allocate("pck")) static char dummy[8] = { 0 };
  40. // Dummy function to prevent LTO from discarding "pck" section.
  41. extern "C" char *__cdecl pck_section_dummy_call() {
  42. return &dummy[0];
  43. }
  44. #if defined _AMD64_
  45. #pragma comment(linker, "/include:pck_section_dummy_call")
  46. #elif defined _X86_
  47. #pragma comment(linker, "/include:_pck_section_dummy_call")
  48. #endif
  49. #elif defined __GNUC__
  50. static const char dummy[8] __attribute__((section("pck"), used)) = { 0 };
  51. #endif
  52. #endif
  53. PCHAR *
  54. CommandLineToArgvA(
  55. PCHAR CmdLine,
  56. int *_argc) {
  57. PCHAR *argv;
  58. PCHAR _argv;
  59. ULONG len;
  60. ULONG argc;
  61. CHAR a;
  62. ULONG i, j;
  63. BOOLEAN in_QM;
  64. BOOLEAN in_TEXT;
  65. BOOLEAN in_SPACE;
  66. len = strlen(CmdLine);
  67. i = ((len + 2) / 2) * sizeof(PVOID) + sizeof(PVOID);
  68. argv = (PCHAR *)GlobalAlloc(GMEM_FIXED,
  69. i + (len + 2) * sizeof(CHAR));
  70. _argv = (PCHAR)(((PUCHAR)argv) + i);
  71. argc = 0;
  72. argv[argc] = _argv;
  73. in_QM = FALSE;
  74. in_TEXT = FALSE;
  75. in_SPACE = TRUE;
  76. i = 0;
  77. j = 0;
  78. a = CmdLine[i];
  79. while (a) {
  80. if (in_QM) {
  81. if (a == '\"') {
  82. in_QM = FALSE;
  83. } else {
  84. _argv[j] = a;
  85. j++;
  86. }
  87. } else {
  88. switch (a) {
  89. case '\"':
  90. in_QM = TRUE;
  91. in_TEXT = TRUE;
  92. if (in_SPACE) {
  93. argv[argc] = _argv + j;
  94. argc++;
  95. }
  96. in_SPACE = FALSE;
  97. break;
  98. case ' ':
  99. case '\t':
  100. case '\n':
  101. case '\r':
  102. if (in_TEXT) {
  103. _argv[j] = '\0';
  104. j++;
  105. }
  106. in_TEXT = FALSE;
  107. in_SPACE = TRUE;
  108. break;
  109. default:
  110. in_TEXT = TRUE;
  111. if (in_SPACE) {
  112. argv[argc] = _argv + j;
  113. argc++;
  114. }
  115. _argv[j] = a;
  116. j++;
  117. in_SPACE = FALSE;
  118. break;
  119. }
  120. }
  121. i++;
  122. a = CmdLine[i];
  123. }
  124. _argv[j] = '\0';
  125. argv[argc] = nullptr;
  126. (*_argc) = argc;
  127. return argv;
  128. }
  129. char *wc_to_utf8(const wchar_t *wc) {
  130. int ulen = WideCharToMultiByte(CP_UTF8, 0, wc, -1, nullptr, 0, nullptr, nullptr);
  131. char *ubuf = new char[ulen + 1];
  132. WideCharToMultiByte(CP_UTF8, 0, wc, -1, ubuf, ulen, nullptr, nullptr);
  133. ubuf[ulen] = 0;
  134. return ubuf;
  135. }
  136. int widechar_main(int argc, wchar_t **argv) {
  137. OS_Windows os(nullptr);
  138. setlocale(LC_CTYPE, "");
  139. char **argv_utf8 = new char *[argc];
  140. for (int i = 0; i < argc; ++i) {
  141. argv_utf8[i] = wc_to_utf8(argv[i]);
  142. }
  143. TEST_MAIN_PARAM_OVERRIDE(argc, argv_utf8)
  144. Error err = Main::setup(argv_utf8[0], argc - 1, &argv_utf8[1]);
  145. if (err != OK) {
  146. for (int i = 0; i < argc; ++i) {
  147. delete[] argv_utf8[i];
  148. }
  149. delete[] argv_utf8;
  150. if (err == ERR_HELP) { // Returned by --help and --version, so success.
  151. return EXIT_SUCCESS;
  152. }
  153. return EXIT_FAILURE;
  154. }
  155. if (Main::start() == EXIT_SUCCESS) {
  156. os.run();
  157. } else {
  158. os.set_exit_code(EXIT_FAILURE);
  159. }
  160. Main::cleanup();
  161. for (int i = 0; i < argc; ++i) {
  162. delete[] argv_utf8[i];
  163. }
  164. delete[] argv_utf8;
  165. return os.get_exit_code();
  166. }
  167. int _main() {
  168. LPWSTR *wc_argv;
  169. int argc;
  170. int result;
  171. wc_argv = CommandLineToArgvW(GetCommandLineW(), &argc);
  172. if (nullptr == wc_argv) {
  173. wprintf(L"CommandLineToArgvW failed\n");
  174. return 0;
  175. }
  176. result = widechar_main(argc, wc_argv);
  177. LocalFree(wc_argv);
  178. return result;
  179. }
  180. int main(int argc, char **argv) {
  181. // override the arguments for the test handler / if symbol is provided
  182. // TEST_MAIN_OVERRIDE
  183. // _argc and _argv are ignored
  184. // we are going to use the WideChar version of them instead
  185. #if defined(CRASH_HANDLER_EXCEPTION) && defined(_MSC_VER)
  186. __try {
  187. return _main();
  188. } __except (CrashHandlerException(GetExceptionInformation())) {
  189. return 1;
  190. }
  191. #else
  192. return _main();
  193. #endif
  194. }
  195. HINSTANCE godot_hinstance = nullptr;
  196. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  197. godot_hinstance = hInstance;
  198. return main(0, nullptr);
  199. }