godot_windows.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*************************************************************************/
  2. /* godot_windows.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "main/main.h"
  31. #include "os_windows.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 const char dummy[8] = { 0 };
  40. #elif defined __GNUC__
  41. static const char dummy[8] __attribute__((section("pck"), used)) = { 0 };
  42. #endif
  43. #endif
  44. PCHAR *
  45. CommandLineToArgvA(
  46. PCHAR CmdLine,
  47. int *_argc) {
  48. PCHAR *argv;
  49. PCHAR _argv;
  50. ULONG len;
  51. ULONG argc;
  52. CHAR a;
  53. ULONG i, j;
  54. BOOLEAN in_QM;
  55. BOOLEAN in_TEXT;
  56. BOOLEAN in_SPACE;
  57. len = strlen(CmdLine);
  58. i = ((len + 2) / 2) * sizeof(PVOID) + sizeof(PVOID);
  59. argv = (PCHAR *)GlobalAlloc(GMEM_FIXED,
  60. i + (len + 2) * sizeof(CHAR));
  61. _argv = (PCHAR)(((PUCHAR)argv) + i);
  62. argc = 0;
  63. argv[argc] = _argv;
  64. in_QM = FALSE;
  65. in_TEXT = FALSE;
  66. in_SPACE = TRUE;
  67. i = 0;
  68. j = 0;
  69. while ((a = CmdLine[i])) {
  70. if (in_QM) {
  71. if (a == '\"') {
  72. in_QM = FALSE;
  73. } else {
  74. _argv[j] = a;
  75. j++;
  76. }
  77. } else {
  78. switch (a) {
  79. case '\"':
  80. in_QM = TRUE;
  81. in_TEXT = TRUE;
  82. if (in_SPACE) {
  83. argv[argc] = _argv + j;
  84. argc++;
  85. }
  86. in_SPACE = FALSE;
  87. break;
  88. case ' ':
  89. case '\t':
  90. case '\n':
  91. case '\r':
  92. if (in_TEXT) {
  93. _argv[j] = '\0';
  94. j++;
  95. }
  96. in_TEXT = FALSE;
  97. in_SPACE = TRUE;
  98. break;
  99. default:
  100. in_TEXT = TRUE;
  101. if (in_SPACE) {
  102. argv[argc] = _argv + j;
  103. argc++;
  104. }
  105. _argv[j] = a;
  106. j++;
  107. in_SPACE = FALSE;
  108. break;
  109. }
  110. }
  111. i++;
  112. }
  113. _argv[j] = '\0';
  114. argv[argc] = NULL;
  115. (*_argc) = argc;
  116. return argv;
  117. }
  118. char *wc_to_utf8(const wchar_t *wc) {
  119. int ulen = WideCharToMultiByte(CP_UTF8, 0, wc, -1, NULL, 0, NULL, NULL);
  120. char *ubuf = new char[ulen + 1];
  121. WideCharToMultiByte(CP_UTF8, 0, wc, -1, ubuf, ulen, NULL, NULL);
  122. ubuf[ulen] = 0;
  123. return ubuf;
  124. }
  125. __declspec(dllexport) int widechar_main(int argc, wchar_t **argv) {
  126. OS_Windows os(NULL);
  127. setlocale(LC_CTYPE, "");
  128. #ifndef TOOLS_ENABLED
  129. // Workaround to prevent LTCG (MSVC LTO) from removing "pck" section
  130. const char *dummy_guard = dummy;
  131. #endif
  132. char **argv_utf8 = new char *[argc];
  133. for (int i = 0; i < argc; ++i) {
  134. argv_utf8[i] = wc_to_utf8(argv[i]);
  135. }
  136. Error err = Main::setup(argv_utf8[0], argc - 1, &argv_utf8[1]);
  137. if (err != OK) {
  138. for (int i = 0; i < argc; ++i) {
  139. delete[] argv_utf8[i];
  140. }
  141. delete[] argv_utf8;
  142. return 255;
  143. }
  144. if (Main::start())
  145. os.run();
  146. Main::cleanup();
  147. for (int i = 0; i < argc; ++i) {
  148. delete[] argv_utf8[i];
  149. }
  150. delete[] argv_utf8;
  151. return os.get_exit_code();
  152. };
  153. __declspec(dllexport) int _main() {
  154. LPWSTR *wc_argv;
  155. int argc;
  156. int result;
  157. wc_argv = CommandLineToArgvW(GetCommandLineW(), &argc);
  158. if (NULL == wc_argv) {
  159. wprintf(L"CommandLineToArgvW failed\n");
  160. return 0;
  161. }
  162. result = widechar_main(argc, wc_argv);
  163. LocalFree(wc_argv);
  164. return result;
  165. }
  166. __declspec(dllexport) int main(int _argc, char **_argv) {
  167. // _argc and _argv are ignored
  168. // we are going to use the WideChar version of them instead
  169. #ifdef CRASH_HANDLER_EXCEPTION
  170. __try {
  171. return _main();
  172. } __except (CrashHandlerException(GetExceptionInformation())) {
  173. return 1;
  174. }
  175. #else
  176. return _main();
  177. #endif
  178. }
  179. HINSTANCE godot_hinstance = NULL;
  180. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  181. godot_hinstance = hInstance;
  182. return main(0, NULL);
  183. }