export_plugin.cpp 19 KB

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