godot_windows.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "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 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. while ((a = CmdLine[i])) {
  79. if (in_QM) {
  80. if (a == '\"') {
  81. in_QM = FALSE;
  82. } else {
  83. _argv[j] = a;
  84. j++;
  85. }
  86. } else {
  87. switch (a) {
  88. case '\"':
  89. in_QM = TRUE;
  90. in_TEXT = TRUE;
  91. if (in_SPACE) {
  92. argv[argc] = _argv + j;
  93. argc++;
  94. }
  95. in_SPACE = FALSE;
  96. break;
  97. case ' ':
  98. case '\t':
  99. case '\n':
  100. case '\r':
  101. if (in_TEXT) {
  102. _argv[j] = '\0';
  103. j++;
  104. }
  105. in_TEXT = FALSE;
  106. in_SPACE = TRUE;
  107. break;
  108. default:
  109. in_TEXT = TRUE;
  110. if (in_SPACE) {
  111. argv[argc] = _argv + j;
  112. argc++;
  113. }
  114. _argv[j] = a;
  115. j++;
  116. in_SPACE = FALSE;
  117. break;
  118. }
  119. }
  120. i++;
  121. }
  122. _argv[j] = '\0';
  123. argv[argc] = NULL;
  124. (*_argc) = argc;
  125. return argv;
  126. }
  127. char *wc_to_utf8(const wchar_t *wc) {
  128. int ulen = WideCharToMultiByte(CP_UTF8, 0, wc, -1, NULL, 0, NULL, NULL);
  129. char *ubuf = new char[ulen + 1];
  130. WideCharToMultiByte(CP_UTF8, 0, wc, -1, ubuf, ulen, NULL, NULL);
  131. ubuf[ulen] = 0;
  132. return ubuf;
  133. }
  134. __declspec(dllexport) int widechar_main(int argc, wchar_t **argv) {
  135. OS_Windows os(NULL);
  136. setlocale(LC_CTYPE, "");
  137. char **argv_utf8 = new char *[argc];
  138. for (int i = 0; i < argc; ++i) {
  139. argv_utf8[i] = wc_to_utf8(argv[i]);
  140. }
  141. Error err = Main::setup(argv_utf8[0], argc - 1, &argv_utf8[1]);
  142. if (err != OK) {
  143. for (int i = 0; i < argc; ++i) {
  144. delete[] argv_utf8[i];
  145. }
  146. delete[] argv_utf8;
  147. if (err == ERR_HELP) { // Returned by --help and --version, so success.
  148. return 0;
  149. }
  150. return 255;
  151. }
  152. if (Main::start())
  153. os.run();
  154. Main::cleanup();
  155. for (int i = 0; i < argc; ++i) {
  156. delete[] argv_utf8[i];
  157. }
  158. delete[] argv_utf8;
  159. return os.get_exit_code();
  160. };
  161. __declspec(dllexport) int _main() {
  162. LPWSTR *wc_argv;
  163. int argc;
  164. int result;
  165. wc_argv = CommandLineToArgvW(GetCommandLineW(), &argc);
  166. if (NULL == wc_argv) {
  167. wprintf(L"CommandLineToArgvW failed\n");
  168. return 0;
  169. }
  170. result = widechar_main(argc, wc_argv);
  171. LocalFree(wc_argv);
  172. return result;
  173. }
  174. __declspec(dllexport) int main(int _argc, char **_argv) {
  175. // _argc and _argv are ignored
  176. // we are going to use the WideChar version of them instead
  177. #ifdef CRASH_HANDLER_EXCEPTION
  178. __try {
  179. return _main();
  180. } __except (CrashHandlerException(GetExceptionInformation())) {
  181. return 1;
  182. }
  183. #else
  184. return _main();
  185. #endif
  186. }
  187. HINSTANCE godot_hinstance = NULL;
  188. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  189. godot_hinstance = hInstance;
  190. return main(0, NULL);
  191. }