class_db.cpp 74 KB

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