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
31#ifndef IOS_EXPORT_PLUGIN_H
32#define IOS_EXPORT_PLUGIN_H
33
34#include "godot_plugin_config.h"
35
36#include "core/config/project_settings.h"
37#include "core/io/file_access.h"
38#include "core/io/image_loader.h"
39#include "core/io/marshalls.h"
40#include "core/io/resource_saver.h"
41#include "core/io/zip_io.h"
42#include "core/os/os.h"
43#include "core/templates/safe_refcount.h"
44#include "core/version.h"
45#include "editor/editor_settings.h"
46#include "editor/export/editor_export_platform.h"
47#include "main/splash.gen.h"
48#include "scene/resources/image_texture.h"
49
50#include <string.h>
51#include <sys/stat.h>
52
53// Optional environment variables for defining confidential information. If any
54// of these is set, they will override the values set in the credentials file.
55const String ENV_IOS_PROFILE_UUID_DEBUG = "GODOT_IOS_PROVISIONING_PROFILE_UUID_DEBUG";
56const String ENV_IOS_PROFILE_UUID_RELEASE = "GODOT_IOS_PROVISIONING_PROFILE_UUID_RELEASE";
57
58class EditorExportPlatformIOS : public EditorExportPlatform {
59 GDCLASS(EditorExportPlatformIOS, EditorExportPlatform);
60
61 Ref<ImageTexture> ;
62 Ref<ImageTexture> run_icon;
63
64 // Plugins
65 mutable SafeFlag plugins_changed;
66 SafeFlag devices_changed;
67
68 struct Device {
69 String id;
70 String name;
71 bool simulator = false;
72 bool wifi = false;
73 };
74
75 Vector<Device> devices;
76 Mutex device_lock;
77
78 Mutex plugins_lock;
79 mutable Vector<PluginConfigIOS> plugins;
80#ifdef MACOS_ENABLED
81 Thread check_for_changes_thread;
82 SafeFlag quit_request;
83
84 static void _check_for_changes_poll_thread(void *ud);
85#endif
86
87 typedef Error (*FileHandler)(String p_file, void *p_userdata);
88 static Error _walk_dir_recursive(Ref<DirAccess> &p_da, FileHandler p_handler, void *p_userdata);
89 static Error _codesign(String p_file, void *p_userdata);
90 void _blend_and_rotate(Ref<Image> &p_dst, Ref<Image> &p_src, bool p_rot);
91
92 struct IOSConfigData {
93 String pkg_name;
94 String binary_name;
95 String plist_content;
96 String architectures;
97 String linker_flags;
98 String cpp_code;
99 String modules_buildfile;
100 String modules_fileref;
101 String modules_buildphase;
102 String modules_buildgrp;
103 Vector<String> capabilities;
104 bool use_swift_runtime;
105 };
106 struct ExportArchitecture {
107 String name;
108 bool is_default = false;
109
110 ExportArchitecture() {}
111
112 ExportArchitecture(String p_name, bool p_is_default) {
113 name = p_name;
114 is_default = p_is_default;
115 }
116 };
117
118 struct IOSExportAsset {
119 String exported_path;
120 bool is_framework = false; // framework is anything linked to the binary, otherwise it's a resource
121 bool should_embed = false;
122 };
123
124 String _get_additional_plist_content();
125 String _get_linker_flags();
126 String _get_cpp_code();
127 void _fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const IOSConfigData &p_config, bool p_debug);
128 Error _export_loading_screen_images(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir);
129 Error _export_loading_screen_file(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir);
130 Error _export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir);
131
132 Vector<ExportArchitecture> _get_supported_architectures() const;
133 Vector<String> _get_preset_architectures(const Ref<EditorExportPreset> &p_preset) const;
134
135 void _add_assets_to_project(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_project_data, const Vector<IOSExportAsset> &p_additional_assets);
136 Error _export_additional_assets(const String &p_out_dir, const Vector<String> &p_assets, bool p_is_framework, bool p_should_embed, Vector<IOSExportAsset> &r_exported_assets);
137 Error _copy_asset(const String &p_out_dir, const String &p_asset, const String *p_custom_file_name, bool p_is_framework, bool p_should_embed, Vector<IOSExportAsset> &r_exported_assets);
138 Error _export_additional_assets(const String &p_out_dir, const Vector<SharedObject> &p_libraries, Vector<IOSExportAsset> &r_exported_assets);
139 Error _export_ios_plugins(const Ref<EditorExportPreset> &p_preset, IOSConfigData &p_config_data, const String &dest_dir, Vector<IOSExportAsset> &r_exported_assets, bool p_debug);
140
141 Error _export_project_helper(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags, bool p_simulator, bool p_skip_ipa);
142
143 bool is_package_name_valid(const String &p_package, String *r_error = nullptr) const;
144
145protected:
146 virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const override;
147 virtual void get_export_options(List<ExportOption> *r_options) const override;
148 virtual bool get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const override;
149 virtual String get_export_option_warning(const EditorExportPreset *p_preset, const StringName &p_name) const override;
150
151public:
152 virtual String get_name() const override { return "iOS"; }
153 virtual String get_os_name() const override { return "iOS"; }
154 virtual Ref<Texture2D> get_logo() const override { return logo; }
155 virtual Ref<Texture2D> get_run_icon() const override { return run_icon; }
156
157 virtual int get_options_count() const override;
158 virtual String get_options_tooltip() const override;
159 virtual Ref<ImageTexture> get_option_icon(int p_index) const override;
160 virtual String get_option_label(int p_index) const override;
161 virtual String get_option_tooltip(int p_index) const override;
162 virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) override;
163
164 virtual bool poll_export() override {
165 bool dc = devices_changed.is_set();
166 if (dc) {
167 // don't clear unless we're reporting true, to avoid race
168 devices_changed.clear();
169 }
170 return dc;
171 }
172
173 virtual bool should_update_export_options() override {
174 bool export_options_changed = plugins_changed.is_set();
175 if (export_options_changed) {
176 // don't clear unless we're reporting true, to avoid race
177 plugins_changed.clear();
178 }
179 return export_options_changed;
180 }
181
182 virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const override {
183 List<String> list;
184 if (p_preset.is_valid()) {
185 bool project_only = p_preset->get("application/export_project_only");
186 if (project_only) {
187 list.push_back("xcodeproj");
188 } else {
189 list.push_back("ipa");
190 }
191 }
192 return list;
193 }
194
195 virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) override;
196
197 virtual bool has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug = false) const override;
198 virtual bool has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const override;
199
200 virtual void get_platform_features(List<String> *r_features) const override {
201 r_features->push_back("mobile");
202 r_features->push_back("ios");
203 }
204
205 virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, HashSet<String> &p_features) override {
206 }
207
208 EditorExportPlatformIOS();
209 ~EditorExportPlatformIOS();
210
211 /// List the gdip files in the directory specified by the p_path parameter.
212 static Vector<String> list_plugin_config_files(const String &p_path, bool p_check_directories) {
213 Vector<String> dir_files;
214 Ref<DirAccess> da = DirAccess::open(p_path);
215 if (da.is_valid()) {
216 da->list_dir_begin();
217 while (true) {
218 String file = da->get_next();
219 if (file.is_empty()) {
220 break;
221 }
222
223 if (file == "." || file == "..") {
224 continue;
225 }
226
227 if (da->current_is_hidden()) {
228 continue;
229 }
230
231 if (da->current_is_dir()) {
232 if (p_check_directories) {
233 Vector<String> directory_files = list_plugin_config_files(p_path.path_join(file), false);
234 for (int i = 0; i < directory_files.size(); ++i) {
235 dir_files.push_back(file.path_join(directory_files[i]));
236 }
237 }
238
239 continue;
240 }
241
242 if (file.ends_with(PluginConfigIOS::PLUGIN_CONFIG_EXT)) {
243 dir_files.push_back(file);
244 }
245 }
246 da->list_dir_end();
247 }
248
249 return dir_files;
250 }
251
252 static Vector<PluginConfigIOS> get_plugins() {
253 Vector<PluginConfigIOS> loaded_plugins;
254
255 String plugins_dir = ProjectSettings::get_singleton()->get_resource_path().path_join("ios/plugins");
256
257 if (DirAccess::exists(plugins_dir)) {
258 Vector<String> plugins_filenames = list_plugin_config_files(plugins_dir, true);
259
260 if (!plugins_filenames.is_empty()) {
261 Ref<ConfigFile> config_file = memnew(ConfigFile);
262 for (int i = 0; i < plugins_filenames.size(); i++) {
263 PluginConfigIOS config = PluginConfigIOS::load_plugin_config(config_file, plugins_dir.path_join(plugins_filenames[i]));
264 if (config.valid_config) {
265 loaded_plugins.push_back(config);
266 } else {
267 print_error("Invalid plugin config file " + plugins_filenames[i]);
268 }
269 }
270 }
271 }
272
273 return loaded_plugins;
274 }
275
276 static Vector<PluginConfigIOS> get_enabled_plugins(const Ref<EditorExportPreset> &p_presets) {
277 Vector<PluginConfigIOS> enabled_plugins;
278 Vector<PluginConfigIOS> all_plugins = get_plugins();
279 for (int i = 0; i < all_plugins.size(); i++) {
280 PluginConfigIOS plugin = all_plugins[i];
281 bool enabled = p_presets->get("plugins/" + plugin.name);
282 if (enabled) {
283 enabled_plugins.push_back(plugin);
284 }
285 }
286
287 return enabled_plugins;
288 }
289};
290
291#endif // IOS_EXPORT_PLUGIN_H
292