export_plugin.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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.is_empty()) {
  124. WARN_PRINT("The rcedit tool is not configured in the Editor Settings (Export > Windows > Rcedit). No custom icon or app information data will be embedded in the exported executable.");
  125. return ERR_FILE_NOT_FOUND;
  126. }
  127. if (!FileAccess::exists(rcedit_path)) {
  128. ERR_PRINT("Could not find rcedit executable at " + rcedit_path + ", no icon or app information data will be included.");
  129. return ERR_FILE_NOT_FOUND;
  130. }
  131. if (rcedit_path.is_empty()) {
  132. rcedit_path = "rcedit"; // try to run signtool from PATH
  133. }
  134. #ifndef WINDOWS_ENABLED
  135. // On non-Windows we need WINE to run rcedit
  136. String wine_path = EditorSettings::get_singleton()->get("export/windows/wine");
  137. if (!wine_path.is_empty() && !FileAccess::exists(wine_path)) {
  138. ERR_PRINT("Could not find wine executable at " + wine_path + ", no icon or app information data will be included.");
  139. return ERR_FILE_NOT_FOUND;
  140. }
  141. if (wine_path.is_empty()) {
  142. wine_path = "wine"; // try to run wine from PATH
  143. }
  144. #endif
  145. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  146. String file_verion = p_preset->get("application/file_version");
  147. String product_version = p_preset->get("application/product_version");
  148. String company_name = p_preset->get("application/company_name");
  149. String product_name = p_preset->get("application/product_name");
  150. String file_description = p_preset->get("application/file_description");
  151. String copyright = p_preset->get("application/copyright");
  152. String trademarks = p_preset->get("application/trademarks");
  153. String comments = p_preset->get("application/comments");
  154. List<String> args;
  155. args.push_back(p_path);
  156. if (!icon_path.is_empty()) {
  157. args.push_back("--set-icon");
  158. args.push_back(icon_path);
  159. }
  160. if (!file_verion.is_empty()) {
  161. args.push_back("--set-file-version");
  162. args.push_back(file_verion);
  163. }
  164. if (!product_version.is_empty()) {
  165. args.push_back("--set-product-version");
  166. args.push_back(product_version);
  167. }
  168. if (!company_name.is_empty()) {
  169. args.push_back("--set-version-string");
  170. args.push_back("CompanyName");
  171. args.push_back(company_name);
  172. }
  173. if (!product_name.is_empty()) {
  174. args.push_back("--set-version-string");
  175. args.push_back("ProductName");
  176. args.push_back(product_name);
  177. }
  178. if (!file_description.is_empty()) {
  179. args.push_back("--set-version-string");
  180. args.push_back("FileDescription");
  181. args.push_back(file_description);
  182. }
  183. if (!copyright.is_empty()) {
  184. args.push_back("--set-version-string");
  185. args.push_back("LegalCopyright");
  186. args.push_back(copyright);
  187. }
  188. if (!trademarks.is_empty()) {
  189. args.push_back("--set-version-string");
  190. args.push_back("LegalTrademarks");
  191. args.push_back(trademarks);
  192. }
  193. #ifndef WINDOWS_ENABLED
  194. // On non-Windows we need WINE to run rcedit
  195. args.push_front(rcedit_path);
  196. rcedit_path = wine_path;
  197. #endif
  198. String str;
  199. Error err = OS::get_singleton()->execute(rcedit_path, args, &str, nullptr, true);
  200. ERR_FAIL_COND_V(err != OK, err);
  201. print_line("rcedit (" + p_path + "): " + str);
  202. if (str.find("Fatal error") != -1) {
  203. return FAILED;
  204. }
  205. return OK;
  206. }
  207. Error EditorExportPlatformWindows::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  208. List<String> args;
  209. #ifdef WINDOWS_ENABLED
  210. String signtool_path = EditorSettings::get_singleton()->get("export/windows/signtool");
  211. if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) {
  212. ERR_PRINT("Could not find signtool executable at " + signtool_path + ", aborting.");
  213. return ERR_FILE_NOT_FOUND;
  214. }
  215. if (signtool_path.is_empty()) {
  216. signtool_path = "signtool"; // try to run signtool from PATH
  217. }
  218. #else
  219. String signtool_path = EditorSettings::get_singleton()->get("export/windows/osslsigncode");
  220. if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) {
  221. ERR_PRINT("Could not find osslsigncode executable at " + signtool_path + ", aborting.");
  222. return ERR_FILE_NOT_FOUND;
  223. }
  224. if (signtool_path.is_empty()) {
  225. signtool_path = "osslsigncode"; // try to run signtool from PATH
  226. }
  227. #endif
  228. args.push_back("sign");
  229. //identity
  230. #ifdef WINDOWS_ENABLED
  231. int id_type = p_preset->get("codesign/identity_type");
  232. if (id_type == 0) { //auto select
  233. args.push_back("/a");
  234. } else if (id_type == 1) { //pkcs12
  235. if (p_preset->get("codesign/identity") != "") {
  236. args.push_back("/f");
  237. args.push_back(p_preset->get("codesign/identity"));
  238. } else {
  239. EditorNode::add_io_error("codesign: no identity found");
  240. return FAILED;
  241. }
  242. } else if (id_type == 2) { //Windows certificate store
  243. if (p_preset->get("codesign/identity") != "") {
  244. args.push_back("/sha1");
  245. args.push_back(p_preset->get("codesign/identity"));
  246. } else {
  247. EditorNode::add_io_error("codesign: no identity found");
  248. return FAILED;
  249. }
  250. } else {
  251. EditorNode::add_io_error("codesign: invalid identity type");
  252. return FAILED;
  253. }
  254. #else
  255. if (p_preset->get("codesign/identity") != "") {
  256. args.push_back("-pkcs12");
  257. args.push_back(p_preset->get("codesign/identity"));
  258. } else {
  259. EditorNode::add_io_error("codesign: no identity found");
  260. return FAILED;
  261. }
  262. #endif
  263. //password
  264. if (p_preset->get("codesign/password") != "") {
  265. #ifdef WINDOWS_ENABLED
  266. args.push_back("/p");
  267. #else
  268. args.push_back("-pass");
  269. #endif
  270. args.push_back(p_preset->get("codesign/password"));
  271. }
  272. //timestamp
  273. if (p_preset->get("codesign/timestamp")) {
  274. if (p_preset->get("codesign/timestamp_server") != "") {
  275. #ifdef WINDOWS_ENABLED
  276. args.push_back("/tr");
  277. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  278. args.push_back("/td");
  279. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  280. args.push_back("sha1");
  281. } else {
  282. args.push_back("sha256");
  283. }
  284. #else
  285. args.push_back("-ts");
  286. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  287. #endif
  288. } else {
  289. EditorNode::add_io_error("codesign: invalid timestamp server");
  290. return FAILED;
  291. }
  292. }
  293. //digest
  294. #ifdef WINDOWS_ENABLED
  295. args.push_back("/fd");
  296. #else
  297. args.push_back("-h");
  298. #endif
  299. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  300. args.push_back("sha1");
  301. } else {
  302. args.push_back("sha256");
  303. }
  304. //description
  305. if (p_preset->get("codesign/description") != "") {
  306. #ifdef WINDOWS_ENABLED
  307. args.push_back("/d");
  308. #else
  309. args.push_back("-n");
  310. #endif
  311. args.push_back(p_preset->get("codesign/description"));
  312. }
  313. //user options
  314. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  315. for (int i = 0; i < user_args.size(); i++) {
  316. String user_arg = user_args[i].strip_edges();
  317. if (!user_arg.is_empty()) {
  318. args.push_back(user_arg);
  319. }
  320. }
  321. #ifndef WINDOWS_ENABLED
  322. args.push_back("-in");
  323. #endif
  324. args.push_back(p_path);
  325. #ifndef WINDOWS_ENABLED
  326. args.push_back("-out");
  327. args.push_back(p_path + "_signed");
  328. #endif
  329. String str;
  330. Error err = OS::get_singleton()->execute(signtool_path, args, &str, nullptr, true);
  331. ERR_FAIL_COND_V(err != OK, err);
  332. print_line("codesign (" + p_path + "): " + str);
  333. #ifndef WINDOWS_ENABLED
  334. if (str.find("SignTool Error") != -1) {
  335. #else
  336. if (str.find("Failed") != -1) {
  337. #endif
  338. return FAILED;
  339. }
  340. #ifndef WINDOWS_ENABLED
  341. Ref<DirAccess> tmp_dir = DirAccess::create_for_path(p_path.get_base_dir());
  342. err = tmp_dir->remove(p_path);
  343. ERR_FAIL_COND_V(err != OK, err);
  344. err = tmp_dir->rename(p_path + "_signed", p_path);
  345. ERR_FAIL_COND_V(err != OK, err);
  346. #endif
  347. return OK;
  348. }
  349. bool EditorExportPlatformWindows::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  350. String err = "";
  351. bool valid = EditorExportPlatformPC::can_export(p_preset, err, r_missing_templates);
  352. String rcedit_path = EditorSettings::get_singleton()->get("export/windows/rcedit");
  353. if (rcedit_path.is_empty()) {
  354. err += TTR("The rcedit tool must be configured in the Editor Settings (Export > Windows > Rcedit) to change the icon or app information data.") + "\n";
  355. }
  356. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  357. if (!icon_path.is_empty() && !FileAccess::exists(icon_path)) {
  358. err += TTR("Invalid icon path:") + " " + icon_path + "\n";
  359. }
  360. // Only non-negative integers can exist in the version string.
  361. String file_version = p_preset->get("application/file_version");
  362. if (!file_version.is_empty()) {
  363. PackedStringArray version_array = file_version.split(".", false);
  364. if (version_array.size() != 4 || !version_array[0].is_valid_int() ||
  365. !version_array[1].is_valid_int() || !version_array[2].is_valid_int() ||
  366. !version_array[3].is_valid_int() || file_version.find("-") > -1) {
  367. err += TTR("Invalid file version:") + " " + file_version + "\n";
  368. }
  369. }
  370. String product_version = p_preset->get("application/product_version");
  371. if (!product_version.is_empty()) {
  372. PackedStringArray version_array = product_version.split(".", false);
  373. if (version_array.size() != 4 || !version_array[0].is_valid_int() ||
  374. !version_array[1].is_valid_int() || !version_array[2].is_valid_int() ||
  375. !version_array[3].is_valid_int() || product_version.find("-") > -1) {
  376. err += TTR("Invalid product version:") + " " + product_version + "\n";
  377. }
  378. }
  379. if (!err.is_empty()) {
  380. r_error = err;
  381. }
  382. return valid;
  383. }
  384. Error EditorExportPlatformWindows::fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) const {
  385. // Patch the header of the "pck" section in the PE file so that it corresponds to the embedded data
  386. if (p_embedded_size + p_embedded_start >= 0x100000000) { // Check for total executable size
  387. ERR_FAIL_V_MSG(ERR_INVALID_DATA, "Windows executables cannot be >= 4 GiB.");
  388. }
  389. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  390. if (f.is_null()) {
  391. return ERR_CANT_OPEN;
  392. }
  393. // Jump to the PE header and check the magic number
  394. {
  395. f->seek(0x3c);
  396. uint32_t pe_pos = f->get_32();
  397. f->seek(pe_pos);
  398. uint32_t magic = f->get_32();
  399. if (magic != 0x00004550) {
  400. return ERR_FILE_CORRUPT;
  401. }
  402. }
  403. // Process header
  404. int num_sections;
  405. {
  406. int64_t header_pos = f->get_position();
  407. f->seek(header_pos + 2);
  408. num_sections = f->get_16();
  409. f->seek(header_pos + 16);
  410. uint16_t opt_header_size = f->get_16();
  411. // Skip rest of header + optional header to go to the section headers
  412. f->seek(f->get_position() + 2 + opt_header_size);
  413. }
  414. // Search for the "pck" section
  415. int64_t section_table_pos = f->get_position();
  416. bool found = false;
  417. for (int i = 0; i < num_sections; ++i) {
  418. int64_t section_header_pos = section_table_pos + i * 40;
  419. f->seek(section_header_pos);
  420. uint8_t section_name[9];
  421. f->get_buffer(section_name, 8);
  422. section_name[8] = '\0';
  423. if (strcmp((char *)section_name, "pck") == 0) {
  424. // "pck" section found, let's patch!
  425. // Set virtual size to a little to avoid it taking memory (zero would give issues)
  426. f->seek(section_header_pos + 8);
  427. f->store_32(8);
  428. f->seek(section_header_pos + 16);
  429. f->store_32(p_embedded_size);
  430. f->seek(section_header_pos + 20);
  431. f->store_32(p_embedded_start);
  432. found = true;
  433. break;
  434. }
  435. }
  436. return found ? OK : ERR_FILE_CORRUPT;
  437. }