class_db.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. /*************************************************************************/
  2. /* class_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.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. p_methods->push_back(minfo);
  398. }
  399. #else
  400. const StringName *K = NULL;
  401. while ((K = type->method_map.next(K))) {
  402. MethodBind *m = type->method_map[*K];
  403. MethodInfo mi;
  404. mi.name = m->get_name();
  405. p_methods->push_back(mi);
  406. }
  407. #endif
  408. if (p_no_inheritance)
  409. break;
  410. type = type->inherits_ptr;
  411. }
  412. }
  413. MethodBind *ClassDB::get_method(StringName p_class, StringName p_name) {
  414. OBJTYPE_RLOCK;
  415. ClassInfo *type = classes.getptr(p_class);
  416. while (type) {
  417. MethodBind **method = type->method_map.getptr(p_name);
  418. if (method && *method)
  419. return *method;
  420. type = type->inherits_ptr;
  421. }
  422. return NULL;
  423. }
  424. void ClassDB::bind_integer_constant(const StringName &p_class, const StringName &p_enum, const StringName &p_name, int p_constant) {
  425. OBJTYPE_WLOCK;
  426. ClassInfo *type = classes.getptr(p_class);
  427. if (!type) {
  428. ERR_FAIL_COND(!type);
  429. }
  430. if (type->constant_map.has(p_name)) {
  431. ERR_FAIL();
  432. }
  433. type->constant_map[p_name] = p_constant;
  434. #ifdef DEBUG_METHODS_ENABLED
  435. String enum_name = p_enum;
  436. if (enum_name!=String()) {
  437. if (enum_name.find(".")!=-1) {
  438. enum_name=enum_name.get_slicec('.',1);
  439. }
  440. List<StringName> *constants_list = type->enum_map.getptr(enum_name);
  441. if (constants_list) {
  442. constants_list->push_back(p_name);
  443. } else {
  444. List<StringName> new_list;
  445. new_list.push_back(p_name);
  446. type->enum_map[enum_name] = new_list;
  447. }
  448. }
  449. type->constant_order.push_back(p_name);
  450. #endif
  451. }
  452. void ClassDB::get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance) {
  453. OBJTYPE_RLOCK;
  454. ClassInfo *type = classes.getptr(p_class);
  455. while (type) {
  456. #ifdef DEBUG_METHODS_ENABLED
  457. for (List<StringName>::Element *E = type->constant_order.front(); E; E = E->next())
  458. p_constants->push_back(E->get());
  459. #else
  460. const StringName *K = NULL;
  461. while ((K = type->constant_map.next(K))) {
  462. p_constants->push_back(*K);
  463. }
  464. #endif
  465. if (p_no_inheritance)
  466. break;
  467. type = type->inherits_ptr;
  468. }
  469. }
  470. int ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success) {
  471. OBJTYPE_RLOCK;
  472. ClassInfo *type = classes.getptr(p_class);
  473. while (type) {
  474. int *constant = type->constant_map.getptr(p_name);
  475. if (constant) {
  476. if (p_success)
  477. *p_success = true;
  478. return *constant;
  479. }
  480. type = type->inherits_ptr;
  481. }
  482. if (p_success)
  483. *p_success = false;
  484. return 0;
  485. }
  486. #ifdef DEBUG_METHODS_ENABLED
  487. StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  488. OBJTYPE_RLOCK;
  489. ClassInfo *type = classes.getptr(p_class);
  490. while (type) {
  491. const StringName *k = NULL;
  492. while ((k = type->enum_map.next(k))) {
  493. List<StringName> &constants_list = type->enum_map.get(*k);
  494. const List<StringName>::Element *found = constants_list.find(p_name);
  495. if (found)
  496. return *k;
  497. }
  498. if (p_no_inheritance)
  499. break;
  500. type = type->inherits_ptr;
  501. }
  502. return StringName();
  503. }
  504. void ClassDB::get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance) {
  505. OBJTYPE_RLOCK;
  506. ClassInfo *type = classes.getptr(p_class);
  507. while (type) {
  508. const StringName *k = NULL;
  509. while ((k = type->enum_map.next(k))) {
  510. p_enums->push_back(*k);
  511. }
  512. if (p_no_inheritance)
  513. break;
  514. type = type->inherits_ptr;
  515. }
  516. }
  517. void ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance) {
  518. OBJTYPE_RLOCK;
  519. ClassInfo *type = classes.getptr(p_class);
  520. while (type) {
  521. const List<StringName> *constants = type->enum_map.getptr(p_enum);
  522. if (constants) {
  523. for (const List<StringName>::Element *E = constants->front(); E; E = E->next()) {
  524. p_constants->push_back(E->get());
  525. }
  526. }
  527. if (p_no_inheritance)
  528. break;
  529. type = type->inherits_ptr;
  530. }
  531. }
  532. #endif
  533. void ClassDB::add_signal(StringName p_class, const MethodInfo &p_signal) {
  534. OBJTYPE_WLOCK;
  535. ClassInfo *type = classes.getptr(p_class);
  536. ERR_FAIL_COND(!type);
  537. ClassInfo *check = type;
  538. StringName sname = p_signal.name;
  539. #ifdef DEBUG_METHODS_ENABLED
  540. while (check) {
  541. if (check->signal_map.has(sname)) {
  542. ERR_EXPLAIN("Type " + String(p_class) + " already has signal: " + String(sname));
  543. ERR_FAIL();
  544. }
  545. check = check->inherits_ptr;
  546. }
  547. #endif
  548. type->signal_map[sname] = p_signal;
  549. }
  550. void ClassDB::get_signal_list(StringName p_class, List<MethodInfo> *p_signals, bool p_no_inheritance) {
  551. OBJTYPE_RLOCK;
  552. ClassInfo *type = classes.getptr(p_class);
  553. ERR_FAIL_COND(!type);
  554. ClassInfo *check = type;
  555. while (check) {
  556. const StringName *S = NULL;
  557. while ((S = check->signal_map.next(S))) {
  558. p_signals->push_back(check->signal_map[*S]);
  559. }
  560. if (p_no_inheritance)
  561. return;
  562. check = check->inherits_ptr;
  563. }
  564. }
  565. bool ClassDB::has_signal(StringName p_class, StringName p_signal) {
  566. OBJTYPE_RLOCK;
  567. ClassInfo *type = classes.getptr(p_class);
  568. ClassInfo *check = type;
  569. while (check) {
  570. if (check->signal_map.has(p_signal))
  571. return true;
  572. check = check->inherits_ptr;
  573. }
  574. return false;
  575. }
  576. bool ClassDB::get_signal(StringName p_class, StringName p_signal, MethodInfo *r_signal) {
  577. OBJTYPE_RLOCK;
  578. ClassInfo *type = classes.getptr(p_class);
  579. ClassInfo *check = type;
  580. while (check) {
  581. if (check->signal_map.has(p_signal)) {
  582. if (r_signal) {
  583. *r_signal = check->signal_map[p_signal];
  584. }
  585. return true;
  586. }
  587. check = check->inherits_ptr;
  588. }
  589. return false;
  590. }
  591. void ClassDB::add_property_group(StringName p_class, const String &p_name, const String &p_prefix) {
  592. OBJTYPE_WLOCK;
  593. ClassInfo *type = classes.getptr(p_class);
  594. ERR_FAIL_COND(!type);
  595. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, p_prefix, PROPERTY_USAGE_GROUP));
  596. }
  597. void ClassDB::add_property(StringName p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index) {
  598. #ifndef NO_THREADS
  599. lock->read_lock();
  600. #endif
  601. ClassInfo *type = classes.getptr(p_class);
  602. #ifndef NO_THREADS
  603. lock->read_unlock();
  604. #endif
  605. ERR_FAIL_COND(!type);
  606. MethodBind *mb_set = NULL;
  607. if (p_setter) {
  608. mb_set = get_method(p_class, p_setter);
  609. #ifdef DEBUG_METHODS_ENABLED
  610. if (!mb_set) {
  611. ERR_EXPLAIN("Invalid Setter: " + p_class + "::" + p_setter + " for property: " + p_pinfo.name);
  612. ERR_FAIL_COND(!mb_set);
  613. } else {
  614. int exp_args = 1 + (p_index >= 0 ? 1 : 0);
  615. if (mb_set->get_argument_count() != exp_args) {
  616. ERR_EXPLAIN("Invalid Function for Setter: " + p_class + "::" + p_setter + " for property: " + p_pinfo.name);
  617. ERR_FAIL();
  618. }
  619. }
  620. #endif
  621. }
  622. MethodBind *mb_get = NULL;
  623. if (p_getter) {
  624. MethodBind *mb_get = get_method(p_class, p_getter);
  625. #ifdef DEBUG_METHODS_ENABLED
  626. if (!mb_get) {
  627. ERR_EXPLAIN("Invalid Getter: " + p_class + "::" + p_getter + " for property: " + p_pinfo.name);
  628. ERR_FAIL_COND(!mb_get);
  629. } else {
  630. int exp_args = 0 + (p_index >= 0 ? 1 : 0);
  631. if (mb_get->get_argument_count() != exp_args) {
  632. ERR_EXPLAIN("Invalid Function for Getter: " + p_class + "::" + p_getter + " for property: " + p_pinfo.name);
  633. ERR_FAIL();
  634. }
  635. }
  636. #endif
  637. }
  638. #ifdef DEBUG_METHODS_ENABLED
  639. if (type->property_setget.has(p_pinfo.name)) {
  640. ERR_EXPLAIN("Object already has property: " + p_class);
  641. ERR_FAIL();
  642. }
  643. #endif
  644. OBJTYPE_WLOCK
  645. type->property_list.push_back(p_pinfo);
  646. #ifdef DEBUG_METHODS_ENABLED
  647. if (mb_get) {
  648. type->methods_in_properties.insert(p_getter);
  649. }
  650. if (mb_set) {
  651. type->methods_in_properties.insert(p_setter);
  652. }
  653. #endif
  654. PropertySetGet psg;
  655. psg.setter = p_setter;
  656. psg.getter = p_getter;
  657. psg._setptr = mb_set;
  658. psg._getptr = mb_get;
  659. psg.index = p_index;
  660. psg.type = p_pinfo.type;
  661. type->property_setget[p_pinfo.name] = psg;
  662. }
  663. void ClassDB::get_property_list(StringName p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator) {
  664. OBJTYPE_RLOCK;
  665. ClassInfo *type = classes.getptr(p_class);
  666. ClassInfo *check = type;
  667. while (check) {
  668. for (List<PropertyInfo>::Element *E = check->property_list.front(); E; E = E->next()) {
  669. if (p_validator) {
  670. PropertyInfo pi = E->get();
  671. p_validator->_validate_property(pi);
  672. p_list->push_back(pi);
  673. } else {
  674. p_list->push_back(E->get());
  675. }
  676. }
  677. if (p_no_inheritance)
  678. return;
  679. check = check->inherits_ptr;
  680. }
  681. }
  682. bool ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid) {
  683. ClassInfo *type = classes.getptr(p_object->get_class_name());
  684. ClassInfo *check = type;
  685. while (check) {
  686. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  687. if (psg) {
  688. if (!psg->setter) {
  689. if (r_valid)
  690. *r_valid = false;
  691. return true; //return true but do nothing
  692. }
  693. Variant::CallError ce;
  694. if (psg->index >= 0) {
  695. Variant index = psg->index;
  696. const Variant *arg[2] = { &index, &p_value };
  697. //p_object->call(psg->setter,arg,2,ce);
  698. if (psg->_setptr) {
  699. psg->_setptr->call(p_object, arg, 2, ce);
  700. } else {
  701. p_object->call(psg->setter, arg, 2, ce);
  702. }
  703. } else {
  704. const Variant *arg[1] = { &p_value };
  705. if (psg->_setptr) {
  706. psg->_setptr->call(p_object, arg, 1, ce);
  707. } else {
  708. p_object->call(psg->setter, arg, 1, ce);
  709. }
  710. }
  711. if (r_valid)
  712. *r_valid = ce.error == Variant::CallError::CALL_OK;
  713. return true;
  714. }
  715. check = check->inherits_ptr;
  716. }
  717. return false;
  718. }
  719. bool ClassDB::get_property(Object *p_object, const StringName &p_property, Variant &r_value) {
  720. ClassInfo *type = classes.getptr(p_object->get_class_name());
  721. ClassInfo *check = type;
  722. while (check) {
  723. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  724. if (psg) {
  725. if (!psg->getter)
  726. return true; //return true but do nothing
  727. if (psg->index >= 0) {
  728. Variant index = psg->index;
  729. const Variant *arg[1] = { &index };
  730. Variant::CallError ce;
  731. r_value = p_object->call(psg->getter, arg, 1, ce);
  732. } else {
  733. Variant::CallError ce;
  734. if (psg->_getptr) {
  735. r_value = psg->_getptr->call(p_object, NULL, 0, ce);
  736. } else {
  737. r_value = p_object->call(psg->getter, NULL, 0, ce);
  738. }
  739. }
  740. return true;
  741. }
  742. const int *c = check->constant_map.getptr(p_property);
  743. if (c) {
  744. r_value = *c;
  745. return true;
  746. }
  747. //if (check->constant_map.fin)
  748. check = check->inherits_ptr;
  749. }
  750. return false;
  751. }
  752. int ClassDB::get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  753. ClassInfo *type = classes.getptr(p_class);
  754. ClassInfo *check = type;
  755. while (check) {
  756. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  757. if (psg) {
  758. if (r_is_valid)
  759. *r_is_valid = true;
  760. return psg->index;
  761. }
  762. check = check->inherits_ptr;
  763. }
  764. if (r_is_valid)
  765. *r_is_valid = false;
  766. return -1;
  767. }
  768. Variant::Type ClassDB::get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  769. ClassInfo *type = classes.getptr(p_class);
  770. ClassInfo *check = type;
  771. while (check) {
  772. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  773. if (psg) {
  774. if (r_is_valid)
  775. *r_is_valid = true;
  776. return psg->type;
  777. }
  778. check = check->inherits_ptr;
  779. }
  780. if (r_is_valid)
  781. *r_is_valid = false;
  782. return Variant::NIL;
  783. }
  784. StringName ClassDB::get_property_setter(StringName p_class, const StringName p_property) {
  785. ClassInfo *type = classes.getptr(p_class);
  786. ClassInfo *check = type;
  787. while (check) {
  788. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  789. if (psg) {
  790. return psg->setter;
  791. }
  792. check = check->inherits_ptr;
  793. }
  794. return StringName();
  795. }
  796. StringName ClassDB::get_property_getter(StringName p_class, const StringName p_property) {
  797. ClassInfo *type = classes.getptr(p_class);
  798. ClassInfo *check = type;
  799. while (check) {
  800. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  801. if (psg) {
  802. return psg->getter;
  803. }
  804. check = check->inherits_ptr;
  805. }
  806. return StringName();
  807. }
  808. bool ClassDB::has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance) {
  809. ClassInfo *type = classes.getptr(p_class);
  810. ClassInfo *check = type;
  811. while (check) {
  812. if (check->property_setget.has(p_property))
  813. return true;
  814. if (p_no_inheritance)
  815. break;
  816. check = check->inherits_ptr;
  817. }
  818. return false;
  819. }
  820. void ClassDB::set_method_flags(StringName p_class, StringName p_method, int p_flags) {
  821. OBJTYPE_WLOCK;
  822. ClassInfo *type = classes.getptr(p_class);
  823. ClassInfo *check = type;
  824. ERR_FAIL_COND(!check);
  825. ERR_FAIL_COND(!check->method_map.has(p_method));
  826. check->method_map[p_method]->set_hint_flags(p_flags);
  827. }
  828. bool ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inheritance) {
  829. ClassInfo *type = classes.getptr(p_class);
  830. ClassInfo *check = type;
  831. while (check) {
  832. if (check->method_map.has(p_method))
  833. return true;
  834. if (p_no_inheritance)
  835. return false;
  836. check = check->inherits_ptr;
  837. }
  838. return false;
  839. }
  840. bool ClassDB::get_setter_and_type_for_property(const StringName &p_class, const StringName &p_prop, StringName &r_class, StringName &r_setter) {
  841. ClassInfo *type = classes.getptr(p_class);
  842. ClassInfo *check = type;
  843. while (check) {
  844. if (check->property_setget.has(p_prop)) {
  845. r_class = check->name;
  846. r_setter = check->property_setget[p_prop].setter;
  847. return true;
  848. }
  849. check = check->inherits_ptr;
  850. }
  851. return false;
  852. }
  853. #ifdef DEBUG_METHODS_ENABLED
  854. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount) {
  855. StringName mdname = method_name.name;
  856. #else
  857. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const char *method_name, const Variant **p_defs, int p_defcount) {
  858. StringName mdname = StaticCString::create(method_name);
  859. #endif
  860. OBJTYPE_WLOCK;
  861. ERR_FAIL_COND_V(!p_bind, NULL);
  862. p_bind->set_name(mdname);
  863. String instance_type = p_bind->get_instance_class();
  864. #ifdef DEBUG_ENABLED
  865. if (has_method(instance_type, mdname)) {
  866. ERR_EXPLAIN("Class " + String(instance_type) + " already has a method " + String(mdname));
  867. ERR_FAIL_V(NULL);
  868. }
  869. #endif
  870. ClassInfo *type = classes.getptr(instance_type);
  871. if (!type) {
  872. ERR_PRINTS("Couldn't bind method '" + mdname + "' for instance: " + instance_type);
  873. memdelete(p_bind);
  874. ERR_FAIL_V(NULL);
  875. }
  876. if (type->method_map.has(mdname)) {
  877. memdelete(p_bind);
  878. // overloading not supported
  879. ERR_EXPLAIN("Method already bound: " + instance_type + "::" + mdname);
  880. ERR_FAIL_V(NULL);
  881. }
  882. #ifdef DEBUG_METHODS_ENABLED
  883. if (method_name.args.size() > p_bind->get_argument_count()) {
  884. memdelete(p_bind);
  885. ERR_EXPLAIN("Method definition provides more arguments than the method actually has: " + instance_type + "::" + mdname);
  886. ERR_FAIL_V(NULL);
  887. }
  888. p_bind->set_argument_names(method_name.args);
  889. type->method_order.push_back(mdname);
  890. #endif
  891. type->method_map[mdname] = p_bind;
  892. Vector<Variant> defvals;
  893. defvals.resize(p_defcount);
  894. for (int i = 0; i < p_defcount; i++) {
  895. defvals[i] = *p_defs[p_defcount - i - 1];
  896. }
  897. p_bind->set_default_arguments(defvals);
  898. p_bind->set_hint_flags(p_flags);
  899. return p_bind;
  900. }
  901. void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual) {
  902. ERR_FAIL_COND(!classes.has(p_class));
  903. OBJTYPE_WLOCK;
  904. #ifdef DEBUG_METHODS_ENABLED
  905. MethodInfo mi = p_method;
  906. if (p_virtual)
  907. mi.flags |= METHOD_FLAG_VIRTUAL;
  908. classes[p_class].virtual_methods.push_back(mi);
  909. #endif
  910. }
  911. void ClassDB::get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance) {
  912. ERR_FAIL_COND(!classes.has(p_class));
  913. #ifdef DEBUG_METHODS_ENABLED
  914. ClassInfo *type = classes.getptr(p_class);
  915. ClassInfo *check = type;
  916. while (check) {
  917. for (List<MethodInfo>::Element *E = check->virtual_methods.front(); E; E = E->next()) {
  918. p_methods->push_back(E->get());
  919. }
  920. if (p_no_inheritance)
  921. return;
  922. check = check->inherits_ptr;
  923. }
  924. #endif
  925. }
  926. void ClassDB::set_class_enabled(StringName p_class, bool p_enable) {
  927. OBJTYPE_WLOCK;
  928. ERR_FAIL_COND(!classes.has(p_class));
  929. classes[p_class].disabled = !p_enable;
  930. }
  931. bool ClassDB::is_class_enabled(StringName p_class) {
  932. OBJTYPE_RLOCK;
  933. ClassInfo *ti = classes.getptr(p_class);
  934. if (!ti || !ti->creation_func) {
  935. if (compat_classes.has(p_class)) {
  936. ti = classes.getptr(compat_classes[p_class]);
  937. }
  938. }
  939. ERR_FAIL_COND_V(!ti, false);
  940. return !ti->disabled;
  941. }
  942. StringName ClassDB::get_category(const StringName &p_node) {
  943. ERR_FAIL_COND_V(!classes.has(p_node), StringName());
  944. #ifdef DEBUG_ENABLED
  945. return classes[p_node].category;
  946. #else
  947. return StringName();
  948. #endif
  949. }
  950. void ClassDB::add_resource_base_extension(const StringName &p_extension, const StringName &p_class) {
  951. if (resource_base_extensions.has(p_extension))
  952. return;
  953. resource_base_extensions[p_extension] = p_class;
  954. }
  955. void ClassDB::get_resource_base_extensions(List<String> *p_extensions) {
  956. const StringName *K = NULL;
  957. while ((K = resource_base_extensions.next(K))) {
  958. p_extensions->push_back(*K);
  959. }
  960. }
  961. void ClassDB::get_extensions_for_type(const StringName &p_class, List<String> *p_extensions) {
  962. const StringName *K = NULL;
  963. while ((K = resource_base_extensions.next(K))) {
  964. StringName cmp = resource_base_extensions[*K];
  965. if (is_parent_class(p_class, cmp) || is_parent_class(cmp, p_class))
  966. p_extensions->push_back(*K);
  967. }
  968. }
  969. RWLock *ClassDB::lock = NULL;
  970. void ClassDB::init() {
  971. #ifndef NO_THREADS
  972. lock = RWLock::create();
  973. #endif
  974. }
  975. void ClassDB::cleanup() {
  976. //OBJTYPE_LOCK; hah not here
  977. const StringName *k = NULL;
  978. while ((k = classes.next(k))) {
  979. ClassInfo &ti = classes[*k];
  980. const StringName *m = NULL;
  981. while ((m = ti.method_map.next(m))) {
  982. memdelete(ti.method_map[*m]);
  983. }
  984. }
  985. classes.clear();
  986. resource_base_extensions.clear();
  987. compat_classes.clear();
  988. #ifndef NO_THREADS
  989. memdelete(lock);
  990. #endif
  991. }
  992. //