godot_plugin_config.h 12 KB

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