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
31#ifndef GDEXTENSION_H
32#define GDEXTENSION_H
33
34#include <functional>
35
36#include "core/extension/gdextension_interface.h"
37#include "core/io/config_file.h"
38#include "core/io/resource_loader.h"
39#include "core/object/ref_counted.h"
40
41class GDExtension : public Resource {
42 GDCLASS(GDExtension, Resource)
43
44 void *library = nullptr; // pointer if valid,
45 String library_path;
46#if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
47 String temp_lib_path;
48#endif
49
50 struct Extension {
51 ObjectGDExtension gdextension;
52 };
53
54 HashMap<StringName, Extension> extension_classes;
55
56 struct ClassCreationDeprecatedInfo {
57#ifndef DISABLE_DEPRECATED
58 GDExtensionClassNotification notification_func = nullptr;
59#endif // DISABLE_DEPRECATED
60 };
61
62#ifndef DISABLE_DEPRECATED
63 static void _register_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo *p_extension_funcs);
64#endif // DISABLE_DEPRECATED
65 static void _register_extension_class2(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo2 *p_extension_funcs);
66 static void _register_extension_class_internal(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_parent_class_name, const GDExtensionClassCreationInfo2 *p_extension_funcs, const ClassCreationDeprecatedInfo *p_deprecated_funcs = nullptr);
67 static void _register_extension_class_method(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionClassMethodInfo *p_method_info);
68 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);
69 static void _register_extension_class_property(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, const GDExtensionPropertyInfo *p_info, GDExtensionConstStringNamePtr p_setter, GDExtensionConstStringNamePtr p_getter);
70 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);
71 static void _register_extension_class_property_group(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_group_name, GDExtensionConstStringNamePtr p_prefix);
72 static void _register_extension_class_property_subgroup(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstStringNamePtr p_subgroup_name, GDExtensionConstStringNamePtr p_prefix);
73 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);
74 static void _unregister_extension_class(GDExtensionClassLibraryPtr p_library, GDExtensionConstStringNamePtr p_class_name);
75 static void _get_library_path(GDExtensionClassLibraryPtr p_library, GDExtensionStringPtr r_path);
76
77 GDExtensionInitialization initialization;
78 int32_t level_initialized = -1;
79
80protected:
81 static void _bind_methods();
82
83public:
84 HashMap<String, String> class_icon_paths;
85
86 static String get_extension_list_config_file();
87 static String find_extension_library(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature, PackedStringArray *r_tags = nullptr);
88
89 Error open_library(const String &p_path, const String &p_entry_symbol);
90 void close_library();
91
92#if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
93 void set_temp_library_path(const String &p_path) { temp_lib_path = p_path; }
94 String get_temp_library_path() const { return temp_lib_path; }
95#endif
96
97 enum InitializationLevel {
98 INITIALIZATION_LEVEL_CORE = GDEXTENSION_INITIALIZATION_CORE,
99 INITIALIZATION_LEVEL_SERVERS = GDEXTENSION_INITIALIZATION_SERVERS,
100 INITIALIZATION_LEVEL_SCENE = GDEXTENSION_INITIALIZATION_SCENE,
101 INITIALIZATION_LEVEL_EDITOR = GDEXTENSION_INITIALIZATION_EDITOR
102 };
103
104 bool is_library_open() const;
105
106 InitializationLevel get_minimum_library_initialization_level() const;
107 void initialize_library(InitializationLevel p_level);
108 void deinitialize_library(InitializationLevel p_level);
109
110 static void register_interface_function(StringName p_function_name, GDExtensionInterfaceFunctionPtr p_function_pointer);
111 static GDExtensionInterfaceFunctionPtr get_interface_function(StringName p_function_name);
112 static void initialize_gdextensions();
113
114 GDExtension();
115 ~GDExtension();
116};
117
118VARIANT_ENUM_CAST(GDExtension::InitializationLevel)
119
120class GDExtensionResourceLoader : public ResourceFormatLoader {
121public:
122 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);
123 virtual void get_recognized_extensions(List<String> *p_extensions) const;
124 virtual bool handles_type(const String &p_type) const;
125 virtual String get_resource_type(const String &p_path) const;
126};
127
128#ifdef TOOLS_ENABLED
129class GDExtensionEditorPlugins {
130private:
131 static Vector<StringName> extension_classes;
132
133protected:
134 friend class EditorNode;
135
136 // Since this in core, we can't directly reference EditorNode, so it will
137 // set these function pointers in its constructor.
138 typedef void (*EditorPluginRegisterFunc)(const StringName &p_class_name);
139 static EditorPluginRegisterFunc editor_node_add_plugin;
140 static EditorPluginRegisterFunc editor_node_remove_plugin;
141
142public:
143 static void add_extension_class(const StringName &p_class_name);
144 static void remove_extension_class(const StringName &p_class_name);
145
146 static const Vector<StringName> &get_extension_classes() {
147 return extension_classes;
148 }
149};
150#endif // TOOLS_ENABLED
151
152#endif // GDEXTENSION_H
153