class_db.cpp 43 KB

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