class_db.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**************************************************************************/
  2. /* class_db.hpp */
  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 GODOT_CLASS_DB_HPP
  31. #define GODOT_CLASS_DB_HPP
  32. #include <gdextension_interface.h>
  33. #include <godot_cpp/core/defs.hpp>
  34. #include <godot_cpp/core/error_macros.hpp>
  35. #include <godot_cpp/core/method_bind.hpp>
  36. #include <godot_cpp/core/object.hpp>
  37. #include <godot_cpp/classes/class_db_singleton.hpp>
  38. #include <list>
  39. #include <set>
  40. #include <string>
  41. #include <unordered_map>
  42. #include <vector>
  43. // Needed to use StringName as key in `std::unordered_map`
  44. template <>
  45. struct std::hash<godot::StringName> {
  46. std::size_t operator()(godot::StringName const &s) const noexcept {
  47. return s.hash();
  48. }
  49. };
  50. namespace godot {
  51. #define DEFVAL(m_defval) (m_defval)
  52. struct MethodDefinition {
  53. StringName name;
  54. std::list<StringName> args;
  55. MethodDefinition() {}
  56. MethodDefinition(StringName p_name) :
  57. name(p_name) {}
  58. };
  59. MethodDefinition D_METHOD(StringName p_name);
  60. MethodDefinition D_METHOD(StringName p_name, StringName p_arg1);
  61. template <typename... Args>
  62. MethodDefinition D_METHOD(StringName p_name, StringName p_arg1, Args... args) {
  63. MethodDefinition md = D_METHOD(p_name, args...);
  64. md.args.push_front(p_arg1);
  65. return md;
  66. }
  67. class ClassDB {
  68. static GDExtensionInitializationLevel current_level;
  69. friend class godot::GDExtensionBinding;
  70. public:
  71. struct PropertySetGet {
  72. int index;
  73. StringName setter;
  74. StringName getter;
  75. MethodBind *_setptr;
  76. MethodBind *_getptr;
  77. Variant::Type type;
  78. };
  79. struct ClassInfo {
  80. StringName name;
  81. StringName parent_name;
  82. GDExtensionInitializationLevel level = GDEXTENSION_INITIALIZATION_SCENE;
  83. std::unordered_map<StringName, MethodBind *> method_map;
  84. std::set<StringName> signal_names;
  85. std::unordered_map<StringName, GDExtensionClassCallVirtual> virtual_methods;
  86. std::set<StringName> property_names;
  87. std::set<StringName> constant_names;
  88. // Pointer to the parent custom class, if any. Will be null if the parent class is a Godot class.
  89. ClassInfo *parent_ptr = nullptr;
  90. };
  91. private:
  92. // This may only contain custom classes, not Godot classes
  93. static std::unordered_map<StringName, ClassInfo> classes;
  94. static std::unordered_map<StringName, const GDExtensionInstanceBindingCallbacks *> instance_binding_callbacks;
  95. // Used to remember the custom class registration order.
  96. static std::vector<StringName> class_register_order;
  97. static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const void **p_defs, int p_defcount);
  98. static void initialize_class(const ClassInfo &cl);
  99. static void bind_method_godot(const StringName &p_class_name, MethodBind *p_method);
  100. template <class T, bool is_abstract>
  101. static void _register_class(bool p_virtual = false);
  102. public:
  103. template <class T>
  104. static void register_class(bool p_virtual = false);
  105. template <class T>
  106. static void register_abstract_class();
  107. template <class T>
  108. static void register_engine_class();
  109. template <class N, class M, typename... VarArgs>
  110. static MethodBind *bind_method(N p_method_name, M p_method, VarArgs... p_args);
  111. template <class N, class M, typename... VarArgs>
  112. static MethodBind *bind_static_method(StringName p_class, N p_method_name, M p_method, VarArgs... p_args);
  113. template <class M>
  114. 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);
  115. static void add_property_group(const StringName &p_class, const String &p_name, const String &p_prefix);
  116. static void add_property_subgroup(const StringName &p_class, const String &p_name, const String &p_prefix);
  117. static void add_property(const StringName &p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index = -1);
  118. static void add_signal(const StringName &p_class, const MethodInfo &p_signal);
  119. 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);
  120. static void bind_virtual_method(const StringName &p_class, const StringName &p_method, GDExtensionClassCallVirtual p_call);
  121. static MethodBind *get_method(const StringName &p_class, const StringName &p_method);
  122. static GDExtensionClassCallVirtual get_virtual_func(void *p_userdata, GDExtensionConstStringNamePtr p_name);
  123. static const GDExtensionInstanceBindingCallbacks *get_instance_binding_callbacks(const StringName &p_class);
  124. static void initialize(GDExtensionInitializationLevel p_level);
  125. static void deinitialize(GDExtensionInitializationLevel p_level);
  126. CLASSDB_SINGLETON_FORWARD_METHODS;
  127. };
  128. #define BIND_CONSTANT(m_constant) \
  129. godot::ClassDB::bind_integer_constant(get_class_static(), "", #m_constant, m_constant);
  130. #define BIND_ENUM_CONSTANT(m_constant) \
  131. godot::ClassDB::bind_integer_constant(get_class_static(), godot::_gde_constant_get_enum_name(m_constant, #m_constant), #m_constant, m_constant);
  132. #define BIND_BITFIELD_FLAG(m_constant) \
  133. godot::ClassDB::bind_integer_constant(get_class_static(), godot::_gde_constant_get_bitfield_name(m_constant, #m_constant), #m_constant, m_constant, true);
  134. #define BIND_VIRTUAL_METHOD(m_class, m_method) \
  135. { \
  136. auto _call##m_method = [](GDExtensionObjectPtr p_instance, const GDExtensionConstTypePtr *p_args, GDExtensionTypePtr p_ret) -> void { \
  137. call_with_ptr_args(reinterpret_cast<m_class *>(p_instance), &m_class::m_method, p_args, p_ret); \
  138. }; \
  139. godot::ClassDB::bind_virtual_method(m_class::get_class_static(), #m_method, _call##m_method); \
  140. }
  141. template <class T, bool is_abstract>
  142. void ClassDB::_register_class(bool p_virtual) {
  143. instance_binding_callbacks[T::get_class_static()] = &T::_gde_binding_callbacks;
  144. // Register this class within our plugin
  145. ClassInfo cl;
  146. cl.name = T::get_class_static();
  147. cl.parent_name = T::get_parent_class_static();
  148. cl.level = current_level;
  149. std::unordered_map<StringName, ClassInfo>::iterator parent_it = classes.find(cl.parent_name);
  150. if (parent_it != classes.end()) {
  151. // Assign parent if it is also a custom class
  152. cl.parent_ptr = &parent_it->second;
  153. }
  154. classes[cl.name] = cl;
  155. class_register_order.push_back(cl.name);
  156. // Register this class with Godot
  157. GDExtensionClassCreationInfo class_info = {
  158. p_virtual, // GDExtensionBool is_virtual;
  159. is_abstract, // GDExtensionBool is_abstract;
  160. T::set_bind, // GDExtensionClassSet set_func;
  161. T::get_bind, // GDExtensionClassGet get_func;
  162. T::get_property_list_bind, // GDExtensionClassGetPropertyList get_property_list_func;
  163. T::free_property_list_bind, // GDExtensionClassFreePropertyList free_property_list_func;
  164. T::property_can_revert_bind, // GDExtensionClassPropertyCanRevert property_can_revert_func;
  165. T::property_get_revert_bind, // GDExtensionClassPropertyGetRevert property_get_revert_func;
  166. T::notification_bind, // GDExtensionClassNotification notification_func;
  167. T::to_string_bind, // GDExtensionClassToString to_string_func;
  168. nullptr, // GDExtensionClassReference reference_func;
  169. nullptr, // GDExtensionClassUnreference unreference_func;
  170. T::create, // GDExtensionClassCreateInstance create_instance_func; /* this one is mandatory */
  171. T::free, // GDExtensionClassFreeInstance free_instance_func; /* this one is mandatory */
  172. &ClassDB::get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func;
  173. nullptr, // GDExtensionClassGetRID get_rid;
  174. (void *)&T::get_class_static(), // void *class_userdata;
  175. };
  176. internal::gdextension_interface_classdb_register_extension_class(internal::library, cl.name._native_ptr(), cl.parent_name._native_ptr(), &class_info);
  177. // call bind_methods etc. to register all members of the class
  178. T::initialize_class();
  179. // now register our class within ClassDB within Godot
  180. initialize_class(classes[cl.name]);
  181. }
  182. template <class T>
  183. void ClassDB::register_class(bool p_virtual) {
  184. ClassDB::_register_class<T, false>(p_virtual);
  185. }
  186. template <class T>
  187. void ClassDB::register_abstract_class() {
  188. ClassDB::_register_class<T, true>();
  189. }
  190. template <class T>
  191. void ClassDB::register_engine_class() {
  192. instance_binding_callbacks[T::get_class_static()] = &T::_gde_binding_callbacks;
  193. }
  194. template <class N, class M, typename... VarArgs>
  195. MethodBind *ClassDB::bind_method(N p_method_name, M p_method, VarArgs... p_args) {
  196. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  197. const Variant *argptrs[sizeof...(p_args) + 1];
  198. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  199. argptrs[i] = &args[i];
  200. }
  201. MethodBind *bind = create_method_bind(p_method);
  202. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const void **)argptrs, sizeof...(p_args));
  203. }
  204. template <class N, class M, typename... VarArgs>
  205. MethodBind *ClassDB::bind_static_method(StringName p_class, N p_method_name, M p_method, VarArgs... p_args) {
  206. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  207. const Variant *argptrs[sizeof...(p_args) + 1];
  208. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  209. argptrs[i] = &args[i];
  210. }
  211. MethodBind *bind = create_static_method_bind(p_method);
  212. bind->set_instance_class(p_class);
  213. return bind_methodfi(0, bind, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const void **)argptrs, sizeof...(p_args));
  214. }
  215. template <class M>
  216. 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) {
  217. MethodBind *bind = create_vararg_method_bind(p_method, p_info, p_return_nil_is_variant);
  218. ERR_FAIL_COND_V(!bind, nullptr);
  219. bind->set_name(p_name);
  220. bind->set_default_arguments(p_default_args);
  221. StringName instance_type = bind->get_instance_class();
  222. std::unordered_map<StringName, ClassInfo>::iterator type_it = classes.find(instance_type);
  223. if (type_it == classes.end()) {
  224. memdelete(bind);
  225. ERR_FAIL_V_MSG(nullptr, String("Class '{0}' doesn't exist.").format(Array::make(instance_type)));
  226. }
  227. ClassInfo &type = type_it->second;
  228. if (type.method_map.find(p_name) != type.method_map.end()) {
  229. memdelete(bind);
  230. ERR_FAIL_V_MSG(nullptr, String("Binding duplicate method: {0}::{1}.").format(Array::make(instance_type, p_method)));
  231. }
  232. // register our method bind within our plugin
  233. type.method_map[p_name] = bind;
  234. // and register with godot
  235. bind_method_godot(type.name, bind);
  236. return bind;
  237. }
  238. #define GDREGISTER_CLASS(m_class) ClassDB::register_class<m_class>();
  239. #define GDREGISTER_VIRTUAL_CLASS(m_class) ClassDB::register_class<m_class>(true);
  240. #define GDREGISTER_ABSTRACT_CLASS(m_class) ClassDB::register_abstract_class<m_class>();
  241. } // namespace godot
  242. #endif // GODOT_CLASS_DB_HPP