export_plugin.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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("\\", "/").path_join(path);
  52. }
  53. path = path.simplify_path();
  54. path = path.replace("/", "\\");
  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;
  106. src_image.instantiate();
  107. err = ImageLoader::load_image(p_src_path, src_image);
  108. ERR_FAIL_COND_V(err != OK || src_image->is_empty(), ERR_CANT_OPEN);
  109. for (size_t i = 0; i < sizeof(icon_size) / sizeof(icon_size[0]); ++i) {
  110. int size = (icon_size[i] == 0) ? 256 : icon_size[i];
  111. Ref<Image> res_image = src_image->duplicate();
  112. ERR_FAIL_COND_V(res_image.is_null() || res_image->is_empty(), ERR_CANT_OPEN);
  113. res_image->resize(size, size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  114. images[icon_size[i]].data = res_image->save_png_to_buffer();
  115. }
  116. }
  117. uint16_t valid_icon_count = 0;
  118. for (size_t i = 0; i < sizeof(icon_size) / sizeof(icon_size[0]); ++i) {
  119. if (images.has(icon_size[i])) {
  120. valid_icon_count++;
  121. } else {
  122. int size = (icon_size[i] == 0) ? 256 : icon_size[i];
  123. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Icon size \"%d\" is missing."), size));
  124. }
  125. }
  126. ERR_FAIL_COND_V(valid_icon_count == 0, ERR_CANT_OPEN);
  127. Ref<FileAccess> fw = FileAccess::open(p_dst_path, FileAccess::WRITE, &err);
  128. if (err != OK) {
  129. return err;
  130. }
  131. // Write ICONDIR.
  132. fw->store_16(0); // Reserved.
  133. fw->store_16(1); // Image type: 1 - ICO.
  134. fw->store_16(valid_icon_count); // Number of images.
  135. // Write ICONDIRENTRY.
  136. uint32_t img_offset = 6 + 16 * valid_icon_count;
  137. for (size_t i = 0; i < sizeof(icon_size) / sizeof(icon_size[0]); ++i) {
  138. if (images.has(icon_size[i])) {
  139. const IconData &di = images[icon_size[i]];
  140. fw->store_8(icon_size[i]); // Width in pixels.
  141. fw->store_8(icon_size[i]); // Height in pixels.
  142. fw->store_8(di.pal_colors); // Number of colors in the palette (0 - no palette).
  143. fw->store_8(0); // Reserved.
  144. fw->store_16(di.planes); // Number of color planes.
  145. fw->store_16(di.bpp); // Bits per pixel.
  146. fw->store_32(di.data.size()); // Image data size in bytes.
  147. fw->store_32(img_offset); // Image data offset.
  148. img_offset += di.data.size();
  149. }
  150. }
  151. // Write image data.
  152. for (size_t i = 0; i < sizeof(icon_size) / sizeof(icon_size[0]); ++i) {
  153. if (images.has(icon_size[i])) {
  154. const IconData &di = images[icon_size[i]];
  155. fw->store_buffer(di.data.ptr(), di.data.size());
  156. }
  157. }
  158. return OK;
  159. }
  160. Error EditorExportPlatformWindows::sign_shared_object(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) {
  161. if (p_preset->get("codesign/enable")) {
  162. return _code_sign(p_preset, p_path);
  163. } else {
  164. return OK;
  165. }
  166. }
  167. Error EditorExportPlatformWindows::modify_template(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) {
  168. if (p_preset->get("application/modify_resources")) {
  169. _rcedit_add_data(p_preset, p_path, false);
  170. String wrapper_path = p_path.get_basename() + ".console.exe";
  171. if (FileAccess::exists(wrapper_path)) {
  172. _rcedit_add_data(p_preset, wrapper_path, true);
  173. }
  174. }
  175. return OK;
  176. }
  177. Error EditorExportPlatformWindows::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) {
  178. String custom_debug = p_preset->get("custom_template/debug");
  179. String custom_release = p_preset->get("custom_template/release");
  180. String arch = p_preset->get("binary_format/architecture");
  181. String template_path = p_debug ? custom_debug : custom_release;
  182. template_path = template_path.strip_edges();
  183. if (template_path.is_empty()) {
  184. template_path = find_export_template(get_template_file_name(p_debug ? "debug" : "release", arch));
  185. } else {
  186. String exe_arch = _get_exe_arch(template_path);
  187. if (arch != exe_arch) {
  188. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Mismatching custom export template executable architecture: found \"%s\", expected \"%s\"."), exe_arch, arch));
  189. return ERR_CANT_CREATE;
  190. }
  191. }
  192. bool export_as_zip = p_path.ends_with("zip");
  193. bool embedded = p_preset->get("binary_format/embed_pck");
  194. String pkg_name;
  195. if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  196. pkg_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  197. } else {
  198. pkg_name = "Unnamed";
  199. }
  200. pkg_name = OS::get_singleton()->get_safe_dir_name(pkg_name);
  201. // Setup temp folder.
  202. String path = p_path;
  203. String tmp_dir_path = EditorPaths::get_singleton()->get_temp_dir().path_join(pkg_name);
  204. Ref<DirAccess> tmp_app_dir = DirAccess::create_for_path(tmp_dir_path);
  205. if (export_as_zip) {
  206. if (tmp_app_dir.is_null()) {
  207. add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Could not create and open the directory: \"%s\""), tmp_dir_path));
  208. return ERR_CANT_CREATE;
  209. }
  210. if (DirAccess::exists(tmp_dir_path)) {
  211. if (tmp_app_dir->change_dir(tmp_dir_path) == OK) {
  212. tmp_app_dir->erase_contents_recursive();
  213. }
  214. }
  215. tmp_app_dir->make_dir_recursive(tmp_dir_path);
  216. path = tmp_dir_path.path_join(p_path.get_file().get_basename() + ".exe");
  217. }
  218. int export_angle = p_preset->get("application/export_angle");
  219. bool include_angle_libs = false;
  220. if (export_angle == 0) {
  221. include_angle_libs = (String(GLOBAL_GET("rendering/gl_compatibility/driver.windows")) == "opengl3_angle") && (String(GLOBAL_GET("rendering/renderer/rendering_method")) == "gl_compatibility");
  222. } else if (export_angle == 1) {
  223. include_angle_libs = true;
  224. }
  225. if (include_angle_libs) {
  226. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  227. if (da->file_exists(template_path.get_base_dir().path_join("libEGL." + arch + ".dll"))) {
  228. da->copy(template_path.get_base_dir().path_join("libEGL." + arch + ".dll"), path.get_base_dir().path_join("libEGL.dll"), get_chmod_flags());
  229. }
  230. if (da->file_exists(template_path.get_base_dir().path_join("libGLESv2." + arch + ".dll"))) {
  231. da->copy(template_path.get_base_dir().path_join("libGLESv2." + arch + ".dll"), path.get_base_dir().path_join("libGLESv2.dll"), get_chmod_flags());
  232. }
  233. }
  234. int export_d3d12 = p_preset->get("application/export_d3d12");
  235. bool agility_sdk_multiarch = p_preset->get("application/d3d12_agility_sdk_multiarch");
  236. bool include_d3d12_extra_libs = false;
  237. if (export_d3d12 == 0) {
  238. include_d3d12_extra_libs = (String(GLOBAL_GET("rendering/rendering_device/driver.windows")) == "d3d12") && (String(GLOBAL_GET("rendering/renderer/rendering_method")) != "gl_compatibility");
  239. } else if (export_d3d12 == 1) {
  240. include_d3d12_extra_libs = true;
  241. }
  242. if (include_d3d12_extra_libs) {
  243. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  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 (GLOBAL_GET("application/config/windows_native_icon") != "") {
  465. icon_path = GLOBAL_GET("application/config/windows_native_icon");
  466. } else {
  467. icon_path = GLOBAL_GET("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 = EditorSettings::get_singleton()->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. }