export_plugin.cpp 25 KB

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