class_db.cpp 47 KB

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