export_plugin.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*************************************************************************/
  2. /* export_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "core/config/project_settings.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_paths.h"
  34. Error EditorExportPlatformWindows::_process_icon(const Ref<EditorExportPreset> &p_preset, const String &p_src_path, const String &p_dst_path) {
  35. static const uint8_t icon_size[] = { 16, 32, 48, 64, 128, 0 /*256*/ };
  36. struct IconData {
  37. Vector<uint8_t> data;
  38. uint8_t pal_colors = 0;
  39. uint16_t planes = 0;
  40. uint16_t bpp = 32;
  41. };
  42. HashMap<uint8_t, IconData> images;
  43. Error err;
  44. if (p_src_path.get_extension() == "ico") {
  45. Ref<FileAccess> f = FileAccess::open(p_src_path, FileAccess::READ, &err);
  46. if (err != OK) {
  47. return err;
  48. }
  49. // Read ICONDIR.
  50. f->get_16(); // Reserved.
  51. uint16_t icon_type = f->get_16(); // Image type: 1 - ICO.
  52. uint16_t icon_count = f->get_16(); // Number of images.
  53. ERR_FAIL_COND_V(icon_type != 1, ERR_CANT_OPEN);
  54. for (uint16_t i = 0; i < icon_count; i++) {
  55. // Read ICONDIRENTRY.
  56. uint16_t w = f->get_8(); // Width in pixels.
  57. uint16_t h = f->get_8(); // Height in pixels.
  58. uint8_t pal_colors = f->get_8(); // Number of colors in the palette (0 - no palette).
  59. f->get_8(); // Reserved.
  60. uint16_t planes = f->get_16(); // Number of color planes.
  61. uint16_t bpp = f->get_16(); // Bits per pixel.
  62. uint32_t img_size = f->get_32(); // Image data size in bytes.
  63. uint32_t img_offset = f->get_32(); // Image data offset.
  64. if (w != h) {
  65. continue;
  66. }
  67. // Read image data.
  68. uint64_t prev_offset = f->get_position();
  69. images[w].pal_colors = pal_colors;
  70. images[w].planes = planes;
  71. images[w].bpp = bpp;
  72. images[w].data.resize(img_size);
  73. f->seek(img_offset);
  74. f->get_buffer(images[w].data.ptrw(), img_size);
  75. f->seek(prev_offset);
  76. }
  77. } else {
  78. Ref<Image> src_image = Image::load_from_file(p_src_path);
  79. ERR_FAIL_COND_V(src_image.is_null() || src_image->is_empty(), ERR_CANT_OPEN);
  80. for (size_t i = 0; i < sizeof(icon_size) / sizeof(icon_size[0]); ++i) {
  81. int size = (icon_size[i] == 0) ? 256 : icon_size[i];
  82. Ref<Image> res_image = src_image->duplicate();
  83. ERR_FAIL_COND_V(res_image.is_null() || res_image->is_empty(), ERR_CANT_OPEN);
  84. res_image->resize(size, size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  85. images[icon_size[i]].data = res_image->save_png_to_buffer();
  86. }
  87. }
  88. uint16_t valid_icon_count = 0;
  89. for (size_t i = 0; i < sizeof(icon_size) / sizeof(icon_size[0]); ++i) {
  90. if (images.has(icon_size[i])) {
  91. valid_icon_count++;
  92. } else {
  93. int size = (icon_size[i] == 0) ? 256 : icon_size[i];
  94. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Icon size \"%d\" is missing."), size));
  95. }
  96. }
  97. ERR_FAIL_COND_V(valid_icon_count == 0, ERR_CANT_OPEN);
  98. Ref<FileAccess> fw = FileAccess::open(p_dst_path, FileAccess::WRITE, &err);
  99. if (err != OK) {
  100. return err;
  101. }
  102. // Write ICONDIR.
  103. fw->store_16(0); // Reserved.
  104. fw->store_16(1); // Image type: 1 - ICO.
  105. fw->store_16(valid_icon_count); // Number of images.
  106. // Write ICONDIRENTRY.
  107. uint32_t img_offset = 6 + 16 * valid_icon_count;
  108. for (size_t i = 0; i < sizeof(icon_size) / sizeof(icon_size[0]); ++i) {
  109. if (images.has(icon_size[i])) {
  110. const IconData &di = images[icon_size[i]];
  111. fw->store_8(icon_size[i]); // Width in pixels.
  112. fw->store_8(icon_size[i]); // Height in pixels.
  113. fw->store_8(di.pal_colors); // Number of colors in the palette (0 - no palette).
  114. fw->store_8(0); // Reserved.
  115. fw->store_16(di.planes); // Number of color planes.
  116. fw->store_16(di.bpp); // Bits per pixel.
  117. fw->store_32(di.data.size()); // Image data size in bytes.
  118. fw->store_32(img_offset); // Image data offset.
  119. img_offset += di.data.size();
  120. }
  121. }
  122. // Write image data.
  123. for (size_t i = 0; i < sizeof(icon_size) / sizeof(icon_size[0]); ++i) {
  124. if (images.has(icon_size[i])) {
  125. const IconData &di = images[icon_size[i]];
  126. fw->store_buffer(di.data.ptr(), di.data.size());
  127. }
  128. }
  129. return OK;
  130. }
  131. Error EditorExportPlatformWindows::sign_shared_object(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) {
  132. if (p_preset->get("codesign/enable")) {
  133. return _code_sign(p_preset, p_path);
  134. } else {
  135. return OK;
  136. }
  137. }
  138. Error EditorExportPlatformWindows::modify_template(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  139. if (p_preset->get("application/modify_resources")) {
  140. _rcedit_add_data(p_preset, p_path, true);
  141. String wrapper_path = p_path.get_basename() + ".console.exe";
  142. if (FileAccess::exists(wrapper_path)) {
  143. _rcedit_add_data(p_preset, wrapper_path, false);
  144. }
  145. }
  146. return OK;
  147. }
  148. Error EditorExportPlatformWindows::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  149. String pck_path = p_path;
  150. if (p_preset->get("binary_format/embed_pck")) {
  151. pck_path = p_path.get_basename() + ".tmp";
  152. }
  153. Error err = EditorExportPlatformPC::export_project(p_preset, p_debug, pck_path, p_flags);
  154. if (p_preset->get("codesign/enable") && err == OK) {
  155. _code_sign(p_preset, pck_path);
  156. String wrapper_path = p_path.get_basename() + ".console.exe";
  157. if (FileAccess::exists(wrapper_path)) {
  158. _code_sign(p_preset, wrapper_path);
  159. }
  160. }
  161. if (p_preset->get("binary_format/embed_pck") && err == OK) {
  162. Ref<DirAccess> tmp_dir = DirAccess::create_for_path(p_path.get_base_dir());
  163. err = tmp_dir->rename(pck_path, p_path);
  164. if (err != OK) {
  165. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), vformat(TTR("Failed to rename temporary file \"%s\"."), pck_path));
  166. }
  167. }
  168. return err;
  169. }
  170. String EditorExportPlatformWindows::get_template_file_name(const String &p_target, const String &p_arch) const {
  171. return "windows_" + p_target + "_" + p_arch + ".exe";
  172. }
  173. List<String> EditorExportPlatformWindows::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  174. List<String> list;
  175. list.push_back("exe");
  176. return list;
  177. }
  178. bool EditorExportPlatformWindows::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  179. // This option is not supported by "osslsigncode", used on non-Windows host.
  180. if (!OS::get_singleton()->has_feature("windows") && p_option == "codesign/identity_type") {
  181. return false;
  182. }
  183. return true;
  184. }
  185. void EditorExportPlatformWindows::get_export_options(List<ExportOption> *r_options) {
  186. EditorExportPlatformPC::get_export_options(r_options);
  187. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "binary_format/architecture", PROPERTY_HINT_ENUM, "x86_64,x86_32,arm64"), "x86_64"));
  188. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), false));
  189. 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 SHA1 hash)"), 0));
  190. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity", PROPERTY_HINT_GLOBAL_FILE, "*.pfx,*.p12"), ""));
  191. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/password", PROPERTY_HINT_PASSWORD), ""));
  192. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/timestamp"), true));
  193. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/timestamp_server_url"), ""));
  194. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/digest_algorithm", PROPERTY_HINT_ENUM, "SHA1,SHA256"), 1));
  195. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/description"), ""));
  196. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "codesign/custom_options"), PackedStringArray()));
  197. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "application/modify_resources"), true));
  198. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.ico,*.png,*.webp,*.svg"), ""));
  199. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/console_wrapper_icon", PROPERTY_HINT_FILE, "*.ico.*.png,*.webp,*.svg"), ""));
  200. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/icon_interpolation", PROPERTY_HINT_ENUM, "Nearest neighbor,Bilinear,Cubic,Trilinear,Lanczos"), 4));
  201. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0.0"), ""));
  202. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0.0"), ""));
  203. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/company_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Company Name"), ""));
  204. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  205. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_description"), ""));
  206. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  207. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/trademarks"), ""));
  208. }
  209. Error EditorExportPlatformWindows::_rcedit_add_data(const Ref<EditorExportPreset> &p_preset, const String &p_path, bool p_console_icon) {
  210. String rcedit_path = EDITOR_GET("export/windows/rcedit");
  211. if (rcedit_path != String() && !FileAccess::exists(rcedit_path)) {
  212. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Could not find rcedit executable at \"%s\"."), rcedit_path));
  213. return ERR_FILE_NOT_FOUND;
  214. }
  215. if (rcedit_path == String()) {
  216. rcedit_path = "rcedit"; // try to run rcedit from PATH
  217. }
  218. #ifndef WINDOWS_ENABLED
  219. // On non-Windows we need WINE to run rcedit
  220. String wine_path = EDITOR_GET("export/windows/wine");
  221. if (!wine_path.is_empty() && !FileAccess::exists(wine_path)) {
  222. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Could not find wine executable at \"%s\"."), wine_path));
  223. return ERR_FILE_NOT_FOUND;
  224. }
  225. if (wine_path.is_empty()) {
  226. wine_path = "wine"; // try to run wine from PATH
  227. }
  228. #endif
  229. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  230. if (p_console_icon) {
  231. String console_icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/console_wrapper_icon"));
  232. if (!console_icon_path.is_empty() && FileAccess::exists(console_icon_path)) {
  233. icon_path = console_icon_path;
  234. }
  235. }
  236. String tmp_icon_path = EditorPaths::get_singleton()->get_cache_dir().path_join("_rcedit.ico");
  237. if (!icon_path.is_empty()) {
  238. if (_process_icon(p_preset, icon_path, tmp_icon_path) != OK) {
  239. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Invalid icon file \"%s\"."), icon_path));
  240. icon_path = String();
  241. }
  242. }
  243. String file_verion = p_preset->get("application/file_version");
  244. String product_version = p_preset->get("application/product_version");
  245. String company_name = p_preset->get("application/company_name");
  246. String product_name = p_preset->get("application/product_name");
  247. String file_description = p_preset->get("application/file_description");
  248. String copyright = p_preset->get("application/copyright");
  249. String trademarks = p_preset->get("application/trademarks");
  250. String comments = p_preset->get("application/comments");
  251. List<String> args;
  252. args.push_back(p_path);
  253. if (!icon_path.is_empty()) {
  254. args.push_back("--set-icon");
  255. args.push_back(tmp_icon_path);
  256. }
  257. if (!file_verion.is_empty()) {
  258. args.push_back("--set-file-version");
  259. args.push_back(file_verion);
  260. }
  261. if (!product_version.is_empty()) {
  262. args.push_back("--set-product-version");
  263. args.push_back(product_version);
  264. }
  265. if (!company_name.is_empty()) {
  266. args.push_back("--set-version-string");
  267. args.push_back("CompanyName");
  268. args.push_back(company_name);
  269. }
  270. if (!product_name.is_empty()) {
  271. args.push_back("--set-version-string");
  272. args.push_back("ProductName");
  273. args.push_back(product_name);
  274. }
  275. if (!file_description.is_empty()) {
  276. args.push_back("--set-version-string");
  277. args.push_back("FileDescription");
  278. args.push_back(file_description);
  279. }
  280. if (!copyright.is_empty()) {
  281. args.push_back("--set-version-string");
  282. args.push_back("LegalCopyright");
  283. args.push_back(copyright);
  284. }
  285. if (!trademarks.is_empty()) {
  286. args.push_back("--set-version-string");
  287. args.push_back("LegalTrademarks");
  288. args.push_back(trademarks);
  289. }
  290. #ifndef WINDOWS_ENABLED
  291. // On non-Windows we need WINE to run rcedit
  292. args.push_front(rcedit_path);
  293. rcedit_path = wine_path;
  294. #endif
  295. String str;
  296. Error err = OS::get_singleton()->execute(rcedit_path, args, &str, nullptr, true);
  297. if (FileAccess::exists(tmp_icon_path)) {
  298. DirAccess::remove_file_or_error(tmp_icon_path);
  299. }
  300. if (err != OK || (str.find("not found") != -1) || (str.find("not recognized") != -1)) {
  301. 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."));
  302. return err;
  303. }
  304. print_line("rcedit (" + p_path + "): " + str);
  305. if (str.find("Fatal error") != -1) {
  306. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("rcedit failed to modify executable: %s."), str));
  307. return FAILED;
  308. }
  309. return OK;
  310. }
  311. Error EditorExportPlatformWindows::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  312. List<String> args;
  313. #ifdef WINDOWS_ENABLED
  314. String signtool_path = EDITOR_GET("export/windows/signtool");
  315. if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) {
  316. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Could not find signtool executable at \"%s\"."), signtool_path));
  317. return ERR_FILE_NOT_FOUND;
  318. }
  319. if (signtool_path.is_empty()) {
  320. signtool_path = "signtool"; // try to run signtool from PATH
  321. }
  322. #else
  323. String signtool_path = EDITOR_GET("export/windows/osslsigncode");
  324. if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) {
  325. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Could not find osslsigncode executable at \"%s\"."), signtool_path));
  326. return ERR_FILE_NOT_FOUND;
  327. }
  328. if (signtool_path.is_empty()) {
  329. signtool_path = "osslsigncode"; // try to run signtool from PATH
  330. }
  331. #endif
  332. args.push_back("sign");
  333. //identity
  334. #ifdef WINDOWS_ENABLED
  335. int id_type = p_preset->get("codesign/identity_type");
  336. if (id_type == 0) { //auto select
  337. args.push_back("/a");
  338. } else if (id_type == 1) { //pkcs12
  339. if (p_preset->get("codesign/identity") != "") {
  340. args.push_back("/f");
  341. args.push_back(p_preset->get("codesign/identity"));
  342. } else {
  343. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found."));
  344. return FAILED;
  345. }
  346. } else if (id_type == 2) { //Windows certificate store
  347. if (p_preset->get("codesign/identity") != "") {
  348. args.push_back("/sha1");
  349. args.push_back(p_preset->get("codesign/identity"));
  350. } else {
  351. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found."));
  352. return FAILED;
  353. }
  354. } else {
  355. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Invalid identity type."));
  356. return FAILED;
  357. }
  358. #else
  359. if (p_preset->get("codesign/identity") != "") {
  360. args.push_back("-pkcs12");
  361. args.push_back(p_preset->get("codesign/identity"));
  362. } else {
  363. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found."));
  364. return FAILED;
  365. }
  366. #endif
  367. //password
  368. if (p_preset->get("codesign/password") != "") {
  369. #ifdef WINDOWS_ENABLED
  370. args.push_back("/p");
  371. #else
  372. args.push_back("-pass");
  373. #endif
  374. args.push_back(p_preset->get("codesign/password"));
  375. }
  376. //timestamp
  377. if (p_preset->get("codesign/timestamp")) {
  378. if (p_preset->get("codesign/timestamp_server") != "") {
  379. #ifdef WINDOWS_ENABLED
  380. args.push_back("/tr");
  381. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  382. args.push_back("/td");
  383. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  384. args.push_back("sha1");
  385. } else {
  386. args.push_back("sha256");
  387. }
  388. #else
  389. args.push_back("-ts");
  390. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  391. #endif
  392. } else {
  393. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Invalid timestamp server."));
  394. return FAILED;
  395. }
  396. }
  397. //digest
  398. #ifdef WINDOWS_ENABLED
  399. args.push_back("/fd");
  400. #else
  401. args.push_back("-h");
  402. #endif
  403. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  404. args.push_back("sha1");
  405. } else {
  406. args.push_back("sha256");
  407. }
  408. //description
  409. if (p_preset->get("codesign/description") != "") {
  410. #ifdef WINDOWS_ENABLED
  411. args.push_back("/d");
  412. #else
  413. args.push_back("-n");
  414. #endif
  415. args.push_back(p_preset->get("codesign/description"));
  416. }
  417. //user options
  418. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  419. for (int i = 0; i < user_args.size(); i++) {
  420. String user_arg = user_args[i].strip_edges();
  421. if (!user_arg.is_empty()) {
  422. args.push_back(user_arg);
  423. }
  424. }
  425. #ifndef WINDOWS_ENABLED
  426. args.push_back("-in");
  427. #endif
  428. args.push_back(p_path);
  429. #ifndef WINDOWS_ENABLED
  430. args.push_back("-out");
  431. args.push_back(p_path + "_signed");
  432. #endif
  433. String str;
  434. Error err = OS::get_singleton()->execute(signtool_path, args, &str, nullptr, true);
  435. if (err != OK || (str.find("not found") != -1) || (str.find("not recognized") != -1)) {
  436. #ifndef WINDOWS_ENABLED
  437. 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."));
  438. #else
  439. 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."));
  440. #endif
  441. return err;
  442. }
  443. print_line("codesign (" + p_path + "): " + str);
  444. #ifndef WINDOWS_ENABLED
  445. if (str.find("SignTool Error") != -1) {
  446. #else
  447. if (str.find("Failed") != -1) {
  448. #endif
  449. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Signtool failed to sign executable: %s."), str));
  450. return FAILED;
  451. }
  452. #ifndef WINDOWS_ENABLED
  453. Ref<DirAccess> tmp_dir = DirAccess::create_for_path(p_path.get_base_dir());
  454. err = tmp_dir->remove(p_path);
  455. if (err != OK) {
  456. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Failed to remove temporary file \"%s\"."), p_path));
  457. return err;
  458. }
  459. err = tmp_dir->rename(p_path + "_signed", p_path);
  460. if (err != OK) {
  461. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Failed to rename temporary file \"%s\"."), p_path + "_signed"));
  462. return err;
  463. }
  464. #endif
  465. return OK;
  466. }
  467. bool EditorExportPlatformWindows::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  468. String err = "";
  469. bool valid = EditorExportPlatformPC::has_valid_export_configuration(p_preset, err, r_missing_templates);
  470. String rcedit_path = EDITOR_GET("export/windows/rcedit");
  471. if (p_preset->get("application/modify_resources") && rcedit_path.is_empty()) {
  472. err += TTR("The rcedit tool must be configured in the Editor Settings (Export > Windows > rcedit) to change the icon or app information data.") + "\n";
  473. }
  474. if (!err.is_empty()) {
  475. r_error = err;
  476. }
  477. return valid;
  478. }
  479. bool EditorExportPlatformWindows::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const {
  480. String err = "";
  481. bool valid = true;
  482. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  483. if (!icon_path.is_empty() && !FileAccess::exists(icon_path)) {
  484. err += TTR("Invalid icon path:") + " " + icon_path + "\n";
  485. }
  486. // Only non-negative integers can exist in the version string.
  487. String file_version = p_preset->get("application/file_version");
  488. if (!file_version.is_empty()) {
  489. PackedStringArray version_array = file_version.split(".", false);
  490. if (version_array.size() != 4 || !version_array[0].is_valid_int() ||
  491. !version_array[1].is_valid_int() || !version_array[2].is_valid_int() ||
  492. !version_array[3].is_valid_int() || file_version.find("-") > -1) {
  493. err += TTR("Invalid file version:") + " " + file_version + "\n";
  494. }
  495. }
  496. String product_version = p_preset->get("application/product_version");
  497. if (!product_version.is_empty()) {
  498. PackedStringArray version_array = product_version.split(".", false);
  499. if (version_array.size() != 4 || !version_array[0].is_valid_int() ||
  500. !version_array[1].is_valid_int() || !version_array[2].is_valid_int() ||
  501. !version_array[3].is_valid_int() || product_version.find("-") > -1) {
  502. err += TTR("Invalid product version:") + " " + product_version + "\n";
  503. }
  504. }
  505. if (!err.is_empty()) {
  506. r_error = err;
  507. }
  508. return valid;
  509. }
  510. Error EditorExportPlatformWindows::fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) {
  511. // Patch the header of the "pck" section in the PE file so that it corresponds to the embedded data
  512. if (p_embedded_size + p_embedded_start >= 0x100000000) { // Check for total executable size
  513. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Windows executables cannot be >= 4 GiB."));
  514. return ERR_INVALID_DATA;
  515. }
  516. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  517. if (f.is_null()) {
  518. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), vformat(TTR("Failed to open executable file \"%s\"."), p_path));
  519. return ERR_CANT_OPEN;
  520. }
  521. // Jump to the PE header and check the magic number
  522. {
  523. f->seek(0x3c);
  524. uint32_t pe_pos = f->get_32();
  525. f->seek(pe_pos);
  526. uint32_t magic = f->get_32();
  527. if (magic != 0x00004550) {
  528. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable file header corrupted."));
  529. return ERR_FILE_CORRUPT;
  530. }
  531. }
  532. // Process header
  533. int num_sections;
  534. {
  535. int64_t header_pos = f->get_position();
  536. f->seek(header_pos + 2);
  537. num_sections = f->get_16();
  538. f->seek(header_pos + 16);
  539. uint16_t opt_header_size = f->get_16();
  540. // Skip rest of header + optional header to go to the section headers
  541. f->seek(f->get_position() + 2 + opt_header_size);
  542. }
  543. // Search for the "pck" section
  544. int64_t section_table_pos = f->get_position();
  545. bool found = false;
  546. for (int i = 0; i < num_sections; ++i) {
  547. int64_t section_header_pos = section_table_pos + i * 40;
  548. f->seek(section_header_pos);
  549. uint8_t section_name[9];
  550. f->get_buffer(section_name, 8);
  551. section_name[8] = '\0';
  552. if (strcmp((char *)section_name, "pck") == 0) {
  553. // "pck" section found, let's patch!
  554. // Set virtual size to a little to avoid it taking memory (zero would give issues)
  555. f->seek(section_header_pos + 8);
  556. f->store_32(8);
  557. f->seek(section_header_pos + 16);
  558. f->store_32(p_embedded_size);
  559. f->seek(section_header_pos + 20);
  560. f->store_32(p_embedded_start);
  561. found = true;
  562. break;
  563. }
  564. }
  565. if (!found) {
  566. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable \"pck\" section not found."));
  567. return ERR_FILE_CORRUPT;
  568. }
  569. return OK;
  570. }