godot_plugin_config.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_list.h"
  33. #include "core/io/config_file.h"
  34. #include "core/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. static const char *PLUGIN_CONFIG_EXT;
  50. static const char *CONFIG_SECTION;
  51. static const char *CONFIG_NAME_KEY;
  52. static const char *CONFIG_BINARY_KEY;
  53. static const char *CONFIG_INITIALIZE_KEY;
  54. static const char *CONFIG_DEINITIALIZE_KEY;
  55. static const char *DEPENDENCIES_SECTION;
  56. static const char *DEPENDENCIES_LINKED_KEY;
  57. static const char *DEPENDENCIES_EMBEDDED_KEY;
  58. static const char *DEPENDENCIES_SYSTEM_KEY;
  59. static const char *DEPENDENCIES_CAPABILITIES_KEY;
  60. static const char *DEPENDENCIES_FILES_KEY;
  61. static const char *DEPENDENCIES_LINKER_FLAGS;
  62. static const char *PLIST_SECTION;
  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. const char *PluginConfigIOS::PLUGIN_CONFIG_EXT = ".gdip";
  85. const char *PluginConfigIOS::CONFIG_SECTION = "config";
  86. const char *PluginConfigIOS::CONFIG_NAME_KEY = "name";
  87. const char *PluginConfigIOS::CONFIG_BINARY_KEY = "binary";
  88. const char *PluginConfigIOS::CONFIG_INITIALIZE_KEY = "initialization";
  89. const char *PluginConfigIOS::CONFIG_DEINITIALIZE_KEY = "deinitialization";
  90. const char *PluginConfigIOS::DEPENDENCIES_SECTION = "dependencies";
  91. const char *PluginConfigIOS::DEPENDENCIES_LINKED_KEY = "linked";
  92. const char *PluginConfigIOS::DEPENDENCIES_EMBEDDED_KEY = "embedded";
  93. const char *PluginConfigIOS::DEPENDENCIES_SYSTEM_KEY = "system";
  94. const char *PluginConfigIOS::DEPENDENCIES_CAPABILITIES_KEY = "capabilities";
  95. const char *PluginConfigIOS::DEPENDENCIES_LINKER_FLAGS = "linker_flags";
  96. const char *PluginConfigIOS::DEPENDENCIES_FILES_KEY = "files";
  97. const char *PluginConfigIOS::PLIST_SECTION = "plist";
  98. static inline String resolve_local_dependency_path(String plugin_config_dir, String dependency_path) {
  99. String absolute_path;
  100. if (dependency_path.empty()) {
  101. return absolute_path;
  102. }
  103. if (dependency_path.is_abs_path()) {
  104. return dependency_path;
  105. }
  106. String res_path = ProjectSettings::get_singleton()->globalize_path("res://");
  107. absolute_path = plugin_config_dir.plus_file(dependency_path);
  108. return absolute_path.replace(res_path, "res://");
  109. }
  110. static inline String resolve_system_dependency_path(String dependency_path) {
  111. String absolute_path;
  112. if (dependency_path.empty()) {
  113. return absolute_path;
  114. }
  115. if (dependency_path.is_abs_path()) {
  116. return dependency_path;
  117. }
  118. String system_path = "/System/Library/Frameworks";
  119. return system_path.plus_file(dependency_path);
  120. }
  121. static inline Vector<String> resolve_local_dependencies(String plugin_config_dir, Vector<String> p_paths) {
  122. Vector<String> paths;
  123. for (int i = 0; i < p_paths.size(); i++) {
  124. String path = resolve_local_dependency_path(plugin_config_dir, p_paths[i]);
  125. if (path.empty()) {
  126. continue;
  127. }
  128. paths.push_back(path);
  129. }
  130. return paths;
  131. }
  132. static inline Vector<String> resolve_system_dependencies(Vector<String> p_paths) {
  133. Vector<String> paths;
  134. for (int i = 0; i < p_paths.size(); i++) {
  135. String path = resolve_system_dependency_path(p_paths[i]);
  136. if (path.empty()) {
  137. continue;
  138. }
  139. paths.push_back(path);
  140. }
  141. return paths;
  142. }
  143. static inline bool validate_plugin(PluginConfigIOS &plugin_config) {
  144. bool valid_name = !plugin_config.name.empty();
  145. bool valid_binary_name = !plugin_config.binary.empty();
  146. bool valid_initialize = !plugin_config.initialization_method.empty();
  147. bool valid_deinitialize = !plugin_config.deinitialization_method.empty();
  148. bool fields_value = valid_name && valid_binary_name && valid_initialize && valid_deinitialize;
  149. if (!fields_value) {
  150. return false;
  151. }
  152. String plugin_extension = plugin_config.binary.get_extension().to_lower();
  153. if ((plugin_extension == "a" && FileAccess::exists(plugin_config.binary)) ||
  154. (plugin_extension == "xcframework" && DirAccess::exists(plugin_config.binary))) {
  155. plugin_config.valid_config = true;
  156. plugin_config.supports_targets = false;
  157. } else {
  158. String file_path = plugin_config.binary.get_base_dir();
  159. String file_name = plugin_config.binary.get_basename().get_file();
  160. String file_extension = plugin_config.binary.get_extension();
  161. String release_file_name = file_path.plus_file(file_name + ".release." + file_extension);
  162. String debug_file_name = file_path.plus_file(file_name + ".debug." + file_extension);
  163. if ((plugin_extension == "a" && FileAccess::exists(release_file_name) && FileAccess::exists(debug_file_name)) ||
  164. (plugin_extension == "xcframework" && DirAccess::exists(release_file_name) && DirAccess::exists(debug_file_name))) {
  165. plugin_config.valid_config = true;
  166. plugin_config.supports_targets = true;
  167. }
  168. }
  169. return plugin_config.valid_config;
  170. }
  171. static inline String get_plugin_main_binary(PluginConfigIOS &plugin_config, bool p_debug) {
  172. if (!plugin_config.supports_targets) {
  173. return plugin_config.binary;
  174. }
  175. String plugin_binary_dir = plugin_config.binary.get_base_dir();
  176. String plugin_name_prefix = plugin_config.binary.get_basename().get_file();
  177. String plugin_extension = plugin_config.binary.get_extension();
  178. String plugin_file = plugin_name_prefix + "." + (p_debug ? "debug" : "release") + "." + plugin_extension;
  179. return plugin_binary_dir.plus_file(plugin_file);
  180. }
  181. static inline uint64_t get_plugin_modification_time(const PluginConfigIOS &plugin_config, const String &config_path) {
  182. uint64_t last_updated = FileAccess::get_modified_time(config_path);
  183. if (!plugin_config.supports_targets) {
  184. last_updated = MAX(last_updated, FileAccess::get_modified_time(plugin_config.binary));
  185. } else {
  186. String file_path = plugin_config.binary.get_base_dir();
  187. String file_name = plugin_config.binary.get_basename().get_file();
  188. String plugin_extension = plugin_config.binary.get_extension();
  189. String release_file_name = file_path.plus_file(file_name + ".release." + plugin_extension);
  190. String debug_file_name = file_path.plus_file(file_name + ".debug." + plugin_extension);
  191. last_updated = MAX(last_updated, FileAccess::get_modified_time(release_file_name));
  192. last_updated = MAX(last_updated, FileAccess::get_modified_time(debug_file_name));
  193. }
  194. return last_updated;
  195. }
  196. static inline PluginConfigIOS load_plugin_config(Ref<ConfigFile> config_file, const String &path) {
  197. PluginConfigIOS plugin_config = {};
  198. if (!config_file.is_valid()) {
  199. return plugin_config;
  200. }
  201. Error err = config_file->load(path);
  202. if (err != OK) {
  203. return plugin_config;
  204. }
  205. String config_base_dir = path.get_base_dir();
  206. plugin_config.name = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_NAME_KEY, String());
  207. plugin_config.initialization_method = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_INITIALIZE_KEY, String());
  208. plugin_config.deinitialization_method = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_DEINITIALIZE_KEY, String());
  209. String binary_path = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_BINARY_KEY, String());
  210. plugin_config.binary = resolve_local_dependency_path(config_base_dir, binary_path);
  211. if (config_file->has_section(PluginConfigIOS::DEPENDENCIES_SECTION)) {
  212. Vector<String> linked_dependencies = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_LINKED_KEY, Vector<String>());
  213. Vector<String> embedded_dependencies = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_EMBEDDED_KEY, Vector<String>());
  214. Vector<String> system_dependencies = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_SYSTEM_KEY, Vector<String>());
  215. Vector<String> files = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_FILES_KEY, Vector<String>());
  216. plugin_config.linked_dependencies = resolve_local_dependencies(config_base_dir, linked_dependencies);
  217. plugin_config.embedded_dependencies = resolve_local_dependencies(config_base_dir, embedded_dependencies);
  218. plugin_config.system_dependencies = resolve_system_dependencies(system_dependencies);
  219. plugin_config.files_to_copy = resolve_local_dependencies(config_base_dir, files);
  220. plugin_config.capabilities = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_CAPABILITIES_KEY, Vector<String>());
  221. plugin_config.linker_flags = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_LINKER_FLAGS, Vector<String>());
  222. }
  223. if (config_file->has_section(PluginConfigIOS::PLIST_SECTION)) {
  224. List<String> keys;
  225. config_file->get_section_keys(PluginConfigIOS::PLIST_SECTION, &keys);
  226. for (int i = 0; i < keys.size(); i++) {
  227. String value = config_file->get_value(PluginConfigIOS::PLIST_SECTION, keys[i], String());
  228. if (value.empty()) {
  229. continue;
  230. }
  231. plugin_config.plist[keys[i]] = value;
  232. }
  233. }
  234. if (validate_plugin(plugin_config)) {
  235. plugin_config.last_updated = get_plugin_modification_time(plugin_config, path);
  236. }
  237. return plugin_config;
  238. }
  239. #endif // GODOT_PLUGIN_CONFIG_H