godot_plugin_config.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #include "core/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/io/file_access.h"
  34. String PluginConfigIOS::resolve_local_dependency_path(String plugin_config_dir, String dependency_path) {
  35. String absolute_path;
  36. if (dependency_path.is_empty()) {
  37. return absolute_path;
  38. }
  39. if (dependency_path.is_absolute_path()) {
  40. return dependency_path;
  41. }
  42. String res_path = ProjectSettings::get_singleton()->globalize_path("res://");
  43. absolute_path = plugin_config_dir.path_join(dependency_path);
  44. return absolute_path.replace(res_path, "res://");
  45. }
  46. String PluginConfigIOS::resolve_system_dependency_path(String dependency_path) {
  47. String absolute_path;
  48. if (dependency_path.is_empty()) {
  49. return absolute_path;
  50. }
  51. if (dependency_path.is_absolute_path()) {
  52. return dependency_path;
  53. }
  54. String system_path = "/System/Library/Frameworks";
  55. return system_path.path_join(dependency_path);
  56. }
  57. Vector<String> PluginConfigIOS::resolve_local_dependencies(String plugin_config_dir, Vector<String> p_paths) {
  58. Vector<String> paths;
  59. for (int i = 0; i < p_paths.size(); i++) {
  60. String path = resolve_local_dependency_path(plugin_config_dir, p_paths[i]);
  61. if (path.is_empty()) {
  62. continue;
  63. }
  64. paths.push_back(path);
  65. }
  66. return paths;
  67. }
  68. Vector<String> PluginConfigIOS::resolve_system_dependencies(Vector<String> p_paths) {
  69. Vector<String> paths;
  70. for (int i = 0; i < p_paths.size(); i++) {
  71. String path = resolve_system_dependency_path(p_paths[i]);
  72. if (path.is_empty()) {
  73. continue;
  74. }
  75. paths.push_back(path);
  76. }
  77. return paths;
  78. }
  79. bool PluginConfigIOS::validate_plugin(PluginConfigIOS &plugin_config) {
  80. bool valid_name = !plugin_config.name.is_empty();
  81. bool valid_binary_name = !plugin_config.binary.is_empty();
  82. bool valid_initialize = !plugin_config.initialization_method.is_empty();
  83. bool valid_deinitialize = !plugin_config.deinitialization_method.is_empty();
  84. bool fields_value = valid_name && valid_binary_name && valid_initialize && valid_deinitialize;
  85. if (!fields_value) {
  86. return false;
  87. }
  88. String plugin_extension = plugin_config.binary.get_extension().to_lower();
  89. if ((plugin_extension == "a" && FileAccess::exists(plugin_config.binary)) ||
  90. (plugin_extension == "xcframework" && DirAccess::exists(plugin_config.binary))) {
  91. plugin_config.valid_config = true;
  92. plugin_config.supports_targets = false;
  93. } else {
  94. String file_path = plugin_config.binary.get_base_dir();
  95. String file_name = plugin_config.binary.get_basename().get_file();
  96. String file_extension = plugin_config.binary.get_extension();
  97. String release_file_name = file_path.path_join(file_name + ".release." + file_extension);
  98. String debug_file_name = file_path.path_join(file_name + ".debug." + file_extension);
  99. if ((plugin_extension == "a" && FileAccess::exists(release_file_name) && FileAccess::exists(debug_file_name)) ||
  100. (plugin_extension == "xcframework" && DirAccess::exists(release_file_name) && DirAccess::exists(debug_file_name))) {
  101. plugin_config.valid_config = true;
  102. plugin_config.supports_targets = true;
  103. }
  104. }
  105. return plugin_config.valid_config;
  106. }
  107. String PluginConfigIOS::get_plugin_main_binary(PluginConfigIOS &plugin_config, bool p_debug) {
  108. if (!plugin_config.supports_targets) {
  109. return plugin_config.binary;
  110. }
  111. String plugin_binary_dir = plugin_config.binary.get_base_dir();
  112. String plugin_name_prefix = plugin_config.binary.get_basename().get_file();
  113. String plugin_extension = plugin_config.binary.get_extension();
  114. String plugin_file = plugin_name_prefix + "." + (p_debug ? "debug" : "release") + "." + plugin_extension;
  115. return plugin_binary_dir.path_join(plugin_file);
  116. }
  117. uint64_t PluginConfigIOS::get_plugin_modification_time(const PluginConfigIOS &plugin_config, const String &config_path) {
  118. uint64_t last_updated = FileAccess::get_modified_time(config_path);
  119. if (!plugin_config.supports_targets) {
  120. last_updated = MAX(last_updated, FileAccess::get_modified_time(plugin_config.binary));
  121. } else {
  122. String file_path = plugin_config.binary.get_base_dir();
  123. String file_name = plugin_config.binary.get_basename().get_file();
  124. String plugin_extension = plugin_config.binary.get_extension();
  125. String release_file_name = file_path.path_join(file_name + ".release." + plugin_extension);
  126. String debug_file_name = file_path.path_join(file_name + ".debug." + plugin_extension);
  127. last_updated = MAX(last_updated, FileAccess::get_modified_time(release_file_name));
  128. last_updated = MAX(last_updated, FileAccess::get_modified_time(debug_file_name));
  129. }
  130. return last_updated;
  131. }
  132. PluginConfigIOS PluginConfigIOS::load_plugin_config(Ref<ConfigFile> config_file, const String &path) {
  133. PluginConfigIOS plugin_config = {};
  134. if (!config_file.is_valid()) {
  135. return plugin_config;
  136. }
  137. config_file->clear();
  138. Error err = config_file->load(path);
  139. if (err != OK) {
  140. return plugin_config;
  141. }
  142. String config_base_dir = path.get_base_dir();
  143. plugin_config.name = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_NAME_KEY, String());
  144. plugin_config.use_swift_runtime = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_USE_SWIFT_KEY, false);
  145. plugin_config.initialization_method = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_INITIALIZE_KEY, String());
  146. plugin_config.deinitialization_method = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_DEINITIALIZE_KEY, String());
  147. String binary_path = config_file->get_value(PluginConfigIOS::CONFIG_SECTION, PluginConfigIOS::CONFIG_BINARY_KEY, String());
  148. plugin_config.binary = resolve_local_dependency_path(config_base_dir, binary_path);
  149. if (config_file->has_section(PluginConfigIOS::DEPENDENCIES_SECTION)) {
  150. Vector<String> linked_dependencies = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_LINKED_KEY, Vector<String>());
  151. Vector<String> embedded_dependencies = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_EMBEDDED_KEY, Vector<String>());
  152. Vector<String> system_dependencies = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_SYSTEM_KEY, Vector<String>());
  153. Vector<String> files = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_FILES_KEY, Vector<String>());
  154. plugin_config.linked_dependencies = resolve_local_dependencies(config_base_dir, linked_dependencies);
  155. plugin_config.embedded_dependencies = resolve_local_dependencies(config_base_dir, embedded_dependencies);
  156. plugin_config.system_dependencies = resolve_system_dependencies(system_dependencies);
  157. plugin_config.files_to_copy = resolve_local_dependencies(config_base_dir, files);
  158. plugin_config.capabilities = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_CAPABILITIES_KEY, Vector<String>());
  159. plugin_config.linker_flags = config_file->get_value(PluginConfigIOS::DEPENDENCIES_SECTION, PluginConfigIOS::DEPENDENCIES_LINKER_FLAGS, Vector<String>());
  160. }
  161. if (config_file->has_section(PluginConfigIOS::PLIST_SECTION)) {
  162. List<String> keys;
  163. config_file->get_section_keys(PluginConfigIOS::PLIST_SECTION, &keys);
  164. for (const String &key : keys) {
  165. Vector<String> key_components = key.split(":");
  166. String key_value = "";
  167. PluginConfigIOS::PlistItemType key_type = PluginConfigIOS::PlistItemType::UNKNOWN;
  168. if (key_components.size() == 1) {
  169. key_value = key_components[0];
  170. key_type = PluginConfigIOS::PlistItemType::STRING;
  171. } else if (key_components.size() == 2) {
  172. key_value = key_components[0];
  173. if (key_components[1].to_lower() == "string") {
  174. key_type = PluginConfigIOS::PlistItemType::STRING;
  175. } else if (key_components[1].to_lower() == "integer") {
  176. key_type = PluginConfigIOS::PlistItemType::INTEGER;
  177. } else if (key_components[1].to_lower() == "boolean") {
  178. key_type = PluginConfigIOS::PlistItemType::BOOLEAN;
  179. } else if (key_components[1].to_lower() == "raw") {
  180. key_type = PluginConfigIOS::PlistItemType::RAW;
  181. } else if (key_components[1].to_lower() == "string_input") {
  182. key_type = PluginConfigIOS::PlistItemType::STRING_INPUT;
  183. }
  184. }
  185. if (key_value.is_empty() || key_type == PluginConfigIOS::PlistItemType::UNKNOWN) {
  186. continue;
  187. }
  188. String value;
  189. switch (key_type) {
  190. case PluginConfigIOS::PlistItemType::STRING: {
  191. String raw_value = config_file->get_value(PluginConfigIOS::PLIST_SECTION, key, String());
  192. value = "<string>" + raw_value + "</string>";
  193. } break;
  194. case PluginConfigIOS::PlistItemType::INTEGER: {
  195. int raw_value = config_file->get_value(PluginConfigIOS::PLIST_SECTION, key, 0);
  196. Dictionary value_dictionary;
  197. String value_format = "<integer>$value</integer>";
  198. value_dictionary["value"] = raw_value;
  199. value = value_format.format(value_dictionary, "$_");
  200. } break;
  201. case PluginConfigIOS::PlistItemType::BOOLEAN:
  202. if (config_file->get_value(PluginConfigIOS::PLIST_SECTION, key, false)) {
  203. value = "<true/>";
  204. } else {
  205. value = "<false/>";
  206. }
  207. break;
  208. case PluginConfigIOS::PlistItemType::RAW: {
  209. String raw_value = config_file->get_value(PluginConfigIOS::PLIST_SECTION, key, String());
  210. value = raw_value;
  211. } break;
  212. case PluginConfigIOS::PlistItemType::STRING_INPUT: {
  213. String raw_value = config_file->get_value(PluginConfigIOS::PLIST_SECTION, key, String());
  214. value = raw_value;
  215. } break;
  216. default:
  217. continue;
  218. }
  219. plugin_config.plist[key_value] = PluginConfigIOS::PlistItem{ key_type, value };
  220. }
  221. }
  222. if (validate_plugin(plugin_config)) {
  223. plugin_config.last_updated = get_plugin_modification_time(plugin_config, path);
  224. }
  225. return plugin_config;
  226. }