export_plugin.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /**************************************************************************/
  2. /* export_plugin.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 "export_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/image_loader.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_paths.h"
  35. #include "editor/editor_scale.h"
  36. #include "platform/windows/logo_svg.gen.h"
  37. #include "platform/windows/run_icon_svg.gen.h"
  38. #include "modules/modules_enabled.gen.h" // For svg.
  39. #ifdef MODULE_SVG_ENABLED
  40. #include "modules/svg/image_loader_svg.h"
  41. #endif
  42. Error EditorExportPlatformWindows::_process_icon(const Ref<EditorExportPreset> &p_preset, const String &p_src_path, const String &p_dst_path) {
  43. static const uint8_t icon_size[] = { 16, 32, 48, 64, 128, 0 /*256*/ };
  44. struct IconData {
  45. Vector<uint8_t> data;
  46. uint8_t pal_colors = 0;
  47. uint16_t planes = 0;
  48. uint16_t bpp = 32;
  49. };
  50. HashMap<uint8_t, IconData> images;
  51. Error err;
  52. if (p_src_path.get_extension() == "ico") {
  53. Ref<FileAccess> f = FileAccess::open(p_src_path, FileAccess::READ, &err);
  54. if (err != OK) {
  55. return err;
  56. }
  57. // Read ICONDIR.
  58. f->get_16(); // Reserved.
  59. uint16_t icon_type = f->get_16(); // Image type: 1 - ICO.
  60. uint16_t icon_count = f->get_16(); // Number of images.
  61. ERR_FAIL_COND_V(icon_type != 1, ERR_CANT_OPEN);
  62. for (uint16_t i = 0; i < icon_count; i++) {
  63. // Read ICONDIRENTRY.
  64. uint16_t w = f->get_8(); // Width in pixels.
  65. uint16_t h = f->get_8(); // Height in pixels.
  66. uint8_t pal_colors = f->get_8(); // Number of colors in the palette (0 - no palette).
  67. f->get_8(); // Reserved.
  68. uint16_t planes = f->get_16(); // Number of color planes.
  69. uint16_t bpp = f->get_16(); // Bits per pixel.
  70. uint32_t img_size = f->get_32(); // Image data size in bytes.
  71. uint32_t img_offset = f->get_32(); // Image data offset.
  72. if (w != h) {
  73. continue;
  74. }
  75. // Read image data.
  76. uint64_t prev_offset = f->get_position();
  77. images[w].pal_colors = pal_colors;
  78. images[w].planes = planes;
  79. images[w].bpp = bpp;
  80. images[w].data.resize(img_size);
  81. f->seek(img_offset);
  82. f->get_buffer(images[w].data.ptrw(), img_size);
  83. f->seek(prev_offset);
  84. }
  85. } else {
  86. Ref<Image> src_image;
  87. src_image.instantiate();
  88. err = ImageLoader::load_image(p_src_path, src_image);
  89. ERR_FAIL_COND_V(err != OK || src_image->is_empty(), ERR_CANT_OPEN);
  90. for (size_t i = 0; i < sizeof(icon_size) / sizeof(icon_size[0]); ++i) {
  91. int size = (icon_size[i] == 0) ? 256 : icon_size[i];
  92. Ref<Image> res_image = src_image->duplicate();
  93. ERR_FAIL_COND_V(res_image.is_null() || res_image->is_empty(), ERR_CANT_OPEN);
  94. res_image->resize(size, size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  95. images[icon_size[i]].data = res_image->save_png_to_buffer();
  96. }
  97. }
  98. uint16_t valid_icon_count = 0;
  99. for (size_t i = 0; i < sizeof(icon_size) / sizeof(icon_size[0]); ++i) {
  100. if (images.has(icon_size[i])) {
  101. valid_icon_count++;
  102. } else {
  103. int size = (icon_size[i] == 0) ? 256 : icon_size[i];
  104. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Icon size \"%d\" is missing."), size));
  105. }
  106. }
  107. ERR_FAIL_COND_V(valid_icon_count == 0, ERR_CANT_OPEN);
  108. Ref<FileAccess> fw = FileAccess::open(p_dst_path, FileAccess::WRITE, &err);
  109. if (err != OK) {
  110. return err;
  111. }
  112. // Write ICONDIR.
  113. fw->store_16(0); // Reserved.
  114. fw->store_16(1); // Image type: 1 - ICO.
  115. fw->store_16(valid_icon_count); // Number of images.
  116. // Write ICONDIRENTRY.
  117. uint32_t img_offset = 6 + 16 * valid_icon_count;
  118. for (size_t i = 0; i < sizeof(icon_size) / sizeof(icon_size[0]); ++i) {
  119. if (images.has(icon_size[i])) {
  120. const IconData &di = images[icon_size[i]];
  121. fw->store_8(icon_size[i]); // Width in pixels.
  122. fw->store_8(icon_size[i]); // Height in pixels.
  123. fw->store_8(di.pal_colors); // Number of colors in the palette (0 - no palette).
  124. fw->store_8(0); // Reserved.
  125. fw->store_16(di.planes); // Number of color planes.
  126. fw->store_16(di.bpp); // Bits per pixel.
  127. fw->store_32(di.data.size()); // Image data size in bytes.
  128. fw->store_32(img_offset); // Image data offset.
  129. img_offset += di.data.size();
  130. }
  131. }
  132. // Write image data.
  133. for (size_t i = 0; i < sizeof(icon_size) / sizeof(icon_size[0]); ++i) {
  134. if (images.has(icon_size[i])) {
  135. const IconData &di = images[icon_size[i]];
  136. fw->store_buffer(di.data.ptr(), di.data.size());
  137. }
  138. }
  139. return OK;
  140. }
  141. Error EditorExportPlatformWindows::sign_shared_object(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) {
  142. if (p_preset->get("codesign/enable")) {
  143. return _code_sign(p_preset, p_path);
  144. } else {
  145. return OK;
  146. }
  147. }
  148. Error EditorExportPlatformWindows::modify_template(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  149. if (p_preset->get("application/modify_resources")) {
  150. _rcedit_add_data(p_preset, p_path, true);
  151. String wrapper_path = p_path.get_basename() + ".console.exe";
  152. if (FileAccess::exists(wrapper_path)) {
  153. _rcedit_add_data(p_preset, wrapper_path, false);
  154. }
  155. }
  156. return OK;
  157. }
  158. Error EditorExportPlatformWindows::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  159. bool export_as_zip = p_path.ends_with("zip");
  160. bool embedded = p_preset->get("binary_format/embed_pck");
  161. String pkg_name;
  162. if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  163. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  164. } else {
  165. pkg_name = "Unnamed";
  166. }
  167. pkg_name = OS::get_singleton()->get_safe_dir_name(pkg_name);
  168. // Setup temp folder.
  169. String path = p_path;
  170. String tmp_dir_path = EditorPaths::get_singleton()->get_cache_dir().path_join(pkg_name);
  171. Ref<DirAccess> tmp_app_dir = DirAccess::create_for_path(tmp_dir_path);
  172. if (export_as_zip) {
  173. if (tmp_app_dir.is_null()) {
  174. return ERR_CANT_CREATE;
  175. }
  176. if (DirAccess::exists(tmp_dir_path)) {
  177. if (tmp_app_dir->change_dir(tmp_dir_path) == OK) {
  178. tmp_app_dir->erase_contents_recursive();
  179. }
  180. }
  181. tmp_app_dir->make_dir_recursive(tmp_dir_path);
  182. path = tmp_dir_path.path_join(p_path.get_file().get_basename() + ".exe");
  183. }
  184. // Export project.
  185. String pck_path = path;
  186. if (embedded) {
  187. pck_path = pck_path.get_basename() + ".tmp";
  188. }
  189. Error err = EditorExportPlatformPC::export_project(p_preset, p_debug, pck_path, p_flags);
  190. if (p_preset->get("codesign/enable") && err == OK) {
  191. _code_sign(p_preset, pck_path);
  192. String wrapper_path = p_path.get_basename() + ".console.exe";
  193. if (FileAccess::exists(wrapper_path)) {
  194. _code_sign(p_preset, wrapper_path);
  195. }
  196. }
  197. if (embedded && err == OK) {
  198. Ref<DirAccess> tmp_dir = DirAccess::create_for_path(p_path.get_base_dir());
  199. err = tmp_dir->rename(pck_path, p_path);
  200. if (err != OK) {
  201. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), vformat(TTR("Failed to rename temporary file \"%s\"."), pck_path));
  202. }
  203. }
  204. // ZIP project.
  205. if (export_as_zip) {
  206. if (FileAccess::exists(p_path)) {
  207. OS::get_singleton()->move_to_trash(p_path);
  208. }
  209. Ref<FileAccess> io_fa_dst;
  210. zlib_filefunc_def io_dst = zipio_create_io(&io_fa_dst);
  211. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst);
  212. zip_folder_recursive(zip, tmp_dir_path, "", pkg_name);
  213. zipClose(zip, nullptr);
  214. if (tmp_app_dir->change_dir(tmp_dir_path) == OK) {
  215. tmp_app_dir->erase_contents_recursive();
  216. tmp_app_dir->change_dir("..");
  217. tmp_app_dir->remove(pkg_name);
  218. }
  219. }
  220. return err;
  221. }
  222. String EditorExportPlatformWindows::get_template_file_name(const String &p_target, const String &p_arch) const {
  223. return "windows_" + p_target + "_" + p_arch + ".exe";
  224. }
  225. List<String> EditorExportPlatformWindows::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  226. List<String> list;
  227. list.push_back("exe");
  228. list.push_back("zip");
  229. return list;
  230. }
  231. bool EditorExportPlatformWindows::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  232. // This option is not supported by "osslsigncode", used on non-Windows host.
  233. if (!OS::get_singleton()->has_feature("windows") && p_option == "codesign/identity_type") {
  234. return false;
  235. }
  236. return true;
  237. }
  238. void EditorExportPlatformWindows::get_export_options(List<ExportOption> *r_options) {
  239. EditorExportPlatformPC::get_export_options(r_options);
  240. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "binary_format/architecture", PROPERTY_HINT_ENUM, "x86_64,x86_32,arm64"), "x86_64"));
  241. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), false));
  242. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/identity_type", PROPERTY_HINT_ENUM, "Select automatically,Use PKCS12 file (specify *.PFX/*.P12 file),Use certificate store (specify SHA1 hash)"), 0));
  243. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity", PROPERTY_HINT_GLOBAL_FILE, "*.pfx,*.p12"), ""));
  244. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/password", PROPERTY_HINT_PASSWORD), ""));
  245. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/timestamp"), true));
  246. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/timestamp_server_url"), ""));
  247. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/digest_algorithm", PROPERTY_HINT_ENUM, "SHA1,SHA256"), 1));
  248. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/description"), ""));
  249. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "codesign/custom_options"), PackedStringArray()));
  250. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "application/modify_resources"), true));
  251. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.ico,*.png,*.webp,*.svg"), ""));
  252. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/console_wrapper_icon", PROPERTY_HINT_FILE, "*.ico,*.png,*.webp,*.svg"), ""));
  253. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/icon_interpolation", PROPERTY_HINT_ENUM, "Nearest neighbor,Bilinear,Cubic,Trilinear,Lanczos"), 4));
  254. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0.0"), ""));
  255. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0.0"), ""));
  256. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/company_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Company Name"), ""));
  257. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  258. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_description"), ""));
  259. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  260. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/trademarks"), ""));
  261. String run_script = "Expand-Archive -LiteralPath '{temp_dir}\\{archive_name}' -DestinationPath '{temp_dir}'\n"
  262. "$action = New-ScheduledTaskAction -Execute '{temp_dir}\\{exe_name}' -Argument '{cmd_args}'\n"
  263. "$trigger = New-ScheduledTaskTrigger -Once -At 00:00\n"
  264. "$settings = New-ScheduledTaskSettingsSet\n"
  265. "$task = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings\n"
  266. "Register-ScheduledTask godot_remote_debug -InputObject $task -Force:$true\n"
  267. "Start-ScheduledTask -TaskName godot_remote_debug\n"
  268. "while (Get-ScheduledTask -TaskName godot_remote_debug | ? State -eq running) { Start-Sleep -Milliseconds 100 }\n"
  269. "Unregister-ScheduledTask -TaskName godot_remote_debug -Confirm:$false -ErrorAction:SilentlyContinue";
  270. String cleanup_script = "Stop-ScheduledTask -TaskName godot_remote_debug -ErrorAction:SilentlyContinue\n"
  271. "Unregister-ScheduledTask -TaskName godot_remote_debug -Confirm:$false -ErrorAction:SilentlyContinue\n"
  272. "Remove-Item -Recurse -Force '{temp_dir}'";
  273. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "ssh_remote_deploy/enabled"), false));
  274. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/host"), "user@host_ip"));
  275. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/port"), "22"));
  276. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/extra_args_ssh", PROPERTY_HINT_MULTILINE_TEXT), ""));
  277. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/extra_args_scp", PROPERTY_HINT_MULTILINE_TEXT), ""));
  278. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/run_script", PROPERTY_HINT_MULTILINE_TEXT), run_script));
  279. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/cleanup_script", PROPERTY_HINT_MULTILINE_TEXT), cleanup_script));
  280. }
  281. Error EditorExportPlatformWindows::_rcedit_add_data(const Ref<EditorExportPreset> &p_preset, const String &p_path, bool p_console_icon) {
  282. String rcedit_path = EDITOR_GET("export/windows/rcedit");
  283. if (rcedit_path != String() && !FileAccess::exists(rcedit_path)) {
  284. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Could not find rcedit executable at \"%s\"."), rcedit_path));
  285. return ERR_FILE_NOT_FOUND;
  286. }
  287. if (rcedit_path == String()) {
  288. rcedit_path = "rcedit"; // try to run rcedit from PATH
  289. }
  290. #ifndef WINDOWS_ENABLED
  291. // On non-Windows we need WINE to run rcedit
  292. String wine_path = EDITOR_GET("export/windows/wine");
  293. if (!wine_path.is_empty() && !FileAccess::exists(wine_path)) {
  294. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Could not find wine executable at \"%s\"."), wine_path));
  295. return ERR_FILE_NOT_FOUND;
  296. }
  297. if (wine_path.is_empty()) {
  298. wine_path = "wine"; // try to run wine from PATH
  299. }
  300. #endif
  301. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  302. if (p_console_icon) {
  303. String console_icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/console_wrapper_icon"));
  304. if (!console_icon_path.is_empty() && FileAccess::exists(console_icon_path)) {
  305. icon_path = console_icon_path;
  306. }
  307. }
  308. String tmp_icon_path = EditorPaths::get_singleton()->get_cache_dir().path_join("_rcedit.ico");
  309. if (!icon_path.is_empty()) {
  310. if (_process_icon(p_preset, icon_path, tmp_icon_path) != OK) {
  311. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Invalid icon file \"%s\"."), icon_path));
  312. icon_path = String();
  313. }
  314. }
  315. String file_verion = p_preset->get("application/file_version");
  316. String product_version = p_preset->get("application/product_version");
  317. String company_name = p_preset->get("application/company_name");
  318. String product_name = p_preset->get("application/product_name");
  319. String file_description = p_preset->get("application/file_description");
  320. String copyright = p_preset->get("application/copyright");
  321. String trademarks = p_preset->get("application/trademarks");
  322. String comments = p_preset->get("application/comments");
  323. List<String> args;
  324. args.push_back(p_path);
  325. if (!icon_path.is_empty()) {
  326. args.push_back("--set-icon");
  327. args.push_back(tmp_icon_path);
  328. }
  329. if (!file_verion.is_empty()) {
  330. args.push_back("--set-file-version");
  331. args.push_back(file_verion);
  332. }
  333. if (!product_version.is_empty()) {
  334. args.push_back("--set-product-version");
  335. args.push_back(product_version);
  336. }
  337. if (!company_name.is_empty()) {
  338. args.push_back("--set-version-string");
  339. args.push_back("CompanyName");
  340. args.push_back(company_name);
  341. }
  342. if (!product_name.is_empty()) {
  343. args.push_back("--set-version-string");
  344. args.push_back("ProductName");
  345. args.push_back(product_name);
  346. }
  347. if (!file_description.is_empty()) {
  348. args.push_back("--set-version-string");
  349. args.push_back("FileDescription");
  350. args.push_back(file_description);
  351. }
  352. if (!copyright.is_empty()) {
  353. args.push_back("--set-version-string");
  354. args.push_back("LegalCopyright");
  355. args.push_back(copyright);
  356. }
  357. if (!trademarks.is_empty()) {
  358. args.push_back("--set-version-string");
  359. args.push_back("LegalTrademarks");
  360. args.push_back(trademarks);
  361. }
  362. #ifndef WINDOWS_ENABLED
  363. // On non-Windows we need WINE to run rcedit
  364. args.push_front(rcedit_path);
  365. rcedit_path = wine_path;
  366. #endif
  367. String str;
  368. Error err = OS::get_singleton()->execute(rcedit_path, args, &str, nullptr, true);
  369. if (FileAccess::exists(tmp_icon_path)) {
  370. DirAccess::remove_file_or_error(tmp_icon_path);
  371. }
  372. if (err != OK || (str.find("not found") != -1) || (str.find("not recognized") != -1)) {
  373. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), TTR("Could not start rcedit executable. Configure rcedit path in the Editor Settings (Export > Windows > rcedit), or disable \"Application > Modify Resources\" in the export preset."));
  374. return err;
  375. }
  376. print_line("rcedit (" + p_path + "): " + str);
  377. if (str.find("Fatal error") != -1) {
  378. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("rcedit failed to modify executable: %s."), str));
  379. return FAILED;
  380. }
  381. return OK;
  382. }
  383. Error EditorExportPlatformWindows::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  384. List<String> args;
  385. #ifdef WINDOWS_ENABLED
  386. String signtool_path = EDITOR_GET("export/windows/signtool");
  387. if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) {
  388. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Could not find signtool executable at \"%s\"."), signtool_path));
  389. return ERR_FILE_NOT_FOUND;
  390. }
  391. if (signtool_path.is_empty()) {
  392. signtool_path = "signtool"; // try to run signtool from PATH
  393. }
  394. #else
  395. String signtool_path = EDITOR_GET("export/windows/osslsigncode");
  396. if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) {
  397. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Could not find osslsigncode executable at \"%s\"."), signtool_path));
  398. return ERR_FILE_NOT_FOUND;
  399. }
  400. if (signtool_path.is_empty()) {
  401. signtool_path = "osslsigncode"; // try to run signtool from PATH
  402. }
  403. #endif
  404. args.push_back("sign");
  405. //identity
  406. #ifdef WINDOWS_ENABLED
  407. int id_type = p_preset->get("codesign/identity_type");
  408. if (id_type == 0) { //auto select
  409. args.push_back("/a");
  410. } else if (id_type == 1) { //pkcs12
  411. if (p_preset->get("codesign/identity") != "") {
  412. args.push_back("/f");
  413. args.push_back(p_preset->get("codesign/identity"));
  414. } else {
  415. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found."));
  416. return FAILED;
  417. }
  418. } else if (id_type == 2) { //Windows certificate store
  419. if (p_preset->get("codesign/identity") != "") {
  420. args.push_back("/sha1");
  421. args.push_back(p_preset->get("codesign/identity"));
  422. } else {
  423. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found."));
  424. return FAILED;
  425. }
  426. } else {
  427. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Invalid identity type."));
  428. return FAILED;
  429. }
  430. #else
  431. int id_type = 1;
  432. if (p_preset->get("codesign/identity") != "") {
  433. args.push_back("-pkcs12");
  434. args.push_back(p_preset->get("codesign/identity"));
  435. } else {
  436. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found."));
  437. return FAILED;
  438. }
  439. #endif
  440. //password
  441. if ((id_type == 1) && (p_preset->get("codesign/password") != "")) {
  442. #ifdef WINDOWS_ENABLED
  443. args.push_back("/p");
  444. #else
  445. args.push_back("-pass");
  446. #endif
  447. args.push_back(p_preset->get("codesign/password"));
  448. }
  449. //timestamp
  450. if (p_preset->get("codesign/timestamp")) {
  451. if (p_preset->get("codesign/timestamp_server") != "") {
  452. #ifdef WINDOWS_ENABLED
  453. args.push_back("/tr");
  454. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  455. args.push_back("/td");
  456. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  457. args.push_back("sha1");
  458. } else {
  459. args.push_back("sha256");
  460. }
  461. #else
  462. args.push_back("-ts");
  463. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  464. #endif
  465. } else {
  466. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Invalid timestamp server."));
  467. return FAILED;
  468. }
  469. }
  470. //digest
  471. #ifdef WINDOWS_ENABLED
  472. args.push_back("/fd");
  473. #else
  474. args.push_back("-h");
  475. #endif
  476. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  477. args.push_back("sha1");
  478. } else {
  479. args.push_back("sha256");
  480. }
  481. //description
  482. if (p_preset->get("codesign/description") != "") {
  483. #ifdef WINDOWS_ENABLED
  484. args.push_back("/d");
  485. #else
  486. args.push_back("-n");
  487. #endif
  488. args.push_back(p_preset->get("codesign/description"));
  489. }
  490. //user options
  491. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  492. for (int i = 0; i < user_args.size(); i++) {
  493. String user_arg = user_args[i].strip_edges();
  494. if (!user_arg.is_empty()) {
  495. args.push_back(user_arg);
  496. }
  497. }
  498. #ifndef WINDOWS_ENABLED
  499. args.push_back("-in");
  500. #endif
  501. args.push_back(p_path);
  502. #ifndef WINDOWS_ENABLED
  503. args.push_back("-out");
  504. args.push_back(p_path + "_signed");
  505. #endif
  506. String str;
  507. Error err = OS::get_singleton()->execute(signtool_path, args, &str, nullptr, true);
  508. if (err != OK || (str.find("not found") != -1) || (str.find("not recognized") != -1)) {
  509. #ifdef WINDOWS_ENABLED
  510. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not start signtool executable. Configure signtool path in the Editor Settings (Export > Windows > signtool), or disable \"Codesign\" in the export preset."));
  511. #else
  512. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not start osslsigncode executable. Configure signtool path in the Editor Settings (Export > Windows > osslsigncode), or disable \"Codesign\" in the export preset."));
  513. #endif
  514. return err;
  515. }
  516. print_line("codesign (" + p_path + "): " + str);
  517. #ifndef WINDOWS_ENABLED
  518. if (str.find("SignTool Error") != -1) {
  519. #else
  520. if (str.find("Failed") != -1) {
  521. #endif
  522. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Signtool failed to sign executable: %s."), str));
  523. return FAILED;
  524. }
  525. #ifndef WINDOWS_ENABLED
  526. Ref<DirAccess> tmp_dir = DirAccess::create_for_path(p_path.get_base_dir());
  527. err = tmp_dir->remove(p_path);
  528. if (err != OK) {
  529. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Failed to remove temporary file \"%s\"."), p_path));
  530. return err;
  531. }
  532. err = tmp_dir->rename(p_path + "_signed", p_path);
  533. if (err != OK) {
  534. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Failed to rename temporary file \"%s\"."), p_path + "_signed"));
  535. return err;
  536. }
  537. #endif
  538. return OK;
  539. }
  540. bool EditorExportPlatformWindows::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  541. String err = "";
  542. bool valid = EditorExportPlatformPC::has_valid_export_configuration(p_preset, err, r_missing_templates);
  543. String rcedit_path = EDITOR_GET("export/windows/rcedit");
  544. if (p_preset->get("application/modify_resources") && rcedit_path.is_empty()) {
  545. err += TTR("The rcedit tool must be configured in the Editor Settings (Export > Windows > rcedit) to change the icon or app information data.") + "\n";
  546. }
  547. if (!err.is_empty()) {
  548. r_error = err;
  549. }
  550. return valid;
  551. }
  552. bool EditorExportPlatformWindows::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const {
  553. String err = "";
  554. bool valid = true;
  555. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  556. if (!icon_path.is_empty() && !FileAccess::exists(icon_path)) {
  557. err += TTR("Invalid icon path:") + " " + icon_path + "\n";
  558. }
  559. // Only non-negative integers can exist in the version string.
  560. String file_version = p_preset->get("application/file_version");
  561. if (!file_version.is_empty()) {
  562. PackedStringArray version_array = file_version.split(".", false);
  563. if (version_array.size() != 4 || !version_array[0].is_valid_int() ||
  564. !version_array[1].is_valid_int() || !version_array[2].is_valid_int() ||
  565. !version_array[3].is_valid_int() || file_version.find("-") > -1) {
  566. err += TTR("Invalid file version:") + " " + file_version + "\n";
  567. }
  568. }
  569. String product_version = p_preset->get("application/product_version");
  570. if (!product_version.is_empty()) {
  571. PackedStringArray version_array = product_version.split(".", false);
  572. if (version_array.size() != 4 || !version_array[0].is_valid_int() ||
  573. !version_array[1].is_valid_int() || !version_array[2].is_valid_int() ||
  574. !version_array[3].is_valid_int() || product_version.find("-") > -1) {
  575. err += TTR("Invalid product version:") + " " + product_version + "\n";
  576. }
  577. }
  578. if (!err.is_empty()) {
  579. r_error = err;
  580. }
  581. return valid;
  582. }
  583. Error EditorExportPlatformWindows::fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) {
  584. // Patch the header of the "pck" section in the PE file so that it corresponds to the embedded data
  585. if (p_embedded_size + p_embedded_start >= 0x100000000) { // Check for total executable size
  586. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Windows executables cannot be >= 4 GiB."));
  587. return ERR_INVALID_DATA;
  588. }
  589. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  590. if (f.is_null()) {
  591. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), vformat(TTR("Failed to open executable file \"%s\"."), p_path));
  592. return ERR_CANT_OPEN;
  593. }
  594. // Jump to the PE header and check the magic number
  595. {
  596. f->seek(0x3c);
  597. uint32_t pe_pos = f->get_32();
  598. f->seek(pe_pos);
  599. uint32_t magic = f->get_32();
  600. if (magic != 0x00004550) {
  601. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable file header corrupted."));
  602. return ERR_FILE_CORRUPT;
  603. }
  604. }
  605. // Process header
  606. int num_sections;
  607. {
  608. int64_t header_pos = f->get_position();
  609. f->seek(header_pos + 2);
  610. num_sections = f->get_16();
  611. f->seek(header_pos + 16);
  612. uint16_t opt_header_size = f->get_16();
  613. // Skip rest of header + optional header to go to the section headers
  614. f->seek(f->get_position() + 2 + opt_header_size);
  615. }
  616. // Search for the "pck" section
  617. int64_t section_table_pos = f->get_position();
  618. bool found = false;
  619. for (int i = 0; i < num_sections; ++i) {
  620. int64_t section_header_pos = section_table_pos + i * 40;
  621. f->seek(section_header_pos);
  622. uint8_t section_name[9];
  623. f->get_buffer(section_name, 8);
  624. section_name[8] = '\0';
  625. if (strcmp((char *)section_name, "pck") == 0) {
  626. // "pck" section found, let's patch!
  627. // Set virtual size to a little to avoid it taking memory (zero would give issues)
  628. f->seek(section_header_pos + 8);
  629. f->store_32(8);
  630. f->seek(section_header_pos + 16);
  631. f->store_32(p_embedded_size);
  632. f->seek(section_header_pos + 20);
  633. f->store_32(p_embedded_start);
  634. found = true;
  635. break;
  636. }
  637. }
  638. if (!found) {
  639. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable \"pck\" section not found."));
  640. return ERR_FILE_CORRUPT;
  641. }
  642. return OK;
  643. }
  644. Ref<Texture2D> EditorExportPlatformWindows::get_run_icon() const {
  645. return run_icon;
  646. }
  647. bool EditorExportPlatformWindows::poll_export() {
  648. Ref<EditorExportPreset> preset;
  649. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) {
  650. Ref<EditorExportPreset> ep = EditorExport::get_singleton()->get_export_preset(i);
  651. if (ep->is_runnable() && ep->get_platform() == this) {
  652. preset = ep;
  653. break;
  654. }
  655. }
  656. int prev = menu_options;
  657. menu_options = (preset.is_valid() && preset->get("ssh_remote_deploy/enabled").operator bool());
  658. if (ssh_pid != 0 || !cleanup_commands.is_empty()) {
  659. if (menu_options == 0) {
  660. cleanup();
  661. } else {
  662. menu_options += 1;
  663. }
  664. }
  665. return menu_options != prev;
  666. }
  667. Ref<ImageTexture> EditorExportPlatformWindows::get_option_icon(int p_index) const {
  668. return p_index == 1 ? stop_icon : EditorExportPlatform::get_option_icon(p_index);
  669. }
  670. int EditorExportPlatformWindows::get_options_count() const {
  671. return menu_options;
  672. }
  673. String EditorExportPlatformWindows::get_option_label(int p_index) const {
  674. return (p_index) ? TTR("Stop and uninstall") : TTR("Run on remote Windows system");
  675. }
  676. String EditorExportPlatformWindows::get_option_tooltip(int p_index) const {
  677. return (p_index) ? TTR("Stop and uninstall running project from the remote system") : TTR("Run exported project on remote Windows system");
  678. }
  679. void EditorExportPlatformWindows::cleanup() {
  680. if (ssh_pid != 0 && OS::get_singleton()->is_process_running(ssh_pid)) {
  681. print_line("Terminating connection...");
  682. OS::get_singleton()->kill(ssh_pid);
  683. OS::get_singleton()->delay_usec(1000);
  684. }
  685. if (!cleanup_commands.is_empty()) {
  686. print_line("Stopping and deleting previous version...");
  687. for (const SSHCleanupCommand &cmd : cleanup_commands) {
  688. if (cmd.wait) {
  689. ssh_run_on_remote(cmd.host, cmd.port, cmd.ssh_args, cmd.cmd_args);
  690. } else {
  691. ssh_run_on_remote_no_wait(cmd.host, cmd.port, cmd.ssh_args, cmd.cmd_args);
  692. }
  693. }
  694. }
  695. ssh_pid = 0;
  696. cleanup_commands.clear();
  697. }
  698. Error EditorExportPlatformWindows::run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) {
  699. cleanup();
  700. if (p_device) { // Stop command, cleanup only.
  701. return OK;
  702. }
  703. EditorProgress ep("run", TTR("Running..."), 5);
  704. const String dest = EditorPaths::get_singleton()->get_cache_dir().path_join("windows");
  705. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  706. if (!da->dir_exists(dest)) {
  707. Error err = da->make_dir_recursive(dest);
  708. if (err != OK) {
  709. EditorNode::get_singleton()->show_warning(TTR("Could not create temp directory:") + "\n" + dest);
  710. return err;
  711. }
  712. }
  713. String host = p_preset->get("ssh_remote_deploy/host").operator String();
  714. String port = p_preset->get("ssh_remote_deploy/port").operator String();
  715. if (port.is_empty()) {
  716. port = "22";
  717. }
  718. Vector<String> extra_args_ssh = p_preset->get("ssh_remote_deploy/extra_args_ssh").operator String().split(" ", false);
  719. Vector<String> extra_args_scp = p_preset->get("ssh_remote_deploy/extra_args_scp").operator String().split(" ", false);
  720. const String basepath = dest.path_join("tmp_windows_export");
  721. #define CLEANUP_AND_RETURN(m_err) \
  722. { \
  723. if (da->file_exists(basepath + ".zip")) { \
  724. da->remove(basepath + ".zip"); \
  725. } \
  726. if (da->file_exists(basepath + "_start.ps1")) { \
  727. da->remove(basepath + "_start.ps1"); \
  728. } \
  729. if (da->file_exists(basepath + "_clean.ps1")) { \
  730. da->remove(basepath + "_clean.ps1"); \
  731. } \
  732. return m_err; \
  733. } \
  734. ((void)0)
  735. if (ep.step(TTR("Exporting project..."), 1)) {
  736. return ERR_SKIP;
  737. }
  738. Error err = export_project(p_preset, true, basepath + ".zip", p_debug_flags);
  739. if (err != OK) {
  740. DirAccess::remove_file_or_error(basepath + ".zip");
  741. return err;
  742. }
  743. String cmd_args;
  744. {
  745. Vector<String> cmd_args_list;
  746. gen_debug_flags(cmd_args_list, p_debug_flags);
  747. for (int i = 0; i < cmd_args_list.size(); i++) {
  748. if (i != 0) {
  749. cmd_args += " ";
  750. }
  751. cmd_args += cmd_args_list[i];
  752. }
  753. }
  754. const bool use_remote = (p_debug_flags & DEBUG_FLAG_REMOTE_DEBUG) || (p_debug_flags & DEBUG_FLAG_DUMB_CLIENT);
  755. int dbg_port = EditorSettings::get_singleton()->get("network/debug/remote_port");
  756. print_line("Creating temporary directory...");
  757. ep.step(TTR("Creating temporary directory..."), 2);
  758. String temp_dir;
  759. err = ssh_run_on_remote(host, port, extra_args_ssh, "powershell -command \\\"\\$tmp = Join-Path \\$Env:Temp \\$(New-Guid); New-Item -Type Directory -Path \\$tmp | Out-Null; Write-Output \\$tmp\\\"", &temp_dir);
  760. if (err != OK || temp_dir.is_empty()) {
  761. CLEANUP_AND_RETURN(err);
  762. }
  763. print_line("Uploading archive...");
  764. ep.step(TTR("Uploading archive..."), 3);
  765. err = ssh_push_to_remote(host, port, extra_args_scp, basepath + ".zip", temp_dir);
  766. if (err != OK) {
  767. CLEANUP_AND_RETURN(err);
  768. }
  769. {
  770. String run_script = p_preset->get("ssh_remote_deploy/run_script");
  771. run_script = run_script.replace("{temp_dir}", temp_dir);
  772. run_script = run_script.replace("{archive_name}", basepath.get_file() + ".zip");
  773. run_script = run_script.replace("{exe_name}", basepath.get_file() + ".exe");
  774. run_script = run_script.replace("{cmd_args}", cmd_args);
  775. Ref<FileAccess> f = FileAccess::open(basepath + "_start.ps1", FileAccess::WRITE);
  776. if (f.is_null()) {
  777. CLEANUP_AND_RETURN(err);
  778. }
  779. f->store_string(run_script);
  780. }
  781. {
  782. String clean_script = p_preset->get("ssh_remote_deploy/cleanup_script");
  783. clean_script = clean_script.replace("{temp_dir}", temp_dir);
  784. clean_script = clean_script.replace("{archive_name}", basepath.get_file() + ".zip");
  785. clean_script = clean_script.replace("{exe_name}", basepath.get_file() + ".exe");
  786. clean_script = clean_script.replace("{cmd_args}", cmd_args);
  787. Ref<FileAccess> f = FileAccess::open(basepath + "_clean.ps1", FileAccess::WRITE);
  788. if (f.is_null()) {
  789. CLEANUP_AND_RETURN(err);
  790. }
  791. f->store_string(clean_script);
  792. }
  793. print_line("Uploading scripts...");
  794. ep.step(TTR("Uploading scripts..."), 4);
  795. err = ssh_push_to_remote(host, port, extra_args_scp, basepath + "_start.ps1", temp_dir);
  796. if (err != OK) {
  797. CLEANUP_AND_RETURN(err);
  798. }
  799. err = ssh_push_to_remote(host, port, extra_args_scp, basepath + "_clean.ps1", temp_dir);
  800. if (err != OK) {
  801. CLEANUP_AND_RETURN(err);
  802. }
  803. print_line("Starting project...");
  804. ep.step(TTR("Starting project..."), 5);
  805. err = ssh_run_on_remote_no_wait(host, port, extra_args_ssh, vformat("powershell -file \"%s\\%s\"", temp_dir, basepath.get_file() + "_start.ps1"), &ssh_pid, (use_remote) ? dbg_port : -1);
  806. if (err != OK) {
  807. CLEANUP_AND_RETURN(err);
  808. }
  809. cleanup_commands.clear();
  810. cleanup_commands.push_back(SSHCleanupCommand(host, port, extra_args_ssh, vformat("powershell -file \"%s\\%s\"", temp_dir, basepath.get_file() + "_clean.ps1")));
  811. print_line("Project started.");
  812. CLEANUP_AND_RETURN(OK);
  813. #undef CLEANUP_AND_RETURN
  814. }
  815. EditorExportPlatformWindows::EditorExportPlatformWindows() {
  816. #ifdef MODULE_SVG_ENABLED
  817. Ref<Image> img = memnew(Image);
  818. const bool upsample = !Math::is_equal_approx(Math::round(EDSCALE), EDSCALE);
  819. ImageLoaderSVG img_loader;
  820. img_loader.create_image_from_string(img, _windows_logo_svg, EDSCALE, upsample, false);
  821. set_logo(ImageTexture::create_from_image(img));
  822. img_loader.create_image_from_string(img, _windows_run_icon_svg, EDSCALE, upsample, false);
  823. run_icon = ImageTexture::create_from_image(img);
  824. #endif
  825. Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
  826. if (theme.is_valid()) {
  827. stop_icon = theme->get_icon(SNAME("Stop"), SNAME("EditorIcons"));
  828. } else {
  829. stop_icon.instantiate();
  830. }
  831. }