gdextension.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <functional>
  33. #include "core/extension/gdextension_interface.h"
  34. #include "core/io/config_file.h"
  35. #include "core/io/resource_loader.h"
  36. #include "core/object/ref_counted.h"
  37. class GDExtension : public Resource {
  38. GDCLASS(GDExtension, Resource)
  39. void *library = nullptr; // pointer if valid,
  40. String library_path;
  41. struct Extension {
  42. ObjectGDExtension gdextension;
  43. };
  44. HashMap<StringName, Extension> extension_classes;
  45. static void _register_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo *p_extension_funcs);
  46. static void _register_extension_class_method(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionClassMethodInfo *p_method_info);
  47. 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);
  48. static void _register_extension_class_property(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionPropertyInfo *p_info, GDExtensionConstStringNamePtr p_setter, GDExtensionConstStringNamePtr p_getter);
  49. 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);
  50. static void _register_extension_class_property_group(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_group_name, GDExtensionConstStringNamePtr p_prefix);
  51. static void _register_extension_class_property_subgroup(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_subgroup_name, GDExtensionConstStringNamePtr p_prefix);
  52. 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);
  53. static void _unregister_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name);
  54. static void _get_library_path(GDExtensionClassLibraryPtr p_library, GDExtensionStringPtr r_path);
  55. GDExtensionInitialization initialization;
  56. int32_t level_initialized = -1;
  57. protected:
  58. static void _bind_methods();
  59. public:
  60. HashMap<String, String> class_icon_paths;
  61. static String get_extension_list_config_file();
  62. static String find_extension_library(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature, PackedStringArray *r_tags = nullptr);
  63. Error open_library(const String &p_path, const String &p_entry_symbol);
  64. void close_library();
  65. enum InitializationLevel {
  66. INITIALIZATION_LEVEL_CORE = GDEXTENSION_INITIALIZATION_CORE,
  67. INITIALIZATION_LEVEL_SERVERS = GDEXTENSION_INITIALIZATION_SERVERS,
  68. INITIALIZATION_LEVEL_SCENE = GDEXTENSION_INITIALIZATION_SCENE,
  69. INITIALIZATION_LEVEL_EDITOR = GDEXTENSION_INITIALIZATION_EDITOR
  70. };
  71. bool is_library_open() const;
  72. InitializationLevel get_minimum_library_initialization_level() const;
  73. void initialize_library(InitializationLevel p_level);
  74. void deinitialize_library(InitializationLevel p_level);
  75. static void register_interface_function(StringName p_function_name, GDExtensionInterfaceFunctionPtr p_function_pointer);
  76. static GDExtensionInterfaceFunctionPtr get_interface_function(StringName p_function_name);
  77. static void initialize_gdextensions();
  78. GDExtension();
  79. ~GDExtension();
  80. };
  81. VARIANT_ENUM_CAST(GDExtension::InitializationLevel)
  82. class GDExtensionResourceLoader : public ResourceFormatLoader {
  83. public:
  84. 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);
  85. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  86. virtual bool handles_type(const String &p_type) const;
  87. virtual String get_resource_type(const String &p_path) const;
  88. };
  89. #ifdef TOOLS_ENABLED
  90. class GDExtensionEditorPlugins {
  91. private:
  92. static Vector<StringName> extension_classes;
  93. protected:
  94. friend class EditorNode;
  95. // Since this in core, we can't directly reference EditorNode, so it will
  96. // set these function pointers in its constructor.
  97. typedef void (*EditorPluginRegisterFunc)(const StringName &p_class_name);
  98. static EditorPluginRegisterFunc editor_node_add_plugin;
  99. static EditorPluginRegisterFunc editor_node_remove_plugin;
  100. public:
  101. static void add_extension_class(const StringName &p_class_name);
  102. static void remove_extension_class(const StringName &p_class_name);
  103. static const Vector<StringName> &get_extension_classes() {
  104. return extension_classes;
  105. }
  106. };
  107. #endif // TOOLS_ENABLED
  108. #endif // GDEXTENSION_H