class_db.cpp 48 KB

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