class_db.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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. Variant::Type ClassDB::get_property_type(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->type;
  702. }
  703. check = check->inherits_ptr;
  704. }
  705. if (r_is_valid)
  706. *r_is_valid = false;
  707. return Variant::NIL;
  708. }
  709. StringName ClassDB::get_property_setter(StringName p_class, const StringName p_property) {
  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. return psg->setter;
  716. }
  717. check = check->inherits_ptr;
  718. }
  719. return StringName();
  720. }
  721. StringName ClassDB::get_property_getter(StringName p_class, const StringName p_property) {
  722. ClassInfo *type = classes.getptr(p_class);
  723. ClassInfo *check = type;
  724. while (check) {
  725. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  726. if (psg) {
  727. return psg->getter;
  728. }
  729. check = check->inherits_ptr;
  730. }
  731. return StringName();
  732. }
  733. bool ClassDB::has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance) {
  734. ClassInfo *type = classes.getptr(p_class);
  735. ClassInfo *check = type;
  736. while (check) {
  737. if (check->property_setget.has(p_property))
  738. return true;
  739. if (p_no_inheritance)
  740. break;
  741. check = check->inherits_ptr;
  742. }
  743. return false;
  744. }
  745. void ClassDB::set_method_flags(StringName p_class, StringName p_method, int p_flags) {
  746. OBJTYPE_WLOCK;
  747. ClassInfo *type = classes.getptr(p_class);
  748. ClassInfo *check = type;
  749. ERR_FAIL_COND(!check);
  750. ERR_FAIL_COND(!check->method_map.has(p_method));
  751. check->method_map[p_method]->set_hint_flags(p_flags);
  752. }
  753. bool ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inheritance) {
  754. ClassInfo *type = classes.getptr(p_class);
  755. ClassInfo *check = type;
  756. while (check) {
  757. if (check->method_map.has(p_method))
  758. return true;
  759. if (p_no_inheritance)
  760. return false;
  761. check = check->inherits_ptr;
  762. }
  763. return false;
  764. }
  765. bool ClassDB::get_setter_and_type_for_property(const StringName &p_class, const StringName &p_prop, StringName &r_class, StringName &r_setter) {
  766. ClassInfo *type = classes.getptr(p_class);
  767. ClassInfo *check = type;
  768. while (check) {
  769. if (check->property_setget.has(p_prop)) {
  770. r_class = check->name;
  771. r_setter = check->property_setget[p_prop].setter;
  772. return true;
  773. }
  774. check = check->inherits_ptr;
  775. }
  776. return false;
  777. }
  778. #ifdef DEBUG_METHODS_ENABLED
  779. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount) {
  780. StringName mdname = method_name.name;
  781. #else
  782. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const char *method_name, const Variant **p_defs, int p_defcount) {
  783. StringName mdname = StaticCString::create(method_name);
  784. #endif
  785. StringName rettype;
  786. if (mdname.operator String().find(":") != -1) {
  787. rettype = mdname.operator String().get_slice(":", 1);
  788. mdname = mdname.operator String().get_slice(":", 0);
  789. }
  790. OBJTYPE_WLOCK;
  791. ERR_FAIL_COND_V(!p_bind, NULL);
  792. p_bind->set_name(mdname);
  793. String instance_type = p_bind->get_instance_class();
  794. #ifdef DEBUG_ENABLED
  795. if (has_method(instance_type, mdname)) {
  796. ERR_EXPLAIN("Class " + String(instance_type) + " already has a method " + String(mdname));
  797. ERR_FAIL_V(NULL);
  798. }
  799. #endif
  800. ClassInfo *type = classes.getptr(instance_type);
  801. if (!type) {
  802. ERR_PRINTS("Couldn't bind method '" + mdname + "' for instance: " + instance_type);
  803. memdelete(p_bind);
  804. ERR_FAIL_COND_V(!type, NULL);
  805. }
  806. if (type->method_map.has(mdname)) {
  807. memdelete(p_bind);
  808. // overloading not supported
  809. ERR_EXPLAIN("Method already bound: " + instance_type + "::" + mdname);
  810. ERR_FAIL_V(NULL);
  811. }
  812. #ifdef DEBUG_METHODS_ENABLED
  813. p_bind->set_argument_names(method_name.args);
  814. p_bind->set_return_type(rettype);
  815. type->method_order.push_back(mdname);
  816. #endif
  817. type->method_map[mdname] = p_bind;
  818. Vector<Variant> defvals;
  819. defvals.resize(p_defcount);
  820. for (int i = 0; i < p_defcount; i++) {
  821. defvals[i] = *p_defs[p_defcount - i - 1];
  822. }
  823. p_bind->set_default_arguments(defvals);
  824. p_bind->set_hint_flags(p_flags);
  825. return p_bind;
  826. }
  827. void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual) {
  828. ERR_FAIL_COND(!classes.has(p_class));
  829. OBJTYPE_WLOCK;
  830. #ifdef DEBUG_METHODS_ENABLED
  831. MethodInfo mi = p_method;
  832. if (p_virtual)
  833. mi.flags |= METHOD_FLAG_VIRTUAL;
  834. classes[p_class].virtual_methods.push_back(mi);
  835. #endif
  836. }
  837. void ClassDB::get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance) {
  838. ERR_FAIL_COND(!classes.has(p_class));
  839. #ifdef DEBUG_METHODS_ENABLED
  840. ClassInfo *type = classes.getptr(p_class);
  841. ClassInfo *check = type;
  842. while (check) {
  843. for (List<MethodInfo>::Element *E = check->virtual_methods.front(); E; E = E->next()) {
  844. p_methods->push_back(E->get());
  845. }
  846. if (p_no_inheritance)
  847. return;
  848. check = check->inherits_ptr;
  849. }
  850. #endif
  851. }
  852. void ClassDB::set_class_enabled(StringName p_class, bool p_enable) {
  853. OBJTYPE_WLOCK;
  854. ERR_FAIL_COND(!classes.has(p_class));
  855. classes[p_class].disabled = !p_enable;
  856. }
  857. bool ClassDB::is_class_enabled(StringName p_class) {
  858. OBJTYPE_RLOCK;
  859. ClassInfo *ti = classes.getptr(p_class);
  860. if (!ti || !ti->creation_func) {
  861. if (compat_classes.has(p_class)) {
  862. ti = classes.getptr(compat_classes[p_class]);
  863. }
  864. }
  865. ERR_FAIL_COND_V(!ti, false);
  866. return !ti->disabled;
  867. }
  868. StringName ClassDB::get_category(const StringName &p_node) {
  869. ERR_FAIL_COND_V(!classes.has(p_node), StringName());
  870. #ifdef DEBUG_ENABLED
  871. return classes[p_node].category;
  872. #else
  873. return StringName();
  874. #endif
  875. }
  876. void ClassDB::add_resource_base_extension(const StringName &p_extension, const StringName &p_class) {
  877. if (resource_base_extensions.has(p_extension))
  878. return;
  879. resource_base_extensions[p_extension] = p_class;
  880. }
  881. void ClassDB::get_resource_base_extensions(List<String> *p_extensions) {
  882. const StringName *K = NULL;
  883. while ((K = resource_base_extensions.next(K))) {
  884. p_extensions->push_back(*K);
  885. }
  886. }
  887. void ClassDB::get_extensions_for_type(const StringName &p_class, List<String> *p_extensions) {
  888. const StringName *K = NULL;
  889. while ((K = resource_base_extensions.next(K))) {
  890. StringName cmp = resource_base_extensions[*K];
  891. if (is_parent_class(p_class, cmp) || is_parent_class(cmp, p_class))
  892. p_extensions->push_back(*K);
  893. }
  894. }
  895. RWLock *ClassDB::lock = NULL;
  896. void ClassDB::init() {
  897. #ifndef NO_THREADS
  898. lock = RWLock::create();
  899. #endif
  900. }
  901. void ClassDB::cleanup() {
  902. //OBJTYPE_LOCK; hah not here
  903. const StringName *k = NULL;
  904. while ((k = classes.next(k))) {
  905. ClassInfo &ti = classes[*k];
  906. const StringName *m = NULL;
  907. while ((m = ti.method_map.next(m))) {
  908. memdelete(ti.method_map[*m]);
  909. }
  910. }
  911. classes.clear();
  912. resource_base_extensions.clear();
  913. compat_classes.clear();
  914. #ifndef NO_THREADS
  915. memdelete(lock);
  916. #endif
  917. }
  918. //