export_plugin.cpp 17 KB

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