godot_plugin_config.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*************************************************************************/
  2. /* godot_plugin_config.cpp */
  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. #include "godot_plugin_config.h"
  31. const char *PluginConfigAndroid::PLUGIN_CONFIG_EXT = ".gdap";
  32. const char *PluginConfigAndroid::CONFIG_SECTION = "config";
  33. const char *PluginConfigAndroid::CONFIG_NAME_KEY = "name";
  34. const char *PluginConfigAndroid::CONFIG_BINARY_TYPE_KEY = "binary_type";
  35. const char *PluginConfigAndroid::CONFIG_BINARY_KEY = "binary";
  36. const char *PluginConfigAndroid::DEPENDENCIES_SECTION = "dependencies";
  37. const char *PluginConfigAndroid::DEPENDENCIES_LOCAL_KEY = "local";
  38. const char *PluginConfigAndroid::DEPENDENCIES_REMOTE_KEY = "remote";
  39. const char *PluginConfigAndroid::DEPENDENCIES_CUSTOM_MAVEN_REPOS_KEY = "custom_maven_repos";
  40. const char *PluginConfigAndroid::BINARY_TYPE_LOCAL = "local";
  41. const char *PluginConfigAndroid::BINARY_TYPE_REMOTE = "remote";
  42. const char *PluginConfigAndroid::PLUGIN_VALUE_SEPARATOR = "|";
  43. /*
  44. * Set of prebuilt plugins.
  45. * Currently unused, this is just for future reference:
  46. */
  47. // static const PluginConfigAndroid MY_PREBUILT_PLUGIN = {
  48. // /*.valid_config =*/true,
  49. // /*.last_updated =*/0,
  50. // /*.name =*/"GodotPayment",
  51. // /*.binary_type =*/"local",
  52. // /*.binary =*/"res://android/build/libs/plugins/GodotPayment.release.aar",
  53. // /*.local_dependencies =*/{},
  54. // /*.remote_dependencies =*/String("com.android.billingclient:billing:2.2.1").split("|"),
  55. // /*.custom_maven_repos =*/{}
  56. // };
  57. String PluginConfigAndroid::resolve_local_dependency_path(String plugin_config_dir, String dependency_path) {
  58. String absolute_path;
  59. if (!dependency_path.empty()) {
  60. if (dependency_path.is_abs_path()) {
  61. absolute_path = ProjectSettings::get_singleton()->globalize_path(dependency_path);
  62. } else {
  63. absolute_path = plugin_config_dir.plus_file(dependency_path);
  64. }
  65. }
  66. return absolute_path;
  67. }
  68. PluginConfigAndroid PluginConfigAndroid::resolve_prebuilt_plugin(PluginConfigAndroid prebuilt_plugin, String plugin_config_dir) {
  69. PluginConfigAndroid resolved = prebuilt_plugin;
  70. resolved.binary = resolved.binary_type == PluginConfigAndroid::BINARY_TYPE_LOCAL ? resolve_local_dependency_path(plugin_config_dir, prebuilt_plugin.binary) : prebuilt_plugin.binary;
  71. if (!prebuilt_plugin.local_dependencies.empty()) {
  72. resolved.local_dependencies.clear();
  73. for (int i = 0; i < prebuilt_plugin.local_dependencies.size(); i++) {
  74. resolved.local_dependencies.push_back(resolve_local_dependency_path(plugin_config_dir, prebuilt_plugin.local_dependencies[i]));
  75. }
  76. }
  77. return resolved;
  78. }
  79. Vector<PluginConfigAndroid> PluginConfigAndroid::get_prebuilt_plugins(String plugins_base_dir) {
  80. Vector<PluginConfigAndroid> prebuilt_plugins;
  81. // prebuilt_plugins.push_back(resolve_prebuilt_plugin(MY_PREBUILT_PLUGIN, plugins_base_dir));
  82. return prebuilt_plugins;
  83. }
  84. bool PluginConfigAndroid::is_plugin_config_valid(PluginConfigAndroid plugin_config) {
  85. bool valid_name = !plugin_config.name.empty();
  86. bool valid_binary_type = plugin_config.binary_type == PluginConfigAndroid::BINARY_TYPE_LOCAL ||
  87. plugin_config.binary_type == PluginConfigAndroid::BINARY_TYPE_REMOTE;
  88. bool valid_binary = false;
  89. if (valid_binary_type) {
  90. valid_binary = !plugin_config.binary.empty() &&
  91. (plugin_config.binary_type == PluginConfigAndroid::BINARY_TYPE_REMOTE ||
  92. FileAccess::exists(plugin_config.binary));
  93. }
  94. bool valid_local_dependencies = true;
  95. if (!plugin_config.local_dependencies.empty()) {
  96. for (int i = 0; i < plugin_config.local_dependencies.size(); i++) {
  97. if (!FileAccess::exists(plugin_config.local_dependencies[i])) {
  98. valid_local_dependencies = false;
  99. break;
  100. }
  101. }
  102. }
  103. return valid_name && valid_binary && valid_binary_type && valid_local_dependencies;
  104. }
  105. uint64_t PluginConfigAndroid::get_plugin_modification_time(const PluginConfigAndroid &plugin_config, const String &config_path) {
  106. uint64_t last_modified = FileAccess::get_modified_time(config_path);
  107. last_modified = MAX(last_modified, FileAccess::get_modified_time(plugin_config.binary));
  108. for (int i = 0; i < plugin_config.local_dependencies.size(); i++) {
  109. String local_dependency = plugin_config.local_dependencies.get(i);
  110. last_modified = MAX(last_modified, FileAccess::get_modified_time(local_dependency));
  111. }
  112. return last_modified;
  113. }
  114. PluginConfigAndroid PluginConfigAndroid::load_plugin_config(Ref<ConfigFile> config_file, const String &path) {
  115. PluginConfigAndroid plugin_config = {};
  116. if (config_file.is_valid()) {
  117. Error err = config_file->load(path);
  118. if (err == OK) {
  119. String config_base_dir = path.get_base_dir();
  120. plugin_config.name = config_file->get_value(PluginConfigAndroid::CONFIG_SECTION, PluginConfigAndroid::CONFIG_NAME_KEY, String());
  121. plugin_config.binary_type = config_file->get_value(PluginConfigAndroid::CONFIG_SECTION, PluginConfigAndroid::CONFIG_BINARY_TYPE_KEY, String());
  122. String binary_path = config_file->get_value(PluginConfigAndroid::CONFIG_SECTION, PluginConfigAndroid::CONFIG_BINARY_KEY, String());
  123. plugin_config.binary = plugin_config.binary_type == PluginConfigAndroid::BINARY_TYPE_LOCAL ? resolve_local_dependency_path(config_base_dir, binary_path) : binary_path;
  124. if (config_file->has_section(PluginConfigAndroid::DEPENDENCIES_SECTION)) {
  125. Vector<String> local_dependencies_paths = config_file->get_value(PluginConfigAndroid::DEPENDENCIES_SECTION, PluginConfigAndroid::DEPENDENCIES_LOCAL_KEY, Vector<String>());
  126. if (!local_dependencies_paths.empty()) {
  127. for (int i = 0; i < local_dependencies_paths.size(); i++) {
  128. plugin_config.local_dependencies.push_back(resolve_local_dependency_path(config_base_dir, local_dependencies_paths[i]));
  129. }
  130. }
  131. plugin_config.remote_dependencies = config_file->get_value(PluginConfigAndroid::DEPENDENCIES_SECTION, PluginConfigAndroid::DEPENDENCIES_REMOTE_KEY, Vector<String>());
  132. plugin_config.custom_maven_repos = config_file->get_value(PluginConfigAndroid::DEPENDENCIES_SECTION, PluginConfigAndroid::DEPENDENCIES_CUSTOM_MAVEN_REPOS_KEY, Vector<String>());
  133. }
  134. plugin_config.valid_config = is_plugin_config_valid(plugin_config);
  135. plugin_config.last_updated = get_plugin_modification_time(plugin_config, path);
  136. }
  137. }
  138. return plugin_config;
  139. }
  140. String PluginConfigAndroid::get_plugins_binaries(String type, Vector<PluginConfigAndroid> plugins_configs) {
  141. String plugins_binaries;
  142. if (!plugins_configs.empty()) {
  143. Vector<String> binaries;
  144. for (int i = 0; i < plugins_configs.size(); i++) {
  145. PluginConfigAndroid config = plugins_configs[i];
  146. if (!config.valid_config) {
  147. continue;
  148. }
  149. if (config.binary_type == type) {
  150. binaries.push_back(config.binary);
  151. }
  152. if (type == PluginConfigAndroid::BINARY_TYPE_LOCAL) {
  153. binaries.append_array(config.local_dependencies);
  154. }
  155. if (type == PluginConfigAndroid::BINARY_TYPE_REMOTE) {
  156. binaries.append_array(config.remote_dependencies);
  157. }
  158. }
  159. plugins_binaries = String(PluginConfigAndroid::PLUGIN_VALUE_SEPARATOR).join(binaries);
  160. }
  161. return plugins_binaries;
  162. }
  163. String PluginConfigAndroid::get_plugins_custom_maven_repos(Vector<PluginConfigAndroid> plugins_configs) {
  164. String maven_repos;
  165. if (!plugins_configs.empty()) {
  166. Vector<String> repos_urls;
  167. for (int i = 0; i < plugins_configs.size(); i++) {
  168. PluginConfigAndroid config = plugins_configs[i];
  169. if (!config.valid_config) {
  170. continue;
  171. }
  172. repos_urls.append_array(config.custom_maven_repos);
  173. }
  174. maven_repos = String(PluginConfigAndroid::PLUGIN_VALUE_SEPARATOR).join(repos_urls);
  175. }
  176. return maven_repos;
  177. }
  178. String PluginConfigAndroid::get_plugins_names(Vector<PluginConfigAndroid> plugins_configs) {
  179. String plugins_names;
  180. if (!plugins_configs.empty()) {
  181. Vector<String> names;
  182. for (int i = 0; i < plugins_configs.size(); i++) {
  183. PluginConfigAndroid config = plugins_configs[i];
  184. if (!config.valid_config) {
  185. continue;
  186. }
  187. names.push_back(config.name);
  188. }
  189. plugins_names = String(PluginConfigAndroid::PLUGIN_VALUE_SEPARATOR).join(names);
  190. }
  191. return plugins_names;
  192. }