class_db.cpp 37 KB

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