export_plugin.cpp 45 KB

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