export_plugin.cpp 18 KB

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