class_db.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  1. /*************************************************************************/
  2. /* class_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 = 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. void ClassDB::get_direct_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
  257. OBJTYPE_RLOCK;
  258. const StringName *k = NULL;
  259. while ((k = classes.next(k))) {
  260. if (*k != p_class && get_parent_class(*k) == p_class)
  261. p_classes->push_back(*k);
  262. }
  263. }
  264. StringName ClassDB::get_parent_class_nocheck(const StringName &p_class) {
  265. OBJTYPE_RLOCK;
  266. ClassInfo *ti = classes.getptr(p_class);
  267. if (!ti)
  268. return StringName();
  269. return ti->inherits;
  270. }
  271. StringName ClassDB::get_parent_class(const StringName &p_class) {
  272. OBJTYPE_RLOCK;
  273. ClassInfo *ti = classes.getptr(p_class);
  274. ERR_FAIL_COND_V(!ti, StringName());
  275. return ti->inherits;
  276. }
  277. ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) {
  278. OBJTYPE_RLOCK;
  279. ClassInfo *ti = classes.getptr(p_class);
  280. ERR_FAIL_COND_V(!ti, API_NONE);
  281. return ti->api;
  282. }
  283. uint64_t ClassDB::get_api_hash(APIType p_api) {
  284. OBJTYPE_RLOCK;
  285. #ifdef DEBUG_METHODS_ENABLED
  286. uint64_t hash = hash_djb2_one_64(HashMapHasherDefault::hash(VERSION_FULL_CONFIG));
  287. List<StringName> names;
  288. const StringName *k = NULL;
  289. while ((k = classes.next(k))) {
  290. names.push_back(*k);
  291. }
  292. //must be alphabetically sorted for hash to compute
  293. names.sort_custom<StringName::AlphCompare>();
  294. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  295. ClassInfo *t = classes.getptr(E->get());
  296. ERR_FAIL_COND_V(!t, 0);
  297. if (t->api != p_api || !t->exposed)
  298. continue;
  299. hash = hash_djb2_one_64(t->name.hash(), hash);
  300. hash = hash_djb2_one_64(t->inherits.hash(), hash);
  301. { //methods
  302. List<StringName> snames;
  303. k = NULL;
  304. while ((k = t->method_map.next(k))) {
  305. snames.push_back(*k);
  306. }
  307. snames.sort_custom<StringName::AlphCompare>();
  308. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  309. MethodBind *mb = t->method_map[F->get()];
  310. hash = hash_djb2_one_64(mb->get_name().hash(), hash);
  311. hash = hash_djb2_one_64(mb->get_argument_count(), hash);
  312. hash = hash_djb2_one_64(mb->get_argument_type(-1), hash); //return
  313. for (int i = 0; i < mb->get_argument_count(); i++) {
  314. const PropertyInfo info = mb->get_argument_info(i);
  315. hash = hash_djb2_one_64(info.type, hash);
  316. hash = hash_djb2_one_64(info.name.hash(), hash);
  317. hash = hash_djb2_one_64(info.hint, hash);
  318. hash = hash_djb2_one_64(info.hint_string.hash(), hash);
  319. }
  320. hash = hash_djb2_one_64(mb->get_default_argument_count(), hash);
  321. for (int i = 0; i < mb->get_default_argument_count(); i++) {
  322. //hash should not change, i hope for tis
  323. Variant da = mb->get_default_argument(i);
  324. hash = hash_djb2_one_64(da.hash(), hash);
  325. }
  326. hash = hash_djb2_one_64(mb->get_hint_flags(), hash);
  327. }
  328. }
  329. { //constants
  330. List<StringName> snames;
  331. k = NULL;
  332. while ((k = t->constant_map.next(k))) {
  333. snames.push_back(*k);
  334. }
  335. snames.sort_custom<StringName::AlphCompare>();
  336. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  337. hash = hash_djb2_one_64(F->get().hash(), hash);
  338. hash = hash_djb2_one_64(t->constant_map[F->get()], hash);
  339. }
  340. }
  341. { //signals
  342. List<StringName> snames;
  343. k = NULL;
  344. while ((k = t->signal_map.next(k))) {
  345. snames.push_back(*k);
  346. }
  347. snames.sort_custom<StringName::AlphCompare>();
  348. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  349. MethodInfo &mi = t->signal_map[F->get()];
  350. hash = hash_djb2_one_64(F->get().hash(), hash);
  351. for (int i = 0; i < mi.arguments.size(); i++) {
  352. hash = hash_djb2_one_64(mi.arguments[i].type, hash);
  353. }
  354. }
  355. }
  356. { //properties
  357. List<StringName> snames;
  358. k = NULL;
  359. while ((k = t->property_setget.next(k))) {
  360. snames.push_back(*k);
  361. }
  362. snames.sort_custom<StringName::AlphCompare>();
  363. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  364. PropertySetGet *psg = t->property_setget.getptr(F->get());
  365. ERR_FAIL_COND_V(!psg, 0);
  366. hash = hash_djb2_one_64(F->get().hash(), hash);
  367. hash = hash_djb2_one_64(psg->setter.hash(), hash);
  368. hash = hash_djb2_one_64(psg->getter.hash(), hash);
  369. }
  370. }
  371. //property list
  372. for (List<PropertyInfo>::Element *F = t->property_list.front(); F; F = F->next()) {
  373. hash = hash_djb2_one_64(F->get().name.hash(), hash);
  374. hash = hash_djb2_one_64(F->get().type, hash);
  375. hash = hash_djb2_one_64(F->get().hint, hash);
  376. hash = hash_djb2_one_64(F->get().hint_string.hash(), hash);
  377. hash = hash_djb2_one_64(F->get().usage, hash);
  378. }
  379. }
  380. return hash;
  381. #else
  382. return 0;
  383. #endif
  384. }
  385. bool ClassDB::class_exists(const StringName &p_class) {
  386. OBJTYPE_RLOCK;
  387. return classes.has(p_class);
  388. }
  389. void ClassDB::add_compatibility_class(const StringName &p_class, const StringName &p_fallback) {
  390. OBJTYPE_WLOCK;
  391. compat_classes[p_class] = p_fallback;
  392. }
  393. Object *ClassDB::instance(const StringName &p_class) {
  394. ClassInfo *ti;
  395. {
  396. OBJTYPE_RLOCK;
  397. ti = classes.getptr(p_class);
  398. if (!ti || ti->disabled || !ti->creation_func) {
  399. if (compat_classes.has(p_class)) {
  400. ti = classes.getptr(compat_classes[p_class]);
  401. }
  402. }
  403. ERR_FAIL_COND_V(!ti, NULL);
  404. ERR_FAIL_COND_V(ti->disabled, NULL);
  405. ERR_FAIL_COND_V(!ti->creation_func, NULL);
  406. }
  407. #ifdef TOOLS_ENABLED
  408. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  409. ERR_PRINTS("Class '" + String(p_class) + "' can only be instantiated by editor.");
  410. return NULL;
  411. }
  412. #endif
  413. return ti->creation_func();
  414. }
  415. bool ClassDB::can_instance(const StringName &p_class) {
  416. OBJTYPE_RLOCK;
  417. ClassInfo *ti = classes.getptr(p_class);
  418. ERR_FAIL_COND_V(!ti, false);
  419. #ifdef TOOLS_ENABLED
  420. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  421. return false;
  422. }
  423. #endif
  424. return (!ti->disabled && ti->creation_func != NULL);
  425. }
  426. void ClassDB::_add_class2(const StringName &p_class, const StringName &p_inherits) {
  427. OBJTYPE_WLOCK;
  428. const StringName &name = p_class;
  429. ERR_FAIL_COND(classes.has(name));
  430. classes[name] = ClassInfo();
  431. ClassInfo &ti = classes[name];
  432. ti.name = name;
  433. ti.inherits = p_inherits;
  434. ti.api = current_api;
  435. if (ti.inherits) {
  436. ERR_FAIL_COND(!classes.has(ti.inherits)); //it MUST be registered.
  437. ti.inherits_ptr = &classes[ti.inherits];
  438. } else {
  439. ti.inherits_ptr = NULL;
  440. }
  441. }
  442. void ClassDB::get_method_list(StringName p_class, List<MethodInfo> *p_methods, bool p_no_inheritance, bool p_exclude_from_properties) {
  443. OBJTYPE_RLOCK;
  444. ClassInfo *type = classes.getptr(p_class);
  445. while (type) {
  446. if (type->disabled) {
  447. if (p_no_inheritance)
  448. break;
  449. type = type->inherits_ptr;
  450. continue;
  451. }
  452. #ifdef DEBUG_METHODS_ENABLED
  453. for (List<MethodInfo>::Element *E = type->virtual_methods.front(); E; E = E->next()) {
  454. p_methods->push_back(E->get());
  455. }
  456. for (List<StringName>::Element *E = type->method_order.front(); E; E = E->next()) {
  457. MethodBind *method = type->method_map.get(E->get());
  458. MethodInfo minfo;
  459. minfo.name = E->get();
  460. minfo.id = method->get_method_id();
  461. if (p_exclude_from_properties && type->methods_in_properties.has(minfo.name))
  462. continue;
  463. for (int i = 0; i < method->get_argument_count(); i++) {
  464. //Variant::Type t=method->get_argument_type(i);
  465. minfo.arguments.push_back(method->get_argument_info(i));
  466. }
  467. minfo.return_val = method->get_return_info();
  468. minfo.flags = method->get_hint_flags();
  469. for (int i = 0; i < method->get_argument_count(); i++) {
  470. if (method->has_default_argument(i))
  471. minfo.default_arguments.push_back(method->get_default_argument(i));
  472. }
  473. p_methods->push_back(minfo);
  474. }
  475. #else
  476. const StringName *K = NULL;
  477. while ((K = type->method_map.next(K))) {
  478. MethodBind *m = type->method_map[*K];
  479. MethodInfo mi;
  480. mi.name = m->get_name();
  481. p_methods->push_back(mi);
  482. }
  483. #endif
  484. if (p_no_inheritance)
  485. break;
  486. type = type->inherits_ptr;
  487. }
  488. }
  489. MethodBind *ClassDB::get_method(StringName p_class, StringName p_name) {
  490. OBJTYPE_RLOCK;
  491. ClassInfo *type = classes.getptr(p_class);
  492. while (type) {
  493. MethodBind **method = type->method_map.getptr(p_name);
  494. if (method && *method)
  495. return *method;
  496. type = type->inherits_ptr;
  497. }
  498. return NULL;
  499. }
  500. void ClassDB::bind_integer_constant(const StringName &p_class, const StringName &p_enum, const StringName &p_name, int p_constant) {
  501. OBJTYPE_WLOCK;
  502. ClassInfo *type = classes.getptr(p_class);
  503. ERR_FAIL_COND(!type);
  504. if (type->constant_map.has(p_name)) {
  505. ERR_FAIL();
  506. }
  507. type->constant_map[p_name] = p_constant;
  508. String enum_name = p_enum;
  509. if (enum_name != String()) {
  510. if (enum_name.find(".") != -1) {
  511. enum_name = enum_name.get_slicec('.', 1);
  512. }
  513. List<StringName> *constants_list = type->enum_map.getptr(enum_name);
  514. if (constants_list) {
  515. constants_list->push_back(p_name);
  516. } else {
  517. List<StringName> new_list;
  518. new_list.push_back(p_name);
  519. type->enum_map[enum_name] = new_list;
  520. }
  521. }
  522. #ifdef DEBUG_METHODS_ENABLED
  523. type->constant_order.push_back(p_name);
  524. #endif
  525. }
  526. void ClassDB::get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance) {
  527. OBJTYPE_RLOCK;
  528. ClassInfo *type = classes.getptr(p_class);
  529. while (type) {
  530. #ifdef DEBUG_METHODS_ENABLED
  531. for (List<StringName>::Element *E = type->constant_order.front(); E; E = E->next())
  532. p_constants->push_back(E->get());
  533. #else
  534. const StringName *K = NULL;
  535. while ((K = type->constant_map.next(K))) {
  536. p_constants->push_back(*K);
  537. }
  538. #endif
  539. if (p_no_inheritance)
  540. break;
  541. type = type->inherits_ptr;
  542. }
  543. }
  544. int ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success) {
  545. OBJTYPE_RLOCK;
  546. ClassInfo *type = classes.getptr(p_class);
  547. while (type) {
  548. int *constant = type->constant_map.getptr(p_name);
  549. if (constant) {
  550. if (p_success)
  551. *p_success = true;
  552. return *constant;
  553. }
  554. type = type->inherits_ptr;
  555. }
  556. if (p_success)
  557. *p_success = false;
  558. return 0;
  559. }
  560. StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, 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. List<StringName> &constants_list = type->enum_map.get(*k);
  567. const List<StringName>::Element *found = constants_list.find(p_name);
  568. if (found)
  569. return *k;
  570. }
  571. if (p_no_inheritance)
  572. break;
  573. type = type->inherits_ptr;
  574. }
  575. return StringName();
  576. }
  577. void ClassDB::get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance) {
  578. OBJTYPE_RLOCK;
  579. ClassInfo *type = classes.getptr(p_class);
  580. while (type) {
  581. const StringName *k = NULL;
  582. while ((k = type->enum_map.next(k))) {
  583. p_enums->push_back(*k);
  584. }
  585. if (p_no_inheritance)
  586. break;
  587. type = type->inherits_ptr;
  588. }
  589. }
  590. void ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance) {
  591. OBJTYPE_RLOCK;
  592. ClassInfo *type = classes.getptr(p_class);
  593. while (type) {
  594. const List<StringName> *constants = type->enum_map.getptr(p_enum);
  595. if (constants) {
  596. for (const List<StringName>::Element *E = constants->front(); E; E = E->next()) {
  597. p_constants->push_back(E->get());
  598. }
  599. }
  600. if (p_no_inheritance)
  601. break;
  602. type = type->inherits_ptr;
  603. }
  604. }
  605. void ClassDB::add_signal(StringName p_class, const MethodInfo &p_signal) {
  606. OBJTYPE_WLOCK;
  607. ClassInfo *type = classes.getptr(p_class);
  608. ERR_FAIL_COND(!type);
  609. StringName sname = p_signal.name;
  610. #ifdef DEBUG_METHODS_ENABLED
  611. ClassInfo *check = type;
  612. while (check) {
  613. if (check->signal_map.has(sname)) {
  614. ERR_EXPLAIN("Type " + String(p_class) + " already has signal: " + String(sname));
  615. ERR_FAIL();
  616. }
  617. check = check->inherits_ptr;
  618. }
  619. #endif
  620. type->signal_map[sname] = p_signal;
  621. }
  622. void ClassDB::get_signal_list(StringName p_class, List<MethodInfo> *p_signals, bool p_no_inheritance) {
  623. OBJTYPE_RLOCK;
  624. ClassInfo *type = classes.getptr(p_class);
  625. ERR_FAIL_COND(!type);
  626. ClassInfo *check = type;
  627. while (check) {
  628. const StringName *S = NULL;
  629. while ((S = check->signal_map.next(S))) {
  630. p_signals->push_back(check->signal_map[*S]);
  631. }
  632. if (p_no_inheritance)
  633. return;
  634. check = check->inherits_ptr;
  635. }
  636. }
  637. bool ClassDB::has_signal(StringName p_class, StringName p_signal) {
  638. OBJTYPE_RLOCK;
  639. ClassInfo *type = classes.getptr(p_class);
  640. ClassInfo *check = type;
  641. while (check) {
  642. if (check->signal_map.has(p_signal))
  643. return true;
  644. check = check->inherits_ptr;
  645. }
  646. return false;
  647. }
  648. bool ClassDB::get_signal(StringName p_class, StringName p_signal, MethodInfo *r_signal) {
  649. OBJTYPE_RLOCK;
  650. ClassInfo *type = classes.getptr(p_class);
  651. ClassInfo *check = type;
  652. while (check) {
  653. if (check->signal_map.has(p_signal)) {
  654. if (r_signal) {
  655. *r_signal = check->signal_map[p_signal];
  656. }
  657. return true;
  658. }
  659. check = check->inherits_ptr;
  660. }
  661. return false;
  662. }
  663. void ClassDB::add_property_group(StringName p_class, const String &p_name, const String &p_prefix) {
  664. OBJTYPE_WLOCK;
  665. ClassInfo *type = classes.getptr(p_class);
  666. ERR_FAIL_COND(!type);
  667. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, p_prefix, PROPERTY_USAGE_GROUP));
  668. }
  669. void ClassDB::add_property(StringName p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index) {
  670. lock->read_lock();
  671. ClassInfo *type = classes.getptr(p_class);
  672. lock->read_unlock();
  673. ERR_FAIL_COND(!type);
  674. MethodBind *mb_set = NULL;
  675. if (p_setter) {
  676. mb_set = get_method(p_class, p_setter);
  677. #ifdef DEBUG_METHODS_ENABLED
  678. if (!mb_set) {
  679. ERR_EXPLAIN("Invalid Setter: " + p_class + "::" + p_setter + " for property: " + p_pinfo.name);
  680. ERR_FAIL();
  681. } else {
  682. int exp_args = 1 + (p_index >= 0 ? 1 : 0);
  683. if (mb_set->get_argument_count() != exp_args) {
  684. ERR_EXPLAIN("Invalid Function for Setter: " + p_class + "::" + p_setter + " for property: " + p_pinfo.name);
  685. ERR_FAIL();
  686. }
  687. }
  688. #endif
  689. }
  690. MethodBind *mb_get = NULL;
  691. if (p_getter) {
  692. mb_get = get_method(p_class, p_getter);
  693. #ifdef DEBUG_METHODS_ENABLED
  694. if (!mb_get) {
  695. ERR_EXPLAIN("Invalid Getter: " + p_class + "::" + p_getter + " for property: " + p_pinfo.name);
  696. ERR_FAIL();
  697. } else {
  698. int exp_args = 0 + (p_index >= 0 ? 1 : 0);
  699. if (mb_get->get_argument_count() != exp_args) {
  700. ERR_EXPLAIN("Invalid Function for Getter: " + p_class + "::" + p_getter + " for property: " + p_pinfo.name);
  701. ERR_FAIL();
  702. }
  703. }
  704. #endif
  705. }
  706. #ifdef DEBUG_METHODS_ENABLED
  707. if (type->property_setget.has(p_pinfo.name)) {
  708. ERR_EXPLAIN("Object " + p_class + " already has property: " + p_pinfo.name);
  709. ERR_FAIL();
  710. }
  711. #endif
  712. OBJTYPE_WLOCK
  713. type->property_list.push_back(p_pinfo);
  714. #ifdef DEBUG_METHODS_ENABLED
  715. if (mb_get) {
  716. type->methods_in_properties.insert(p_getter);
  717. }
  718. if (mb_set) {
  719. type->methods_in_properties.insert(p_setter);
  720. }
  721. #endif
  722. PropertySetGet psg;
  723. psg.setter = p_setter;
  724. psg.getter = p_getter;
  725. psg._setptr = mb_set;
  726. psg._getptr = mb_get;
  727. psg.index = p_index;
  728. psg.type = p_pinfo.type;
  729. type->property_setget[p_pinfo.name] = psg;
  730. }
  731. void ClassDB::set_property_default_value(StringName p_class, const StringName &p_name, const Variant &p_default) {
  732. if (!default_values.has(p_class)) {
  733. default_values[p_class] = HashMap<StringName, Variant>();
  734. }
  735. default_values[p_class][p_name] = p_default;
  736. }
  737. void ClassDB::get_property_list(StringName p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator) {
  738. OBJTYPE_RLOCK;
  739. ClassInfo *type = classes.getptr(p_class);
  740. ClassInfo *check = type;
  741. while (check) {
  742. for (List<PropertyInfo>::Element *E = check->property_list.front(); E; E = E->next()) {
  743. if (p_validator) {
  744. PropertyInfo pi = E->get();
  745. p_validator->_validate_property(pi);
  746. p_list->push_back(pi);
  747. } else {
  748. p_list->push_back(E->get());
  749. }
  750. }
  751. if (p_no_inheritance)
  752. return;
  753. check = check->inherits_ptr;
  754. }
  755. }
  756. bool ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid) {
  757. ClassInfo *type = classes.getptr(p_object->get_class_name());
  758. ClassInfo *check = type;
  759. while (check) {
  760. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  761. if (psg) {
  762. if (!psg->setter) {
  763. if (r_valid)
  764. *r_valid = false;
  765. return true; //return true but do nothing
  766. }
  767. Variant::CallError ce;
  768. if (psg->index >= 0) {
  769. Variant index = psg->index;
  770. const Variant *arg[2] = { &index, &p_value };
  771. //p_object->call(psg->setter,arg,2,ce);
  772. if (psg->_setptr) {
  773. psg->_setptr->call(p_object, arg, 2, ce);
  774. } else {
  775. p_object->call(psg->setter, arg, 2, ce);
  776. }
  777. } else {
  778. const Variant *arg[1] = { &p_value };
  779. if (psg->_setptr) {
  780. psg->_setptr->call(p_object, arg, 1, ce);
  781. } else {
  782. p_object->call(psg->setter, arg, 1, ce);
  783. }
  784. }
  785. if (r_valid)
  786. *r_valid = ce.error == Variant::CallError::CALL_OK;
  787. return true;
  788. }
  789. check = check->inherits_ptr;
  790. }
  791. return false;
  792. }
  793. bool ClassDB::get_property(Object *p_object, const StringName &p_property, Variant &r_value) {
  794. ClassInfo *type = classes.getptr(p_object->get_class_name());
  795. ClassInfo *check = type;
  796. while (check) {
  797. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  798. if (psg) {
  799. if (!psg->getter)
  800. return true; //return true but do nothing
  801. if (psg->index >= 0) {
  802. Variant index = psg->index;
  803. const Variant *arg[1] = { &index };
  804. Variant::CallError ce;
  805. r_value = p_object->call(psg->getter, arg, 1, ce);
  806. } else {
  807. Variant::CallError ce;
  808. if (psg->_getptr) {
  809. r_value = psg->_getptr->call(p_object, NULL, 0, ce);
  810. } else {
  811. r_value = p_object->call(psg->getter, NULL, 0, ce);
  812. }
  813. }
  814. return true;
  815. }
  816. const int *c = check->constant_map.getptr(p_property);
  817. if (c) {
  818. r_value = *c;
  819. return true;
  820. }
  821. check = check->inherits_ptr;
  822. }
  823. return false;
  824. }
  825. int ClassDB::get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  826. ClassInfo *type = classes.getptr(p_class);
  827. ClassInfo *check = type;
  828. while (check) {
  829. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  830. if (psg) {
  831. if (r_is_valid)
  832. *r_is_valid = true;
  833. return psg->index;
  834. }
  835. check = check->inherits_ptr;
  836. }
  837. if (r_is_valid)
  838. *r_is_valid = false;
  839. return -1;
  840. }
  841. Variant::Type ClassDB::get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  842. ClassInfo *type = classes.getptr(p_class);
  843. ClassInfo *check = type;
  844. while (check) {
  845. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  846. if (psg) {
  847. if (r_is_valid)
  848. *r_is_valid = true;
  849. return psg->type;
  850. }
  851. check = check->inherits_ptr;
  852. }
  853. if (r_is_valid)
  854. *r_is_valid = false;
  855. return Variant::NIL;
  856. }
  857. StringName ClassDB::get_property_setter(StringName p_class, const StringName &p_property) {
  858. ClassInfo *type = classes.getptr(p_class);
  859. ClassInfo *check = type;
  860. while (check) {
  861. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  862. if (psg) {
  863. return psg->setter;
  864. }
  865. check = check->inherits_ptr;
  866. }
  867. return StringName();
  868. }
  869. StringName ClassDB::get_property_getter(StringName p_class, const StringName &p_property) {
  870. ClassInfo *type = classes.getptr(p_class);
  871. ClassInfo *check = type;
  872. while (check) {
  873. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  874. if (psg) {
  875. return psg->getter;
  876. }
  877. check = check->inherits_ptr;
  878. }
  879. return StringName();
  880. }
  881. bool ClassDB::has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance) {
  882. ClassInfo *type = classes.getptr(p_class);
  883. ClassInfo *check = type;
  884. while (check) {
  885. if (check->property_setget.has(p_property))
  886. return true;
  887. if (p_no_inheritance)
  888. break;
  889. check = check->inherits_ptr;
  890. }
  891. return false;
  892. }
  893. void ClassDB::set_method_flags(StringName p_class, StringName p_method, int p_flags) {
  894. OBJTYPE_WLOCK;
  895. ClassInfo *type = classes.getptr(p_class);
  896. ClassInfo *check = type;
  897. ERR_FAIL_COND(!check);
  898. ERR_FAIL_COND(!check->method_map.has(p_method));
  899. check->method_map[p_method]->set_hint_flags(p_flags);
  900. }
  901. bool ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inheritance) {
  902. ClassInfo *type = classes.getptr(p_class);
  903. ClassInfo *check = type;
  904. while (check) {
  905. if (check->method_map.has(p_method))
  906. return true;
  907. if (p_no_inheritance)
  908. return false;
  909. check = check->inherits_ptr;
  910. }
  911. return false;
  912. }
  913. #ifdef DEBUG_METHODS_ENABLED
  914. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount) {
  915. StringName mdname = method_name.name;
  916. #else
  917. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const char *method_name, const Variant **p_defs, int p_defcount) {
  918. StringName mdname = StaticCString::create(method_name);
  919. #endif
  920. OBJTYPE_WLOCK;
  921. ERR_FAIL_COND_V(!p_bind, NULL);
  922. p_bind->set_name(mdname);
  923. String instance_type = p_bind->get_instance_class();
  924. #ifdef DEBUG_ENABLED
  925. if (has_method(instance_type, mdname)) {
  926. ERR_EXPLAIN("Class " + String(instance_type) + " already has a method " + String(mdname));
  927. ERR_FAIL_V(NULL);
  928. }
  929. #endif
  930. ClassInfo *type = classes.getptr(instance_type);
  931. if (!type) {
  932. ERR_PRINTS("Couldn't bind method '" + mdname + "' for instance: " + instance_type);
  933. memdelete(p_bind);
  934. ERR_FAIL_V(NULL);
  935. }
  936. if (type->method_map.has(mdname)) {
  937. memdelete(p_bind);
  938. // overloading not supported
  939. ERR_EXPLAIN("Method already bound: " + instance_type + "::" + mdname);
  940. ERR_FAIL_V(NULL);
  941. }
  942. #ifdef DEBUG_METHODS_ENABLED
  943. if (method_name.args.size() > p_bind->get_argument_count()) {
  944. memdelete(p_bind);
  945. ERR_EXPLAIN("Method definition provides more arguments than the method actually has: " + instance_type + "::" + mdname);
  946. ERR_FAIL_V(NULL);
  947. }
  948. p_bind->set_argument_names(method_name.args);
  949. type->method_order.push_back(mdname);
  950. #endif
  951. type->method_map[mdname] = p_bind;
  952. Vector<Variant> defvals;
  953. defvals.resize(p_defcount);
  954. for (int i = 0; i < p_defcount; i++) {
  955. defvals.write[i] = *p_defs[p_defcount - i - 1];
  956. }
  957. p_bind->set_default_arguments(defvals);
  958. p_bind->set_hint_flags(p_flags);
  959. return p_bind;
  960. }
  961. void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual) {
  962. ERR_FAIL_COND(!classes.has(p_class));
  963. OBJTYPE_WLOCK;
  964. #ifdef DEBUG_METHODS_ENABLED
  965. MethodInfo mi = p_method;
  966. if (p_virtual)
  967. mi.flags |= METHOD_FLAG_VIRTUAL;
  968. classes[p_class].virtual_methods.push_back(mi);
  969. #endif
  970. }
  971. void ClassDB::get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance) {
  972. ERR_FAIL_COND(!classes.has(p_class));
  973. #ifdef DEBUG_METHODS_ENABLED
  974. ClassInfo *type = classes.getptr(p_class);
  975. ClassInfo *check = type;
  976. while (check) {
  977. for (List<MethodInfo>::Element *E = check->virtual_methods.front(); E; E = E->next()) {
  978. p_methods->push_back(E->get());
  979. }
  980. if (p_no_inheritance)
  981. return;
  982. check = check->inherits_ptr;
  983. }
  984. #endif
  985. }
  986. void ClassDB::set_class_enabled(StringName p_class, bool p_enable) {
  987. OBJTYPE_WLOCK;
  988. ERR_FAIL_COND(!classes.has(p_class));
  989. classes[p_class].disabled = !p_enable;
  990. }
  991. bool ClassDB::is_class_enabled(StringName p_class) {
  992. OBJTYPE_RLOCK;
  993. ClassInfo *ti = classes.getptr(p_class);
  994. if (!ti || !ti->creation_func) {
  995. if (compat_classes.has(p_class)) {
  996. ti = classes.getptr(compat_classes[p_class]);
  997. }
  998. }
  999. ERR_FAIL_COND_V(!ti, false);
  1000. return !ti->disabled;
  1001. }
  1002. bool ClassDB::is_class_exposed(StringName p_class) {
  1003. OBJTYPE_RLOCK;
  1004. ClassInfo *ti = classes.getptr(p_class);
  1005. ERR_FAIL_COND_V(!ti, false);
  1006. return ti->exposed;
  1007. }
  1008. StringName ClassDB::get_category(const StringName &p_node) {
  1009. ERR_FAIL_COND_V(!classes.has(p_node), StringName());
  1010. #ifdef DEBUG_ENABLED
  1011. return classes[p_node].category;
  1012. #else
  1013. return StringName();
  1014. #endif
  1015. }
  1016. void ClassDB::add_resource_base_extension(const StringName &p_extension, const StringName &p_class) {
  1017. if (resource_base_extensions.has(p_extension))
  1018. return;
  1019. resource_base_extensions[p_extension] = p_class;
  1020. }
  1021. void ClassDB::get_resource_base_extensions(List<String> *p_extensions) {
  1022. const StringName *K = NULL;
  1023. while ((K = resource_base_extensions.next(K))) {
  1024. p_extensions->push_back(*K);
  1025. }
  1026. }
  1027. void ClassDB::get_extensions_for_type(const StringName &p_class, List<String> *p_extensions) {
  1028. const StringName *K = NULL;
  1029. while ((K = resource_base_extensions.next(K))) {
  1030. StringName cmp = resource_base_extensions[*K];
  1031. if (is_parent_class(p_class, cmp) || is_parent_class(cmp, p_class))
  1032. p_extensions->push_back(*K);
  1033. }
  1034. }
  1035. HashMap<StringName, HashMap<StringName, Variant> > ClassDB::default_values;
  1036. Set<StringName> ClassDB::default_values_cached;
  1037. Variant ClassDB::class_get_default_property_value(const StringName &p_class, const StringName &p_property, bool *r_valid) {
  1038. if (!default_values_cached.has(p_class)) {
  1039. if (!default_values.has(p_class)) {
  1040. default_values[p_class] = HashMap<StringName, Variant>();
  1041. }
  1042. Object *c = NULL;
  1043. bool cleanup_c = false;
  1044. if (Engine::get_singleton()->has_singleton(p_class)) {
  1045. c = Engine::get_singleton()->get_singleton_object(p_class);
  1046. cleanup_c = false;
  1047. } else if (ClassDB::can_instance(p_class)) {
  1048. c = ClassDB::instance(p_class);
  1049. cleanup_c = true;
  1050. }
  1051. if (c) {
  1052. List<PropertyInfo> plist;
  1053. c->get_property_list(&plist);
  1054. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1055. if (E->get().usage & (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR)) {
  1056. if (!default_values[p_class].has(E->get().name)) {
  1057. Variant v = c->get(E->get().name);
  1058. default_values[p_class][E->get().name] = v;
  1059. }
  1060. }
  1061. }
  1062. if (cleanup_c) {
  1063. memdelete(c);
  1064. }
  1065. }
  1066. default_values_cached.insert(p_class);
  1067. }
  1068. if (!default_values.has(p_class)) {
  1069. if (r_valid != NULL) *r_valid = false;
  1070. return Variant();
  1071. }
  1072. if (!default_values[p_class].has(p_property)) {
  1073. if (r_valid != NULL) *r_valid = false;
  1074. return Variant();
  1075. }
  1076. if (r_valid != NULL) *r_valid = true;
  1077. return default_values[p_class][p_property];
  1078. }
  1079. RWLock *ClassDB::lock = NULL;
  1080. void ClassDB::init() {
  1081. lock = RWLock::create();
  1082. }
  1083. void ClassDB::cleanup_defaults() {
  1084. default_values.clear();
  1085. default_values_cached.clear();
  1086. }
  1087. void ClassDB::cleanup() {
  1088. //OBJTYPE_LOCK; hah not here
  1089. const StringName *k = NULL;
  1090. while ((k = classes.next(k))) {
  1091. ClassInfo &ti = classes[*k];
  1092. const StringName *m = NULL;
  1093. while ((m = ti.method_map.next(m))) {
  1094. memdelete(ti.method_map[*m]);
  1095. }
  1096. }
  1097. classes.clear();
  1098. resource_base_extensions.clear();
  1099. compat_classes.clear();
  1100. memdelete(lock);
  1101. }
  1102. //