gdextension.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /**************************************************************************/
  2. /* gdextension.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.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/object/class_db.h"
  34. #include "core/object/method_bind.h"
  35. #include "core/os/os.h"
  36. String GDExtension::get_extension_list_config_file() {
  37. return ProjectSettings::get_singleton()->get_project_data_path().path_join("extension_list.cfg");
  38. }
  39. String GDExtension::find_extension_library(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature, PackedStringArray *r_tags) {
  40. // First, check the explicit libraries.
  41. if (p_config->has_section("libraries")) {
  42. List<String> libraries;
  43. p_config->get_section_keys("libraries", &libraries);
  44. // Iterate the libraries, finding the best matching tags.
  45. String best_library_path;
  46. Vector<String> best_library_tags;
  47. for (const String &E : libraries) {
  48. Vector<String> tags = E.split(".");
  49. bool all_tags_met = true;
  50. for (int i = 0; i < tags.size(); i++) {
  51. String tag = tags[i].strip_edges();
  52. if (!p_has_feature(tag)) {
  53. all_tags_met = false;
  54. break;
  55. }
  56. }
  57. if (all_tags_met && tags.size() > best_library_tags.size()) {
  58. best_library_path = p_config->get_value("libraries", E);
  59. best_library_tags = tags;
  60. }
  61. }
  62. if (!best_library_path.is_empty()) {
  63. if (best_library_path.is_relative_path()) {
  64. best_library_path = p_path.get_base_dir().path_join(best_library_path);
  65. }
  66. if (r_tags != nullptr) {
  67. r_tags->append_array(best_library_tags);
  68. }
  69. return best_library_path;
  70. }
  71. }
  72. // Second, try to autodetect
  73. String autodetect_library_prefix;
  74. if (p_config->has_section_key("configuration", "autodetect_library_prefix")) {
  75. autodetect_library_prefix = p_config->get_value("configuration", "autodetect_library_prefix");
  76. }
  77. if (!autodetect_library_prefix.is_empty()) {
  78. String autodetect_path = autodetect_library_prefix;
  79. if (autodetect_path.is_relative_path()) {
  80. autodetect_path = p_path.get_base_dir().path_join(autodetect_path);
  81. }
  82. // Find the folder and file parts of the prefix.
  83. String folder;
  84. String file_prefix;
  85. if (DirAccess::dir_exists_absolute(autodetect_path)) {
  86. folder = autodetect_path;
  87. } else if (DirAccess::dir_exists_absolute(autodetect_path.get_base_dir())) {
  88. folder = autodetect_path.get_base_dir();
  89. file_prefix = autodetect_path.get_file();
  90. } else {
  91. 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));
  92. }
  93. // Open the folder.
  94. Ref<DirAccess> dir = DirAccess::open(folder);
  95. ERR_FAIL_COND_V_MSG(!dir.is_valid(), 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));
  96. // Iterate the files and check the prefixes, finding the best matching file.
  97. String best_file;
  98. Vector<String> best_file_tags;
  99. dir->list_dir_begin();
  100. String file_name = dir->_get_next();
  101. while (file_name != "") {
  102. if (!dir->current_is_dir() && file_name.begins_with(file_prefix)) {
  103. // Check if the files matches all requested feature tags.
  104. String tags_str = file_name.trim_prefix(file_prefix);
  105. tags_str = tags_str.trim_suffix(tags_str.get_extension());
  106. Vector<String> tags = tags_str.split(".", false);
  107. bool all_tags_met = true;
  108. for (int i = 0; i < tags.size(); i++) {
  109. String tag = tags[i].strip_edges();
  110. if (!p_has_feature(tag)) {
  111. all_tags_met = false;
  112. break;
  113. }
  114. }
  115. // If all tags are found in the feature list, and we found more tags than before, use this file.
  116. if (all_tags_met && tags.size() > best_file_tags.size()) {
  117. best_file_tags = tags;
  118. best_file = file_name;
  119. }
  120. }
  121. file_name = dir->_get_next();
  122. }
  123. if (!best_file.is_empty()) {
  124. String library_path = folder.path_join(best_file);
  125. if (r_tags != nullptr) {
  126. r_tags->append_array(best_file_tags);
  127. }
  128. return library_path;
  129. }
  130. }
  131. return String();
  132. }
  133. class GDExtensionMethodBind : public MethodBind {
  134. GDExtensionClassMethodCall call_func;
  135. GDExtensionClassMethodPtrCall ptrcall_func;
  136. void *method_userdata;
  137. bool vararg;
  138. PropertyInfo return_value_info;
  139. GodotTypeInfo::Metadata return_value_metadata;
  140. List<PropertyInfo> arguments_info;
  141. List<GodotTypeInfo::Metadata> arguments_metadata;
  142. protected:
  143. virtual Variant::Type _gen_argument_type(int p_arg) const override {
  144. if (p_arg < 0) {
  145. return return_value_info.type;
  146. } else {
  147. return arguments_info[p_arg].type;
  148. }
  149. }
  150. virtual PropertyInfo _gen_argument_type_info(int p_arg) const override {
  151. if (p_arg < 0) {
  152. return return_value_info;
  153. } else {
  154. return arguments_info[p_arg];
  155. }
  156. }
  157. public:
  158. #ifdef DEBUG_METHODS_ENABLED
  159. virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const override {
  160. if (p_arg < 0) {
  161. return return_value_metadata;
  162. } else {
  163. return arguments_metadata[p_arg];
  164. }
  165. }
  166. #endif
  167. virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
  168. Variant ret;
  169. GDExtensionClassInstancePtr extension_instance = is_static() ? nullptr : p_object->_get_extension_instance();
  170. GDExtensionCallError ce{ GDEXTENSION_CALL_OK, 0, 0 };
  171. call_func(method_userdata, extension_instance, reinterpret_cast<GDExtensionConstVariantPtr *>(p_args), p_arg_count, (GDExtensionVariantPtr)&ret, &ce);
  172. r_error.error = Callable::CallError::Error(ce.error);
  173. r_error.argument = ce.argument;
  174. r_error.expected = ce.expected;
  175. return ret;
  176. }
  177. virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
  178. ERR_FAIL_COND_MSG(vararg, "Vararg methods don't have ptrcall support. This is most likely an engine bug.");
  179. GDExtensionClassInstancePtr extension_instance = p_object->_get_extension_instance();
  180. ptrcall_func(method_userdata, extension_instance, reinterpret_cast<GDExtensionConstTypePtr *>(p_args), (GDExtensionTypePtr)r_ret);
  181. }
  182. virtual bool is_vararg() const override {
  183. return false;
  184. }
  185. explicit GDExtensionMethodBind(const GDExtensionClassMethodInfo *p_method_info) {
  186. method_userdata = p_method_info->method_userdata;
  187. call_func = p_method_info->call_func;
  188. ptrcall_func = p_method_info->ptrcall_func;
  189. set_name(*reinterpret_cast<StringName *>(p_method_info->name));
  190. if (p_method_info->has_return_value) {
  191. return_value_info = PropertyInfo(*p_method_info->return_value_info);
  192. return_value_metadata = GodotTypeInfo::Metadata(p_method_info->return_value_metadata);
  193. }
  194. for (uint32_t i = 0; i < p_method_info->argument_count; i++) {
  195. arguments_info.push_back(PropertyInfo(p_method_info->arguments_info[i]));
  196. arguments_metadata.push_back(GodotTypeInfo::Metadata(p_method_info->arguments_metadata[i]));
  197. }
  198. set_hint_flags(p_method_info->method_flags);
  199. vararg = p_method_info->method_flags & GDEXTENSION_METHOD_FLAG_VARARG;
  200. _set_returns(p_method_info->has_return_value);
  201. _set_const(p_method_info->method_flags & GDEXTENSION_METHOD_FLAG_CONST);
  202. _set_static(p_method_info->method_flags & GDEXTENSION_METHOD_FLAG_STATIC);
  203. #ifdef DEBUG_METHODS_ENABLED
  204. _generate_argument_types(p_method_info->argument_count);
  205. #endif
  206. set_argument_count(p_method_info->argument_count);
  207. Vector<Variant> defargs;
  208. defargs.resize(p_method_info->default_argument_count);
  209. for (uint32_t i = 0; i < p_method_info->default_argument_count; i++) {
  210. defargs.write[i] = *static_cast<Variant *>(p_method_info->default_arguments[i]);
  211. }
  212. set_default_arguments(defargs);
  213. }
  214. };
  215. static GDExtensionInterface gdextension_interface;
  216. void GDExtension::_register_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo *p_extension_funcs) {
  217. GDExtension *self = reinterpret_cast<GDExtension *>(p_library);
  218. StringName class_name = *reinterpret_cast<const StringName *>(p_class_name);
  219. StringName parent_class_name = *reinterpret_cast<const StringName *>(p_parent_class_name);
  220. ERR_FAIL_COND_MSG(!String(class_name).is_valid_identifier(), "Attempt to register extension class '" + class_name + "', which is not a valid class identifier.");
  221. ERR_FAIL_COND_MSG(ClassDB::class_exists(class_name), "Attempt to register extension class '" + class_name + "', which appears to be already registered.");
  222. Extension *parent_extension = nullptr;
  223. if (self->extension_classes.has(parent_class_name)) {
  224. parent_extension = &self->extension_classes[parent_class_name];
  225. } else if (ClassDB::class_exists(parent_class_name)) {
  226. if (ClassDB::get_api_type(parent_class_name) == ClassDB::API_EXTENSION || ClassDB::get_api_type(parent_class_name) == ClassDB::API_EDITOR_EXTENSION) {
  227. ERR_PRINT("Unimplemented yet");
  228. //inheriting from another extension
  229. } else {
  230. //inheriting from engine class
  231. }
  232. } else {
  233. ERR_FAIL_MSG("Attempt to register an extension class '" + String(class_name) + "' using non-existing parent class '" + String(parent_class_name) + "'");
  234. }
  235. self->extension_classes[class_name] = Extension();
  236. Extension *extension = &self->extension_classes[class_name];
  237. if (parent_extension) {
  238. extension->gdextension.parent = &parent_extension->gdextension;
  239. parent_extension->gdextension.children.push_back(&extension->gdextension);
  240. }
  241. extension->gdextension.parent_class_name = parent_class_name;
  242. extension->gdextension.class_name = class_name;
  243. extension->gdextension.editor_class = self->level_initialized == INITIALIZATION_LEVEL_EDITOR;
  244. extension->gdextension.is_virtual = p_extension_funcs->is_virtual;
  245. extension->gdextension.is_abstract = p_extension_funcs->is_abstract;
  246. extension->gdextension.set = p_extension_funcs->set_func;
  247. extension->gdextension.get = p_extension_funcs->get_func;
  248. extension->gdextension.get_property_list = p_extension_funcs->get_property_list_func;
  249. extension->gdextension.free_property_list = p_extension_funcs->free_property_list_func;
  250. extension->gdextension.property_can_revert = p_extension_funcs->property_can_revert_func;
  251. extension->gdextension.property_get_revert = p_extension_funcs->property_get_revert_func;
  252. extension->gdextension.notification = p_extension_funcs->notification_func;
  253. extension->gdextension.to_string = p_extension_funcs->to_string_func;
  254. extension->gdextension.reference = p_extension_funcs->reference_func;
  255. extension->gdextension.unreference = p_extension_funcs->unreference_func;
  256. extension->gdextension.class_userdata = p_extension_funcs->class_userdata;
  257. extension->gdextension.create_instance = p_extension_funcs->create_instance_func;
  258. extension->gdextension.free_instance = p_extension_funcs->free_instance_func;
  259. extension->gdextension.get_virtual = p_extension_funcs->get_virtual_func;
  260. extension->gdextension.get_rid = p_extension_funcs->get_rid_func;
  261. ClassDB::register_extension_class(&extension->gdextension);
  262. }
  263. void GDExtension::_register_extension_class_method(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionClassMethodInfo *p_method_info) {
  264. GDExtension *self = reinterpret_cast<GDExtension *>(p_library);
  265. StringName class_name = *reinterpret_cast<const StringName *>(p_class_name);
  266. StringName method_name = *reinterpret_cast<const StringName *>(p_method_info->name);
  267. ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension method '" + String(method_name) + "' for unexisting class '" + class_name + "'.");
  268. //Extension *extension = &self->extension_classes[class_name];
  269. GDExtensionMethodBind *method = memnew(GDExtensionMethodBind(p_method_info));
  270. method->set_instance_class(class_name);
  271. ClassDB::bind_method_custom(class_name, method);
  272. }
  273. void GDExtension::_register_extension_class_integer_constant(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_enum_name, GDExtensionConstStringNamePtr p_constant_name, GDExtensionInt p_constant_value, GDExtensionBool p_is_bitfield) {
  274. GDExtension *self = reinterpret_cast<GDExtension *>(p_library);
  275. StringName class_name = *reinterpret_cast<const StringName *>(p_class_name);
  276. StringName enum_name = *reinterpret_cast<const StringName *>(p_enum_name);
  277. StringName constant_name = *reinterpret_cast<const StringName *>(p_constant_name);
  278. ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension constant '" + constant_name + "' for unexisting class '" + class_name + "'.");
  279. ClassDB::bind_integer_constant(class_name, enum_name, constant_name, p_constant_value, p_is_bitfield);
  280. }
  281. void GDExtension::_register_extension_class_property(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionPropertyInfo *p_info, GDExtensionConstStringNamePtr p_setter, GDExtensionConstStringNamePtr p_getter) {
  282. GDExtension *self = reinterpret_cast<GDExtension *>(p_library);
  283. StringName class_name = *reinterpret_cast<const StringName *>(p_class_name);
  284. StringName setter = *reinterpret_cast<const StringName *>(p_setter);
  285. StringName getter = *reinterpret_cast<const StringName *>(p_getter);
  286. String property_name = *reinterpret_cast<const StringName *>(p_info->name);
  287. ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property '" + property_name + "' for unexisting class '" + class_name + "'.");
  288. //Extension *extension = &self->extension_classes[class_name];
  289. PropertyInfo pinfo(*p_info);
  290. ClassDB::add_property(class_name, pinfo, setter, getter);
  291. }
  292. void GDExtension::_register_extension_class_property_group(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringPtr p_group_name, GDExtensionConstStringPtr p_prefix) {
  293. GDExtension *self = reinterpret_cast<GDExtension *>(p_library);
  294. StringName class_name = *reinterpret_cast<const StringName *>(p_class_name);
  295. String group_name = *reinterpret_cast<const String *>(p_group_name);
  296. String prefix = *reinterpret_cast<const String *>(p_prefix);
  297. ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property group '" + group_name + "' for unexisting class '" + class_name + "'.");
  298. ClassDB::add_property_group(class_name, group_name, prefix);
  299. }
  300. void GDExtension::_register_extension_class_property_subgroup(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringPtr p_subgroup_name, GDExtensionConstStringPtr p_prefix) {
  301. GDExtension *self = reinterpret_cast<GDExtension *>(p_library);
  302. StringName class_name = *reinterpret_cast<const StringName *>(p_class_name);
  303. String subgroup_name = *reinterpret_cast<const String *>(p_subgroup_name);
  304. String prefix = *reinterpret_cast<const String *>(p_prefix);
  305. ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property subgroup '" + subgroup_name + "' for unexisting class '" + class_name + "'.");
  306. ClassDB::add_property_subgroup(class_name, subgroup_name, prefix);
  307. }
  308. void GDExtension::_register_extension_class_signal(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_signal_name, const GDExtensionPropertyInfo *p_argument_info, GDExtensionInt p_argument_count) {
  309. GDExtension *self = reinterpret_cast<GDExtension *>(p_library);
  310. StringName class_name = *reinterpret_cast<const StringName *>(p_class_name);
  311. StringName signal_name = *reinterpret_cast<const StringName *>(p_signal_name);
  312. ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class signal '" + signal_name + "' for unexisting class '" + class_name + "'.");
  313. MethodInfo s;
  314. s.name = signal_name;
  315. for (int i = 0; i < p_argument_count; i++) {
  316. PropertyInfo arg(p_argument_info[i]);
  317. s.arguments.push_back(arg);
  318. }
  319. ClassDB::add_signal(class_name, s);
  320. }
  321. void GDExtension::_unregister_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name) {
  322. GDExtension *self = reinterpret_cast<GDExtension *>(p_library);
  323. StringName class_name = *reinterpret_cast<const StringName *>(p_class_name);
  324. ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to unregister unexisting extension class '" + class_name + "'.");
  325. Extension *ext = &self->extension_classes[class_name];
  326. ERR_FAIL_COND_MSG(ext->gdextension.children.size(), "Attempt to unregister class '" + class_name + "' while other extension classes inherit from it.");
  327. ClassDB::unregister_extension_class(class_name);
  328. if (ext->gdextension.parent != nullptr) {
  329. ext->gdextension.parent->children.erase(&ext->gdextension);
  330. }
  331. self->extension_classes.erase(class_name);
  332. }
  333. void GDExtension::_get_library_path(GDExtensionClassLibraryPtr p_library, GDExtensionStringPtr r_path) {
  334. GDExtension *self = reinterpret_cast<GDExtension *>(p_library);
  335. *(String *)r_path = self->library_path;
  336. }
  337. Error GDExtension::open_library(const String &p_path, const String &p_entry_symbol) {
  338. Error err = OS::get_singleton()->open_dynamic_library(p_path, library, true, &library_path);
  339. if (err != OK) {
  340. ERR_PRINT("GDExtension dynamic library not found: " + p_path);
  341. return err;
  342. }
  343. void *entry_funcptr = nullptr;
  344. err = OS::get_singleton()->get_dynamic_library_symbol_handle(library, p_entry_symbol, entry_funcptr, false);
  345. if (err != OK) {
  346. ERR_PRINT("GDExtension entry point '" + p_entry_symbol + "' not found in library " + p_path);
  347. OS::get_singleton()->close_dynamic_library(library);
  348. return err;
  349. }
  350. GDExtensionInitializationFunction initialization_function = (GDExtensionInitializationFunction)entry_funcptr;
  351. if (initialization_function(&gdextension_interface, this, &initialization)) {
  352. level_initialized = -1;
  353. return OK;
  354. } else {
  355. ERR_PRINT("GDExtension initialization function '" + p_entry_symbol + "' returned an error.");
  356. return FAILED;
  357. }
  358. }
  359. void GDExtension::close_library() {
  360. ERR_FAIL_COND(library == nullptr);
  361. OS::get_singleton()->close_dynamic_library(library);
  362. library = nullptr;
  363. }
  364. bool GDExtension::is_library_open() const {
  365. return library != nullptr;
  366. }
  367. GDExtension::InitializationLevel GDExtension::get_minimum_library_initialization_level() const {
  368. ERR_FAIL_COND_V(library == nullptr, INITIALIZATION_LEVEL_CORE);
  369. return InitializationLevel(initialization.minimum_initialization_level);
  370. }
  371. void GDExtension::initialize_library(InitializationLevel p_level) {
  372. ERR_FAIL_COND(library == nullptr);
  373. ERR_FAIL_COND_MSG(p_level <= int32_t(level_initialized), vformat("Level '%d' must be higher than the current level '%d'", p_level, level_initialized));
  374. level_initialized = int32_t(p_level);
  375. ERR_FAIL_COND(initialization.initialize == nullptr);
  376. initialization.initialize(initialization.userdata, GDExtensionInitializationLevel(p_level));
  377. }
  378. void GDExtension::deinitialize_library(InitializationLevel p_level) {
  379. ERR_FAIL_COND(library == nullptr);
  380. ERR_FAIL_COND(p_level > int32_t(level_initialized));
  381. level_initialized = int32_t(p_level) - 1;
  382. initialization.deinitialize(initialization.userdata, GDExtensionInitializationLevel(p_level));
  383. }
  384. void GDExtension::_bind_methods() {
  385. ClassDB::bind_method(D_METHOD("open_library", "path", "entry_symbol"), &GDExtension::open_library);
  386. ClassDB::bind_method(D_METHOD("close_library"), &GDExtension::close_library);
  387. ClassDB::bind_method(D_METHOD("is_library_open"), &GDExtension::is_library_open);
  388. ClassDB::bind_method(D_METHOD("get_minimum_library_initialization_level"), &GDExtension::get_minimum_library_initialization_level);
  389. ClassDB::bind_method(D_METHOD("initialize_library", "level"), &GDExtension::initialize_library);
  390. BIND_ENUM_CONSTANT(INITIALIZATION_LEVEL_CORE);
  391. BIND_ENUM_CONSTANT(INITIALIZATION_LEVEL_SERVERS);
  392. BIND_ENUM_CONSTANT(INITIALIZATION_LEVEL_SCENE);
  393. BIND_ENUM_CONSTANT(INITIALIZATION_LEVEL_EDITOR);
  394. }
  395. GDExtension::GDExtension() {
  396. }
  397. GDExtension::~GDExtension() {
  398. if (library != nullptr) {
  399. close_library();
  400. }
  401. }
  402. extern void gdextension_setup_interface(GDExtensionInterface *p_interface);
  403. void GDExtension::initialize_gdextensions() {
  404. gdextension_setup_interface(&gdextension_interface);
  405. gdextension_interface.classdb_register_extension_class = _register_extension_class;
  406. gdextension_interface.classdb_register_extension_class_method = _register_extension_class_method;
  407. gdextension_interface.classdb_register_extension_class_integer_constant = _register_extension_class_integer_constant;
  408. gdextension_interface.classdb_register_extension_class_property = _register_extension_class_property;
  409. gdextension_interface.classdb_register_extension_class_property_group = _register_extension_class_property_group;
  410. gdextension_interface.classdb_register_extension_class_property_subgroup = _register_extension_class_property_subgroup;
  411. gdextension_interface.classdb_register_extension_class_signal = _register_extension_class_signal;
  412. gdextension_interface.classdb_unregister_extension_class = _unregister_extension_class;
  413. gdextension_interface.get_library_path = _get_library_path;
  414. }
  415. Ref<Resource> GDExtensionResourceLoader::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  416. Ref<ConfigFile> config;
  417. config.instantiate();
  418. Error err = config->load(p_path);
  419. if (r_error) {
  420. *r_error = err;
  421. }
  422. if (err != OK) {
  423. ERR_PRINT("Error loading GDExtension configuration file: " + p_path);
  424. return Ref<Resource>();
  425. }
  426. if (!config->has_section_key("configuration", "entry_symbol")) {
  427. if (r_error) {
  428. *r_error = ERR_INVALID_DATA;
  429. }
  430. ERR_PRINT("GDExtension configuration file must contain a \"configuration/entry_symbol\" key: " + p_path);
  431. return Ref<Resource>();
  432. }
  433. String entry_symbol = config->get_value("configuration", "entry_symbol");
  434. String library_path = GDExtension::find_extension_library(p_path, config, [](String p_feature) { return OS::get_singleton()->has_feature(p_feature); });
  435. if (library_path.is_empty()) {
  436. if (r_error) {
  437. *r_error = ERR_FILE_NOT_FOUND;
  438. }
  439. const String os_arch = OS::get_singleton()->get_name().to_lower() + "." + Engine::get_singleton()->get_architecture_name();
  440. ERR_PRINT(vformat("No GDExtension library found for current OS and architecture (%s) in configuration file: %s", os_arch, p_path));
  441. return Ref<Resource>();
  442. }
  443. if (!library_path.is_resource_file() && !library_path.is_absolute_path()) {
  444. library_path = p_path.get_base_dir().path_join(library_path);
  445. }
  446. Ref<GDExtension> lib;
  447. lib.instantiate();
  448. String abs_path = ProjectSettings::get_singleton()->globalize_path(library_path);
  449. err = lib->open_library(abs_path, entry_symbol);
  450. if (r_error) {
  451. *r_error = err;
  452. }
  453. if (err != OK) {
  454. // Errors already logged in open_library()
  455. return Ref<Resource>();
  456. }
  457. return lib;
  458. }
  459. void GDExtensionResourceLoader::get_recognized_extensions(List<String> *p_extensions) const {
  460. p_extensions->push_back("gdextension");
  461. }
  462. bool GDExtensionResourceLoader::handles_type(const String &p_type) const {
  463. return p_type == "GDExtension";
  464. }
  465. String GDExtensionResourceLoader::get_resource_type(const String &p_path) const {
  466. String el = p_path.get_extension().to_lower();
  467. if (el == "gdextension") {
  468. return "GDExtension";
  469. }
  470. return "";
  471. }