export_plugin.cpp 17 KB

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