export_plugin.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 "editor/editor_settings.h"
  32. #include "platform/uwp/logo.gen.h"
  33. String EditorExportPlatformUWP::get_name() const {
  34. return "UWP";
  35. }
  36. String EditorExportPlatformUWP::get_os_name() const {
  37. return "UWP";
  38. }
  39. List<String> EditorExportPlatformUWP::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  40. List<String> list;
  41. list.push_back("appx");
  42. return list;
  43. }
  44. Ref<Texture2D> EditorExportPlatformUWP::get_logo() const {
  45. return logo;
  46. }
  47. void EditorExportPlatformUWP::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const {
  48. r_features->push_back("s3tc");
  49. r_features->push_back("etc");
  50. r_features->push_back(p_preset->get("binary_format/architecture"));
  51. }
  52. void EditorExportPlatformUWP::get_export_options(List<ExportOption> *r_options) {
  53. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  54. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
  55. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "binary_format/architecture", PROPERTY_HINT_ENUM, "x86_64,x86_32,arm32"), "x86_64"));
  56. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "command_line/extra_args"), ""));
  57. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/display_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  58. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/short_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), ""));
  59. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/unique_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game.Name"), ""));
  60. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/description"), ""));
  61. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/publisher", PROPERTY_HINT_PLACEHOLDER_TEXT, "CN=CompanyName"), ""));
  62. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/publisher_display_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Company Name"), ""));
  63. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "identity/product_guid", PROPERTY_HINT_PLACEHOLDER_TEXT, "00000000-0000-0000-0000-000000000000"), ""));
  64. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "identity/publisher_guid", PROPERTY_HINT_PLACEHOLDER_TEXT, "00000000-0000-0000-0000-000000000000"), ""));
  65. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "signing/certificate", PROPERTY_HINT_GLOBAL_FILE, "*.pfx"), ""));
  66. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "signing/password"), ""));
  67. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "signing/algorithm", PROPERTY_HINT_ENUM, "MD5,SHA1,SHA256"), 2));
  68. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/major"), 1));
  69. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/minor"), 0));
  70. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/build"), 0));
  71. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/revision"), 0));
  72. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape"), true));
  73. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait"), true));
  74. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/landscape_flipped"), true));
  75. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "orientation/portrait_flipped"), true));
  76. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "images/background_color"), "transparent"));
  77. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/store_logo", PROPERTY_HINT_RESOURCE_TYPE, "CompressedTexture2D"), Variant()));
  78. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/square44x44_logo", PROPERTY_HINT_RESOURCE_TYPE, "CompressedTexture2D"), Variant()));
  79. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/square71x71_logo", PROPERTY_HINT_RESOURCE_TYPE, "CompressedTexture2D"), Variant()));
  80. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/square150x150_logo", PROPERTY_HINT_RESOURCE_TYPE, "CompressedTexture2D"), Variant()));
  81. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/square310x310_logo", PROPERTY_HINT_RESOURCE_TYPE, "CompressedTexture2D"), Variant()));
  82. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/wide310x150_logo", PROPERTY_HINT_RESOURCE_TYPE, "CompressedTexture2D"), Variant()));
  83. r_options->push_back(ExportOption(PropertyInfo(Variant::OBJECT, "images/splash_screen", PROPERTY_HINT_RESOURCE_TYPE, "CompressedTexture2D"), Variant()));
  84. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "tiles/show_name_on_square150x150"), false));
  85. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "tiles/show_name_on_wide310x150"), false));
  86. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "tiles/show_name_on_square310x310"), false));
  87. // Capabilities
  88. const char **basic = uwp_capabilities;
  89. while (*basic) {
  90. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/" + String(*basic)), false));
  91. basic++;
  92. }
  93. const char **uap = uwp_uap_capabilities;
  94. while (*uap) {
  95. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/" + String(*uap)), false));
  96. uap++;
  97. }
  98. const char **device = uwp_device_capabilities;
  99. while (*device) {
  100. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/" + String(*device)), false));
  101. device++;
  102. }
  103. }
  104. bool EditorExportPlatformUWP::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  105. #ifndef DEV_ENABLED
  106. // We don't provide export templates for the UWP platform currently as it
  107. // has not been ported for Godot 4.0. This is skipped in DEV_ENABLED so that
  108. // contributors can still test the pipeline if/when we can build it again.
  109. r_error = "The UWP platform is currently not supported in Godot 4.0.\n";
  110. return false;
  111. #endif
  112. String err;
  113. bool valid = false;
  114. // Look for export templates (first official, and if defined custom templates).
  115. String arch = p_preset->get("binary_format/architecture");
  116. String arch_infix;
  117. if (arch == "arm32") {
  118. arch_infix = "arm";
  119. } else if (arch == "x86_32") {
  120. arch_infix = "x86";
  121. } else if (arch == "x86_64") {
  122. arch_infix = "x64";
  123. }
  124. bool dvalid = exists_export_template("uwp_" + arch_infix + "_debug.zip", &err);
  125. bool rvalid = exists_export_template("uwp_" + arch_infix + "_release.zip", &err);
  126. if (p_preset->get("custom_template/debug") != "") {
  127. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  128. if (!dvalid) {
  129. err += TTR("Custom debug template not found.") + "\n";
  130. }
  131. }
  132. if (p_preset->get("custom_template/release") != "") {
  133. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  134. if (!rvalid) {
  135. err += TTR("Custom release template not found.") + "\n";
  136. }
  137. }
  138. valid = dvalid || rvalid;
  139. r_missing_templates = !valid;
  140. // Validate the rest of the configuration.
  141. if (!_valid_resource_name(p_preset->get("package/short_name"))) {
  142. valid = false;
  143. err += TTR("Invalid package short name.") + "\n";
  144. }
  145. if (!_valid_resource_name(p_preset->get("package/unique_name"))) {
  146. valid = false;
  147. err += TTR("Invalid package unique name.") + "\n";
  148. }
  149. if (!_valid_resource_name(p_preset->get("package/publisher_display_name"))) {
  150. valid = false;
  151. err += TTR("Invalid package publisher display name.") + "\n";
  152. }
  153. if (!_valid_guid(p_preset->get("identity/product_guid"))) {
  154. valid = false;
  155. err += TTR("Invalid product GUID.") + "\n";
  156. }
  157. if (!_valid_guid(p_preset->get("identity/publisher_guid"))) {
  158. valid = false;
  159. err += TTR("Invalid publisher GUID.") + "\n";
  160. }
  161. if (!_valid_bgcolor(p_preset->get("images/background_color"))) {
  162. valid = false;
  163. err += TTR("Invalid background color.") + "\n";
  164. }
  165. if (!p_preset->get("images/store_logo").is_zero() && !_valid_image((Object::cast_to<CompressedTexture2D>((Object *)p_preset->get("images/store_logo"))), 50, 50)) {
  166. valid = false;
  167. err += TTR("Invalid Store Logo image dimensions (should be 50x50).") + "\n";
  168. }
  169. if (!p_preset->get("images/square44x44_logo").is_zero() && !_valid_image((Object::cast_to<CompressedTexture2D>((Object *)p_preset->get("images/square44x44_logo"))), 44, 44)) {
  170. valid = false;
  171. err += TTR("Invalid square 44x44 logo image dimensions (should be 44x44).") + "\n";
  172. }
  173. if (!p_preset->get("images/square71x71_logo").is_zero() && !_valid_image((Object::cast_to<CompressedTexture2D>((Object *)p_preset->get("images/square71x71_logo"))), 71, 71)) {
  174. valid = false;
  175. err += TTR("Invalid square 71x71 logo image dimensions (should be 71x71).") + "\n";
  176. }
  177. if (!p_preset->get("images/square150x150_logo").is_zero() && !_valid_image((Object::cast_to<CompressedTexture2D>((Object *)p_preset->get("images/square150x150_logo"))), 150, 150)) {
  178. valid = false;
  179. err += TTR("Invalid square 150x150 logo image dimensions (should be 150x150).") + "\n";
  180. }
  181. if (!p_preset->get("images/square310x310_logo").is_zero() && !_valid_image((Object::cast_to<CompressedTexture2D>((Object *)p_preset->get("images/square310x310_logo"))), 310, 310)) {
  182. valid = false;
  183. err += TTR("Invalid square 310x310 logo image dimensions (should be 310x310).") + "\n";
  184. }
  185. if (!p_preset->get("images/wide310x150_logo").is_zero() && !_valid_image((Object::cast_to<CompressedTexture2D>((Object *)p_preset->get("images/wide310x150_logo"))), 310, 150)) {
  186. valid = false;
  187. err += TTR("Invalid wide 310x150 logo image dimensions (should be 310x150).") + "\n";
  188. }
  189. if (!p_preset->get("images/splash_screen").is_zero() && !_valid_image((Object::cast_to<CompressedTexture2D>((Object *)p_preset->get("images/splash_screen"))), 620, 300)) {
  190. valid = false;
  191. err += TTR("Invalid splash screen image dimensions (should be 620x300).") + "\n";
  192. }
  193. r_error = err;
  194. return valid;
  195. }
  196. Error EditorExportPlatformUWP::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  197. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  198. String src_appx;
  199. EditorProgress ep("export", "Exporting for UWP", 7, true);
  200. if (p_debug) {
  201. src_appx = p_preset->get("custom_template/debug");
  202. } else {
  203. src_appx = p_preset->get("custom_template/release");
  204. }
  205. src_appx = src_appx.strip_edges();
  206. String arch = p_preset->get("binary_format/architecture");
  207. if (src_appx.is_empty()) {
  208. String err, arch_infix;
  209. if (arch == "arm32") {
  210. arch_infix = "arm";
  211. } else if (arch == "x86_32") {
  212. arch_infix = "x86";
  213. } else if (arch == "x86_64") {
  214. arch_infix = "x64";
  215. }
  216. if (p_debug) {
  217. src_appx = find_export_template("uwp_" + arch_infix + "_debug.zip", &err);
  218. } else {
  219. src_appx = find_export_template("uwp_" + arch_infix + "_release.zip", &err);
  220. }
  221. if (src_appx.is_empty()) {
  222. EditorNode::add_io_error(err);
  223. return ERR_FILE_NOT_FOUND;
  224. }
  225. }
  226. if (!DirAccess::exists(p_path.get_base_dir())) {
  227. return ERR_FILE_BAD_PATH;
  228. }
  229. Error err = OK;
  230. Ref<FileAccess> fa_pack = FileAccess::open(p_path, FileAccess::WRITE, &err);
  231. ERR_FAIL_COND_V_MSG(err != OK, ERR_CANT_CREATE, "Cannot create file '" + p_path + "'.");
  232. AppxPackager packager;
  233. packager.init(fa_pack);
  234. Ref<FileAccess> io_fa;
  235. zlib_filefunc_def io = zipio_create_io(&io_fa);
  236. if (ep.step("Creating package...", 0)) {
  237. return ERR_SKIP;
  238. }
  239. unzFile pkg = unzOpen2(src_appx.utf8().get_data(), &io);
  240. if (!pkg) {
  241. EditorNode::add_io_error("Could not find template appx to export:\n" + src_appx);
  242. return ERR_FILE_NOT_FOUND;
  243. }
  244. int ret = unzGoToFirstFile(pkg);
  245. if (ep.step("Copying template files...", 1)) {
  246. return ERR_SKIP;
  247. }
  248. EditorNode::progress_add_task("template_files", "Template files", 100);
  249. packager.set_progress_task("template_files");
  250. int template_files_amount = 9;
  251. int template_file_no = 1;
  252. while (ret == UNZ_OK) {
  253. // get file name
  254. unz_file_info info;
  255. char fname[16834];
  256. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16834, nullptr, 0, nullptr, 0);
  257. if (ret != UNZ_OK) {
  258. break;
  259. }
  260. String path = String::utf8(fname);
  261. if (path.ends_with("/")) {
  262. // Ignore directories
  263. ret = unzGoToNextFile(pkg);
  264. continue;
  265. }
  266. Vector<uint8_t> data;
  267. bool do_read = true;
  268. if (path.begins_with("Assets/")) {
  269. path = path.replace(".scale-100", "");
  270. data = _get_image_data(p_preset, path);
  271. if (data.size() > 0) {
  272. do_read = false;
  273. }
  274. }
  275. //read
  276. if (do_read) {
  277. data.resize(info.uncompressed_size);
  278. unzOpenCurrentFile(pkg);
  279. unzReadCurrentFile(pkg, data.ptrw(), data.size());
  280. unzCloseCurrentFile(pkg);
  281. }
  282. if (path == "AppxManifest.xml") {
  283. data = _fix_manifest(p_preset, data, p_flags & (DEBUG_FLAG_DUMB_CLIENT | DEBUG_FLAG_REMOTE_DEBUG));
  284. }
  285. print_line("ADDING: " + path);
  286. err = packager.add_file(path, data.ptr(), data.size(), template_file_no++, template_files_amount, _should_compress_asset(path, data));
  287. if (err != OK) {
  288. return err;
  289. }
  290. ret = unzGoToNextFile(pkg);
  291. }
  292. EditorNode::progress_end_task("template_files");
  293. if (ep.step("Creating command line...", 2)) {
  294. return ERR_SKIP;
  295. }
  296. Vector<String> cl = ((String)p_preset->get("command_line/extra_args")).strip_edges().split(" ");
  297. for (int i = 0; i < cl.size(); i++) {
  298. if (cl[i].strip_edges().length() == 0) {
  299. cl.remove_at(i);
  300. i--;
  301. }
  302. }
  303. if (!(p_flags & DEBUG_FLAG_DUMB_CLIENT)) {
  304. cl.push_back("--path");
  305. cl.push_back("game");
  306. }
  307. gen_export_flags(cl, p_flags);
  308. // Command line file
  309. Vector<uint8_t> clf;
  310. // Argc
  311. clf.resize(4);
  312. encode_uint32(cl.size(), clf.ptrw());
  313. for (int i = 0; i < cl.size(); i++) {
  314. CharString txt = cl[i].utf8();
  315. int base = clf.size();
  316. clf.resize(base + 4 + txt.length());
  317. encode_uint32(txt.length(), &clf.write[base]);
  318. memcpy(&clf.write[base + 4], txt.ptr(), txt.length());
  319. print_line(itos(i) + " param: " + cl[i]);
  320. }
  321. err = packager.add_file("__cl__.cl", clf.ptr(), clf.size(), -1, -1, false);
  322. if (err != OK) {
  323. return err;
  324. }
  325. if (ep.step("Adding project files...", 3)) {
  326. return ERR_SKIP;
  327. }
  328. EditorNode::progress_add_task("project_files", "Project Files", 100);
  329. packager.set_progress_task("project_files");
  330. err = export_project_files(p_preset, p_debug, save_appx_file, &packager);
  331. EditorNode::progress_end_task("project_files");
  332. if (ep.step("Closing package...", 7)) {
  333. return ERR_SKIP;
  334. }
  335. unzClose(pkg);
  336. packager.finish();
  337. #ifdef WINDOWS_ENABLED
  338. // Sign with signtool
  339. String signtool_path = EditorSettings::get_singleton()->get("export/uwp/signtool");
  340. if (signtool_path.is_empty()) {
  341. return OK;
  342. }
  343. if (!FileAccess::exists(signtool_path)) {
  344. ERR_PRINT("Could not find signtool executable at " + signtool_path + ", aborting.");
  345. return ERR_FILE_NOT_FOUND;
  346. }
  347. static String algs[] = { "MD5", "SHA1", "SHA256" };
  348. String cert_path = EditorSettings::get_singleton()->get("export/uwp/debug_certificate");
  349. String cert_pass = EditorSettings::get_singleton()->get("export/uwp/debug_password");
  350. int cert_alg = EditorSettings::get_singleton()->get("export/uwp/debug_algorithm");
  351. if (!p_debug) {
  352. cert_path = p_preset->get("signing/certificate");
  353. cert_pass = p_preset->get("signing/password");
  354. cert_alg = p_preset->get("signing/algorithm");
  355. }
  356. if (cert_path.is_empty()) {
  357. return OK; // Certificate missing, don't try to sign
  358. }
  359. if (!FileAccess::exists(cert_path)) {
  360. ERR_PRINT("Could not find certificate file at " + cert_path + ", aborting.");
  361. return ERR_FILE_NOT_FOUND;
  362. }
  363. if (cert_alg < 0 || cert_alg > 2) {
  364. ERR_PRINT("Invalid certificate algorithm " + itos(cert_alg) + ", aborting.");
  365. return ERR_INVALID_DATA;
  366. }
  367. List<String> args;
  368. args.push_back("sign");
  369. args.push_back("/fd");
  370. args.push_back(algs[cert_alg]);
  371. args.push_back("/a");
  372. args.push_back("/f");
  373. args.push_back(cert_path);
  374. args.push_back("/p");
  375. args.push_back(cert_pass);
  376. args.push_back(p_path);
  377. OS::get_singleton()->execute(signtool_path, args);
  378. #endif // WINDOWS_ENABLED
  379. return OK;
  380. }
  381. void EditorExportPlatformUWP::get_platform_features(List<String> *r_features) const {
  382. r_features->push_back("pc");
  383. r_features->push_back("uwp");
  384. }
  385. void EditorExportPlatformUWP::resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, HashSet<String> &p_features) {
  386. }
  387. EditorExportPlatformUWP::EditorExportPlatformUWP() {
  388. logo = ImageTexture::create_from_image(memnew(Image(_uwp_logo)));
  389. }