class_db.cpp 71 KB

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