godot_windows.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <clocale>
  33. #include <cstdio>
  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. char *wc_to_utf8(const wchar_t *wc) {
  54. int ulen = WideCharToMultiByte(CP_UTF8, 0, wc, -1, nullptr, 0, nullptr, nullptr);
  55. char *ubuf = new char[ulen + 1];
  56. WideCharToMultiByte(CP_UTF8, 0, wc, -1, ubuf, ulen, nullptr, nullptr);
  57. ubuf[ulen] = 0;
  58. return ubuf;
  59. }
  60. int widechar_main(int argc, wchar_t **argv) {
  61. OS_Windows os(nullptr);
  62. setlocale(LC_CTYPE, "");
  63. char **argv_utf8 = new char *[argc];
  64. for (int i = 0; i < argc; ++i) {
  65. argv_utf8[i] = wc_to_utf8(argv[i]);
  66. }
  67. TEST_MAIN_PARAM_OVERRIDE(argc, argv_utf8)
  68. Error err = Main::setup(argv_utf8[0], argc - 1, &argv_utf8[1]);
  69. if (err != OK) {
  70. for (int i = 0; i < argc; ++i) {
  71. delete[] argv_utf8[i];
  72. }
  73. delete[] argv_utf8;
  74. if (err == ERR_HELP) { // Returned by --help and --version, so success.
  75. return EXIT_SUCCESS;
  76. }
  77. return EXIT_FAILURE;
  78. }
  79. if (Main::start() == EXIT_SUCCESS) {
  80. os.run();
  81. } else {
  82. os.set_exit_code(EXIT_FAILURE);
  83. }
  84. Main::cleanup();
  85. for (int i = 0; i < argc; ++i) {
  86. delete[] argv_utf8[i];
  87. }
  88. delete[] argv_utf8;
  89. return os.get_exit_code();
  90. }
  91. int _main() {
  92. LPWSTR *wc_argv;
  93. int argc;
  94. int result;
  95. wc_argv = CommandLineToArgvW(GetCommandLineW(), &argc);
  96. if (nullptr == wc_argv) {
  97. wprintf(L"CommandLineToArgvW failed\n");
  98. return 0;
  99. }
  100. result = widechar_main(argc, wc_argv);
  101. LocalFree(wc_argv);
  102. return result;
  103. }
  104. int main(int argc, char **argv) {
  105. // override the arguments for the test handler / if symbol is provided
  106. // TEST_MAIN_OVERRIDE
  107. // _argc and _argv are ignored
  108. // we are going to use the WideChar version of them instead
  109. #if defined(CRASH_HANDLER_EXCEPTION) && defined(_MSC_VER)
  110. __try {
  111. return _main();
  112. } __except (CrashHandlerException(GetExceptionInformation())) {
  113. return 1;
  114. }
  115. #else
  116. return _main();
  117. #endif
  118. }
  119. HINSTANCE godot_hinstance = nullptr;
  120. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  121. godot_hinstance = hInstance;
  122. return main(0, nullptr);
  123. }