export.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "core/io/file_access.h"
  31. #include "core/os/os.h"
  32. #include "editor/editor_export.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "platform/windows/logo.gen.h"
  36. static Error fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size);
  37. class EditorExportPlatformWindows : public EditorExportPlatformPC {
  38. void _rcedit_add_data(const Ref<EditorExportPreset> &p_preset, const String &p_path);
  39. Error _code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path);
  40. public:
  41. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  42. virtual Error sign_shared_object(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path);
  43. virtual void get_export_options(List<ExportOption> *r_options);
  44. };
  45. Error EditorExportPlatformWindows::sign_shared_object(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) {
  46. if (p_preset->get("codesign/enable")) {
  47. return _code_sign(p_preset, p_path);
  48. } else {
  49. return OK;
  50. }
  51. }
  52. Error EditorExportPlatformWindows::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  53. Error err = EditorExportPlatformPC::export_project(p_preset, p_debug, p_path, p_flags);
  54. if (err != OK) {
  55. return err;
  56. }
  57. _rcedit_add_data(p_preset, p_path);
  58. if (p_preset->get("codesign/enable") && err == OK) {
  59. err = _code_sign(p_preset, p_path);
  60. }
  61. return err;
  62. }
  63. void EditorExportPlatformWindows::get_export_options(List<ExportOption> *r_options) {
  64. EditorExportPlatformPC::get_export_options(r_options);
  65. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), false));
  66. #ifdef WINDOWS_ENABLED
  67. 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));
  68. #endif
  69. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity", PROPERTY_HINT_GLOBAL_FILE, "*.pfx,*.p12"), ""));
  70. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/password"), ""));
  71. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/timestamp"), true));
  72. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/timestamp_server_url"), ""));
  73. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/digest_algorithm", PROPERTY_HINT_ENUM, "SHA1,SHA256"), 1));
  74. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/description"), ""));
  75. r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "codesign/custom_options"), PackedStringArray()));
  76. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.ico"), ""));
  77. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0"), ""));
  78. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "1.0.0"), ""));
  79. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/company_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Company Name"), ""));
  80. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  81. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_description"), ""));
  82. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), ""));
  83. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/trademarks"), ""));
  84. }
  85. void EditorExportPlatformWindows::_rcedit_add_data(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  86. String rcedit_path = EditorSettings::get_singleton()->get("export/windows/rcedit");
  87. if (rcedit_path == String()) {
  88. return;
  89. }
  90. if (!FileAccess::exists(rcedit_path)) {
  91. ERR_PRINT("Could not find rcedit executable at " + rcedit_path + ", no icon or app information data will be included.");
  92. return;
  93. }
  94. #ifndef WINDOWS_ENABLED
  95. // On non-Windows we need WINE to run rcedit
  96. String wine_path = EditorSettings::get_singleton()->get("export/windows/wine");
  97. if (wine_path != String() && !FileAccess::exists(wine_path)) {
  98. ERR_PRINT("Could not find wine executable at " + wine_path + ", no icon or app information data will be included.");
  99. return;
  100. }
  101. if (wine_path == String()) {
  102. wine_path = "wine"; // try to run wine from PATH
  103. }
  104. #endif
  105. String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon"));
  106. String file_verion = p_preset->get("application/file_version");
  107. String product_version = p_preset->get("application/product_version");
  108. String company_name = p_preset->get("application/company_name");
  109. String product_name = p_preset->get("application/product_name");
  110. String file_description = p_preset->get("application/file_description");
  111. String copyright = p_preset->get("application/copyright");
  112. String trademarks = p_preset->get("application/trademarks");
  113. String comments = p_preset->get("application/comments");
  114. List<String> args;
  115. args.push_back(p_path);
  116. if (icon_path != String()) {
  117. args.push_back("--set-icon");
  118. args.push_back(icon_path);
  119. }
  120. if (file_verion != String()) {
  121. args.push_back("--set-file-version");
  122. args.push_back(file_verion);
  123. }
  124. if (product_version != String()) {
  125. args.push_back("--set-product-version");
  126. args.push_back(product_version);
  127. }
  128. if (company_name != String()) {
  129. args.push_back("--set-version-string");
  130. args.push_back("CompanyName");
  131. args.push_back(company_name);
  132. }
  133. if (product_name != String()) {
  134. args.push_back("--set-version-string");
  135. args.push_back("ProductName");
  136. args.push_back(product_name);
  137. }
  138. if (file_description != String()) {
  139. args.push_back("--set-version-string");
  140. args.push_back("FileDescription");
  141. args.push_back(file_description);
  142. }
  143. if (copyright != String()) {
  144. args.push_back("--set-version-string");
  145. args.push_back("LegalCopyright");
  146. args.push_back(copyright);
  147. }
  148. if (trademarks != String()) {
  149. args.push_back("--set-version-string");
  150. args.push_back("LegalTrademarks");
  151. args.push_back(trademarks);
  152. }
  153. #ifdef WINDOWS_ENABLED
  154. OS::get_singleton()->execute(rcedit_path, args);
  155. #else
  156. // On non-Windows we need WINE to run rcedit
  157. args.push_front(rcedit_path);
  158. OS::get_singleton()->execute(wine_path, args);
  159. #endif
  160. }
  161. Error EditorExportPlatformWindows::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  162. List<String> args;
  163. #ifdef WINDOWS_ENABLED
  164. String signtool_path = EditorSettings::get_singleton()->get("export/windows/signtool");
  165. if (signtool_path != String() && !FileAccess::exists(signtool_path)) {
  166. ERR_PRINT("Could not find signtool executable at " + signtool_path + ", aborting.");
  167. return ERR_FILE_NOT_FOUND;
  168. }
  169. if (signtool_path == String()) {
  170. signtool_path = "signtool"; // try to run signtool from PATH
  171. }
  172. #else
  173. String signtool_path = EditorSettings::get_singleton()->get("export/windows/osslsigncode");
  174. if (signtool_path != String() && !FileAccess::exists(signtool_path)) {
  175. ERR_PRINT("Could not find osslsigncode executable at " + signtool_path + ", aborting.");
  176. return ERR_FILE_NOT_FOUND;
  177. }
  178. if (signtool_path == String()) {
  179. signtool_path = "osslsigncode"; // try to run signtool from PATH
  180. }
  181. #endif
  182. args.push_back("sign");
  183. //identity
  184. #ifdef WINDOWS_ENABLED
  185. int id_type = p_preset->get("codesign/identity_type");
  186. if (id_type == 0) { //auto select
  187. args.push_back("/a");
  188. } else if (id_type == 1) { //pkcs12
  189. if (p_preset->get("codesign/identity") != "") {
  190. args.push_back("/f");
  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 if (id_type == 2) { //Windows certificate store
  197. if (p_preset->get("codesign/identity") != "") {
  198. args.push_back("/sha1");
  199. args.push_back(p_preset->get("codesign/identity"));
  200. } else {
  201. EditorNode::add_io_error("codesign: no identity found");
  202. return FAILED;
  203. }
  204. } else {
  205. EditorNode::add_io_error("codesign: invalid identity type");
  206. return FAILED;
  207. }
  208. #else
  209. if (p_preset->get("codesign/identity") != "") {
  210. args.push_back("-pkcs12");
  211. args.push_back(p_preset->get("codesign/identity"));
  212. } else {
  213. EditorNode::add_io_error("codesign: no identity found");
  214. return FAILED;
  215. }
  216. #endif
  217. //password
  218. if (p_preset->get("codesign/password") != "") {
  219. #ifdef WINDOWS_ENABLED
  220. args.push_back("/p");
  221. #else
  222. args.push_back("-pass");
  223. #endif
  224. args.push_back(p_preset->get("codesign/password"));
  225. }
  226. //timestamp
  227. if (p_preset->get("codesign/timestamp")) {
  228. if (p_preset->get("codesign/timestamp_server") != "") {
  229. #ifdef WINDOWS_ENABLED
  230. args.push_back("/tr");
  231. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  232. args.push_back("/td");
  233. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  234. args.push_back("sha1");
  235. } else {
  236. args.push_back("sha256");
  237. }
  238. #else
  239. args.push_back("-ts");
  240. args.push_back(p_preset->get("codesign/timestamp_server_url"));
  241. #endif
  242. } else {
  243. EditorNode::add_io_error("codesign: invalid timestamp server");
  244. return FAILED;
  245. }
  246. }
  247. //digest
  248. #ifdef WINDOWS_ENABLED
  249. args.push_back("/fd");
  250. #else
  251. args.push_back("-h");
  252. #endif
  253. if ((int)p_preset->get("codesign/digest_algorithm") == 0) {
  254. args.push_back("sha1");
  255. } else {
  256. args.push_back("sha256");
  257. }
  258. //description
  259. if (p_preset->get("codesign/description") != "") {
  260. #ifdef WINDOWS_ENABLED
  261. args.push_back("/d");
  262. #else
  263. args.push_back("-n");
  264. #endif
  265. args.push_back(p_preset->get("codesign/description"));
  266. }
  267. //user options
  268. PackedStringArray user_args = p_preset->get("codesign/custom_options");
  269. for (int i = 0; i < user_args.size(); i++) {
  270. String user_arg = user_args[i].strip_edges();
  271. if (!user_arg.is_empty()) {
  272. args.push_back(user_arg);
  273. }
  274. }
  275. #ifndef WINDOWS_ENABLED
  276. args.push_back("-in");
  277. #endif
  278. args.push_back(p_path);
  279. #ifndef WINDOWS_ENABLED
  280. args.push_back("-out");
  281. args.push_back(p_path + "_signed");
  282. #endif
  283. String str;
  284. Error err = OS::get_singleton()->execute(signtool_path, args, &str, nullptr, true);
  285. ERR_FAIL_COND_V(err != OK, err);
  286. print_line("codesign (" + p_path + "): " + str);
  287. #ifndef WINDOWS_ENABLED
  288. if (str.find("SignTool Error") != -1) {
  289. #else
  290. if (str.find("Failed") != -1) {
  291. #endif
  292. return FAILED;
  293. }
  294. #ifndef WINDOWS_ENABLED
  295. DirAccessRef tmp_dir = DirAccess::create_for_path(p_path.get_base_dir());
  296. err = tmp_dir->remove(p_path);
  297. ERR_FAIL_COND_V(err != OK, err);
  298. err = tmp_dir->rename(p_path + "_signed", p_path);
  299. ERR_FAIL_COND_V(err != OK, err);
  300. #endif
  301. return OK;
  302. }
  303. void register_windows_exporter() {
  304. EDITOR_DEF("export/windows/rcedit", "");
  305. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/windows/rcedit", PROPERTY_HINT_GLOBAL_FILE, "*.exe"));
  306. #ifdef WINDOWS_ENABLED
  307. EDITOR_DEF("export/windows/signtool", "");
  308. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/windows/signtool", PROPERTY_HINT_GLOBAL_FILE, "*.exe"));
  309. #else
  310. EDITOR_DEF("export/windows/osslsigncode", "");
  311. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/windows/osslsigncode", PROPERTY_HINT_GLOBAL_FILE));
  312. // On non-Windows we need WINE to run rcedit
  313. EDITOR_DEF("export/windows/wine", "");
  314. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/windows/wine", PROPERTY_HINT_GLOBAL_FILE));
  315. #endif
  316. Ref<EditorExportPlatformWindows> platform;
  317. platform.instantiate();
  318. Ref<Image> img = memnew(Image(_windows_logo));
  319. Ref<ImageTexture> logo;
  320. logo.instantiate();
  321. logo->create_from_image(img);
  322. platform->set_logo(logo);
  323. platform->set_name("Windows Desktop");
  324. platform->set_extension("exe");
  325. platform->set_release_32("windows_32_release.exe");
  326. platform->set_debug_32("windows_32_debug.exe");
  327. platform->set_release_64("windows_64_release.exe");
  328. platform->set_debug_64("windows_64_debug.exe");
  329. platform->set_os_name("Windows");
  330. platform->set_fixup_embedded_pck_func(&fixup_embedded_pck);
  331. EditorExport::get_singleton()->add_export_platform(platform);
  332. }
  333. static Error fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) {
  334. // Patch the header of the "pck" section in the PE file so that it corresponds to the embedded data
  335. FileAccess *f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  336. if (!f) {
  337. return ERR_CANT_OPEN;
  338. }
  339. // Jump to the PE header and check the magic number
  340. {
  341. f->seek(0x3c);
  342. uint32_t pe_pos = f->get_32();
  343. f->seek(pe_pos);
  344. uint32_t magic = f->get_32();
  345. if (magic != 0x00004550) {
  346. f->close();
  347. return ERR_FILE_CORRUPT;
  348. }
  349. }
  350. // Process header
  351. int num_sections;
  352. {
  353. int64_t header_pos = f->get_position();
  354. f->seek(header_pos + 2);
  355. num_sections = f->get_16();
  356. f->seek(header_pos + 16);
  357. uint16_t opt_header_size = f->get_16();
  358. // Skip rest of header + optional header to go to the section headers
  359. f->seek(f->get_position() + 2 + opt_header_size);
  360. }
  361. // Search for the "pck" section
  362. int64_t section_table_pos = f->get_position();
  363. bool found = false;
  364. for (int i = 0; i < num_sections; ++i) {
  365. int64_t section_header_pos = section_table_pos + i * 40;
  366. f->seek(section_header_pos);
  367. uint8_t section_name[9];
  368. f->get_buffer(section_name, 8);
  369. section_name[8] = '\0';
  370. if (strcmp((char *)section_name, "pck") == 0) {
  371. // "pck" section found, let's patch!
  372. // Set virtual size to a little to avoid it taking memory (zero would give issues)
  373. f->seek(section_header_pos + 8);
  374. f->store_32(8);
  375. f->seek(section_header_pos + 16);
  376. f->store_32(p_embedded_size);
  377. f->seek(section_header_pos + 20);
  378. f->store_32(p_embedded_start);
  379. found = true;
  380. break;
  381. }
  382. }
  383. f->close();
  384. return found ? OK : ERR_FILE_CORRUPT;
  385. }