godot_plugin_config.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. static const char *PLUGIN_CONFIG_EXT = ".gdap";
  36. static const char *CONFIG_SECTION = "config";
  37. static const char *CONFIG_NAME_KEY = "name";
  38. static const char *CONFIG_BINARY_TYPE_KEY = "binary_type";
  39. static const char *CONFIG_BINARY_KEY = "binary";
  40. static const char *DEPENDENCIES_SECTION = "dependencies";
  41. static const char *DEPENDENCIES_LOCAL_KEY = "local";
  42. static const char *DEPENDENCIES_REMOTE_KEY = "remote";
  43. static const char *DEPENDENCIES_CUSTOM_MAVEN_REPOS_KEY = "custom_maven_repos";
  44. static const char *BINARY_TYPE_LOCAL = "local";
  45. static const char *BINARY_TYPE_REMOTE = "remote";
  46. static const char *PLUGIN_VALUE_SEPARATOR = "|";
  47. /*
  48. The `config` section and fields are required and defined as follow:
  49. - **name**: name of the plugin
  50. - **binary_type**: can be either `local` or `remote`. The type affects the **binary** field
  51. - **binary**:
  52. - if **binary_type** is `local`, then this should be the filename of the plugin `aar` file in the `res://android/plugins` directory (e.g: `MyPlugin.aar`).
  53. - if **binary_type** is `remote`, then this should be a declaration for a remote gradle binary (e.g: "org.godot.example:my-plugin:0.0.0").
  54. The `dependencies` section and fields are optional and defined as follow:
  55. - **local**: contains a list of local `.aar` binary files the plugin depends on. The local binary dependencies must also be located in the `res://android/plugins` directory.
  56. - **remote**: contains a list of remote binary gradle dependencies for the plugin.
  57. - **custom_maven_repos**: contains a list of urls specifying custom maven repos required for the plugin's dependencies.
  58. See https://github.com/godotengine/godot/issues/38157#issuecomment-618773871
  59. */
  60. struct PluginConfig {
  61. // Set to true when the config file is properly loaded.
  62. bool valid_config = false;
  63. // Unix timestamp of last change to this plugin.
  64. uint64_t last_updated = 0;
  65. // Required config section
  66. String name;
  67. String binary_type;
  68. String binary;
  69. // Optional dependencies section
  70. Vector<String> local_dependencies;
  71. Vector<String> remote_dependencies;
  72. Vector<String> custom_maven_repos;
  73. };
  74. /*
  75. * Set of prebuilt plugins.
  76. * Currently unused, this is just for future reference:
  77. */
  78. // static const PluginConfig MY_PREBUILT_PLUGIN = {
  79. // /*.valid_config =*/true,
  80. // /*.last_updated =*/0,
  81. // /*.name =*/"GodotPayment",
  82. // /*.binary_type =*/"local",
  83. // /*.binary =*/"res://android/build/libs/plugins/GodotPayment.release.aar",
  84. // /*.local_dependencies =*/{},
  85. // /*.remote_dependencies =*/String("com.android.billingclient:billing:2.2.1").split("|"),
  86. // /*.custom_maven_repos =*/{}
  87. // };
  88. static inline String resolve_local_dependency_path(String plugin_config_dir, String dependency_path) {
  89. String absolute_path;
  90. if (!dependency_path.empty()) {
  91. if (dependency_path.is_abs_path()) {
  92. absolute_path = ProjectSettings::get_singleton()->globalize_path(dependency_path);
  93. } else {
  94. absolute_path = plugin_config_dir.plus_file(dependency_path);
  95. }
  96. }
  97. return absolute_path;
  98. }
  99. static inline PluginConfig resolve_prebuilt_plugin(PluginConfig prebuilt_plugin, String plugin_config_dir) {
  100. PluginConfig resolved = prebuilt_plugin;
  101. resolved.binary = resolved.binary_type == BINARY_TYPE_LOCAL ? resolve_local_dependency_path(plugin_config_dir, prebuilt_plugin.binary) : prebuilt_plugin.binary;
  102. if (!prebuilt_plugin.local_dependencies.empty()) {
  103. resolved.local_dependencies.clear();
  104. for (int i = 0; i < prebuilt_plugin.local_dependencies.size(); i++) {
  105. resolved.local_dependencies.push_back(resolve_local_dependency_path(plugin_config_dir, prebuilt_plugin.local_dependencies[i]));
  106. }
  107. }
  108. return resolved;
  109. }
  110. static inline Vector<PluginConfig> get_prebuilt_plugins(String plugins_base_dir) {
  111. Vector<PluginConfig> prebuilt_plugins;
  112. // prebuilt_plugins.push_back(resolve_prebuilt_plugin(MY_PREBUILT_PLUGIN, plugins_base_dir));
  113. return prebuilt_plugins;
  114. }
  115. static inline bool is_plugin_config_valid(PluginConfig plugin_config) {
  116. bool valid_name = !plugin_config.name.empty();
  117. bool valid_binary_type = plugin_config.binary_type == BINARY_TYPE_LOCAL ||
  118. plugin_config.binary_type == BINARY_TYPE_REMOTE;
  119. bool valid_binary = false;
  120. if (valid_binary_type) {
  121. valid_binary = !plugin_config.binary.empty() &&
  122. (plugin_config.binary_type == BINARY_TYPE_REMOTE ||
  123. FileAccess::exists(plugin_config.binary));
  124. }
  125. bool valid_local_dependencies = true;
  126. if (!plugin_config.local_dependencies.empty()) {
  127. for (int i = 0; i < plugin_config.local_dependencies.size(); i++) {
  128. if (!FileAccess::exists(plugin_config.local_dependencies[i])) {
  129. valid_local_dependencies = false;
  130. break;
  131. }
  132. }
  133. }
  134. return valid_name && valid_binary && valid_binary_type && valid_local_dependencies;
  135. }
  136. static inline uint64_t get_plugin_modification_time(const PluginConfig &plugin_config, const String &config_path) {
  137. uint64_t last_updated = FileAccess::get_modified_time(config_path);
  138. last_updated = MAX(last_updated, FileAccess::get_modified_time(plugin_config.binary));
  139. for (int i = 0; i < plugin_config.local_dependencies.size(); i++) {
  140. String binary = plugin_config.local_dependencies.get(i);
  141. last_updated = MAX(last_updated, FileAccess::get_modified_time(binary));
  142. }
  143. return last_updated;
  144. }
  145. static inline PluginConfig load_plugin_config(Ref<ConfigFile> config_file, const String &path) {
  146. PluginConfig plugin_config = {};
  147. if (config_file.is_valid()) {
  148. Error err = config_file->load(path);
  149. if (err == OK) {
  150. String config_base_dir = path.get_base_dir();
  151. plugin_config.name = config_file->get_value(CONFIG_SECTION, CONFIG_NAME_KEY, String());
  152. plugin_config.binary_type = config_file->get_value(CONFIG_SECTION, CONFIG_BINARY_TYPE_KEY, String());
  153. String binary_path = config_file->get_value(CONFIG_SECTION, CONFIG_BINARY_KEY, String());
  154. plugin_config.binary = plugin_config.binary_type == BINARY_TYPE_LOCAL ? resolve_local_dependency_path(config_base_dir, binary_path) : binary_path;
  155. if (config_file->has_section(DEPENDENCIES_SECTION)) {
  156. Vector<String> local_dependencies_paths = config_file->get_value(DEPENDENCIES_SECTION, DEPENDENCIES_LOCAL_KEY, Vector<String>());
  157. if (!local_dependencies_paths.empty()) {
  158. for (int i = 0; i < local_dependencies_paths.size(); i++) {
  159. plugin_config.local_dependencies.push_back(resolve_local_dependency_path(config_base_dir, local_dependencies_paths[i]));
  160. }
  161. }
  162. plugin_config.remote_dependencies = config_file->get_value(DEPENDENCIES_SECTION, DEPENDENCIES_REMOTE_KEY, Vector<String>());
  163. plugin_config.custom_maven_repos = config_file->get_value(DEPENDENCIES_SECTION, DEPENDENCIES_CUSTOM_MAVEN_REPOS_KEY, Vector<String>());
  164. }
  165. plugin_config.valid_config = is_plugin_config_valid(plugin_config);
  166. plugin_config.last_updated = get_plugin_modification_time(plugin_config, path);
  167. }
  168. }
  169. return plugin_config;
  170. }
  171. static inline String get_plugins_binaries(String binary_type, Vector<PluginConfig> plugins_configs) {
  172. String plugins_binaries;
  173. if (!plugins_configs.empty()) {
  174. Vector<String> binaries;
  175. for (int i = 0; i < plugins_configs.size(); i++) {
  176. PluginConfig config = plugins_configs[i];
  177. if (!config.valid_config) {
  178. continue;
  179. }
  180. if (config.binary_type == binary_type) {
  181. binaries.push_back(config.binary);
  182. }
  183. if (binary_type == BINARY_TYPE_LOCAL) {
  184. binaries.append_array(config.local_dependencies);
  185. }
  186. if (binary_type == BINARY_TYPE_REMOTE) {
  187. binaries.append_array(config.remote_dependencies);
  188. }
  189. }
  190. plugins_binaries = String(PLUGIN_VALUE_SEPARATOR).join(binaries);
  191. }
  192. return plugins_binaries;
  193. }
  194. static inline String get_plugins_custom_maven_repos(Vector<PluginConfig> plugins_configs) {
  195. String custom_maven_repos;
  196. if (!plugins_configs.empty()) {
  197. Vector<String> repos_urls;
  198. for (int i = 0; i < plugins_configs.size(); i++) {
  199. PluginConfig config = plugins_configs[i];
  200. if (!config.valid_config) {
  201. continue;
  202. }
  203. repos_urls.append_array(config.custom_maven_repos);
  204. }
  205. custom_maven_repos = String(PLUGIN_VALUE_SEPARATOR).join(repos_urls);
  206. }
  207. return custom_maven_repos;
  208. }
  209. static inline String get_plugins_names(Vector<PluginConfig> plugins_configs) {
  210. String plugins_names;
  211. if (!plugins_configs.empty()) {
  212. Vector<String> names;
  213. for (int i = 0; i < plugins_configs.size(); i++) {
  214. PluginConfig config = plugins_configs[i];
  215. if (!config.valid_config) {
  216. continue;
  217. }
  218. names.push_back(config.name);
  219. }
  220. plugins_names = String(PLUGIN_VALUE_SEPARATOR).join(names);
  221. }
  222. return plugins_names;
  223. }
  224. #endif // GODOT_PLUGIN_CONFIG_H