gdextension.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**************************************************************************/
  2. /* gdextension.h */
  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. #ifndef GDEXTENSION_H
  31. #define GDEXTENSION_H
  32. #include "core/extension/gdextension_interface.h"
  33. #include "core/extension/gdextension_loader.h"
  34. #include "core/io/config_file.h"
  35. #include "core/io/resource_loader.h"
  36. #include "core/object/ref_counted.h"
  37. class GDExtensionMethodBind;
  38. class GDExtension : public Resource {
  39. GDCLASS(GDExtension, Resource)
  40. friend class GDExtensionManager;
  41. Ref<GDExtensionLoader> loader;
  42. bool reloadable = false;
  43. struct Extension {
  44. ObjectGDExtension gdextension;
  45. #ifdef TOOLS_ENABLED
  46. bool is_reloading = false;
  47. HashMap<StringName, GDExtensionMethodBind *> methods;
  48. HashSet<ObjectID> instances;
  49. struct InstanceState {
  50. List<Pair<String, Variant>> properties;
  51. bool is_placeholder = false;
  52. };
  53. HashMap<ObjectID, InstanceState> instance_state;
  54. #endif
  55. };
  56. HashMap<StringName, Extension> extension_classes;
  57. struct ClassCreationDeprecatedInfo {
  58. #ifndef DISABLE_DEPRECATED
  59. GDExtensionClassNotification notification_func = nullptr;
  60. GDExtensionClassFreePropertyList free_property_list_func = nullptr;
  61. GDExtensionClassCreateInstance create_instance_func = nullptr;
  62. GDExtensionClassGetRID get_rid_func = nullptr;
  63. #endif // DISABLE_DEPRECATED
  64. };
  65. #ifndef DISABLE_DEPRECATED
  66. static void _register_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo *p_extension_funcs);
  67. static void _register_extension_class2(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo2 *p_extension_funcs);
  68. static void _register_extension_class3(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo3 *p_extension_funcs);
  69. #endif // DISABLE_DEPRECATED
  70. static void _register_extension_class4(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo4 *p_extension_funcs);
  71. static void _register_extension_class_internal(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo4 *p_extension_funcs, const ClassCreationDeprecatedInfo *p_deprecated_funcs = nullptr);
  72. static void _register_extension_class_method(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionClassMethodInfo *p_method_info);
  73. static void _register_extension_class_virtual_method(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionClassVirtualMethodInfo *p_method_info);
  74. static void _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);
  75. static void _register_extension_class_property(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionPropertyInfo *p_info, GDExtensionConstStringNamePtr p_setter, GDExtensionConstStringNamePtr p_getter);
  76. static void _register_extension_class_property_indexed(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionPropertyInfo *p_info, GDExtensionConstStringNamePtr p_setter, GDExtensionConstStringNamePtr p_getter, GDExtensionInt p_index);
  77. static void _register_extension_class_property_group(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_group_name, GDExtensionConstStringNamePtr p_prefix);
  78. static void _register_extension_class_property_subgroup(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_subgroup_name, GDExtensionConstStringNamePtr p_prefix);
  79. static void _register_extension_class_signal(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_signal_name, const GDExtensionPropertyInfo *p_argument_info, GDExtensionInt p_argument_count);
  80. static void _unregister_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name);
  81. static void _get_library_path(GDExtensionClassLibraryPtr p_library, GDExtensionStringPtr r_path);
  82. GDExtensionInitialization initialization;
  83. int32_t level_initialized = -1;
  84. #ifdef TOOLS_ENABLED
  85. bool is_reloading = false;
  86. Vector<GDExtensionMethodBind *> invalid_methods;
  87. Vector<ObjectID> instance_bindings;
  88. static void _track_instance(void *p_user_data, void *p_instance);
  89. static void _untrack_instance(void *p_user_data, void *p_instance);
  90. void _clear_extension(Extension *p_extension);
  91. // Only called by GDExtensionManager during the reload process.
  92. void prepare_reload();
  93. void finish_reload();
  94. void clear_instance_bindings();
  95. #endif
  96. static HashMap<StringName, GDExtensionInterfaceFunctionPtr> gdextension_interface_functions;
  97. protected:
  98. static void _bind_methods();
  99. public:
  100. HashMap<String, String> class_icon_paths;
  101. virtual bool editor_can_reload_from_file() override { return false; } // Reloading is handled in a special way.
  102. static String get_extension_list_config_file();
  103. const Ref<GDExtensionLoader> get_loader() const { return loader; }
  104. Error open_library(const String &p_path, const Ref<GDExtensionLoader> &p_loader);
  105. void close_library();
  106. bool is_library_open() const;
  107. enum InitializationLevel {
  108. INITIALIZATION_LEVEL_CORE = GDEXTENSION_INITIALIZATION_CORE,
  109. INITIALIZATION_LEVEL_SERVERS = GDEXTENSION_INITIALIZATION_SERVERS,
  110. INITIALIZATION_LEVEL_SCENE = GDEXTENSION_INITIALIZATION_SCENE,
  111. INITIALIZATION_LEVEL_EDITOR = GDEXTENSION_INITIALIZATION_EDITOR
  112. };
  113. protected:
  114. #ifndef DISABLE_DEPRECATED
  115. Error _open_library_bind_compat_88418(const String &p_path, const String &p_entry_symbol);
  116. void _close_library_bind_compat_88418();
  117. void _initialize_library_bind_compat_88418(InitializationLevel p_level);
  118. static void _bind_compatibility_methods();
  119. #endif
  120. public:
  121. #ifdef TOOLS_ENABLED
  122. bool is_reloadable() const { return reloadable; }
  123. void set_reloadable(bool p_reloadable) { reloadable = p_reloadable; }
  124. bool has_library_changed() const;
  125. void track_instance_binding(Object *p_object);
  126. void untrack_instance_binding(Object *p_object);
  127. #endif
  128. InitializationLevel get_minimum_library_initialization_level() const;
  129. void initialize_library(InitializationLevel p_level);
  130. void deinitialize_library(InitializationLevel p_level);
  131. static void register_interface_function(const StringName &p_function_name, GDExtensionInterfaceFunctionPtr p_function_pointer);
  132. static GDExtensionInterfaceFunctionPtr get_interface_function(const StringName &p_function_name);
  133. static void initialize_gdextensions();
  134. static void finalize_gdextensions();
  135. GDExtension();
  136. ~GDExtension();
  137. };
  138. VARIANT_ENUM_CAST(GDExtension::InitializationLevel)
  139. class GDExtensionResourceLoader : public ResourceFormatLoader {
  140. public:
  141. static Error load_gdextension_resource(const String &p_path, Ref<GDExtension> &p_extension);
  142. virtual Ref<Resource> load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
  143. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  144. virtual bool handles_type(const String &p_type) const override;
  145. virtual String get_resource_type(const String &p_path) const override;
  146. };
  147. #ifdef TOOLS_ENABLED
  148. class GDExtensionEditorPlugins {
  149. private:
  150. static Vector<StringName> extension_classes;
  151. protected:
  152. friend class EditorNode;
  153. // Since this in core, we can't directly reference EditorNode, so it will
  154. // set these function pointers in its constructor.
  155. typedef void (*EditorPluginRegisterFunc)(const StringName &p_class_name);
  156. static EditorPluginRegisterFunc editor_node_add_plugin;
  157. static EditorPluginRegisterFunc editor_node_remove_plugin;
  158. public:
  159. static void add_extension_class(const StringName &p_class_name);
  160. static void remove_extension_class(const StringName &p_class_name);
  161. static const Vector<StringName> &get_extension_classes() {
  162. return extension_classes;
  163. }
  164. };
  165. class GDExtensionEditorHelp {
  166. protected:
  167. friend class EditorHelp;
  168. // Similarly to EditorNode above, we need to be able to ask EditorHelp to parse
  169. // new documentation data. Note though that, differently from EditorHelp, this
  170. // is initialized even _before_ it gets instantiated, as we need to rely on
  171. // this method while initializing the engine.
  172. typedef void (*EditorHelpLoadXmlBufferFunc)(const uint8_t *p_buffer, int p_size);
  173. static EditorHelpLoadXmlBufferFunc editor_help_load_xml_buffer;
  174. typedef void (*EditorHelpRemoveClassFunc)(const String &p_class);
  175. static EditorHelpRemoveClassFunc editor_help_remove_class;
  176. public:
  177. static void load_xml_buffer(const uint8_t *p_buffer, int p_size);
  178. static void remove_class(const String &p_class);
  179. };
  180. #endif // TOOLS_ENABLED
  181. #endif // GDEXTENSION_H