export_plugin.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. bool EditorExportPlatformWindows::get_export_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
  76. // This option is not supported by "osslsigncode", used on non-Windows host.
  77. if (!OS::get_singleton()->has_feature("windows") && p_option == "codesign/identity_type") {
  78. return false;
  79. }
  80. return true;
  81. }
  82. void EditorExportPlatformWindows::get_export_options(List<ExportOption> *r_options) {
  83. EditorExportPlatformPC::get_export_options(r_options);
  84. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), false));
  85. 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));
  86. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity", PROPERTY_HINT_GLOBAL_FILE, "*.pfx,*.p12"), ""));
  87. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/password"), ""));
  88. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/timestamp"), true));
  89. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/timestamp_server_url"), ""));
  90. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/digest_algorithm", PROPERTY_HINT_ENUM, "SHA1,SHA256"), 1));
  91. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/description"), ""));
  92. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "codesign/custom_options"), PackedStringArray()));
  93. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.ico"), ""));
  94. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0.0"), ""));
  95. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0.0"), ""));
  96. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/company_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Company Name"), ""));
  97. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  98. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_description"), ""));
  99. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  100. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/trademarks"), ""));
  101. }
  102. void EditorExportPlatformWindows::_rcedit_add_data(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  103. String rcedit_path = EditorSettings::get_singleton()->get("export/windows/rcedit");
  104. if (rcedit_path.is_empty()) {
  105. 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.");
  106. return;
  107. }
  108. if (!FileAccess::exists(rcedit_path)) {
  109. ERR_PRINT("Could not find rcedit executable at " + rcedit_path + ", no icon or app information data will be included.");
  110. return;
  111. }
  112. #ifndef WINDOWS_ENABLED
  113. // On non-Windows we need WINE to run rcedit
  114. String wine_path = EditorSettings::get_singleton()->get("export/windows/wine");
  115. if (!wine_path.is_empty() && !FileAccess::exists(wine_path)) {
  116. ERR_PRINT("Could not find wine executable at " + wine_path + ", no icon or app information data will be included.");
  117. return;
  118. }
  119. if (wine_path.is_empty()) {
  120. wine_path = "wine"; // try to run wine from PATH
  121. }
  122. #endif
  123. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  124. String file_verion = p_preset->get("application/file_version");
  125. String product_version = p_preset->get("application/product_version");
  126. String company_name = p_preset->get("application/company_name");
  127. String product_name = p_preset->get("application/product_name");
  128. String file_description = p_preset->get("application/file_description");
  129. String copyright = p_preset->get("application/copyright");
  130. String trademarks = p_preset->get("application/trademarks");
  131. String comments = p_preset->get("application/comments");
  132. List<String> args;
  133. args.push_back(p_path);
  134. if (!icon_path.is_empty()) {
  135. args.push_back("--set-icon");
  136. args.push_back(icon_path);
  137. }
  138. if (!file_verion.is_empty()) {
  139. args.push_back("--set-file-version");
  140. args.push_back(file_verion);
  141. }
  142. if (!product_version.is_empty()) {
  143. args.push_back("--set-product-version");
  144. args.push_back(product_version);
  145. }
  146. if (!company_name.is_empty()) {
  147. args.push_back("--set-version-string");
  148. args.push_back("CompanyName");
  149. args.push_back(company_name);
  150. }
  151. if (!product_name.is_empty()) {
  152. args.push_back("--set-version-string");
  153. args.push_back("ProductName");
  154. args.push_back(product_name);
  155. }
  156. if (!file_description.is_empty()) {
  157. args.push_back("--set-version-string");
  158. args.push_back("FileDescription");
  159. args.push_back(file_description);
  160. }
  161. if (!copyright.is_empty()) {
  162. args.push_back("--set-version-string");
  163. args.push_back("LegalCopyright");
  164. args.push_back(copyright);
  165. }
  166. if (!trademarks.is_empty()) {
  167. args.push_back("--set-version-string");
  168. args.push_back("LegalTrademarks");
  169. args.push_back(trademarks);
  170. }
  171. #ifdef WINDOWS_ENABLED
  172. OS::get_singleton()->execute(rcedit_path, args);
  173. #else
  174. // On non-Windows we need WINE to run rcedit
  175. args.push_front(rcedit_path);
  176. OS::get_singleton()->execute(wine_path, args);
  177. #endif
  178. }
  179. Error EditorExportPlatformWindows::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  180. List<String> args;
  181. #ifdef WINDOWS_ENABLED
  182. String signtool_path = EditorSettings::get_singleton()->get("export/windows/signtool");
  183. if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) {
  184. ERR_PRINT("Could not find signtool executable at " + signtool_path + ", aborting.");
  185. return ERR_FILE_NOT_FOUND;
  186. }
  187. if (signtool_path.is_empty()) {
  188. signtool_path = "signtool"; // try to run signtool from PATH
  189. }
  190. #else
  191. String signtool_path = EditorSettings::get_singleton()->get("export/windows/osslsigncode");
  192. if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) {
  193. ERR_PRINT("Could not find osslsigncode executable at " + signtool_path + ", aborting.");
  194. return ERR_FILE_NOT_FOUND;
  195. }
  196. if (signtool_path.is_empty()) {
  197. signtool_path = "osslsigncode"; // try to run signtool from PATH
  198. }
  199. #endif
  200. args.push_back("sign");
  201. //identity
  202. #ifdef WINDOWS_ENABLED
  203. int id_type = p_preset->get("codesign/identity_type");
  204. if (id_type == 0) { //auto select
  205. args.push_back("/a");
  206. } else if (id_type == 1) { //pkcs12
  207. if (p_preset->get("codesign/identity") != "") {
  208. args.push_back("/f");
  209. args.push_back(p_preset->get("codesign/identity"));
  210. } else {
  211. EditorNode::add_io_error("codesign: no identity found");
  212. return FAILED;
  213. }
  214. } else if (id_type == 2) { //Windows certificate store
  215. if (p_preset->get("codesign/identity") != "") {
  216. args.push_back("/sha1");
  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 {
  223. EditorNode::add_io_error("codesign: invalid identity type");
  224. return FAILED;
  225. }
  226. #else
  227. if (p_preset->get("codesign/identity") != "") {
  228. args.push_back("-pkcs12");
  229. args.push_back(p_preset->get("codesign/identity"));
  230. } else {
  231. EditorNode::add_io_error("codesign: no identity found");
  232. return FAILED;
  233. }
  234. #endif
  235. //password
  236. if (p_preset->get("codesign/password") != "") {
  237. #ifdef WINDOWS_ENABLED
  238. args.push_back("/p");
  239. #else
  240. args.push_back("-pass");
  241. #endif
  242. args.push_back(p_preset->get("codesign/password"));
  243. }
  244. //timestamp
  245. if (p_preset->get("codesign/timestamp")) {
  246. if (p_preset->get("codesign/timestamp_server") != "") {
  247. #ifdef WINDOWS_ENABLED
  248. args.push_back("/tr");
  249. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  250. args.push_back("/td");
  251. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  252. args.push_back("sha1");
  253. } else {
  254. args.push_back("sha256");
  255. }
  256. #else
  257. args.push_back("-ts");
  258. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  259. #endif
  260. } else {
  261. EditorNode::add_io_error("codesign: invalid timestamp server");
  262. return FAILED;
  263. }
  264. }
  265. //digest
  266. #ifdef WINDOWS_ENABLED
  267. args.push_back("/fd");
  268. #else
  269. args.push_back("-h");
  270. #endif
  271. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  272. args.push_back("sha1");
  273. } else {
  274. args.push_back("sha256");
  275. }
  276. //description
  277. if (p_preset->get("codesign/description") != "") {
  278. #ifdef WINDOWS_ENABLED
  279. args.push_back("/d");
  280. #else
  281. args.push_back("-n");
  282. #endif
  283. args.push_back(p_preset->get("codesign/description"));
  284. }
  285. //user options
  286. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  287. for (int i = 0; i < user_args.size(); i++) {
  288. String user_arg = user_args[i].strip_edges();
  289. if (!user_arg.is_empty()) {
  290. args.push_back(user_arg);
  291. }
  292. }
  293. #ifndef WINDOWS_ENABLED
  294. args.push_back("-in");
  295. #endif
  296. args.push_back(p_path);
  297. #ifndef WINDOWS_ENABLED
  298. args.push_back("-out");
  299. args.push_back(p_path + "_signed");
  300. #endif
  301. String str;
  302. Error err = OS::get_singleton()->execute(signtool_path, args, &str, nullptr, true);
  303. ERR_FAIL_COND_V(err != OK, err);
  304. print_line("codesign (" + p_path + "): " + str);
  305. #ifndef WINDOWS_ENABLED
  306. if (str.find("SignTool Error") != -1) {
  307. #else
  308. if (str.find("Failed") != -1) {
  309. #endif
  310. return FAILED;
  311. }
  312. #ifndef WINDOWS_ENABLED
  313. DirAccessRef tmp_dir = DirAccess::create_for_path(p_path.get_base_dir());
  314. err = tmp_dir->remove(p_path);
  315. ERR_FAIL_COND_V(err != OK, err);
  316. err = tmp_dir->rename(p_path + "_signed", p_path);
  317. ERR_FAIL_COND_V(err != OK, err);
  318. #endif
  319. return OK;
  320. }
  321. bool EditorExportPlatformWindows::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  322. String err = "";
  323. bool valid = EditorExportPlatformPC::can_export(p_preset, err, r_missing_templates);
  324. String rcedit_path = EditorSettings::get_singleton()->get("export/windows/rcedit");
  325. if (rcedit_path.is_empty()) {
  326. err += TTR("The rcedit tool must be configured in the Editor Settings (Export > Windows > Rcedit) to change the icon or app information data.") + "\n";
  327. }
  328. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  329. if (!icon_path.is_empty() && !FileAccess::exists(icon_path)) {
  330. err += TTR("Invalid icon path:") + " " + icon_path + "\n";
  331. }
  332. // Only non-negative integers can exist in the version string.
  333. String file_version = p_preset->get("application/file_version");
  334. if (!file_version.is_empty()) {
  335. PackedStringArray version_array = file_version.split(".", false);
  336. if (version_array.size() != 4 || !version_array[0].is_valid_int() ||
  337. !version_array[1].is_valid_int() || !version_array[2].is_valid_int() ||
  338. !version_array[3].is_valid_int() || file_version.find("-") > -1) {
  339. err += TTR("Invalid file version:") + " " + file_version + "\n";
  340. }
  341. }
  342. String product_version = p_preset->get("application/product_version");
  343. if (!product_version.is_empty()) {
  344. PackedStringArray version_array = product_version.split(".", false);
  345. if (version_array.size() != 4 || !version_array[0].is_valid_int() ||
  346. !version_array[1].is_valid_int() || !version_array[2].is_valid_int() ||
  347. !version_array[3].is_valid_int() || product_version.find("-") > -1) {
  348. err += TTR("Invalid product version:") + " " + product_version + "\n";
  349. }
  350. }
  351. if (!err.is_empty()) {
  352. r_error = err;
  353. }
  354. return valid;
  355. }