class_db.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*************************************************************************/
  2. /* class_db.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 CLASS_DB_HPP
  31. #define CLASS_DB_HPP
  32. #include <godot/gdnative_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 <list>
  38. #include <set>
  39. #include <string>
  40. #include <unordered_map>
  41. #include <vector>
  42. namespace godot {
  43. #define DEFVAL(m_defval) (m_defval)
  44. struct MethodDefinition {
  45. const char *name = nullptr;
  46. std::list<std::string> args;
  47. MethodDefinition() {}
  48. MethodDefinition(const char *p_name) :
  49. name(p_name) {}
  50. };
  51. MethodDefinition D_METHOD(const char *p_name);
  52. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1);
  53. template <typename... Args>
  54. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, Args... args) {
  55. MethodDefinition md = D_METHOD(p_name, args...);
  56. md.args.push_front(p_arg1);
  57. return md;
  58. }
  59. class ClassDB {
  60. static GDNativeInitializationLevel current_level;
  61. friend class godot::GDExtensionBinding;
  62. public:
  63. struct PropertySetGet {
  64. int index;
  65. const char *setter;
  66. const char *getter;
  67. MethodBind *_setptr;
  68. MethodBind *_getptr;
  69. Variant::Type type;
  70. };
  71. struct ClassInfo {
  72. const char *name = nullptr;
  73. const char *parent_name = nullptr;
  74. GDNativeInitializationLevel level = GDNATIVE_INITIALIZATION_SCENE;
  75. std::unordered_map<std::string, MethodBind *> method_map;
  76. std::set<std::string> signal_names;
  77. std::unordered_map<std::string, GDNativeExtensionClassCallVirtual> virtual_methods;
  78. std::set<std::string> property_names;
  79. std::set<std::string> constant_names;
  80. ClassInfo *parent_ptr = nullptr;
  81. };
  82. private:
  83. static std::unordered_map<std::string, ClassInfo> classes;
  84. static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const void **p_defs, int p_defcount);
  85. static void initialize_class(const ClassInfo &cl);
  86. static void bind_method_godot(const char *p_class_name, MethodBind *p_method);
  87. static _FORCE_INLINE_ char *_alloc_and_copy_cstr(const char *p_str) {
  88. size_t size = strlen(p_str) + 1;
  89. char *ret = reinterpret_cast<char *>(memalloc(size));
  90. memcpy(ret, p_str, size);
  91. return ret;
  92. }
  93. public:
  94. template <class T>
  95. static void register_class();
  96. template <class N, class M, typename... VarArgs>
  97. static MethodBind *bind_method(N p_method_name, M p_method, VarArgs... p_args);
  98. template <class N, class M, typename... VarArgs>
  99. static MethodBind *bind_static_method(const char *p_class, N p_method_name, M p_method, VarArgs... p_args);
  100. template <class M>
  101. static MethodBind *bind_vararg_method(uint32_t p_flags, const char *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);
  102. static void add_property_group(const char *p_class, const char *p_name, const char *p_prefix);
  103. static void add_property_subgroup(const char *p_class, const char *p_name, const char *p_prefix);
  104. static void add_property(const char *p_class, const PropertyInfo &p_pinfo, const char *p_setter, const char *p_getter, int p_index = -1);
  105. static void add_signal(const char *p_class, const MethodInfo &p_signal);
  106. static void bind_integer_constant(const char *p_class_name, const char *p_enum_name, const char *p_constant_name, GDNativeInt p_constant_value, bool p_is_bitfield = false);
  107. static void bind_virtual_method(const char *p_class, const char *p_method, GDNativeExtensionClassCallVirtual p_call);
  108. static MethodBind *get_method(const char *p_class, const char *p_method);
  109. static GDNativeExtensionClassCallVirtual get_virtual_func(void *p_userdata, const char *p_name);
  110. static void initialize(GDNativeInitializationLevel p_level);
  111. static void deinitialize(GDNativeInitializationLevel p_level);
  112. };
  113. #define BIND_CONSTANT(m_constant) \
  114. godot::ClassDB::bind_integer_constant(get_class_static(), "", #m_constant, m_constant);
  115. #define BIND_ENUM_CONSTANT(m_constant) \
  116. godot::ClassDB::bind_integer_constant(get_class_static(), godot::__constant_get_enum_name(m_constant, #m_constant), #m_constant, m_constant);
  117. #define BIND_BITFIELD_FLAG(m_constant) \
  118. godot::ClassDB::bind_integer_constant(get_class_static(), godot::__constant_get_bitfield_name(m_constant, #m_constant), #m_constant, m_constant, true);
  119. #define BIND_VIRTUAL_METHOD(m_class, m_method) \
  120. { \
  121. auto ___call##m_method = [](GDNativeObjectPtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr p_ret) -> void { \
  122. call_with_ptr_args(reinterpret_cast<m_class *>(p_instance), &m_class::m_method, p_args, p_ret); \
  123. }; \
  124. godot::ClassDB::bind_virtual_method(m_class::get_class_static(), #m_method, ___call##m_method); \
  125. }
  126. template <class T>
  127. void ClassDB::register_class() {
  128. // Register this class within our plugin
  129. ClassInfo cl;
  130. cl.name = T::get_class_static();
  131. cl.parent_name = T::get_parent_class_static();
  132. cl.level = current_level;
  133. classes[cl.name] = cl;
  134. if (classes.find(cl.parent_name) != classes.end()) {
  135. cl.parent_ptr = &classes[cl.parent_name];
  136. }
  137. // Register this class with Godot
  138. GDNativeExtensionClassCreationInfo class_info = {
  139. T::set_bind, // GDNativeExtensionClassSet set_func;
  140. T::get_bind, // GDNativeExtensionClassGet get_func;
  141. T::get_property_list_bind, // GDNativeExtensionClassGetPropertyList get_property_list_func;
  142. T::free_property_list_bind, // GDNativeExtensionClassFreePropertyList free_property_list_func;
  143. T::property_can_revert_bind, // GDNativeExtensionClassPropertyCanRevert property_can_revert_func;
  144. T::property_get_revert_bind, // GDNativeExtensionClassPropertyGetRevert property_get_revert_func;
  145. T::notification_bind, // GDNativeExtensionClassNotification notification_func;
  146. T::to_string_bind, // GDNativeExtensionClassToString to_string_func;
  147. nullptr, // GDNativeExtensionClassReference reference_func;
  148. nullptr, // GDNativeExtensionClassUnreference unreference_func;
  149. T::create, // GDNativeExtensionClassCreateInstance create_instance_func; /* this one is mandatory */
  150. T::free, // GDNativeExtensionClassFreeInstance free_instance_func; /* this one is mandatory */
  151. &ClassDB::get_virtual_func, // GDNativeExtensionClassGetVirtual get_virtual_func;
  152. nullptr, // GDNativeExtensionClassGetRID get_rid;
  153. (void *)cl.name, // void *class_userdata;
  154. };
  155. internal::gdn_interface->classdb_register_extension_class(internal::library, cl.name, cl.parent_name, &class_info);
  156. // call bind_methods etc. to register all members of the class
  157. T::initialize_class();
  158. // now register our class within ClassDB within Godot
  159. initialize_class(classes[cl.name]);
  160. }
  161. template <class N, class M, typename... VarArgs>
  162. MethodBind *ClassDB::bind_method(N p_method_name, M p_method, VarArgs... p_args) {
  163. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  164. const Variant *argptrs[sizeof...(p_args) + 1];
  165. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  166. argptrs[i] = &args[i];
  167. }
  168. MethodBind *bind = create_method_bind(p_method);
  169. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const void **)argptrs, sizeof...(p_args));
  170. }
  171. template <class N, class M, typename... VarArgs>
  172. MethodBind *ClassDB::bind_static_method(const char *p_class, N p_method_name, M p_method, VarArgs... p_args) {
  173. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  174. const Variant *argptrs[sizeof...(p_args) + 1];
  175. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  176. argptrs[i] = &args[i];
  177. }
  178. MethodBind *bind = create_static_method_bind(p_method);
  179. bind->set_instance_class(p_class);
  180. return bind_methodfi(0, bind, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const void **)argptrs, sizeof...(p_args));
  181. }
  182. template <class M>
  183. MethodBind *ClassDB::bind_vararg_method(uint32_t p_flags, const char *p_name, M p_method, const MethodInfo &p_info, const std::vector<Variant> &p_default_args, bool p_return_nil_is_variant) {
  184. MethodBind *bind = create_vararg_method_bind(p_method, p_info, p_return_nil_is_variant);
  185. ERR_FAIL_COND_V(!bind, nullptr);
  186. bind->set_name(p_name);
  187. bind->set_default_arguments(p_default_args);
  188. const char *instance_type = bind->get_instance_class();
  189. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(instance_type);
  190. if (type_it == classes.end()) {
  191. memdelete(bind);
  192. ERR_FAIL_V_MSG(nullptr, "Class doesn't exist.");
  193. }
  194. ClassInfo &type = type_it->second;
  195. if (type.method_map.find(p_name) != type.method_map.end()) {
  196. memdelete(bind);
  197. ERR_FAIL_V_MSG(nullptr, "Binding duplicate method.");
  198. }
  199. // register our method bind within our plugin
  200. type.method_map[p_name] = bind;
  201. // and register with godot
  202. bind_method_godot(type.name, bind);
  203. return bind;
  204. }
  205. #define GDREGISTER_CLASS(m_class) ClassDB::register_class<m_class>();
  206. } // namespace godot
  207. #endif // ! CLASS_DB_HPP