export_plugin.cpp 47 KB

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