export_plugin.h 11 KB

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