class_db.cpp 31 KB

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