godot_win.cpp 5.0 KB

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