export_plugin.cpp 14 KB

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