class_db.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. /**************************************************************************/
  2. /* class_db.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "class_db.h"
  31. #include "core/engine.h"
  32. #include "core/os/mutex.h"
  33. #include "core/version.h"
  34. #define OBJTYPE_RLOCK RWLockRead _rw_lockr_(lock);
  35. #define OBJTYPE_WLOCK RWLockWrite _rw_lockw_(lock);
  36. #ifdef DEBUG_METHODS_ENABLED
  37. MethodDefinition D_METHOD(const char *p_name) {
  38. MethodDefinition md;
  39. md.name = StaticCString::create(p_name);
  40. return md;
  41. }
  42. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1) {
  43. MethodDefinition md;
  44. md.name = StaticCString::create(p_name);
  45. md.args.push_back(StaticCString::create(p_arg1));
  46. return md;
  47. }
  48. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2) {
  49. MethodDefinition md;
  50. md.name = StaticCString::create(p_name);
  51. md.args.resize(2);
  52. md.args.write[0] = StaticCString::create(p_arg1);
  53. md.args.write[1] = StaticCString::create(p_arg2);
  54. return md;
  55. }
  56. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3) {
  57. MethodDefinition md;
  58. md.name = StaticCString::create(p_name);
  59. md.args.resize(3);
  60. md.args.write[0] = StaticCString::create(p_arg1);
  61. md.args.write[1] = StaticCString::create(p_arg2);
  62. md.args.write[2] = StaticCString::create(p_arg3);
  63. return md;
  64. }
  65. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4) {
  66. MethodDefinition md;
  67. md.name = StaticCString::create(p_name);
  68. md.args.resize(4);
  69. md.args.write[0] = StaticCString::create(p_arg1);
  70. md.args.write[1] = StaticCString::create(p_arg2);
  71. md.args.write[2] = StaticCString::create(p_arg3);
  72. md.args.write[3] = StaticCString::create(p_arg4);
  73. return md;
  74. }
  75. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5) {
  76. MethodDefinition md;
  77. md.name = StaticCString::create(p_name);
  78. md.args.resize(5);
  79. md.args.write[0] = StaticCString::create(p_arg1);
  80. md.args.write[1] = StaticCString::create(p_arg2);
  81. md.args.write[2] = StaticCString::create(p_arg3);
  82. md.args.write[3] = StaticCString::create(p_arg4);
  83. md.args.write[4] = StaticCString::create(p_arg5);
  84. return md;
  85. }
  86. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6) {
  87. MethodDefinition md;
  88. md.name = StaticCString::create(p_name);
  89. md.args.resize(6);
  90. md.args.write[0] = StaticCString::create(p_arg1);
  91. md.args.write[1] = StaticCString::create(p_arg2);
  92. md.args.write[2] = StaticCString::create(p_arg3);
  93. md.args.write[3] = StaticCString::create(p_arg4);
  94. md.args.write[4] = StaticCString::create(p_arg5);
  95. md.args.write[5] = StaticCString::create(p_arg6);
  96. return md;
  97. }
  98. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7) {
  99. MethodDefinition md;
  100. md.name = StaticCString::create(p_name);
  101. md.args.resize(7);
  102. md.args.write[0] = StaticCString::create(p_arg1);
  103. md.args.write[1] = StaticCString::create(p_arg2);
  104. md.args.write[2] = StaticCString::create(p_arg3);
  105. md.args.write[3] = StaticCString::create(p_arg4);
  106. md.args.write[4] = StaticCString::create(p_arg5);
  107. md.args.write[5] = StaticCString::create(p_arg6);
  108. md.args.write[6] = StaticCString::create(p_arg7);
  109. return md;
  110. }
  111. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8) {
  112. MethodDefinition md;
  113. md.name = StaticCString::create(p_name);
  114. md.args.resize(8);
  115. md.args.write[0] = StaticCString::create(p_arg1);
  116. md.args.write[1] = StaticCString::create(p_arg2);
  117. md.args.write[2] = StaticCString::create(p_arg3);
  118. md.args.write[3] = StaticCString::create(p_arg4);
  119. md.args.write[4] = StaticCString::create(p_arg5);
  120. md.args.write[5] = StaticCString::create(p_arg6);
  121. md.args.write[6] = StaticCString::create(p_arg7);
  122. md.args.write[7] = StaticCString::create(p_arg8);
  123. return md;
  124. }
  125. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9) {
  126. MethodDefinition md;
  127. md.name = StaticCString::create(p_name);
  128. md.args.resize(9);
  129. md.args.write[0] = StaticCString::create(p_arg1);
  130. md.args.write[1] = StaticCString::create(p_arg2);
  131. md.args.write[2] = StaticCString::create(p_arg3);
  132. md.args.write[3] = StaticCString::create(p_arg4);
  133. md.args.write[4] = StaticCString::create(p_arg5);
  134. md.args.write[5] = StaticCString::create(p_arg6);
  135. md.args.write[6] = StaticCString::create(p_arg7);
  136. md.args.write[7] = StaticCString::create(p_arg8);
  137. md.args.write[8] = StaticCString::create(p_arg9);
  138. return md;
  139. }
  140. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10) {
  141. MethodDefinition md;
  142. md.name = StaticCString::create(p_name);
  143. md.args.resize(10);
  144. md.args.write[0] = StaticCString::create(p_arg1);
  145. md.args.write[1] = StaticCString::create(p_arg2);
  146. md.args.write[2] = StaticCString::create(p_arg3);
  147. md.args.write[3] = StaticCString::create(p_arg4);
  148. md.args.write[4] = StaticCString::create(p_arg5);
  149. md.args.write[5] = StaticCString::create(p_arg6);
  150. md.args.write[6] = StaticCString::create(p_arg7);
  151. md.args.write[7] = StaticCString::create(p_arg8);
  152. md.args.write[8] = StaticCString::create(p_arg9);
  153. md.args.write[9] = StaticCString::create(p_arg10);
  154. return md;
  155. }
  156. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10, const char *p_arg11) {
  157. MethodDefinition md;
  158. md.name = StaticCString::create(p_name);
  159. md.args.resize(11);
  160. md.args.write[0] = StaticCString::create(p_arg1);
  161. md.args.write[1] = StaticCString::create(p_arg2);
  162. md.args.write[2] = StaticCString::create(p_arg3);
  163. md.args.write[3] = StaticCString::create(p_arg4);
  164. md.args.write[4] = StaticCString::create(p_arg5);
  165. md.args.write[5] = StaticCString::create(p_arg6);
  166. md.args.write[6] = StaticCString::create(p_arg7);
  167. md.args.write[7] = StaticCString::create(p_arg8);
  168. md.args.write[8] = StaticCString::create(p_arg9);
  169. md.args.write[9] = StaticCString::create(p_arg10);
  170. md.args.write[10] = StaticCString::create(p_arg11);
  171. return md;
  172. }
  173. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10, const char *p_arg11, const char *p_arg12) {
  174. MethodDefinition md;
  175. md.name = StaticCString::create(p_name);
  176. md.args.resize(12);
  177. md.args.write[0] = StaticCString::create(p_arg1);
  178. md.args.write[1] = StaticCString::create(p_arg2);
  179. md.args.write[2] = StaticCString::create(p_arg3);
  180. md.args.write[3] = StaticCString::create(p_arg4);
  181. md.args.write[4] = StaticCString::create(p_arg5);
  182. md.args.write[5] = StaticCString::create(p_arg6);
  183. md.args.write[6] = StaticCString::create(p_arg7);
  184. md.args.write[7] = StaticCString::create(p_arg8);
  185. md.args.write[8] = StaticCString::create(p_arg9);
  186. md.args.write[9] = StaticCString::create(p_arg10);
  187. md.args.write[10] = StaticCString::create(p_arg11);
  188. md.args.write[11] = StaticCString::create(p_arg12);
  189. return md;
  190. }
  191. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10, const char *p_arg11, const char *p_arg12, const char *p_arg13) {
  192. MethodDefinition md;
  193. md.name = StaticCString::create(p_name);
  194. md.args.resize(13);
  195. md.args.write[0] = StaticCString::create(p_arg1);
  196. md.args.write[1] = StaticCString::create(p_arg2);
  197. md.args.write[2] = StaticCString::create(p_arg3);
  198. md.args.write[3] = StaticCString::create(p_arg4);
  199. md.args.write[4] = StaticCString::create(p_arg5);
  200. md.args.write[5] = StaticCString::create(p_arg6);
  201. md.args.write[6] = StaticCString::create(p_arg7);
  202. md.args.write[7] = StaticCString::create(p_arg8);
  203. md.args.write[8] = StaticCString::create(p_arg9);
  204. md.args.write[9] = StaticCString::create(p_arg10);
  205. md.args.write[10] = StaticCString::create(p_arg11);
  206. md.args.write[11] = StaticCString::create(p_arg12);
  207. md.args.write[12] = StaticCString::create(p_arg13);
  208. return md;
  209. }
  210. #endif
  211. ClassDB::APIType ClassDB::current_api = API_CORE;
  212. void ClassDB::set_current_api(APIType p_api) {
  213. current_api = p_api;
  214. }
  215. ClassDB::APIType ClassDB::get_current_api() {
  216. return current_api;
  217. }
  218. HashMap<StringName, ClassDB::ClassInfo> ClassDB::classes;
  219. HashMap<StringName, StringName> ClassDB::resource_base_extensions;
  220. HashMap<StringName, StringName> ClassDB::compat_classes;
  221. ClassDB::ClassInfo::ClassInfo() {
  222. api = API_NONE;
  223. creation_func = nullptr;
  224. inherits_ptr = nullptr;
  225. disabled = false;
  226. exposed = false;
  227. }
  228. ClassDB::ClassInfo::~ClassInfo() {
  229. }
  230. bool ClassDB::_is_parent_class(const StringName &p_class, const StringName &p_inherits) {
  231. StringName inherits = p_class;
  232. while (inherits.operator String().length()) {
  233. if (inherits == p_inherits) {
  234. return true;
  235. }
  236. inherits = _get_parent_class(inherits);
  237. }
  238. return false;
  239. }
  240. bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) {
  241. OBJTYPE_RLOCK;
  242. return _is_parent_class(p_class, p_inherits);
  243. }
  244. void ClassDB::get_class_list(List<StringName> *p_classes) {
  245. OBJTYPE_RLOCK;
  246. const StringName *k = nullptr;
  247. while ((k = classes.next(k))) {
  248. p_classes->push_back(*k);
  249. }
  250. p_classes->sort();
  251. }
  252. void ClassDB::get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
  253. OBJTYPE_RLOCK;
  254. const StringName *k = nullptr;
  255. while ((k = classes.next(k))) {
  256. if (*k != p_class && _is_parent_class(*k, p_class)) {
  257. p_classes->push_back(*k);
  258. }
  259. }
  260. }
  261. void ClassDB::get_direct_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
  262. OBJTYPE_RLOCK;
  263. const StringName *k = nullptr;
  264. while ((k = classes.next(k))) {
  265. if (*k != p_class && _get_parent_class(*k) == p_class) {
  266. p_classes->push_back(*k);
  267. }
  268. }
  269. }
  270. StringName ClassDB::get_parent_class_nocheck(const StringName &p_class) {
  271. OBJTYPE_RLOCK;
  272. ClassInfo *ti = classes.getptr(p_class);
  273. if (!ti) {
  274. return StringName();
  275. }
  276. return ti->inherits;
  277. }
  278. StringName ClassDB::_get_parent_class(const StringName &p_class) {
  279. ClassInfo *ti = classes.getptr(p_class);
  280. ERR_FAIL_COND_V_MSG(!ti, StringName(), "Cannot get class '" + String(p_class) + "'.");
  281. return ti->inherits;
  282. }
  283. StringName ClassDB::get_parent_class(const StringName &p_class) {
  284. OBJTYPE_RLOCK;
  285. return _get_parent_class(p_class);
  286. }
  287. ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) {
  288. OBJTYPE_RLOCK;
  289. ClassInfo *ti = classes.getptr(p_class);
  290. ERR_FAIL_COND_V_MSG(!ti, API_NONE, "Cannot get class '" + String(p_class) + "'.");
  291. return ti->api;
  292. }
  293. uint64_t ClassDB::get_api_hash(APIType p_api) {
  294. OBJTYPE_RLOCK;
  295. #ifdef DEBUG_METHODS_ENABLED
  296. uint64_t hash = hash_djb2_one_64(HashMapHasherDefault::hash(VERSION_FULL_CONFIG));
  297. List<StringName> names;
  298. const StringName *k = nullptr;
  299. while ((k = classes.next(k))) {
  300. names.push_back(*k);
  301. }
  302. //must be alphabetically sorted for hash to compute
  303. names.sort_custom<StringName::AlphCompare>();
  304. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  305. ClassInfo *t = classes.getptr(E->get());
  306. ERR_FAIL_COND_V_MSG(!t, 0, "Cannot get class '" + String(E->get()) + "'.");
  307. if (t->api != p_api || !t->exposed) {
  308. continue;
  309. }
  310. hash = hash_djb2_one_64(t->name.hash(), hash);
  311. hash = hash_djb2_one_64(t->inherits.hash(), hash);
  312. { //methods
  313. List<StringName> snames;
  314. k = nullptr;
  315. while ((k = t->method_map.next(k))) {
  316. String name = k->operator String();
  317. ERR_CONTINUE(name.empty());
  318. if (name[0] == '_') {
  319. continue; // Ignore non-virtual methods that start with an underscore
  320. }
  321. snames.push_back(*k);
  322. }
  323. snames.sort_custom<StringName::AlphCompare>();
  324. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  325. MethodBind *mb = t->method_map[F->get()];
  326. hash = hash_djb2_one_64(mb->get_name().hash(), hash);
  327. hash = hash_djb2_one_64(mb->get_argument_count(), hash);
  328. hash = hash_djb2_one_64(mb->get_argument_type(-1), hash); //return
  329. for (int i = 0; i < mb->get_argument_count(); i++) {
  330. const PropertyInfo info = mb->get_argument_info(i);
  331. hash = hash_djb2_one_64(info.type, hash);
  332. hash = hash_djb2_one_64(info.name.hash(), hash);
  333. hash = hash_djb2_one_64(info.hint, hash);
  334. hash = hash_djb2_one_64(info.hint_string.hash(), hash);
  335. }
  336. hash = hash_djb2_one_64(mb->get_default_argument_count(), hash);
  337. for (int i = 0; i < mb->get_default_argument_count(); i++) {
  338. //hash should not change, i hope for tis
  339. Variant da = mb->get_default_argument(i);
  340. hash = hash_djb2_one_64(da.hash(), hash);
  341. }
  342. hash = hash_djb2_one_64(mb->get_hint_flags(), hash);
  343. }
  344. }
  345. { //constants
  346. List<StringName> snames;
  347. k = nullptr;
  348. while ((k = t->constant_map.next(k))) {
  349. snames.push_back(*k);
  350. }
  351. snames.sort_custom<StringName::AlphCompare>();
  352. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  353. hash = hash_djb2_one_64(F->get().hash(), hash);
  354. hash = hash_djb2_one_64(t->constant_map[F->get()], hash);
  355. }
  356. }
  357. { //signals
  358. List<StringName> snames;
  359. k = nullptr;
  360. while ((k = t->signal_map.next(k))) {
  361. snames.push_back(*k);
  362. }
  363. snames.sort_custom<StringName::AlphCompare>();
  364. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  365. MethodInfo &mi = t->signal_map[F->get()];
  366. hash = hash_djb2_one_64(F->get().hash(), hash);
  367. for (int i = 0; i < mi.arguments.size(); i++) {
  368. hash = hash_djb2_one_64(mi.arguments[i].type, hash);
  369. }
  370. }
  371. }
  372. { //properties
  373. List<StringName> snames;
  374. k = nullptr;
  375. while ((k = t->property_setget.next(k))) {
  376. snames.push_back(*k);
  377. }
  378. snames.sort_custom<StringName::AlphCompare>();
  379. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  380. PropertySetGet *psg = t->property_setget.getptr(F->get());
  381. ERR_FAIL_COND_V(!psg, 0);
  382. hash = hash_djb2_one_64(F->get().hash(), hash);
  383. hash = hash_djb2_one_64(psg->setter.hash(), hash);
  384. hash = hash_djb2_one_64(psg->getter.hash(), hash);
  385. }
  386. }
  387. //property list
  388. for (List<PropertyInfo>::Element *F = t->property_list.front(); F; F = F->next()) {
  389. hash = hash_djb2_one_64(F->get().name.hash(), hash);
  390. hash = hash_djb2_one_64(F->get().type, hash);
  391. hash = hash_djb2_one_64(F->get().hint, hash);
  392. hash = hash_djb2_one_64(F->get().hint_string.hash(), hash);
  393. hash = hash_djb2_one_64(F->get().usage, hash);
  394. }
  395. }
  396. return hash;
  397. #else
  398. return 0;
  399. #endif
  400. }
  401. bool ClassDB::class_exists(const StringName &p_class) {
  402. OBJTYPE_RLOCK;
  403. return classes.has(p_class);
  404. }
  405. void ClassDB::add_compatibility_class(const StringName &p_class, const StringName &p_fallback) {
  406. OBJTYPE_WLOCK;
  407. compat_classes[p_class] = p_fallback;
  408. }
  409. Object *ClassDB::instance(const StringName &p_class) {
  410. ClassInfo *ti;
  411. {
  412. OBJTYPE_RLOCK;
  413. ti = classes.getptr(p_class);
  414. if (!ti || ti->disabled || !ti->creation_func) {
  415. if (compat_classes.has(p_class)) {
  416. ti = classes.getptr(compat_classes[p_class]);
  417. }
  418. }
  419. ERR_FAIL_COND_V_MSG(!ti, nullptr, "Cannot get class '" + String(p_class) + "'.");
  420. ERR_FAIL_COND_V_MSG(ti->disabled, nullptr, "Class '" + String(p_class) + "' is disabled.");
  421. ERR_FAIL_COND_V_MSG(!ti->creation_func, nullptr, "Class '" + String(p_class) + "' or its base class cannot be instantiated.");
  422. }
  423. #ifdef TOOLS_ENABLED
  424. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  425. ERR_PRINT("Class '" + String(p_class) + "' can only be instantiated by editor.");
  426. return nullptr;
  427. }
  428. #endif
  429. return ti->creation_func();
  430. }
  431. bool ClassDB::can_instance(const StringName &p_class) {
  432. OBJTYPE_RLOCK;
  433. ClassInfo *ti = classes.getptr(p_class);
  434. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  435. #ifdef TOOLS_ENABLED
  436. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  437. return false;
  438. }
  439. #endif
  440. return (!ti->disabled && ti->creation_func != nullptr);
  441. }
  442. void ClassDB::_add_class2(const StringName &p_class, const StringName &p_inherits) {
  443. OBJTYPE_WLOCK;
  444. const StringName &name = p_class;
  445. ERR_FAIL_COND_MSG(classes.has(name), "Class '" + String(p_class) + "' already exists.");
  446. classes[name] = ClassInfo();
  447. ClassInfo &ti = classes[name];
  448. ti.name = name;
  449. ti.inherits = p_inherits;
  450. ti.api = current_api;
  451. if (ti.inherits) {
  452. ERR_FAIL_COND(!classes.has(ti.inherits)); //it MUST be registered.
  453. ti.inherits_ptr = &classes[ti.inherits];
  454. } else {
  455. ti.inherits_ptr = nullptr;
  456. }
  457. }
  458. static MethodInfo info_from_bind(MethodBind *p_method) {
  459. MethodInfo minfo;
  460. minfo.name = p_method->get_name();
  461. minfo.id = p_method->get_method_id();
  462. for (int i = 0; i < p_method->get_argument_count(); i++) {
  463. minfo.arguments.push_back(p_method->get_argument_info(i));
  464. }
  465. minfo.return_val = p_method->get_return_info();
  466. minfo.flags = p_method->get_hint_flags();
  467. for (int i = 0; i < p_method->get_argument_count(); i++) {
  468. if (p_method->has_default_argument(i)) {
  469. minfo.default_arguments.push_back(p_method->get_default_argument(i));
  470. }
  471. }
  472. return minfo;
  473. }
  474. void ClassDB::get_method_list(StringName p_class, List<MethodInfo> *p_methods, bool p_no_inheritance, bool p_exclude_from_properties) {
  475. OBJTYPE_RLOCK;
  476. ClassInfo *type = classes.getptr(p_class);
  477. while (type) {
  478. if (type->disabled) {
  479. if (p_no_inheritance) {
  480. break;
  481. }
  482. type = type->inherits_ptr;
  483. continue;
  484. }
  485. #ifdef DEBUG_METHODS_ENABLED
  486. for (List<MethodInfo>::Element *E = type->virtual_methods.front(); E; E = E->next()) {
  487. p_methods->push_back(E->get());
  488. }
  489. for (List<StringName>::Element *E = type->method_order.front(); E; E = E->next()) {
  490. if (p_exclude_from_properties && type->methods_in_properties.has(E->get())) {
  491. continue;
  492. }
  493. MethodBind *method = type->method_map.get(E->get());
  494. MethodInfo minfo = info_from_bind(method);
  495. p_methods->push_back(minfo);
  496. }
  497. #else
  498. const StringName *K = nullptr;
  499. while ((K = type->method_map.next(K))) {
  500. MethodBind *m = type->method_map[*K];
  501. MethodInfo minfo = info_from_bind(m);
  502. p_methods->push_back(minfo);
  503. }
  504. #endif
  505. if (p_no_inheritance) {
  506. break;
  507. }
  508. type = type->inherits_ptr;
  509. }
  510. }
  511. MethodBind *ClassDB::get_method(StringName p_class, StringName p_name) {
  512. OBJTYPE_RLOCK;
  513. ClassInfo *type = classes.getptr(p_class);
  514. while (type) {
  515. MethodBind **method = type->method_map.getptr(p_name);
  516. if (method && *method) {
  517. return *method;
  518. }
  519. type = type->inherits_ptr;
  520. }
  521. return nullptr;
  522. }
  523. void ClassDB::bind_integer_constant(const StringName &p_class, const StringName &p_enum, const StringName &p_name, int p_constant) {
  524. OBJTYPE_WLOCK;
  525. ClassInfo *type = classes.getptr(p_class);
  526. ERR_FAIL_COND(!type);
  527. if (type->constant_map.has(p_name)) {
  528. ERR_FAIL();
  529. }
  530. type->constant_map[p_name] = p_constant;
  531. String enum_name = p_enum;
  532. if (enum_name != String()) {
  533. if (enum_name.find(".") != -1) {
  534. enum_name = enum_name.get_slicec('.', 1);
  535. }
  536. List<StringName> *constants_list = type->enum_map.getptr(enum_name);
  537. if (constants_list) {
  538. constants_list->push_back(p_name);
  539. } else {
  540. List<StringName> new_list;
  541. new_list.push_back(p_name);
  542. type->enum_map[enum_name] = new_list;
  543. }
  544. }
  545. #ifdef DEBUG_METHODS_ENABLED
  546. type->constant_order.push_back(p_name);
  547. #endif
  548. }
  549. void ClassDB::get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance) {
  550. OBJTYPE_RLOCK;
  551. ClassInfo *type = classes.getptr(p_class);
  552. while (type) {
  553. #ifdef DEBUG_METHODS_ENABLED
  554. for (List<StringName>::Element *E = type->constant_order.front(); E; E = E->next()) {
  555. p_constants->push_back(E->get());
  556. }
  557. #else
  558. const StringName *K = NULL;
  559. while ((K = type->constant_map.next(K))) {
  560. p_constants->push_back(*K);
  561. }
  562. #endif
  563. if (p_no_inheritance) {
  564. break;
  565. }
  566. type = type->inherits_ptr;
  567. }
  568. }
  569. int ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success) {
  570. OBJTYPE_RLOCK;
  571. ClassInfo *type = classes.getptr(p_class);
  572. while (type) {
  573. int *constant = type->constant_map.getptr(p_name);
  574. if (constant) {
  575. if (p_success) {
  576. *p_success = true;
  577. }
  578. return *constant;
  579. }
  580. type = type->inherits_ptr;
  581. }
  582. if (p_success) {
  583. *p_success = false;
  584. }
  585. return 0;
  586. }
  587. StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  588. OBJTYPE_RLOCK;
  589. ClassInfo *type = classes.getptr(p_class);
  590. while (type) {
  591. const StringName *k = nullptr;
  592. while ((k = type->enum_map.next(k))) {
  593. List<StringName> &constants_list = type->enum_map.get(*k);
  594. const List<StringName>::Element *found = constants_list.find(p_name);
  595. if (found) {
  596. return *k;
  597. }
  598. }
  599. if (p_no_inheritance) {
  600. break;
  601. }
  602. type = type->inherits_ptr;
  603. }
  604. return StringName();
  605. }
  606. void ClassDB::get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance) {
  607. OBJTYPE_RLOCK;
  608. ClassInfo *type = classes.getptr(p_class);
  609. while (type) {
  610. const StringName *k = nullptr;
  611. while ((k = type->enum_map.next(k))) {
  612. p_enums->push_back(*k);
  613. }
  614. if (p_no_inheritance) {
  615. break;
  616. }
  617. type = type->inherits_ptr;
  618. }
  619. }
  620. void ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance) {
  621. OBJTYPE_RLOCK;
  622. ClassInfo *type = classes.getptr(p_class);
  623. while (type) {
  624. const List<StringName> *constants = type->enum_map.getptr(p_enum);
  625. if (constants) {
  626. for (const List<StringName>::Element *E = constants->front(); E; E = E->next()) {
  627. p_constants->push_back(E->get());
  628. }
  629. }
  630. if (p_no_inheritance) {
  631. break;
  632. }
  633. type = type->inherits_ptr;
  634. }
  635. }
  636. bool ClassDB::has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  637. OBJTYPE_RLOCK;
  638. ClassInfo *type = classes.getptr(p_class);
  639. while (type) {
  640. if (type->enum_map.has(p_name)) {
  641. return true;
  642. }
  643. if (p_no_inheritance) {
  644. return false;
  645. }
  646. type = type->inherits_ptr;
  647. }
  648. return false;
  649. }
  650. void ClassDB::add_signal(StringName p_class, const MethodInfo &p_signal) {
  651. OBJTYPE_WLOCK;
  652. ClassInfo *type = classes.getptr(p_class);
  653. ERR_FAIL_COND(!type);
  654. StringName sname = p_signal.name;
  655. #ifdef DEBUG_METHODS_ENABLED
  656. ClassInfo *check = type;
  657. while (check) {
  658. ERR_FAIL_COND_MSG(check->signal_map.has(sname), "Class '" + String(p_class) + "' already has signal '" + String(sname) + "'.");
  659. check = check->inherits_ptr;
  660. }
  661. #endif
  662. type->signal_map[sname] = p_signal;
  663. }
  664. void ClassDB::get_signal_list(StringName p_class, List<MethodInfo> *p_signals, bool p_no_inheritance) {
  665. OBJTYPE_RLOCK;
  666. ClassInfo *type = classes.getptr(p_class);
  667. ERR_FAIL_COND(!type);
  668. ClassInfo *check = type;
  669. while (check) {
  670. const StringName *S = nullptr;
  671. while ((S = check->signal_map.next(S))) {
  672. p_signals->push_back(check->signal_map[*S]);
  673. }
  674. if (p_no_inheritance) {
  675. return;
  676. }
  677. check = check->inherits_ptr;
  678. }
  679. }
  680. bool ClassDB::has_signal(StringName p_class, StringName p_signal) {
  681. OBJTYPE_RLOCK;
  682. ClassInfo *type = classes.getptr(p_class);
  683. ClassInfo *check = type;
  684. while (check) {
  685. if (check->signal_map.has(p_signal)) {
  686. return true;
  687. }
  688. check = check->inherits_ptr;
  689. }
  690. return false;
  691. }
  692. bool ClassDB::get_signal(StringName p_class, StringName p_signal, MethodInfo *r_signal) {
  693. OBJTYPE_RLOCK;
  694. ClassInfo *type = classes.getptr(p_class);
  695. ClassInfo *check = type;
  696. while (check) {
  697. if (check->signal_map.has(p_signal)) {
  698. if (r_signal) {
  699. *r_signal = check->signal_map[p_signal];
  700. }
  701. return true;
  702. }
  703. check = check->inherits_ptr;
  704. }
  705. return false;
  706. }
  707. void ClassDB::add_property_group(StringName p_class, const String &p_name, const String &p_prefix) {
  708. OBJTYPE_WLOCK;
  709. ClassInfo *type = classes.getptr(p_class);
  710. ERR_FAIL_COND(!type);
  711. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, p_prefix, PROPERTY_USAGE_GROUP));
  712. }
  713. void ClassDB::add_property(StringName p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index) {
  714. lock.read_lock();
  715. ClassInfo *type = classes.getptr(p_class);
  716. lock.read_unlock();
  717. ERR_FAIL_COND(!type);
  718. MethodBind *mb_set = nullptr;
  719. if (p_setter) {
  720. mb_set = get_method(p_class, p_setter);
  721. #ifdef DEBUG_METHODS_ENABLED
  722. ERR_FAIL_COND_MSG(!mb_set, "Invalid setter '" + p_class + "::" + p_setter + "' for property '" + p_pinfo.name + "'.");
  723. int exp_args = 1 + (p_index >= 0 ? 1 : 0);
  724. ERR_FAIL_COND_MSG(mb_set->get_argument_count() != exp_args, "Invalid function for setter '" + p_class + "::" + p_setter + " for property '" + p_pinfo.name + "'.");
  725. #endif
  726. }
  727. MethodBind *mb_get = nullptr;
  728. if (p_getter) {
  729. mb_get = get_method(p_class, p_getter);
  730. #ifdef DEBUG_METHODS_ENABLED
  731. ERR_FAIL_COND_MSG(!mb_get, "Invalid getter '" + p_class + "::" + p_getter + "' for property '" + p_pinfo.name + "'.");
  732. int exp_args = 0 + (p_index >= 0 ? 1 : 0);
  733. ERR_FAIL_COND_MSG(mb_get->get_argument_count() != exp_args, "Invalid function for getter '" + p_class + "::" + p_getter + "' for property: '" + p_pinfo.name + "'.");
  734. #endif
  735. }
  736. #ifdef DEBUG_METHODS_ENABLED
  737. ERR_FAIL_COND_MSG(type->property_setget.has(p_pinfo.name), "Object '" + p_class + "' already has property '" + p_pinfo.name + "'.");
  738. #endif
  739. OBJTYPE_WLOCK
  740. type->property_list.push_back(p_pinfo);
  741. #ifdef DEBUG_METHODS_ENABLED
  742. if (mb_get) {
  743. type->methods_in_properties.insert(p_getter);
  744. }
  745. if (mb_set) {
  746. type->methods_in_properties.insert(p_setter);
  747. }
  748. #endif
  749. PropertySetGet psg;
  750. psg.setter = p_setter;
  751. psg.getter = p_getter;
  752. psg._setptr = mb_set;
  753. psg._getptr = mb_get;
  754. psg.index = p_index;
  755. psg.type = p_pinfo.type;
  756. type->property_setget[p_pinfo.name] = psg;
  757. }
  758. void ClassDB::set_property_default_value(StringName p_class, const StringName &p_name, const Variant &p_default) {
  759. if (!default_values.has(p_class)) {
  760. default_values[p_class] = HashMap<StringName, Variant>();
  761. }
  762. default_values[p_class][p_name] = p_default;
  763. }
  764. void ClassDB::get_property_list(StringName p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator) {
  765. OBJTYPE_RLOCK;
  766. ClassInfo *type = classes.getptr(p_class);
  767. ClassInfo *check = type;
  768. while (check) {
  769. for (List<PropertyInfo>::Element *E = check->property_list.front(); E; E = E->next()) {
  770. if (p_validator) {
  771. PropertyInfo pi = E->get();
  772. p_validator->_validate_property(pi);
  773. p_list->push_back(pi);
  774. } else {
  775. p_list->push_back(E->get());
  776. }
  777. }
  778. if (p_no_inheritance) {
  779. return;
  780. }
  781. check = check->inherits_ptr;
  782. }
  783. }
  784. bool ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid) {
  785. ERR_FAIL_NULL_V(p_object, false);
  786. ClassInfo *type = classes.getptr(p_object->get_class_name());
  787. ClassInfo *check = type;
  788. while (check) {
  789. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  790. if (psg) {
  791. if (!psg->setter) {
  792. if (r_valid) {
  793. *r_valid = false;
  794. }
  795. return true; //return true but do nothing
  796. }
  797. Variant::CallError ce;
  798. if (psg->index >= 0) {
  799. Variant index = psg->index;
  800. const Variant *arg[2] = { &index, &p_value };
  801. //p_object->call(psg->setter,arg,2,ce);
  802. if (psg->_setptr) {
  803. psg->_setptr->call(p_object, arg, 2, ce);
  804. } else {
  805. p_object->call(psg->setter, arg, 2, ce);
  806. }
  807. } else {
  808. const Variant *arg[1] = { &p_value };
  809. if (psg->_setptr) {
  810. psg->_setptr->call(p_object, arg, 1, ce);
  811. } else {
  812. p_object->call(psg->setter, arg, 1, ce);
  813. }
  814. }
  815. if (r_valid) {
  816. *r_valid = ce.error == Variant::CallError::CALL_OK;
  817. }
  818. return true;
  819. }
  820. check = check->inherits_ptr;
  821. }
  822. return false;
  823. }
  824. bool ClassDB::get_property(Object *p_object, const StringName &p_property, Variant &r_value) {
  825. ERR_FAIL_NULL_V(p_object, false);
  826. ClassInfo *type = classes.getptr(p_object->get_class_name());
  827. ClassInfo *check = type;
  828. while (check) {
  829. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  830. if (psg) {
  831. if (!psg->getter) {
  832. return true; //return true but do nothing
  833. }
  834. if (psg->index >= 0) {
  835. Variant index = psg->index;
  836. const Variant *arg[1] = { &index };
  837. Variant::CallError ce;
  838. r_value = p_object->call(psg->getter, arg, 1, ce);
  839. } else {
  840. Variant::CallError ce;
  841. if (psg->_getptr) {
  842. r_value = psg->_getptr->call(p_object, nullptr, 0, ce);
  843. } else {
  844. r_value = p_object->call(psg->getter, nullptr, 0, ce);
  845. }
  846. }
  847. return true;
  848. }
  849. const int *c = check->constant_map.getptr(p_property);
  850. if (c) {
  851. r_value = *c;
  852. return true;
  853. }
  854. check = check->inherits_ptr;
  855. }
  856. return false;
  857. }
  858. int ClassDB::get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  859. ClassInfo *type = classes.getptr(p_class);
  860. ClassInfo *check = type;
  861. while (check) {
  862. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  863. if (psg) {
  864. if (r_is_valid) {
  865. *r_is_valid = true;
  866. }
  867. return psg->index;
  868. }
  869. check = check->inherits_ptr;
  870. }
  871. if (r_is_valid) {
  872. *r_is_valid = false;
  873. }
  874. return -1;
  875. }
  876. Variant::Type ClassDB::get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  877. ClassInfo *type = classes.getptr(p_class);
  878. ClassInfo *check = type;
  879. while (check) {
  880. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  881. if (psg) {
  882. if (r_is_valid) {
  883. *r_is_valid = true;
  884. }
  885. return psg->type;
  886. }
  887. check = check->inherits_ptr;
  888. }
  889. if (r_is_valid) {
  890. *r_is_valid = false;
  891. }
  892. return Variant::NIL;
  893. }
  894. StringName ClassDB::get_property_setter(StringName p_class, const StringName &p_property) {
  895. ClassInfo *type = classes.getptr(p_class);
  896. ClassInfo *check = type;
  897. while (check) {
  898. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  899. if (psg) {
  900. return psg->setter;
  901. }
  902. check = check->inherits_ptr;
  903. }
  904. return StringName();
  905. }
  906. StringName ClassDB::get_property_getter(StringName p_class, const StringName &p_property) {
  907. ClassInfo *type = classes.getptr(p_class);
  908. ClassInfo *check = type;
  909. while (check) {
  910. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  911. if (psg) {
  912. return psg->getter;
  913. }
  914. check = check->inherits_ptr;
  915. }
  916. return StringName();
  917. }
  918. bool ClassDB::has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance) {
  919. ClassInfo *type = classes.getptr(p_class);
  920. ClassInfo *check = type;
  921. while (check) {
  922. if (check->property_setget.has(p_property)) {
  923. return true;
  924. }
  925. if (p_no_inheritance) {
  926. break;
  927. }
  928. check = check->inherits_ptr;
  929. }
  930. return false;
  931. }
  932. void ClassDB::set_method_flags(StringName p_class, StringName p_method, int p_flags) {
  933. OBJTYPE_WLOCK;
  934. ClassInfo *type = classes.getptr(p_class);
  935. ClassInfo *check = type;
  936. ERR_FAIL_COND(!check);
  937. ERR_FAIL_COND(!check->method_map.has(p_method));
  938. check->method_map[p_method]->set_hint_flags(p_flags);
  939. }
  940. bool ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inheritance) {
  941. ClassInfo *type = classes.getptr(p_class);
  942. ClassInfo *check = type;
  943. while (check) {
  944. if (check->method_map.has(p_method)) {
  945. return true;
  946. }
  947. if (p_no_inheritance) {
  948. return false;
  949. }
  950. check = check->inherits_ptr;
  951. }
  952. return false;
  953. }
  954. #ifdef DEBUG_METHODS_ENABLED
  955. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount) {
  956. StringName mdname = method_name.name;
  957. #else
  958. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const char *method_name, const Variant **p_defs, int p_defcount) {
  959. StringName mdname = StaticCString::create(method_name);
  960. #endif
  961. OBJTYPE_WLOCK;
  962. ERR_FAIL_COND_V(!p_bind, nullptr);
  963. p_bind->set_name(mdname);
  964. String instance_type = p_bind->get_instance_class();
  965. #ifdef DEBUG_ENABLED
  966. ERR_FAIL_COND_V_MSG(has_method(instance_type, mdname), nullptr, "Class " + String(instance_type) + " already has a method " + String(mdname) + ".");
  967. #endif
  968. ClassInfo *type = classes.getptr(instance_type);
  969. if (!type) {
  970. memdelete(p_bind);
  971. ERR_FAIL_V_MSG(nullptr, "Couldn't bind method '" + mdname + "' for instance '" + instance_type + "'.");
  972. }
  973. if (type->method_map.has(mdname)) {
  974. memdelete(p_bind);
  975. // overloading not supported
  976. ERR_FAIL_V_MSG(nullptr, "Method already bound '" + instance_type + "::" + mdname + "'.");
  977. }
  978. #ifdef DEBUG_METHODS_ENABLED
  979. if (method_name.args.size() > p_bind->get_argument_count()) {
  980. memdelete(p_bind);
  981. ERR_FAIL_V_MSG(nullptr, "Method definition provides more arguments than the method actually has '" + instance_type + "::" + mdname + "'.");
  982. }
  983. p_bind->set_argument_names(method_name.args);
  984. type->method_order.push_back(mdname);
  985. #endif
  986. type->method_map[mdname] = p_bind;
  987. Vector<Variant> defvals;
  988. defvals.resize(p_defcount);
  989. for (int i = 0; i < p_defcount; i++) {
  990. defvals.write[i] = *p_defs[p_defcount - i - 1];
  991. }
  992. p_bind->set_default_arguments(defvals);
  993. p_bind->set_hint_flags(p_flags);
  994. return p_bind;
  995. }
  996. void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual) {
  997. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  998. OBJTYPE_WLOCK;
  999. #ifdef DEBUG_METHODS_ENABLED
  1000. MethodInfo mi = p_method;
  1001. if (p_virtual) {
  1002. mi.flags |= METHOD_FLAG_VIRTUAL;
  1003. }
  1004. classes[p_class].virtual_methods.push_back(mi);
  1005. #endif
  1006. }
  1007. void ClassDB::get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance) {
  1008. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1009. #ifdef DEBUG_METHODS_ENABLED
  1010. ClassInfo *type = classes.getptr(p_class);
  1011. ClassInfo *check = type;
  1012. while (check) {
  1013. for (List<MethodInfo>::Element *E = check->virtual_methods.front(); E; E = E->next()) {
  1014. p_methods->push_back(E->get());
  1015. }
  1016. if (p_no_inheritance) {
  1017. return;
  1018. }
  1019. check = check->inherits_ptr;
  1020. }
  1021. #endif
  1022. }
  1023. void ClassDB::set_class_enabled(StringName p_class, bool p_enable) {
  1024. OBJTYPE_WLOCK;
  1025. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1026. classes[p_class].disabled = !p_enable;
  1027. }
  1028. bool ClassDB::is_class_enabled(StringName p_class) {
  1029. OBJTYPE_RLOCK;
  1030. ClassInfo *ti = classes.getptr(p_class);
  1031. if (!ti || !ti->creation_func) {
  1032. if (compat_classes.has(p_class)) {
  1033. ti = classes.getptr(compat_classes[p_class]);
  1034. }
  1035. }
  1036. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  1037. return !ti->disabled;
  1038. }
  1039. bool ClassDB::is_class_exposed(StringName p_class) {
  1040. OBJTYPE_RLOCK;
  1041. ClassInfo *ti = classes.getptr(p_class);
  1042. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  1043. return ti->exposed;
  1044. }
  1045. StringName ClassDB::get_category(const StringName &p_node) {
  1046. ERR_FAIL_COND_V(!classes.has(p_node), StringName());
  1047. #ifdef DEBUG_ENABLED
  1048. return classes[p_node].category;
  1049. #else
  1050. return StringName();
  1051. #endif
  1052. }
  1053. void ClassDB::add_resource_base_extension(const StringName &p_extension, const StringName &p_class) {
  1054. if (resource_base_extensions.has(p_extension)) {
  1055. return;
  1056. }
  1057. resource_base_extensions[p_extension] = p_class;
  1058. }
  1059. void ClassDB::get_resource_base_extensions(List<String> *p_extensions) {
  1060. const StringName *K = nullptr;
  1061. while ((K = resource_base_extensions.next(K))) {
  1062. p_extensions->push_back(*K);
  1063. }
  1064. }
  1065. void ClassDB::get_extensions_for_type(const StringName &p_class, List<String> *p_extensions) {
  1066. const StringName *K = nullptr;
  1067. while ((K = resource_base_extensions.next(K))) {
  1068. StringName cmp = resource_base_extensions[*K];
  1069. if (is_parent_class(p_class, cmp) || is_parent_class(cmp, p_class)) {
  1070. p_extensions->push_back(*K);
  1071. }
  1072. }
  1073. }
  1074. HashMap<StringName, HashMap<StringName, Variant>> ClassDB::default_values;
  1075. Set<StringName> ClassDB::default_values_cached;
  1076. Variant ClassDB::class_get_default_property_value(const StringName &p_class, const StringName &p_property, bool *r_valid) {
  1077. if (!default_values_cached.has(p_class)) {
  1078. if (!default_values.has(p_class)) {
  1079. default_values[p_class] = HashMap<StringName, Variant>();
  1080. }
  1081. Object *c = nullptr;
  1082. bool cleanup_c = false;
  1083. if (Engine::get_singleton()->has_singleton(p_class)) {
  1084. c = Engine::get_singleton()->get_singleton_object(p_class);
  1085. cleanup_c = false;
  1086. } else if (ClassDB::can_instance(p_class)) {
  1087. c = ClassDB::instance(p_class);
  1088. cleanup_c = true;
  1089. }
  1090. if (c) {
  1091. List<PropertyInfo> plist;
  1092. c->get_property_list(&plist);
  1093. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1094. if (E->get().usage & (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR)) {
  1095. if (!default_values[p_class].has(E->get().name)) {
  1096. Variant v = c->get(E->get().name);
  1097. default_values[p_class][E->get().name] = v;
  1098. }
  1099. }
  1100. }
  1101. if (cleanup_c) {
  1102. memdelete(c);
  1103. }
  1104. }
  1105. default_values_cached.insert(p_class);
  1106. }
  1107. if (!default_values.has(p_class)) {
  1108. if (r_valid != nullptr) {
  1109. *r_valid = false;
  1110. }
  1111. return Variant();
  1112. }
  1113. if (!default_values[p_class].has(p_property)) {
  1114. if (r_valid != nullptr) {
  1115. *r_valid = false;
  1116. }
  1117. return Variant();
  1118. }
  1119. if (r_valid != nullptr) {
  1120. *r_valid = true;
  1121. }
  1122. return default_values[p_class][p_property];
  1123. }
  1124. RWLock ClassDB::lock;
  1125. void ClassDB::cleanup_defaults() {
  1126. default_values.clear();
  1127. default_values_cached.clear();
  1128. }
  1129. void ClassDB::cleanup() {
  1130. //OBJTYPE_LOCK; hah not here
  1131. const StringName *k = nullptr;
  1132. while ((k = classes.next(k))) {
  1133. ClassInfo &ti = classes[*k];
  1134. const StringName *m = nullptr;
  1135. while ((m = ti.method_map.next(m))) {
  1136. memdelete(ti.method_map[*m]);
  1137. }
  1138. }
  1139. classes.clear();
  1140. resource_base_extensions.clear();
  1141. compat_classes.clear();
  1142. }
  1143. //