gdextension_library_loader.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /**************************************************************************/
  2. /* gdextension_library_loader.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 "gdextension_library_loader.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/version.h"
  34. #include "gdextension.h"
  35. Vector<SharedObject> GDExtensionLibraryLoader::find_extension_dependencies(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature) {
  36. Vector<SharedObject> dependencies_shared_objects;
  37. if (p_config->has_section("dependencies")) {
  38. List<String> config_dependencies;
  39. p_config->get_section_keys("dependencies", &config_dependencies);
  40. for (const String &dependency : config_dependencies) {
  41. Vector<String> dependency_tags = dependency.split(".");
  42. bool all_tags_met = true;
  43. for (int i = 0; i < dependency_tags.size(); i++) {
  44. String tag = dependency_tags[i].strip_edges();
  45. if (!p_has_feature(tag)) {
  46. all_tags_met = false;
  47. break;
  48. }
  49. }
  50. if (all_tags_met) {
  51. Dictionary dependency_value = p_config->get_value("dependencies", dependency);
  52. for (const Variant *key = dependency_value.next(nullptr); key; key = dependency_value.next(key)) {
  53. String dependency_path = *key;
  54. String target_path = dependency_value[*key];
  55. if (dependency_path.is_relative_path()) {
  56. dependency_path = p_path.get_base_dir().path_join(dependency_path);
  57. }
  58. dependencies_shared_objects.push_back(SharedObject(dependency_path, dependency_tags, target_path));
  59. }
  60. break;
  61. }
  62. }
  63. }
  64. return dependencies_shared_objects;
  65. }
  66. String GDExtensionLibraryLoader::find_extension_library(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature, PackedStringArray *r_tags) {
  67. // First, check the explicit libraries.
  68. if (p_config->has_section("libraries")) {
  69. List<String> libraries;
  70. p_config->get_section_keys("libraries", &libraries);
  71. // Iterate the libraries, finding the best matching tags.
  72. String best_library_path;
  73. Vector<String> best_library_tags;
  74. for (const String &E : libraries) {
  75. Vector<String> tags = E.split(".");
  76. bool all_tags_met = true;
  77. for (int i = 0; i < tags.size(); i++) {
  78. String tag = tags[i].strip_edges();
  79. if (!p_has_feature(tag)) {
  80. all_tags_met = false;
  81. break;
  82. }
  83. }
  84. if (all_tags_met && tags.size() > best_library_tags.size()) {
  85. best_library_path = p_config->get_value("libraries", E);
  86. best_library_tags = tags;
  87. }
  88. }
  89. if (!best_library_path.is_empty()) {
  90. if (best_library_path.is_relative_path()) {
  91. best_library_path = p_path.get_base_dir().path_join(best_library_path);
  92. }
  93. if (r_tags != nullptr) {
  94. r_tags->append_array(best_library_tags);
  95. }
  96. return best_library_path;
  97. }
  98. }
  99. // Second, try to autodetect.
  100. String autodetect_library_prefix;
  101. if (p_config->has_section_key("configuration", "autodetect_library_prefix")) {
  102. autodetect_library_prefix = p_config->get_value("configuration", "autodetect_library_prefix");
  103. }
  104. if (!autodetect_library_prefix.is_empty()) {
  105. String autodetect_path = autodetect_library_prefix;
  106. if (autodetect_path.is_relative_path()) {
  107. autodetect_path = p_path.get_base_dir().path_join(autodetect_path);
  108. }
  109. // Find the folder and file parts of the prefix.
  110. String folder;
  111. String file_prefix;
  112. if (DirAccess::dir_exists_absolute(autodetect_path)) {
  113. folder = autodetect_path;
  114. } else if (DirAccess::dir_exists_absolute(autodetect_path.get_base_dir())) {
  115. folder = autodetect_path.get_base_dir();
  116. file_prefix = autodetect_path.get_file();
  117. } else {
  118. ERR_FAIL_V_MSG(String(), vformat("Error in extension: %s. Could not find folder for automatic detection of libraries files. autodetect_library_prefix=\"%s\"", p_path, autodetect_library_prefix));
  119. }
  120. // Open the folder.
  121. Ref<DirAccess> dir = DirAccess::open(folder);
  122. ERR_FAIL_COND_V_MSG(dir.is_null(), String(), vformat("Error in extension: %s. Could not open folder for automatic detection of libraries files. autodetect_library_prefix=\"%s\"", p_path, autodetect_library_prefix));
  123. // Iterate the files and check the prefixes, finding the best matching file.
  124. String best_file;
  125. Vector<String> best_file_tags;
  126. dir->list_dir_begin();
  127. String file_name = dir->_get_next();
  128. while (file_name != "") {
  129. if (!dir->current_is_dir() && file_name.begins_with(file_prefix)) {
  130. // Check if the files matches all requested feature tags.
  131. String tags_str = file_name.trim_prefix(file_prefix);
  132. tags_str = tags_str.trim_suffix(tags_str.get_extension());
  133. Vector<String> tags = tags_str.split(".", false);
  134. bool all_tags_met = true;
  135. for (int i = 0; i < tags.size(); i++) {
  136. String tag = tags[i].strip_edges();
  137. if (!p_has_feature(tag)) {
  138. all_tags_met = false;
  139. break;
  140. }
  141. }
  142. // If all tags are found in the feature list, and we found more tags than before, use this file.
  143. if (all_tags_met && tags.size() > best_file_tags.size()) {
  144. best_file_tags = tags;
  145. best_file = file_name;
  146. }
  147. }
  148. file_name = dir->_get_next();
  149. }
  150. if (!best_file.is_empty()) {
  151. String library_path = folder.path_join(best_file);
  152. if (r_tags != nullptr) {
  153. r_tags->append_array(best_file_tags);
  154. }
  155. return library_path;
  156. }
  157. }
  158. return String();
  159. }
  160. Error GDExtensionLibraryLoader::open_library(const String &p_path) {
  161. Error err = parse_gdextension_file(p_path);
  162. if (err != OK) {
  163. return err;
  164. }
  165. String abs_path = ProjectSettings::get_singleton()->globalize_path(library_path);
  166. Vector<String> abs_dependencies_paths;
  167. if (!library_dependencies.is_empty()) {
  168. for (const SharedObject &dependency : library_dependencies) {
  169. abs_dependencies_paths.push_back(ProjectSettings::get_singleton()->globalize_path(dependency.path));
  170. }
  171. }
  172. OS::GDExtensionData data = {
  173. true, // also_set_library_path
  174. &library_path, // r_resolved_path
  175. Engine::get_singleton()->is_editor_hint(), // generate_temp_files
  176. &abs_dependencies_paths, // library_dependencies
  177. };
  178. err = OS::get_singleton()->open_dynamic_library(is_static_library ? String() : abs_path, library, &data);
  179. if (err != OK) {
  180. return err;
  181. }
  182. return OK;
  183. }
  184. Error GDExtensionLibraryLoader::initialize(GDExtensionInterfaceGetProcAddress p_get_proc_address, const Ref<GDExtension> &p_extension, GDExtensionInitialization *r_initialization) {
  185. #ifdef TOOLS_ENABLED
  186. p_extension->set_reloadable(is_reloadable && Engine::get_singleton()->is_extension_reloading_enabled());
  187. #endif
  188. for (const KeyValue<String, String> &icon : class_icon_paths) {
  189. p_extension->class_icon_paths[icon.key] = icon.value;
  190. }
  191. void *entry_funcptr = nullptr;
  192. Error err = OS::get_singleton()->get_dynamic_library_symbol_handle(library, entry_symbol, entry_funcptr, false);
  193. if (err != OK) {
  194. ERR_PRINT(vformat("GDExtension entry point '%s' not found in library %s.", entry_symbol, library_path));
  195. return err;
  196. }
  197. GDExtensionInitializationFunction initialization_function = (GDExtensionInitializationFunction)entry_funcptr;
  198. GDExtensionBool ret = initialization_function(p_get_proc_address, p_extension.ptr(), r_initialization);
  199. if (ret) {
  200. return OK;
  201. } else {
  202. ERR_PRINT(vformat("GDExtension initialization function '%s' returned an error.", entry_symbol));
  203. return FAILED;
  204. }
  205. }
  206. void GDExtensionLibraryLoader::close_library() {
  207. OS::get_singleton()->close_dynamic_library(library);
  208. library = nullptr;
  209. }
  210. bool GDExtensionLibraryLoader::is_library_open() const {
  211. return library != nullptr;
  212. }
  213. bool GDExtensionLibraryLoader::has_library_changed() const {
  214. #ifdef TOOLS_ENABLED
  215. // Check only that the last modified time is different (rather than checking
  216. // that it's newer) since some OS's (namely Windows) will preserve the modified
  217. // time by default when copying files.
  218. if (FileAccess::get_modified_time(resource_path) != resource_last_modified_time) {
  219. return true;
  220. }
  221. if (FileAccess::get_modified_time(library_path) != library_last_modified_time) {
  222. return true;
  223. }
  224. #endif
  225. return false;
  226. }
  227. bool GDExtensionLibraryLoader::library_exists() const {
  228. return FileAccess::exists(resource_path);
  229. }
  230. Error GDExtensionLibraryLoader::parse_gdextension_file(const String &p_path) {
  231. resource_path = p_path;
  232. Ref<ConfigFile> config;
  233. config.instantiate();
  234. Error err = config->load(p_path);
  235. if (err != OK) {
  236. ERR_PRINT(vformat("Error loading GDExtension configuration file: '%s'.", p_path));
  237. return err;
  238. }
  239. if (!config->has_section_key("configuration", "entry_symbol")) {
  240. ERR_PRINT(vformat("GDExtension configuration file must contain a \"configuration/entry_symbol\" key: '%s'.", p_path));
  241. return ERR_INVALID_DATA;
  242. }
  243. entry_symbol = config->get_value("configuration", "entry_symbol");
  244. uint32_t compatibility_minimum[3] = { 0, 0, 0 };
  245. if (config->has_section_key("configuration", "compatibility_minimum")) {
  246. String compat_string = config->get_value("configuration", "compatibility_minimum");
  247. Vector<int> parts = compat_string.split_ints(".");
  248. for (int i = 0; i < parts.size(); i++) {
  249. if (i >= 3) {
  250. break;
  251. }
  252. if (parts[i] >= 0) {
  253. compatibility_minimum[i] = parts[i];
  254. }
  255. }
  256. } else {
  257. ERR_PRINT(vformat("GDExtension configuration file must contain a \"configuration/compatibility_minimum\" key: '%s'.", p_path));
  258. return ERR_INVALID_DATA;
  259. }
  260. if (compatibility_minimum[0] < 4 || (compatibility_minimum[0] == 4 && compatibility_minimum[1] == 0)) {
  261. ERR_PRINT(vformat("GDExtension's compatibility_minimum (%d.%d.%d) must be at least 4.1.0: %s", compatibility_minimum[0], compatibility_minimum[1], compatibility_minimum[2], p_path));
  262. return ERR_INVALID_DATA;
  263. }
  264. bool compatible = true;
  265. // Check version lexicographically.
  266. if (VERSION_MAJOR != compatibility_minimum[0]) {
  267. compatible = VERSION_MAJOR > compatibility_minimum[0];
  268. } else if (VERSION_MINOR != compatibility_minimum[1]) {
  269. compatible = VERSION_MINOR > compatibility_minimum[1];
  270. } else {
  271. compatible = VERSION_PATCH >= compatibility_minimum[2];
  272. }
  273. if (!compatible) {
  274. ERR_PRINT(vformat("GDExtension only compatible with Godot version %d.%d.%d or later: %s", compatibility_minimum[0], compatibility_minimum[1], compatibility_minimum[2], p_path));
  275. return ERR_INVALID_DATA;
  276. }
  277. // Optionally check maximum compatibility.
  278. if (config->has_section_key("configuration", "compatibility_maximum")) {
  279. uint32_t compatibility_maximum[3] = { 0, 0, 0 };
  280. String compat_string = config->get_value("configuration", "compatibility_maximum");
  281. Vector<int> parts = compat_string.split_ints(".");
  282. for (int i = 0; i < 3; i++) {
  283. if (i < parts.size() && parts[i] >= 0) {
  284. compatibility_maximum[i] = parts[i];
  285. } else {
  286. // If a version part is missing, set the maximum to an arbitrary high value.
  287. compatibility_maximum[i] = 9999;
  288. }
  289. }
  290. compatible = true;
  291. if (VERSION_MAJOR != compatibility_maximum[0]) {
  292. compatible = VERSION_MAJOR < compatibility_maximum[0];
  293. } else if (VERSION_MINOR != compatibility_maximum[1]) {
  294. compatible = VERSION_MINOR < compatibility_maximum[1];
  295. }
  296. #if VERSION_PATCH
  297. // #if check to avoid -Wtype-limits warning when 0.
  298. else {
  299. compatible = VERSION_PATCH <= compatibility_maximum[2];
  300. }
  301. #endif
  302. if (!compatible) {
  303. ERR_PRINT(vformat("GDExtension only compatible with Godot version %s or earlier: %s", compat_string, p_path));
  304. return ERR_INVALID_DATA;
  305. }
  306. }
  307. library_path = find_extension_library(p_path, config, [](const String &p_feature) { return OS::get_singleton()->has_feature(p_feature); });
  308. if (library_path.is_empty()) {
  309. const String os_arch = OS::get_singleton()->get_name().to_lower() + "." + Engine::get_singleton()->get_architecture_name();
  310. ERR_PRINT(vformat("No GDExtension library found for current OS and architecture (%s) in configuration file: %s", os_arch, p_path));
  311. return ERR_FILE_NOT_FOUND;
  312. }
  313. is_static_library = library_path.ends_with(".a") || library_path.ends_with(".xcframework");
  314. if (!library_path.is_resource_file() && !library_path.is_absolute_path()) {
  315. library_path = p_path.get_base_dir().path_join(library_path);
  316. }
  317. #ifdef TOOLS_ENABLED
  318. is_reloadable = config->get_value("configuration", "reloadable", false);
  319. update_last_modified_time(
  320. FileAccess::get_modified_time(resource_path),
  321. FileAccess::get_modified_time(library_path));
  322. #endif
  323. library_dependencies = find_extension_dependencies(p_path, config, [](const String &p_feature) { return OS::get_singleton()->has_feature(p_feature); });
  324. // Handle icons if any are specified.
  325. if (config->has_section("icons")) {
  326. List<String> keys;
  327. config->get_section_keys("icons", &keys);
  328. for (const String &key : keys) {
  329. String icon_path = config->get_value("icons", key);
  330. if (icon_path.is_relative_path()) {
  331. icon_path = p_path.get_base_dir().path_join(icon_path);
  332. }
  333. class_icon_paths[key] = icon_path;
  334. }
  335. }
  336. return OK;
  337. }