export_plugin.cpp 43 KB

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