class_db.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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 (const StringName &E : names) {
  308. ClassInfo *t = classes.getptr(E);
  309. ERR_FAIL_COND_V_MSG(!t, 0, "Cannot get class '" + String(E) + "'.");
  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 (const StringName &F : snames) {
  328. MethodBind *mb = t->method_map[F];
  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 (const StringName &F : snames) {
  356. hash = hash_djb2_one_64(F.hash(), hash);
  357. hash = hash_djb2_one_64(t->constant_map[F], 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 (const StringName &F : snames) {
  368. MethodInfo &mi = t->signal_map[F];
  369. hash = hash_djb2_one_64(F.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 (const StringName &F : snames) {
  383. PropertySetGet *psg = t->property_setget.getptr(F);
  384. ERR_FAIL_COND_V(!psg, 0);
  385. hash = hash_djb2_one_64(F.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 (const PropertyInfo &F : t->property_list) {
  392. hash = hash_djb2_one_64(F.name.hash(), hash);
  393. hash = hash_djb2_one_64(F.type, hash);
  394. hash = hash_djb2_one_64(F.hint, hash);
  395. hash = hash_djb2_one_64(F.hint_string.hash(), hash);
  396. hash = hash_djb2_one_64(F.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. thread_local bool initializing_with_extension = false;
  413. thread_local ObjectNativeExtension *initializing_extension = nullptr;
  414. thread_local GDExtensionClassInstancePtr initializing_extension_instance = nullptr;
  415. void ClassDB::instance_get_native_extension_data(ObjectNativeExtension **r_extension, GDExtensionClassInstancePtr *r_extension_instance) {
  416. if (initializing_with_extension) {
  417. *r_extension = initializing_extension;
  418. *r_extension_instance = initializing_extension_instance;
  419. initializing_with_extension = false;
  420. } else {
  421. *r_extension = nullptr;
  422. *r_extension_instance = nullptr;
  423. }
  424. }
  425. Object *ClassDB::instantiate(const StringName &p_class) {
  426. ClassInfo *ti;
  427. {
  428. OBJTYPE_RLOCK;
  429. ti = classes.getptr(p_class);
  430. if (!ti || ti->disabled || !ti->creation_func || (ti->native_extension && !ti->native_extension->create_instance)) {
  431. if (compat_classes.has(p_class)) {
  432. ti = classes.getptr(compat_classes[p_class]);
  433. }
  434. }
  435. ERR_FAIL_COND_V_MSG(!ti, nullptr, "Cannot get class '" + String(p_class) + "'.");
  436. ERR_FAIL_COND_V_MSG(ti->disabled, nullptr, "Class '" + String(p_class) + "' is disabled.");
  437. ERR_FAIL_COND_V(!ti->creation_func, nullptr);
  438. }
  439. #ifdef TOOLS_ENABLED
  440. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  441. ERR_PRINT("Class '" + String(p_class) + "' can only be instantiated by editor.");
  442. return nullptr;
  443. }
  444. #endif
  445. if (ti->native_extension) {
  446. initializing_with_extension = true;
  447. initializing_extension = ti->native_extension;
  448. initializing_extension_instance = ti->native_extension->create_instance(ti->native_extension->class_userdata);
  449. }
  450. return ti->creation_func();
  451. }
  452. bool ClassDB::can_instantiate(const StringName &p_class) {
  453. OBJTYPE_RLOCK;
  454. ClassInfo *ti = classes.getptr(p_class);
  455. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  456. #ifdef TOOLS_ENABLED
  457. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  458. return false;
  459. }
  460. #endif
  461. return (!ti->disabled && ti->creation_func != nullptr && !(ti->native_extension && !ti->native_extension->create_instance));
  462. }
  463. void ClassDB::_add_class2(const StringName &p_class, const StringName &p_inherits) {
  464. OBJTYPE_WLOCK;
  465. const StringName &name = p_class;
  466. ERR_FAIL_COND_MSG(classes.has(name), "Class '" + String(p_class) + "' already exists.");
  467. classes[name] = ClassInfo();
  468. ClassInfo &ti = classes[name];
  469. ti.name = name;
  470. ti.inherits = p_inherits;
  471. ti.api = current_api;
  472. if (ti.inherits) {
  473. ERR_FAIL_COND(!classes.has(ti.inherits)); //it MUST be registered.
  474. ti.inherits_ptr = &classes[ti.inherits];
  475. } else {
  476. ti.inherits_ptr = nullptr;
  477. }
  478. }
  479. #ifdef DEBUG_METHODS_ENABLED
  480. static MethodInfo info_from_bind(MethodBind *p_method) {
  481. MethodInfo minfo;
  482. minfo.name = p_method->get_name();
  483. minfo.id = p_method->get_method_id();
  484. for (int i = 0; i < p_method->get_argument_count(); i++) {
  485. minfo.arguments.push_back(p_method->get_argument_info(i));
  486. }
  487. minfo.return_val = p_method->get_return_info();
  488. minfo.flags = p_method->get_hint_flags();
  489. for (int i = 0; i < p_method->get_argument_count(); i++) {
  490. if (p_method->has_default_argument(i)) {
  491. minfo.default_arguments.push_back(p_method->get_default_argument(i));
  492. }
  493. }
  494. return minfo;
  495. }
  496. #endif
  497. void ClassDB::get_method_list(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance, bool p_exclude_from_properties) {
  498. OBJTYPE_RLOCK;
  499. ClassInfo *type = classes.getptr(p_class);
  500. while (type) {
  501. if (type->disabled) {
  502. if (p_no_inheritance) {
  503. break;
  504. }
  505. type = type->inherits_ptr;
  506. continue;
  507. }
  508. #ifdef DEBUG_METHODS_ENABLED
  509. for (const MethodInfo &E : type->virtual_methods) {
  510. p_methods->push_back(E);
  511. }
  512. for (const StringName &E : type->method_order) {
  513. if (p_exclude_from_properties && type->methods_in_properties.has(E)) {
  514. continue;
  515. }
  516. MethodBind *method = type->method_map.get(E);
  517. MethodInfo minfo = info_from_bind(method);
  518. p_methods->push_back(minfo);
  519. }
  520. #else
  521. const StringName *K = nullptr;
  522. while ((K = type->method_map.next(K))) {
  523. MethodBind *m = type->method_map[*K];
  524. MethodInfo mi;
  525. mi.name = m->get_name();
  526. p_methods->push_back(mi);
  527. }
  528. #endif
  529. if (p_no_inheritance) {
  530. break;
  531. }
  532. type = type->inherits_ptr;
  533. }
  534. }
  535. bool ClassDB::get_method_info(const StringName &p_class, const StringName &p_method, MethodInfo *r_info, bool p_no_inheritance, bool p_exclude_from_properties) {
  536. OBJTYPE_RLOCK;
  537. ClassInfo *type = classes.getptr(p_class);
  538. while (type) {
  539. if (type->disabled) {
  540. if (p_no_inheritance) {
  541. break;
  542. }
  543. type = type->inherits_ptr;
  544. continue;
  545. }
  546. #ifdef DEBUG_METHODS_ENABLED
  547. MethodBind **method = type->method_map.getptr(p_method);
  548. if (method && *method) {
  549. if (r_info != nullptr) {
  550. MethodInfo minfo = info_from_bind(*method);
  551. *r_info = minfo;
  552. }
  553. return true;
  554. } else if (type->virtual_methods_map.has(p_method)) {
  555. if (r_info) {
  556. *r_info = type->virtual_methods_map[p_method];
  557. }
  558. return true;
  559. }
  560. #else
  561. if (type->method_map.has(p_method)) {
  562. if (r_info) {
  563. MethodBind *m = type->method_map[p_method];
  564. MethodInfo mi;
  565. mi.name = m->get_name();
  566. *r_info = mi;
  567. }
  568. return true;
  569. }
  570. #endif
  571. if (p_no_inheritance) {
  572. break;
  573. }
  574. type = type->inherits_ptr;
  575. }
  576. return false;
  577. }
  578. MethodBind *ClassDB::get_method(const StringName &p_class, const StringName &p_name) {
  579. OBJTYPE_RLOCK;
  580. ClassInfo *type = classes.getptr(p_class);
  581. while (type) {
  582. MethodBind **method = type->method_map.getptr(p_name);
  583. if (method && *method) {
  584. return *method;
  585. }
  586. type = type->inherits_ptr;
  587. }
  588. return nullptr;
  589. }
  590. void ClassDB::bind_integer_constant(const StringName &p_class, const StringName &p_enum, const StringName &p_name, int p_constant) {
  591. OBJTYPE_WLOCK;
  592. ClassInfo *type = classes.getptr(p_class);
  593. ERR_FAIL_COND(!type);
  594. if (type->constant_map.has(p_name)) {
  595. ERR_FAIL();
  596. }
  597. type->constant_map[p_name] = p_constant;
  598. String enum_name = p_enum;
  599. if (enum_name != String()) {
  600. if (enum_name.find(".") != -1) {
  601. enum_name = enum_name.get_slicec('.', 1);
  602. }
  603. List<StringName> *constants_list = type->enum_map.getptr(enum_name);
  604. if (constants_list) {
  605. constants_list->push_back(p_name);
  606. } else {
  607. List<StringName> new_list;
  608. new_list.push_back(p_name);
  609. type->enum_map[enum_name] = new_list;
  610. }
  611. }
  612. #ifdef DEBUG_METHODS_ENABLED
  613. type->constant_order.push_back(p_name);
  614. #endif
  615. }
  616. void ClassDB::get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance) {
  617. OBJTYPE_RLOCK;
  618. ClassInfo *type = classes.getptr(p_class);
  619. while (type) {
  620. #ifdef DEBUG_METHODS_ENABLED
  621. for (const StringName &E : type->constant_order) {
  622. p_constants->push_back(E);
  623. }
  624. #else
  625. const StringName *K = nullptr;
  626. while ((K = type->constant_map.next(K))) {
  627. p_constants->push_back(*K);
  628. }
  629. #endif
  630. if (p_no_inheritance) {
  631. break;
  632. }
  633. type = type->inherits_ptr;
  634. }
  635. }
  636. int ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success) {
  637. OBJTYPE_RLOCK;
  638. ClassInfo *type = classes.getptr(p_class);
  639. while (type) {
  640. int *constant = type->constant_map.getptr(p_name);
  641. if (constant) {
  642. if (p_success) {
  643. *p_success = true;
  644. }
  645. return *constant;
  646. }
  647. type = type->inherits_ptr;
  648. }
  649. if (p_success) {
  650. *p_success = false;
  651. }
  652. return 0;
  653. }
  654. bool ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  655. OBJTYPE_RLOCK;
  656. ClassInfo *type = classes.getptr(p_class);
  657. while (type) {
  658. if (type->constant_map.has(p_name)) {
  659. return true;
  660. }
  661. if (p_no_inheritance) {
  662. return false;
  663. }
  664. type = type->inherits_ptr;
  665. }
  666. return false;
  667. }
  668. StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  669. OBJTYPE_RLOCK;
  670. ClassInfo *type = classes.getptr(p_class);
  671. while (type) {
  672. const StringName *k = nullptr;
  673. while ((k = type->enum_map.next(k))) {
  674. List<StringName> &constants_list = type->enum_map.get(*k);
  675. const List<StringName>::Element *found = constants_list.find(p_name);
  676. if (found) {
  677. return *k;
  678. }
  679. }
  680. if (p_no_inheritance) {
  681. break;
  682. }
  683. type = type->inherits_ptr;
  684. }
  685. return StringName();
  686. }
  687. void ClassDB::get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance) {
  688. OBJTYPE_RLOCK;
  689. ClassInfo *type = classes.getptr(p_class);
  690. while (type) {
  691. const StringName *k = nullptr;
  692. while ((k = type->enum_map.next(k))) {
  693. p_enums->push_back(*k);
  694. }
  695. if (p_no_inheritance) {
  696. break;
  697. }
  698. type = type->inherits_ptr;
  699. }
  700. }
  701. void ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance) {
  702. OBJTYPE_RLOCK;
  703. ClassInfo *type = classes.getptr(p_class);
  704. while (type) {
  705. const List<StringName> *constants = type->enum_map.getptr(p_enum);
  706. if (constants) {
  707. for (const List<StringName>::Element *E = constants->front(); E; E = E->next()) {
  708. p_constants->push_back(E->get());
  709. }
  710. }
  711. if (p_no_inheritance) {
  712. break;
  713. }
  714. type = type->inherits_ptr;
  715. }
  716. }
  717. bool ClassDB::has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  718. OBJTYPE_RLOCK;
  719. ClassInfo *type = classes.getptr(p_class);
  720. while (type) {
  721. if (type->enum_map.has(p_name)) {
  722. return true;
  723. }
  724. if (p_no_inheritance) {
  725. return false;
  726. }
  727. type = type->inherits_ptr;
  728. }
  729. return false;
  730. }
  731. void ClassDB::add_signal(const StringName &p_class, const MethodInfo &p_signal) {
  732. OBJTYPE_WLOCK;
  733. ClassInfo *type = classes.getptr(p_class);
  734. ERR_FAIL_COND(!type);
  735. StringName sname = p_signal.name;
  736. #ifdef DEBUG_METHODS_ENABLED
  737. ClassInfo *check = type;
  738. while (check) {
  739. ERR_FAIL_COND_MSG(check->signal_map.has(sname), "Class '" + String(p_class) + "' already has signal '" + String(sname) + "'.");
  740. check = check->inherits_ptr;
  741. }
  742. #endif
  743. type->signal_map[sname] = p_signal;
  744. }
  745. void ClassDB::get_signal_list(const StringName &p_class, List<MethodInfo> *p_signals, bool p_no_inheritance) {
  746. OBJTYPE_RLOCK;
  747. ClassInfo *type = classes.getptr(p_class);
  748. ERR_FAIL_COND(!type);
  749. ClassInfo *check = type;
  750. while (check) {
  751. const StringName *S = nullptr;
  752. while ((S = check->signal_map.next(S))) {
  753. p_signals->push_back(check->signal_map[*S]);
  754. }
  755. if (p_no_inheritance) {
  756. return;
  757. }
  758. check = check->inherits_ptr;
  759. }
  760. }
  761. bool ClassDB::has_signal(const StringName &p_class, const StringName &p_signal, bool p_no_inheritance) {
  762. OBJTYPE_RLOCK;
  763. ClassInfo *type = classes.getptr(p_class);
  764. ClassInfo *check = type;
  765. while (check) {
  766. if (check->signal_map.has(p_signal)) {
  767. return true;
  768. }
  769. if (p_no_inheritance) {
  770. return false;
  771. }
  772. check = check->inherits_ptr;
  773. }
  774. return false;
  775. }
  776. bool ClassDB::get_signal(const StringName &p_class, const StringName &p_signal, MethodInfo *r_signal) {
  777. OBJTYPE_RLOCK;
  778. ClassInfo *type = classes.getptr(p_class);
  779. ClassInfo *check = type;
  780. while (check) {
  781. if (check->signal_map.has(p_signal)) {
  782. if (r_signal) {
  783. *r_signal = check->signal_map[p_signal];
  784. }
  785. return true;
  786. }
  787. check = check->inherits_ptr;
  788. }
  789. return false;
  790. }
  791. void ClassDB::add_property_group(const StringName &p_class, const String &p_name, const String &p_prefix) {
  792. OBJTYPE_WLOCK;
  793. ClassInfo *type = classes.getptr(p_class);
  794. ERR_FAIL_COND(!type);
  795. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, p_prefix, PROPERTY_USAGE_GROUP));
  796. }
  797. void ClassDB::add_property_subgroup(const StringName &p_class, const String &p_name, const String &p_prefix) {
  798. OBJTYPE_WLOCK;
  799. ClassInfo *type = classes.getptr(p_class);
  800. ERR_FAIL_COND(!type);
  801. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, p_prefix, PROPERTY_USAGE_SUBGROUP));
  802. }
  803. // NOTE: For implementation simplicity reasons, this method doesn't allow setters to have optional arguments at the end.
  804. void ClassDB::add_property(const StringName &p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index) {
  805. lock.read_lock();
  806. ClassInfo *type = classes.getptr(p_class);
  807. lock.read_unlock();
  808. ERR_FAIL_COND(!type);
  809. MethodBind *mb_set = nullptr;
  810. if (p_setter) {
  811. mb_set = get_method(p_class, p_setter);
  812. #ifdef DEBUG_METHODS_ENABLED
  813. ERR_FAIL_COND_MSG(!mb_set, "Invalid setter '" + p_class + "::" + p_setter + "' for property '" + p_pinfo.name + "'.");
  814. int exp_args = 1 + (p_index >= 0 ? 1 : 0);
  815. ERR_FAIL_COND_MSG(mb_set->get_argument_count() != exp_args, "Invalid function for setter '" + p_class + "::" + p_setter + " for property '" + p_pinfo.name + "'.");
  816. #endif
  817. }
  818. MethodBind *mb_get = nullptr;
  819. if (p_getter) {
  820. mb_get = get_method(p_class, p_getter);
  821. #ifdef DEBUG_METHODS_ENABLED
  822. ERR_FAIL_COND_MSG(!mb_get, "Invalid getter '" + p_class + "::" + p_getter + "' for property '" + p_pinfo.name + "'.");
  823. int exp_args = 0 + (p_index >= 0 ? 1 : 0);
  824. ERR_FAIL_COND_MSG(mb_get->get_argument_count() != exp_args, "Invalid function for getter '" + p_class + "::" + p_getter + "' for property: '" + p_pinfo.name + "'.");
  825. #endif
  826. }
  827. #ifdef DEBUG_METHODS_ENABLED
  828. ERR_FAIL_COND_MSG(type->property_setget.has(p_pinfo.name), "Object '" + p_class + "' already has property '" + p_pinfo.name + "'.");
  829. #endif
  830. OBJTYPE_WLOCK
  831. type->property_list.push_back(p_pinfo);
  832. type->property_map[p_pinfo.name] = p_pinfo;
  833. #ifdef DEBUG_METHODS_ENABLED
  834. if (mb_get) {
  835. type->methods_in_properties.insert(p_getter);
  836. }
  837. if (mb_set) {
  838. type->methods_in_properties.insert(p_setter);
  839. }
  840. #endif
  841. PropertySetGet psg;
  842. psg.setter = p_setter;
  843. psg.getter = p_getter;
  844. psg._setptr = mb_set;
  845. psg._getptr = mb_get;
  846. psg.index = p_index;
  847. psg.type = p_pinfo.type;
  848. type->property_setget[p_pinfo.name] = psg;
  849. }
  850. void ClassDB::set_property_default_value(const StringName &p_class, const StringName &p_name, const Variant &p_default) {
  851. if (!default_values.has(p_class)) {
  852. default_values[p_class] = HashMap<StringName, Variant>();
  853. }
  854. default_values[p_class][p_name] = p_default;
  855. }
  856. void ClassDB::get_property_list(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator) {
  857. OBJTYPE_RLOCK;
  858. ClassInfo *type = classes.getptr(p_class);
  859. ClassInfo *check = type;
  860. while (check) {
  861. for (const PropertyInfo &pi : check->property_list) {
  862. if (p_validator) {
  863. // Making a copy as we may modify it.
  864. PropertyInfo pi_mut = pi;
  865. p_validator->_validate_property(pi_mut);
  866. p_list->push_back(pi_mut);
  867. } else {
  868. p_list->push_back(pi);
  869. }
  870. }
  871. if (p_no_inheritance) {
  872. return;
  873. }
  874. check = check->inherits_ptr;
  875. }
  876. }
  877. bool ClassDB::get_property_info(const StringName &p_class, const StringName &p_property, PropertyInfo *r_info, bool p_no_inheritance, const Object *p_validator) {
  878. OBJTYPE_RLOCK;
  879. ClassInfo *check = classes.getptr(p_class);
  880. while (check) {
  881. if (check->property_map.has(p_property)) {
  882. PropertyInfo pinfo = check->property_map[p_property];
  883. if (p_validator) {
  884. p_validator->_validate_property(pinfo);
  885. }
  886. if (r_info) {
  887. *r_info = pinfo;
  888. }
  889. return true;
  890. }
  891. if (p_no_inheritance) {
  892. break;
  893. }
  894. check = check->inherits_ptr;
  895. }
  896. return false;
  897. }
  898. bool ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid) {
  899. ERR_FAIL_NULL_V(p_object, false);
  900. ClassInfo *type = classes.getptr(p_object->get_class_name());
  901. ClassInfo *check = type;
  902. while (check) {
  903. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  904. if (psg) {
  905. if (!psg->setter) {
  906. if (r_valid) {
  907. *r_valid = false;
  908. }
  909. return true; //return true but do nothing
  910. }
  911. Callable::CallError ce;
  912. if (psg->index >= 0) {
  913. Variant index = psg->index;
  914. const Variant *arg[2] = { &index, &p_value };
  915. //p_object->call(psg->setter,arg,2,ce);
  916. if (psg->_setptr) {
  917. psg->_setptr->call(p_object, arg, 2, ce);
  918. } else {
  919. p_object->call(psg->setter, arg, 2, ce);
  920. }
  921. } else {
  922. const Variant *arg[1] = { &p_value };
  923. if (psg->_setptr) {
  924. psg->_setptr->call(p_object, arg, 1, ce);
  925. } else {
  926. p_object->call(psg->setter, arg, 1, ce);
  927. }
  928. }
  929. if (r_valid) {
  930. *r_valid = ce.error == Callable::CallError::CALL_OK;
  931. }
  932. return true;
  933. }
  934. check = check->inherits_ptr;
  935. }
  936. return false;
  937. }
  938. bool ClassDB::get_property(Object *p_object, const StringName &p_property, Variant &r_value) {
  939. ERR_FAIL_NULL_V(p_object, false);
  940. ClassInfo *type = classes.getptr(p_object->get_class_name());
  941. ClassInfo *check = type;
  942. while (check) {
  943. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  944. if (psg) {
  945. if (!psg->getter) {
  946. return true; //return true but do nothing
  947. }
  948. if (psg->index >= 0) {
  949. Variant index = psg->index;
  950. const Variant *arg[1] = { &index };
  951. Callable::CallError ce;
  952. r_value = p_object->call(psg->getter, arg, 1, ce);
  953. } else {
  954. Callable::CallError ce;
  955. if (psg->_getptr) {
  956. r_value = psg->_getptr->call(p_object, nullptr, 0, ce);
  957. } else {
  958. r_value = p_object->call(psg->getter, nullptr, 0, ce);
  959. }
  960. }
  961. return true;
  962. }
  963. const int *c = check->constant_map.getptr(p_property); //constants count
  964. if (c) {
  965. r_value = *c;
  966. return true;
  967. }
  968. if (check->method_map.has(p_property)) { //methods count
  969. r_value = Callable(p_object, p_property);
  970. return true;
  971. }
  972. if (check->signal_map.has(p_property)) { //signals count
  973. r_value = Signal(p_object, p_property);
  974. return true;
  975. }
  976. check = check->inherits_ptr;
  977. }
  978. return false;
  979. }
  980. int ClassDB::get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  981. ClassInfo *type = classes.getptr(p_class);
  982. ClassInfo *check = type;
  983. while (check) {
  984. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  985. if (psg) {
  986. if (r_is_valid) {
  987. *r_is_valid = true;
  988. }
  989. return psg->index;
  990. }
  991. check = check->inherits_ptr;
  992. }
  993. if (r_is_valid) {
  994. *r_is_valid = false;
  995. }
  996. return -1;
  997. }
  998. Variant::Type ClassDB::get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  999. ClassInfo *type = classes.getptr(p_class);
  1000. ClassInfo *check = type;
  1001. while (check) {
  1002. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  1003. if (psg) {
  1004. if (r_is_valid) {
  1005. *r_is_valid = true;
  1006. }
  1007. return psg->type;
  1008. }
  1009. check = check->inherits_ptr;
  1010. }
  1011. if (r_is_valid) {
  1012. *r_is_valid = false;
  1013. }
  1014. return Variant::NIL;
  1015. }
  1016. StringName ClassDB::get_property_setter(const StringName &p_class, const StringName &p_property) {
  1017. ClassInfo *type = classes.getptr(p_class);
  1018. ClassInfo *check = type;
  1019. while (check) {
  1020. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  1021. if (psg) {
  1022. return psg->setter;
  1023. }
  1024. check = check->inherits_ptr;
  1025. }
  1026. return StringName();
  1027. }
  1028. StringName ClassDB::get_property_getter(const StringName &p_class, const StringName &p_property) {
  1029. ClassInfo *type = classes.getptr(p_class);
  1030. ClassInfo *check = type;
  1031. while (check) {
  1032. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  1033. if (psg) {
  1034. return psg->getter;
  1035. }
  1036. check = check->inherits_ptr;
  1037. }
  1038. return StringName();
  1039. }
  1040. bool ClassDB::has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance) {
  1041. ClassInfo *type = classes.getptr(p_class);
  1042. ClassInfo *check = type;
  1043. while (check) {
  1044. if (check->property_setget.has(p_property)) {
  1045. return true;
  1046. }
  1047. if (p_no_inheritance) {
  1048. break;
  1049. }
  1050. check = check->inherits_ptr;
  1051. }
  1052. return false;
  1053. }
  1054. void ClassDB::set_method_flags(const StringName &p_class, const StringName &p_method, int p_flags) {
  1055. OBJTYPE_WLOCK;
  1056. ClassInfo *type = classes.getptr(p_class);
  1057. ClassInfo *check = type;
  1058. ERR_FAIL_COND(!check);
  1059. ERR_FAIL_COND(!check->method_map.has(p_method));
  1060. check->method_map[p_method]->set_hint_flags(p_flags);
  1061. }
  1062. bool ClassDB::has_method(const StringName &p_class, const StringName &p_method, bool p_no_inheritance) {
  1063. ClassInfo *type = classes.getptr(p_class);
  1064. ClassInfo *check = type;
  1065. while (check) {
  1066. if (check->method_map.has(p_method)) {
  1067. return true;
  1068. }
  1069. if (p_no_inheritance) {
  1070. return false;
  1071. }
  1072. check = check->inherits_ptr;
  1073. }
  1074. return false;
  1075. }
  1076. void ClassDB::bind_method_custom(const StringName &p_class, MethodBind *p_method) {
  1077. ClassInfo *type = classes.getptr(p_class);
  1078. if (!type) {
  1079. ERR_FAIL_MSG("Couldn't bind custom method '" + p_method->get_name() + "' for instance '" + p_class + "'.");
  1080. }
  1081. if (type->method_map.has(p_method->get_name())) {
  1082. // overloading not supported
  1083. ERR_FAIL_MSG("Method already bound '" + p_class + "::" + p_method->get_name() + "'.");
  1084. }
  1085. #ifdef DEBUG_METHODS_ENABLED
  1086. type->method_order.push_back(p_method->get_name());
  1087. #endif
  1088. type->method_map[p_method->get_name()] = p_method;
  1089. }
  1090. #ifdef DEBUG_METHODS_ENABLED
  1091. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount) {
  1092. StringName mdname = method_name.name;
  1093. #else
  1094. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const char *method_name, const Variant **p_defs, int p_defcount) {
  1095. StringName mdname = StaticCString::create(method_name);
  1096. #endif
  1097. OBJTYPE_WLOCK;
  1098. ERR_FAIL_COND_V(!p_bind, nullptr);
  1099. p_bind->set_name(mdname);
  1100. String instance_type = p_bind->get_instance_class();
  1101. #ifdef DEBUG_ENABLED
  1102. ERR_FAIL_COND_V_MSG(has_method(instance_type, mdname), nullptr, "Class " + String(instance_type) + " already has a method " + String(mdname) + ".");
  1103. #endif
  1104. ClassInfo *type = classes.getptr(instance_type);
  1105. if (!type) {
  1106. memdelete(p_bind);
  1107. ERR_FAIL_V_MSG(nullptr, "Couldn't bind method '" + mdname + "' for instance '" + instance_type + "'.");
  1108. }
  1109. if (type->method_map.has(mdname)) {
  1110. memdelete(p_bind);
  1111. // overloading not supported
  1112. ERR_FAIL_V_MSG(nullptr, "Method already bound '" + instance_type + "::" + mdname + "'.");
  1113. }
  1114. #ifdef DEBUG_METHODS_ENABLED
  1115. if (method_name.args.size() > p_bind->get_argument_count()) {
  1116. memdelete(p_bind);
  1117. ERR_FAIL_V_MSG(nullptr, "Method definition provides more arguments than the method actually has '" + instance_type + "::" + mdname + "'.");
  1118. }
  1119. p_bind->set_argument_names(method_name.args);
  1120. type->method_order.push_back(mdname);
  1121. #endif
  1122. type->method_map[mdname] = p_bind;
  1123. Vector<Variant> defvals;
  1124. defvals.resize(p_defcount);
  1125. for (int i = 0; i < p_defcount; i++) {
  1126. defvals.write[i] = *p_defs[i];
  1127. }
  1128. p_bind->set_default_arguments(defvals);
  1129. p_bind->set_hint_flags(p_flags);
  1130. return p_bind;
  1131. }
  1132. void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual) {
  1133. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1134. OBJTYPE_WLOCK;
  1135. #ifdef DEBUG_METHODS_ENABLED
  1136. MethodInfo mi = p_method;
  1137. if (p_virtual) {
  1138. mi.flags |= METHOD_FLAG_VIRTUAL;
  1139. }
  1140. classes[p_class].virtual_methods.push_back(mi);
  1141. classes[p_class].virtual_methods_map[p_method.name] = mi;
  1142. #endif
  1143. }
  1144. void ClassDB::get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance) {
  1145. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1146. #ifdef DEBUG_METHODS_ENABLED
  1147. ClassInfo *type = classes.getptr(p_class);
  1148. ClassInfo *check = type;
  1149. while (check) {
  1150. for (const MethodInfo &E : check->virtual_methods) {
  1151. p_methods->push_back(E);
  1152. }
  1153. if (p_no_inheritance) {
  1154. return;
  1155. }
  1156. check = check->inherits_ptr;
  1157. }
  1158. #endif
  1159. }
  1160. void ClassDB::set_class_enabled(const StringName &p_class, bool p_enable) {
  1161. OBJTYPE_WLOCK;
  1162. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1163. classes[p_class].disabled = !p_enable;
  1164. }
  1165. bool ClassDB::is_class_enabled(const StringName &p_class) {
  1166. OBJTYPE_RLOCK;
  1167. ClassInfo *ti = classes.getptr(p_class);
  1168. if (!ti || !ti->creation_func) {
  1169. if (compat_classes.has(p_class)) {
  1170. ti = classes.getptr(compat_classes[p_class]);
  1171. }
  1172. }
  1173. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  1174. return !ti->disabled;
  1175. }
  1176. bool ClassDB::is_class_exposed(const StringName &p_class) {
  1177. OBJTYPE_RLOCK;
  1178. ClassInfo *ti = classes.getptr(p_class);
  1179. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  1180. return ti->exposed;
  1181. }
  1182. StringName ClassDB::get_category(const StringName &p_node) {
  1183. ERR_FAIL_COND_V(!classes.has(p_node), StringName());
  1184. #ifdef DEBUG_ENABLED
  1185. return classes[p_node].category;
  1186. #else
  1187. return StringName();
  1188. #endif
  1189. }
  1190. void ClassDB::add_resource_base_extension(const StringName &p_extension, const StringName &p_class) {
  1191. if (resource_base_extensions.has(p_extension)) {
  1192. return;
  1193. }
  1194. resource_base_extensions[p_extension] = p_class;
  1195. }
  1196. void ClassDB::get_resource_base_extensions(List<String> *p_extensions) {
  1197. const StringName *K = nullptr;
  1198. while ((K = resource_base_extensions.next(K))) {
  1199. p_extensions->push_back(*K);
  1200. }
  1201. }
  1202. bool ClassDB::is_resource_extension(const StringName &p_extension) {
  1203. return resource_base_extensions.has(p_extension);
  1204. }
  1205. void ClassDB::get_extensions_for_type(const StringName &p_class, List<String> *p_extensions) {
  1206. const StringName *K = nullptr;
  1207. while ((K = resource_base_extensions.next(K))) {
  1208. StringName cmp = resource_base_extensions[*K];
  1209. if (is_parent_class(p_class, cmp) || is_parent_class(cmp, p_class)) {
  1210. p_extensions->push_back(*K);
  1211. }
  1212. }
  1213. }
  1214. HashMap<StringName, HashMap<StringName, Variant>> ClassDB::default_values;
  1215. Set<StringName> ClassDB::default_values_cached;
  1216. Variant ClassDB::class_get_default_property_value(const StringName &p_class, const StringName &p_property, bool *r_valid) {
  1217. if (!default_values_cached.has(p_class)) {
  1218. if (!default_values.has(p_class)) {
  1219. default_values[p_class] = HashMap<StringName, Variant>();
  1220. }
  1221. Object *c = nullptr;
  1222. bool cleanup_c = false;
  1223. if (Engine::get_singleton()->has_singleton(p_class)) {
  1224. c = Engine::get_singleton()->get_singleton_object(p_class);
  1225. cleanup_c = false;
  1226. } else if (ClassDB::can_instantiate(p_class)) {
  1227. c = ClassDB::instantiate(p_class);
  1228. cleanup_c = true;
  1229. }
  1230. if (c) {
  1231. List<PropertyInfo> plist;
  1232. c->get_property_list(&plist);
  1233. for (const PropertyInfo &E : plist) {
  1234. if (E.usage & (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR)) {
  1235. if (!default_values[p_class].has(E.name)) {
  1236. Variant v = c->get(E.name);
  1237. default_values[p_class][E.name] = v;
  1238. }
  1239. }
  1240. }
  1241. if (cleanup_c) {
  1242. memdelete(c);
  1243. }
  1244. }
  1245. default_values_cached.insert(p_class);
  1246. }
  1247. if (!default_values.has(p_class)) {
  1248. if (r_valid != nullptr) {
  1249. *r_valid = false;
  1250. }
  1251. return Variant();
  1252. }
  1253. if (!default_values[p_class].has(p_property)) {
  1254. if (r_valid != nullptr) {
  1255. *r_valid = false;
  1256. }
  1257. return Variant();
  1258. }
  1259. if (r_valid != nullptr) {
  1260. *r_valid = true;
  1261. }
  1262. Variant var = default_values[p_class][p_property];
  1263. #ifdef DEBUG_ENABLED
  1264. // Some properties may have an instantiated Object as default value,
  1265. // (like Path2D's `curve` used to have), but that's not a good practice.
  1266. // Instead, those properties should use PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT
  1267. // to be auto-instantiated when created in the editor.
  1268. if (var.get_type() == Variant::OBJECT) {
  1269. Object *obj = var.get_validated_object();
  1270. if (obj) {
  1271. WARN_PRINT(vformat("Instantiated %s used as default value for %s's \"%s\" property.", obj->get_class(), p_class, p_property));
  1272. }
  1273. }
  1274. #endif
  1275. return var;
  1276. }
  1277. void ClassDB::register_extension_class(ObjectNativeExtension *p_extension) {
  1278. GLOBAL_LOCK_FUNCTION;
  1279. ERR_FAIL_COND_MSG(classes.has(p_extension->class_name), "Class already registered: " + String(p_extension->class_name));
  1280. ERR_FAIL_COND_MSG(classes.has(p_extension->parent_class_name), "Parent class name for extension class not found: " + String(p_extension->parent_class_name));
  1281. ClassInfo *parent = classes.getptr(p_extension->parent_class_name);
  1282. ClassInfo c;
  1283. c.api = p_extension->editor_class ? API_EDITOR_EXTENSION : API_EXTENSION;
  1284. c.native_extension = p_extension;
  1285. c.name = p_extension->class_name;
  1286. c.creation_func = parent->creation_func;
  1287. c.inherits = parent->name;
  1288. c.class_ptr = parent->class_ptr;
  1289. c.inherits_ptr = parent;
  1290. classes[p_extension->class_name] = c;
  1291. }
  1292. void ClassDB::unregister_extension_class(const StringName &p_class) {
  1293. ERR_FAIL_COND(!classes.has(p_class));
  1294. classes.erase(p_class);
  1295. }
  1296. RWLock ClassDB::lock;
  1297. void ClassDB::cleanup_defaults() {
  1298. default_values.clear();
  1299. default_values_cached.clear();
  1300. }
  1301. void ClassDB::cleanup() {
  1302. //OBJTYPE_LOCK; hah not here
  1303. const StringName *k = nullptr;
  1304. while ((k = classes.next(k))) {
  1305. ClassInfo &ti = classes[*k];
  1306. const StringName *m = nullptr;
  1307. while ((m = ti.method_map.next(m))) {
  1308. memdelete(ti.method_map[*m]);
  1309. }
  1310. }
  1311. classes.clear();
  1312. resource_base_extensions.clear();
  1313. compat_classes.clear();
  1314. }
  1315. //