class_db.cpp 43 KB

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