export_plugin.cpp 18 KB

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