class_db.cpp 48 KB

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