godot_plugin_config.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**************************************************************************/
  2. /* godot_plugin_config.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef DISABLE_DEPRECATED
  32. /*
  33. * Set of prebuilt plugins.
  34. * Currently unused, this is just for future reference:
  35. */
  36. // static const PluginConfigAndroid MY_PREBUILT_PLUGIN = {
  37. // /*.valid_config =*/true,
  38. // /*.last_updated =*/0,
  39. // /*.name =*/"GodotPayment",
  40. // /*.binary_type =*/"local",
  41. // /*.binary =*/"res://android/build/libs/plugins/GodotPayment.release.aar",
  42. // /*.local_dependencies =*/{},
  43. // /*.remote_dependencies =*/String("com.android.billingclient:billing:2.2.1").split("|"),
  44. // /*.custom_maven_repos =*/{}
  45. // };
  46. String PluginConfigAndroid::resolve_local_dependency_path(String plugin_config_dir, String dependency_path) {
  47. String absolute_path;
  48. if (!dependency_path.is_empty()) {
  49. if (dependency_path.is_absolute_path()) {
  50. absolute_path = ProjectSettings::get_singleton()->globalize_path(dependency_path);
  51. } else {
  52. absolute_path = plugin_config_dir.path_join(dependency_path);
  53. }
  54. }
  55. return absolute_path;
  56. }
  57. PluginConfigAndroid PluginConfigAndroid::resolve_prebuilt_plugin(PluginConfigAndroid prebuilt_plugin, String plugin_config_dir) {
  58. PluginConfigAndroid resolved = prebuilt_plugin;
  59. resolved.binary = resolved.binary_type == PluginConfigAndroid::BINARY_TYPE_LOCAL ? resolve_local_dependency_path(plugin_config_dir, prebuilt_plugin.binary) : prebuilt_plugin.binary;
  60. if (!prebuilt_plugin.local_dependencies.is_empty()) {
  61. resolved.local_dependencies.clear();
  62. for (int i = 0; i < prebuilt_plugin.local_dependencies.size(); i++) {
  63. resolved.local_dependencies.push_back(resolve_local_dependency_path(plugin_config_dir, prebuilt_plugin.local_dependencies[i]));
  64. }
  65. }
  66. return resolved;
  67. }
  68. Vector<PluginConfigAndroid> PluginConfigAndroid::get_prebuilt_plugins(String plugins_base_dir) {
  69. Vector<PluginConfigAndroid> prebuilt_plugins;
  70. return prebuilt_plugins;
  71. }
  72. bool PluginConfigAndroid::is_plugin_config_valid(PluginConfigAndroid plugin_config) {
  73. bool valid_name = !plugin_config.name.is_empty();
  74. bool valid_binary_type = plugin_config.binary_type == PluginConfigAndroid::BINARY_TYPE_LOCAL ||
  75. plugin_config.binary_type == PluginConfigAndroid::BINARY_TYPE_REMOTE;
  76. bool valid_binary = false;
  77. if (valid_binary_type) {
  78. valid_binary = !plugin_config.binary.is_empty() &&
  79. (plugin_config.binary_type == PluginConfigAndroid::BINARY_TYPE_REMOTE ||
  80. FileAccess::exists(plugin_config.binary));
  81. }
  82. bool valid_local_dependencies = true;
  83. if (!plugin_config.local_dependencies.is_empty()) {
  84. for (int i = 0; i < plugin_config.local_dependencies.size(); i++) {
  85. if (!FileAccess::exists(plugin_config.local_dependencies[i])) {
  86. valid_local_dependencies = false;
  87. break;
  88. }
  89. }
  90. }
  91. return valid_name && valid_binary && valid_binary_type && valid_local_dependencies;
  92. }
  93. uint64_t PluginConfigAndroid::get_plugin_modification_time(const PluginConfigAndroid &plugin_config, const String &config_path) {
  94. uint64_t last_updated = FileAccess::get_modified_time(config_path);
  95. last_updated = MAX(last_updated, FileAccess::get_modified_time(plugin_config.binary));
  96. for (int i = 0; i < plugin_config.local_dependencies.size(); i++) {
  97. String binary = plugin_config.local_dependencies.get(i);
  98. last_updated = MAX(last_updated, FileAccess::get_modified_time(binary));
  99. }
  100. return last_updated;
  101. }
  102. PluginConfigAndroid PluginConfigAndroid::load_plugin_config(Ref<ConfigFile> config_file, const String &path) {
  103. PluginConfigAndroid plugin_config = {};
  104. if (config_file.is_valid()) {
  105. Error err = config_file->load(path);
  106. if (err == OK) {
  107. String config_base_dir = path.get_base_dir();
  108. plugin_config.name = config_file->get_value(PluginConfigAndroid::CONFIG_SECTION, PluginConfigAndroid::CONFIG_NAME_KEY, String());
  109. plugin_config.binary_type = config_file->get_value(PluginConfigAndroid::CONFIG_SECTION, PluginConfigAndroid::CONFIG_BINARY_TYPE_KEY, String());
  110. String binary_path = config_file->get_value(PluginConfigAndroid::CONFIG_SECTION, PluginConfigAndroid::CONFIG_BINARY_KEY, String());
  111. plugin_config.binary = plugin_config.binary_type == PluginConfigAndroid::BINARY_TYPE_LOCAL ? resolve_local_dependency_path(config_base_dir, binary_path) : binary_path;
  112. if (config_file->has_section(PluginConfigAndroid::DEPENDENCIES_SECTION)) {
  113. Vector<String> local_dependencies_paths = config_file->get_value(PluginConfigAndroid::DEPENDENCIES_SECTION, PluginConfigAndroid::DEPENDENCIES_LOCAL_KEY, Vector<String>());
  114. if (!local_dependencies_paths.is_empty()) {
  115. for (int i = 0; i < local_dependencies_paths.size(); i++) {
  116. plugin_config.local_dependencies.push_back(resolve_local_dependency_path(config_base_dir, local_dependencies_paths[i]));
  117. }
  118. }
  119. plugin_config.remote_dependencies = config_file->get_value(PluginConfigAndroid::DEPENDENCIES_SECTION, PluginConfigAndroid::DEPENDENCIES_REMOTE_KEY, Vector<String>());
  120. plugin_config.custom_maven_repos = config_file->get_value(PluginConfigAndroid::DEPENDENCIES_SECTION, PluginConfigAndroid::DEPENDENCIES_CUSTOM_MAVEN_REPOS_KEY, Vector<String>());
  121. }
  122. plugin_config.valid_config = is_plugin_config_valid(plugin_config);
  123. plugin_config.last_updated = get_plugin_modification_time(plugin_config, path);
  124. }
  125. }
  126. return plugin_config;
  127. }
  128. void PluginConfigAndroid::get_plugins_binaries(String binary_type, Vector<PluginConfigAndroid> plugins_configs, Vector<String> &r_result) {
  129. if (!plugins_configs.is_empty()) {
  130. for (int i = 0; i < plugins_configs.size(); i++) {
  131. PluginConfigAndroid config = plugins_configs[i];
  132. if (!config.valid_config) {
  133. continue;
  134. }
  135. if (config.binary_type == binary_type) {
  136. r_result.push_back(config.binary);
  137. }
  138. if (binary_type == PluginConfigAndroid::BINARY_TYPE_LOCAL) {
  139. r_result.append_array(config.local_dependencies);
  140. }
  141. if (binary_type == PluginConfigAndroid::BINARY_TYPE_REMOTE) {
  142. r_result.append_array(config.remote_dependencies);
  143. }
  144. }
  145. }
  146. }
  147. void PluginConfigAndroid::get_plugins_custom_maven_repos(Vector<PluginConfigAndroid> plugins_configs, Vector<String> &r_result) {
  148. if (!plugins_configs.is_empty()) {
  149. for (int i = 0; i < plugins_configs.size(); i++) {
  150. PluginConfigAndroid config = plugins_configs[i];
  151. if (!config.valid_config) {
  152. continue;
  153. }
  154. r_result.append_array(config.custom_maven_repos);
  155. }
  156. }
  157. }
  158. void PluginConfigAndroid::get_plugins_names(Vector<PluginConfigAndroid> plugins_configs, Vector<String> &r_result) {
  159. if (!plugins_configs.is_empty()) {
  160. for (int i = 0; i < plugins_configs.size(); i++) {
  161. PluginConfigAndroid config = plugins_configs[i];
  162. if (!config.valid_config) {
  163. continue;
  164. }
  165. r_result.push_back(config.name);
  166. }
  167. }
  168. }
  169. #endif // DISABLE_DEPRECATED