class_db.cpp 48 KB

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