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