export.cpp 15 KB

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