class_db.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /**************************************************************************/
  2. /* class_db.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. #pragma once
  31. #include "core/object/method_bind.h"
  32. #include "core/object/object.h"
  33. #include "core/string/print_string.h"
  34. // Makes callable_mp readily available in all classes connecting signals.
  35. // Needs to come after method_bind and object have been included.
  36. #include "core/object/callable_method_pointer.h"
  37. #include "core/templates/hash_set.h"
  38. #include <type_traits>
  39. #define DEFVAL(m_defval) (m_defval)
  40. #define DEFVAL_ARRAY DEFVAL(ClassDB::default_array_arg)
  41. #ifdef DEBUG_METHODS_ENABLED
  42. struct MethodDefinition {
  43. StringName name;
  44. Vector<StringName> args;
  45. MethodDefinition() {}
  46. MethodDefinition(const char *p_name) :
  47. name(p_name) {}
  48. MethodDefinition(const StringName &p_name) :
  49. name(p_name) {}
  50. };
  51. MethodDefinition D_METHODP(const char *p_name, const char *const **p_args, uint32_t p_argcount);
  52. template <typename... VarArgs>
  53. MethodDefinition D_METHOD(const char *p_name, const VarArgs... p_args) {
  54. const char *args[sizeof...(p_args) + 1] = { p_args..., nullptr }; // +1 makes sure zero sized arrays are also supported.
  55. const char *const *argptrs[sizeof...(p_args) + 1];
  56. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  57. argptrs[i] = &args[i];
  58. }
  59. return D_METHODP(p_name, sizeof...(p_args) == 0 ? nullptr : (const char *const **)argptrs, sizeof...(p_args));
  60. }
  61. #else
  62. // When DEBUG_METHODS_ENABLED is set this will let the engine know
  63. // the argument names for easier debugging.
  64. #define D_METHOD(m_c, ...) m_c
  65. #endif
  66. class ClassDB {
  67. public:
  68. enum APIType {
  69. API_CORE,
  70. API_EDITOR,
  71. API_EXTENSION,
  72. API_EDITOR_EXTENSION,
  73. API_NONE
  74. };
  75. public:
  76. struct PropertySetGet {
  77. int index;
  78. StringName setter;
  79. StringName getter;
  80. MethodBind *_setptr = nullptr;
  81. MethodBind *_getptr = nullptr;
  82. Variant::Type type;
  83. };
  84. struct ClassInfo {
  85. APIType api = API_NONE;
  86. ClassInfo *inherits_ptr = nullptr;
  87. void *class_ptr = nullptr;
  88. ObjectGDExtension *gdextension = nullptr;
  89. HashMap<StringName, MethodBind *> method_map;
  90. HashMap<StringName, LocalVector<MethodBind *>> method_map_compatibility;
  91. HashMap<StringName, int64_t> constant_map;
  92. struct EnumInfo {
  93. List<StringName> constants;
  94. bool is_bitfield = false;
  95. };
  96. HashMap<StringName, EnumInfo> enum_map;
  97. HashMap<StringName, MethodInfo> signal_map;
  98. List<PropertyInfo> property_list;
  99. HashMap<StringName, PropertyInfo> property_map;
  100. #ifdef DEBUG_METHODS_ENABLED
  101. List<StringName> constant_order;
  102. List<StringName> method_order;
  103. HashSet<StringName> methods_in_properties;
  104. List<MethodInfo> virtual_methods;
  105. HashMap<StringName, MethodInfo> virtual_methods_map;
  106. HashMap<StringName, Vector<Error>> method_error_values;
  107. HashMap<StringName, List<StringName>> linked_properties;
  108. #endif
  109. HashMap<StringName, PropertySetGet> property_setget;
  110. HashMap<StringName, Vector<uint32_t>> virtual_methods_compat;
  111. StringName inherits;
  112. StringName name;
  113. bool disabled = false;
  114. bool exposed = false;
  115. bool reloadable = false;
  116. bool is_virtual = false;
  117. bool is_runtime = false;
  118. // The bool argument indicates the need to postinitialize.
  119. Object *(*creation_func)(bool) = nullptr;
  120. ClassInfo() {}
  121. ~ClassInfo() {}
  122. };
  123. template <typename T>
  124. static Object *creator(bool p_notify_postinitialize) {
  125. Object *ret = new ("") T;
  126. ret->_initialize();
  127. if (p_notify_postinitialize) {
  128. ret->_postinitialize();
  129. }
  130. return ret;
  131. }
  132. // We need a recursive r/w lock because there are various code paths
  133. // that may in turn invoke other entry points with require locking.
  134. class Locker {
  135. public:
  136. enum State {
  137. STATE_UNLOCKED,
  138. STATE_READ,
  139. STATE_WRITE,
  140. };
  141. private:
  142. inline static RWLock lock;
  143. inline thread_local static State thread_state = STATE_UNLOCKED;
  144. public:
  145. class Lock {
  146. State state = STATE_UNLOCKED;
  147. public:
  148. explicit Lock(State p_state);
  149. ~Lock();
  150. };
  151. };
  152. static HashMap<StringName, ClassInfo> classes;
  153. static HashMap<StringName, StringName> resource_base_extensions;
  154. static HashMap<StringName, StringName> compat_classes;
  155. #ifdef TOOLS_ENABLED
  156. static HashMap<StringName, ObjectGDExtension> placeholder_extensions;
  157. #endif
  158. #ifdef DEBUG_METHODS_ENABLED
  159. static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, bool p_compatibility, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount);
  160. #else
  161. static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, bool p_compatibility, const char *method_name, const Variant **p_defs, int p_defcount);
  162. #endif
  163. static APIType current_api;
  164. static HashMap<APIType, uint32_t> api_hashes_cache;
  165. static void _add_class2(const StringName &p_class, const StringName &p_inherits);
  166. static HashMap<StringName, HashMap<StringName, Variant>> default_values;
  167. static HashSet<StringName> default_values_cached;
  168. // Native structs, used by binder
  169. struct NativeStruct {
  170. 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.
  171. uint64_t struct_size; // local size of struct, for comparison
  172. };
  173. static HashMap<StringName, NativeStruct> native_structs;
  174. static Array default_array_arg;
  175. static bool is_default_array_arg(const Array &p_array);
  176. private:
  177. // Non-locking variants of get_parent_class and is_parent_class.
  178. static StringName _get_parent_class(const StringName &p_class);
  179. static bool _is_parent_class(const StringName &p_class, const StringName &p_inherits);
  180. static void _bind_compatibility(ClassInfo *type, MethodBind *p_method);
  181. static MethodBind *_bind_vararg_method(MethodBind *p_bind, const StringName &p_name, const Vector<Variant> &p_default_args, bool p_compatibility);
  182. static void _bind_method_custom(const StringName &p_class, MethodBind *p_method, bool p_compatibility);
  183. static Object *_instantiate_internal(const StringName &p_class, bool p_require_real_class = false, bool p_notify_postinitialize = true, bool p_exposed_only = true);
  184. static bool _can_instantiate(ClassInfo *p_class_info, bool p_exposed_only = true);
  185. public:
  186. // DO NOT USE THIS!!!!!! NEEDS TO BE PUBLIC BUT DO NOT USE NO MATTER WHAT!!!
  187. template <typename T>
  188. static void _add_class() {
  189. _add_class2(T::get_class_static(), T::get_parent_class_static());
  190. }
  191. template <typename T>
  192. static void register_class(bool p_virtual = false) {
  193. Locker::Lock lock(Locker::STATE_WRITE);
  194. static_assert(std::is_same_v<typename T::self_type, T>, "Class not declared properly, please use GDCLASS.");
  195. T::initialize_class();
  196. ClassInfo *t = classes.getptr(T::get_class_static());
  197. ERR_FAIL_NULL(t);
  198. t->creation_func = &creator<T>;
  199. t->exposed = true;
  200. t->is_virtual = p_virtual;
  201. t->class_ptr = T::get_class_ptr_static();
  202. t->api = current_api;
  203. T::register_custom_data_to_otdb();
  204. }
  205. template <typename T>
  206. static void register_abstract_class() {
  207. Locker::Lock lock(Locker::STATE_WRITE);
  208. static_assert(std::is_same_v<typename T::self_type, T>, "Class not declared properly, please use GDCLASS.");
  209. T::initialize_class();
  210. ClassInfo *t = classes.getptr(T::get_class_static());
  211. ERR_FAIL_NULL(t);
  212. t->exposed = true;
  213. t->class_ptr = T::get_class_ptr_static();
  214. t->api = current_api;
  215. //nothing
  216. }
  217. template <typename T>
  218. static void register_internal_class() {
  219. Locker::Lock lock(Locker::STATE_WRITE);
  220. static_assert(std::is_same_v<typename T::self_type, T>, "Class not declared properly, please use GDCLASS.");
  221. T::initialize_class();
  222. ClassInfo *t = classes.getptr(T::get_class_static());
  223. ERR_FAIL_NULL(t);
  224. t->creation_func = &creator<T>;
  225. t->exposed = false;
  226. t->is_virtual = false;
  227. t->class_ptr = T::get_class_ptr_static();
  228. t->api = current_api;
  229. T::register_custom_data_to_otdb();
  230. }
  231. template <typename T>
  232. static void register_runtime_class() {
  233. Locker::Lock lock(Locker::STATE_WRITE);
  234. static_assert(std::is_same_v<typename T::self_type, T>, "Class not declared properly, please use GDCLASS.");
  235. T::initialize_class();
  236. ClassInfo *t = classes.getptr(T::get_class_static());
  237. ERR_FAIL_NULL(t);
  238. ERR_FAIL_COND_MSG(t->inherits_ptr && !t->inherits_ptr->creation_func, vformat("Cannot register runtime class '%s' that descends from an abstract parent class.", T::get_class_static()));
  239. t->creation_func = &creator<T>;
  240. t->exposed = true;
  241. t->is_virtual = false;
  242. t->is_runtime = true;
  243. t->class_ptr = T::get_class_ptr_static();
  244. t->api = current_api;
  245. T::register_custom_data_to_otdb();
  246. }
  247. static void register_extension_class(ObjectGDExtension *p_extension);
  248. static void unregister_extension_class(const StringName &p_class, bool p_free_method_binds = true);
  249. template <typename T>
  250. static Object *_create_ptr_func(bool p_notify_postinitialize) {
  251. return T::create(p_notify_postinitialize);
  252. }
  253. template <typename T>
  254. static void register_custom_instance_class() {
  255. Locker::Lock lock(Locker::STATE_WRITE);
  256. static_assert(std::is_same_v<typename T::self_type, T>, "Class not declared properly, please use GDCLASS.");
  257. T::initialize_class();
  258. ClassInfo *t = classes.getptr(T::get_class_static());
  259. ERR_FAIL_NULL(t);
  260. t->creation_func = &_create_ptr_func<T>;
  261. t->exposed = true;
  262. t->class_ptr = T::get_class_ptr_static();
  263. t->api = current_api;
  264. T::register_custom_data_to_otdb();
  265. }
  266. static void get_class_list(List<StringName> *p_classes);
  267. #ifdef TOOLS_ENABLED
  268. static void get_extensions_class_list(List<StringName> *p_classes);
  269. static void get_extension_class_list(const Ref<GDExtension> &p_extension, List<StringName> *p_classes);
  270. static ObjectGDExtension *get_placeholder_extension(const StringName &p_class);
  271. #endif
  272. static void get_inheriters_from_class(const StringName &p_class, LocalVector<StringName> &p_classes);
  273. static void get_direct_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes);
  274. static StringName get_parent_class_nocheck(const StringName &p_class);
  275. static bool get_inheritance_chain_nocheck(const StringName &p_class, Vector<StringName> &r_result);
  276. static StringName get_parent_class(const StringName &p_class);
  277. static StringName get_compatibility_remapped_class(const StringName &p_class);
  278. static bool class_exists(const StringName &p_class);
  279. static bool is_parent_class(const StringName &p_class, const StringName &p_inherits);
  280. static bool can_instantiate(const StringName &p_class);
  281. static bool is_abstract(const StringName &p_class);
  282. static bool is_virtual(const StringName &p_class);
  283. static Object *instantiate(const StringName &p_class);
  284. static Object *instantiate_no_placeholders(const StringName &p_class);
  285. static Object *instantiate_without_postinitialization(const StringName &p_class);
  286. static void set_object_extension_instance(Object *p_object, const StringName &p_class, GDExtensionClassInstancePtr p_instance);
  287. static APIType get_api_type(const StringName &p_class);
  288. static uint32_t get_api_hash(APIType p_api);
  289. template <typename>
  290. struct member_function_traits;
  291. template <typename R, typename T, typename... Args>
  292. struct member_function_traits<R (T::*)(Args...)> {
  293. using return_type = R;
  294. };
  295. template <typename R, typename T, typename... Args>
  296. struct member_function_traits<R (T::*)(Args...) const> {
  297. using return_type = R;
  298. };
  299. template <typename R, typename... Args>
  300. struct member_function_traits<R (*)(Args...)> {
  301. using return_type = R;
  302. };
  303. template <typename N, typename M, typename... VarArgs>
  304. static MethodBind *bind_method(N p_method_name, M p_method, VarArgs... p_args) {
  305. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  306. const Variant *argptrs[sizeof...(p_args) + 1];
  307. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  308. argptrs[i] = &args[i];
  309. }
  310. MethodBind *bind = create_method_bind(p_method);
  311. if constexpr (std::is_same_v<typename member_function_traits<M>::return_type, Object *>) {
  312. bind->set_return_type_is_raw_object_ptr(true);
  313. }
  314. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, false, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  315. }
  316. template <typename N, typename M, typename... VarArgs>
  317. static MethodBind *bind_static_method(const StringName &p_class, N p_method_name, M p_method, VarArgs... p_args) {
  318. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  319. const Variant *argptrs[sizeof...(p_args) + 1];
  320. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  321. argptrs[i] = &args[i];
  322. }
  323. MethodBind *bind = create_static_method_bind(p_method);
  324. bind->set_instance_class(p_class);
  325. if constexpr (std::is_same_v<typename member_function_traits<M>::return_type, Object *>) {
  326. bind->set_return_type_is_raw_object_ptr(true);
  327. }
  328. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, false, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  329. }
  330. template <typename N, typename M, typename... VarArgs>
  331. static MethodBind *bind_compatibility_method(N p_method_name, M p_method, VarArgs... p_args) {
  332. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  333. const Variant *argptrs[sizeof...(p_args) + 1];
  334. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  335. argptrs[i] = &args[i];
  336. }
  337. MethodBind *bind = create_method_bind(p_method);
  338. if constexpr (std::is_same_v<typename member_function_traits<M>::return_type, Object *>) {
  339. bind->set_return_type_is_raw_object_ptr(true);
  340. }
  341. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, true, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  342. }
  343. template <typename N, typename M, typename... VarArgs>
  344. static MethodBind *bind_compatibility_static_method(const StringName &p_class, N p_method_name, M p_method, VarArgs... p_args) {
  345. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  346. const Variant *argptrs[sizeof...(p_args) + 1];
  347. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  348. argptrs[i] = &args[i];
  349. }
  350. MethodBind *bind = create_static_method_bind(p_method);
  351. bind->set_instance_class(p_class);
  352. if constexpr (std::is_same_v<typename member_function_traits<M>::return_type, Object *>) {
  353. bind->set_return_type_is_raw_object_ptr(true);
  354. }
  355. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, true, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  356. }
  357. template <typename M>
  358. 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) {
  359. Locker::Lock lock(Locker::STATE_WRITE);
  360. MethodBind *bind = create_vararg_method_bind(p_method, p_info, p_return_nil_is_variant);
  361. ERR_FAIL_NULL_V(bind, nullptr);
  362. if constexpr (std::is_same_v<typename member_function_traits<M>::return_type, Object *>) {
  363. bind->set_return_type_is_raw_object_ptr(true);
  364. }
  365. return _bind_vararg_method(bind, p_name, p_default_args, false);
  366. }
  367. template <typename M>
  368. static MethodBind *bind_compatibility_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) {
  369. Locker::Lock lock(Locker::STATE_WRITE);
  370. MethodBind *bind = create_vararg_method_bind(p_method, p_info, p_return_nil_is_variant);
  371. ERR_FAIL_NULL_V(bind, nullptr);
  372. if constexpr (std::is_same_v<typename member_function_traits<M>::return_type, Object *>) {
  373. bind->set_return_type_is_raw_object_ptr(true);
  374. }
  375. return _bind_vararg_method(bind, p_name, p_default_args, true);
  376. }
  377. static void bind_method_custom(const StringName &p_class, MethodBind *p_method);
  378. static void bind_compatibility_method_custom(const StringName &p_class, MethodBind *p_method);
  379. static void add_signal(const StringName &p_class, const MethodInfo &p_signal);
  380. static bool has_signal(const StringName &p_class, const StringName &p_signal, bool p_no_inheritance = false);
  381. static bool get_signal(const StringName &p_class, const StringName &p_signal, MethodInfo *r_signal);
  382. static void get_signal_list(const StringName &p_class, List<MethodInfo> *p_signals, bool p_no_inheritance = false);
  383. static void add_property_group(const StringName &p_class, const String &p_name, const String &p_prefix = "", int p_indent_depth = 0);
  384. static void add_property_subgroup(const StringName &p_class, const String &p_name, const String &p_prefix = "", int p_indent_depth = 0);
  385. 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);
  386. static void add_property_array(const StringName &p_class, const StringName &p_path, const String &p_array_element_prefix);
  387. static void add_property(const StringName &p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index = -1);
  388. static void set_property_default_value(const StringName &p_class, const StringName &p_name, const Variant &p_default);
  389. static void add_linked_property(const StringName &p_class, const String &p_property, const String &p_linked_property);
  390. static void get_property_list(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance = false, const Object *p_validator = nullptr);
  391. 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);
  392. static void get_linked_properties_info(const StringName &p_class, const StringName &p_property, List<StringName> *r_properties, bool p_no_inheritance = false);
  393. static bool set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid = nullptr);
  394. static bool get_property(Object *p_object, const StringName &p_property, Variant &r_value);
  395. static bool has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance = false);
  396. static int get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid = nullptr);
  397. static Variant::Type get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid = nullptr);
  398. static StringName get_property_setter(const StringName &p_class, const StringName &p_property);
  399. static StringName get_property_getter(const StringName &p_class, const StringName &p_property);
  400. static bool has_method(const StringName &p_class, const StringName &p_method, bool p_no_inheritance = false);
  401. static void set_method_flags(const StringName &p_class, const StringName &p_method, int p_flags);
  402. static void get_method_list(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance = false, bool p_exclude_from_properties = false);
  403. static void get_method_list_with_compatibility(const StringName &p_class, List<Pair<MethodInfo, uint32_t>> *p_methods_with_hash, bool p_no_inheritance = false, bool p_exclude_from_properties = false);
  404. 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);
  405. static int get_method_argument_count(const StringName &p_class, const StringName &p_method, bool *r_is_valid = nullptr, bool p_no_inheritance = false);
  406. static MethodBind *get_method(const StringName &p_class, const StringName &p_name);
  407. static MethodBind *get_method_with_compatibility(const StringName &p_class, const StringName &p_name, uint64_t p_hash, bool *r_method_exists = nullptr, bool *r_is_deprecated = nullptr);
  408. static Vector<uint32_t> get_method_compatibility_hashes(const StringName &p_class, const StringName &p_name);
  409. 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);
  410. static void add_virtual_compatibility_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);
  411. static void get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance = false);
  412. static void add_extension_class_virtual_method(const StringName &p_class, const GDExtensionClassVirtualMethodInfo *p_method_info);
  413. static Vector<uint32_t> get_virtual_method_compatibility_hashes(const StringName &p_class, const StringName &p_name);
  414. 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);
  415. static void get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance = false);
  416. static int64_t get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success = nullptr);
  417. static bool has_integer_constant(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
  418. static StringName get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
  419. static StringName get_integer_constant_bitfield(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
  420. static void get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance = false);
  421. static void get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance = false);
  422. static bool has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
  423. static bool is_enum_bitfield(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
  424. static void set_method_error_return_values(const StringName &p_class, const StringName &p_method, const Vector<Error> &p_values);
  425. static Vector<Error> get_method_error_return_values(const StringName &p_class, const StringName &p_method);
  426. static Variant class_get_default_property_value(const StringName &p_class, const StringName &p_property, bool *r_valid = nullptr);
  427. static void set_class_enabled(const StringName &p_class, bool p_enable);
  428. static bool is_class_enabled(const StringName &p_class);
  429. static bool is_class_exposed(const StringName &p_class);
  430. static bool is_class_reloadable(const StringName &p_class);
  431. static bool is_class_runtime(const StringName &p_class);
  432. static void add_resource_base_extension(const StringName &p_extension, const StringName &p_class);
  433. static void get_resource_base_extensions(List<String> *p_extensions);
  434. static void get_extensions_for_type(const StringName &p_class, List<String> *p_extensions);
  435. static bool is_resource_extension(const StringName &p_extension);
  436. static void add_compatibility_class(const StringName &p_class, const StringName &p_fallback);
  437. static StringName get_compatibility_class(const StringName &p_class);
  438. static void set_current_api(APIType p_api);
  439. static APIType get_current_api();
  440. static void cleanup_defaults();
  441. static void cleanup();
  442. static void register_native_struct(const StringName &p_name, const String &p_code, uint64_t p_current_size);
  443. static void get_native_struct_list(List<StringName> *r_names);
  444. static String get_native_struct_code(const StringName &p_name);
  445. static uint64_t get_native_struct_size(const StringName &p_name); // Used for asserting
  446. static Object *_instantiate_allow_unexposed(const StringName &p_class); // Used to create unexposed classes from GDExtension, typically for unexposed EditorPlugin.
  447. };
  448. #define BIND_ENUM_CONSTANT(m_constant) \
  449. ::ClassDB::bind_integer_constant(get_class_static(), __constant_get_enum_name(m_constant, #m_constant), #m_constant, m_constant);
  450. #define BIND_BITFIELD_FLAG(m_constant) \
  451. ::ClassDB::bind_integer_constant(get_class_static(), __constant_get_bitfield_name(m_constant, #m_constant), #m_constant, m_constant, true);
  452. #define BIND_CONSTANT(m_constant) \
  453. ::ClassDB::bind_integer_constant(get_class_static(), StringName(), #m_constant, m_constant);
  454. #ifdef DEBUG_METHODS_ENABLED
  455. _FORCE_INLINE_ void errarray_add_str(Vector<Error> &arr) {
  456. }
  457. _FORCE_INLINE_ void errarray_add_str(Vector<Error> &arr, const Error &p_err) {
  458. arr.push_back(p_err);
  459. }
  460. template <typename... P>
  461. _FORCE_INLINE_ void errarray_add_str(Vector<Error> &arr, const Error &p_err, P... p_args) {
  462. arr.push_back(p_err);
  463. errarray_add_str(arr, p_args...);
  464. }
  465. template <typename... P>
  466. _FORCE_INLINE_ Vector<Error> errarray(P... p_args) {
  467. Vector<Error> arr;
  468. errarray_add_str(arr, p_args...);
  469. return arr;
  470. }
  471. #define BIND_METHOD_ERR_RETURN_DOC(m_method, ...) \
  472. ::ClassDB::set_method_error_return_values(get_class_static(), m_method, errarray(__VA_ARGS__));
  473. #else
  474. #define BIND_METHOD_ERR_RETURN_DOC(m_method, ...)
  475. #endif
  476. #define GDREGISTER_CLASS(m_class) \
  477. if (m_class::_class_is_enabled) { \
  478. ::ClassDB::register_class<m_class>(); \
  479. }
  480. #define GDREGISTER_VIRTUAL_CLASS(m_class) \
  481. if (m_class::_class_is_enabled) { \
  482. ::ClassDB::register_class<m_class>(true); \
  483. }
  484. #define GDREGISTER_ABSTRACT_CLASS(m_class) \
  485. if (m_class::_class_is_enabled) { \
  486. ::ClassDB::register_abstract_class<m_class>(); \
  487. }
  488. #define GDREGISTER_INTERNAL_CLASS(m_class) \
  489. if (m_class::_class_is_enabled) { \
  490. ::ClassDB::register_internal_class<m_class>(); \
  491. }
  492. #define GDREGISTER_RUNTIME_CLASS(m_class) \
  493. if (m_class::_class_is_enabled) { \
  494. ::ClassDB::register_runtime_class<m_class>(); \
  495. }
  496. #define GDREGISTER_NATIVE_STRUCT(m_class, m_code) ClassDB::register_native_struct(#m_class, m_code, sizeof(m_class))
  497. #define GD_IS_CLASS_ENABLED(m_class) m_class::_class_is_enabled
  498. #include "core/disabled_classes.gen.h"