class_db.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*************************************************************************/
  2. /* class_db.h */
  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_H
  31. #define CLASS_DB_H
  32. #include "core/object/method_bind.h"
  33. #include "core/object/object.h"
  34. #include "core/string/print_string.h"
  35. // Makes callable_mp readily available in all classes connecting signals.
  36. // Needs to come after method_bind and object have been included.
  37. #include "core/object/callable_method_pointer.h"
  38. #include "core/templates/hash_set.h"
  39. #define DEFVAL(m_defval) (m_defval)
  40. #ifdef DEBUG_METHODS_ENABLED
  41. struct MethodDefinition {
  42. StringName name;
  43. Vector<StringName> args;
  44. MethodDefinition() {}
  45. MethodDefinition(const char *p_name) :
  46. name(p_name) {}
  47. MethodDefinition(const StringName &p_name) :
  48. name(p_name) {}
  49. };
  50. MethodDefinition D_METHODP(const char *p_name, const char *const **p_args, uint32_t p_argcount);
  51. template <typename... VarArgs>
  52. MethodDefinition D_METHOD(const char *p_name, const VarArgs... p_args) {
  53. const char *args[sizeof...(p_args) + 1] = { p_args..., nullptr }; // +1 makes sure zero sized arrays are also supported.
  54. const char *const *argptrs[sizeof...(p_args) + 1];
  55. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  56. argptrs[i] = &args[i];
  57. }
  58. return D_METHODP(p_name, sizeof...(p_args) == 0 ? nullptr : (const char *const **)argptrs, sizeof...(p_args));
  59. }
  60. #else
  61. // When DEBUG_METHODS_ENABLED is set this will let the engine know
  62. // the argument names for easier debugging.
  63. #define D_METHOD(m_c, ...) m_c
  64. #endif
  65. class ClassDB {
  66. public:
  67. enum APIType {
  68. API_CORE,
  69. API_EDITOR,
  70. API_EXTENSION,
  71. API_EDITOR_EXTENSION,
  72. API_NONE
  73. };
  74. public:
  75. struct PropertySetGet {
  76. int index;
  77. StringName setter;
  78. StringName getter;
  79. MethodBind *_setptr = nullptr;
  80. MethodBind *_getptr = nullptr;
  81. Variant::Type type;
  82. };
  83. struct ClassInfo {
  84. APIType api = API_NONE;
  85. ClassInfo *inherits_ptr = nullptr;
  86. void *class_ptr = nullptr;
  87. ObjectNativeExtension *native_extension = nullptr;
  88. HashMap<StringName, MethodBind *> method_map;
  89. HashMap<StringName, int64_t> constant_map;
  90. struct EnumInfo {
  91. List<StringName> constants;
  92. bool is_bitfield = false;
  93. };
  94. HashMap<StringName, EnumInfo> enum_map;
  95. HashMap<StringName, MethodInfo> signal_map;
  96. List<PropertyInfo> property_list;
  97. HashMap<StringName, PropertyInfo> property_map;
  98. #ifdef DEBUG_METHODS_ENABLED
  99. List<StringName> constant_order;
  100. List<StringName> method_order;
  101. HashSet<StringName> methods_in_properties;
  102. List<MethodInfo> virtual_methods;
  103. HashMap<StringName, MethodInfo> virtual_methods_map;
  104. HashMap<StringName, Vector<Error>> method_error_values;
  105. HashMap<StringName, List<StringName>> linked_properties;
  106. #endif
  107. HashMap<StringName, PropertySetGet> property_setget;
  108. StringName inherits;
  109. StringName name;
  110. bool disabled = false;
  111. bool exposed = false;
  112. bool is_virtual = false;
  113. Object *(*creation_func)() = nullptr;
  114. ClassInfo() {}
  115. ~ClassInfo() {}
  116. };
  117. template <class T>
  118. static Object *creator() {
  119. return memnew(T);
  120. }
  121. static RWLock lock;
  122. static HashMap<StringName, ClassInfo> classes;
  123. static HashMap<StringName, StringName> resource_base_extensions;
  124. static HashMap<StringName, StringName> compat_classes;
  125. #ifdef DEBUG_METHODS_ENABLED
  126. static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount);
  127. #else
  128. static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const char *method_name, const Variant **p_defs, int p_defcount);
  129. #endif
  130. static APIType current_api;
  131. static void _add_class2(const StringName &p_class, const StringName &p_inherits);
  132. static HashMap<StringName, HashMap<StringName, Variant>> default_values;
  133. static HashSet<StringName> default_values_cached;
  134. // Native structs, used by binder
  135. struct NativeStruct {
  136. String ccode; // C code to create the native struct, fields separated by ; Arrays accepted (even containing other structs), also function pointers. All types must be Godot types.
  137. uint64_t struct_size; // local size of struct, for comparison
  138. };
  139. static HashMap<StringName, NativeStruct> native_structs;
  140. private:
  141. // Non-locking variants of get_parent_class and is_parent_class.
  142. static StringName _get_parent_class(const StringName &p_class);
  143. static bool _is_parent_class(const StringName &p_class, const StringName &p_inherits);
  144. public:
  145. // DO NOT USE THIS!!!!!! NEEDS TO BE PUBLIC BUT DO NOT USE NO MATTER WHAT!!!
  146. template <class T>
  147. static void _add_class() {
  148. _add_class2(T::get_class_static(), T::get_parent_class_static());
  149. }
  150. template <class T>
  151. static void register_class(bool p_virtual = false) {
  152. GLOBAL_LOCK_FUNCTION;
  153. T::initialize_class();
  154. ClassInfo *t = classes.getptr(T::get_class_static());
  155. ERR_FAIL_COND(!t);
  156. t->creation_func = &creator<T>;
  157. t->exposed = true;
  158. t->is_virtual = p_virtual;
  159. t->class_ptr = T::get_class_ptr_static();
  160. t->api = current_api;
  161. T::register_custom_data_to_otdb();
  162. }
  163. template <class T>
  164. static void register_abstract_class() {
  165. GLOBAL_LOCK_FUNCTION;
  166. T::initialize_class();
  167. ClassInfo *t = classes.getptr(T::get_class_static());
  168. ERR_FAIL_COND(!t);
  169. t->exposed = true;
  170. t->class_ptr = T::get_class_ptr_static();
  171. t->api = current_api;
  172. //nothing
  173. }
  174. static void register_extension_class(ObjectNativeExtension *p_extension);
  175. static void unregister_extension_class(const StringName &p_class);
  176. template <class T>
  177. static Object *_create_ptr_func() {
  178. return T::create();
  179. }
  180. template <class T>
  181. static void register_custom_instance_class() {
  182. GLOBAL_LOCK_FUNCTION;
  183. T::initialize_class();
  184. ClassInfo *t = classes.getptr(T::get_class_static());
  185. ERR_FAIL_COND(!t);
  186. t->creation_func = &_create_ptr_func<T>;
  187. t->exposed = true;
  188. t->class_ptr = T::get_class_ptr_static();
  189. t->api = current_api;
  190. T::register_custom_data_to_otdb();
  191. }
  192. static void get_class_list(List<StringName> *p_classes);
  193. static void get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes);
  194. static void get_direct_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes);
  195. static StringName get_parent_class_nocheck(const StringName &p_class);
  196. static StringName get_parent_class(const StringName &p_class);
  197. static StringName get_compatibility_remapped_class(const StringName &p_class);
  198. static bool class_exists(const StringName &p_class);
  199. static bool is_parent_class(const StringName &p_class, const StringName &p_inherits);
  200. static bool can_instantiate(const StringName &p_class);
  201. static bool is_virtual(const StringName &p_class);
  202. static Object *instantiate(const StringName &p_class);
  203. static void set_object_extension_instance(Object *p_object, const StringName &p_class, GDExtensionClassInstancePtr p_instance);
  204. static APIType get_api_type(const StringName &p_class);
  205. static uint64_t get_api_hash(APIType p_api);
  206. template <class N, class M, typename... VarArgs>
  207. static MethodBind *bind_method(N p_method_name, M p_method, VarArgs... p_args) {
  208. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  209. const Variant *argptrs[sizeof...(p_args) + 1];
  210. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  211. argptrs[i] = &args[i];
  212. }
  213. MethodBind *bind = create_method_bind(p_method);
  214. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  215. }
  216. template <class N, class M, typename... VarArgs>
  217. static MethodBind *bind_static_method(const StringName &p_class, N p_method_name, M p_method, VarArgs... p_args) {
  218. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  219. const Variant *argptrs[sizeof...(p_args) + 1];
  220. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  221. argptrs[i] = &args[i];
  222. }
  223. MethodBind *bind = create_static_method_bind(p_method);
  224. bind->set_instance_class(p_class);
  225. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  226. }
  227. template <class M>
  228. static MethodBind *bind_vararg_method(uint32_t p_flags, const StringName &p_name, M p_method, const MethodInfo &p_info = MethodInfo(), const Vector<Variant> &p_default_args = Vector<Variant>(), bool p_return_nil_is_variant = true) {
  229. GLOBAL_LOCK_FUNCTION;
  230. MethodBind *bind = create_vararg_method_bind(p_method, p_info, p_return_nil_is_variant);
  231. ERR_FAIL_COND_V(!bind, nullptr);
  232. bind->set_name(p_name);
  233. bind->set_default_arguments(p_default_args);
  234. String instance_type = bind->get_instance_class();
  235. ClassInfo *type = classes.getptr(instance_type);
  236. if (!type) {
  237. memdelete(bind);
  238. ERR_FAIL_COND_V(!type, nullptr);
  239. }
  240. if (type->method_map.has(p_name)) {
  241. memdelete(bind);
  242. // Overloading not supported
  243. ERR_FAIL_V_MSG(nullptr, "Method already bound: " + instance_type + "::" + p_name + ".");
  244. }
  245. type->method_map[p_name] = bind;
  246. #ifdef DEBUG_METHODS_ENABLED
  247. // FIXME: <reduz> set_return_type is no longer in MethodBind, so I guess it should be moved to vararg method bind
  248. //bind->set_return_type("Variant");
  249. type->method_order.push_back(p_name);
  250. #endif
  251. return bind;
  252. }
  253. static void bind_method_custom(const StringName &p_class, MethodBind *p_method);
  254. static void add_signal(const StringName &p_class, const MethodInfo &p_signal);
  255. static bool has_signal(const StringName &p_class, const StringName &p_signal, bool p_no_inheritance = false);
  256. static bool get_signal(const StringName &p_class, const StringName &p_signal, MethodInfo *r_signal);
  257. static void get_signal_list(const StringName &p_class, List<MethodInfo> *p_signals, bool p_no_inheritance = false);
  258. static void add_property_group(const StringName &p_class, const String &p_name, const String &p_prefix = "", int p_indent_depth = 0);
  259. static void add_property_subgroup(const StringName &p_class, const String &p_name, const String &p_prefix = "", int p_indent_depth = 0);
  260. static void add_property_array_count(const StringName &p_class, const String &p_label, const StringName &p_count_property, const StringName &p_count_setter, const StringName &p_count_getter, const String &p_array_element_prefix, uint32_t p_count_usage = PROPERTY_USAGE_DEFAULT);
  261. static void add_property_array(const StringName &p_class, const StringName &p_path, const String &p_array_element_prefix);
  262. static void add_property(const StringName &p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index = -1);
  263. static void set_property_default_value(const StringName &p_class, const StringName &p_name, const Variant &p_default);
  264. static void add_linked_property(const StringName &p_class, const String &p_property, const String &p_linked_property);
  265. static void get_property_list(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance = false, const Object *p_validator = nullptr);
  266. static bool get_property_info(const StringName &p_class, const StringName &p_property, PropertyInfo *r_info, bool p_no_inheritance = false, const Object *p_validator = nullptr);
  267. static void get_linked_properties_info(const StringName &p_class, const StringName &p_property, List<StringName> *r_properties, bool p_no_inheritance = false);
  268. static bool set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid = nullptr);
  269. static bool get_property(Object *p_object, const StringName &p_property, Variant &r_value);
  270. static bool has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance = false);
  271. static int get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid = nullptr);
  272. static Variant::Type get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid = nullptr);
  273. static StringName get_property_setter(const StringName &p_class, const StringName &p_property);
  274. static StringName get_property_getter(const StringName &p_class, const StringName &p_property);
  275. static bool has_method(const StringName &p_class, const StringName &p_method, bool p_no_inheritance = false);
  276. static void set_method_flags(const StringName &p_class, const StringName &p_method, int p_flags);
  277. static void get_method_list(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance = false, bool p_exclude_from_properties = false);
  278. static bool get_method_info(const StringName &p_class, const StringName &p_method, MethodInfo *r_info, bool p_no_inheritance = false, bool p_exclude_from_properties = false);
  279. static MethodBind *get_method(const StringName &p_class, const StringName &p_name);
  280. static void add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual = true, const Vector<String> &p_arg_names = Vector<String>(), bool p_object_core = false);
  281. static void get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance = false);
  282. static void bind_integer_constant(const StringName &p_class, const StringName &p_enum, const StringName &p_name, int64_t p_constant, bool p_is_bitfield = false);
  283. static void get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance = false);
  284. static int64_t get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success = nullptr);
  285. static bool has_integer_constant(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
  286. static StringName get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
  287. static StringName get_integer_constant_bitfield(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
  288. static void get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance = false);
  289. static void get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance = false);
  290. static bool has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
  291. static bool is_enum_bitfield(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
  292. static void set_method_error_return_values(const StringName &p_class, const StringName &p_method, const Vector<Error> &p_values);
  293. static Vector<Error> get_method_error_return_values(const StringName &p_class, const StringName &p_method);
  294. static Variant class_get_default_property_value(const StringName &p_class, const StringName &p_property, bool *r_valid = nullptr);
  295. static void set_class_enabled(const StringName &p_class, bool p_enable);
  296. static bool is_class_enabled(const StringName &p_class);
  297. static bool is_class_exposed(const StringName &p_class);
  298. static void add_resource_base_extension(const StringName &p_extension, const StringName &p_class);
  299. static void get_resource_base_extensions(List<String> *p_extensions);
  300. static void get_extensions_for_type(const StringName &p_class, List<String> *p_extensions);
  301. static bool is_resource_extension(const StringName &p_extension);
  302. static void add_compatibility_class(const StringName &p_class, const StringName &p_fallback);
  303. static StringName get_compatibility_class(const StringName &p_class);
  304. static void set_current_api(APIType p_api);
  305. static APIType get_current_api();
  306. static void cleanup_defaults();
  307. static void cleanup();
  308. static void register_native_struct(const StringName &p_name, const String &p_code, uint64_t p_current_size);
  309. static void get_native_struct_list(List<StringName> *r_names);
  310. static String get_native_struct_code(const StringName &p_name);
  311. static uint64_t get_native_struct_size(const StringName &p_name); // Used for asserting
  312. };
  313. #ifdef DEBUG_METHODS_ENABLED
  314. #define BIND_CONSTANT(m_constant) \
  315. ::ClassDB::bind_integer_constant(get_class_static(), StringName(), #m_constant, m_constant);
  316. #define BIND_ENUM_CONSTANT(m_constant) \
  317. ::ClassDB::bind_integer_constant(get_class_static(), __constant_get_enum_name(m_constant, #m_constant), #m_constant, m_constant);
  318. #define BIND_BITFIELD_FLAG(m_constant) \
  319. ::ClassDB::bind_integer_constant(get_class_static(), __constant_get_bitfield_name(m_constant, #m_constant), #m_constant, m_constant, true);
  320. _FORCE_INLINE_ void errarray_add_str(Vector<Error> &arr) {
  321. }
  322. _FORCE_INLINE_ void errarray_add_str(Vector<Error> &arr, const Error &p_err) {
  323. arr.push_back(p_err);
  324. }
  325. template <class... P>
  326. _FORCE_INLINE_ void errarray_add_str(Vector<Error> &arr, const Error &p_err, P... p_args) {
  327. arr.push_back(p_err);
  328. errarray_add_str(arr, p_args...);
  329. }
  330. template <class... P>
  331. _FORCE_INLINE_ Vector<Error> errarray(P... p_args) {
  332. Vector<Error> arr;
  333. errarray_add_str(arr, p_args...);
  334. return arr;
  335. }
  336. #define BIND_METHOD_ERR_RETURN_DOC(m_method, ...) \
  337. ::ClassDB::set_method_error_return_values(get_class_static(), m_method, errarray(__VA_ARGS__));
  338. #else
  339. #define BIND_CONSTANT(m_constant) \
  340. ::ClassDB::bind_integer_constant(get_class_static(), StringName(), #m_constant, m_constant);
  341. #define BIND_ENUM_CONSTANT(m_constant) \
  342. ::ClassDB::bind_integer_constant(get_class_static(), StringName(), #m_constant, m_constant);
  343. #define BIND_BITFIELD_FLAG(m_constant) \
  344. ::ClassDB::bind_integer_constant(get_class_static(), StringName(), #m_constant, m_constant, true);
  345. #define BIND_METHOD_ERR_RETURN_DOC(m_method, ...)
  346. #endif
  347. #define GDREGISTER_CLASS(m_class) \
  348. if (m_class::_class_is_enabled) { \
  349. ::ClassDB::register_class<m_class>(); \
  350. }
  351. #define GDREGISTER_VIRTUAL_CLASS(m_class) \
  352. if (m_class::_class_is_enabled) { \
  353. ::ClassDB::register_class<m_class>(true); \
  354. }
  355. #define GDREGISTER_ABSTRACT_CLASS(m_class) \
  356. if (m_class::_class_is_enabled) { \
  357. ::ClassDB::register_abstract_class<m_class>(); \
  358. }
  359. #define GDREGISTER_NATIVE_STRUCT(m_class, m_code) ClassDB::register_native_struct(#m_class, m_code, sizeof(m_class))
  360. #include "core/disabled_classes.gen.h"
  361. #endif // CLASS_DB_H