class_db.cpp 48 KB

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