class_db.cpp 39 KB

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