gdextension.h 7.0 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_group(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_group_name, GDExtensionConstStringNamePtr p_prefix);
  50. static void _register_extension_class_property_subgroup(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_subgroup_name, GDExtensionConstStringNamePtr p_prefix);
  51. 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);
  52. static void _unregister_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name);
  53. static void _get_library_path(GDExtensionClassLibraryPtr p_library, GDExtensionStringPtr r_path);
  54. GDExtensionInitialization initialization;
  55. int32_t level_initialized = -1;
  56. protected:
  57. static void _bind_methods();
  58. public:
  59. HashMap<String, String> class_icon_paths;
  60. static String get_extension_list_config_file();
  61. static String find_extension_library(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature, PackedStringArray *r_tags = nullptr);
  62. Error open_library(const String &p_path, const String &p_entry_symbol);
  63. void close_library();
  64. enum InitializationLevel {
  65. INITIALIZATION_LEVEL_CORE = GDEXTENSION_INITIALIZATION_CORE,
  66. INITIALIZATION_LEVEL_SERVERS = GDEXTENSION_INITIALIZATION_SERVERS,
  67. INITIALIZATION_LEVEL_SCENE = GDEXTENSION_INITIALIZATION_SCENE,
  68. INITIALIZATION_LEVEL_EDITOR = GDEXTENSION_INITIALIZATION_EDITOR
  69. };
  70. bool is_library_open() const;
  71. InitializationLevel get_minimum_library_initialization_level() const;
  72. void initialize_library(InitializationLevel p_level);
  73. void deinitialize_library(InitializationLevel p_level);
  74. static void register_interface_function(StringName p_function_name, GDExtensionInterfaceFunctionPtr p_function_pointer);
  75. static GDExtensionInterfaceFunctionPtr get_interface_function(StringName p_function_name);
  76. static void initialize_gdextensions();
  77. GDExtension();
  78. ~GDExtension();
  79. };
  80. VARIANT_ENUM_CAST(GDExtension::InitializationLevel)
  81. class GDExtensionResourceLoader : public ResourceFormatLoader {
  82. public:
  83. 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);
  84. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  85. virtual bool handles_type(const String &p_type) const;
  86. virtual String get_resource_type(const String &p_path) const;
  87. };
  88. #ifdef TOOLS_ENABLED
  89. class GDExtensionEditorPlugins {
  90. private:
  91. static Vector<StringName> extension_classes;
  92. protected:
  93. friend class EditorNode;
  94. // Since this in core, we can't directly reference EditorNode, so it will
  95. // set these function pointers in its constructor.
  96. typedef void (*EditorPluginRegisterFunc)(const StringName &p_class_name);
  97. static EditorPluginRegisterFunc editor_node_add_plugin;
  98. static EditorPluginRegisterFunc editor_node_remove_plugin;
  99. public:
  100. static void add_extension_class(const StringName &p_class_name);
  101. static void remove_extension_class(const StringName &p_class_name);
  102. static const Vector<StringName> &get_extension_classes() {
  103. return extension_classes;
  104. }
  105. };
  106. #endif // TOOLS_ENABLED
  107. #endif // GDEXTENSION_H