class_db.cpp 33 KB

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