export_plugin.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. Error EditorExportPlatformWindows::sign_shared_object(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) {
  34. if (p_preset->get("codesign/enable")) {
  35. return _code_sign(p_preset, p_path);
  36. } else {
  37. return OK;
  38. }
  39. }
  40. Error EditorExportPlatformWindows::_export_debug_script(const Ref<EditorExportPreset> &p_preset, const String &p_app_name, const String &p_pkg_name, const String &p_path) {
  41. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::WRITE);
  42. if (f.is_null()) {
  43. add_message(EXPORT_MESSAGE_ERROR, TTR("Debug Script Export"), vformat(TTR("Could not open file \"%s\"."), p_path));
  44. return ERR_CANT_CREATE;
  45. }
  46. f->store_line("@echo off");
  47. f->store_line("title \"" + p_app_name + "\"");
  48. f->store_line("\"%~dp0" + p_pkg_name + "\" \"%*\"");
  49. f->store_line("pause > nul");
  50. return OK;
  51. }
  52. Error EditorExportPlatformWindows::modify_template(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  53. if (p_preset->get("application/modify_resources")) {
  54. _rcedit_add_data(p_preset, p_path);
  55. }
  56. return OK;
  57. }
  58. Error EditorExportPlatformWindows::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  59. String pck_path = p_path;
  60. if (p_preset->get("binary_format/embed_pck")) {
  61. pck_path = p_path.get_basename() + ".tmp";
  62. }
  63. Error err = EditorExportPlatformPC::export_project(p_preset, p_debug, pck_path, p_flags);
  64. if (p_preset->get("codesign/enable") && err == OK) {
  65. _code_sign(p_preset, pck_path);
  66. }
  67. if (p_preset->get("binary_format/embed_pck") && err == OK) {
  68. Ref<DirAccess> tmp_dir = DirAccess::create_for_path(p_path.get_base_dir());
  69. err = tmp_dir->rename(pck_path, p_path);
  70. if (err != OK) {
  71. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), vformat(TTR("Failed to rename temporary file \"%s\"."), pck_path));
  72. }
  73. }
  74. String app_name;
  75. if (String(GLOBAL_GET("application/config/name")) != "") {
  76. app_name = String(GLOBAL_GET("application/config/name"));
  77. } else {
  78. app_name = "Unnamed";
  79. }
  80. app_name = OS::get_singleton()->get_safe_dir_name(app_name);
  81. // Save console script.
  82. if (err == OK) {
  83. int con_scr = p_preset->get("debug/export_console_script");
  84. if ((con_scr == 1 && p_debug) || (con_scr == 2)) {
  85. String scr_path = p_path.get_basename() + ".cmd";
  86. if (_export_debug_script(p_preset, app_name, p_path.get_file(), scr_path) != OK) {
  87. add_message(EXPORT_MESSAGE_ERROR, TTR("Debug Script Export"), TTR("Could not create console script."));
  88. }
  89. }
  90. }
  91. return err;
  92. }
  93. String EditorExportPlatformWindows::get_template_file_name(const String &p_target, const String &p_arch) const {
  94. return "windows_" + p_target + "_" + p_arch + ".exe";
  95. }
  96. List<String> EditorExportPlatformWindows::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  97. List<String> list;
  98. list.push_back("exe");
  99. return list;
  100. }
  101. bool EditorExportPlatformWindows::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  102. // This option is not supported by "osslsigncode", used on non-Windows host.
  103. if (!OS::get_singleton()->has_feature("windows") && p_option == "codesign/identity_type") {
  104. return false;
  105. }
  106. return true;
  107. }
  108. void EditorExportPlatformWindows::get_export_options(List<ExportOption> *r_options) {
  109. EditorExportPlatformPC::get_export_options(r_options);
  110. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "binary_format/architecture", PROPERTY_HINT_ENUM, "x86_64,x86_32,arm64"), "x86_64"));
  111. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), false));
  112. 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));
  113. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity", PROPERTY_HINT_GLOBAL_FILE, "*.pfx,*.p12"), ""));
  114. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/password", PROPERTY_HINT_PASSWORD), ""));
  115. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/timestamp"), true));
  116. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/timestamp_server_url"), ""));
  117. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/digest_algorithm", PROPERTY_HINT_ENUM, "SHA1,SHA256"), 1));
  118. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/description"), ""));
  119. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "codesign/custom_options"), PackedStringArray()));
  120. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "application/modify_resources"), true));
  121. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.ico"), ""));
  122. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0.0"), ""));
  123. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0.0"), ""));
  124. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/company_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Company Name"), ""));
  125. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  126. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_description"), ""));
  127. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  128. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/trademarks"), ""));
  129. }
  130. Error EditorExportPlatformWindows::_rcedit_add_data(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  131. String rcedit_path = EDITOR_GET("export/windows/rcedit");
  132. if (rcedit_path != String() && !FileAccess::exists(rcedit_path)) {
  133. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Could not find rcedit executable at \"%s\"."), rcedit_path));
  134. return ERR_FILE_NOT_FOUND;
  135. }
  136. if (rcedit_path == String()) {
  137. rcedit_path = "rcedit"; // try to run rcedit from PATH
  138. }
  139. #ifndef WINDOWS_ENABLED
  140. // On non-Windows we need WINE to run rcedit
  141. String wine_path = EDITOR_GET("export/windows/wine");
  142. if (!wine_path.is_empty() && !FileAccess::exists(wine_path)) {
  143. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Could not find wine executable at \"%s\"."), wine_path));
  144. return ERR_FILE_NOT_FOUND;
  145. }
  146. if (wine_path.is_empty()) {
  147. wine_path = "wine"; // try to run wine from PATH
  148. }
  149. #endif
  150. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  151. String file_verion = p_preset->get("application/file_version");
  152. String product_version = p_preset->get("application/product_version");
  153. String company_name = p_preset->get("application/company_name");
  154. String product_name = p_preset->get("application/product_name");
  155. String file_description = p_preset->get("application/file_description");
  156. String copyright = p_preset->get("application/copyright");
  157. String trademarks = p_preset->get("application/trademarks");
  158. String comments = p_preset->get("application/comments");
  159. List<String> args;
  160. args.push_back(p_path);
  161. if (!icon_path.is_empty()) {
  162. args.push_back("--set-icon");
  163. args.push_back(icon_path);
  164. }
  165. if (!file_verion.is_empty()) {
  166. args.push_back("--set-file-version");
  167. args.push_back(file_verion);
  168. }
  169. if (!product_version.is_empty()) {
  170. args.push_back("--set-product-version");
  171. args.push_back(product_version);
  172. }
  173. if (!company_name.is_empty()) {
  174. args.push_back("--set-version-string");
  175. args.push_back("CompanyName");
  176. args.push_back(company_name);
  177. }
  178. if (!product_name.is_empty()) {
  179. args.push_back("--set-version-string");
  180. args.push_back("ProductName");
  181. args.push_back(product_name);
  182. }
  183. if (!file_description.is_empty()) {
  184. args.push_back("--set-version-string");
  185. args.push_back("FileDescription");
  186. args.push_back(file_description);
  187. }
  188. if (!copyright.is_empty()) {
  189. args.push_back("--set-version-string");
  190. args.push_back("LegalCopyright");
  191. args.push_back(copyright);
  192. }
  193. if (!trademarks.is_empty()) {
  194. args.push_back("--set-version-string");
  195. args.push_back("LegalTrademarks");
  196. args.push_back(trademarks);
  197. }
  198. #ifndef WINDOWS_ENABLED
  199. // On non-Windows we need WINE to run rcedit
  200. args.push_front(rcedit_path);
  201. rcedit_path = wine_path;
  202. #endif
  203. String str;
  204. Error err = OS::get_singleton()->execute(rcedit_path, args, &str, nullptr, true);
  205. if (err != OK || (str.find("not found") != -1) || (str.find("not recognized") != -1)) {
  206. 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."));
  207. return err;
  208. }
  209. print_line("rcedit (" + p_path + "): " + str);
  210. if (str.find("Fatal error") != -1) {
  211. add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("rcedit failed to modify executable: %s."), str));
  212. return FAILED;
  213. }
  214. return OK;
  215. }
  216. Error EditorExportPlatformWindows::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  217. List<String> args;
  218. #ifdef WINDOWS_ENABLED
  219. String signtool_path = EDITOR_GET("export/windows/signtool");
  220. if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) {
  221. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Could not find signtool executable at \"%s\"."), signtool_path));
  222. return ERR_FILE_NOT_FOUND;
  223. }
  224. if (signtool_path.is_empty()) {
  225. signtool_path = "signtool"; // try to run signtool from PATH
  226. }
  227. #else
  228. String signtool_path = EDITOR_GET("export/windows/osslsigncode");
  229. if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) {
  230. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Could not find osslsigncode executable at \"%s\"."), signtool_path));
  231. return ERR_FILE_NOT_FOUND;
  232. }
  233. if (signtool_path.is_empty()) {
  234. signtool_path = "osslsigncode"; // try to run signtool from PATH
  235. }
  236. #endif
  237. args.push_back("sign");
  238. //identity
  239. #ifdef WINDOWS_ENABLED
  240. int id_type = p_preset->get("codesign/identity_type");
  241. if (id_type == 0) { //auto select
  242. args.push_back("/a");
  243. } else if (id_type == 1) { //pkcs12
  244. if (p_preset->get("codesign/identity") != "") {
  245. args.push_back("/f");
  246. args.push_back(p_preset->get("codesign/identity"));
  247. } else {
  248. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found."));
  249. return FAILED;
  250. }
  251. } else if (id_type == 2) { //Windows certificate store
  252. if (p_preset->get("codesign/identity") != "") {
  253. args.push_back("/sha1");
  254. args.push_back(p_preset->get("codesign/identity"));
  255. } else {
  256. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found."));
  257. return FAILED;
  258. }
  259. } else {
  260. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Invalid identity type."));
  261. return FAILED;
  262. }
  263. #else
  264. if (p_preset->get("codesign/identity") != "") {
  265. args.push_back("-pkcs12");
  266. args.push_back(p_preset->get("codesign/identity"));
  267. } else {
  268. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found."));
  269. return FAILED;
  270. }
  271. #endif
  272. //password
  273. if (p_preset->get("codesign/password") != "") {
  274. #ifdef WINDOWS_ENABLED
  275. args.push_back("/p");
  276. #else
  277. args.push_back("-pass");
  278. #endif
  279. args.push_back(p_preset->get("codesign/password"));
  280. }
  281. //timestamp
  282. if (p_preset->get("codesign/timestamp")) {
  283. if (p_preset->get("codesign/timestamp_server") != "") {
  284. #ifdef WINDOWS_ENABLED
  285. args.push_back("/tr");
  286. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  287. args.push_back("/td");
  288. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  289. args.push_back("sha1");
  290. } else {
  291. args.push_back("sha256");
  292. }
  293. #else
  294. args.push_back("-ts");
  295. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  296. #endif
  297. } else {
  298. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Invalid timestamp server."));
  299. return FAILED;
  300. }
  301. }
  302. //digest
  303. #ifdef WINDOWS_ENABLED
  304. args.push_back("/fd");
  305. #else
  306. args.push_back("-h");
  307. #endif
  308. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  309. args.push_back("sha1");
  310. } else {
  311. args.push_back("sha256");
  312. }
  313. //description
  314. if (p_preset->get("codesign/description") != "") {
  315. #ifdef WINDOWS_ENABLED
  316. args.push_back("/d");
  317. #else
  318. args.push_back("-n");
  319. #endif
  320. args.push_back(p_preset->get("codesign/description"));
  321. }
  322. //user options
  323. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  324. for (int i = 0; i < user_args.size(); i++) {
  325. String user_arg = user_args[i].strip_edges();
  326. if (!user_arg.is_empty()) {
  327. args.push_back(user_arg);
  328. }
  329. }
  330. #ifndef WINDOWS_ENABLED
  331. args.push_back("-in");
  332. #endif
  333. args.push_back(p_path);
  334. #ifndef WINDOWS_ENABLED
  335. args.push_back("-out");
  336. args.push_back(p_path + "_signed");
  337. #endif
  338. String str;
  339. Error err = OS::get_singleton()->execute(signtool_path, args, &str, nullptr, true);
  340. if (err != OK || (str.find("not found") != -1) || (str.find("not recognized") != -1)) {
  341. #ifndef WINDOWS_ENABLED
  342. 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."));
  343. #else
  344. 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."));
  345. #endif
  346. return err;
  347. }
  348. print_line("codesign (" + p_path + "): " + str);
  349. #ifndef WINDOWS_ENABLED
  350. if (str.find("SignTool Error") != -1) {
  351. #else
  352. if (str.find("Failed") != -1) {
  353. #endif
  354. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Signtool failed to sign executable: %s."), str));
  355. return FAILED;
  356. }
  357. #ifndef WINDOWS_ENABLED
  358. Ref<DirAccess> tmp_dir = DirAccess::create_for_path(p_path.get_base_dir());
  359. err = tmp_dir->remove(p_path);
  360. if (err != OK) {
  361. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Failed to remove temporary file \"%s\"."), p_path));
  362. return err;
  363. }
  364. err = tmp_dir->rename(p_path + "_signed", p_path);
  365. if (err != OK) {
  366. add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Failed to rename temporary file \"%s\"."), p_path + "_signed"));
  367. return err;
  368. }
  369. #endif
  370. return OK;
  371. }
  372. bool EditorExportPlatformWindows::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  373. String err = "";
  374. bool valid = EditorExportPlatformPC::has_valid_export_configuration(p_preset, err, r_missing_templates);
  375. String rcedit_path = EDITOR_GET("export/windows/rcedit");
  376. if (p_preset->get("application/modify_resources") && rcedit_path.is_empty()) {
  377. err += TTR("The rcedit tool must be configured in the Editor Settings (Export > Windows > rcedit) to change the icon or app information data.") + "\n";
  378. }
  379. if (!err.is_empty()) {
  380. r_error = err;
  381. }
  382. return valid;
  383. }
  384. bool EditorExportPlatformWindows::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const {
  385. String err = "";
  386. bool valid = true;
  387. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  388. if (!icon_path.is_empty() && !FileAccess::exists(icon_path)) {
  389. err += TTR("Invalid icon path:") + " " + icon_path + "\n";
  390. }
  391. // Only non-negative integers can exist in the version string.
  392. String file_version = p_preset->get("application/file_version");
  393. if (!file_version.is_empty()) {
  394. PackedStringArray version_array = file_version.split(".", false);
  395. if (version_array.size() != 4 || !version_array[0].is_valid_int() ||
  396. !version_array[1].is_valid_int() || !version_array[2].is_valid_int() ||
  397. !version_array[3].is_valid_int() || file_version.find("-") > -1) {
  398. err += TTR("Invalid file version:") + " " + file_version + "\n";
  399. }
  400. }
  401. String product_version = p_preset->get("application/product_version");
  402. if (!product_version.is_empty()) {
  403. PackedStringArray version_array = product_version.split(".", false);
  404. if (version_array.size() != 4 || !version_array[0].is_valid_int() ||
  405. !version_array[1].is_valid_int() || !version_array[2].is_valid_int() ||
  406. !version_array[3].is_valid_int() || product_version.find("-") > -1) {
  407. err += TTR("Invalid product version:") + " " + product_version + "\n";
  408. }
  409. }
  410. if (!err.is_empty()) {
  411. r_error = err;
  412. }
  413. return valid;
  414. }
  415. Error EditorExportPlatformWindows::fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) {
  416. // Patch the header of the "pck" section in the PE file so that it corresponds to the embedded data
  417. if (p_embedded_size + p_embedded_start >= 0x100000000) { // Check for total executable size
  418. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Windows executables cannot be >= 4 GiB."));
  419. return ERR_INVALID_DATA;
  420. }
  421. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  422. if (f.is_null()) {
  423. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), vformat(TTR("Failed to open executable file \"%s\"."), p_path));
  424. return ERR_CANT_OPEN;
  425. }
  426. // Jump to the PE header and check the magic number
  427. {
  428. f->seek(0x3c);
  429. uint32_t pe_pos = f->get_32();
  430. f->seek(pe_pos);
  431. uint32_t magic = f->get_32();
  432. if (magic != 0x00004550) {
  433. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable file header corrupted."));
  434. return ERR_FILE_CORRUPT;
  435. }
  436. }
  437. // Process header
  438. int num_sections;
  439. {
  440. int64_t header_pos = f->get_position();
  441. f->seek(header_pos + 2);
  442. num_sections = f->get_16();
  443. f->seek(header_pos + 16);
  444. uint16_t opt_header_size = f->get_16();
  445. // Skip rest of header + optional header to go to the section headers
  446. f->seek(f->get_position() + 2 + opt_header_size);
  447. }
  448. // Search for the "pck" section
  449. int64_t section_table_pos = f->get_position();
  450. bool found = false;
  451. for (int i = 0; i < num_sections; ++i) {
  452. int64_t section_header_pos = section_table_pos + i * 40;
  453. f->seek(section_header_pos);
  454. uint8_t section_name[9];
  455. f->get_buffer(section_name, 8);
  456. section_name[8] = '\0';
  457. if (strcmp((char *)section_name, "pck") == 0) {
  458. // "pck" section found, let's patch!
  459. // Set virtual size to a little to avoid it taking memory (zero would give issues)
  460. f->seek(section_header_pos + 8);
  461. f->store_32(8);
  462. f->seek(section_header_pos + 16);
  463. f->store_32(p_embedded_size);
  464. f->seek(section_header_pos + 20);
  465. f->store_32(p_embedded_start);
  466. found = true;
  467. break;
  468. }
  469. }
  470. if (!found) {
  471. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable \"pck\" section not found."));
  472. return ERR_FILE_CORRUPT;
  473. }
  474. return OK;
  475. }