export_plugin.h 18 KB

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