class_db.cpp 48 KB

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