2023-01-10 15:14:30 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* class_db.hpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2022-10-09 06:47:07 +00:00
|
|
|
#ifndef GODOT_CLASS_DB_HPP
|
|
|
|
#define GODOT_CLASS_DB_HPP
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2022-12-13 23:40:17 +00:00
|
|
|
#include <gdextension_interface.h>
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
#include <godot_cpp/core/defs.hpp>
|
|
|
|
#include <godot_cpp/core/error_macros.hpp>
|
|
|
|
#include <godot_cpp/core/method_bind.hpp>
|
|
|
|
#include <godot_cpp/core/object.hpp>
|
|
|
|
|
2023-07-07 02:16:27 +00:00
|
|
|
#include <godot_cpp/classes/class_db_singleton.hpp>
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
#include <list>
|
2021-09-26 06:15:01 +00:00
|
|
|
#include <set>
|
2021-08-18 14:03:52 +00:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2022-10-22 17:42:48 +00:00
|
|
|
// Needed to use StringName as key in `std::unordered_map`
|
|
|
|
template <>
|
|
|
|
struct std::hash<godot::StringName> {
|
|
|
|
std::size_t operator()(godot::StringName const &s) const noexcept {
|
|
|
|
return s.hash();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
namespace godot {
|
|
|
|
|
2022-05-04 11:27:08 +00:00
|
|
|
#define DEFVAL(m_defval) (m_defval)
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
struct MethodDefinition {
|
2022-10-22 17:42:48 +00:00
|
|
|
StringName name;
|
|
|
|
std::list<StringName> args;
|
2021-08-18 14:03:52 +00:00
|
|
|
MethodDefinition() {}
|
2022-10-22 17:42:48 +00:00
|
|
|
MethodDefinition(StringName p_name) :
|
2021-08-18 14:03:52 +00:00
|
|
|
name(p_name) {}
|
|
|
|
};
|
|
|
|
|
2022-10-22 17:42:48 +00:00
|
|
|
MethodDefinition D_METHOD(StringName p_name);
|
|
|
|
MethodDefinition D_METHOD(StringName p_name, StringName p_arg1);
|
2021-08-18 14:03:52 +00:00
|
|
|
template <typename... Args>
|
2022-10-22 17:42:48 +00:00
|
|
|
MethodDefinition D_METHOD(StringName p_name, StringName p_arg1, Args... args) {
|
2021-08-18 14:03:52 +00:00
|
|
|
MethodDefinition md = D_METHOD(p_name, args...);
|
|
|
|
md.args.push_front(p_arg1);
|
|
|
|
return md;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ClassDB {
|
2022-12-07 13:11:44 +00:00
|
|
|
static GDExtensionInitializationLevel current_level;
|
2021-09-10 00:47:45 +00:00
|
|
|
|
|
|
|
friend class godot::GDExtensionBinding;
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
public:
|
|
|
|
struct PropertySetGet {
|
|
|
|
int index;
|
2022-10-22 17:42:48 +00:00
|
|
|
StringName setter;
|
|
|
|
StringName getter;
|
2021-08-18 14:03:52 +00:00
|
|
|
MethodBind *_setptr;
|
|
|
|
MethodBind *_getptr;
|
|
|
|
Variant::Type type;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ClassInfo {
|
2022-10-22 17:42:48 +00:00
|
|
|
StringName name;
|
|
|
|
StringName parent_name;
|
2022-12-07 13:11:44 +00:00
|
|
|
GDExtensionInitializationLevel level = GDEXTENSION_INITIALIZATION_SCENE;
|
2022-10-22 17:42:48 +00:00
|
|
|
std::unordered_map<StringName, MethodBind *> method_map;
|
|
|
|
std::set<StringName> signal_names;
|
2022-12-07 13:11:44 +00:00
|
|
|
std::unordered_map<StringName, GDExtensionClassCallVirtual> virtual_methods;
|
2022-10-22 17:42:48 +00:00
|
|
|
std::set<StringName> property_names;
|
|
|
|
std::set<StringName> constant_names;
|
2022-09-19 23:29:39 +00:00
|
|
|
// Pointer to the parent custom class, if any. Will be null if the parent class is a Godot class.
|
2021-08-18 14:03:52 +00:00
|
|
|
ClassInfo *parent_ptr = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2022-09-19 23:29:39 +00:00
|
|
|
// This may only contain custom classes, not Godot classes
|
2022-10-22 17:42:48 +00:00
|
|
|
static std::unordered_map<StringName, ClassInfo> classes;
|
2023-02-17 16:49:09 +00:00
|
|
|
static std::unordered_map<StringName, const GDExtensionInstanceBindingCallbacks *> instance_binding_callbacks;
|
2023-02-16 14:43:26 +00:00
|
|
|
// Used to remember the custom class registration order.
|
|
|
|
static std::vector<StringName> class_register_order;
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const void **p_defs, int p_defcount);
|
2021-09-26 06:15:01 +00:00
|
|
|
static void initialize_class(const ClassInfo &cl);
|
2022-10-22 17:42:48 +00:00
|
|
|
static void bind_method_godot(const StringName &p_class_name, MethodBind *p_method);
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2022-10-15 10:23:36 +00:00
|
|
|
template <class T, bool is_abstract>
|
|
|
|
static void _register_class(bool p_virtual = false);
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
public:
|
|
|
|
template <class T>
|
2022-10-15 10:23:36 +00:00
|
|
|
static void register_class(bool p_virtual = false);
|
|
|
|
template <class T>
|
|
|
|
static void register_abstract_class();
|
2023-10-12 03:59:59 +00:00
|
|
|
|
|
|
|
_FORCE_INLINE_ static void _register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
|
|
|
|
instance_binding_callbacks[p_name] = p_callbacks;
|
|
|
|
}
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2022-05-04 11:27:08 +00:00
|
|
|
template <class N, class M, typename... VarArgs>
|
|
|
|
static MethodBind *bind_method(N p_method_name, M p_method, VarArgs... p_args);
|
|
|
|
|
|
|
|
template <class N, class M, typename... VarArgs>
|
2022-10-22 17:42:48 +00:00
|
|
|
static MethodBind *bind_static_method(StringName p_class, N p_method_name, M p_method, VarArgs... p_args);
|
2022-05-04 11:27:08 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
template <class M>
|
2022-10-22 17:42:48 +00:00
|
|
|
static MethodBind *bind_vararg_method(uint32_t p_flags, StringName p_name, M p_method, const MethodInfo &p_info = MethodInfo(), const std::vector<Variant> &p_default_args = std::vector<Variant>{}, bool p_return_nil_is_variant = true);
|
2022-05-04 11:27:08 +00:00
|
|
|
|
2022-10-22 17:42:48 +00:00
|
|
|
static void add_property_group(const StringName &p_class, const String &p_name, const String &p_prefix);
|
|
|
|
static void add_property_subgroup(const StringName &p_class, const String &p_name, const String &p_prefix);
|
|
|
|
static void add_property(const StringName &p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index = -1);
|
|
|
|
static void add_signal(const StringName &p_class, const MethodInfo &p_signal);
|
2022-12-07 13:11:44 +00:00
|
|
|
static void bind_integer_constant(const StringName &p_class_name, const StringName &p_enum_name, const StringName &p_constant_name, GDExtensionInt p_constant_value, bool p_is_bitfield = false);
|
|
|
|
static void bind_virtual_method(const StringName &p_class, const StringName &p_method, GDExtensionClassCallVirtual p_call);
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2022-10-22 17:42:48 +00:00
|
|
|
static MethodBind *get_method(const StringName &p_class, const StringName &p_method);
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2022-12-07 13:11:44 +00:00
|
|
|
static GDExtensionClassCallVirtual get_virtual_func(void *p_userdata, GDExtensionConstStringNamePtr p_name);
|
2023-02-17 16:49:09 +00:00
|
|
|
static const GDExtensionInstanceBindingCallbacks *get_instance_binding_callbacks(const StringName &p_class);
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2022-12-07 13:11:44 +00:00
|
|
|
static void initialize(GDExtensionInitializationLevel p_level);
|
|
|
|
static void deinitialize(GDExtensionInitializationLevel p_level);
|
2023-07-07 02:16:27 +00:00
|
|
|
|
|
|
|
CLASSDB_SINGLETON_FORWARD_METHODS;
|
2021-08-18 14:03:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define BIND_CONSTANT(m_constant) \
|
2024-03-08 19:42:07 +00:00
|
|
|
::godot::ClassDB::bind_integer_constant(get_class_static(), "", #m_constant, m_constant);
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
#define BIND_ENUM_CONSTANT(m_constant) \
|
2024-03-08 19:42:07 +00:00
|
|
|
::godot::ClassDB::bind_integer_constant(get_class_static(), ::godot::_gde_constant_get_enum_name(m_constant, #m_constant), #m_constant, m_constant);
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2022-07-11 11:37:14 +00:00
|
|
|
#define BIND_BITFIELD_FLAG(m_constant) \
|
2024-03-08 19:42:07 +00:00
|
|
|
::godot::ClassDB::bind_integer_constant(get_class_static(), ::godot::_gde_constant_get_bitfield_name(m_constant, #m_constant), #m_constant, m_constant, true);
|
2023-02-16 15:38:38 +00:00
|
|
|
|
|
|
|
#define BIND_VIRTUAL_METHOD(m_class, m_method) \
|
|
|
|
{ \
|
|
|
|
auto _call##m_method = [](GDExtensionObjectPtr p_instance, const GDExtensionConstTypePtr *p_args, GDExtensionTypePtr p_ret) -> void { \
|
|
|
|
call_with_ptr_args(reinterpret_cast<m_class *>(p_instance), &m_class::m_method, p_args, p_ret); \
|
|
|
|
}; \
|
2024-03-08 19:42:07 +00:00
|
|
|
::godot::ClassDB::bind_virtual_method(m_class::get_class_static(), #m_method, _call##m_method); \
|
2021-08-18 14:03:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 10:23:36 +00:00
|
|
|
template <class T, bool is_abstract>
|
|
|
|
void ClassDB::_register_class(bool p_virtual) {
|
2023-10-21 22:54:46 +00:00
|
|
|
static_assert(TypesAreSame<typename T::self_type, T>::value, "Class not declared properly, please use GDCLASS.");
|
2023-02-16 15:38:38 +00:00
|
|
|
instance_binding_callbacks[T::get_class_static()] = &T::_gde_binding_callbacks;
|
2023-02-17 16:49:09 +00:00
|
|
|
|
2021-09-26 06:15:01 +00:00
|
|
|
// Register this class within our plugin
|
2021-08-18 14:03:52 +00:00
|
|
|
ClassInfo cl;
|
|
|
|
cl.name = T::get_class_static();
|
|
|
|
cl.parent_name = T::get_parent_class_static();
|
2021-09-10 00:47:45 +00:00
|
|
|
cl.level = current_level;
|
2022-10-22 17:42:48 +00:00
|
|
|
std::unordered_map<StringName, ClassInfo>::iterator parent_it = classes.find(cl.parent_name);
|
2022-09-19 23:29:39 +00:00
|
|
|
if (parent_it != classes.end()) {
|
|
|
|
// Assign parent if it is also a custom class
|
|
|
|
cl.parent_ptr = &parent_it->second;
|
2021-08-18 14:03:52 +00:00
|
|
|
}
|
2022-09-19 23:29:39 +00:00
|
|
|
classes[cl.name] = cl;
|
2023-02-16 14:43:26 +00:00
|
|
|
class_register_order.push_back(cl.name);
|
2021-09-26 06:15:01 +00:00
|
|
|
|
|
|
|
// Register this class with Godot
|
2022-12-07 13:11:44 +00:00
|
|
|
GDExtensionClassCreationInfo class_info = {
|
|
|
|
p_virtual, // GDExtensionBool is_virtual;
|
|
|
|
is_abstract, // GDExtensionBool is_abstract;
|
|
|
|
T::set_bind, // GDExtensionClassSet set_func;
|
|
|
|
T::get_bind, // GDExtensionClassGet get_func;
|
|
|
|
T::get_property_list_bind, // GDExtensionClassGetPropertyList get_property_list_func;
|
|
|
|
T::free_property_list_bind, // GDExtensionClassFreePropertyList free_property_list_func;
|
|
|
|
T::property_can_revert_bind, // GDExtensionClassPropertyCanRevert property_can_revert_func;
|
|
|
|
T::property_get_revert_bind, // GDExtensionClassPropertyGetRevert property_get_revert_func;
|
|
|
|
T::notification_bind, // GDExtensionClassNotification notification_func;
|
|
|
|
T::to_string_bind, // GDExtensionClassToString to_string_func;
|
|
|
|
nullptr, // GDExtensionClassReference reference_func;
|
|
|
|
nullptr, // GDExtensionClassUnreference unreference_func;
|
|
|
|
T::create, // GDExtensionClassCreateInstance create_instance_func; /* this one is mandatory */
|
|
|
|
T::free, // GDExtensionClassFreeInstance free_instance_func; /* this one is mandatory */
|
|
|
|
&ClassDB::get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func;
|
|
|
|
nullptr, // GDExtensionClassGetRID get_rid;
|
2022-10-23 20:31:57 +00:00
|
|
|
(void *)&T::get_class_static(), // void *class_userdata;
|
2021-09-26 06:15:01 +00:00
|
|
|
};
|
|
|
|
|
2023-04-24 16:45:45 +00:00
|
|
|
internal::gdextension_interface_classdb_register_extension_class(internal::library, cl.name._native_ptr(), cl.parent_name._native_ptr(), &class_info);
|
2021-09-26 06:15:01 +00:00
|
|
|
|
|
|
|
// call bind_methods etc. to register all members of the class
|
2021-08-18 14:03:52 +00:00
|
|
|
T::initialize_class();
|
2021-09-26 06:15:01 +00:00
|
|
|
|
|
|
|
// now register our class within ClassDB within Godot
|
|
|
|
initialize_class(classes[cl.name]);
|
2021-08-18 14:03:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 10:23:36 +00:00
|
|
|
template <class T>
|
|
|
|
void ClassDB::register_class(bool p_virtual) {
|
|
|
|
ClassDB::_register_class<T, false>(p_virtual);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void ClassDB::register_abstract_class() {
|
|
|
|
ClassDB::_register_class<T, true>();
|
|
|
|
}
|
|
|
|
|
2022-05-04 11:27:08 +00:00
|
|
|
template <class N, class M, typename... VarArgs>
|
|
|
|
MethodBind *ClassDB::bind_method(N p_method_name, M p_method, VarArgs... p_args) {
|
|
|
|
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
|
|
|
|
const Variant *argptrs[sizeof...(p_args) + 1];
|
|
|
|
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
|
|
|
|
argptrs[i] = &args[i];
|
|
|
|
}
|
2021-08-18 14:03:52 +00:00
|
|
|
MethodBind *bind = create_method_bind(p_method);
|
2022-05-04 11:27:08 +00:00
|
|
|
return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const void **)argptrs, sizeof...(p_args));
|
|
|
|
}
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2022-05-04 11:27:08 +00:00
|
|
|
template <class N, class M, typename... VarArgs>
|
2022-10-22 17:42:48 +00:00
|
|
|
MethodBind *ClassDB::bind_static_method(StringName p_class, N p_method_name, M p_method, VarArgs... p_args) {
|
2022-05-04 11:27:08 +00:00
|
|
|
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
|
|
|
|
const Variant *argptrs[sizeof...(p_args) + 1];
|
|
|
|
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
|
|
|
|
argptrs[i] = &args[i];
|
|
|
|
}
|
|
|
|
MethodBind *bind = create_static_method_bind(p_method);
|
|
|
|
bind->set_instance_class(p_class);
|
|
|
|
return bind_methodfi(0, bind, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const void **)argptrs, sizeof...(p_args));
|
2021-08-18 14:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class M>
|
2022-10-22 17:42:48 +00:00
|
|
|
MethodBind *ClassDB::bind_vararg_method(uint32_t p_flags, StringName p_name, M p_method, const MethodInfo &p_info, const std::vector<Variant> &p_default_args, bool p_return_nil_is_variant) {
|
2021-08-18 14:03:52 +00:00
|
|
|
MethodBind *bind = create_vararg_method_bind(p_method, p_info, p_return_nil_is_variant);
|
2023-09-12 13:33:49 +00:00
|
|
|
ERR_FAIL_NULL_V(bind, nullptr);
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
bind->set_name(p_name);
|
|
|
|
bind->set_default_arguments(p_default_args);
|
|
|
|
|
2022-10-22 17:42:48 +00:00
|
|
|
StringName instance_type = bind->get_instance_class();
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2022-10-22 17:42:48 +00:00
|
|
|
std::unordered_map<StringName, ClassInfo>::iterator type_it = classes.find(instance_type);
|
2021-08-18 14:03:52 +00:00
|
|
|
if (type_it == classes.end()) {
|
|
|
|
memdelete(bind);
|
2022-10-23 20:31:57 +00:00
|
|
|
ERR_FAIL_V_MSG(nullptr, String("Class '{0}' doesn't exist.").format(Array::make(instance_type)));
|
2021-08-18 14:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ClassInfo &type = type_it->second;
|
|
|
|
|
|
|
|
if (type.method_map.find(p_name) != type.method_map.end()) {
|
|
|
|
memdelete(bind);
|
2022-09-30 06:40:50 +00:00
|
|
|
ERR_FAIL_V_MSG(nullptr, String("Binding duplicate method: {0}::{1}.").format(Array::make(instance_type, p_method)));
|
2021-08-18 14:03:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 06:15:01 +00:00
|
|
|
// register our method bind within our plugin
|
2021-08-18 14:03:52 +00:00
|
|
|
type.method_map[p_name] = bind;
|
2021-09-26 06:15:01 +00:00
|
|
|
|
|
|
|
// and register with godot
|
|
|
|
bind_method_godot(type.name, bind);
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
return bind;
|
|
|
|
}
|
2022-02-15 10:58:28 +00:00
|
|
|
|
|
|
|
#define GDREGISTER_CLASS(m_class) ClassDB::register_class<m_class>();
|
2022-10-15 10:23:36 +00:00
|
|
|
#define GDREGISTER_VIRTUAL_CLASS(m_class) ClassDB::register_class<m_class>(true);
|
|
|
|
#define GDREGISTER_ABSTRACT_CLASS(m_class) ClassDB::register_abstract_class<m_class>();
|
2022-02-15 10:58:28 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
} // namespace godot
|
|
|
|
|
2022-10-09 06:47:07 +00:00
|
|
|
#endif // GODOT_CLASS_DB_HPP
|