export_plugin.cpp 43 KB

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