export_plugin.h 12 KB

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