export_plugin.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**************************************************************************/
  2. /* export_plugin.cpp */
  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. #include "export_plugin.h"
  31. #include "logo_svg.gen.h"
  32. #include "run_icon_svg.gen.h"
  33. #include "editor/editor_node.h"
  34. Vector<String> EditorExportPlatformIOS::device_types({ "iPhone", "iPad" });
  35. void EditorExportPlatformIOS::initialize() {
  36. if (EditorNode::get_singleton()) {
  37. EditorExportPlatformAppleEmbedded::_initialize(_ios_logo_svg, _ios_run_icon_svg);
  38. #ifdef MACOS_ENABLED
  39. _start_remote_device_poller_thread();
  40. #endif
  41. }
  42. }
  43. EditorExportPlatformIOS::~EditorExportPlatformIOS() {
  44. }
  45. void EditorExportPlatformIOS::get_export_options(List<ExportOption> *r_options) const {
  46. EditorExportPlatformAppleEmbedded::get_export_options(r_options);
  47. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/targeted_device_family", PROPERTY_HINT_ENUM, "iPhone,iPad,iPhone & iPad"), 2));
  48. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/min_ios_version"), get_minimum_deployment_target()));
  49. r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "storyboard/image_scale_mode", PROPERTY_HINT_ENUM, "Same as Logo,Center,Scale to Fit,Scale to Fill,Scale"), 0));
  50. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@2x", PROPERTY_HINT_FILE_PATH, "*.png,*.jpg,*.jpeg"), ""));
  51. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "storyboard/custom_image@3x", PROPERTY_HINT_FILE_PATH, "*.png,*.jpg,*.jpeg"), ""));
  52. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "storyboard/use_custom_bg_color"), false));
  53. r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "storyboard/custom_bg_color"), Color()));
  54. }
  55. bool EditorExportPlatformIOS::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const {
  56. bool valid = EditorExportPlatformAppleEmbedded::has_valid_export_configuration(p_preset, r_error, r_missing_templates, p_debug);
  57. String err;
  58. String rendering_method = get_project_setting(p_preset, "rendering/renderer/rendering_method.mobile");
  59. String rendering_driver = get_project_setting(p_preset, "rendering/rendering_device/driver." + get_platform_name());
  60. if ((rendering_method == "forward_plus" || rendering_method == "mobile") && rendering_driver == "metal") {
  61. float version = p_preset->get("application/min_ios_version").operator String().to_float();
  62. if (version < 14.0) {
  63. err += TTR("Metal renderer require iOS 14+.") + "\n";
  64. }
  65. }
  66. if (!err.is_empty()) {
  67. if (!r_error.is_empty()) {
  68. r_error += err;
  69. } else {
  70. r_error = err;
  71. }
  72. }
  73. return valid;
  74. }
  75. HashMap<String, Variant> EditorExportPlatformIOS::get_custom_project_settings(const Ref<EditorExportPreset> &p_preset) const {
  76. HashMap<String, Variant> settings;
  77. int image_scale_mode = p_preset->get("storyboard/image_scale_mode");
  78. String value;
  79. switch (image_scale_mode) {
  80. case 0: {
  81. String logo_path = get_project_setting(p_preset, "application/boot_splash/image");
  82. bool is_on = get_project_setting(p_preset, "application/boot_splash/fullsize");
  83. // If custom logo is not specified, Godot does not scale default one, so we should do the same.
  84. value = (is_on && logo_path.length() > 0) ? "scaleAspectFit" : "center";
  85. } break;
  86. default: {
  87. value = storyboard_image_scale_mode[image_scale_mode - 1];
  88. }
  89. }
  90. settings["ios/launch_screen_image_mode"] = value;
  91. return settings;
  92. }
  93. Error EditorExportPlatformIOS::_export_loading_screen_file(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir) {
  94. const String custom_launch_image_2x = p_preset->get("storyboard/custom_image@2x");
  95. const String custom_launch_image_3x = p_preset->get("storyboard/custom_image@3x");
  96. if (custom_launch_image_2x.length() > 0 && custom_launch_image_3x.length() > 0) {
  97. String image_path = p_dest_dir.path_join("[email protected]");
  98. Error err = OK;
  99. Ref<Image> image = _load_icon_or_splash_image(custom_launch_image_2x, &err);
  100. if (err != OK || image.is_null() || image->is_empty()) {
  101. return err;
  102. }
  103. if (image->save_png(image_path) != OK) {
  104. return ERR_FILE_CANT_WRITE;
  105. }
  106. image_path = p_dest_dir.path_join("[email protected]");
  107. image = _load_icon_or_splash_image(custom_launch_image_3x, &err);
  108. if (err != OK || image.is_null() || image->is_empty()) {
  109. return err;
  110. }
  111. if (image->save_png(image_path) != OK) {
  112. return ERR_FILE_CANT_WRITE;
  113. }
  114. } else {
  115. Error err = OK;
  116. Ref<Image> splash;
  117. const String splash_path = get_project_setting(p_preset, "application/boot_splash/image");
  118. if (!splash_path.is_empty()) {
  119. splash = _load_icon_or_splash_image(splash_path, &err);
  120. }
  121. if (err != OK || splash.is_null() || splash->is_empty()) {
  122. splash.instantiate(boot_splash_png);
  123. }
  124. // Using same image for both @2x and @3x
  125. // because Godot's own boot logo uses single image for all resolutions.
  126. // Also not using @1x image, because devices using this image variant
  127. // are not supported by iOS 9, which is minimal target.
  128. const String splash_png_path_2x = p_dest_dir.path_join("[email protected]");
  129. const String splash_png_path_3x = p_dest_dir.path_join("[email protected]");
  130. if (splash->save_png(splash_png_path_2x) != OK) {
  131. return ERR_FILE_CANT_WRITE;
  132. }
  133. if (splash->save_png(splash_png_path_3x) != OK) {
  134. return ERR_FILE_CANT_WRITE;
  135. }
  136. }
  137. return OK;
  138. }
  139. Vector<EditorExportPlatformAppleEmbedded::IconInfo> EditorExportPlatformIOS::get_icon_infos() const {
  140. Vector<EditorExportPlatformAppleEmbedded::IconInfo> icon_infos;
  141. return {
  142. // Settings on iPhone, iPad Pro, iPad, iPad mini
  143. { PNAME("icons/settings_58x58"), "universal", "Icon-58", "58", "2x", "29x29", false },
  144. { PNAME("icons/settings_87x87"), "universal", "Icon-87", "87", "3x", "29x29", false },
  145. // Notifications on iPhone, iPad Pro, iPad, iPad mini
  146. { PNAME("icons/notification_40x40"), "universal", "Icon-40", "40", "2x", "20x20", false },
  147. { PNAME("icons/notification_60x60"), "universal", "Icon-60", "60", "3x", "20x20", false },
  148. { PNAME("icons/notification_76x76"), "universal", "Icon-76", "76", "2x", "38x38", false },
  149. { PNAME("icons/notification_114x114"), "universal", "Icon-114", "114", "3x", "38x38", false },
  150. // Spotlight on iPhone, iPad Pro, iPad, iPad mini
  151. { PNAME("icons/spotlight_80x80"), "universal", "Icon-80", "80", "2x", "40x40", false },
  152. { PNAME("icons/spotlight_120x120"), "universal", "Icon-120", "120", "3x", "40x40", false },
  153. // Home Screen on iPhone
  154. { PNAME("icons/iphone_120x120"), "universal", "Icon-120-1", "120", "2x", "60x60", false },
  155. { PNAME("icons/iphone_180x180"), "universal", "Icon-180", "180", "3x", "60x60", false },
  156. // Home Screen on iPad Pro
  157. { PNAME("icons/ipad_167x167"), "universal", "Icon-167", "167", "2x", "83.5x83.5", false },
  158. // Home Screen on iPad, iPad mini
  159. { PNAME("icons/ipad_152x152"), "universal", "Icon-152", "152", "2x", "76x76", false },
  160. { PNAME("icons/ios_128x128"), "universal", "Icon-128", "128", "2x", "64x64", false },
  161. { PNAME("icons/ios_192x192"), "universal", "Icon-192", "192", "3x", "64x64", false },
  162. { PNAME("icons/ios_136x136"), "universal", "Icon-136", "136", "2x", "68x68", false },
  163. // App Store
  164. { PNAME("icons/app_store_1024x1024"), "universal", "Icon-1024", "1024", "1x", "1024x1024", true },
  165. };
  166. }
  167. Error EditorExportPlatformIOS::_export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir) {
  168. String json_description = "{\"images\":[";
  169. String sizes;
  170. Ref<DirAccess> da = DirAccess::open(p_iconset_dir);
  171. if (da.is_null()) {
  172. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not open a directory at path \"%s\"."), p_iconset_dir));
  173. return ERR_CANT_OPEN;
  174. }
  175. Color boot_bg_color = get_project_setting(p_preset, "application/boot_splash/bg_color");
  176. enum IconColorMode {
  177. ICON_NORMAL,
  178. ICON_DARK,
  179. ICON_TINTED,
  180. ICON_MAX,
  181. };
  182. Vector<IconInfo> icon_infos = get_icon_infos();
  183. bool first_icon = true;
  184. for (int i = 0; i < icon_infos.size(); ++i) {
  185. for (int color_mode = ICON_NORMAL; color_mode < ICON_MAX; color_mode++) {
  186. IconInfo info = icon_infos[i];
  187. int side_size = String(info.actual_size_side).to_int();
  188. String key = info.preset_key;
  189. String exp_name = info.export_name;
  190. if (color_mode == ICON_DARK) {
  191. key += "_dark";
  192. exp_name += "_dark";
  193. } else if (color_mode == ICON_TINTED) {
  194. key += "_tinted";
  195. exp_name += "_tinted";
  196. }
  197. exp_name += ".png";
  198. String icon_path = p_preset->get(key);
  199. bool resize_waning = true;
  200. if (icon_path.is_empty()) {
  201. // Load and resize base icon.
  202. key = "icons/icon_1024x1024";
  203. if (color_mode == ICON_DARK) {
  204. key += "_dark";
  205. } else if (color_mode == ICON_TINTED) {
  206. key += "_tinted";
  207. }
  208. icon_path = p_preset->get(key);
  209. resize_waning = false;
  210. }
  211. if (icon_path.is_empty()) {
  212. if (color_mode != ICON_NORMAL) {
  213. continue;
  214. }
  215. // Resize main app icon.
  216. icon_path = get_project_setting(p_preset, "application/config/icon");
  217. Error err = OK;
  218. Ref<Image> img = _load_icon_or_splash_image(icon_path, &err);
  219. if (err != OK || img.is_null() || img->is_empty()) {
  220. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));
  221. return ERR_UNCONFIGURED;
  222. } else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {
  223. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  224. Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);
  225. new_img->fill(boot_bg_color);
  226. _blend_and_rotate(new_img, img, false);
  227. err = new_img->save_png(p_iconset_dir + exp_name);
  228. } else {
  229. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  230. err = img->save_png(p_iconset_dir + exp_name);
  231. }
  232. if (err) {
  233. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));
  234. return err;
  235. }
  236. } else {
  237. // Load custom icon and resize if required.
  238. Error err = OK;
  239. Ref<Image> img = _load_icon_or_splash_image(icon_path, &err);
  240. if (err != OK || img.is_null() || img->is_empty()) {
  241. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Invalid icon (%s): '%s'.", info.preset_key, icon_path));
  242. return ERR_UNCONFIGURED;
  243. } else if (info.force_opaque && img->detect_alpha() != Image::ALPHA_NONE) {
  244. if (resize_waning) {
  245. add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s) must be opaque.", info.preset_key));
  246. }
  247. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  248. Ref<Image> new_img = Image::create_empty(side_size, side_size, false, Image::FORMAT_RGBA8);
  249. new_img->fill(boot_bg_color);
  250. _blend_and_rotate(new_img, img, false);
  251. err = new_img->save_png(p_iconset_dir + exp_name);
  252. } else if (img->get_width() != side_size || img->get_height() != side_size) {
  253. if (resize_waning) {
  254. add_message(EXPORT_MESSAGE_WARNING, TTR("Export Icons"), vformat("Icon (%s): '%s' has incorrect size %s and was automatically resized to %s.", info.preset_key, icon_path, img->get_size(), Vector2i(side_size, side_size)));
  255. }
  256. img->resize(side_size, side_size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int()));
  257. err = img->save_png(p_iconset_dir + exp_name);
  258. } else if (!icon_path.ends_with(".png")) {
  259. err = img->save_png(p_iconset_dir + exp_name);
  260. } else {
  261. err = da->copy(icon_path, p_iconset_dir + exp_name);
  262. }
  263. if (err) {
  264. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat("Failed to export icon (%s): '%s'.", info.preset_key, icon_path));
  265. return err;
  266. }
  267. }
  268. sizes += String(info.actual_size_side) + "\n";
  269. if (first_icon) {
  270. first_icon = false;
  271. } else {
  272. json_description += ",";
  273. }
  274. json_description += String("{");
  275. if (color_mode != ICON_NORMAL) {
  276. json_description += String("\"appearances\":[{");
  277. json_description += String("\"appearance\":\"luminosity\",");
  278. if (color_mode == ICON_DARK) {
  279. json_description += String("\"value\":\"dark\"");
  280. } else if (color_mode == ICON_TINTED) {
  281. json_description += String("\"value\":\"tinted\"");
  282. }
  283. json_description += String("}],");
  284. }
  285. json_description += String("\"idiom\":") + "\"" + info.idiom + "\",";
  286. json_description += String("\"platform\":\"" + get_platform_name() + "\",");
  287. json_description += String("\"size\":") + "\"" + info.unscaled_size + "\",";
  288. if (String(info.scale) != "1x") {
  289. json_description += String("\"scale\":") + "\"" + info.scale + "\",";
  290. }
  291. json_description += String("\"filename\":") + "\"" + exp_name + "\"";
  292. json_description += String("}");
  293. }
  294. }
  295. json_description += "],\"info\":{\"author\":\"xcode\",\"version\":1}}";
  296. Ref<FileAccess> json_file = FileAccess::open(p_iconset_dir + "Contents.json", FileAccess::WRITE);
  297. if (json_file.is_null()) {
  298. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "Contents.json"));
  299. return ERR_CANT_CREATE;
  300. }
  301. CharString json_utf8 = json_description.utf8();
  302. json_file->store_buffer((const uint8_t *)json_utf8.get_data(), json_utf8.length());
  303. Ref<FileAccess> sizes_file = FileAccess::open(p_iconset_dir + "sizes", FileAccess::WRITE);
  304. if (sizes_file.is_null()) {
  305. add_message(EXPORT_MESSAGE_ERROR, TTR("Export Icons"), vformat(TTR("Could not write to a file at path \"%s\"."), p_iconset_dir + "sizes"));
  306. return ERR_CANT_CREATE;
  307. }
  308. CharString sizes_utf8 = sizes.utf8();
  309. sizes_file->store_buffer((const uint8_t *)sizes_utf8.get_data(), sizes_utf8.length());
  310. return OK;
  311. }