godot_plugin_config.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*************************************************************************/
  2. /* godot_plugin_config.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 GODOT_PLUGIN_CONFIG_H
  31. #define GODOT_PLUGIN_CONFIG_H
  32. #include "core/error/error_list.h"
  33. #include "core/io/config_file.h"
  34. #include "core/string/ustring.h"
  35. /*
  36. The `config` section and fields are required and defined as follow:
  37. - **name**: name of the plugin
  38. - **binary**: path to static `.a` library
  39. The `dependencies` and fields are optional.
  40. - **linked**: dependencies that should only be linked.
  41. - **embedded**: dependencies that should be linked and embedded into application.
  42. - **system**: system dependencies that should be linked.
  43. - **capabilities**: capabilities that would be used for `UIRequiredDeviceCapabilities` options in Info.plist file.
  44. - **files**: files that would be copied into application
  45. The `plist` section are optional.
  46. - **key**: key and value that would be added in Info.plist file.
  47. */
  48. struct PluginConfigIOS {
  49. inline static const char *PLUGIN_CONFIG_EXT = ".gdip";
  50. inline static const char *CONFIG_SECTION = "config";
  51. inline static const char *CONFIG_NAME_KEY = "name";
  52. inline static const char *CONFIG_BINARY_KEY = "binary";
  53. inline static const char *CONFIG_INITIALIZE_KEY = "initialization";
  54. inline static const char *CONFIG_DEINITIALIZE_KEY = "deinitialization";
  55. inline static const char *DEPENDENCIES_SECTION = "dependencies";
  56. inline static const char *DEPENDENCIES_LINKED_KEY = "linked";
  57. inline static const char *DEPENDENCIES_EMBEDDED_KEY = "embedded";
  58. inline static const char *DEPENDENCIES_SYSTEM_KEY = "system";
  59. inline static const char *DEPENDENCIES_CAPABILITIES_KEY = "capabilities";
  60. inline static const char *DEPENDENCIES_FILES_KEY = "files";
  61. inline static const char *DEPENDENCIES_LINKER_FLAGS = "linker_flags";
  62. inline static const char *PLIST_SECTION = "plist";
  63. // Set to true when the config file is properly loaded.
  64. bool valid_config = false;
  65. bool supports_targets = false;
  66. // Unix timestamp of last change to this plugin.
  67. uint64_t last_updated = 0;
  68. // Required config section
  69. String name;
  70. String binary;
  71. String initialization_method;
  72. String deinitialization_method;
  73. // Optional dependencies section
  74. Vector<String> linked_dependencies;
  75. Vector<String> embedded_dependencies;
  76. Vector<String> system_dependencies;
  77. Vector<String> files_to_copy;
  78. Vector<String> capabilities;
  79. Vector<String> linker_flags;
  80. // Optional plist section
  81. // Supports only string types for now
  82. HashMap<String, String> plist;
  83. };
  84. static inline String resolve_local_dependency_path(String plugin_config_dir, String dependency_path) {
  85. String absolute_path;
  86. if (dependency_path.is_empty()) {
  87. return absolute_path;
  88. }
  89. if (dependency_path.is_abs_path()) {
  90. return dependency_path;
  91. }
  92. String res_path = ProjectSettings::get_singleton()->globalize_path("res://");
  93. absolute_path = plugin_config_dir.plus_file(dependency_path);
  94. return absolute_path.replace(res_path, "res://");
  95. }
  96. static inline String resolve_system_dependency_path(String dependency_path) {
  97. String absolute_path;
  98. if (dependency_path.is_empty()) {
  99. return absolute_path;
  100. }
  101. if (dependency_path.is_abs_path()) {
  102. return dependency_path;
  103. }
  104. String system_path = "/System/Library/Frameworks";
  105. return system_path.plus_file(dependency_path);
  106. }
  107. static inline Vector<String> resolve_local_dependencies(String plugin_config_dir, Vector<String> p_paths) {
  108. Vector<String> paths;
  109. for (int i = 0; i < p_paths.size(); i++) {
  110. String path = resolve_local_dependency_path(plugin_config_dir, p_paths[i]);
  111. if (path.is_empty()) {
  112. continue;
  113. }
  114. paths.push_back(path);
  115. }
  116. return paths;
  117. }
  118. static inline Vector<String> resolve_system_dependencies(Vector<String> p_paths) {
  119. Vector<String> paths;
  120. for (int i = 0; i < p_paths.size(); i++) {
  121. String path = resolve_system_dependency_path(p_paths[i]);
  122. if (path.is_empty()) {
  123. continue;
  124. }
  125. paths.push_back(path);
  126. }
  127. return paths;
  128. }
  129. static inline bool validate_plugin(PluginConfigIOS &plugin_config) {
  130. bool valid_name = !plugin_config.name.is_empty();
  131. bool valid_binary_name = !plugin_config.binary.is_empty();
  132. bool valid_initialize = !plugin_config.initialization_method.is_empty();
  133. bool valid_deinitialize = !plugin_config.deinitialization_method.is_empty();
  134. bool fields_value = valid_name && valid_binary_name && valid_initialize && valid_deinitialize;
  135. if (!fields_value) {
  136. return false;
  137. }
  138. String plugin_extension = plugin_config.binary.get_extension().to_lower();
  139. if ((plugin_extension == "a" && FileAccess::exists(plugin_config.binary)) ||
  140. (plugin_extension == "xcframework" && DirAccess::exists(plugin_config.binary))) {
  141. plugin_config.valid_config = true;
  142. plugin_config.supports_targets = false;
  143. } else {
  144. String file_path = plugin_config.binary.get_base_dir();
  145. String file_name = plugin_config.binary.get_basename().get_file();
  146. String file_extension = plugin_config.binary.get_extension();
  147. String release_file_name = file_path.plus_file(file_name + ".release." + file_extension);
  148. String debug_file_name = file_path.plus_file(file_name + ".debug." + file_extension);
  149. if ((plugin_extension == "a" && FileAccess::exists(release_file_name) && FileAccess::exists(debug_file_name)) ||
  150. (plugin_extension == "xcframework" && DirAccess::exists(release_file_name) && DirAccess::exists(debug_file_name))) {
  151. plugin_config.valid_config = true;
  152. plugin_config.supports_targets = true;
  153. }
  154. }
  155. return plugin_config.valid_config;
  156. }
  157. static inline String get_plugin_main_binary(PluginConfigIOS &plugin_config, bool p_debug) {
  158. if (!plugin_config.supports_targets) {
  159. return plugin_config.binary;
  160. }
  161. String plugin_binary_dir = plugin_config.binary.get_base_dir();
  162. String plugin_name_prefix = plugin_config.binary.get_basename().get_file();
  163. String plugin_extension = plugin_config.binary.get_extension();
  164. String plugin_file = plugin_name_prefix + "." + (p_debug ? "debug" : "release") + "." + plugin_extension;
  165. return plugin_binary_dir.plus_file(plugin_file);
  166. }
  167. static inline uint64_t get_plugin_modification_time(const PluginConfigIOS &plugin_config, const String &config_path) {
  168. uint64_t last_updated = FileAccess::get_modified_time(config_path);
  169. if (!plugin_config.supports_targets) {
  170. last_updated = MAX(last_updated, FileAccess::get_modified_time(plugin_config.binary));
  171. } else {
  172. String file_path = plugin_config.binary.get_base_dir();
  173. String file_name = plugin_config.binary.get_basename().get_file();
  174. String release_file_name = file_path.plus_file(file_name + ".release.a");
  175. String debug_file_name = file_path.plus_file(file_name + ".debug.a");
  176. last_updated = MAX(last_updated, FileAccess::get_modified_time(release_file_name));
  177. last_updated = MAX(last_updated, FileAccess::get_modified_time(debug_file_name));
  178. }
  179. return last_updated;
  180. }
  181. static inline PluginConfigIOS load_plugin_config(Ref<ConfigFile> config_file, const String &path) {
  182. PluginConfigIOS plugin_config = {};
  183. if (!config_file.is_valid()) {
  184. return plugin_config;
  185. }
  186. Error err = config_file->load(path);
  187. if (err != OK) {
  188. return plugin_config;
  189. }
  190. String config_base_dir = path.get_base_dir();
  191. plugin_config.name = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_NAME_KEY, String());
  192. plugin_config.initialization_method = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_INITIALIZE_KEY, String());
  193. plugin_config.deinitialization_method = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_DEINITIALIZE_KEY, String());
  194. String binary_path = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_BINARY_KEY, String());
  195. plugin_config.binary = resolve_local_dependency_path(config_base_dir, binary_path);
  196. if (config_file->has_section(PluginConfigIOS::DEPENDENCIES_SECTION)) {
  197. Vector<String> linked_dependencies = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_LINKED_KEY, Vector<String>());
  198. Vector<String> embedded_dependencies = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_EMBEDDED_KEY, Vector<String>());
  199. Vector<String> system_dependencies = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_SYSTEM_KEY, Vector<String>());
  200. Vector<String> files = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_FILES_KEY, Vector<String>());
  201. plugin_config.linked_dependencies = resolve_local_dependencies(config_base_dir, linked_dependencies);
  202. plugin_config.embedded_dependencies = resolve_local_dependencies(config_base_dir, embedded_dependencies);
  203. plugin_config.system_dependencies = resolve_system_dependencies(system_dependencies);
  204. plugin_config.files_to_copy = resolve_local_dependencies(config_base_dir, files);
  205. plugin_config.capabilities = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_CAPABILITIES_KEY, Vector<String>());
  206. plugin_config.linker_flags = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_LINKER_FLAGS, Vector<String>());
  207. }
  208. if (config_file->has_section(PluginConfigIOS::PLIST_SECTION)) {
  209. List<String> keys;
  210. config_file->get_section_keys(PluginConfigIOS::PLIST_SECTION, &keys);
  211. for (int i = 0; i < keys.size(); i++) {
  212. String value = config_file->get_value(PluginConfigIOS::PLIST_SECTION, keys[i], String());
  213. if (value.is_empty()) {
  214. continue;
  215. }
  216. plugin_config.plist[keys[i]] = value;
  217. }
  218. }
  219. if (validate_plugin(plugin_config)) {
  220. plugin_config.last_updated = get_plugin_modification_time(plugin_config, path);
  221. }
  222. return plugin_config;
  223. }
  224. #endif // GODOT_PLUGIN_CONFIG_H