export.cpp 27 KB

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