export_plugin.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*************************************************************************/
  2. /* export_plugin.h */
  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. #ifndef UWP_EXPORT_PLUGIN_H
  31. #define UWP_EXPORT_PLUGIN_H
  32. #include "core/config/project_settings.h"
  33. #include "core/crypto/crypto_core.h"
  34. #include "core/io/dir_access.h"
  35. #include "core/io/file_access.h"
  36. #include "core/io/marshalls.h"
  37. #include "core/io/zip_io.h"
  38. #include "core/object/class_db.h"
  39. #include "core/version.h"
  40. #include "editor/editor_node.h"
  41. #include "editor/editor_paths.h"
  42. #include "editor/export/editor_export_platform.h"
  43. #include "thirdparty/minizip/unzip.h"
  44. #include "thirdparty/minizip/zip.h"
  45. #include "app_packager.h"
  46. #include <zlib.h>
  47. // Capabilities
  48. static const char *uwp_capabilities[] = {
  49. "allJoyn",
  50. "codeGeneration",
  51. "internetClient",
  52. "internetClientServer",
  53. "privateNetworkClientServer",
  54. nullptr
  55. };
  56. static const char *uwp_uap_capabilities[] = {
  57. "appointments",
  58. "blockedChatMessages",
  59. "chat",
  60. "contacts",
  61. "enterpriseAuthentication",
  62. "musicLibrary",
  63. "objects3D",
  64. "picturesLibrary",
  65. "phoneCall",
  66. "removableStorage",
  67. "sharedUserCertificates",
  68. "userAccountInformation",
  69. "videosLibrary",
  70. "voipCall",
  71. nullptr
  72. };
  73. static const char *uwp_device_capabilities[] = {
  74. "bluetooth",
  75. "location",
  76. "microphone",
  77. "proximity",
  78. "webcam",
  79. nullptr
  80. };
  81. class EditorExportPlatformUWP : public EditorExportPlatform {
  82. GDCLASS(EditorExportPlatformUWP, EditorExportPlatform);
  83. Ref<ImageTexture> logo;
  84. bool _valid_resource_name(const String &p_name) const {
  85. if (p_name.is_empty()) {
  86. return false;
  87. }
  88. if (p_name.ends_with(".")) {
  89. return false;
  90. }
  91. static const char *invalid_names[] = {
  92. "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7",
  93. "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
  94. nullptr
  95. };
  96. const char **t = invalid_names;
  97. while (*t) {
  98. if (p_name == *t) {
  99. return false;
  100. }
  101. t++;
  102. }
  103. return true;
  104. }
  105. bool _valid_guid(const String &p_guid) const {
  106. Vector<String> parts = p_guid.split("-");
  107. if (parts.size() != 5) {
  108. return false;
  109. }
  110. if (parts[0].length() != 8) {
  111. return false;
  112. }
  113. for (int i = 1; i < 4; i++) {
  114. if (parts[i].length() != 4) {
  115. return false;
  116. }
  117. }
  118. if (parts[4].length() != 12) {
  119. return false;
  120. }
  121. return true;
  122. }
  123. bool _valid_bgcolor(const String &p_color) const {
  124. if (p_color.is_empty()) {
  125. return true;
  126. }
  127. if (p_color.begins_with("#") && p_color.is_valid_html_color()) {
  128. return true;
  129. }
  130. // Colors from https://msdn.microsoft.com/en-us/library/windows/apps/dn934817.aspx
  131. static const char *valid_colors[] = {
  132. "aliceBlue", "antiqueWhite", "aqua", "aquamarine", "azure", "beige",
  133. "bisque", "black", "blanchedAlmond", "blue", "blueViolet", "brown",
  134. "burlyWood", "cadetBlue", "chartreuse", "chocolate", "coral", "cornflowerBlue",
  135. "cornsilk", "crimson", "cyan", "darkBlue", "darkCyan", "darkGoldenrod",
  136. "darkGray", "darkGreen", "darkKhaki", "darkMagenta", "darkOliveGreen", "darkOrange",
  137. "darkOrchid", "darkRed", "darkSalmon", "darkSeaGreen", "darkSlateBlue", "darkSlateGray",
  138. "darkTurquoise", "darkViolet", "deepPink", "deepSkyBlue", "dimGray", "dodgerBlue",
  139. "firebrick", "floralWhite", "forestGreen", "fuchsia", "gainsboro", "ghostWhite",
  140. "gold", "goldenrod", "gray", "green", "greenYellow", "honeydew",
  141. "hotPink", "indianRed", "indigo", "ivory", "khaki", "lavender",
  142. "lavenderBlush", "lawnGreen", "lemonChiffon", "lightBlue", "lightCoral", "lightCyan",
  143. "lightGoldenrodYellow", "lightGreen", "lightGray", "lightPink", "lightSalmon", "lightSeaGreen",
  144. "lightSkyBlue", "lightSlateGray", "lightSteelBlue", "lightYellow", "lime", "limeGreen",
  145. "linen", "magenta", "maroon", "mediumAquamarine", "mediumBlue", "mediumOrchid",
  146. "mediumPurple", "mediumSeaGreen", "mediumSlateBlue", "mediumSpringGreen", "mediumTurquoise", "mediumVioletRed",
  147. "midnightBlue", "mintCream", "mistyRose", "moccasin", "navajoWhite", "navy",
  148. "oldLace", "olive", "oliveDrab", "orange", "orangeRed", "orchid",
  149. "paleGoldenrod", "paleGreen", "paleTurquoise", "paleVioletRed", "papayaWhip", "peachPuff",
  150. "peru", "pink", "plum", "powderBlue", "purple", "red",
  151. "rosyBrown", "royalBlue", "saddleBrown", "salmon", "sandyBrown", "seaGreen",
  152. "seaShell", "sienna", "silver", "skyBlue", "slateBlue", "slateGray",
  153. "snow", "springGreen", "steelBlue", "tan", "teal", "thistle",
  154. "tomato", "transparent", "turquoise", "violet", "wheat", "white",
  155. "whiteSmoke", "yellow", "yellowGreen",
  156. nullptr
  157. };
  158. const char **color = valid_colors;
  159. while (*color) {
  160. if (p_color == *color) {
  161. return true;
  162. }
  163. color++;
  164. }
  165. return false;
  166. }
  167. bool _valid_image(const CompressedTexture2D *p_image, int p_width, int p_height) const {
  168. if (!p_image) {
  169. return false;
  170. }
  171. // TODO: Add resource creation or image rescaling to enable other scales:
  172. // 1.25, 1.5, 2.0
  173. return p_width == p_image->get_width() && p_height == p_image->get_height();
  174. }
  175. Vector<uint8_t> _fix_manifest(const Ref<EditorExportPreset> &p_preset, const Vector<uint8_t> &p_template, bool p_give_internet) const {
  176. String result = String::utf8((const char *)p_template.ptr(), p_template.size());
  177. result = result.replace("$godot_version$", VERSION_FULL_NAME);
  178. result = result.replace("$identity_name$", p_preset->get("package/unique_name"));
  179. result = result.replace("$publisher$", p_preset->get("package/publisher"));
  180. result = result.replace("$product_guid$", p_preset->get("identity/product_guid"));
  181. result = result.replace("$publisher_guid$", p_preset->get("identity/publisher_guid"));
  182. String version = itos(p_preset->get("version/major")) + "." + itos(p_preset->get("version/minor")) + "." + itos(p_preset->get("version/build")) + "." + itos(p_preset->get("version/revision"));
  183. result = result.replace("$version_string$", version);
  184. String arch = p_preset->get("binary_format/architecture");
  185. String architecture = arch == "arm32" ? "arm" : (arch == "x86_32" ? "x86" : "x64");
  186. result = result.replace("$architecture$", architecture);
  187. result = result.replace("$display_name$", String(p_preset->get("package/display_name")).is_empty() ? (String)GLOBAL_GET("application/config/name") : String(p_preset->get("package/display_name")));
  188. result = result.replace("$publisher_display_name$", p_preset->get("package/publisher_display_name"));
  189. result = result.replace("$app_description$", p_preset->get("package/description"));
  190. result = result.replace("$bg_color$", p_preset->get("images/background_color"));
  191. result = result.replace("$short_name$", p_preset->get("package/short_name"));
  192. String name_on_tiles = "";
  193. if ((bool)p_preset->get("tiles/show_name_on_square150x150")) {
  194. name_on_tiles += " <uap:ShowOn Tile=\"square150x150Logo\" />\n";
  195. }
  196. if ((bool)p_preset->get("tiles/show_name_on_wide310x150")) {
  197. name_on_tiles += " <uap:ShowOn Tile=\"wide310x150Logo\" />\n";
  198. }
  199. if ((bool)p_preset->get("tiles/show_name_on_square310x310")) {
  200. name_on_tiles += " <uap:ShowOn Tile=\"square310x310Logo\" />\n";
  201. }
  202. String show_name_on_tiles = "";
  203. if (!name_on_tiles.is_empty()) {
  204. show_name_on_tiles = "<uap:ShowNameOnTiles>\n" + name_on_tiles + " </uap:ShowNameOnTiles>";
  205. }
  206. result = result.replace("$name_on_tiles$", name_on_tiles);
  207. String rotations = "";
  208. if ((bool)p_preset->get("orientation/landscape")) {
  209. rotations += " <uap:Rotation Preference=\"landscape\" />\n";
  210. }
  211. if ((bool)p_preset->get("orientation/portrait")) {
  212. rotations += " <uap:Rotation Preference=\"portrait\" />\n";
  213. }
  214. if ((bool)p_preset->get("orientation/landscape_flipped")) {
  215. rotations += " <uap:Rotation Preference=\"landscapeFlipped\" />\n";
  216. }
  217. if ((bool)p_preset->get("orientation/portrait_flipped")) {
  218. rotations += " <uap:Rotation Preference=\"portraitFlipped\" />\n";
  219. }
  220. String rotation_preference = "";
  221. if (!rotations.is_empty()) {
  222. rotation_preference = "<uap:InitialRotationPreference>\n" + rotations + " </uap:InitialRotationPreference>";
  223. }
  224. result = result.replace("$rotation_preference$", rotation_preference);
  225. String capabilities_elements = "";
  226. const char **basic = uwp_capabilities;
  227. while (*basic) {
  228. if ((bool)p_preset->get("capabilities/" + String(*basic))) {
  229. capabilities_elements += " <Capability Name=\"" + String(*basic) + "\" />\n";
  230. }
  231. basic++;
  232. }
  233. const char **uap = uwp_uap_capabilities;
  234. while (*uap) {
  235. if ((bool)p_preset->get("capabilities/" + String(*uap))) {
  236. capabilities_elements += " <uap:Capability Name=\"" + String(*uap) + "\" />\n";
  237. }
  238. uap++;
  239. }
  240. const char **device = uwp_device_capabilities;
  241. while (*device) {
  242. if ((bool)p_preset->get("capabilities/" + String(*device))) {
  243. capabilities_elements += " <DeviceCapability Name=\"" + String(*device) + "\" />\n";
  244. }
  245. device++;
  246. }
  247. if (!((bool)p_preset->get("capabilities/internetClient")) && p_give_internet) {
  248. capabilities_elements += " <Capability Name=\"internetClient\" />\n";
  249. }
  250. String capabilities_string = "<Capabilities />";
  251. if (!capabilities_elements.is_empty()) {
  252. capabilities_string = "<Capabilities>\n" + capabilities_elements + " </Capabilities>";
  253. }
  254. result = result.replace("$capabilities_place$", capabilities_string);
  255. Vector<uint8_t> r_ret;
  256. r_ret.resize(result.length());
  257. for (int i = 0; i < result.length(); i++) {
  258. r_ret.write[i] = result.utf8().get(i);
  259. }
  260. return r_ret;
  261. }
  262. Vector<uint8_t> _get_image_data(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  263. Vector<uint8_t> data;
  264. CompressedTexture2D *texture = nullptr;
  265. if (p_path.find("StoreLogo") != -1) {
  266. texture = p_preset->get("images/store_logo").is_zero() ? nullptr : Object::cast_to<CompressedTexture2D>(((Object *)p_preset->get("images/store_logo")));
  267. } else if (p_path.find("Square44x44Logo") != -1) {
  268. texture = p_preset->get("images/square44x44_logo").is_zero() ? nullptr : Object::cast_to<CompressedTexture2D>(((Object *)p_preset->get("images/square44x44_logo")));
  269. } else if (p_path.find("Square71x71Logo") != -1) {
  270. texture = p_preset->get("images/square71x71_logo").is_zero() ? nullptr : Object::cast_to<CompressedTexture2D>(((Object *)p_preset->get("images/square71x71_logo")));
  271. } else if (p_path.find("Square150x150Logo") != -1) {
  272. texture = p_preset->get("images/square150x150_logo").is_zero() ? nullptr : Object::cast_to<CompressedTexture2D>(((Object *)p_preset->get("images/square150x150_logo")));
  273. } else if (p_path.find("Square310x310Logo") != -1) {
  274. texture = p_preset->get("images/square310x310_logo").is_zero() ? nullptr : Object::cast_to<CompressedTexture2D>(((Object *)p_preset->get("images/square310x310_logo")));
  275. } else if (p_path.find("Wide310x150Logo") != -1) {
  276. texture = p_preset->get("images/wide310x150_logo").is_zero() ? nullptr : Object::cast_to<CompressedTexture2D>(((Object *)p_preset->get("images/wide310x150_logo")));
  277. } else if (p_path.find("SplashScreen") != -1) {
  278. texture = p_preset->get("images/splash_screen").is_zero() ? nullptr : Object::cast_to<CompressedTexture2D>(((Object *)p_preset->get("images/splash_screen")));
  279. } else {
  280. ERR_PRINT("Unable to load logo");
  281. }
  282. if (!texture) {
  283. return data;
  284. }
  285. String tmp_path = EditorPaths::get_singleton()->get_cache_dir().path_join("uwp_tmp_logo.png");
  286. Error err = texture->get_image()->save_png(tmp_path);
  287. if (err != OK) {
  288. String err_string = "Couldn't save temp logo file.";
  289. EditorNode::add_io_error(err_string);
  290. ERR_FAIL_V_MSG(data, err_string);
  291. }
  292. {
  293. Ref<FileAccess> f = FileAccess::open(tmp_path, FileAccess::READ, &err);
  294. if (err != OK) {
  295. String err_string = "Couldn't open temp logo file.";
  296. // Cleanup generated file.
  297. DirAccess::remove_file_or_error(tmp_path);
  298. EditorNode::add_io_error(err_string);
  299. ERR_FAIL_V_MSG(data, err_string);
  300. }
  301. data.resize(f->get_length());
  302. f->get_buffer(data.ptrw(), data.size());
  303. }
  304. DirAccess::remove_file_or_error(tmp_path);
  305. return data;
  306. }
  307. static bool _should_compress_asset(const String &p_path, const Vector<uint8_t> &p_data) {
  308. /* TODO: This was copied verbatim from Android export. It should be
  309. * refactored to the parent class and also be used for .zip export.
  310. */
  311. /*
  312. * By not compressing files with little or not benefit in doing so,
  313. * a performance gain is expected at runtime. Moreover, if the APK is
  314. * zip-aligned, assets stored as they are can be efficiently read by
  315. * Android by memory-mapping them.
  316. */
  317. // -- Unconditional uncompress to mimic AAPT plus some other
  318. static const char *unconditional_compress_ext[] = {
  319. // From https://github.com/android/platform_frameworks_base/blob/master/tools/aapt/Package.cpp
  320. // These formats are already compressed, or don't compress well:
  321. ".jpg", ".jpeg", ".png", ".gif",
  322. ".wav", ".mp2", ".mp3", ".ogg", ".aac",
  323. ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
  324. ".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
  325. ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
  326. ".amr", ".awb", ".wma", ".wmv",
  327. // Godot-specific:
  328. ".webp", // Same reasoning as .png
  329. ".cfb", // Don't let small config files slow-down startup
  330. ".scn", // Binary scenes are usually already compressed
  331. ".ctex", // Streamable textures are usually already compressed
  332. // Trailer for easier processing
  333. nullptr
  334. };
  335. for (const char **ext = unconditional_compress_ext; *ext; ++ext) {
  336. if (p_path.to_lower().ends_with(String(*ext))) {
  337. return false;
  338. }
  339. }
  340. // -- Compressed resource?
  341. if (p_data.size() >= 4 && p_data[0] == 'R' && p_data[1] == 'S' && p_data[2] == 'C' && p_data[3] == 'C') {
  342. // Already compressed
  343. return false;
  344. }
  345. // --- TODO: Decide on texture resources according to their image compression setting
  346. return true;
  347. }
  348. static Error save_appx_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key) {
  349. AppxPackager *packager = static_cast<AppxPackager *>(p_userdata);
  350. String dst_path = p_path.replace_first("res://", "game/");
  351. return packager->add_file(dst_path, p_data.ptr(), p_data.size(), p_file, p_total, _should_compress_asset(p_path, p_data));
  352. }
  353. public:
  354. virtual String get_name() const override;
  355. virtual String get_os_name() const override;
  356. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const override;
  357. virtual Ref<Texture2D> get_logo() const override;
  358. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const override;
  359. virtual void get_export_options(List<ExportOption> *r_options) override;
  360. virtual bool has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const override;
  361. virtual bool has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const override;
  362. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) override;
  363. virtual void get_platform_features(List<String> *r_features) const override;
  364. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, HashSet<String> &p_features) override;
  365. EditorExportPlatformUWP();
  366. };
  367. #endif // UWP_EXPORT_PLUGIN_H