export_plugin.cpp 43 KB

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