export.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "export.h"
  31. #include "export_plugin.h"
  32. static Error fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size);
  33. void register_windows_exporter() {
  34. EDITOR_DEF("export/windows/rcedit", "");
  35. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/windows/rcedit", PROPERTY_HINT_GLOBAL_FILE, "*.exe"));
  36. #ifdef WINDOWS_ENABLED
  37. EDITOR_DEF("export/windows/signtool", "");
  38. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/windows/signtool", PROPERTY_HINT_GLOBAL_FILE, "*.exe"));
  39. #else
  40. EDITOR_DEF("export/windows/osslsigncode", "");
  41. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/windows/osslsigncode", PROPERTY_HINT_GLOBAL_FILE));
  42. // On non-Windows we need WINE to run rcedit
  43. EDITOR_DEF("export/windows/wine", "");
  44. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/windows/wine", PROPERTY_HINT_GLOBAL_FILE));
  45. #endif
  46. Ref<EditorExportPlatformWindows> platform;
  47. platform.instantiate();
  48. Ref<Image> img = memnew(Image(_windows_logo));
  49. Ref<ImageTexture> logo;
  50. logo.instantiate();
  51. logo->create_from_image(img);
  52. platform->set_logo(logo);
  53. platform->set_name("Windows Desktop");
  54. platform->set_extension("exe");
  55. platform->set_release_32("windows_32_release.exe");
  56. platform->set_debug_32("windows_32_debug.exe");
  57. platform->set_release_64("windows_64_release.exe");
  58. platform->set_debug_64("windows_64_debug.exe");
  59. platform->set_os_name("Windows");
  60. platform->set_fixup_embedded_pck_func(&fixup_embedded_pck);
  61. EditorExport::get_singleton()->add_export_platform(platform);
  62. }
  63. static Error fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) {
  64. // Patch the header of the "pck" section in the PE file so that it corresponds to the embedded data
  65. FileAccess *f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  66. if (!f) {
  67. return ERR_CANT_OPEN;
  68. }
  69. // Jump to the PE header and check the magic number
  70. {
  71. f->seek(0x3c);
  72. uint32_t pe_pos = f->get_32();
  73. f->seek(pe_pos);
  74. uint32_t magic = f->get_32();
  75. if (magic != 0x00004550) {
  76. f->close();
  77. return ERR_FILE_CORRUPT;
  78. }
  79. }
  80. // Process header
  81. int num_sections;
  82. {
  83. int64_t header_pos = f->get_position();
  84. f->seek(header_pos + 2);
  85. num_sections = f->get_16();
  86. f->seek(header_pos + 16);
  87. uint16_t opt_header_size = f->get_16();
  88. // Skip rest of header + optional header to go to the section headers
  89. f->seek(f->get_position() + 2 + opt_header_size);
  90. }
  91. // Search for the "pck" section
  92. int64_t section_table_pos = f->get_position();
  93. bool found = false;
  94. for (int i = 0; i < num_sections; ++i) {
  95. int64_t section_header_pos = section_table_pos + i * 40;
  96. f->seek(section_header_pos);
  97. uint8_t section_name[9];
  98. f->get_buffer(section_name, 8);
  99. section_name[8] = '\0';
  100. if (strcmp((char *)section_name, "pck") == 0) {
  101. // "pck" section found, let's patch!
  102. // Set virtual size to a little to avoid it taking memory (zero would give issues)
  103. f->seek(section_header_pos + 8);
  104. f->store_32(8);
  105. f->seek(section_header_pos + 16);
  106. f->store_32(p_embedded_size);
  107. f->seek(section_header_pos + 20);
  108. f->store_32(p_embedded_start);
  109. found = true;
  110. break;
  111. }
  112. }
  113. f->close();
  114. return found ? OK : ERR_FILE_CORRUPT;
  115. }