export_plugin.cpp 45 KB

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