class_db.cpp 40 KB

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