export_plugin.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 <string.h>
  46. #include <sys/stat.h>
  47. // Optional environment variables for defining confidential information. If any
  48. // of these is set, they will override the values set in the credentials file.
  49. const String ENV_IOS_PROFILE_UUID_DEBUG = "GODOT_IOS_PROVISIONING_PROFILE_UUID_DEBUG";
  50. const String ENV_IOS_PROFILE_UUID_RELEASE = "GODOT_IOS_PROVISIONING_PROFILE_UUID_RELEASE";
  51. class EditorExportPlatformIOS : public EditorExportPlatform {
  52. GDCLASS(EditorExportPlatformIOS, EditorExportPlatform);
  53. Ref<ImageTexture> logo;
  54. // Plugins
  55. mutable SafeFlag plugins_changed;
  56. #ifndef ANDROID_ENABLED
  57. Thread check_for_changes_thread;
  58. SafeFlag quit_request;
  59. #endif
  60. Mutex plugins_lock;
  61. mutable Vector<PluginConfigIOS> plugins;
  62. typedef Error (*FileHandler)(String p_file, void *p_userdata);
  63. static Error _walk_dir_recursive(Ref<DirAccess> &p_da, FileHandler p_handler, void *p_userdata);
  64. static Error _codesign(String p_file, void *p_userdata);
  65. void _blend_and_rotate(Ref<Image> &p_dst, Ref<Image> &p_src, bool p_rot);
  66. struct IOSConfigData {
  67. String pkg_name;
  68. String binary_name;
  69. String plist_content;
  70. String architectures;
  71. String linker_flags;
  72. String cpp_code;
  73. String modules_buildfile;
  74. String modules_fileref;
  75. String modules_buildphase;
  76. String modules_buildgrp;
  77. Vector<String> capabilities;
  78. bool use_swift_runtime;
  79. };
  80. struct ExportArchitecture {
  81. String name;
  82. bool is_default = false;
  83. ExportArchitecture() {}
  84. ExportArchitecture(String p_name, bool p_is_default) {
  85. name = p_name;
  86. is_default = p_is_default;
  87. }
  88. };
  89. struct IOSExportAsset {
  90. String exported_path;
  91. bool is_framework = false; // framework is anything linked to the binary, otherwise it's a resource
  92. bool should_embed = false;
  93. };
  94. String _get_additional_plist_content();
  95. String _get_linker_flags();
  96. String _get_cpp_code();
  97. void _fix_config_file(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &pfile, const IOSConfigData &p_config, bool p_debug);
  98. Error _export_loading_screen_images(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir);
  99. Error _export_loading_screen_file(const Ref<EditorExportPreset> &p_preset, const String &p_dest_dir);
  100. Error _export_icons(const Ref<EditorExportPreset> &p_preset, const String &p_iconset_dir);
  101. Vector<ExportArchitecture> _get_supported_architectures() const;
  102. Vector<String> _get_preset_architectures(const Ref<EditorExportPreset> &p_preset) const;
  103. void _add_assets_to_project(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_project_data, const Vector<IOSExportAsset> &p_additional_assets);
  104. 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);
  105. 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);
  106. Error _export_additional_assets(const String &p_out_dir, const Vector<SharedObject> &p_libraries, Vector<IOSExportAsset> &r_exported_assets);
  107. 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);
  108. bool is_package_name_valid(const String &p_package, String *r_error = nullptr) const {
  109. String pname = p_package;
  110. if (pname.length() == 0) {
  111. if (r_error) {
  112. *r_error = TTR("Identifier is missing.");
  113. }
  114. return false;
  115. }
  116. for (int i = 0; i < pname.length(); i++) {
  117. char32_t c = pname[i];
  118. if (!(is_ascii_alphanumeric_char(c) || c == '-' || c == '.')) {
  119. if (r_error) {
  120. *r_error = vformat(TTR("The character '%s' is not allowed in Identifier."), String::chr(c));
  121. }
  122. return false;
  123. }
  124. }
  125. return true;
  126. }
  127. #ifndef ANDROID_ENABLED
  128. static void _check_for_changes_poll_thread(void *ud) {
  129. EditorExportPlatformIOS *ea = static_cast<EditorExportPlatformIOS *>(ud);
  130. while (!ea->quit_request.is_set()) {
  131. // Nothing to do if we already know the plugins have changed.
  132. if (!ea->plugins_changed.is_set()) {
  133. MutexLock lock(ea->plugins_lock);
  134. Vector<PluginConfigIOS> loaded_plugins = get_plugins();
  135. if (ea->plugins.size() != loaded_plugins.size()) {
  136. ea->plugins_changed.set();
  137. } else {
  138. for (int i = 0; i < ea->plugins.size(); i++) {
  139. if (ea->plugins[i].name != loaded_plugins[i].name || ea->plugins[i].last_updated != loaded_plugins[i].last_updated) {
  140. ea->plugins_changed.set();
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. uint64_t wait = 3000000;
  147. uint64_t time = OS::get_singleton()->get_ticks_usec();
  148. while (OS::get_singleton()->get_ticks_usec() - time < wait) {
  149. OS::get_singleton()->delay_usec(300000);
  150. if (ea->quit_request.is_set()) {
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. #endif
  157. protected:
  158. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const override;
  159. virtual void get_export_options(List<ExportOption> *r_options) const override;
  160. virtual bool get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const override;
  161. virtual String get_export_option_warning(const EditorExportPreset *p_preset, const StringName &p_name) const override;
  162. public:
  163. virtual String get_name() const override { return "iOS"; }
  164. virtual String get_os_name() const override { return "iOS"; }
  165. virtual Ref<Texture2D> get_logo() const override { return logo; }
  166. virtual bool should_update_export_options() override {
  167. bool export_options_changed = plugins_changed.is_set();
  168. if (export_options_changed) {
  169. // don't clear unless we're reporting true, to avoid race
  170. plugins_changed.clear();
  171. }
  172. return export_options_changed;
  173. }
  174. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const override {
  175. List<String> list;
  176. list.push_back("ipa");
  177. return list;
  178. }
  179. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) override;
  180. virtual bool has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug = false) const override;
  181. virtual bool has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const override;
  182. virtual void get_platform_features(List<String> *r_features) const override {
  183. r_features->push_back("mobile");
  184. r_features->push_back("ios");
  185. }
  186. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, HashSet<String> &p_features) override {
  187. }
  188. EditorExportPlatformIOS();
  189. ~EditorExportPlatformIOS();
  190. /// List the gdip files in the directory specified by the p_path parameter.
  191. static Vector<String> list_plugin_config_files(const String &p_path, bool p_check_directories) {
  192. Vector<String> dir_files;
  193. Ref<DirAccess> da = DirAccess::open(p_path);
  194. if (da.is_valid()) {
  195. da->list_dir_begin();
  196. while (true) {
  197. String file = da->get_next();
  198. if (file.is_empty()) {
  199. break;
  200. }
  201. if (file == "." || file == "..") {
  202. continue;
  203. }
  204. if (da->current_is_hidden()) {
  205. continue;
  206. }
  207. if (da->current_is_dir()) {
  208. if (p_check_directories) {
  209. Vector<String> directory_files = list_plugin_config_files(p_path.path_join(file), false);
  210. for (int i = 0; i < directory_files.size(); ++i) {
  211. dir_files.push_back(file.path_join(directory_files[i]));
  212. }
  213. }
  214. continue;
  215. }
  216. if (file.ends_with(PluginConfigIOS::PLUGIN_CONFIG_EXT)) {
  217. dir_files.push_back(file);
  218. }
  219. }
  220. da->list_dir_end();
  221. }
  222. return dir_files;
  223. }
  224. static Vector<PluginConfigIOS> get_plugins() {
  225. Vector<PluginConfigIOS> loaded_plugins;
  226. String plugins_dir = ProjectSettings::get_singleton()->get_resource_path().path_join("ios/plugins");
  227. if (DirAccess::exists(plugins_dir)) {
  228. Vector<String> plugins_filenames = list_plugin_config_files(plugins_dir, true);
  229. if (!plugins_filenames.is_empty()) {
  230. Ref<ConfigFile> config_file = memnew(ConfigFile);
  231. for (int i = 0; i < plugins_filenames.size(); i++) {
  232. PluginConfigIOS config = PluginConfigIOS::load_plugin_config(config_file, plugins_dir.path_join(plugins_filenames[i]));
  233. if (config.valid_config) {
  234. loaded_plugins.push_back(config);
  235. } else {
  236. print_error("Invalid plugin config file " + plugins_filenames[i]);
  237. }
  238. }
  239. }
  240. }
  241. return loaded_plugins;
  242. }
  243. static Vector<PluginConfigIOS> get_enabled_plugins(const Ref<EditorExportPreset> &p_presets) {
  244. Vector<PluginConfigIOS> enabled_plugins;
  245. Vector<PluginConfigIOS> all_plugins = get_plugins();
  246. for (int i = 0; i < all_plugins.size(); i++) {
  247. PluginConfigIOS plugin = all_plugins[i];
  248. bool enabled = p_presets->get("plugins/" + plugin.name);
  249. if (enabled) {
  250. enabled_plugins.push_back(plugin);
  251. }
  252. }
  253. return enabled_plugins;
  254. }
  255. };
  256. #endif // IOS_EXPORT_PLUGIN_H