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