class_db.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  1. /**************************************************************************/
  2. /* class_db.cpp */
  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. #include "class_db.h"
  31. #include "core/config/engine.h"
  32. #include "core/core_string_names.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/object/script_language.h"
  35. #include "core/os/mutex.h"
  36. #include "core/version.h"
  37. #define OBJTYPE_RLOCK RWLockRead _rw_lockr_(lock);
  38. #define OBJTYPE_WLOCK RWLockWrite _rw_lockw_(lock);
  39. #ifdef DEBUG_METHODS_ENABLED
  40. MethodDefinition D_METHODP(const char *p_name, const char *const **p_args, uint32_t p_argcount) {
  41. MethodDefinition md;
  42. md.name = StaticCString::create(p_name);
  43. md.args.resize(p_argcount);
  44. for (uint32_t i = 0; i < p_argcount; i++) {
  45. md.args.write[i] = StaticCString::create(*p_args[i]);
  46. }
  47. return md;
  48. }
  49. #endif
  50. ClassDB::APIType ClassDB::current_api = API_CORE;
  51. HashMap<ClassDB::APIType, uint32_t> ClassDB::api_hashes_cache;
  52. void ClassDB::set_current_api(APIType p_api) {
  53. DEV_ASSERT(!api_hashes_cache.has(p_api)); // This API type may not be suitable for caching of hash if it can change later.
  54. current_api = p_api;
  55. }
  56. ClassDB::APIType ClassDB::get_current_api() {
  57. return current_api;
  58. }
  59. HashMap<StringName, ClassDB::ClassInfo> ClassDB::classes;
  60. HashMap<StringName, StringName> ClassDB::resource_base_extensions;
  61. HashMap<StringName, StringName> ClassDB::compat_classes;
  62. #ifdef TOOLS_ENABLED
  63. HashMap<StringName, ObjectGDExtension> ClassDB::placeholder_extensions;
  64. class PlaceholderExtensionInstance {
  65. StringName class_name;
  66. HashMap<StringName, Variant> properties;
  67. public:
  68. PlaceholderExtensionInstance(const StringName &p_class_name) {
  69. class_name = p_class_name;
  70. }
  71. ~PlaceholderExtensionInstance() {}
  72. void set(const StringName &p_name, const Variant &p_value) {
  73. bool is_default_valid = false;
  74. Variant default_value = ClassDB::class_get_default_property_value(class_name, p_name, &is_default_valid);
  75. // If there's a default value, then we know it's a valid property.
  76. if (is_default_valid) {
  77. properties[p_name] = p_value;
  78. }
  79. }
  80. Variant get(const StringName &p_name) {
  81. const Variant *value = properties.getptr(p_name);
  82. Variant ret;
  83. if (value) {
  84. ret = *value;
  85. } else {
  86. bool is_default_valid = false;
  87. Variant default_value = ClassDB::class_get_default_property_value(class_name, p_name, &is_default_valid);
  88. if (is_default_valid) {
  89. ret = default_value;
  90. }
  91. }
  92. return ret;
  93. }
  94. static GDExtensionBool placeholder_instance_set(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value) {
  95. PlaceholderExtensionInstance *self = (PlaceholderExtensionInstance *)p_instance;
  96. const StringName &name = *(StringName *)p_name;
  97. const Variant &value = *(const Variant *)p_value;
  98. self->set(name, value);
  99. // We have to return true so Godot doesn't try to call the real setter function.
  100. return true;
  101. }
  102. static GDExtensionBool placeholder_instance_get(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) {
  103. PlaceholderExtensionInstance *self = (PlaceholderExtensionInstance *)p_instance;
  104. const StringName &name = *(StringName *)p_name;
  105. Variant *value = (Variant *)r_ret;
  106. *value = self->get(name);
  107. // We have to return true so Godot doesn't try to call the real getter function.
  108. return true;
  109. }
  110. static const GDExtensionPropertyInfo *placeholder_instance_get_property_list(GDExtensionClassInstancePtr p_instance, uint32_t *r_count) {
  111. *r_count = 0;
  112. return nullptr;
  113. }
  114. static void placeholder_instance_free_property_list(GDExtensionClassInstancePtr p_instance, const GDExtensionPropertyInfo *p_list) {
  115. }
  116. static GDExtensionBool placeholder_instance_property_can_revert(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name) {
  117. return false;
  118. }
  119. static GDExtensionBool placeholder_instance_property_get_revert(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) {
  120. return false;
  121. }
  122. static GDExtensionBool placeholder_instance_validate_property(GDExtensionClassInstancePtr p_instance, GDExtensionPropertyInfo *p_property) {
  123. return false;
  124. }
  125. static void placeholder_instance_notification(GDExtensionClassInstancePtr p_instance, int32_t p_what, GDExtensionBool p_reversed) {
  126. }
  127. static void placeholder_instance_to_string(GDExtensionClassInstancePtr p_instance, GDExtensionBool *r_is_valid, GDExtensionStringPtr p_out) {
  128. *r_is_valid = true;
  129. }
  130. static void placeholder_instance_reference(GDExtensionClassInstancePtr p_instance) {
  131. }
  132. static void placeholder_instance_unreference(GDExtensionClassInstancePtr p_instance) {
  133. }
  134. static uint64_t placeholder_instance_get_rid(GDExtensionClassInstancePtr p_instance) {
  135. return 0;
  136. }
  137. static GDExtensionObjectPtr placeholder_class_create_instance(void *p_class_userdata) {
  138. ClassDB::ClassInfo *ti = (ClassDB::ClassInfo *)p_class_userdata;
  139. // Find the closest native parent.
  140. ClassDB::ClassInfo *native_parent = ti->inherits_ptr;
  141. while (native_parent->gdextension) {
  142. native_parent = native_parent->inherits_ptr;
  143. }
  144. ERR_FAIL_NULL_V(native_parent->creation_func, nullptr);
  145. // Construct a placeholder.
  146. Object *obj = native_parent->creation_func();
  147. obj->_extension = ClassDB::get_placeholder_extension(ti->name);
  148. obj->_extension_instance = memnew(PlaceholderExtensionInstance(ti->name));
  149. return obj;
  150. }
  151. static GDExtensionObjectPtr placeholder_class_recreate_instance(void *p_class_userdata, GDExtensionObjectPtr p_object) {
  152. ClassDB::ClassInfo *ti = (ClassDB::ClassInfo *)p_class_userdata;
  153. return memnew(PlaceholderExtensionInstance(ti->name));
  154. }
  155. static void placeholder_class_free_instance(void *p_class_userdata, GDExtensionClassInstancePtr p_instance) {
  156. PlaceholderExtensionInstance *instance = (PlaceholderExtensionInstance *)p_instance;
  157. memdelete(instance);
  158. }
  159. static GDExtensionClassCallVirtual placeholder_class_get_virtual(void *p_class_userdata, GDExtensionConstStringNamePtr p_name) {
  160. return nullptr;
  161. }
  162. };
  163. #endif
  164. bool ClassDB::_is_parent_class(const StringName &p_class, const StringName &p_inherits) {
  165. if (!classes.has(p_class)) {
  166. return false;
  167. }
  168. StringName inherits = p_class;
  169. while (inherits.operator String().length()) {
  170. if (inherits == p_inherits) {
  171. return true;
  172. }
  173. inherits = _get_parent_class(inherits);
  174. }
  175. return false;
  176. }
  177. bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) {
  178. OBJTYPE_RLOCK;
  179. return _is_parent_class(p_class, p_inherits);
  180. }
  181. void ClassDB::get_class_list(List<StringName> *p_classes) {
  182. OBJTYPE_RLOCK;
  183. for (const KeyValue<StringName, ClassInfo> &E : classes) {
  184. p_classes->push_back(E.key);
  185. }
  186. p_classes->sort_custom<StringName::AlphCompare>();
  187. }
  188. #ifdef TOOLS_ENABLED
  189. void ClassDB::get_extensions_class_list(List<StringName> *p_classes) {
  190. OBJTYPE_RLOCK;
  191. for (const KeyValue<StringName, ClassInfo> &E : classes) {
  192. if (E.value.api != API_EXTENSION && E.value.api != API_EDITOR_EXTENSION) {
  193. continue;
  194. }
  195. p_classes->push_back(E.key);
  196. }
  197. p_classes->sort_custom<StringName::AlphCompare>();
  198. }
  199. #endif
  200. void ClassDB::get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
  201. OBJTYPE_RLOCK;
  202. for (const KeyValue<StringName, ClassInfo> &E : classes) {
  203. if (E.key != p_class && _is_parent_class(E.key, p_class)) {
  204. p_classes->push_back(E.key);
  205. }
  206. }
  207. }
  208. void ClassDB::get_direct_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
  209. OBJTYPE_RLOCK;
  210. for (const KeyValue<StringName, ClassInfo> &E : classes) {
  211. if (E.key != p_class && _get_parent_class(E.key) == p_class) {
  212. p_classes->push_back(E.key);
  213. }
  214. }
  215. }
  216. StringName ClassDB::get_parent_class_nocheck(const StringName &p_class) {
  217. OBJTYPE_RLOCK;
  218. ClassInfo *ti = classes.getptr(p_class);
  219. if (!ti) {
  220. return StringName();
  221. }
  222. return ti->inherits;
  223. }
  224. StringName ClassDB::get_compatibility_remapped_class(const StringName &p_class) {
  225. if (classes.has(p_class)) {
  226. return p_class;
  227. }
  228. if (compat_classes.has(p_class)) {
  229. return compat_classes[p_class];
  230. }
  231. return p_class;
  232. }
  233. StringName ClassDB::_get_parent_class(const StringName &p_class) {
  234. ClassInfo *ti = classes.getptr(p_class);
  235. ERR_FAIL_NULL_V_MSG(ti, StringName(), "Cannot get class '" + String(p_class) + "'.");
  236. return ti->inherits;
  237. }
  238. StringName ClassDB::get_parent_class(const StringName &p_class) {
  239. OBJTYPE_RLOCK;
  240. return _get_parent_class(p_class);
  241. }
  242. ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) {
  243. OBJTYPE_RLOCK;
  244. ClassInfo *ti = classes.getptr(p_class);
  245. ERR_FAIL_NULL_V_MSG(ti, API_NONE, "Cannot get class '" + String(p_class) + "'.");
  246. return ti->api;
  247. }
  248. uint32_t ClassDB::get_api_hash(APIType p_api) {
  249. #ifdef DEBUG_METHODS_ENABLED
  250. OBJTYPE_WLOCK;
  251. if (api_hashes_cache.has(p_api)) {
  252. return api_hashes_cache[p_api];
  253. }
  254. uint64_t hash = hash_murmur3_one_64(HashMapHasherDefault::hash(VERSION_FULL_CONFIG));
  255. List<StringName> class_list;
  256. for (const KeyValue<StringName, ClassInfo> &E : classes) {
  257. class_list.push_back(E.key);
  258. }
  259. // Must be alphabetically sorted for hash to compute.
  260. class_list.sort_custom<StringName::AlphCompare>();
  261. for (const StringName &E : class_list) {
  262. ClassInfo *t = classes.getptr(E);
  263. ERR_FAIL_NULL_V_MSG(t, 0, "Cannot get class '" + String(E) + "'.");
  264. if (t->api != p_api || !t->exposed) {
  265. continue;
  266. }
  267. hash = hash_murmur3_one_64(t->name.hash(), hash);
  268. hash = hash_murmur3_one_64(t->inherits.hash(), hash);
  269. { //methods
  270. List<StringName> snames;
  271. for (const KeyValue<StringName, MethodBind *> &F : t->method_map) {
  272. String name = F.key.operator String();
  273. ERR_CONTINUE(name.is_empty());
  274. if (name[0] == '_') {
  275. continue; // Ignore non-virtual methods that start with an underscore
  276. }
  277. snames.push_back(F.key);
  278. }
  279. snames.sort_custom<StringName::AlphCompare>();
  280. for (const StringName &F : snames) {
  281. MethodBind *mb = t->method_map[F];
  282. hash = hash_murmur3_one_64(mb->get_name().hash(), hash);
  283. hash = hash_murmur3_one_64(mb->get_argument_count(), hash);
  284. hash = hash_murmur3_one_64(mb->get_argument_type(-1), hash); //return
  285. for (int i = 0; i < mb->get_argument_count(); i++) {
  286. const PropertyInfo info = mb->get_argument_info(i);
  287. hash = hash_murmur3_one_64(info.type, hash);
  288. hash = hash_murmur3_one_64(info.name.hash(), hash);
  289. hash = hash_murmur3_one_64(info.hint, hash);
  290. hash = hash_murmur3_one_64(info.hint_string.hash(), hash);
  291. }
  292. hash = hash_murmur3_one_64(mb->get_default_argument_count(), hash);
  293. for (int i = 0; i < mb->get_argument_count(); i++) {
  294. if (mb->has_default_argument(i)) {
  295. Variant da = mb->get_default_argument(i);
  296. hash = hash_murmur3_one_64(da.hash(), hash);
  297. }
  298. }
  299. hash = hash_murmur3_one_64(mb->get_hint_flags(), hash);
  300. }
  301. }
  302. { //constants
  303. List<StringName> snames;
  304. for (const KeyValue<StringName, int64_t> &F : t->constant_map) {
  305. snames.push_back(F.key);
  306. }
  307. snames.sort_custom<StringName::AlphCompare>();
  308. for (const StringName &F : snames) {
  309. hash = hash_murmur3_one_64(F.hash(), hash);
  310. hash = hash_murmur3_one_64(t->constant_map[F], hash);
  311. }
  312. }
  313. { //signals
  314. List<StringName> snames;
  315. for (const KeyValue<StringName, MethodInfo> &F : t->signal_map) {
  316. snames.push_back(F.key);
  317. }
  318. snames.sort_custom<StringName::AlphCompare>();
  319. for (const StringName &F : snames) {
  320. MethodInfo &mi = t->signal_map[F];
  321. hash = hash_murmur3_one_64(F.hash(), hash);
  322. for (int i = 0; i < mi.arguments.size(); i++) {
  323. hash = hash_murmur3_one_64(mi.arguments[i].type, hash);
  324. }
  325. }
  326. }
  327. { //properties
  328. List<StringName> snames;
  329. for (const KeyValue<StringName, PropertySetGet> &F : t->property_setget) {
  330. snames.push_back(F.key);
  331. }
  332. snames.sort_custom<StringName::AlphCompare>();
  333. for (const StringName &F : snames) {
  334. PropertySetGet *psg = t->property_setget.getptr(F);
  335. ERR_FAIL_NULL_V(psg, 0);
  336. hash = hash_murmur3_one_64(F.hash(), hash);
  337. hash = hash_murmur3_one_64(psg->setter.hash(), hash);
  338. hash = hash_murmur3_one_64(psg->getter.hash(), hash);
  339. }
  340. }
  341. //property list
  342. for (const PropertyInfo &F : t->property_list) {
  343. hash = hash_murmur3_one_64(F.name.hash(), hash);
  344. hash = hash_murmur3_one_64(F.type, hash);
  345. hash = hash_murmur3_one_64(F.hint, hash);
  346. hash = hash_murmur3_one_64(F.hint_string.hash(), hash);
  347. hash = hash_murmur3_one_64(F.usage, hash);
  348. }
  349. }
  350. hash = hash_fmix32(hash);
  351. // Extension API changes at runtime; let's just not cache them by now.
  352. if (p_api != API_EXTENSION && p_api != API_EDITOR_EXTENSION) {
  353. api_hashes_cache[p_api] = hash;
  354. }
  355. return hash;
  356. #else
  357. return 0;
  358. #endif
  359. }
  360. bool ClassDB::class_exists(const StringName &p_class) {
  361. OBJTYPE_RLOCK;
  362. return classes.has(p_class);
  363. }
  364. void ClassDB::add_compatibility_class(const StringName &p_class, const StringName &p_fallback) {
  365. OBJTYPE_WLOCK;
  366. compat_classes[p_class] = p_fallback;
  367. }
  368. StringName ClassDB::get_compatibility_class(const StringName &p_class) {
  369. if (compat_classes.has(p_class)) {
  370. return compat_classes[p_class];
  371. }
  372. return StringName();
  373. }
  374. Object *ClassDB::_instantiate_internal(const StringName &p_class, bool p_require_real_class) {
  375. ClassInfo *ti;
  376. {
  377. OBJTYPE_RLOCK;
  378. ti = classes.getptr(p_class);
  379. if (!ti || ti->disabled || !ti->creation_func || (ti->gdextension && !ti->gdextension->create_instance)) {
  380. if (compat_classes.has(p_class)) {
  381. ti = classes.getptr(compat_classes[p_class]);
  382. }
  383. }
  384. ERR_FAIL_NULL_V_MSG(ti, nullptr, "Cannot get class '" + String(p_class) + "'.");
  385. ERR_FAIL_COND_V_MSG(ti->disabled, nullptr, "Class '" + String(p_class) + "' is disabled.");
  386. ERR_FAIL_NULL_V_MSG(ti->creation_func, nullptr, "Class '" + String(p_class) + "' or its base class cannot be instantiated.");
  387. }
  388. #ifdef TOOLS_ENABLED
  389. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  390. ERR_PRINT("Class '" + String(p_class) + "' can only be instantiated by editor.");
  391. return nullptr;
  392. }
  393. #endif
  394. if (ti->gdextension && ti->gdextension->create_instance) {
  395. ObjectGDExtension *extension = ti->gdextension;
  396. #ifdef TOOLS_ENABLED
  397. if (!p_require_real_class && ti->is_runtime && Engine::get_singleton()->is_editor_hint()) {
  398. extension = get_placeholder_extension(ti->name);
  399. }
  400. #endif
  401. Object *obj = (Object *)extension->create_instance(extension->class_userdata);
  402. #ifdef TOOLS_ENABLED
  403. if (extension->track_instance) {
  404. extension->track_instance(extension->tracking_userdata, obj);
  405. }
  406. #endif
  407. return obj;
  408. } else {
  409. #ifdef TOOLS_ENABLED
  410. if (!p_require_real_class && ti->is_runtime && Engine::get_singleton()->is_editor_hint()) {
  411. if (!ti->inherits_ptr || !ti->inherits_ptr->creation_func) {
  412. ERR_PRINT_ONCE(vformat("Cannot make a placeholder instance of runtime class %s because its parent cannot be constructed.", ti->name));
  413. } else {
  414. ObjectGDExtension *extension = get_placeholder_extension(ti->name);
  415. return (Object *)extension->create_instance(extension->class_userdata);
  416. }
  417. }
  418. #endif
  419. return ti->creation_func();
  420. }
  421. }
  422. Object *ClassDB::instantiate(const StringName &p_class) {
  423. return _instantiate_internal(p_class);
  424. }
  425. Object *ClassDB::instantiate_no_placeholders(const StringName &p_class) {
  426. return _instantiate_internal(p_class, true);
  427. }
  428. #ifdef TOOLS_ENABLED
  429. ObjectGDExtension *ClassDB::get_placeholder_extension(const StringName &p_class) {
  430. ObjectGDExtension *placeholder_extension = placeholder_extensions.getptr(p_class);
  431. if (placeholder_extension) {
  432. return placeholder_extension;
  433. }
  434. ClassInfo *ti;
  435. {
  436. OBJTYPE_RLOCK;
  437. ti = classes.getptr(p_class);
  438. if (!ti || ti->disabled || !ti->creation_func || (ti->gdextension && !ti->gdextension->create_instance)) {
  439. if (compat_classes.has(p_class)) {
  440. ti = classes.getptr(compat_classes[p_class]);
  441. }
  442. }
  443. ERR_FAIL_NULL_V_MSG(ti, nullptr, "Cannot get class '" + String(p_class) + "'.");
  444. ERR_FAIL_COND_V_MSG(ti->disabled, nullptr, "Class '" + String(p_class) + "' is disabled.");
  445. }
  446. // Make a "fake" extension to act as a placeholder.
  447. placeholder_extensions[p_class] = ObjectGDExtension();
  448. placeholder_extension = placeholder_extensions.getptr(p_class);
  449. placeholder_extension->is_runtime = true;
  450. placeholder_extension->is_placeholder = true;
  451. if (ti->gdextension) {
  452. placeholder_extension->library = ti->gdextension->library;
  453. placeholder_extension->parent = ti->gdextension->parent;
  454. placeholder_extension->children = ti->gdextension->children;
  455. placeholder_extension->parent_class_name = ti->gdextension->parent_class_name;
  456. placeholder_extension->class_name = ti->gdextension->class_name;
  457. placeholder_extension->editor_class = ti->gdextension->editor_class;
  458. placeholder_extension->reloadable = ti->gdextension->reloadable;
  459. placeholder_extension->is_virtual = ti->gdextension->is_virtual;
  460. placeholder_extension->is_abstract = ti->gdextension->is_abstract;
  461. placeholder_extension->is_exposed = ti->gdextension->is_exposed;
  462. placeholder_extension->tracking_userdata = ti->gdextension->tracking_userdata;
  463. placeholder_extension->track_instance = ti->gdextension->track_instance;
  464. placeholder_extension->untrack_instance = ti->gdextension->untrack_instance;
  465. } else {
  466. placeholder_extension->library = nullptr;
  467. placeholder_extension->parent = nullptr;
  468. placeholder_extension->parent_class_name = ti->inherits;
  469. placeholder_extension->class_name = ti->name;
  470. placeholder_extension->editor_class = ti->api == API_EDITOR;
  471. placeholder_extension->reloadable = false;
  472. placeholder_extension->is_virtual = ti->is_virtual;
  473. placeholder_extension->is_abstract = false;
  474. placeholder_extension->is_exposed = ti->exposed;
  475. }
  476. placeholder_extension->set = &PlaceholderExtensionInstance::placeholder_instance_set;
  477. placeholder_extension->get = &PlaceholderExtensionInstance::placeholder_instance_get;
  478. placeholder_extension->get_property_list = &PlaceholderExtensionInstance::placeholder_instance_get_property_list;
  479. placeholder_extension->free_property_list = &PlaceholderExtensionInstance::placeholder_instance_free_property_list;
  480. placeholder_extension->property_can_revert = &PlaceholderExtensionInstance::placeholder_instance_property_can_revert;
  481. placeholder_extension->property_get_revert = &PlaceholderExtensionInstance::placeholder_instance_property_get_revert;
  482. placeholder_extension->validate_property = &PlaceholderExtensionInstance::placeholder_instance_validate_property;
  483. placeholder_extension->notification = nullptr;
  484. placeholder_extension->notification2 = &PlaceholderExtensionInstance::placeholder_instance_notification;
  485. placeholder_extension->to_string = &PlaceholderExtensionInstance::placeholder_instance_to_string;
  486. placeholder_extension->reference = &PlaceholderExtensionInstance::placeholder_instance_reference;
  487. placeholder_extension->unreference = &PlaceholderExtensionInstance::placeholder_instance_unreference;
  488. placeholder_extension->get_rid = &PlaceholderExtensionInstance::placeholder_instance_get_rid;
  489. placeholder_extension->class_userdata = ti;
  490. placeholder_extension->create_instance = &PlaceholderExtensionInstance::placeholder_class_create_instance;
  491. placeholder_extension->free_instance = &PlaceholderExtensionInstance::placeholder_class_free_instance;
  492. placeholder_extension->get_virtual = &PlaceholderExtensionInstance::placeholder_class_get_virtual;
  493. placeholder_extension->get_virtual_call_data = nullptr;
  494. placeholder_extension->call_virtual_with_data = nullptr;
  495. placeholder_extension->recreate_instance = &PlaceholderExtensionInstance::placeholder_class_recreate_instance;
  496. return placeholder_extension;
  497. }
  498. #endif
  499. void ClassDB::set_object_extension_instance(Object *p_object, const StringName &p_class, GDExtensionClassInstancePtr p_instance) {
  500. ERR_FAIL_NULL(p_object);
  501. ClassInfo *ti;
  502. {
  503. OBJTYPE_RLOCK;
  504. ti = classes.getptr(p_class);
  505. if (!ti || ti->disabled || !ti->creation_func || (ti->gdextension && !ti->gdextension->create_instance)) {
  506. if (compat_classes.has(p_class)) {
  507. ti = classes.getptr(compat_classes[p_class]);
  508. }
  509. }
  510. ERR_FAIL_NULL_MSG(ti, "Cannot get class '" + String(p_class) + "'.");
  511. ERR_FAIL_COND_MSG(ti->disabled, "Class '" + String(p_class) + "' is disabled.");
  512. ERR_FAIL_NULL_MSG(ti->gdextension, "Class '" + String(p_class) + "' has no native extension.");
  513. }
  514. p_object->_extension = ti->gdextension;
  515. p_object->_extension_instance = p_instance;
  516. }
  517. bool ClassDB::can_instantiate(const StringName &p_class) {
  518. OBJTYPE_RLOCK;
  519. ClassInfo *ti = classes.getptr(p_class);
  520. if (!ti) {
  521. if (!ScriptServer::is_global_class(p_class)) {
  522. ERR_FAIL_V_MSG(false, "Cannot get class '" + String(p_class) + "'.");
  523. }
  524. String path = ScriptServer::get_global_class_path(p_class);
  525. Ref<Script> scr = ResourceLoader::load(path);
  526. return scr.is_valid() && scr->is_valid() && !scr->is_abstract();
  527. }
  528. #ifdef TOOLS_ENABLED
  529. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  530. return false;
  531. }
  532. #endif
  533. return (!ti->disabled && ti->creation_func != nullptr && !(ti->gdextension && !ti->gdextension->create_instance));
  534. }
  535. bool ClassDB::is_virtual(const StringName &p_class) {
  536. OBJTYPE_RLOCK;
  537. ClassInfo *ti = classes.getptr(p_class);
  538. if (!ti) {
  539. if (!ScriptServer::is_global_class(p_class)) {
  540. ERR_FAIL_V_MSG(false, "Cannot get class '" + String(p_class) + "'.");
  541. }
  542. String path = ScriptServer::get_global_class_path(p_class);
  543. Ref<Script> scr = ResourceLoader::load(path);
  544. return scr.is_valid() && scr->is_valid() && scr->is_abstract();
  545. }
  546. #ifdef TOOLS_ENABLED
  547. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  548. return false;
  549. }
  550. #endif
  551. return (!ti->disabled && ti->creation_func != nullptr && !(ti->gdextension && !ti->gdextension->create_instance) && ti->is_virtual);
  552. }
  553. void ClassDB::_add_class2(const StringName &p_class, const StringName &p_inherits) {
  554. OBJTYPE_WLOCK;
  555. const StringName &name = p_class;
  556. ERR_FAIL_COND_MSG(classes.has(name), "Class '" + String(p_class) + "' already exists.");
  557. classes[name] = ClassInfo();
  558. ClassInfo &ti = classes[name];
  559. ti.name = name;
  560. ti.inherits = p_inherits;
  561. ti.api = current_api;
  562. if (ti.inherits) {
  563. ERR_FAIL_COND(!classes.has(ti.inherits)); //it MUST be registered.
  564. ti.inherits_ptr = &classes[ti.inherits];
  565. } else {
  566. ti.inherits_ptr = nullptr;
  567. }
  568. }
  569. static MethodInfo info_from_bind(MethodBind *p_method) {
  570. MethodInfo minfo;
  571. minfo.name = p_method->get_name();
  572. minfo.id = p_method->get_method_id();
  573. for (int i = 0; i < p_method->get_argument_count(); i++) {
  574. minfo.arguments.push_back(p_method->get_argument_info(i));
  575. }
  576. minfo.return_val = p_method->get_return_info();
  577. minfo.flags = p_method->get_hint_flags();
  578. for (int i = 0; i < p_method->get_argument_count(); i++) {
  579. if (p_method->has_default_argument(i)) {
  580. minfo.default_arguments.push_back(p_method->get_default_argument(i));
  581. }
  582. }
  583. return minfo;
  584. }
  585. void ClassDB::get_method_list(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance, bool p_exclude_from_properties) {
  586. OBJTYPE_RLOCK;
  587. ClassInfo *type = classes.getptr(p_class);
  588. while (type) {
  589. if (type->disabled) {
  590. if (p_no_inheritance) {
  591. break;
  592. }
  593. type = type->inherits_ptr;
  594. continue;
  595. }
  596. #ifdef DEBUG_METHODS_ENABLED
  597. for (const MethodInfo &E : type->virtual_methods) {
  598. p_methods->push_back(E);
  599. }
  600. for (const StringName &E : type->method_order) {
  601. if (p_exclude_from_properties && type->methods_in_properties.has(E)) {
  602. continue;
  603. }
  604. MethodBind *method = type->method_map.get(E);
  605. MethodInfo minfo = info_from_bind(method);
  606. p_methods->push_back(minfo);
  607. }
  608. #else
  609. for (KeyValue<StringName, MethodBind *> &E : type->method_map) {
  610. MethodBind *m = E.value;
  611. MethodInfo minfo = info_from_bind(m);
  612. p_methods->push_back(minfo);
  613. }
  614. #endif
  615. if (p_no_inheritance) {
  616. break;
  617. }
  618. type = type->inherits_ptr;
  619. }
  620. }
  621. void ClassDB::get_method_list_with_compatibility(const StringName &p_class, List<Pair<MethodInfo, uint32_t>> *p_methods, bool p_no_inheritance, bool p_exclude_from_properties) {
  622. OBJTYPE_RLOCK;
  623. ClassInfo *type = classes.getptr(p_class);
  624. while (type) {
  625. if (type->disabled) {
  626. if (p_no_inheritance) {
  627. break;
  628. }
  629. type = type->inherits_ptr;
  630. continue;
  631. }
  632. #ifdef DEBUG_METHODS_ENABLED
  633. for (const MethodInfo &E : type->virtual_methods) {
  634. Pair<MethodInfo, uint32_t> pair(E, 0);
  635. p_methods->push_back(pair);
  636. }
  637. for (const StringName &E : type->method_order) {
  638. if (p_exclude_from_properties && type->methods_in_properties.has(E)) {
  639. continue;
  640. }
  641. MethodBind *method = type->method_map.get(E);
  642. MethodInfo minfo = info_from_bind(method);
  643. Pair<MethodInfo, uint32_t> pair(minfo, method->get_hash());
  644. p_methods->push_back(pair);
  645. }
  646. #else
  647. for (KeyValue<StringName, MethodBind *> &E : type->method_map) {
  648. MethodBind *method = E.value;
  649. MethodInfo minfo = info_from_bind(method);
  650. Pair<MethodInfo, uint32_t> pair(minfo, method->get_hash());
  651. p_methods->push_back(pair);
  652. }
  653. #endif
  654. for (const KeyValue<StringName, LocalVector<MethodBind *, unsigned int, false, false>> &E : type->method_map_compatibility) {
  655. LocalVector<MethodBind *> compat = E.value;
  656. for (MethodBind *method : compat) {
  657. MethodInfo minfo = info_from_bind(method);
  658. Pair<MethodInfo, uint32_t> pair(minfo, method->get_hash());
  659. p_methods->push_back(pair);
  660. }
  661. }
  662. if (p_no_inheritance) {
  663. break;
  664. }
  665. type = type->inherits_ptr;
  666. }
  667. }
  668. bool ClassDB::get_method_info(const StringName &p_class, const StringName &p_method, MethodInfo *r_info, bool p_no_inheritance, bool p_exclude_from_properties) {
  669. OBJTYPE_RLOCK;
  670. ClassInfo *type = classes.getptr(p_class);
  671. while (type) {
  672. if (type->disabled) {
  673. if (p_no_inheritance) {
  674. break;
  675. }
  676. type = type->inherits_ptr;
  677. continue;
  678. }
  679. #ifdef DEBUG_METHODS_ENABLED
  680. MethodBind **method = type->method_map.getptr(p_method);
  681. if (method && *method) {
  682. if (r_info != nullptr) {
  683. MethodInfo minfo = info_from_bind(*method);
  684. *r_info = minfo;
  685. }
  686. return true;
  687. } else if (type->virtual_methods_map.has(p_method)) {
  688. if (r_info) {
  689. *r_info = type->virtual_methods_map[p_method];
  690. }
  691. return true;
  692. }
  693. #else
  694. if (type->method_map.has(p_method)) {
  695. if (r_info) {
  696. MethodBind *m = type->method_map[p_method];
  697. MethodInfo minfo = info_from_bind(m);
  698. *r_info = minfo;
  699. }
  700. return true;
  701. }
  702. #endif
  703. if (p_no_inheritance) {
  704. break;
  705. }
  706. type = type->inherits_ptr;
  707. }
  708. return false;
  709. }
  710. MethodBind *ClassDB::get_method(const StringName &p_class, const StringName &p_name) {
  711. OBJTYPE_RLOCK;
  712. ClassInfo *type = classes.getptr(p_class);
  713. while (type) {
  714. MethodBind **method = type->method_map.getptr(p_name);
  715. if (method && *method) {
  716. return *method;
  717. }
  718. type = type->inherits_ptr;
  719. }
  720. return nullptr;
  721. }
  722. Vector<uint32_t> ClassDB::get_method_compatibility_hashes(const StringName &p_class, const StringName &p_name) {
  723. OBJTYPE_RLOCK;
  724. ClassInfo *type = classes.getptr(p_class);
  725. while (type) {
  726. if (type->method_map_compatibility.has(p_name)) {
  727. LocalVector<MethodBind *> *c = type->method_map_compatibility.getptr(p_name);
  728. Vector<uint32_t> ret;
  729. for (uint32_t i = 0; i < c->size(); i++) {
  730. ret.push_back((*c)[i]->get_hash());
  731. }
  732. return ret;
  733. }
  734. type = type->inherits_ptr;
  735. }
  736. return Vector<uint32_t>();
  737. }
  738. MethodBind *ClassDB::get_method_with_compatibility(const StringName &p_class, const StringName &p_name, uint64_t p_hash, bool *r_method_exists, bool *r_is_deprecated) {
  739. OBJTYPE_RLOCK;
  740. ClassInfo *type = classes.getptr(p_class);
  741. while (type) {
  742. MethodBind **method = type->method_map.getptr(p_name);
  743. if (method && *method) {
  744. if (r_method_exists) {
  745. *r_method_exists = true;
  746. }
  747. if ((*method)->get_hash() == p_hash) {
  748. return *method;
  749. }
  750. }
  751. LocalVector<MethodBind *> *compat = type->method_map_compatibility.getptr(p_name);
  752. if (compat) {
  753. if (r_method_exists) {
  754. *r_method_exists = true;
  755. }
  756. for (uint32_t i = 0; i < compat->size(); i++) {
  757. if ((*compat)[i]->get_hash() == p_hash) {
  758. if (r_is_deprecated) {
  759. *r_is_deprecated = true;
  760. }
  761. return (*compat)[i];
  762. }
  763. }
  764. }
  765. type = type->inherits_ptr;
  766. }
  767. return nullptr;
  768. }
  769. void ClassDB::bind_integer_constant(const StringName &p_class, const StringName &p_enum, const StringName &p_name, int64_t p_constant, bool p_is_bitfield) {
  770. OBJTYPE_WLOCK;
  771. ClassInfo *type = classes.getptr(p_class);
  772. ERR_FAIL_NULL(type);
  773. if (type->constant_map.has(p_name)) {
  774. ERR_FAIL();
  775. }
  776. type->constant_map[p_name] = p_constant;
  777. String enum_name = p_enum;
  778. if (!enum_name.is_empty()) {
  779. if (enum_name.contains(".")) {
  780. enum_name = enum_name.get_slicec('.', 1);
  781. }
  782. ClassInfo::EnumInfo *constants_list = type->enum_map.getptr(enum_name);
  783. if (constants_list) {
  784. constants_list->constants.push_back(p_name);
  785. constants_list->is_bitfield = p_is_bitfield;
  786. } else {
  787. ClassInfo::EnumInfo new_list;
  788. new_list.is_bitfield = p_is_bitfield;
  789. new_list.constants.push_back(p_name);
  790. type->enum_map[enum_name] = new_list;
  791. }
  792. }
  793. #ifdef DEBUG_METHODS_ENABLED
  794. type->constant_order.push_back(p_name);
  795. #endif
  796. }
  797. void ClassDB::get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance) {
  798. OBJTYPE_RLOCK;
  799. ClassInfo *type = classes.getptr(p_class);
  800. while (type) {
  801. #ifdef DEBUG_METHODS_ENABLED
  802. for (const StringName &E : type->constant_order) {
  803. p_constants->push_back(E);
  804. }
  805. #else
  806. for (const KeyValue<StringName, int64_t> &E : type->constant_map) {
  807. p_constants->push_back(E.key);
  808. }
  809. #endif
  810. if (p_no_inheritance) {
  811. break;
  812. }
  813. type = type->inherits_ptr;
  814. }
  815. }
  816. int64_t ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success) {
  817. OBJTYPE_RLOCK;
  818. ClassInfo *type = classes.getptr(p_class);
  819. while (type) {
  820. int64_t *constant = type->constant_map.getptr(p_name);
  821. if (constant) {
  822. if (p_success) {
  823. *p_success = true;
  824. }
  825. return *constant;
  826. }
  827. type = type->inherits_ptr;
  828. }
  829. if (p_success) {
  830. *p_success = false;
  831. }
  832. return 0;
  833. }
  834. bool ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  835. OBJTYPE_RLOCK;
  836. ClassInfo *type = classes.getptr(p_class);
  837. while (type) {
  838. if (type->constant_map.has(p_name)) {
  839. return true;
  840. }
  841. if (p_no_inheritance) {
  842. return false;
  843. }
  844. type = type->inherits_ptr;
  845. }
  846. return false;
  847. }
  848. StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  849. OBJTYPE_RLOCK;
  850. ClassInfo *type = classes.getptr(p_class);
  851. while (type) {
  852. for (KeyValue<StringName, ClassInfo::EnumInfo> &E : type->enum_map) {
  853. List<StringName> &constants_list = E.value.constants;
  854. const List<StringName>::Element *found = constants_list.find(p_name);
  855. if (found) {
  856. return E.key;
  857. }
  858. }
  859. if (p_no_inheritance) {
  860. break;
  861. }
  862. type = type->inherits_ptr;
  863. }
  864. return StringName();
  865. }
  866. void ClassDB::get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance) {
  867. OBJTYPE_RLOCK;
  868. ClassInfo *type = classes.getptr(p_class);
  869. while (type) {
  870. for (KeyValue<StringName, ClassInfo::EnumInfo> &E : type->enum_map) {
  871. p_enums->push_back(E.key);
  872. }
  873. if (p_no_inheritance) {
  874. break;
  875. }
  876. type = type->inherits_ptr;
  877. }
  878. }
  879. void ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance) {
  880. OBJTYPE_RLOCK;
  881. ClassInfo *type = classes.getptr(p_class);
  882. while (type) {
  883. const ClassInfo::EnumInfo *constants = type->enum_map.getptr(p_enum);
  884. if (constants) {
  885. for (const List<StringName>::Element *E = constants->constants.front(); E; E = E->next()) {
  886. p_constants->push_back(E->get());
  887. }
  888. }
  889. if (p_no_inheritance) {
  890. break;
  891. }
  892. type = type->inherits_ptr;
  893. }
  894. }
  895. void ClassDB::set_method_error_return_values(const StringName &p_class, const StringName &p_method, const Vector<Error> &p_values) {
  896. #ifdef DEBUG_METHODS_ENABLED
  897. OBJTYPE_WLOCK;
  898. ClassInfo *type = classes.getptr(p_class);
  899. ERR_FAIL_NULL(type);
  900. type->method_error_values[p_method] = p_values;
  901. #endif
  902. }
  903. Vector<Error> ClassDB::get_method_error_return_values(const StringName &p_class, const StringName &p_method) {
  904. #ifdef DEBUG_METHODS_ENABLED
  905. OBJTYPE_RLOCK;
  906. ClassInfo *type = classes.getptr(p_class);
  907. ERR_FAIL_NULL_V(type, Vector<Error>());
  908. if (!type->method_error_values.has(p_method)) {
  909. return Vector<Error>();
  910. }
  911. return type->method_error_values[p_method];
  912. #else
  913. return Vector<Error>();
  914. #endif
  915. }
  916. bool ClassDB::has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  917. OBJTYPE_RLOCK;
  918. ClassInfo *type = classes.getptr(p_class);
  919. while (type) {
  920. if (type->enum_map.has(p_name)) {
  921. return true;
  922. }
  923. if (p_no_inheritance) {
  924. return false;
  925. }
  926. type = type->inherits_ptr;
  927. }
  928. return false;
  929. }
  930. bool ClassDB::is_enum_bitfield(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  931. OBJTYPE_RLOCK;
  932. ClassInfo *type = classes.getptr(p_class);
  933. while (type) {
  934. if (type->enum_map.has(p_name) && type->enum_map[p_name].is_bitfield) {
  935. return true;
  936. }
  937. if (p_no_inheritance) {
  938. return false;
  939. }
  940. type = type->inherits_ptr;
  941. }
  942. return false;
  943. }
  944. void ClassDB::add_signal(const StringName &p_class, const MethodInfo &p_signal) {
  945. OBJTYPE_WLOCK;
  946. ClassInfo *type = classes.getptr(p_class);
  947. ERR_FAIL_NULL(type);
  948. StringName sname = p_signal.name;
  949. #ifdef DEBUG_METHODS_ENABLED
  950. ClassInfo *check = type;
  951. while (check) {
  952. ERR_FAIL_COND_MSG(check->signal_map.has(sname), "Class '" + String(p_class) + "' already has signal '" + String(sname) + "'.");
  953. check = check->inherits_ptr;
  954. }
  955. #endif
  956. type->signal_map[sname] = p_signal;
  957. }
  958. void ClassDB::get_signal_list(const StringName &p_class, List<MethodInfo> *p_signals, bool p_no_inheritance) {
  959. OBJTYPE_RLOCK;
  960. ClassInfo *type = classes.getptr(p_class);
  961. ERR_FAIL_NULL(type);
  962. ClassInfo *check = type;
  963. while (check) {
  964. for (KeyValue<StringName, MethodInfo> &E : check->signal_map) {
  965. p_signals->push_back(E.value);
  966. }
  967. if (p_no_inheritance) {
  968. return;
  969. }
  970. check = check->inherits_ptr;
  971. }
  972. }
  973. bool ClassDB::has_signal(const StringName &p_class, const StringName &p_signal, bool p_no_inheritance) {
  974. OBJTYPE_RLOCK;
  975. ClassInfo *type = classes.getptr(p_class);
  976. ClassInfo *check = type;
  977. while (check) {
  978. if (check->signal_map.has(p_signal)) {
  979. return true;
  980. }
  981. if (p_no_inheritance) {
  982. return false;
  983. }
  984. check = check->inherits_ptr;
  985. }
  986. return false;
  987. }
  988. bool ClassDB::get_signal(const StringName &p_class, const StringName &p_signal, MethodInfo *r_signal) {
  989. OBJTYPE_RLOCK;
  990. ClassInfo *type = classes.getptr(p_class);
  991. ClassInfo *check = type;
  992. while (check) {
  993. if (check->signal_map.has(p_signal)) {
  994. if (r_signal) {
  995. *r_signal = check->signal_map[p_signal];
  996. }
  997. return true;
  998. }
  999. check = check->inherits_ptr;
  1000. }
  1001. return false;
  1002. }
  1003. void ClassDB::add_property_group(const StringName &p_class, const String &p_name, const String &p_prefix, int p_indent_depth) {
  1004. OBJTYPE_WLOCK;
  1005. ClassInfo *type = classes.getptr(p_class);
  1006. ERR_FAIL_NULL(type);
  1007. String prefix = p_prefix;
  1008. if (p_indent_depth > 0) {
  1009. prefix = vformat("%s,%d", p_prefix, p_indent_depth);
  1010. }
  1011. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, prefix, PROPERTY_USAGE_GROUP));
  1012. }
  1013. void ClassDB::add_property_subgroup(const StringName &p_class, const String &p_name, const String &p_prefix, int p_indent_depth) {
  1014. OBJTYPE_WLOCK;
  1015. ClassInfo *type = classes.getptr(p_class);
  1016. ERR_FAIL_NULL(type);
  1017. String prefix = p_prefix;
  1018. if (p_indent_depth > 0) {
  1019. prefix = vformat("%s,%d", p_prefix, p_indent_depth);
  1020. }
  1021. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, prefix, PROPERTY_USAGE_SUBGROUP));
  1022. }
  1023. void ClassDB::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) {
  1024. add_property(p_class, PropertyInfo(Variant::INT, p_count_property, PROPERTY_HINT_NONE, "", p_count_usage | PROPERTY_USAGE_ARRAY, vformat("%s,%s", p_label, p_array_element_prefix)), p_count_setter, p_count_getter);
  1025. }
  1026. void ClassDB::add_property_array(const StringName &p_class, const StringName &p_path, const String &p_array_element_prefix) {
  1027. OBJTYPE_WLOCK;
  1028. ClassInfo *type = classes.getptr(p_class);
  1029. ERR_FAIL_NULL(type);
  1030. type->property_list.push_back(PropertyInfo(Variant::NIL, p_path, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY, p_array_element_prefix));
  1031. }
  1032. // NOTE: For implementation simplicity reasons, this method doesn't allow setters to have optional arguments at the end.
  1033. void ClassDB::add_property(const StringName &p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index) {
  1034. lock.read_lock();
  1035. ClassInfo *type = classes.getptr(p_class);
  1036. lock.read_unlock();
  1037. ERR_FAIL_NULL(type);
  1038. MethodBind *mb_set = nullptr;
  1039. if (p_setter) {
  1040. mb_set = get_method(p_class, p_setter);
  1041. #ifdef DEBUG_METHODS_ENABLED
  1042. ERR_FAIL_NULL_MSG(mb_set, "Invalid setter '" + p_class + "::" + p_setter + "' for property '" + p_pinfo.name + "'.");
  1043. int exp_args = 1 + (p_index >= 0 ? 1 : 0);
  1044. ERR_FAIL_COND_MSG(mb_set->get_argument_count() != exp_args, "Invalid function for setter '" + p_class + "::" + p_setter + " for property '" + p_pinfo.name + "'.");
  1045. #endif
  1046. }
  1047. MethodBind *mb_get = nullptr;
  1048. if (p_getter) {
  1049. mb_get = get_method(p_class, p_getter);
  1050. #ifdef DEBUG_METHODS_ENABLED
  1051. ERR_FAIL_NULL_MSG(mb_get, "Invalid getter '" + p_class + "::" + p_getter + "' for property '" + p_pinfo.name + "'.");
  1052. int exp_args = 0 + (p_index >= 0 ? 1 : 0);
  1053. ERR_FAIL_COND_MSG(mb_get->get_argument_count() != exp_args, "Invalid function for getter '" + p_class + "::" + p_getter + "' for property: '" + p_pinfo.name + "'.");
  1054. #endif
  1055. }
  1056. #ifdef DEBUG_METHODS_ENABLED
  1057. ERR_FAIL_COND_MSG(type->property_setget.has(p_pinfo.name), "Object '" + p_class + "' already has property '" + p_pinfo.name + "'.");
  1058. #endif
  1059. OBJTYPE_WLOCK
  1060. type->property_list.push_back(p_pinfo);
  1061. type->property_map[p_pinfo.name] = p_pinfo;
  1062. #ifdef DEBUG_METHODS_ENABLED
  1063. if (mb_get) {
  1064. type->methods_in_properties.insert(p_getter);
  1065. }
  1066. if (mb_set) {
  1067. type->methods_in_properties.insert(p_setter);
  1068. }
  1069. #endif
  1070. PropertySetGet psg;
  1071. psg.setter = p_setter;
  1072. psg.getter = p_getter;
  1073. psg._setptr = mb_set;
  1074. psg._getptr = mb_get;
  1075. psg.index = p_index;
  1076. psg.type = p_pinfo.type;
  1077. type->property_setget[p_pinfo.name] = psg;
  1078. }
  1079. void ClassDB::set_property_default_value(const StringName &p_class, const StringName &p_name, const Variant &p_default) {
  1080. if (!default_values.has(p_class)) {
  1081. default_values[p_class] = HashMap<StringName, Variant>();
  1082. }
  1083. default_values[p_class][p_name] = p_default;
  1084. }
  1085. void ClassDB::add_linked_property(const StringName &p_class, const String &p_property, const String &p_linked_property) {
  1086. #ifdef TOOLS_ENABLED
  1087. OBJTYPE_WLOCK;
  1088. ClassInfo *type = classes.getptr(p_class);
  1089. ERR_FAIL_NULL(type);
  1090. ERR_FAIL_COND(!type->property_map.has(p_property));
  1091. ERR_FAIL_COND(!type->property_map.has(p_linked_property));
  1092. if (!type->linked_properties.has(p_property)) {
  1093. type->linked_properties.insert(p_property, List<StringName>());
  1094. }
  1095. type->linked_properties[p_property].push_back(p_linked_property);
  1096. #endif
  1097. }
  1098. void ClassDB::get_property_list(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator) {
  1099. OBJTYPE_RLOCK;
  1100. ClassInfo *type = classes.getptr(p_class);
  1101. ClassInfo *check = type;
  1102. while (check) {
  1103. for (const PropertyInfo &pi : check->property_list) {
  1104. if (p_validator) {
  1105. // Making a copy as we may modify it.
  1106. PropertyInfo pi_mut = pi;
  1107. p_validator->validate_property(pi_mut);
  1108. p_list->push_back(pi_mut);
  1109. } else {
  1110. p_list->push_back(pi);
  1111. }
  1112. }
  1113. if (p_no_inheritance) {
  1114. return;
  1115. }
  1116. check = check->inherits_ptr;
  1117. }
  1118. }
  1119. void ClassDB::get_linked_properties_info(const StringName &p_class, const StringName &p_property, List<StringName> *r_properties, bool p_no_inheritance) {
  1120. #ifdef TOOLS_ENABLED
  1121. ClassInfo *check = classes.getptr(p_class);
  1122. while (check) {
  1123. if (!check->linked_properties.has(p_property)) {
  1124. return;
  1125. }
  1126. for (const StringName &E : check->linked_properties[p_property]) {
  1127. r_properties->push_back(E);
  1128. }
  1129. if (p_no_inheritance) {
  1130. break;
  1131. }
  1132. check = check->inherits_ptr;
  1133. }
  1134. #endif
  1135. }
  1136. bool ClassDB::get_property_info(const StringName &p_class, const StringName &p_property, PropertyInfo *r_info, bool p_no_inheritance, const Object *p_validator) {
  1137. OBJTYPE_RLOCK;
  1138. ClassInfo *check = classes.getptr(p_class);
  1139. while (check) {
  1140. if (check->property_map.has(p_property)) {
  1141. PropertyInfo pinfo = check->property_map[p_property];
  1142. if (p_validator) {
  1143. p_validator->validate_property(pinfo);
  1144. }
  1145. if (r_info) {
  1146. *r_info = pinfo;
  1147. }
  1148. return true;
  1149. }
  1150. if (p_no_inheritance) {
  1151. break;
  1152. }
  1153. check = check->inherits_ptr;
  1154. }
  1155. return false;
  1156. }
  1157. bool ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid) {
  1158. ERR_FAIL_NULL_V(p_object, false);
  1159. ClassInfo *type = classes.getptr(p_object->get_class_name());
  1160. ClassInfo *check = type;
  1161. while (check) {
  1162. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  1163. if (psg) {
  1164. if (!psg->setter) {
  1165. if (r_valid) {
  1166. *r_valid = false;
  1167. }
  1168. return true; //return true but do nothing
  1169. }
  1170. Callable::CallError ce;
  1171. if (psg->index >= 0) {
  1172. Variant index = psg->index;
  1173. const Variant *arg[2] = { &index, &p_value };
  1174. //p_object->call(psg->setter,arg,2,ce);
  1175. if (psg->_setptr) {
  1176. psg->_setptr->call(p_object, arg, 2, ce);
  1177. } else {
  1178. p_object->callp(psg->setter, arg, 2, ce);
  1179. }
  1180. } else {
  1181. const Variant *arg[1] = { &p_value };
  1182. if (psg->_setptr) {
  1183. psg->_setptr->call(p_object, arg, 1, ce);
  1184. } else {
  1185. p_object->callp(psg->setter, arg, 1, ce);
  1186. }
  1187. }
  1188. if (r_valid) {
  1189. *r_valid = ce.error == Callable::CallError::CALL_OK;
  1190. }
  1191. return true;
  1192. }
  1193. check = check->inherits_ptr;
  1194. }
  1195. return false;
  1196. }
  1197. bool ClassDB::get_property(Object *p_object, const StringName &p_property, Variant &r_value) {
  1198. ERR_FAIL_NULL_V(p_object, false);
  1199. ClassInfo *type = classes.getptr(p_object->get_class_name());
  1200. ClassInfo *check = type;
  1201. while (check) {
  1202. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  1203. if (psg) {
  1204. if (!psg->getter) {
  1205. return true; //return true but do nothing
  1206. }
  1207. if (psg->index >= 0) {
  1208. Variant index = psg->index;
  1209. const Variant *arg[1] = { &index };
  1210. Callable::CallError ce;
  1211. r_value = p_object->callp(psg->getter, arg, 1, ce);
  1212. } else {
  1213. Callable::CallError ce;
  1214. if (psg->_getptr) {
  1215. r_value = psg->_getptr->call(p_object, nullptr, 0, ce);
  1216. } else {
  1217. r_value = p_object->callp(psg->getter, nullptr, 0, ce);
  1218. }
  1219. }
  1220. return true;
  1221. }
  1222. const int64_t *c = check->constant_map.getptr(p_property); //constants count
  1223. if (c) {
  1224. r_value = *c;
  1225. return true;
  1226. }
  1227. if (check->method_map.has(p_property)) { //methods count
  1228. r_value = Callable(p_object, p_property);
  1229. return true;
  1230. }
  1231. if (check->signal_map.has(p_property)) { //signals count
  1232. r_value = Signal(p_object, p_property);
  1233. return true;
  1234. }
  1235. check = check->inherits_ptr;
  1236. }
  1237. // The "free()" method is special, so we assume it exists and return a Callable.
  1238. if (p_property == CoreStringNames::get_singleton()->_free) {
  1239. r_value = Callable(p_object, p_property);
  1240. return true;
  1241. }
  1242. return false;
  1243. }
  1244. int ClassDB::get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  1245. ClassInfo *type = classes.getptr(p_class);
  1246. ClassInfo *check = type;
  1247. while (check) {
  1248. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  1249. if (psg) {
  1250. if (r_is_valid) {
  1251. *r_is_valid = true;
  1252. }
  1253. return psg->index;
  1254. }
  1255. check = check->inherits_ptr;
  1256. }
  1257. if (r_is_valid) {
  1258. *r_is_valid = false;
  1259. }
  1260. return -1;
  1261. }
  1262. Variant::Type ClassDB::get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  1263. ClassInfo *type = classes.getptr(p_class);
  1264. ClassInfo *check = type;
  1265. while (check) {
  1266. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  1267. if (psg) {
  1268. if (r_is_valid) {
  1269. *r_is_valid = true;
  1270. }
  1271. return psg->type;
  1272. }
  1273. check = check->inherits_ptr;
  1274. }
  1275. if (r_is_valid) {
  1276. *r_is_valid = false;
  1277. }
  1278. return Variant::NIL;
  1279. }
  1280. StringName ClassDB::get_property_setter(const StringName &p_class, const StringName &p_property) {
  1281. ClassInfo *type = classes.getptr(p_class);
  1282. ClassInfo *check = type;
  1283. while (check) {
  1284. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  1285. if (psg) {
  1286. return psg->setter;
  1287. }
  1288. check = check->inherits_ptr;
  1289. }
  1290. return StringName();
  1291. }
  1292. StringName ClassDB::get_property_getter(const StringName &p_class, const StringName &p_property) {
  1293. ClassInfo *type = classes.getptr(p_class);
  1294. ClassInfo *check = type;
  1295. while (check) {
  1296. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  1297. if (psg) {
  1298. return psg->getter;
  1299. }
  1300. check = check->inherits_ptr;
  1301. }
  1302. return StringName();
  1303. }
  1304. bool ClassDB::has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance) {
  1305. ClassInfo *type = classes.getptr(p_class);
  1306. ClassInfo *check = type;
  1307. while (check) {
  1308. if (check->property_setget.has(p_property)) {
  1309. return true;
  1310. }
  1311. if (p_no_inheritance) {
  1312. break;
  1313. }
  1314. check = check->inherits_ptr;
  1315. }
  1316. return false;
  1317. }
  1318. void ClassDB::set_method_flags(const StringName &p_class, const StringName &p_method, int p_flags) {
  1319. OBJTYPE_WLOCK;
  1320. ClassInfo *type = classes.getptr(p_class);
  1321. ClassInfo *check = type;
  1322. ERR_FAIL_NULL(check);
  1323. ERR_FAIL_COND(!check->method_map.has(p_method));
  1324. check->method_map[p_method]->set_hint_flags(p_flags);
  1325. }
  1326. bool ClassDB::has_method(const StringName &p_class, const StringName &p_method, bool p_no_inheritance) {
  1327. ClassInfo *type = classes.getptr(p_class);
  1328. ClassInfo *check = type;
  1329. while (check) {
  1330. if (check->method_map.has(p_method)) {
  1331. return true;
  1332. }
  1333. if (p_no_inheritance) {
  1334. return false;
  1335. }
  1336. check = check->inherits_ptr;
  1337. }
  1338. return false;
  1339. }
  1340. void ClassDB::bind_method_custom(const StringName &p_class, MethodBind *p_method) {
  1341. _bind_method_custom(p_class, p_method, false);
  1342. }
  1343. void ClassDB::bind_compatibility_method_custom(const StringName &p_class, MethodBind *p_method) {
  1344. _bind_method_custom(p_class, p_method, true);
  1345. }
  1346. void ClassDB::_bind_compatibility(ClassInfo *type, MethodBind *p_method) {
  1347. if (!type->method_map_compatibility.has(p_method->get_name())) {
  1348. type->method_map_compatibility.insert(p_method->get_name(), LocalVector<MethodBind *>());
  1349. }
  1350. type->method_map_compatibility[p_method->get_name()].push_back(p_method);
  1351. }
  1352. void ClassDB::_bind_method_custom(const StringName &p_class, MethodBind *p_method, bool p_compatibility) {
  1353. OBJTYPE_WLOCK;
  1354. ClassInfo *type = classes.getptr(p_class);
  1355. if (!type) {
  1356. ERR_FAIL_MSG("Couldn't bind custom method '" + p_method->get_name() + "' for instance '" + p_class + "'.");
  1357. }
  1358. if (p_compatibility) {
  1359. _bind_compatibility(type, p_method);
  1360. return;
  1361. }
  1362. if (type->method_map.has(p_method->get_name())) {
  1363. // overloading not supported
  1364. ERR_FAIL_MSG("Method already bound '" + p_class + "::" + p_method->get_name() + "'.");
  1365. }
  1366. #ifdef DEBUG_METHODS_ENABLED
  1367. type->method_order.push_back(p_method->get_name());
  1368. #endif
  1369. type->method_map[p_method->get_name()] = p_method;
  1370. }
  1371. MethodBind *ClassDB::_bind_vararg_method(MethodBind *p_bind, const StringName &p_name, const Vector<Variant> &p_default_args, bool p_compatibility) {
  1372. MethodBind *bind = p_bind;
  1373. bind->set_name(p_name);
  1374. bind->set_default_arguments(p_default_args);
  1375. String instance_type = bind->get_instance_class();
  1376. ClassInfo *type = classes.getptr(instance_type);
  1377. if (!type) {
  1378. memdelete(bind);
  1379. ERR_FAIL_NULL_V(type, nullptr);
  1380. }
  1381. if (p_compatibility) {
  1382. _bind_compatibility(type, bind);
  1383. return bind;
  1384. }
  1385. if (type->method_map.has(p_name)) {
  1386. memdelete(bind);
  1387. // Overloading not supported
  1388. ERR_FAIL_V_MSG(nullptr, "Method already bound: " + instance_type + "::" + p_name + ".");
  1389. }
  1390. type->method_map[p_name] = bind;
  1391. #ifdef DEBUG_METHODS_ENABLED
  1392. // FIXME: <reduz> set_return_type is no longer in MethodBind, so I guess it should be moved to vararg method bind
  1393. //bind->set_return_type("Variant");
  1394. type->method_order.push_back(p_name);
  1395. #endif
  1396. return bind;
  1397. }
  1398. #ifdef DEBUG_METHODS_ENABLED
  1399. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, bool p_compatibility, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount) {
  1400. StringName mdname = method_name.name;
  1401. #else
  1402. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, bool p_compatibility, const char *method_name, const Variant **p_defs, int p_defcount) {
  1403. StringName mdname = StaticCString::create(method_name);
  1404. #endif
  1405. OBJTYPE_WLOCK;
  1406. ERR_FAIL_NULL_V(p_bind, nullptr);
  1407. p_bind->set_name(mdname);
  1408. String instance_type = p_bind->get_instance_class();
  1409. #ifdef DEBUG_ENABLED
  1410. ERR_FAIL_COND_V_MSG(!p_compatibility && has_method(instance_type, mdname), nullptr, "Class " + String(instance_type) + " already has a method " + String(mdname) + ".");
  1411. #endif
  1412. ClassInfo *type = classes.getptr(instance_type);
  1413. if (!type) {
  1414. memdelete(p_bind);
  1415. ERR_FAIL_V_MSG(nullptr, "Couldn't bind method '" + mdname + "' for instance '" + instance_type + "'.");
  1416. }
  1417. if (!p_compatibility && type->method_map.has(mdname)) {
  1418. memdelete(p_bind);
  1419. // overloading not supported
  1420. ERR_FAIL_V_MSG(nullptr, "Method already bound '" + instance_type + "::" + mdname + "'.");
  1421. }
  1422. #ifdef DEBUG_METHODS_ENABLED
  1423. if (method_name.args.size() > p_bind->get_argument_count()) {
  1424. memdelete(p_bind);
  1425. ERR_FAIL_V_MSG(nullptr, "Method definition provides more arguments than the method actually has '" + instance_type + "::" + mdname + "'.");
  1426. }
  1427. p_bind->set_argument_names(method_name.args);
  1428. if (!p_compatibility) {
  1429. type->method_order.push_back(mdname);
  1430. }
  1431. #endif
  1432. if (p_compatibility) {
  1433. _bind_compatibility(type, p_bind);
  1434. } else {
  1435. type->method_map[mdname] = p_bind;
  1436. }
  1437. Vector<Variant> defvals;
  1438. defvals.resize(p_defcount);
  1439. for (int i = 0; i < p_defcount; i++) {
  1440. defvals.write[i] = *p_defs[i];
  1441. }
  1442. p_bind->set_default_arguments(defvals);
  1443. p_bind->set_hint_flags(p_flags);
  1444. return p_bind;
  1445. }
  1446. void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual, const Vector<String> &p_arg_names, bool p_object_core) {
  1447. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1448. OBJTYPE_WLOCK;
  1449. #ifdef DEBUG_METHODS_ENABLED
  1450. MethodInfo mi = p_method;
  1451. if (p_virtual) {
  1452. mi.flags |= METHOD_FLAG_VIRTUAL;
  1453. }
  1454. if (p_object_core) {
  1455. mi.flags |= METHOD_FLAG_OBJECT_CORE;
  1456. }
  1457. if (!p_object_core) {
  1458. if (p_arg_names.size() != mi.arguments.size()) {
  1459. WARN_PRINT("Mismatch argument name count for virtual method: " + String(p_class) + "::" + p_method.name);
  1460. } else {
  1461. for (int i = 0; i < p_arg_names.size(); i++) {
  1462. mi.arguments[i].name = p_arg_names[i];
  1463. }
  1464. }
  1465. }
  1466. if (classes[p_class].virtual_methods_map.has(p_method.name)) {
  1467. // overloading not supported
  1468. ERR_FAIL_MSG("Virtual method already bound '" + String(p_class) + "::" + p_method.name + "'.");
  1469. }
  1470. classes[p_class].virtual_methods.push_back(mi);
  1471. classes[p_class].virtual_methods_map[p_method.name] = mi;
  1472. #endif
  1473. }
  1474. void ClassDB::get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance) {
  1475. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1476. #ifdef DEBUG_METHODS_ENABLED
  1477. ClassInfo *type = classes.getptr(p_class);
  1478. ClassInfo *check = type;
  1479. while (check) {
  1480. for (const MethodInfo &E : check->virtual_methods) {
  1481. p_methods->push_back(E);
  1482. }
  1483. if (p_no_inheritance) {
  1484. return;
  1485. }
  1486. check = check->inherits_ptr;
  1487. }
  1488. #endif
  1489. }
  1490. void ClassDB::add_extension_class_virtual_method(const StringName &p_class, const GDExtensionClassVirtualMethodInfo *p_method_info) {
  1491. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1492. #ifdef DEBUG_METHODS_ENABLED
  1493. PackedStringArray arg_names;
  1494. MethodInfo mi;
  1495. mi.name = *reinterpret_cast<StringName *>(p_method_info->name);
  1496. mi.return_val = PropertyInfo(p_method_info->return_value);
  1497. mi.return_val_metadata = p_method_info->return_value_metadata;
  1498. mi.flags = p_method_info->method_flags;
  1499. for (int i = 0; i < (int)p_method_info->argument_count; i++) {
  1500. PropertyInfo arg(p_method_info->arguments[i]);
  1501. mi.arguments.push_back(arg);
  1502. mi.arguments_metadata.push_back(p_method_info->arguments_metadata[i]);
  1503. arg_names.push_back(arg.name);
  1504. }
  1505. add_virtual_method(p_class, mi, true, arg_names);
  1506. #endif
  1507. }
  1508. void ClassDB::set_class_enabled(const StringName &p_class, bool p_enable) {
  1509. OBJTYPE_WLOCK;
  1510. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1511. classes[p_class].disabled = !p_enable;
  1512. }
  1513. bool ClassDB::is_class_enabled(const StringName &p_class) {
  1514. OBJTYPE_RLOCK;
  1515. ClassInfo *ti = classes.getptr(p_class);
  1516. if (!ti || !ti->creation_func) {
  1517. if (compat_classes.has(p_class)) {
  1518. ti = classes.getptr(compat_classes[p_class]);
  1519. }
  1520. }
  1521. ERR_FAIL_NULL_V_MSG(ti, false, "Cannot get class '" + String(p_class) + "'.");
  1522. return !ti->disabled;
  1523. }
  1524. bool ClassDB::is_class_exposed(const StringName &p_class) {
  1525. OBJTYPE_RLOCK;
  1526. ClassInfo *ti = classes.getptr(p_class);
  1527. ERR_FAIL_NULL_V_MSG(ti, false, "Cannot get class '" + String(p_class) + "'.");
  1528. return ti->exposed;
  1529. }
  1530. bool ClassDB::is_class_reloadable(const StringName &p_class) {
  1531. OBJTYPE_RLOCK;
  1532. ClassInfo *ti = classes.getptr(p_class);
  1533. ERR_FAIL_NULL_V_MSG(ti, false, "Cannot get class '" + String(p_class) + "'.");
  1534. return ti->reloadable;
  1535. }
  1536. void ClassDB::add_resource_base_extension(const StringName &p_extension, const StringName &p_class) {
  1537. if (resource_base_extensions.has(p_extension)) {
  1538. return;
  1539. }
  1540. resource_base_extensions[p_extension] = p_class;
  1541. }
  1542. void ClassDB::get_resource_base_extensions(List<String> *p_extensions) {
  1543. for (const KeyValue<StringName, StringName> &E : resource_base_extensions) {
  1544. p_extensions->push_back(E.key);
  1545. }
  1546. }
  1547. bool ClassDB::is_resource_extension(const StringName &p_extension) {
  1548. return resource_base_extensions.has(p_extension);
  1549. }
  1550. void ClassDB::get_extensions_for_type(const StringName &p_class, List<String> *p_extensions) {
  1551. for (const KeyValue<StringName, StringName> &E : resource_base_extensions) {
  1552. if (is_parent_class(p_class, E.value) || is_parent_class(E.value, p_class)) {
  1553. p_extensions->push_back(E.key);
  1554. }
  1555. }
  1556. }
  1557. HashMap<StringName, HashMap<StringName, Variant>> ClassDB::default_values;
  1558. HashSet<StringName> ClassDB::default_values_cached;
  1559. Variant ClassDB::class_get_default_property_value(const StringName &p_class, const StringName &p_property, bool *r_valid) {
  1560. if (!default_values_cached.has(p_class)) {
  1561. if (!default_values.has(p_class)) {
  1562. default_values[p_class] = HashMap<StringName, Variant>();
  1563. }
  1564. Object *c = nullptr;
  1565. bool cleanup_c = false;
  1566. if (Engine::get_singleton()->has_singleton(p_class)) {
  1567. c = Engine::get_singleton()->get_singleton_object(p_class);
  1568. cleanup_c = false;
  1569. } else if (ClassDB::can_instantiate(p_class) && !ClassDB::is_virtual(p_class)) { // Keep this condition in sync with doc_tools.cpp get_documentation_default_value.
  1570. c = ClassDB::instantiate_no_placeholders(p_class);
  1571. cleanup_c = true;
  1572. }
  1573. if (c) {
  1574. List<PropertyInfo> plist;
  1575. c->get_property_list(&plist);
  1576. for (const PropertyInfo &E : plist) {
  1577. if (E.usage & (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR)) {
  1578. if (!default_values[p_class].has(E.name)) {
  1579. Variant v = c->get(E.name);
  1580. default_values[p_class][E.name] = v;
  1581. }
  1582. }
  1583. }
  1584. if (cleanup_c) {
  1585. memdelete(c);
  1586. }
  1587. }
  1588. default_values_cached.insert(p_class);
  1589. }
  1590. if (!default_values.has(p_class)) {
  1591. if (r_valid != nullptr) {
  1592. *r_valid = false;
  1593. }
  1594. return Variant();
  1595. }
  1596. if (!default_values[p_class].has(p_property)) {
  1597. if (r_valid != nullptr) {
  1598. *r_valid = false;
  1599. }
  1600. return Variant();
  1601. }
  1602. if (r_valid != nullptr) {
  1603. *r_valid = true;
  1604. }
  1605. Variant var = default_values[p_class][p_property];
  1606. #ifdef DEBUG_ENABLED
  1607. // Some properties may have an instantiated Object as default value,
  1608. // (like Path2D's `curve` used to have), but that's not a good practice.
  1609. // Instead, those properties should use PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT
  1610. // to be auto-instantiated when created in the editor with the following method:
  1611. // EditorNode::get_editor_data().instantiate_object_properties(obj);
  1612. if (var.get_type() == Variant::OBJECT) {
  1613. Object *obj = var.get_validated_object();
  1614. if (obj) {
  1615. WARN_PRINT(vformat("Instantiated %s used as default value for %s's \"%s\" property.", obj->get_class(), p_class, p_property));
  1616. }
  1617. }
  1618. #endif
  1619. return var;
  1620. }
  1621. void ClassDB::register_extension_class(ObjectGDExtension *p_extension) {
  1622. GLOBAL_LOCK_FUNCTION;
  1623. ERR_FAIL_COND_MSG(classes.has(p_extension->class_name), "Class already registered: " + String(p_extension->class_name));
  1624. ERR_FAIL_COND_MSG(!classes.has(p_extension->parent_class_name), "Parent class name for extension class not found: " + String(p_extension->parent_class_name));
  1625. ClassInfo *parent = classes.getptr(p_extension->parent_class_name);
  1626. ClassInfo c;
  1627. c.api = p_extension->editor_class ? API_EDITOR_EXTENSION : API_EXTENSION;
  1628. c.gdextension = p_extension;
  1629. c.name = p_extension->class_name;
  1630. c.is_virtual = p_extension->is_virtual;
  1631. if (!p_extension->is_abstract) {
  1632. // Find the closest ancestor which is either non-abstract or native (or both).
  1633. ClassInfo *concrete_ancestor = parent;
  1634. while (concrete_ancestor->creation_func == nullptr &&
  1635. concrete_ancestor->inherits_ptr != nullptr &&
  1636. concrete_ancestor->gdextension != nullptr) {
  1637. concrete_ancestor = concrete_ancestor->inherits_ptr;
  1638. }
  1639. ERR_FAIL_NULL_MSG(concrete_ancestor->creation_func, "Extension class " + String(p_extension->class_name) + " cannot extend native abstract class " + String(concrete_ancestor->name));
  1640. c.creation_func = concrete_ancestor->creation_func;
  1641. }
  1642. c.inherits = parent->name;
  1643. c.class_ptr = parent->class_ptr;
  1644. c.inherits_ptr = parent;
  1645. c.exposed = p_extension->is_exposed;
  1646. if (c.exposed) {
  1647. // The parent classes should be exposed if it has an exposed child class.
  1648. while (parent && !parent->exposed) {
  1649. parent->exposed = true;
  1650. parent = classes.getptr(parent->name);
  1651. }
  1652. }
  1653. c.reloadable = p_extension->reloadable;
  1654. #ifdef TOOLS_ENABLED
  1655. c.is_runtime = p_extension->is_runtime;
  1656. #endif
  1657. classes[p_extension->class_name] = c;
  1658. }
  1659. void ClassDB::unregister_extension_class(const StringName &p_class, bool p_free_method_binds) {
  1660. ClassInfo *c = classes.getptr(p_class);
  1661. ERR_FAIL_NULL_MSG(c, "Class '" + String(p_class) + "' does not exist.");
  1662. if (p_free_method_binds) {
  1663. for (KeyValue<StringName, MethodBind *> &F : c->method_map) {
  1664. memdelete(F.value);
  1665. }
  1666. }
  1667. classes.erase(p_class);
  1668. default_values_cached.erase(p_class);
  1669. default_values.erase(p_class);
  1670. #ifdef TOOLS_ENABLED
  1671. placeholder_extensions.erase(p_class);
  1672. #endif
  1673. }
  1674. HashMap<StringName, ClassDB::NativeStruct> ClassDB::native_structs;
  1675. void ClassDB::register_native_struct(const StringName &p_name, const String &p_code, uint64_t p_current_size) {
  1676. NativeStruct ns;
  1677. ns.ccode = p_code;
  1678. ns.struct_size = p_current_size;
  1679. native_structs[p_name] = ns;
  1680. }
  1681. void ClassDB::get_native_struct_list(List<StringName> *r_names) {
  1682. for (const KeyValue<StringName, NativeStruct> &E : native_structs) {
  1683. r_names->push_back(E.key);
  1684. }
  1685. }
  1686. String ClassDB::get_native_struct_code(const StringName &p_name) {
  1687. ERR_FAIL_COND_V(!native_structs.has(p_name), String());
  1688. return native_structs[p_name].ccode;
  1689. }
  1690. uint64_t ClassDB::get_native_struct_size(const StringName &p_name) {
  1691. ERR_FAIL_COND_V(!native_structs.has(p_name), 0);
  1692. return native_structs[p_name].struct_size;
  1693. }
  1694. RWLock ClassDB::lock;
  1695. void ClassDB::cleanup_defaults() {
  1696. default_values.clear();
  1697. default_values_cached.clear();
  1698. }
  1699. void ClassDB::cleanup() {
  1700. //OBJTYPE_LOCK; hah not here
  1701. for (KeyValue<StringName, ClassInfo> &E : classes) {
  1702. ClassInfo &ti = E.value;
  1703. for (KeyValue<StringName, MethodBind *> &F : ti.method_map) {
  1704. memdelete(F.value);
  1705. }
  1706. for (KeyValue<StringName, LocalVector<MethodBind *>> &F : ti.method_map_compatibility) {
  1707. for (uint32_t i = 0; i < F.value.size(); i++) {
  1708. memdelete(F.value[i]);
  1709. }
  1710. }
  1711. }
  1712. classes.clear();
  1713. resource_base_extensions.clear();
  1714. compat_classes.clear();
  1715. native_structs.clear();
  1716. }
  1717. //