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