class_db.cpp 35 KB

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