class_db.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  1. /*************************************************************************/
  2. /* class_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "class_db.h"
  31. #include "core/config/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. bool ClassDB::_is_parent_class(const StringName &p_class, const StringName &p_inherits) {
  222. if (!classes.has(p_class)) {
  223. return false;
  224. }
  225. StringName inherits = p_class;
  226. while (inherits.operator String().length()) {
  227. if (inherits == p_inherits) {
  228. return true;
  229. }
  230. inherits = _get_parent_class(inherits);
  231. }
  232. return false;
  233. }
  234. bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) {
  235. OBJTYPE_RLOCK;
  236. return _is_parent_class(p_class, p_inherits);
  237. }
  238. void ClassDB::get_class_list(List<StringName> *p_classes) {
  239. OBJTYPE_RLOCK;
  240. const StringName *k = nullptr;
  241. while ((k = classes.next(k))) {
  242. p_classes->push_back(*k);
  243. }
  244. p_classes->sort();
  245. }
  246. void ClassDB::get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
  247. OBJTYPE_RLOCK;
  248. const StringName *k = nullptr;
  249. while ((k = classes.next(k))) {
  250. if (*k != p_class && _is_parent_class(*k, p_class)) {
  251. p_classes->push_back(*k);
  252. }
  253. }
  254. }
  255. void ClassDB::get_direct_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
  256. OBJTYPE_RLOCK;
  257. const StringName *k = nullptr;
  258. while ((k = classes.next(k))) {
  259. if (*k != p_class && _get_parent_class(*k) == p_class) {
  260. p_classes->push_back(*k);
  261. }
  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. }
  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. ClassInfo *ti = classes.getptr(p_class);
  283. ERR_FAIL_COND_V_MSG(!ti, StringName(), "Cannot get class '" + String(p_class) + "'.");
  284. return ti->inherits;
  285. }
  286. StringName ClassDB::get_parent_class(const StringName &p_class) {
  287. OBJTYPE_RLOCK;
  288. return _get_parent_class(p_class);
  289. }
  290. ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) {
  291. OBJTYPE_RLOCK;
  292. ClassInfo *ti = classes.getptr(p_class);
  293. ERR_FAIL_COND_V_MSG(!ti, API_NONE, "Cannot get class '" + String(p_class) + "'.");
  294. return ti->api;
  295. }
  296. uint64_t ClassDB::get_api_hash(APIType p_api) {
  297. OBJTYPE_RLOCK;
  298. #ifdef DEBUG_METHODS_ENABLED
  299. uint64_t hash = hash_djb2_one_64(HashMapHasherDefault::hash(VERSION_FULL_CONFIG));
  300. List<StringName> names;
  301. const StringName *k = nullptr;
  302. while ((k = classes.next(k))) {
  303. names.push_back(*k);
  304. }
  305. //must be alphabetically sorted for hash to compute
  306. names.sort_custom<StringName::AlphCompare>();
  307. for (const StringName &E : names) {
  308. ClassInfo *t = classes.getptr(E);
  309. ERR_FAIL_COND_V_MSG(!t, 0, "Cannot get class '" + String(E) + "'.");
  310. if (t->api != p_api || !t->exposed) {
  311. continue;
  312. }
  313. hash = hash_djb2_one_64(t->name.hash(), hash);
  314. hash = hash_djb2_one_64(t->inherits.hash(), hash);
  315. { //methods
  316. List<StringName> snames;
  317. k = nullptr;
  318. while ((k = t->method_map.next(k))) {
  319. String name = k->operator String();
  320. ERR_CONTINUE(name.is_empty());
  321. if (name[0] == '_') {
  322. continue; // Ignore non-virtual methods that start with an underscore
  323. }
  324. snames.push_back(*k);
  325. }
  326. snames.sort_custom<StringName::AlphCompare>();
  327. for (const StringName &F : snames) {
  328. MethodBind *mb = t->method_map[F];
  329. hash = hash_djb2_one_64(mb->get_name().hash(), hash);
  330. hash = hash_djb2_one_64(mb->get_argument_count(), hash);
  331. hash = hash_djb2_one_64(mb->get_argument_type(-1), hash); //return
  332. for (int i = 0; i < mb->get_argument_count(); i++) {
  333. const PropertyInfo info = mb->get_argument_info(i);
  334. hash = hash_djb2_one_64(info.type, hash);
  335. hash = hash_djb2_one_64(info.name.hash(), hash);
  336. hash = hash_djb2_one_64(info.hint, hash);
  337. hash = hash_djb2_one_64(info.hint_string.hash(), hash);
  338. }
  339. hash = hash_djb2_one_64(mb->get_default_argument_count(), hash);
  340. for (int i = 0; i < mb->get_default_argument_count(); i++) {
  341. //hash should not change, i hope for tis
  342. Variant da = mb->get_default_argument(i);
  343. hash = hash_djb2_one_64(da.hash(), hash);
  344. }
  345. hash = hash_djb2_one_64(mb->get_hint_flags(), hash);
  346. }
  347. }
  348. { //constants
  349. List<StringName> snames;
  350. k = nullptr;
  351. while ((k = t->constant_map.next(k))) {
  352. snames.push_back(*k);
  353. }
  354. snames.sort_custom<StringName::AlphCompare>();
  355. for (const StringName &F : snames) {
  356. hash = hash_djb2_one_64(F.hash(), hash);
  357. hash = hash_djb2_one_64(t->constant_map[F], hash);
  358. }
  359. }
  360. { //signals
  361. List<StringName> snames;
  362. k = nullptr;
  363. while ((k = t->signal_map.next(k))) {
  364. snames.push_back(*k);
  365. }
  366. snames.sort_custom<StringName::AlphCompare>();
  367. for (const StringName &F : snames) {
  368. MethodInfo &mi = t->signal_map[F];
  369. hash = hash_djb2_one_64(F.hash(), hash);
  370. for (int i = 0; i < mi.arguments.size(); i++) {
  371. hash = hash_djb2_one_64(mi.arguments[i].type, hash);
  372. }
  373. }
  374. }
  375. { //properties
  376. List<StringName> snames;
  377. k = nullptr;
  378. while ((k = t->property_setget.next(k))) {
  379. snames.push_back(*k);
  380. }
  381. snames.sort_custom<StringName::AlphCompare>();
  382. for (const StringName &F : snames) {
  383. PropertySetGet *psg = t->property_setget.getptr(F);
  384. ERR_FAIL_COND_V(!psg, 0);
  385. hash = hash_djb2_one_64(F.hash(), hash);
  386. hash = hash_djb2_one_64(psg->setter.hash(), hash);
  387. hash = hash_djb2_one_64(psg->getter.hash(), hash);
  388. }
  389. }
  390. //property list
  391. for (const PropertyInfo &F : t->property_list) {
  392. hash = hash_djb2_one_64(F.name.hash(), hash);
  393. hash = hash_djb2_one_64(F.type, hash);
  394. hash = hash_djb2_one_64(F.hint, hash);
  395. hash = hash_djb2_one_64(F.hint_string.hash(), hash);
  396. hash = hash_djb2_one_64(F.usage, hash);
  397. }
  398. }
  399. return hash;
  400. #else
  401. return 0;
  402. #endif
  403. }
  404. bool ClassDB::class_exists(const StringName &p_class) {
  405. OBJTYPE_RLOCK;
  406. return classes.has(p_class);
  407. }
  408. void ClassDB::add_compatibility_class(const StringName &p_class, const StringName &p_fallback) {
  409. OBJTYPE_WLOCK;
  410. compat_classes[p_class] = p_fallback;
  411. }
  412. thread_local bool initializing_with_extension = false;
  413. thread_local ObjectNativeExtension *initializing_extension = nullptr;
  414. thread_local GDExtensionClassInstancePtr initializing_extension_instance = nullptr;
  415. void ClassDB::instance_get_native_extension_data(ObjectNativeExtension **r_extension, GDExtensionClassInstancePtr *r_extension_instance, Object *p_base) {
  416. if (initializing_with_extension) {
  417. *r_extension = initializing_extension;
  418. *r_extension_instance = initializing_extension_instance;
  419. initializing_with_extension = false;
  420. initializing_extension->set_object_instance(*r_extension_instance, p_base);
  421. } else {
  422. *r_extension = nullptr;
  423. *r_extension_instance = nullptr;
  424. }
  425. }
  426. Object *ClassDB::instantiate(const StringName &p_class) {
  427. ClassInfo *ti;
  428. {
  429. OBJTYPE_RLOCK;
  430. ti = classes.getptr(p_class);
  431. if (!ti || ti->disabled || !ti->creation_func || (ti->native_extension && !ti->native_extension->create_instance)) {
  432. if (compat_classes.has(p_class)) {
  433. ti = classes.getptr(compat_classes[p_class]);
  434. }
  435. }
  436. ERR_FAIL_COND_V_MSG(!ti, nullptr, "Cannot get class '" + String(p_class) + "'.");
  437. ERR_FAIL_COND_V_MSG(ti->disabled, nullptr, "Class '" + String(p_class) + "' is disabled.");
  438. ERR_FAIL_COND_V(!ti->creation_func, nullptr);
  439. }
  440. #ifdef TOOLS_ENABLED
  441. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  442. ERR_PRINT("Class '" + String(p_class) + "' can only be instantiated by editor.");
  443. return nullptr;
  444. }
  445. #endif
  446. if (ti->native_extension) {
  447. initializing_with_extension = true;
  448. initializing_extension = ti->native_extension;
  449. initializing_extension_instance = ti->native_extension->create_instance(ti->native_extension->class_userdata);
  450. }
  451. return ti->creation_func();
  452. }
  453. Object *ClassDB::construct_extended(Object *(*p_create_func)(), ObjectNativeExtension *p_extension) {
  454. initializing_with_extension = true;
  455. initializing_extension = p_extension;
  456. initializing_extension_instance = p_extension->create_instance(p_extension->class_userdata);
  457. return p_create_func();
  458. }
  459. bool ClassDB::can_instantiate(const StringName &p_class) {
  460. OBJTYPE_RLOCK;
  461. ClassInfo *ti = classes.getptr(p_class);
  462. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  463. #ifdef TOOLS_ENABLED
  464. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  465. return false;
  466. }
  467. #endif
  468. return (!ti->disabled && ti->creation_func != nullptr && !(ti->native_extension && !ti->native_extension->create_instance));
  469. }
  470. void ClassDB::_add_class2(const StringName &p_class, const StringName &p_inherits) {
  471. OBJTYPE_WLOCK;
  472. const StringName &name = p_class;
  473. ERR_FAIL_COND_MSG(classes.has(name), "Class '" + String(p_class) + "' already exists.");
  474. classes[name] = ClassInfo();
  475. ClassInfo &ti = classes[name];
  476. ti.name = name;
  477. ti.inherits = p_inherits;
  478. ti.api = current_api;
  479. if (ti.inherits) {
  480. ERR_FAIL_COND(!classes.has(ti.inherits)); //it MUST be registered.
  481. ti.inherits_ptr = &classes[ti.inherits];
  482. } else {
  483. ti.inherits_ptr = nullptr;
  484. }
  485. }
  486. #ifdef DEBUG_METHODS_ENABLED
  487. static MethodInfo info_from_bind(MethodBind *p_method) {
  488. MethodInfo minfo;
  489. minfo.name = p_method->get_name();
  490. minfo.id = p_method->get_method_id();
  491. for (int i = 0; i < p_method->get_argument_count(); i++) {
  492. minfo.arguments.push_back(p_method->get_argument_info(i));
  493. }
  494. minfo.return_val = p_method->get_return_info();
  495. minfo.flags = p_method->get_hint_flags();
  496. for (int i = 0; i < p_method->get_argument_count(); i++) {
  497. if (p_method->has_default_argument(i)) {
  498. minfo.default_arguments.push_back(p_method->get_default_argument(i));
  499. }
  500. }
  501. return minfo;
  502. }
  503. #endif
  504. void ClassDB::get_method_list(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance, bool p_exclude_from_properties) {
  505. OBJTYPE_RLOCK;
  506. ClassInfo *type = classes.getptr(p_class);
  507. while (type) {
  508. if (type->disabled) {
  509. if (p_no_inheritance) {
  510. break;
  511. }
  512. type = type->inherits_ptr;
  513. continue;
  514. }
  515. #ifdef DEBUG_METHODS_ENABLED
  516. for (const MethodInfo &E : type->virtual_methods) {
  517. p_methods->push_back(E);
  518. }
  519. for (const StringName &E : type->method_order) {
  520. if (p_exclude_from_properties && type->methods_in_properties.has(E)) {
  521. continue;
  522. }
  523. MethodBind *method = type->method_map.get(E);
  524. MethodInfo minfo = info_from_bind(method);
  525. p_methods->push_back(minfo);
  526. }
  527. #else
  528. const StringName *K = nullptr;
  529. while ((K = type->method_map.next(K))) {
  530. MethodBind *m = type->method_map[*K];
  531. MethodInfo mi;
  532. mi.name = m->get_name();
  533. p_methods->push_back(mi);
  534. }
  535. #endif
  536. if (p_no_inheritance) {
  537. break;
  538. }
  539. type = type->inherits_ptr;
  540. }
  541. }
  542. bool ClassDB::get_method_info(const StringName &p_class, const StringName &p_method, MethodInfo *r_info, bool p_no_inheritance, bool p_exclude_from_properties) {
  543. OBJTYPE_RLOCK;
  544. ClassInfo *type = classes.getptr(p_class);
  545. while (type) {
  546. if (type->disabled) {
  547. if (p_no_inheritance) {
  548. break;
  549. }
  550. type = type->inherits_ptr;
  551. continue;
  552. }
  553. #ifdef DEBUG_METHODS_ENABLED
  554. MethodBind **method = type->method_map.getptr(p_method);
  555. if (method && *method) {
  556. if (r_info != nullptr) {
  557. MethodInfo minfo = info_from_bind(*method);
  558. *r_info = minfo;
  559. }
  560. return true;
  561. } else if (type->virtual_methods_map.has(p_method)) {
  562. if (r_info) {
  563. *r_info = type->virtual_methods_map[p_method];
  564. }
  565. return true;
  566. }
  567. #else
  568. if (type->method_map.has(p_method)) {
  569. if (r_info) {
  570. MethodBind *m = type->method_map[p_method];
  571. MethodInfo mi;
  572. mi.name = m->get_name();
  573. *r_info = mi;
  574. }
  575. return true;
  576. }
  577. #endif
  578. if (p_no_inheritance) {
  579. break;
  580. }
  581. type = type->inherits_ptr;
  582. }
  583. return false;
  584. }
  585. MethodBind *ClassDB::get_method(const StringName &p_class, const StringName &p_name) {
  586. OBJTYPE_RLOCK;
  587. ClassInfo *type = classes.getptr(p_class);
  588. while (type) {
  589. MethodBind **method = type->method_map.getptr(p_name);
  590. if (method && *method) {
  591. return *method;
  592. }
  593. type = type->inherits_ptr;
  594. }
  595. return nullptr;
  596. }
  597. void ClassDB::bind_integer_constant(const StringName &p_class, const StringName &p_enum, const StringName &p_name, int p_constant) {
  598. OBJTYPE_WLOCK;
  599. ClassInfo *type = classes.getptr(p_class);
  600. ERR_FAIL_COND(!type);
  601. if (type->constant_map.has(p_name)) {
  602. ERR_FAIL();
  603. }
  604. type->constant_map[p_name] = p_constant;
  605. String enum_name = p_enum;
  606. if (enum_name != String()) {
  607. if (enum_name.find(".") != -1) {
  608. enum_name = enum_name.get_slicec('.', 1);
  609. }
  610. List<StringName> *constants_list = type->enum_map.getptr(enum_name);
  611. if (constants_list) {
  612. constants_list->push_back(p_name);
  613. } else {
  614. List<StringName> new_list;
  615. new_list.push_back(p_name);
  616. type->enum_map[enum_name] = new_list;
  617. }
  618. }
  619. #ifdef DEBUG_METHODS_ENABLED
  620. type->constant_order.push_back(p_name);
  621. #endif
  622. }
  623. void ClassDB::get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance) {
  624. OBJTYPE_RLOCK;
  625. ClassInfo *type = classes.getptr(p_class);
  626. while (type) {
  627. #ifdef DEBUG_METHODS_ENABLED
  628. for (const StringName &E : type->constant_order) {
  629. p_constants->push_back(E);
  630. }
  631. #else
  632. const StringName *K = nullptr;
  633. while ((K = type->constant_map.next(K))) {
  634. p_constants->push_back(*K);
  635. }
  636. #endif
  637. if (p_no_inheritance) {
  638. break;
  639. }
  640. type = type->inherits_ptr;
  641. }
  642. }
  643. int ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success) {
  644. OBJTYPE_RLOCK;
  645. ClassInfo *type = classes.getptr(p_class);
  646. while (type) {
  647. int *constant = type->constant_map.getptr(p_name);
  648. if (constant) {
  649. if (p_success) {
  650. *p_success = true;
  651. }
  652. return *constant;
  653. }
  654. type = type->inherits_ptr;
  655. }
  656. if (p_success) {
  657. *p_success = false;
  658. }
  659. return 0;
  660. }
  661. bool ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  662. OBJTYPE_RLOCK;
  663. ClassInfo *type = classes.getptr(p_class);
  664. while (type) {
  665. if (type->constant_map.has(p_name)) {
  666. return true;
  667. }
  668. if (p_no_inheritance) {
  669. return false;
  670. }
  671. type = type->inherits_ptr;
  672. }
  673. return false;
  674. }
  675. StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  676. OBJTYPE_RLOCK;
  677. ClassInfo *type = classes.getptr(p_class);
  678. while (type) {
  679. const StringName *k = nullptr;
  680. while ((k = type->enum_map.next(k))) {
  681. List<StringName> &constants_list = type->enum_map.get(*k);
  682. const List<StringName>::Element *found = constants_list.find(p_name);
  683. if (found) {
  684. return *k;
  685. }
  686. }
  687. if (p_no_inheritance) {
  688. break;
  689. }
  690. type = type->inherits_ptr;
  691. }
  692. return StringName();
  693. }
  694. void ClassDB::get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance) {
  695. OBJTYPE_RLOCK;
  696. ClassInfo *type = classes.getptr(p_class);
  697. while (type) {
  698. const StringName *k = nullptr;
  699. while ((k = type->enum_map.next(k))) {
  700. p_enums->push_back(*k);
  701. }
  702. if (p_no_inheritance) {
  703. break;
  704. }
  705. type = type->inherits_ptr;
  706. }
  707. }
  708. void ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance) {
  709. OBJTYPE_RLOCK;
  710. ClassInfo *type = classes.getptr(p_class);
  711. while (type) {
  712. const List<StringName> *constants = type->enum_map.getptr(p_enum);
  713. if (constants) {
  714. for (const List<StringName>::Element *E = constants->front(); E; E = E->next()) {
  715. p_constants->push_back(E->get());
  716. }
  717. }
  718. if (p_no_inheritance) {
  719. break;
  720. }
  721. type = type->inherits_ptr;
  722. }
  723. }
  724. void ClassDB::set_method_error_return_values(const StringName &p_class, const StringName &p_method, const Vector<Error> &p_values) {
  725. OBJTYPE_RLOCK;
  726. #ifdef DEBUG_METHODS_ENABLED
  727. ClassInfo *type = classes.getptr(p_class);
  728. ERR_FAIL_COND(!type);
  729. type->method_error_values[p_method] = p_values;
  730. #endif
  731. }
  732. Vector<Error> ClassDB::get_method_error_return_values(const StringName &p_class, const StringName &p_method) {
  733. #ifdef DEBUG_METHODS_ENABLED
  734. ClassInfo *type = classes.getptr(p_class);
  735. ERR_FAIL_COND_V(!type, Vector<Error>());
  736. if (!type->method_error_values.has(p_method)) {
  737. return Vector<Error>();
  738. }
  739. return type->method_error_values[p_method];
  740. #else
  741. return Vector<Error>();
  742. #endif
  743. }
  744. bool ClassDB::has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  745. OBJTYPE_RLOCK;
  746. ClassInfo *type = classes.getptr(p_class);
  747. while (type) {
  748. if (type->enum_map.has(p_name)) {
  749. return true;
  750. }
  751. if (p_no_inheritance) {
  752. return false;
  753. }
  754. type = type->inherits_ptr;
  755. }
  756. return false;
  757. }
  758. void ClassDB::add_signal(const StringName &p_class, const MethodInfo &p_signal) {
  759. OBJTYPE_WLOCK;
  760. ClassInfo *type = classes.getptr(p_class);
  761. ERR_FAIL_COND(!type);
  762. StringName sname = p_signal.name;
  763. #ifdef DEBUG_METHODS_ENABLED
  764. ClassInfo *check = type;
  765. while (check) {
  766. ERR_FAIL_COND_MSG(check->signal_map.has(sname), "Class '" + String(p_class) + "' already has signal '" + String(sname) + "'.");
  767. check = check->inherits_ptr;
  768. }
  769. #endif
  770. type->signal_map[sname] = p_signal;
  771. }
  772. void ClassDB::get_signal_list(const StringName &p_class, List<MethodInfo> *p_signals, bool p_no_inheritance) {
  773. OBJTYPE_RLOCK;
  774. ClassInfo *type = classes.getptr(p_class);
  775. ERR_FAIL_COND(!type);
  776. ClassInfo *check = type;
  777. while (check) {
  778. const StringName *S = nullptr;
  779. while ((S = check->signal_map.next(S))) {
  780. p_signals->push_back(check->signal_map[*S]);
  781. }
  782. if (p_no_inheritance) {
  783. return;
  784. }
  785. check = check->inherits_ptr;
  786. }
  787. }
  788. bool ClassDB::has_signal(const StringName &p_class, const StringName &p_signal, bool p_no_inheritance) {
  789. OBJTYPE_RLOCK;
  790. ClassInfo *type = classes.getptr(p_class);
  791. ClassInfo *check = type;
  792. while (check) {
  793. if (check->signal_map.has(p_signal)) {
  794. return true;
  795. }
  796. if (p_no_inheritance) {
  797. return false;
  798. }
  799. check = check->inherits_ptr;
  800. }
  801. return false;
  802. }
  803. bool ClassDB::get_signal(const StringName &p_class, const StringName &p_signal, MethodInfo *r_signal) {
  804. OBJTYPE_RLOCK;
  805. ClassInfo *type = classes.getptr(p_class);
  806. ClassInfo *check = type;
  807. while (check) {
  808. if (check->signal_map.has(p_signal)) {
  809. if (r_signal) {
  810. *r_signal = check->signal_map[p_signal];
  811. }
  812. return true;
  813. }
  814. check = check->inherits_ptr;
  815. }
  816. return false;
  817. }
  818. void ClassDB::add_property_group(const StringName &p_class, const String &p_name, const String &p_prefix) {
  819. OBJTYPE_WLOCK;
  820. ClassInfo *type = classes.getptr(p_class);
  821. ERR_FAIL_COND(!type);
  822. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, p_prefix, PROPERTY_USAGE_GROUP));
  823. }
  824. void ClassDB::add_property_subgroup(const StringName &p_class, const String &p_name, const String &p_prefix) {
  825. OBJTYPE_WLOCK;
  826. ClassInfo *type = classes.getptr(p_class);
  827. ERR_FAIL_COND(!type);
  828. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, p_prefix, PROPERTY_USAGE_SUBGROUP));
  829. }
  830. void ClassDB::add_property_array_count(const StringName &p_class, const String &p_label, const StringName &p_count_property, const StringName &p_count_setter, const StringName &p_count_getter, const String &p_array_element_prefix, uint32_t p_count_usage) {
  831. add_property(p_class, PropertyInfo(Variant::INT, p_count_property, PROPERTY_HINT_NONE, "", p_count_usage | PROPERTY_USAGE_ARRAY, vformat("%s,%s", p_label, p_array_element_prefix)), p_count_setter, p_count_getter);
  832. }
  833. void ClassDB::add_property_array(const StringName &p_class, const StringName &p_path, const String &p_array_element_prefix) {
  834. OBJTYPE_WLOCK;
  835. ClassInfo *type = classes.getptr(p_class);
  836. ERR_FAIL_COND(!type);
  837. type->property_list.push_back(PropertyInfo(Variant::NIL, p_path, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY, p_array_element_prefix));
  838. }
  839. // NOTE: For implementation simplicity reasons, this method doesn't allow setters to have optional arguments at the end.
  840. void ClassDB::add_property(const StringName &p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index) {
  841. lock.read_lock();
  842. ClassInfo *type = classes.getptr(p_class);
  843. lock.read_unlock();
  844. ERR_FAIL_COND(!type);
  845. MethodBind *mb_set = nullptr;
  846. if (p_setter) {
  847. mb_set = get_method(p_class, p_setter);
  848. #ifdef DEBUG_METHODS_ENABLED
  849. ERR_FAIL_COND_MSG(!mb_set, "Invalid setter '" + p_class + "::" + p_setter + "' for property '" + p_pinfo.name + "'.");
  850. int exp_args = 1 + (p_index >= 0 ? 1 : 0);
  851. ERR_FAIL_COND_MSG(mb_set->get_argument_count() != exp_args, "Invalid function for setter '" + p_class + "::" + p_setter + " for property '" + p_pinfo.name + "'.");
  852. #endif
  853. }
  854. MethodBind *mb_get = nullptr;
  855. if (p_getter) {
  856. mb_get = get_method(p_class, p_getter);
  857. #ifdef DEBUG_METHODS_ENABLED
  858. ERR_FAIL_COND_MSG(!mb_get, "Invalid getter '" + p_class + "::" + p_getter + "' for property '" + p_pinfo.name + "'.");
  859. int exp_args = 0 + (p_index >= 0 ? 1 : 0);
  860. ERR_FAIL_COND_MSG(mb_get->get_argument_count() != exp_args, "Invalid function for getter '" + p_class + "::" + p_getter + "' for property: '" + p_pinfo.name + "'.");
  861. #endif
  862. }
  863. #ifdef DEBUG_METHODS_ENABLED
  864. ERR_FAIL_COND_MSG(type->property_setget.has(p_pinfo.name), "Object '" + p_class + "' already has property '" + p_pinfo.name + "'.");
  865. #endif
  866. OBJTYPE_WLOCK
  867. type->property_list.push_back(p_pinfo);
  868. type->property_map[p_pinfo.name] = p_pinfo;
  869. #ifdef DEBUG_METHODS_ENABLED
  870. if (mb_get) {
  871. type->methods_in_properties.insert(p_getter);
  872. }
  873. if (mb_set) {
  874. type->methods_in_properties.insert(p_setter);
  875. }
  876. #endif
  877. PropertySetGet psg;
  878. psg.setter = p_setter;
  879. psg.getter = p_getter;
  880. psg._setptr = mb_set;
  881. psg._getptr = mb_get;
  882. psg.index = p_index;
  883. psg.type = p_pinfo.type;
  884. type->property_setget[p_pinfo.name] = psg;
  885. }
  886. void ClassDB::set_property_default_value(const StringName &p_class, const StringName &p_name, const Variant &p_default) {
  887. if (!default_values.has(p_class)) {
  888. default_values[p_class] = HashMap<StringName, Variant>();
  889. }
  890. default_values[p_class][p_name] = p_default;
  891. }
  892. void ClassDB::add_linked_property(const StringName &p_class, const String &p_property, const String &p_linked_property) {
  893. #ifdef TOOLS_ENABLED
  894. OBJTYPE_WLOCK;
  895. ClassInfo *type = classes.getptr(p_class);
  896. ERR_FAIL_COND(!type);
  897. ERR_FAIL_COND(!type->property_map.has(p_property));
  898. ERR_FAIL_COND(!type->property_map.has(p_linked_property));
  899. PropertyInfo &pinfo = type->property_map[p_property];
  900. pinfo.linked_properties.push_back(p_linked_property);
  901. #endif
  902. }
  903. void ClassDB::get_property_list(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator) {
  904. OBJTYPE_RLOCK;
  905. ClassInfo *type = classes.getptr(p_class);
  906. ClassInfo *check = type;
  907. while (check) {
  908. for (const PropertyInfo &pi : check->property_list) {
  909. if (p_validator) {
  910. // Making a copy as we may modify it.
  911. PropertyInfo pi_mut = pi;
  912. p_validator->_validate_property(pi_mut);
  913. p_list->push_back(pi_mut);
  914. } else {
  915. p_list->push_back(pi);
  916. }
  917. }
  918. if (p_no_inheritance) {
  919. return;
  920. }
  921. check = check->inherits_ptr;
  922. }
  923. }
  924. bool ClassDB::get_property_info(const StringName &p_class, const StringName &p_property, PropertyInfo *r_info, bool p_no_inheritance, const Object *p_validator) {
  925. OBJTYPE_RLOCK;
  926. ClassInfo *check = classes.getptr(p_class);
  927. while (check) {
  928. if (check->property_map.has(p_property)) {
  929. PropertyInfo pinfo = check->property_map[p_property];
  930. if (p_validator) {
  931. p_validator->_validate_property(pinfo);
  932. }
  933. if (r_info) {
  934. *r_info = pinfo;
  935. }
  936. return true;
  937. }
  938. if (p_no_inheritance) {
  939. break;
  940. }
  941. check = check->inherits_ptr;
  942. }
  943. return false;
  944. }
  945. bool ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid) {
  946. ERR_FAIL_NULL_V(p_object, false);
  947. ClassInfo *type = classes.getptr(p_object->get_class_name());
  948. ClassInfo *check = type;
  949. while (check) {
  950. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  951. if (psg) {
  952. if (!psg->setter) {
  953. if (r_valid) {
  954. *r_valid = false;
  955. }
  956. return true; //return true but do nothing
  957. }
  958. Callable::CallError ce;
  959. if (psg->index >= 0) {
  960. Variant index = psg->index;
  961. const Variant *arg[2] = { &index, &p_value };
  962. //p_object->call(psg->setter,arg,2,ce);
  963. if (psg->_setptr) {
  964. psg->_setptr->call(p_object, arg, 2, ce);
  965. } else {
  966. p_object->call(psg->setter, arg, 2, ce);
  967. }
  968. } else {
  969. const Variant *arg[1] = { &p_value };
  970. if (psg->_setptr) {
  971. psg->_setptr->call(p_object, arg, 1, ce);
  972. } else {
  973. p_object->call(psg->setter, arg, 1, ce);
  974. }
  975. }
  976. if (r_valid) {
  977. *r_valid = ce.error == Callable::CallError::CALL_OK;
  978. }
  979. return true;
  980. }
  981. check = check->inherits_ptr;
  982. }
  983. return false;
  984. }
  985. bool ClassDB::get_property(Object *p_object, const StringName &p_property, Variant &r_value) {
  986. ERR_FAIL_NULL_V(p_object, false);
  987. ClassInfo *type = classes.getptr(p_object->get_class_name());
  988. ClassInfo *check = type;
  989. while (check) {
  990. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  991. if (psg) {
  992. if (!psg->getter) {
  993. return true; //return true but do nothing
  994. }
  995. if (psg->index >= 0) {
  996. Variant index = psg->index;
  997. const Variant *arg[1] = { &index };
  998. Callable::CallError ce;
  999. r_value = p_object->call(psg->getter, arg, 1, ce);
  1000. } else {
  1001. Callable::CallError ce;
  1002. if (psg->_getptr) {
  1003. r_value = psg->_getptr->call(p_object, nullptr, 0, ce);
  1004. } else {
  1005. r_value = p_object->call(psg->getter, nullptr, 0, ce);
  1006. }
  1007. }
  1008. return true;
  1009. }
  1010. const int *c = check->constant_map.getptr(p_property); //constants count
  1011. if (c) {
  1012. r_value = *c;
  1013. return true;
  1014. }
  1015. if (check->method_map.has(p_property)) { //methods count
  1016. r_value = Callable(p_object, p_property);
  1017. return true;
  1018. }
  1019. if (check->signal_map.has(p_property)) { //signals count
  1020. r_value = Signal(p_object, p_property);
  1021. return true;
  1022. }
  1023. check = check->inherits_ptr;
  1024. }
  1025. return false;
  1026. }
  1027. int ClassDB::get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  1028. ClassInfo *type = classes.getptr(p_class);
  1029. ClassInfo *check = type;
  1030. while (check) {
  1031. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  1032. if (psg) {
  1033. if (r_is_valid) {
  1034. *r_is_valid = true;
  1035. }
  1036. return psg->index;
  1037. }
  1038. check = check->inherits_ptr;
  1039. }
  1040. if (r_is_valid) {
  1041. *r_is_valid = false;
  1042. }
  1043. return -1;
  1044. }
  1045. Variant::Type ClassDB::get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  1046. ClassInfo *type = classes.getptr(p_class);
  1047. ClassInfo *check = type;
  1048. while (check) {
  1049. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  1050. if (psg) {
  1051. if (r_is_valid) {
  1052. *r_is_valid = true;
  1053. }
  1054. return psg->type;
  1055. }
  1056. check = check->inherits_ptr;
  1057. }
  1058. if (r_is_valid) {
  1059. *r_is_valid = false;
  1060. }
  1061. return Variant::NIL;
  1062. }
  1063. StringName ClassDB::get_property_setter(const StringName &p_class, const StringName &p_property) {
  1064. ClassInfo *type = classes.getptr(p_class);
  1065. ClassInfo *check = type;
  1066. while (check) {
  1067. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  1068. if (psg) {
  1069. return psg->setter;
  1070. }
  1071. check = check->inherits_ptr;
  1072. }
  1073. return StringName();
  1074. }
  1075. StringName ClassDB::get_property_getter(const StringName &p_class, const StringName &p_property) {
  1076. ClassInfo *type = classes.getptr(p_class);
  1077. ClassInfo *check = type;
  1078. while (check) {
  1079. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  1080. if (psg) {
  1081. return psg->getter;
  1082. }
  1083. check = check->inherits_ptr;
  1084. }
  1085. return StringName();
  1086. }
  1087. bool ClassDB::has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance) {
  1088. ClassInfo *type = classes.getptr(p_class);
  1089. ClassInfo *check = type;
  1090. while (check) {
  1091. if (check->property_setget.has(p_property)) {
  1092. return true;
  1093. }
  1094. if (p_no_inheritance) {
  1095. break;
  1096. }
  1097. check = check->inherits_ptr;
  1098. }
  1099. return false;
  1100. }
  1101. void ClassDB::set_method_flags(const StringName &p_class, const StringName &p_method, int p_flags) {
  1102. OBJTYPE_WLOCK;
  1103. ClassInfo *type = classes.getptr(p_class);
  1104. ClassInfo *check = type;
  1105. ERR_FAIL_COND(!check);
  1106. ERR_FAIL_COND(!check->method_map.has(p_method));
  1107. check->method_map[p_method]->set_hint_flags(p_flags);
  1108. }
  1109. bool ClassDB::has_method(const StringName &p_class, const StringName &p_method, bool p_no_inheritance) {
  1110. ClassInfo *type = classes.getptr(p_class);
  1111. ClassInfo *check = type;
  1112. while (check) {
  1113. if (check->method_map.has(p_method)) {
  1114. return true;
  1115. }
  1116. if (p_no_inheritance) {
  1117. return false;
  1118. }
  1119. check = check->inherits_ptr;
  1120. }
  1121. return false;
  1122. }
  1123. void ClassDB::bind_method_custom(const StringName &p_class, MethodBind *p_method) {
  1124. ClassInfo *type = classes.getptr(p_class);
  1125. if (!type) {
  1126. ERR_FAIL_MSG("Couldn't bind custom method '" + p_method->get_name() + "' for instance '" + p_class + "'.");
  1127. }
  1128. if (type->method_map.has(p_method->get_name())) {
  1129. // overloading not supported
  1130. ERR_FAIL_MSG("Method already bound '" + p_class + "::" + p_method->get_name() + "'.");
  1131. }
  1132. #ifdef DEBUG_METHODS_ENABLED
  1133. type->method_order.push_back(p_method->get_name());
  1134. #endif
  1135. type->method_map[p_method->get_name()] = p_method;
  1136. }
  1137. #ifdef DEBUG_METHODS_ENABLED
  1138. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount) {
  1139. StringName mdname = method_name.name;
  1140. #else
  1141. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const char *method_name, const Variant **p_defs, int p_defcount) {
  1142. StringName mdname = StaticCString::create(method_name);
  1143. #endif
  1144. OBJTYPE_WLOCK;
  1145. ERR_FAIL_COND_V(!p_bind, nullptr);
  1146. p_bind->set_name(mdname);
  1147. String instance_type = p_bind->get_instance_class();
  1148. #ifdef DEBUG_ENABLED
  1149. ERR_FAIL_COND_V_MSG(has_method(instance_type, mdname), nullptr, "Class " + String(instance_type) + " already has a method " + String(mdname) + ".");
  1150. #endif
  1151. ClassInfo *type = classes.getptr(instance_type);
  1152. if (!type) {
  1153. memdelete(p_bind);
  1154. ERR_FAIL_V_MSG(nullptr, "Couldn't bind method '" + mdname + "' for instance '" + instance_type + "'.");
  1155. }
  1156. if (type->method_map.has(mdname)) {
  1157. memdelete(p_bind);
  1158. // overloading not supported
  1159. ERR_FAIL_V_MSG(nullptr, "Method already bound '" + instance_type + "::" + mdname + "'.");
  1160. }
  1161. #ifdef DEBUG_METHODS_ENABLED
  1162. if (method_name.args.size() > p_bind->get_argument_count()) {
  1163. memdelete(p_bind);
  1164. ERR_FAIL_V_MSG(nullptr, "Method definition provides more arguments than the method actually has '" + instance_type + "::" + mdname + "'.");
  1165. }
  1166. p_bind->set_argument_names(method_name.args);
  1167. type->method_order.push_back(mdname);
  1168. #endif
  1169. type->method_map[mdname] = p_bind;
  1170. Vector<Variant> defvals;
  1171. defvals.resize(p_defcount);
  1172. for (int i = 0; i < p_defcount; i++) {
  1173. defvals.write[i] = *p_defs[i];
  1174. }
  1175. p_bind->set_default_arguments(defvals);
  1176. p_bind->set_hint_flags(p_flags);
  1177. return p_bind;
  1178. }
  1179. void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual, const Vector<String> &p_arg_names, bool p_object_core) {
  1180. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1181. OBJTYPE_WLOCK;
  1182. #ifdef DEBUG_METHODS_ENABLED
  1183. MethodInfo mi = p_method;
  1184. if (p_virtual) {
  1185. mi.flags |= METHOD_FLAG_VIRTUAL;
  1186. }
  1187. if (p_object_core) {
  1188. mi.flags |= METHOD_FLAG_OBJECT_CORE;
  1189. }
  1190. if (p_arg_names.size()) {
  1191. if (p_arg_names.size() != mi.arguments.size()) {
  1192. WARN_PRINT("Mismatch argument name count for virtual function: " + String(p_class) + "::" + p_method.name);
  1193. } else {
  1194. for (int i = 0; i < p_arg_names.size(); i++) {
  1195. mi.arguments[i].name = p_arg_names[i];
  1196. }
  1197. }
  1198. }
  1199. classes[p_class].virtual_methods.push_back(mi);
  1200. classes[p_class].virtual_methods_map[p_method.name] = mi;
  1201. #endif
  1202. }
  1203. void ClassDB::get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance) {
  1204. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1205. #ifdef DEBUG_METHODS_ENABLED
  1206. ClassInfo *type = classes.getptr(p_class);
  1207. ClassInfo *check = type;
  1208. while (check) {
  1209. for (const MethodInfo &E : check->virtual_methods) {
  1210. p_methods->push_back(E);
  1211. }
  1212. if (p_no_inheritance) {
  1213. return;
  1214. }
  1215. check = check->inherits_ptr;
  1216. }
  1217. #endif
  1218. }
  1219. void ClassDB::set_class_enabled(const StringName &p_class, bool p_enable) {
  1220. OBJTYPE_WLOCK;
  1221. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1222. classes[p_class].disabled = !p_enable;
  1223. }
  1224. bool ClassDB::is_class_enabled(const StringName &p_class) {
  1225. OBJTYPE_RLOCK;
  1226. ClassInfo *ti = classes.getptr(p_class);
  1227. if (!ti || !ti->creation_func) {
  1228. if (compat_classes.has(p_class)) {
  1229. ti = classes.getptr(compat_classes[p_class]);
  1230. }
  1231. }
  1232. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  1233. return !ti->disabled;
  1234. }
  1235. bool ClassDB::is_class_exposed(const StringName &p_class) {
  1236. OBJTYPE_RLOCK;
  1237. ClassInfo *ti = classes.getptr(p_class);
  1238. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  1239. return ti->exposed;
  1240. }
  1241. StringName ClassDB::get_category(const StringName &p_node) {
  1242. ERR_FAIL_COND_V(!classes.has(p_node), StringName());
  1243. #ifdef DEBUG_ENABLED
  1244. return classes[p_node].category;
  1245. #else
  1246. return StringName();
  1247. #endif
  1248. }
  1249. void ClassDB::add_resource_base_extension(const StringName &p_extension, const StringName &p_class) {
  1250. if (resource_base_extensions.has(p_extension)) {
  1251. return;
  1252. }
  1253. resource_base_extensions[p_extension] = p_class;
  1254. }
  1255. void ClassDB::get_resource_base_extensions(List<String> *p_extensions) {
  1256. const StringName *K = nullptr;
  1257. while ((K = resource_base_extensions.next(K))) {
  1258. p_extensions->push_back(*K);
  1259. }
  1260. }
  1261. bool ClassDB::is_resource_extension(const StringName &p_extension) {
  1262. return resource_base_extensions.has(p_extension);
  1263. }
  1264. void ClassDB::get_extensions_for_type(const StringName &p_class, List<String> *p_extensions) {
  1265. const StringName *K = nullptr;
  1266. while ((K = resource_base_extensions.next(K))) {
  1267. StringName cmp = resource_base_extensions[*K];
  1268. if (is_parent_class(p_class, cmp) || is_parent_class(cmp, p_class)) {
  1269. p_extensions->push_back(*K);
  1270. }
  1271. }
  1272. }
  1273. HashMap<StringName, HashMap<StringName, Variant>> ClassDB::default_values;
  1274. Set<StringName> ClassDB::default_values_cached;
  1275. Variant ClassDB::class_get_default_property_value(const StringName &p_class, const StringName &p_property, bool *r_valid) {
  1276. if (!default_values_cached.has(p_class)) {
  1277. if (!default_values.has(p_class)) {
  1278. default_values[p_class] = HashMap<StringName, Variant>();
  1279. }
  1280. Object *c = nullptr;
  1281. bool cleanup_c = false;
  1282. if (Engine::get_singleton()->has_singleton(p_class)) {
  1283. c = Engine::get_singleton()->get_singleton_object(p_class);
  1284. cleanup_c = false;
  1285. } else if (ClassDB::can_instantiate(p_class)) {
  1286. c = ClassDB::instantiate(p_class);
  1287. cleanup_c = true;
  1288. }
  1289. if (c) {
  1290. List<PropertyInfo> plist;
  1291. c->get_property_list(&plist);
  1292. for (const PropertyInfo &E : plist) {
  1293. if (E.usage & (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR)) {
  1294. if (!default_values[p_class].has(E.name)) {
  1295. Variant v = c->get(E.name);
  1296. default_values[p_class][E.name] = v;
  1297. }
  1298. }
  1299. }
  1300. if (cleanup_c) {
  1301. memdelete(c);
  1302. }
  1303. }
  1304. default_values_cached.insert(p_class);
  1305. }
  1306. if (!default_values.has(p_class)) {
  1307. if (r_valid != nullptr) {
  1308. *r_valid = false;
  1309. }
  1310. return Variant();
  1311. }
  1312. if (!default_values[p_class].has(p_property)) {
  1313. if (r_valid != nullptr) {
  1314. *r_valid = false;
  1315. }
  1316. return Variant();
  1317. }
  1318. if (r_valid != nullptr) {
  1319. *r_valid = true;
  1320. }
  1321. Variant var = default_values[p_class][p_property];
  1322. #ifdef DEBUG_ENABLED
  1323. // Some properties may have an instantiated Object as default value,
  1324. // (like Path2D's `curve` used to have), but that's not a good practice.
  1325. // Instead, those properties should use PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT
  1326. // to be auto-instantiated when created in the editor.
  1327. if (var.get_type() == Variant::OBJECT) {
  1328. Object *obj = var.get_validated_object();
  1329. if (obj) {
  1330. WARN_PRINT(vformat("Instantiated %s used as default value for %s's \"%s\" property.", obj->get_class(), p_class, p_property));
  1331. }
  1332. }
  1333. #endif
  1334. return var;
  1335. }
  1336. void ClassDB::register_extension_class(ObjectNativeExtension *p_extension) {
  1337. GLOBAL_LOCK_FUNCTION;
  1338. ERR_FAIL_COND_MSG(classes.has(p_extension->class_name), "Class already registered: " + String(p_extension->class_name));
  1339. ERR_FAIL_COND_MSG(!classes.has(p_extension->parent_class_name), "Parent class name for extension class not found: " + String(p_extension->parent_class_name));
  1340. ClassInfo *parent = classes.getptr(p_extension->parent_class_name);
  1341. ClassInfo c;
  1342. c.api = p_extension->editor_class ? API_EDITOR_EXTENSION : API_EXTENSION;
  1343. c.native_extension = p_extension;
  1344. c.name = p_extension->class_name;
  1345. c.creation_func = parent->creation_func;
  1346. c.inherits = parent->name;
  1347. c.class_ptr = parent->class_ptr;
  1348. c.inherits_ptr = parent;
  1349. c.exposed = true;
  1350. classes[p_extension->class_name] = c;
  1351. }
  1352. void ClassDB::unregister_extension_class(const StringName &p_class) {
  1353. ERR_FAIL_COND(!classes.has(p_class));
  1354. classes.erase(p_class);
  1355. }
  1356. RWLock ClassDB::lock;
  1357. void ClassDB::cleanup_defaults() {
  1358. default_values.clear();
  1359. default_values_cached.clear();
  1360. }
  1361. void ClassDB::cleanup() {
  1362. //OBJTYPE_LOCK; hah not here
  1363. const StringName *k = nullptr;
  1364. while ((k = classes.next(k))) {
  1365. ClassInfo &ti = classes[*k];
  1366. const StringName *m = nullptr;
  1367. while ((m = ti.method_map.next(m))) {
  1368. memdelete(ti.method_map[*m]);
  1369. }
  1370. }
  1371. classes.clear();
  1372. resource_base_extensions.clear();
  1373. compat_classes.clear();
  1374. }
  1375. //