export.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/os/file_access.h"
  31. #include "core/os/os.h"
  32. #include "editor/editor_export.h"
  33. #include "editor/editor_settings.h"
  34. #include "platform/windows/logo.gen.h"
  35. static Error fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size);
  36. class EditorExportPlatformWindows : public EditorExportPlatformPC {
  37. public:
  38. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  39. virtual void get_export_options(List<ExportOption> *r_options);
  40. };
  41. Error EditorExportPlatformWindows::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  42. Error err = EditorExportPlatformPC::export_project(p_preset, p_debug, p_path, p_flags);
  43. if (err != OK) {
  44. return err;
  45. }
  46. String rcedit_path = EditorSettings::get_singleton()->get("export/windows/rcedit");
  47. if (rcedit_path == String()) {
  48. return OK;
  49. }
  50. if (!FileAccess::exists(rcedit_path)) {
  51. ERR_PRINTS("Could not find rcedit executable at " + rcedit_path + ", aborting.");
  52. return ERR_FILE_NOT_FOUND;
  53. }
  54. #ifndef WINDOWS_ENABLED
  55. // On non-Windows we need WINE to run rcedit
  56. String wine_path = EditorSettings::get_singleton()->get("export/windows/wine");
  57. if (wine_path != String() && !FileAccess::exists(wine_path)) {
  58. ERR_PRINTS("Could not find wine executable at " + wine_path + ", aborting.");
  59. return ERR_FILE_NOT_FOUND;
  60. }
  61. if (wine_path == String()) {
  62. wine_path = "wine"; // try to run wine from PATH
  63. }
  64. #endif
  65. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  66. String file_verion = p_preset->get("application/file_version");
  67. String product_version = p_preset->get("application/product_version");
  68. String company_name = p_preset->get("application/company_name");
  69. String product_name = p_preset->get("application/product_name");
  70. String file_description = p_preset->get("application/file_description");
  71. String copyright = p_preset->get("application/copyright");
  72. String trademarks = p_preset->get("application/trademarks");
  73. String comments = p_preset->get("application/comments");
  74. List<String> args;
  75. args.push_back(p_path);
  76. if (icon_path != String()) {
  77. args.push_back("--set-icon");
  78. args.push_back(icon_path);
  79. }
  80. if (file_verion != String()) {
  81. args.push_back("--set-file-version");
  82. args.push_back(file_verion);
  83. }
  84. if (product_version != String()) {
  85. args.push_back("--set-product-version");
  86. args.push_back(product_version);
  87. }
  88. if (company_name != String()) {
  89. args.push_back("--set-version-string");
  90. args.push_back("CompanyName");
  91. args.push_back(company_name);
  92. }
  93. if (product_name != String()) {
  94. args.push_back("--set-version-string");
  95. args.push_back("ProductName");
  96. args.push_back(product_name);
  97. }
  98. if (file_description != String()) {
  99. args.push_back("--set-version-string");
  100. args.push_back("FileDescription");
  101. args.push_back(file_description);
  102. }
  103. if (copyright != String()) {
  104. args.push_back("--set-version-string");
  105. args.push_back("LegalCopyright");
  106. args.push_back(copyright);
  107. }
  108. if (trademarks != String()) {
  109. args.push_back("--set-version-string");
  110. args.push_back("LegalTrademarks");
  111. args.push_back(trademarks);
  112. }
  113. #ifdef WINDOWS_ENABLED
  114. OS::get_singleton()->execute(rcedit_path, args, true);
  115. #else
  116. // On non-Windows we need WINE to run rcedit
  117. args.push_front(rcedit_path);
  118. OS::get_singleton()->execute(wine_path, args, true);
  119. #endif
  120. return OK;
  121. }
  122. void EditorExportPlatformWindows::get_export_options(List<ExportOption> *r_options) {
  123. EditorExportPlatformPC::get_export_options(r_options);
  124. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.ico"), ""));
  125. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0"), ""));
  126. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0"), ""));
  127. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/company_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Company Name"), ""));
  128. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  129. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_description"), ""));
  130. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  131. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/trademarks"), ""));
  132. }
  133. void register_windows_exporter() {
  134. EDITOR_DEF("export/windows/rcedit", "");
  135. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/windows/rcedit", PROPERTY_HINT_GLOBAL_FILE, "*.exe"));
  136. #ifndef WINDOWS_ENABLED
  137. // On non-Windows we need WINE to run rcedit
  138. EDITOR_DEF("export/windows/wine", "");
  139. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/windows/wine", PROPERTY_HINT_GLOBAL_FILE));
  140. #endif
  141. Ref<EditorExportPlatformWindows> platform;
  142. platform.instance();
  143. Ref<Image> img = memnew(Image(_windows_logo));
  144. Ref<ImageTexture> logo;
  145. logo.instance();
  146. logo->create_from_image(img);
  147. platform->set_logo(logo);
  148. platform->set_name("Windows Desktop");
  149. platform->set_extension("exe");
  150. platform->set_release_32("windows_32_release.exe");
  151. platform->set_debug_32("windows_32_debug.exe");
  152. platform->set_release_64("windows_64_release.exe");
  153. platform->set_debug_64("windows_64_debug.exe");
  154. platform->set_os_name("Windows");
  155. platform->set_fixup_embedded_pck_func(&fixup_embedded_pck);
  156. EditorExport::get_singleton()->add_export_platform(platform);
  157. }
  158. static Error fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) {
  159. // Patch the header of the "pck" section in the PE file so that it corresponds to the embedded data
  160. FileAccess *f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  161. if (!f) {
  162. return ERR_CANT_OPEN;
  163. }
  164. // Jump to the PE header and check the magic number
  165. {
  166. f->seek(0x3c);
  167. uint32_t pe_pos = f->get_32();
  168. f->seek(pe_pos);
  169. uint32_t magic = f->get_32();
  170. if (magic != 0x00004550) {
  171. f->close();
  172. return ERR_FILE_CORRUPT;
  173. }
  174. }
  175. // Process header
  176. int num_sections;
  177. {
  178. int64_t header_pos = f->get_position();
  179. f->seek(header_pos + 2);
  180. num_sections = f->get_16();
  181. f->seek(header_pos + 16);
  182. uint16_t opt_header_size = f->get_16();
  183. // Skip rest of header + optional header to go to the section headers
  184. f->seek(f->get_position() + 2 + opt_header_size);
  185. }
  186. // Search for the "pck" section
  187. int64_t section_table_pos = f->get_position();
  188. bool found = false;
  189. for (int i = 0; i < num_sections; ++i) {
  190. int64_t section_header_pos = section_table_pos + i * 40;
  191. f->seek(section_header_pos);
  192. uint8_t section_name[9];
  193. f->get_buffer(section_name, 8);
  194. section_name[8] = '\0';
  195. if (strcmp((char *)section_name, "pck") == 0) {
  196. // "pck" section found, let's patch!
  197. // Set virtual size to a little to avoid it taking memory (zero would give issues)
  198. f->seek(section_header_pos + 8);
  199. f->store_32(8);
  200. f->seek(section_header_pos + 16);
  201. f->store_32(p_embedded_size);
  202. f->seek(section_header_pos + 20);
  203. f->store_32(p_embedded_start);
  204. found = true;
  205. break;
  206. }
  207. }
  208. f->close();
  209. return found ? OK : ERR_FILE_CORRUPT;
  210. }