class_db.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. /*************************************************************************/
  2. /* class_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os/mutex.h"
  32. #include "version.h"
  33. #ifdef NO_THREADS
  34. #define OBJTYPE_RLOCK
  35. #define OBJTYPE_WLOCK
  36. #else
  37. #define OBJTYPE_RLOCK RWLockRead _rw_lockr_(lock);
  38. #define OBJTYPE_WLOCK RWLockWrite _rw_lockw_(lock);
  39. #endif
  40. #ifdef DEBUG_METHODS_ENABLED
  41. ParamDef::ParamDef(const Variant &p_variant)
  42. : used(true),
  43. val(p_variant) {
  44. }
  45. MethodDefinition D_METHOD(const char *p_name) {
  46. MethodDefinition md;
  47. md.name = StaticCString::create(p_name);
  48. return md;
  49. }
  50. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1) {
  51. MethodDefinition md;
  52. md.name = StaticCString::create(p_name);
  53. md.args.push_back(StaticCString::create(p_arg1));
  54. return md;
  55. }
  56. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2) {
  57. MethodDefinition md;
  58. md.name = StaticCString::create(p_name);
  59. md.args.resize(2);
  60. md.args[0] = StaticCString::create(p_arg1);
  61. md.args[1] = StaticCString::create(p_arg2);
  62. return md;
  63. }
  64. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3) {
  65. MethodDefinition md;
  66. md.name = StaticCString::create(p_name);
  67. md.args.resize(3);
  68. md.args[0] = StaticCString::create(p_arg1);
  69. md.args[1] = StaticCString::create(p_arg2);
  70. md.args[2] = StaticCString::create(p_arg3);
  71. return md;
  72. }
  73. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4) {
  74. MethodDefinition md;
  75. md.name = StaticCString::create(p_name);
  76. md.args.resize(4);
  77. md.args[0] = StaticCString::create(p_arg1);
  78. md.args[1] = StaticCString::create(p_arg2);
  79. md.args[2] = StaticCString::create(p_arg3);
  80. md.args[3] = StaticCString::create(p_arg4);
  81. return md;
  82. }
  83. 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) {
  84. MethodDefinition md;
  85. md.name = StaticCString::create(p_name);
  86. md.args.resize(5);
  87. md.args[0] = StaticCString::create(p_arg1);
  88. md.args[1] = StaticCString::create(p_arg2);
  89. md.args[2] = StaticCString::create(p_arg3);
  90. md.args[3] = StaticCString::create(p_arg4);
  91. md.args[4] = StaticCString::create(p_arg5);
  92. return md;
  93. }
  94. 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) {
  95. MethodDefinition md;
  96. md.name = StaticCString::create(p_name);
  97. md.args.resize(6);
  98. md.args[0] = StaticCString::create(p_arg1);
  99. md.args[1] = StaticCString::create(p_arg2);
  100. md.args[2] = StaticCString::create(p_arg3);
  101. md.args[3] = StaticCString::create(p_arg4);
  102. md.args[4] = StaticCString::create(p_arg5);
  103. md.args[5] = StaticCString::create(p_arg6);
  104. return md;
  105. }
  106. 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) {
  107. MethodDefinition md;
  108. md.name = StaticCString::create(p_name);
  109. md.args.resize(7);
  110. md.args[0] = StaticCString::create(p_arg1);
  111. md.args[1] = StaticCString::create(p_arg2);
  112. md.args[2] = StaticCString::create(p_arg3);
  113. md.args[3] = StaticCString::create(p_arg4);
  114. md.args[4] = StaticCString::create(p_arg5);
  115. md.args[5] = StaticCString::create(p_arg6);
  116. md.args[6] = StaticCString::create(p_arg7);
  117. return md;
  118. }
  119. 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) {
  120. MethodDefinition md;
  121. md.name = StaticCString::create(p_name);
  122. md.args.resize(8);
  123. md.args[0] = StaticCString::create(p_arg1);
  124. md.args[1] = StaticCString::create(p_arg2);
  125. md.args[2] = StaticCString::create(p_arg3);
  126. md.args[3] = StaticCString::create(p_arg4);
  127. md.args[4] = StaticCString::create(p_arg5);
  128. md.args[5] = StaticCString::create(p_arg6);
  129. md.args[6] = StaticCString::create(p_arg7);
  130. md.args[7] = StaticCString::create(p_arg8);
  131. return md;
  132. }
  133. 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) {
  134. MethodDefinition md;
  135. md.name = StaticCString::create(p_name);
  136. md.args.resize(9);
  137. md.args[0] = StaticCString::create(p_arg1);
  138. md.args[1] = StaticCString::create(p_arg2);
  139. md.args[2] = StaticCString::create(p_arg3);
  140. md.args[3] = StaticCString::create(p_arg4);
  141. md.args[4] = StaticCString::create(p_arg5);
  142. md.args[5] = StaticCString::create(p_arg6);
  143. md.args[6] = StaticCString::create(p_arg7);
  144. md.args[7] = StaticCString::create(p_arg8);
  145. md.args[8] = StaticCString::create(p_arg9);
  146. return md;
  147. }
  148. 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) {
  149. MethodDefinition md;
  150. md.name = StaticCString::create(p_name);
  151. md.args.resize(10);
  152. md.args[0] = StaticCString::create(p_arg1);
  153. md.args[1] = StaticCString::create(p_arg2);
  154. md.args[2] = StaticCString::create(p_arg3);
  155. md.args[3] = StaticCString::create(p_arg4);
  156. md.args[4] = StaticCString::create(p_arg5);
  157. md.args[5] = StaticCString::create(p_arg6);
  158. md.args[6] = StaticCString::create(p_arg7);
  159. md.args[7] = StaticCString::create(p_arg8);
  160. md.args[8] = StaticCString::create(p_arg9);
  161. md.args[9] = StaticCString::create(p_arg10);
  162. return md;
  163. }
  164. #endif
  165. ClassDB::APIType ClassDB::current_api = API_CORE;
  166. void ClassDB::set_current_api(APIType p_api) {
  167. current_api = p_api;
  168. }
  169. HashMap<StringName, ClassDB::ClassInfo, StringNameHasher> ClassDB::classes;
  170. HashMap<StringName, StringName, StringNameHasher> ClassDB::resource_base_extensions;
  171. HashMap<StringName, StringName, StringNameHasher> ClassDB::compat_classes;
  172. ClassDB::ClassInfo::ClassInfo() {
  173. creation_func = NULL;
  174. inherits_ptr = NULL;
  175. disabled = false;
  176. }
  177. ClassDB::ClassInfo::~ClassInfo() {
  178. }
  179. bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) {
  180. OBJTYPE_RLOCK;
  181. StringName inherits = p_class;
  182. while (inherits.operator String().length()) {
  183. if (inherits == p_inherits)
  184. return true;
  185. inherits = get_parent_class(inherits);
  186. }
  187. return false;
  188. }
  189. void ClassDB::get_class_list(List<StringName> *p_classes) {
  190. OBJTYPE_RLOCK;
  191. const StringName *k = NULL;
  192. while ((k = classes.next(k))) {
  193. p_classes->push_back(*k);
  194. }
  195. p_classes->sort();
  196. }
  197. void ClassDB::get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
  198. OBJTYPE_RLOCK;
  199. const StringName *k = NULL;
  200. while ((k = classes.next(k))) {
  201. if (*k != p_class && is_parent_class(*k, p_class))
  202. p_classes->push_back(*k);
  203. }
  204. }
  205. StringName ClassDB::get_parent_class_nocheck(const StringName &p_class) {
  206. OBJTYPE_RLOCK;
  207. ClassInfo *ti = classes.getptr(p_class);
  208. if (!ti)
  209. return StringName();
  210. return ti->inherits;
  211. }
  212. StringName ClassDB::get_parent_class(const StringName &p_class) {
  213. OBJTYPE_RLOCK;
  214. ClassInfo *ti = classes.getptr(p_class);
  215. ERR_FAIL_COND_V(!ti, StringName());
  216. return ti->inherits;
  217. }
  218. ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) {
  219. OBJTYPE_RLOCK;
  220. ClassInfo *ti = classes.getptr(p_class);
  221. ERR_FAIL_COND_V(!ti, API_NONE);
  222. return ti->api;
  223. }
  224. uint64_t ClassDB::get_api_hash(APIType p_api) {
  225. OBJTYPE_RLOCK;
  226. #ifdef DEBUG_METHODS_ENABLED
  227. uint64_t hash = hash_djb2_one_64(HashMapHasherDefault::hash(VERSION_FULL_NAME));
  228. List<StringName> names;
  229. const StringName *k = NULL;
  230. while ((k = classes.next(k))) {
  231. names.push_back(*k);
  232. }
  233. //must be alphabetically sorted for hash to compute
  234. names.sort_custom<StringName::AlphCompare>();
  235. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  236. ClassInfo *t = classes.getptr(E->get());
  237. ERR_FAIL_COND_V(!t, 0);
  238. if (t->api != p_api)
  239. continue;
  240. hash = hash_djb2_one_64(t->name.hash(), hash);
  241. hash = hash_djb2_one_64(t->inherits.hash(), hash);
  242. { //methods
  243. List<StringName> snames;
  244. k = NULL;
  245. while ((k = t->method_map.next(k))) {
  246. snames.push_back(*k);
  247. }
  248. snames.sort_custom<StringName::AlphCompare>();
  249. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  250. MethodBind *mb = t->method_map[F->get()];
  251. hash = hash_djb2_one_64(mb->get_name().hash(), hash);
  252. hash = hash_djb2_one_64(mb->get_argument_count(), hash);
  253. hash = hash_djb2_one_64(mb->get_argument_type(-1), hash); //return
  254. for (int i = 0; i < mb->get_argument_count(); i++) {
  255. hash = hash_djb2_one_64(mb->get_argument_info(i).type, hash);
  256. hash = hash_djb2_one_64(mb->get_argument_info(i).name.hash(), hash);
  257. hash = hash_djb2_one_64(mb->get_argument_info(i).hint, hash);
  258. hash = hash_djb2_one_64(mb->get_argument_info(i).hint_string.hash(), hash);
  259. }
  260. hash = hash_djb2_one_64(mb->get_default_argument_count(), hash);
  261. for (int i = 0; i < mb->get_default_argument_count(); i++) {
  262. //hash should not change, i hope for tis
  263. Variant da = mb->get_default_argument(i);
  264. hash = hash_djb2_one_64(da.hash(), hash);
  265. }
  266. hash = hash_djb2_one_64(mb->get_hint_flags(), hash);
  267. }
  268. }
  269. { //constants
  270. List<StringName> snames;
  271. k = NULL;
  272. while ((k = t->constant_map.next(k))) {
  273. snames.push_back(*k);
  274. }
  275. snames.sort_custom<StringName::AlphCompare>();
  276. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  277. hash = hash_djb2_one_64(F->get().hash(), hash);
  278. hash = hash_djb2_one_64(t->constant_map[F->get()], hash);
  279. }
  280. }
  281. { //signals
  282. List<StringName> snames;
  283. k = NULL;
  284. while ((k = t->signal_map.next(k))) {
  285. snames.push_back(*k);
  286. }
  287. snames.sort_custom<StringName::AlphCompare>();
  288. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  289. MethodInfo &mi = t->signal_map[F->get()];
  290. hash = hash_djb2_one_64(F->get().hash(), hash);
  291. for (int i = 0; i < mi.arguments.size(); i++) {
  292. hash = hash_djb2_one_64(mi.arguments[i].type, hash);
  293. }
  294. }
  295. }
  296. { //properties
  297. List<StringName> snames;
  298. k = NULL;
  299. while ((k = t->property_setget.next(k))) {
  300. snames.push_back(*k);
  301. }
  302. snames.sort_custom<StringName::AlphCompare>();
  303. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  304. PropertySetGet *psg = t->property_setget.getptr(F->get());
  305. hash = hash_djb2_one_64(F->get().hash(), hash);
  306. hash = hash_djb2_one_64(psg->setter.hash(), hash);
  307. hash = hash_djb2_one_64(psg->getter.hash(), hash);
  308. }
  309. }
  310. //property list
  311. for (List<PropertyInfo>::Element *F = t->property_list.front(); F; F = F->next()) {
  312. hash = hash_djb2_one_64(F->get().name.hash(), hash);
  313. hash = hash_djb2_one_64(F->get().type, hash);
  314. hash = hash_djb2_one_64(F->get().hint, hash);
  315. hash = hash_djb2_one_64(F->get().hint_string.hash(), hash);
  316. hash = hash_djb2_one_64(F->get().usage, hash);
  317. }
  318. }
  319. return hash;
  320. #else
  321. return 0;
  322. #endif
  323. }
  324. bool ClassDB::class_exists(const StringName &p_class) {
  325. OBJTYPE_RLOCK;
  326. return classes.has(p_class);
  327. }
  328. void ClassDB::add_compatibility_class(const StringName &p_class, const StringName &p_fallback) {
  329. OBJTYPE_WLOCK;
  330. compat_classes[p_class] = p_fallback;
  331. }
  332. Object *ClassDB::instance(const StringName &p_class) {
  333. ClassInfo *ti;
  334. {
  335. OBJTYPE_RLOCK;
  336. ti = classes.getptr(p_class);
  337. if (!ti || ti->disabled || !ti->creation_func) {
  338. if (compat_classes.has(p_class)) {
  339. ti = classes.getptr(compat_classes[p_class]);
  340. }
  341. }
  342. ERR_FAIL_COND_V(!ti, NULL);
  343. ERR_FAIL_COND_V(ti->disabled, NULL);
  344. ERR_FAIL_COND_V(!ti->creation_func, NULL);
  345. }
  346. return ti->creation_func();
  347. }
  348. bool ClassDB::can_instance(const StringName &p_class) {
  349. OBJTYPE_RLOCK;
  350. ClassInfo *ti = classes.getptr(p_class);
  351. ERR_FAIL_COND_V(!ti, false);
  352. return (!ti->disabled && ti->creation_func != NULL);
  353. }
  354. void ClassDB::_add_class2(const StringName &p_class, const StringName &p_inherits) {
  355. OBJTYPE_WLOCK;
  356. StringName name = p_class;
  357. ERR_FAIL_COND(classes.has(name));
  358. classes[name] = ClassInfo();
  359. ClassInfo &ti = classes[name];
  360. ti.name = name;
  361. ti.inherits = p_inherits;
  362. ti.api = current_api;
  363. if (ti.inherits) {
  364. ERR_FAIL_COND(!classes.has(ti.inherits)); //it MUST be registered.
  365. ti.inherits_ptr = &classes[ti.inherits];
  366. } else {
  367. ti.inherits_ptr = NULL;
  368. }
  369. }
  370. void ClassDB::get_method_list(StringName p_class, List<MethodInfo> *p_methods, bool p_no_inheritance, bool p_exclude_from_properties) {
  371. OBJTYPE_RLOCK;
  372. ClassInfo *type = classes.getptr(p_class);
  373. while (type) {
  374. if (type->disabled) {
  375. if (p_no_inheritance)
  376. break;
  377. type = type->inherits_ptr;
  378. continue;
  379. }
  380. #ifdef DEBUG_METHODS_ENABLED
  381. for (List<MethodInfo>::Element *E = type->virtual_methods.front(); E; E = E->next()) {
  382. p_methods->push_back(E->get());
  383. }
  384. for (List<StringName>::Element *E = type->method_order.front(); E; E = E->next()) {
  385. MethodBind *method = type->method_map.get(E->get());
  386. MethodInfo minfo;
  387. minfo.name = E->get();
  388. minfo.id = method->get_method_id();
  389. if (p_exclude_from_properties && type->methods_in_properties.has(minfo.name))
  390. continue;
  391. for (int i = 0; i < method->get_argument_count(); i++) {
  392. //Variant::Type t=method->get_argument_type(i);
  393. minfo.arguments.push_back(method->get_argument_info(i));
  394. }
  395. minfo.return_val = method->get_return_info();
  396. minfo.flags = method->get_hint_flags();
  397. minfo.default_arguments = method->get_default_arguments();
  398. p_methods->push_back(minfo);
  399. }
  400. #else
  401. const StringName *K = NULL;
  402. while ((K = type->method_map.next(K))) {
  403. MethodBind *m = type->method_map[*K];
  404. MethodInfo mi;
  405. mi.name = m->get_name();
  406. p_methods->push_back(mi);
  407. }
  408. #endif
  409. if (p_no_inheritance)
  410. break;
  411. type = type->inherits_ptr;
  412. }
  413. }
  414. MethodBind *ClassDB::get_method(StringName p_class, StringName p_name) {
  415. OBJTYPE_RLOCK;
  416. ClassInfo *type = classes.getptr(p_class);
  417. while (type) {
  418. MethodBind **method = type->method_map.getptr(p_name);
  419. if (method && *method)
  420. return *method;
  421. type = type->inherits_ptr;
  422. }
  423. return NULL;
  424. }
  425. void ClassDB::bind_integer_constant(const StringName &p_class, const StringName &p_enum, const StringName &p_name, int p_constant) {
  426. OBJTYPE_WLOCK;
  427. ClassInfo *type = classes.getptr(p_class);
  428. if (!type) {
  429. ERR_FAIL_COND(!type);
  430. }
  431. if (type->constant_map.has(p_name)) {
  432. ERR_FAIL();
  433. }
  434. type->constant_map[p_name] = p_constant;
  435. #ifdef DEBUG_METHODS_ENABLED
  436. String enum_name = p_enum;
  437. if (enum_name != String()) {
  438. if (enum_name.find(".") != -1) {
  439. enum_name = enum_name.get_slicec('.', 1);
  440. }
  441. List<StringName> *constants_list = type->enum_map.getptr(enum_name);
  442. if (constants_list) {
  443. constants_list->push_back(p_name);
  444. } else {
  445. List<StringName> new_list;
  446. new_list.push_back(p_name);
  447. type->enum_map[enum_name] = new_list;
  448. }
  449. }
  450. type->constant_order.push_back(p_name);
  451. #endif
  452. }
  453. void ClassDB::get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance) {
  454. OBJTYPE_RLOCK;
  455. ClassInfo *type = classes.getptr(p_class);
  456. while (type) {
  457. #ifdef DEBUG_METHODS_ENABLED
  458. for (List<StringName>::Element *E = type->constant_order.front(); E; E = E->next())
  459. p_constants->push_back(E->get());
  460. #else
  461. const StringName *K = NULL;
  462. while ((K = type->constant_map.next(K))) {
  463. p_constants->push_back(*K);
  464. }
  465. #endif
  466. if (p_no_inheritance)
  467. break;
  468. type = type->inherits_ptr;
  469. }
  470. }
  471. int ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success) {
  472. OBJTYPE_RLOCK;
  473. ClassInfo *type = classes.getptr(p_class);
  474. while (type) {
  475. int *constant = type->constant_map.getptr(p_name);
  476. if (constant) {
  477. if (p_success)
  478. *p_success = true;
  479. return *constant;
  480. }
  481. type = type->inherits_ptr;
  482. }
  483. if (p_success)
  484. *p_success = false;
  485. return 0;
  486. }
  487. #ifdef DEBUG_METHODS_ENABLED
  488. StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  489. OBJTYPE_RLOCK;
  490. ClassInfo *type = classes.getptr(p_class);
  491. while (type) {
  492. const StringName *k = NULL;
  493. while ((k = type->enum_map.next(k))) {
  494. List<StringName> &constants_list = type->enum_map.get(*k);
  495. const List<StringName>::Element *found = constants_list.find(p_name);
  496. if (found)
  497. return *k;
  498. }
  499. if (p_no_inheritance)
  500. break;
  501. type = type->inherits_ptr;
  502. }
  503. return StringName();
  504. }
  505. void ClassDB::get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance) {
  506. OBJTYPE_RLOCK;
  507. ClassInfo *type = classes.getptr(p_class);
  508. while (type) {
  509. const StringName *k = NULL;
  510. while ((k = type->enum_map.next(k))) {
  511. p_enums->push_back(*k);
  512. }
  513. if (p_no_inheritance)
  514. break;
  515. type = type->inherits_ptr;
  516. }
  517. }
  518. void ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance) {
  519. OBJTYPE_RLOCK;
  520. ClassInfo *type = classes.getptr(p_class);
  521. while (type) {
  522. const List<StringName> *constants = type->enum_map.getptr(p_enum);
  523. if (constants) {
  524. for (const List<StringName>::Element *E = constants->front(); E; E = E->next()) {
  525. p_constants->push_back(E->get());
  526. }
  527. }
  528. if (p_no_inheritance)
  529. break;
  530. type = type->inherits_ptr;
  531. }
  532. }
  533. #endif
  534. void ClassDB::add_signal(StringName p_class, const MethodInfo &p_signal) {
  535. OBJTYPE_WLOCK;
  536. ClassInfo *type = classes.getptr(p_class);
  537. ERR_FAIL_COND(!type);
  538. ClassInfo *check = type;
  539. StringName sname = p_signal.name;
  540. #ifdef DEBUG_METHODS_ENABLED
  541. while (check) {
  542. if (check->signal_map.has(sname)) {
  543. ERR_EXPLAIN("Type " + String(p_class) + " already has signal: " + String(sname));
  544. ERR_FAIL();
  545. }
  546. check = check->inherits_ptr;
  547. }
  548. #endif
  549. type->signal_map[sname] = p_signal;
  550. }
  551. void ClassDB::get_signal_list(StringName p_class, List<MethodInfo> *p_signals, bool p_no_inheritance) {
  552. OBJTYPE_RLOCK;
  553. ClassInfo *type = classes.getptr(p_class);
  554. ERR_FAIL_COND(!type);
  555. ClassInfo *check = type;
  556. while (check) {
  557. const StringName *S = NULL;
  558. while ((S = check->signal_map.next(S))) {
  559. p_signals->push_back(check->signal_map[*S]);
  560. }
  561. if (p_no_inheritance)
  562. return;
  563. check = check->inherits_ptr;
  564. }
  565. }
  566. bool ClassDB::has_signal(StringName p_class, StringName p_signal) {
  567. OBJTYPE_RLOCK;
  568. ClassInfo *type = classes.getptr(p_class);
  569. ClassInfo *check = type;
  570. while (check) {
  571. if (check->signal_map.has(p_signal))
  572. return true;
  573. check = check->inherits_ptr;
  574. }
  575. return false;
  576. }
  577. bool ClassDB::get_signal(StringName p_class, StringName p_signal, MethodInfo *r_signal) {
  578. OBJTYPE_RLOCK;
  579. ClassInfo *type = classes.getptr(p_class);
  580. ClassInfo *check = type;
  581. while (check) {
  582. if (check->signal_map.has(p_signal)) {
  583. if (r_signal) {
  584. *r_signal = check->signal_map[p_signal];
  585. }
  586. return true;
  587. }
  588. check = check->inherits_ptr;
  589. }
  590. return false;
  591. }
  592. void ClassDB::add_property_group(StringName p_class, const String &p_name, const String &p_prefix) {
  593. OBJTYPE_WLOCK;
  594. ClassInfo *type = classes.getptr(p_class);
  595. ERR_FAIL_COND(!type);
  596. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, p_prefix, PROPERTY_USAGE_GROUP));
  597. }
  598. void ClassDB::add_property(StringName p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index) {
  599. #ifndef NO_THREADS
  600. lock->read_lock();
  601. #endif
  602. ClassInfo *type = classes.getptr(p_class);
  603. #ifndef NO_THREADS
  604. lock->read_unlock();
  605. #endif
  606. ERR_FAIL_COND(!type);
  607. MethodBind *mb_set = NULL;
  608. if (p_setter) {
  609. mb_set = get_method(p_class, p_setter);
  610. #ifdef DEBUG_METHODS_ENABLED
  611. if (!mb_set) {
  612. ERR_EXPLAIN("Invalid Setter: " + p_class + "::" + p_setter + " for property: " + p_pinfo.name);
  613. ERR_FAIL_COND(!mb_set);
  614. } else {
  615. int exp_args = 1 + (p_index >= 0 ? 1 : 0);
  616. if (mb_set->get_argument_count() != exp_args) {
  617. ERR_EXPLAIN("Invalid Function for Setter: " + p_class + "::" + p_setter + " for property: " + p_pinfo.name);
  618. ERR_FAIL();
  619. }
  620. }
  621. #endif
  622. }
  623. MethodBind *mb_get = NULL;
  624. if (p_getter) {
  625. MethodBind *mb_get = get_method(p_class, p_getter);
  626. #ifdef DEBUG_METHODS_ENABLED
  627. if (!mb_get) {
  628. ERR_EXPLAIN("Invalid Getter: " + p_class + "::" + p_getter + " for property: " + p_pinfo.name);
  629. ERR_FAIL_COND(!mb_get);
  630. } else {
  631. int exp_args = 0 + (p_index >= 0 ? 1 : 0);
  632. if (mb_get->get_argument_count() != exp_args) {
  633. ERR_EXPLAIN("Invalid Function for Getter: " + p_class + "::" + p_getter + " for property: " + p_pinfo.name);
  634. ERR_FAIL();
  635. }
  636. }
  637. #endif
  638. }
  639. #ifdef DEBUG_METHODS_ENABLED
  640. if (type->property_setget.has(p_pinfo.name)) {
  641. ERR_EXPLAIN("Object already has property: " + p_class);
  642. ERR_FAIL();
  643. }
  644. #endif
  645. OBJTYPE_WLOCK
  646. type->property_list.push_back(p_pinfo);
  647. #ifdef DEBUG_METHODS_ENABLED
  648. if (mb_get) {
  649. type->methods_in_properties.insert(p_getter);
  650. }
  651. if (mb_set) {
  652. type->methods_in_properties.insert(p_setter);
  653. }
  654. #endif
  655. PropertySetGet psg;
  656. psg.setter = p_setter;
  657. psg.getter = p_getter;
  658. psg._setptr = mb_set;
  659. psg._getptr = mb_get;
  660. psg.index = p_index;
  661. psg.type = p_pinfo.type;
  662. type->property_setget[p_pinfo.name] = psg;
  663. }
  664. void ClassDB::get_property_list(StringName p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator) {
  665. OBJTYPE_RLOCK;
  666. ClassInfo *type = classes.getptr(p_class);
  667. ClassInfo *check = type;
  668. while (check) {
  669. for (List<PropertyInfo>::Element *E = check->property_list.front(); E; E = E->next()) {
  670. if (p_validator) {
  671. PropertyInfo pi = E->get();
  672. p_validator->_validate_property(pi);
  673. p_list->push_back(pi);
  674. } else {
  675. p_list->push_back(E->get());
  676. }
  677. }
  678. if (p_no_inheritance)
  679. return;
  680. check = check->inherits_ptr;
  681. }
  682. }
  683. bool ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid) {
  684. ClassInfo *type = classes.getptr(p_object->get_class_name());
  685. ClassInfo *check = type;
  686. while (check) {
  687. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  688. if (psg) {
  689. if (!psg->setter) {
  690. if (r_valid)
  691. *r_valid = false;
  692. return true; //return true but do nothing
  693. }
  694. Variant::CallError ce;
  695. if (psg->index >= 0) {
  696. Variant index = psg->index;
  697. const Variant *arg[2] = { &index, &p_value };
  698. //p_object->call(psg->setter,arg,2,ce);
  699. if (psg->_setptr) {
  700. psg->_setptr->call(p_object, arg, 2, ce);
  701. } else {
  702. p_object->call(psg->setter, arg, 2, ce);
  703. }
  704. } else {
  705. const Variant *arg[1] = { &p_value };
  706. if (psg->_setptr) {
  707. psg->_setptr->call(p_object, arg, 1, ce);
  708. } else {
  709. p_object->call(psg->setter, arg, 1, ce);
  710. }
  711. }
  712. if (r_valid)
  713. *r_valid = ce.error == Variant::CallError::CALL_OK;
  714. return true;
  715. }
  716. check = check->inherits_ptr;
  717. }
  718. return false;
  719. }
  720. bool ClassDB::get_property(Object *p_object, const StringName &p_property, Variant &r_value) {
  721. ClassInfo *type = classes.getptr(p_object->get_class_name());
  722. ClassInfo *check = type;
  723. while (check) {
  724. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  725. if (psg) {
  726. if (!psg->getter)
  727. return true; //return true but do nothing
  728. if (psg->index >= 0) {
  729. Variant index = psg->index;
  730. const Variant *arg[1] = { &index };
  731. Variant::CallError ce;
  732. r_value = p_object->call(psg->getter, arg, 1, ce);
  733. } else {
  734. Variant::CallError ce;
  735. if (psg->_getptr) {
  736. r_value = psg->_getptr->call(p_object, NULL, 0, ce);
  737. } else {
  738. r_value = p_object->call(psg->getter, NULL, 0, ce);
  739. }
  740. }
  741. return true;
  742. }
  743. const int *c = check->constant_map.getptr(p_property);
  744. if (c) {
  745. r_value = *c;
  746. return true;
  747. }
  748. //if (check->constant_map.fin)
  749. check = check->inherits_ptr;
  750. }
  751. return false;
  752. }
  753. int ClassDB::get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  754. ClassInfo *type = classes.getptr(p_class);
  755. ClassInfo *check = type;
  756. while (check) {
  757. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  758. if (psg) {
  759. if (r_is_valid)
  760. *r_is_valid = true;
  761. return psg->index;
  762. }
  763. check = check->inherits_ptr;
  764. }
  765. if (r_is_valid)
  766. *r_is_valid = false;
  767. return -1;
  768. }
  769. Variant::Type ClassDB::get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  770. ClassInfo *type = classes.getptr(p_class);
  771. ClassInfo *check = type;
  772. while (check) {
  773. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  774. if (psg) {
  775. if (r_is_valid)
  776. *r_is_valid = true;
  777. return psg->type;
  778. }
  779. check = check->inherits_ptr;
  780. }
  781. if (r_is_valid)
  782. *r_is_valid = false;
  783. return Variant::NIL;
  784. }
  785. StringName ClassDB::get_property_setter(StringName p_class, const StringName p_property) {
  786. ClassInfo *type = classes.getptr(p_class);
  787. ClassInfo *check = type;
  788. while (check) {
  789. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  790. if (psg) {
  791. return psg->setter;
  792. }
  793. check = check->inherits_ptr;
  794. }
  795. return StringName();
  796. }
  797. StringName ClassDB::get_property_getter(StringName p_class, const StringName p_property) {
  798. ClassInfo *type = classes.getptr(p_class);
  799. ClassInfo *check = type;
  800. while (check) {
  801. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  802. if (psg) {
  803. return psg->getter;
  804. }
  805. check = check->inherits_ptr;
  806. }
  807. return StringName();
  808. }
  809. bool ClassDB::has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance) {
  810. ClassInfo *type = classes.getptr(p_class);
  811. ClassInfo *check = type;
  812. while (check) {
  813. if (check->property_setget.has(p_property))
  814. return true;
  815. if (p_no_inheritance)
  816. break;
  817. check = check->inherits_ptr;
  818. }
  819. return false;
  820. }
  821. void ClassDB::set_method_flags(StringName p_class, StringName p_method, int p_flags) {
  822. OBJTYPE_WLOCK;
  823. ClassInfo *type = classes.getptr(p_class);
  824. ClassInfo *check = type;
  825. ERR_FAIL_COND(!check);
  826. ERR_FAIL_COND(!check->method_map.has(p_method));
  827. check->method_map[p_method]->set_hint_flags(p_flags);
  828. }
  829. bool ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inheritance) {
  830. ClassInfo *type = classes.getptr(p_class);
  831. ClassInfo *check = type;
  832. while (check) {
  833. if (check->method_map.has(p_method))
  834. return true;
  835. if (p_no_inheritance)
  836. return false;
  837. check = check->inherits_ptr;
  838. }
  839. return false;
  840. }
  841. bool ClassDB::get_setter_and_type_for_property(const StringName &p_class, const StringName &p_prop, StringName &r_class, StringName &r_setter) {
  842. ClassInfo *type = classes.getptr(p_class);
  843. ClassInfo *check = type;
  844. while (check) {
  845. if (check->property_setget.has(p_prop)) {
  846. r_class = check->name;
  847. r_setter = check->property_setget[p_prop].setter;
  848. return true;
  849. }
  850. check = check->inherits_ptr;
  851. }
  852. return false;
  853. }
  854. #ifdef DEBUG_METHODS_ENABLED
  855. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount) {
  856. StringName mdname = method_name.name;
  857. #else
  858. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const char *method_name, const Variant **p_defs, int p_defcount) {
  859. StringName mdname = StaticCString::create(method_name);
  860. #endif
  861. OBJTYPE_WLOCK;
  862. ERR_FAIL_COND_V(!p_bind, NULL);
  863. p_bind->set_name(mdname);
  864. String instance_type = p_bind->get_instance_class();
  865. #ifdef DEBUG_ENABLED
  866. if (has_method(instance_type, mdname)) {
  867. ERR_EXPLAIN("Class " + String(instance_type) + " already has a method " + String(mdname));
  868. ERR_FAIL_V(NULL);
  869. }
  870. #endif
  871. ClassInfo *type = classes.getptr(instance_type);
  872. if (!type) {
  873. ERR_PRINTS("Couldn't bind method '" + mdname + "' for instance: " + instance_type);
  874. memdelete(p_bind);
  875. ERR_FAIL_V(NULL);
  876. }
  877. if (type->method_map.has(mdname)) {
  878. memdelete(p_bind);
  879. // overloading not supported
  880. ERR_EXPLAIN("Method already bound: " + instance_type + "::" + mdname);
  881. ERR_FAIL_V(NULL);
  882. }
  883. #ifdef DEBUG_METHODS_ENABLED
  884. if (method_name.args.size() > p_bind->get_argument_count()) {
  885. memdelete(p_bind);
  886. ERR_EXPLAIN("Method definition provides more arguments than the method actually has: " + instance_type + "::" + mdname);
  887. ERR_FAIL_V(NULL);
  888. }
  889. p_bind->set_argument_names(method_name.args);
  890. type->method_order.push_back(mdname);
  891. #endif
  892. type->method_map[mdname] = p_bind;
  893. Vector<Variant> defvals;
  894. defvals.resize(p_defcount);
  895. for (int i = 0; i < p_defcount; i++) {
  896. defvals[i] = *p_defs[p_defcount - i - 1];
  897. }
  898. p_bind->set_default_arguments(defvals);
  899. p_bind->set_hint_flags(p_flags);
  900. return p_bind;
  901. }
  902. void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual) {
  903. ERR_FAIL_COND(!classes.has(p_class));
  904. OBJTYPE_WLOCK;
  905. #ifdef DEBUG_METHODS_ENABLED
  906. MethodInfo mi = p_method;
  907. if (p_virtual)
  908. mi.flags |= METHOD_FLAG_VIRTUAL;
  909. classes[p_class].virtual_methods.push_back(mi);
  910. #endif
  911. }
  912. void ClassDB::get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance) {
  913. ERR_FAIL_COND(!classes.has(p_class));
  914. #ifdef DEBUG_METHODS_ENABLED
  915. ClassInfo *type = classes.getptr(p_class);
  916. ClassInfo *check = type;
  917. while (check) {
  918. for (List<MethodInfo>::Element *E = check->virtual_methods.front(); E; E = E->next()) {
  919. p_methods->push_back(E->get());
  920. }
  921. if (p_no_inheritance)
  922. return;
  923. check = check->inherits_ptr;
  924. }
  925. #endif
  926. }
  927. void ClassDB::set_class_enabled(StringName p_class, bool p_enable) {
  928. OBJTYPE_WLOCK;
  929. ERR_FAIL_COND(!classes.has(p_class));
  930. classes[p_class].disabled = !p_enable;
  931. }
  932. bool ClassDB::is_class_enabled(StringName p_class) {
  933. OBJTYPE_RLOCK;
  934. ClassInfo *ti = classes.getptr(p_class);
  935. if (!ti || !ti->creation_func) {
  936. if (compat_classes.has(p_class)) {
  937. ti = classes.getptr(compat_classes[p_class]);
  938. }
  939. }
  940. ERR_FAIL_COND_V(!ti, false);
  941. return !ti->disabled;
  942. }
  943. StringName ClassDB::get_category(const StringName &p_node) {
  944. ERR_FAIL_COND_V(!classes.has(p_node), StringName());
  945. #ifdef DEBUG_ENABLED
  946. return classes[p_node].category;
  947. #else
  948. return StringName();
  949. #endif
  950. }
  951. void ClassDB::add_resource_base_extension(const StringName &p_extension, const StringName &p_class) {
  952. if (resource_base_extensions.has(p_extension))
  953. return;
  954. resource_base_extensions[p_extension] = p_class;
  955. }
  956. void ClassDB::get_resource_base_extensions(List<String> *p_extensions) {
  957. const StringName *K = NULL;
  958. while ((K = resource_base_extensions.next(K))) {
  959. p_extensions->push_back(*K);
  960. }
  961. }
  962. void ClassDB::get_extensions_for_type(const StringName &p_class, List<String> *p_extensions) {
  963. const StringName *K = NULL;
  964. while ((K = resource_base_extensions.next(K))) {
  965. StringName cmp = resource_base_extensions[*K];
  966. if (is_parent_class(p_class, cmp) || is_parent_class(cmp, p_class))
  967. p_extensions->push_back(*K);
  968. }
  969. }
  970. RWLock *ClassDB::lock = NULL;
  971. void ClassDB::init() {
  972. #ifndef NO_THREADS
  973. lock = RWLock::create();
  974. #endif
  975. }
  976. void ClassDB::cleanup() {
  977. //OBJTYPE_LOCK; hah not here
  978. const StringName *k = NULL;
  979. while ((k = classes.next(k))) {
  980. ClassInfo &ti = classes[*k];
  981. const StringName *m = NULL;
  982. while ((m = ti.method_map.next(m))) {
  983. memdelete(ti.method_map[*m]);
  984. }
  985. }
  986. classes.clear();
  987. resource_base_extensions.clear();
  988. compat_classes.clear();
  989. #ifndef NO_THREADS
  990. memdelete(lock);
  991. #endif
  992. }
  993. //