class_db.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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. 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. //