class_db.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  1. /*************************************************************************/
  2. /* class_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "core/config/engine.h"
  32. #include "core/os/mutex.h"
  33. #include "core/version.h"
  34. #define OBJTYPE_RLOCK RWLockRead _rw_lockr_(lock);
  35. #define OBJTYPE_WLOCK RWLockWrite _rw_lockw_(lock);
  36. #ifdef DEBUG_METHODS_ENABLED
  37. MethodDefinition D_METHODP(const char *p_name, const char *const **p_args, uint32_t p_argcount) {
  38. MethodDefinition md;
  39. md.name = StaticCString::create(p_name);
  40. md.args.resize(p_argcount);
  41. for (uint32_t i = 0; i < p_argcount; i++) {
  42. md.args.write[i] = StaticCString::create(*p_args[i]);
  43. }
  44. return md;
  45. }
  46. #endif
  47. ClassDB::APIType ClassDB::current_api = API_CORE;
  48. void ClassDB::set_current_api(APIType p_api) {
  49. current_api = p_api;
  50. }
  51. ClassDB::APIType ClassDB::get_current_api() {
  52. return current_api;
  53. }
  54. HashMap<StringName, ClassDB::ClassInfo> ClassDB::classes;
  55. HashMap<StringName, StringName> ClassDB::resource_base_extensions;
  56. HashMap<StringName, StringName> ClassDB::compat_classes;
  57. bool ClassDB::_is_parent_class(const StringName &p_class, const StringName &p_inherits) {
  58. if (!classes.has(p_class)) {
  59. return false;
  60. }
  61. StringName inherits = p_class;
  62. while (inherits.operator String().length()) {
  63. if (inherits == p_inherits) {
  64. return true;
  65. }
  66. inherits = _get_parent_class(inherits);
  67. }
  68. return false;
  69. }
  70. bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) {
  71. OBJTYPE_RLOCK;
  72. return _is_parent_class(p_class, p_inherits);
  73. }
  74. void ClassDB::get_class_list(List<StringName> *p_classes) {
  75. OBJTYPE_RLOCK;
  76. for (const KeyValue<StringName, ClassInfo> &E : classes) {
  77. p_classes->push_back(E.key);
  78. }
  79. p_classes->sort();
  80. }
  81. void ClassDB::get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
  82. OBJTYPE_RLOCK;
  83. for (const KeyValue<StringName, ClassInfo> &E : classes) {
  84. if (E.key != p_class && _is_parent_class(E.key, p_class)) {
  85. p_classes->push_back(E.key);
  86. }
  87. }
  88. }
  89. void ClassDB::get_direct_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
  90. OBJTYPE_RLOCK;
  91. for (const KeyValue<StringName, ClassInfo> &E : classes) {
  92. if (E.key != p_class && _get_parent_class(E.key) == p_class) {
  93. p_classes->push_back(E.key);
  94. }
  95. }
  96. }
  97. StringName ClassDB::get_parent_class_nocheck(const StringName &p_class) {
  98. OBJTYPE_RLOCK;
  99. ClassInfo *ti = classes.getptr(p_class);
  100. if (!ti) {
  101. return StringName();
  102. }
  103. return ti->inherits;
  104. }
  105. StringName ClassDB::get_compatibility_remapped_class(const StringName &p_class) {
  106. if (classes.has(p_class)) {
  107. return p_class;
  108. }
  109. if (compat_classes.has(p_class)) {
  110. return compat_classes[p_class];
  111. }
  112. return p_class;
  113. }
  114. StringName ClassDB::_get_parent_class(const StringName &p_class) {
  115. ClassInfo *ti = classes.getptr(p_class);
  116. ERR_FAIL_COND_V_MSG(!ti, StringName(), "Cannot get class '" + String(p_class) + "'.");
  117. return ti->inherits;
  118. }
  119. StringName ClassDB::get_parent_class(const StringName &p_class) {
  120. OBJTYPE_RLOCK;
  121. return _get_parent_class(p_class);
  122. }
  123. ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) {
  124. OBJTYPE_RLOCK;
  125. ClassInfo *ti = classes.getptr(p_class);
  126. ERR_FAIL_COND_V_MSG(!ti, API_NONE, "Cannot get class '" + String(p_class) + "'.");
  127. return ti->api;
  128. }
  129. uint64_t ClassDB::get_api_hash(APIType p_api) {
  130. OBJTYPE_RLOCK;
  131. #ifdef DEBUG_METHODS_ENABLED
  132. uint64_t hash = hash_djb2_one_64(HashMapHasherDefault::hash(VERSION_FULL_CONFIG));
  133. List<StringName> class_list;
  134. ClassDB::get_class_list(&class_list);
  135. // Must be alphabetically sorted for hash to compute.
  136. class_list.sort_custom<StringName::AlphCompare>();
  137. for (const StringName &E : class_list) {
  138. ClassInfo *t = classes.getptr(E);
  139. ERR_FAIL_COND_V_MSG(!t, 0, "Cannot get class '" + String(E) + "'.");
  140. if (t->api != p_api || !t->exposed) {
  141. continue;
  142. }
  143. hash = hash_djb2_one_64(t->name.hash(), hash);
  144. hash = hash_djb2_one_64(t->inherits.hash(), hash);
  145. { //methods
  146. List<StringName> snames;
  147. for (const KeyValue<StringName, MethodBind *> &F : t->method_map) {
  148. String name = F.key.operator String();
  149. ERR_CONTINUE(name.is_empty());
  150. if (name[0] == '_') {
  151. continue; // Ignore non-virtual methods that start with an underscore
  152. }
  153. snames.push_back(F.key);
  154. }
  155. snames.sort_custom<StringName::AlphCompare>();
  156. for (const StringName &F : snames) {
  157. MethodBind *mb = t->method_map[F];
  158. hash = hash_djb2_one_64(mb->get_name().hash(), hash);
  159. hash = hash_djb2_one_64(mb->get_argument_count(), hash);
  160. hash = hash_djb2_one_64(mb->get_argument_type(-1), hash); //return
  161. for (int i = 0; i < mb->get_argument_count(); i++) {
  162. const PropertyInfo info = mb->get_argument_info(i);
  163. hash = hash_djb2_one_64(info.type, hash);
  164. hash = hash_djb2_one_64(info.name.hash(), hash);
  165. hash = hash_djb2_one_64(info.hint, hash);
  166. hash = hash_djb2_one_64(info.hint_string.hash(), hash);
  167. }
  168. hash = hash_djb2_one_64(mb->get_default_argument_count(), hash);
  169. for (int i = 0; i < mb->get_default_argument_count(); i++) {
  170. //hash should not change, i hope for tis
  171. Variant da = mb->get_default_argument(i);
  172. hash = hash_djb2_one_64(da.hash(), hash);
  173. }
  174. hash = hash_djb2_one_64(mb->get_hint_flags(), hash);
  175. }
  176. }
  177. { //constants
  178. List<StringName> snames;
  179. for (const KeyValue<StringName, int> &F : t->constant_map) {
  180. snames.push_back(F.key);
  181. }
  182. snames.sort_custom<StringName::AlphCompare>();
  183. for (const StringName &F : snames) {
  184. hash = hash_djb2_one_64(F.hash(), hash);
  185. hash = hash_djb2_one_64(t->constant_map[F], hash);
  186. }
  187. }
  188. { //signals
  189. List<StringName> snames;
  190. for (const KeyValue<StringName, MethodInfo> &F : t->signal_map) {
  191. snames.push_back(F.key);
  192. }
  193. snames.sort_custom<StringName::AlphCompare>();
  194. for (const StringName &F : snames) {
  195. MethodInfo &mi = t->signal_map[F];
  196. hash = hash_djb2_one_64(F.hash(), hash);
  197. for (int i = 0; i < mi.arguments.size(); i++) {
  198. hash = hash_djb2_one_64(mi.arguments[i].type, hash);
  199. }
  200. }
  201. }
  202. { //properties
  203. List<StringName> snames;
  204. for (const KeyValue<StringName, PropertySetGet> &F : t->property_setget) {
  205. snames.push_back(F.key);
  206. }
  207. snames.sort_custom<StringName::AlphCompare>();
  208. for (const StringName &F : snames) {
  209. PropertySetGet *psg = t->property_setget.getptr(F);
  210. ERR_FAIL_COND_V(!psg, 0);
  211. hash = hash_djb2_one_64(F.hash(), hash);
  212. hash = hash_djb2_one_64(psg->setter.hash(), hash);
  213. hash = hash_djb2_one_64(psg->getter.hash(), hash);
  214. }
  215. }
  216. //property list
  217. for (const PropertyInfo &F : t->property_list) {
  218. hash = hash_djb2_one_64(F.name.hash(), hash);
  219. hash = hash_djb2_one_64(F.type, hash);
  220. hash = hash_djb2_one_64(F.hint, hash);
  221. hash = hash_djb2_one_64(F.hint_string.hash(), hash);
  222. hash = hash_djb2_one_64(F.usage, hash);
  223. }
  224. }
  225. return hash;
  226. #else
  227. return 0;
  228. #endif
  229. }
  230. bool ClassDB::class_exists(const StringName &p_class) {
  231. OBJTYPE_RLOCK;
  232. return classes.has(p_class);
  233. }
  234. void ClassDB::add_compatibility_class(const StringName &p_class, const StringName &p_fallback) {
  235. OBJTYPE_WLOCK;
  236. compat_classes[p_class] = p_fallback;
  237. }
  238. Object *ClassDB::instantiate(const StringName &p_class) {
  239. ClassInfo *ti;
  240. {
  241. OBJTYPE_RLOCK;
  242. ti = classes.getptr(p_class);
  243. if (!ti || ti->disabled || !ti->creation_func || (ti->native_extension && !ti->native_extension->create_instance)) {
  244. if (compat_classes.has(p_class)) {
  245. ti = classes.getptr(compat_classes[p_class]);
  246. }
  247. }
  248. ERR_FAIL_COND_V_MSG(!ti, nullptr, "Cannot get class '" + String(p_class) + "'.");
  249. ERR_FAIL_COND_V_MSG(ti->disabled, nullptr, "Class '" + String(p_class) + "' is disabled.");
  250. ERR_FAIL_COND_V_MSG(!ti->creation_func, nullptr, "Class '" + String(p_class) + "' or its base class cannot be instantiated.");
  251. }
  252. #ifdef TOOLS_ENABLED
  253. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  254. ERR_PRINT("Class '" + String(p_class) + "' can only be instantiated by editor.");
  255. return nullptr;
  256. }
  257. #endif
  258. if (ti->native_extension && ti->native_extension->create_instance) {
  259. return (Object *)ti->native_extension->create_instance(ti->native_extension->class_userdata);
  260. } else {
  261. return ti->creation_func();
  262. }
  263. }
  264. void ClassDB::set_object_extension_instance(Object *p_object, const StringName &p_class, GDExtensionClassInstancePtr p_instance) {
  265. ERR_FAIL_COND(!p_object);
  266. ClassInfo *ti;
  267. {
  268. OBJTYPE_RLOCK;
  269. ti = classes.getptr(p_class);
  270. if (!ti || ti->disabled || !ti->creation_func || (ti->native_extension && !ti->native_extension->create_instance)) {
  271. if (compat_classes.has(p_class)) {
  272. ti = classes.getptr(compat_classes[p_class]);
  273. }
  274. }
  275. ERR_FAIL_COND_MSG(!ti, "Cannot get class '" + String(p_class) + "'.");
  276. ERR_FAIL_COND_MSG(ti->disabled, "Class '" + String(p_class) + "' is disabled.");
  277. ERR_FAIL_COND_MSG(!ti->native_extension, "Class '" + String(p_class) + "' has no native extension.");
  278. }
  279. p_object->_extension = ti->native_extension;
  280. p_object->_extension_instance = p_instance;
  281. }
  282. bool ClassDB::can_instantiate(const StringName &p_class) {
  283. OBJTYPE_RLOCK;
  284. ClassInfo *ti = classes.getptr(p_class);
  285. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  286. #ifdef TOOLS_ENABLED
  287. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  288. return false;
  289. }
  290. #endif
  291. return (!ti->disabled && ti->creation_func != nullptr && !(ti->native_extension && !ti->native_extension->create_instance));
  292. }
  293. bool ClassDB::is_virtual(const StringName &p_class) {
  294. OBJTYPE_RLOCK;
  295. ClassInfo *ti = classes.getptr(p_class);
  296. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  297. #ifdef TOOLS_ENABLED
  298. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  299. return false;
  300. }
  301. #endif
  302. return (!ti->disabled && ti->creation_func != nullptr && !(ti->native_extension && !ti->native_extension->create_instance) && ti->is_virtual);
  303. }
  304. void ClassDB::_add_class2(const StringName &p_class, const StringName &p_inherits) {
  305. OBJTYPE_WLOCK;
  306. const StringName &name = p_class;
  307. ERR_FAIL_COND_MSG(classes.has(name), "Class '" + String(p_class) + "' already exists.");
  308. classes[name] = ClassInfo();
  309. ClassInfo &ti = classes[name];
  310. ti.name = name;
  311. ti.inherits = p_inherits;
  312. ti.api = current_api;
  313. if (ti.inherits) {
  314. ERR_FAIL_COND(!classes.has(ti.inherits)); //it MUST be registered.
  315. ti.inherits_ptr = &classes[ti.inherits];
  316. } else {
  317. ti.inherits_ptr = nullptr;
  318. }
  319. }
  320. static MethodInfo info_from_bind(MethodBind *p_method) {
  321. MethodInfo minfo;
  322. minfo.name = p_method->get_name();
  323. minfo.id = p_method->get_method_id();
  324. for (int i = 0; i < p_method->get_argument_count(); i++) {
  325. minfo.arguments.push_back(p_method->get_argument_info(i));
  326. }
  327. minfo.return_val = p_method->get_return_info();
  328. minfo.flags = p_method->get_hint_flags();
  329. for (int i = 0; i < p_method->get_argument_count(); i++) {
  330. if (p_method->has_default_argument(i)) {
  331. minfo.default_arguments.push_back(p_method->get_default_argument(i));
  332. }
  333. }
  334. return minfo;
  335. }
  336. void ClassDB::get_method_list(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance, bool p_exclude_from_properties) {
  337. OBJTYPE_RLOCK;
  338. ClassInfo *type = classes.getptr(p_class);
  339. while (type) {
  340. if (type->disabled) {
  341. if (p_no_inheritance) {
  342. break;
  343. }
  344. type = type->inherits_ptr;
  345. continue;
  346. }
  347. #ifdef DEBUG_METHODS_ENABLED
  348. for (const MethodInfo &E : type->virtual_methods) {
  349. p_methods->push_back(E);
  350. }
  351. for (const StringName &E : type->method_order) {
  352. if (p_exclude_from_properties && type->methods_in_properties.has(E)) {
  353. continue;
  354. }
  355. MethodBind *method = type->method_map.get(E);
  356. MethodInfo minfo = info_from_bind(method);
  357. p_methods->push_back(minfo);
  358. }
  359. #else
  360. for (KeyValue<StringName, MethodBind *> &E : type->method_map) {
  361. MethodBind *m = E.value;
  362. MethodInfo minfo = info_from_bind(m);
  363. p_methods->push_back(minfo);
  364. }
  365. #endif
  366. if (p_no_inheritance) {
  367. break;
  368. }
  369. type = type->inherits_ptr;
  370. }
  371. }
  372. bool ClassDB::get_method_info(const StringName &p_class, const StringName &p_method, MethodInfo *r_info, bool p_no_inheritance, bool p_exclude_from_properties) {
  373. OBJTYPE_RLOCK;
  374. ClassInfo *type = classes.getptr(p_class);
  375. while (type) {
  376. if (type->disabled) {
  377. if (p_no_inheritance) {
  378. break;
  379. }
  380. type = type->inherits_ptr;
  381. continue;
  382. }
  383. #ifdef DEBUG_METHODS_ENABLED
  384. MethodBind **method = type->method_map.getptr(p_method);
  385. if (method && *method) {
  386. if (r_info != nullptr) {
  387. MethodInfo minfo = info_from_bind(*method);
  388. *r_info = minfo;
  389. }
  390. return true;
  391. } else if (type->virtual_methods_map.has(p_method)) {
  392. if (r_info) {
  393. *r_info = type->virtual_methods_map[p_method];
  394. }
  395. return true;
  396. }
  397. #else
  398. if (type->method_map.has(p_method)) {
  399. if (r_info) {
  400. MethodBind *m = type->method_map[p_method];
  401. MethodInfo minfo = info_from_bind(m);
  402. *r_info = minfo;
  403. }
  404. return true;
  405. }
  406. #endif
  407. if (p_no_inheritance) {
  408. break;
  409. }
  410. type = type->inherits_ptr;
  411. }
  412. return false;
  413. }
  414. MethodBind *ClassDB::get_method(const StringName &p_class, const StringName &p_name) {
  415. OBJTYPE_RLOCK;
  416. ClassInfo *type = classes.getptr(p_class);
  417. while (type) {
  418. MethodBind **method = type->method_map.getptr(p_name);
  419. if (method && *method) {
  420. return *method;
  421. }
  422. type = type->inherits_ptr;
  423. }
  424. return nullptr;
  425. }
  426. void ClassDB::bind_integer_constant(const StringName &p_class, const StringName &p_enum, const StringName &p_name, int p_constant) {
  427. OBJTYPE_WLOCK;
  428. ClassInfo *type = classes.getptr(p_class);
  429. ERR_FAIL_COND(!type);
  430. if (type->constant_map.has(p_name)) {
  431. ERR_FAIL();
  432. }
  433. type->constant_map[p_name] = p_constant;
  434. String enum_name = p_enum;
  435. if (!enum_name.is_empty()) {
  436. if (enum_name.contains(".")) {
  437. enum_name = enum_name.get_slicec('.', 1);
  438. }
  439. List<StringName> *constants_list = type->enum_map.getptr(enum_name);
  440. if (constants_list) {
  441. constants_list->push_back(p_name);
  442. } else {
  443. List<StringName> new_list;
  444. new_list.push_back(p_name);
  445. type->enum_map[enum_name] = new_list;
  446. }
  447. }
  448. #ifdef DEBUG_METHODS_ENABLED
  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 (const StringName &E : type->constant_order) {
  458. p_constants->push_back(E);
  459. }
  460. #else
  461. for (const KeyValue<StringName, int> &E : type->constant_map) {
  462. p_constants->push_back(E.key);
  463. }
  464. #endif
  465. if (p_no_inheritance) {
  466. break;
  467. }
  468. type = type->inherits_ptr;
  469. }
  470. }
  471. int ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success) {
  472. OBJTYPE_RLOCK;
  473. ClassInfo *type = classes.getptr(p_class);
  474. while (type) {
  475. int *constant = type->constant_map.getptr(p_name);
  476. if (constant) {
  477. if (p_success) {
  478. *p_success = true;
  479. }
  480. return *constant;
  481. }
  482. type = type->inherits_ptr;
  483. }
  484. if (p_success) {
  485. *p_success = false;
  486. }
  487. return 0;
  488. }
  489. bool ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  490. OBJTYPE_RLOCK;
  491. ClassInfo *type = classes.getptr(p_class);
  492. while (type) {
  493. if (type->constant_map.has(p_name)) {
  494. return true;
  495. }
  496. if (p_no_inheritance) {
  497. return false;
  498. }
  499. type = type->inherits_ptr;
  500. }
  501. return false;
  502. }
  503. StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  504. OBJTYPE_RLOCK;
  505. ClassInfo *type = classes.getptr(p_class);
  506. while (type) {
  507. for (KeyValue<StringName, List<StringName>> &E : type->enum_map) {
  508. List<StringName> &constants_list = E.value;
  509. const List<StringName>::Element *found = constants_list.find(p_name);
  510. if (found) {
  511. return E.key;
  512. }
  513. }
  514. if (p_no_inheritance) {
  515. break;
  516. }
  517. type = type->inherits_ptr;
  518. }
  519. return StringName();
  520. }
  521. void ClassDB::get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance) {
  522. OBJTYPE_RLOCK;
  523. ClassInfo *type = classes.getptr(p_class);
  524. while (type) {
  525. for (KeyValue<StringName, List<StringName>> &E : type->enum_map) {
  526. p_enums->push_back(E.key);
  527. }
  528. if (p_no_inheritance) {
  529. break;
  530. }
  531. type = type->inherits_ptr;
  532. }
  533. }
  534. void ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance) {
  535. OBJTYPE_RLOCK;
  536. ClassInfo *type = classes.getptr(p_class);
  537. while (type) {
  538. const List<StringName> *constants = type->enum_map.getptr(p_enum);
  539. if (constants) {
  540. for (const List<StringName>::Element *E = constants->front(); E; E = E->next()) {
  541. p_constants->push_back(E->get());
  542. }
  543. }
  544. if (p_no_inheritance) {
  545. break;
  546. }
  547. type = type->inherits_ptr;
  548. }
  549. }
  550. void ClassDB::set_method_error_return_values(const StringName &p_class, const StringName &p_method, const Vector<Error> &p_values) {
  551. OBJTYPE_RLOCK;
  552. #ifdef DEBUG_METHODS_ENABLED
  553. ClassInfo *type = classes.getptr(p_class);
  554. ERR_FAIL_COND(!type);
  555. type->method_error_values[p_method] = p_values;
  556. #endif
  557. }
  558. Vector<Error> ClassDB::get_method_error_return_values(const StringName &p_class, const StringName &p_method) {
  559. #ifdef DEBUG_METHODS_ENABLED
  560. ClassInfo *type = classes.getptr(p_class);
  561. ERR_FAIL_COND_V(!type, Vector<Error>());
  562. if (!type->method_error_values.has(p_method)) {
  563. return Vector<Error>();
  564. }
  565. return type->method_error_values[p_method];
  566. #else
  567. return Vector<Error>();
  568. #endif
  569. }
  570. bool ClassDB::has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  571. OBJTYPE_RLOCK;
  572. ClassInfo *type = classes.getptr(p_class);
  573. while (type) {
  574. if (type->enum_map.has(p_name)) {
  575. return true;
  576. }
  577. if (p_no_inheritance) {
  578. return false;
  579. }
  580. type = type->inherits_ptr;
  581. }
  582. return false;
  583. }
  584. void ClassDB::add_signal(const StringName &p_class, const MethodInfo &p_signal) {
  585. OBJTYPE_WLOCK;
  586. ClassInfo *type = classes.getptr(p_class);
  587. ERR_FAIL_COND(!type);
  588. StringName sname = p_signal.name;
  589. #ifdef DEBUG_METHODS_ENABLED
  590. ClassInfo *check = type;
  591. while (check) {
  592. ERR_FAIL_COND_MSG(check->signal_map.has(sname), "Class '" + String(p_class) + "' already has signal '" + String(sname) + "'.");
  593. check = check->inherits_ptr;
  594. }
  595. #endif
  596. type->signal_map[sname] = p_signal;
  597. }
  598. void ClassDB::get_signal_list(const StringName &p_class, List<MethodInfo> *p_signals, bool p_no_inheritance) {
  599. OBJTYPE_RLOCK;
  600. ClassInfo *type = classes.getptr(p_class);
  601. ERR_FAIL_COND(!type);
  602. ClassInfo *check = type;
  603. while (check) {
  604. for (KeyValue<StringName, MethodInfo> &E : check->signal_map) {
  605. p_signals->push_back(E.value);
  606. }
  607. if (p_no_inheritance) {
  608. return;
  609. }
  610. check = check->inherits_ptr;
  611. }
  612. }
  613. bool ClassDB::has_signal(const StringName &p_class, const StringName &p_signal, bool p_no_inheritance) {
  614. OBJTYPE_RLOCK;
  615. ClassInfo *type = classes.getptr(p_class);
  616. ClassInfo *check = type;
  617. while (check) {
  618. if (check->signal_map.has(p_signal)) {
  619. return true;
  620. }
  621. if (p_no_inheritance) {
  622. return false;
  623. }
  624. check = check->inherits_ptr;
  625. }
  626. return false;
  627. }
  628. bool ClassDB::get_signal(const StringName &p_class, const StringName &p_signal, MethodInfo *r_signal) {
  629. OBJTYPE_RLOCK;
  630. ClassInfo *type = classes.getptr(p_class);
  631. ClassInfo *check = type;
  632. while (check) {
  633. if (check->signal_map.has(p_signal)) {
  634. if (r_signal) {
  635. *r_signal = check->signal_map[p_signal];
  636. }
  637. return true;
  638. }
  639. check = check->inherits_ptr;
  640. }
  641. return false;
  642. }
  643. void ClassDB::add_property_group(const StringName &p_class, const String &p_name, const String &p_prefix, int p_indent_depth) {
  644. OBJTYPE_WLOCK;
  645. ClassInfo *type = classes.getptr(p_class);
  646. ERR_FAIL_COND(!type);
  647. String prefix = p_prefix;
  648. if (p_indent_depth > 0) {
  649. prefix = vformat("%s,%d", p_prefix, p_indent_depth);
  650. }
  651. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, prefix, PROPERTY_USAGE_GROUP));
  652. }
  653. void ClassDB::add_property_subgroup(const StringName &p_class, const String &p_name, const String &p_prefix, int p_indent_depth) {
  654. OBJTYPE_WLOCK;
  655. ClassInfo *type = classes.getptr(p_class);
  656. ERR_FAIL_COND(!type);
  657. String prefix = p_prefix;
  658. if (p_indent_depth > 0) {
  659. prefix = vformat("%s,%d", p_prefix, p_indent_depth);
  660. }
  661. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, prefix, PROPERTY_USAGE_SUBGROUP));
  662. }
  663. void ClassDB::add_property_array_count(const StringName &p_class, const String &p_label, const StringName &p_count_property, const StringName &p_count_setter, const StringName &p_count_getter, const String &p_array_element_prefix, uint32_t p_count_usage) {
  664. add_property(p_class, PropertyInfo(Variant::INT, p_count_property, PROPERTY_HINT_NONE, "", p_count_usage | PROPERTY_USAGE_ARRAY, vformat("%s,%s", p_label, p_array_element_prefix)), p_count_setter, p_count_getter);
  665. }
  666. void ClassDB::add_property_array(const StringName &p_class, const StringName &p_path, const String &p_array_element_prefix) {
  667. OBJTYPE_WLOCK;
  668. ClassInfo *type = classes.getptr(p_class);
  669. ERR_FAIL_COND(!type);
  670. type->property_list.push_back(PropertyInfo(Variant::NIL, p_path, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY, p_array_element_prefix));
  671. }
  672. // NOTE: For implementation simplicity reasons, this method doesn't allow setters to have optional arguments at the end.
  673. void ClassDB::add_property(const StringName &p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index) {
  674. lock.read_lock();
  675. ClassInfo *type = classes.getptr(p_class);
  676. lock.read_unlock();
  677. ERR_FAIL_COND(!type);
  678. MethodBind *mb_set = nullptr;
  679. if (p_setter) {
  680. mb_set = get_method(p_class, p_setter);
  681. #ifdef DEBUG_METHODS_ENABLED
  682. ERR_FAIL_COND_MSG(!mb_set, "Invalid setter '" + p_class + "::" + p_setter + "' for property '" + p_pinfo.name + "'.");
  683. int exp_args = 1 + (p_index >= 0 ? 1 : 0);
  684. ERR_FAIL_COND_MSG(mb_set->get_argument_count() != exp_args, "Invalid function for setter '" + p_class + "::" + p_setter + " for property '" + p_pinfo.name + "'.");
  685. #endif
  686. }
  687. MethodBind *mb_get = nullptr;
  688. if (p_getter) {
  689. mb_get = get_method(p_class, p_getter);
  690. #ifdef DEBUG_METHODS_ENABLED
  691. ERR_FAIL_COND_MSG(!mb_get, "Invalid getter '" + p_class + "::" + p_getter + "' for property '" + p_pinfo.name + "'.");
  692. int exp_args = 0 + (p_index >= 0 ? 1 : 0);
  693. ERR_FAIL_COND_MSG(mb_get->get_argument_count() != exp_args, "Invalid function for getter '" + p_class + "::" + p_getter + "' for property: '" + p_pinfo.name + "'.");
  694. #endif
  695. }
  696. #ifdef DEBUG_METHODS_ENABLED
  697. ERR_FAIL_COND_MSG(type->property_setget.has(p_pinfo.name), "Object '" + p_class + "' already has property '" + p_pinfo.name + "'.");
  698. #endif
  699. OBJTYPE_WLOCK
  700. type->property_list.push_back(p_pinfo);
  701. type->property_map[p_pinfo.name] = p_pinfo;
  702. #ifdef DEBUG_METHODS_ENABLED
  703. if (mb_get) {
  704. type->methods_in_properties.insert(p_getter);
  705. }
  706. if (mb_set) {
  707. type->methods_in_properties.insert(p_setter);
  708. }
  709. #endif
  710. PropertySetGet psg;
  711. psg.setter = p_setter;
  712. psg.getter = p_getter;
  713. psg._setptr = mb_set;
  714. psg._getptr = mb_get;
  715. psg.index = p_index;
  716. psg.type = p_pinfo.type;
  717. type->property_setget[p_pinfo.name] = psg;
  718. }
  719. void ClassDB::set_property_default_value(const StringName &p_class, const StringName &p_name, const Variant &p_default) {
  720. if (!default_values.has(p_class)) {
  721. default_values[p_class] = HashMap<StringName, Variant>();
  722. }
  723. default_values[p_class][p_name] = p_default;
  724. }
  725. void ClassDB::add_linked_property(const StringName &p_class, const String &p_property, const String &p_linked_property) {
  726. #ifdef TOOLS_ENABLED
  727. OBJTYPE_WLOCK;
  728. ClassInfo *type = classes.getptr(p_class);
  729. ERR_FAIL_COND(!type);
  730. ERR_FAIL_COND(!type->property_map.has(p_property));
  731. ERR_FAIL_COND(!type->property_map.has(p_linked_property));
  732. PropertyInfo &pinfo = type->property_map[p_property];
  733. pinfo.linked_properties.push_back(p_linked_property);
  734. #endif
  735. }
  736. void ClassDB::get_property_list(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator) {
  737. OBJTYPE_RLOCK;
  738. ClassInfo *type = classes.getptr(p_class);
  739. ClassInfo *check = type;
  740. while (check) {
  741. for (const PropertyInfo &pi : check->property_list) {
  742. if (p_validator) {
  743. // Making a copy as we may modify it.
  744. PropertyInfo pi_mut = pi;
  745. p_validator->_validate_property(pi_mut);
  746. p_list->push_back(pi_mut);
  747. } else {
  748. p_list->push_back(pi);
  749. }
  750. }
  751. if (p_no_inheritance) {
  752. return;
  753. }
  754. check = check->inherits_ptr;
  755. }
  756. }
  757. bool ClassDB::get_property_info(const StringName &p_class, const StringName &p_property, PropertyInfo *r_info, bool p_no_inheritance, const Object *p_validator) {
  758. OBJTYPE_RLOCK;
  759. ClassInfo *check = classes.getptr(p_class);
  760. while (check) {
  761. if (check->property_map.has(p_property)) {
  762. PropertyInfo pinfo = check->property_map[p_property];
  763. if (p_validator) {
  764. p_validator->_validate_property(pinfo);
  765. }
  766. if (r_info) {
  767. *r_info = pinfo;
  768. }
  769. return true;
  770. }
  771. if (p_no_inheritance) {
  772. break;
  773. }
  774. check = check->inherits_ptr;
  775. }
  776. return false;
  777. }
  778. bool ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid) {
  779. ERR_FAIL_NULL_V(p_object, false);
  780. ClassInfo *type = classes.getptr(p_object->get_class_name());
  781. ClassInfo *check = type;
  782. while (check) {
  783. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  784. if (psg) {
  785. if (!psg->setter) {
  786. if (r_valid) {
  787. *r_valid = false;
  788. }
  789. return true; //return true but do nothing
  790. }
  791. Callable::CallError ce;
  792. if (psg->index >= 0) {
  793. Variant index = psg->index;
  794. const Variant *arg[2] = { &index, &p_value };
  795. //p_object->call(psg->setter,arg,2,ce);
  796. if (psg->_setptr) {
  797. psg->_setptr->call(p_object, arg, 2, ce);
  798. } else {
  799. p_object->callp(psg->setter, arg, 2, ce);
  800. }
  801. } else {
  802. const Variant *arg[1] = { &p_value };
  803. if (psg->_setptr) {
  804. psg->_setptr->call(p_object, arg, 1, ce);
  805. } else {
  806. p_object->callp(psg->setter, arg, 1, ce);
  807. }
  808. }
  809. if (r_valid) {
  810. *r_valid = ce.error == Callable::CallError::CALL_OK;
  811. }
  812. return true;
  813. }
  814. check = check->inherits_ptr;
  815. }
  816. return false;
  817. }
  818. bool ClassDB::get_property(Object *p_object, const StringName &p_property, Variant &r_value) {
  819. ERR_FAIL_NULL_V(p_object, false);
  820. ClassInfo *type = classes.getptr(p_object->get_class_name());
  821. ClassInfo *check = type;
  822. while (check) {
  823. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  824. if (psg) {
  825. if (!psg->getter) {
  826. return true; //return true but do nothing
  827. }
  828. if (psg->index >= 0) {
  829. Variant index = psg->index;
  830. const Variant *arg[1] = { &index };
  831. Callable::CallError ce;
  832. r_value = p_object->callp(psg->getter, arg, 1, ce);
  833. } else {
  834. Callable::CallError ce;
  835. if (psg->_getptr) {
  836. r_value = psg->_getptr->call(p_object, nullptr, 0, ce);
  837. } else {
  838. r_value = p_object->callp(psg->getter, nullptr, 0, ce);
  839. }
  840. }
  841. return true;
  842. }
  843. const int *c = check->constant_map.getptr(p_property); //constants count
  844. if (c) {
  845. r_value = *c;
  846. return true;
  847. }
  848. if (check->method_map.has(p_property)) { //methods count
  849. r_value = Callable(p_object, p_property);
  850. return true;
  851. }
  852. if (check->signal_map.has(p_property)) { //signals count
  853. r_value = Signal(p_object, p_property);
  854. return true;
  855. }
  856. check = check->inherits_ptr;
  857. }
  858. return false;
  859. }
  860. int ClassDB::get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  861. ClassInfo *type = classes.getptr(p_class);
  862. ClassInfo *check = type;
  863. while (check) {
  864. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  865. if (psg) {
  866. if (r_is_valid) {
  867. *r_is_valid = true;
  868. }
  869. return psg->index;
  870. }
  871. check = check->inherits_ptr;
  872. }
  873. if (r_is_valid) {
  874. *r_is_valid = false;
  875. }
  876. return -1;
  877. }
  878. Variant::Type ClassDB::get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  879. ClassInfo *type = classes.getptr(p_class);
  880. ClassInfo *check = type;
  881. while (check) {
  882. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  883. if (psg) {
  884. if (r_is_valid) {
  885. *r_is_valid = true;
  886. }
  887. return psg->type;
  888. }
  889. check = check->inherits_ptr;
  890. }
  891. if (r_is_valid) {
  892. *r_is_valid = false;
  893. }
  894. return Variant::NIL;
  895. }
  896. StringName ClassDB::get_property_setter(const StringName &p_class, const StringName &p_property) {
  897. ClassInfo *type = classes.getptr(p_class);
  898. ClassInfo *check = type;
  899. while (check) {
  900. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  901. if (psg) {
  902. return psg->setter;
  903. }
  904. check = check->inherits_ptr;
  905. }
  906. return StringName();
  907. }
  908. StringName ClassDB::get_property_getter(const StringName &p_class, const StringName &p_property) {
  909. ClassInfo *type = classes.getptr(p_class);
  910. ClassInfo *check = type;
  911. while (check) {
  912. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  913. if (psg) {
  914. return psg->getter;
  915. }
  916. check = check->inherits_ptr;
  917. }
  918. return StringName();
  919. }
  920. bool ClassDB::has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance) {
  921. ClassInfo *type = classes.getptr(p_class);
  922. ClassInfo *check = type;
  923. while (check) {
  924. if (check->property_setget.has(p_property)) {
  925. return true;
  926. }
  927. if (p_no_inheritance) {
  928. break;
  929. }
  930. check = check->inherits_ptr;
  931. }
  932. return false;
  933. }
  934. void ClassDB::set_method_flags(const StringName &p_class, const StringName &p_method, int p_flags) {
  935. OBJTYPE_WLOCK;
  936. ClassInfo *type = classes.getptr(p_class);
  937. ClassInfo *check = type;
  938. ERR_FAIL_COND(!check);
  939. ERR_FAIL_COND(!check->method_map.has(p_method));
  940. check->method_map[p_method]->set_hint_flags(p_flags);
  941. }
  942. bool ClassDB::has_method(const StringName &p_class, const StringName &p_method, bool p_no_inheritance) {
  943. ClassInfo *type = classes.getptr(p_class);
  944. ClassInfo *check = type;
  945. while (check) {
  946. if (check->method_map.has(p_method)) {
  947. return true;
  948. }
  949. if (p_no_inheritance) {
  950. return false;
  951. }
  952. check = check->inherits_ptr;
  953. }
  954. return false;
  955. }
  956. void ClassDB::bind_method_custom(const StringName &p_class, MethodBind *p_method) {
  957. ClassInfo *type = classes.getptr(p_class);
  958. if (!type) {
  959. ERR_FAIL_MSG("Couldn't bind custom method '" + p_method->get_name() + "' for instance '" + p_class + "'.");
  960. }
  961. if (type->method_map.has(p_method->get_name())) {
  962. // overloading not supported
  963. ERR_FAIL_MSG("Method already bound '" + p_class + "::" + p_method->get_name() + "'.");
  964. }
  965. #ifdef DEBUG_METHODS_ENABLED
  966. type->method_order.push_back(p_method->get_name());
  967. #endif
  968. type->method_map[p_method->get_name()] = p_method;
  969. }
  970. #ifdef DEBUG_METHODS_ENABLED
  971. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount) {
  972. StringName mdname = method_name.name;
  973. #else
  974. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const char *method_name, const Variant **p_defs, int p_defcount) {
  975. StringName mdname = StaticCString::create(method_name);
  976. #endif
  977. OBJTYPE_WLOCK;
  978. ERR_FAIL_COND_V(!p_bind, nullptr);
  979. p_bind->set_name(mdname);
  980. String instance_type = p_bind->get_instance_class();
  981. #ifdef DEBUG_ENABLED
  982. ERR_FAIL_COND_V_MSG(has_method(instance_type, mdname), nullptr, "Class " + String(instance_type) + " already has a method " + String(mdname) + ".");
  983. #endif
  984. ClassInfo *type = classes.getptr(instance_type);
  985. if (!type) {
  986. memdelete(p_bind);
  987. ERR_FAIL_V_MSG(nullptr, "Couldn't bind method '" + mdname + "' for instance '" + instance_type + "'.");
  988. }
  989. if (type->method_map.has(mdname)) {
  990. memdelete(p_bind);
  991. // overloading not supported
  992. ERR_FAIL_V_MSG(nullptr, "Method already bound '" + instance_type + "::" + mdname + "'.");
  993. }
  994. #ifdef DEBUG_METHODS_ENABLED
  995. if (method_name.args.size() > p_bind->get_argument_count()) {
  996. memdelete(p_bind);
  997. ERR_FAIL_V_MSG(nullptr, "Method definition provides more arguments than the method actually has '" + instance_type + "::" + mdname + "'.");
  998. }
  999. p_bind->set_argument_names(method_name.args);
  1000. type->method_order.push_back(mdname);
  1001. #endif
  1002. type->method_map[mdname] = p_bind;
  1003. Vector<Variant> defvals;
  1004. defvals.resize(p_defcount);
  1005. for (int i = 0; i < p_defcount; i++) {
  1006. defvals.write[i] = *p_defs[i];
  1007. }
  1008. p_bind->set_default_arguments(defvals);
  1009. p_bind->set_hint_flags(p_flags);
  1010. return p_bind;
  1011. }
  1012. void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual, const Vector<String> &p_arg_names, bool p_object_core) {
  1013. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1014. OBJTYPE_WLOCK;
  1015. #ifdef DEBUG_METHODS_ENABLED
  1016. MethodInfo mi = p_method;
  1017. if (p_virtual) {
  1018. mi.flags |= METHOD_FLAG_VIRTUAL;
  1019. }
  1020. if (p_object_core) {
  1021. mi.flags |= METHOD_FLAG_OBJECT_CORE;
  1022. }
  1023. if (!p_object_core) {
  1024. if (p_arg_names.size() != mi.arguments.size()) {
  1025. WARN_PRINT("Mismatch argument name count for virtual method: " + String(p_class) + "::" + p_method.name);
  1026. } else {
  1027. for (int i = 0; i < p_arg_names.size(); i++) {
  1028. mi.arguments[i].name = p_arg_names[i];
  1029. }
  1030. }
  1031. }
  1032. if (classes[p_class].virtual_methods_map.has(p_method.name)) {
  1033. // overloading not supported
  1034. ERR_FAIL_MSG("Virtual method already bound '" + String(p_class) + "::" + p_method.name + "'.");
  1035. }
  1036. classes[p_class].virtual_methods.push_back(mi);
  1037. classes[p_class].virtual_methods_map[p_method.name] = mi;
  1038. #endif
  1039. }
  1040. void ClassDB::get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance) {
  1041. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1042. #ifdef DEBUG_METHODS_ENABLED
  1043. ClassInfo *type = classes.getptr(p_class);
  1044. ClassInfo *check = type;
  1045. while (check) {
  1046. for (const MethodInfo &E : check->virtual_methods) {
  1047. p_methods->push_back(E);
  1048. }
  1049. if (p_no_inheritance) {
  1050. return;
  1051. }
  1052. check = check->inherits_ptr;
  1053. }
  1054. #endif
  1055. }
  1056. void ClassDB::set_class_enabled(const StringName &p_class, bool p_enable) {
  1057. OBJTYPE_WLOCK;
  1058. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1059. classes[p_class].disabled = !p_enable;
  1060. }
  1061. bool ClassDB::is_class_enabled(const StringName &p_class) {
  1062. OBJTYPE_RLOCK;
  1063. ClassInfo *ti = classes.getptr(p_class);
  1064. if (!ti || !ti->creation_func) {
  1065. if (compat_classes.has(p_class)) {
  1066. ti = classes.getptr(compat_classes[p_class]);
  1067. }
  1068. }
  1069. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  1070. return !ti->disabled;
  1071. }
  1072. bool ClassDB::is_class_exposed(const StringName &p_class) {
  1073. OBJTYPE_RLOCK;
  1074. ClassInfo *ti = classes.getptr(p_class);
  1075. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  1076. return ti->exposed;
  1077. }
  1078. void ClassDB::add_resource_base_extension(const StringName &p_extension, const StringName &p_class) {
  1079. if (resource_base_extensions.has(p_extension)) {
  1080. return;
  1081. }
  1082. resource_base_extensions[p_extension] = p_class;
  1083. }
  1084. void ClassDB::get_resource_base_extensions(List<String> *p_extensions) {
  1085. for (const KeyValue<StringName, StringName> &E : resource_base_extensions) {
  1086. p_extensions->push_back(E.key);
  1087. }
  1088. }
  1089. bool ClassDB::is_resource_extension(const StringName &p_extension) {
  1090. return resource_base_extensions.has(p_extension);
  1091. }
  1092. void ClassDB::get_extensions_for_type(const StringName &p_class, List<String> *p_extensions) {
  1093. for (const KeyValue<StringName, StringName> &E : resource_base_extensions) {
  1094. if (is_parent_class(p_class, E.value) || is_parent_class(E.value, p_class)) {
  1095. p_extensions->push_back(E.key);
  1096. }
  1097. }
  1098. }
  1099. HashMap<StringName, HashMap<StringName, Variant>> ClassDB::default_values;
  1100. HashSet<StringName> ClassDB::default_values_cached;
  1101. Variant ClassDB::class_get_default_property_value(const StringName &p_class, const StringName &p_property, bool *r_valid) {
  1102. if (!default_values_cached.has(p_class)) {
  1103. if (!default_values.has(p_class)) {
  1104. default_values[p_class] = HashMap<StringName, Variant>();
  1105. }
  1106. Object *c = nullptr;
  1107. bool cleanup_c = false;
  1108. if (Engine::get_singleton()->has_singleton(p_class)) {
  1109. c = Engine::get_singleton()->get_singleton_object(p_class);
  1110. cleanup_c = false;
  1111. } else if (ClassDB::can_instantiate(p_class) && !ClassDB::is_virtual(p_class)) {
  1112. c = ClassDB::instantiate(p_class);
  1113. cleanup_c = true;
  1114. }
  1115. if (c) {
  1116. List<PropertyInfo> plist;
  1117. c->get_property_list(&plist);
  1118. for (const PropertyInfo &E : plist) {
  1119. if (E.usage & (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR)) {
  1120. if (!default_values[p_class].has(E.name)) {
  1121. Variant v = c->get(E.name);
  1122. default_values[p_class][E.name] = v;
  1123. }
  1124. }
  1125. }
  1126. if (cleanup_c) {
  1127. memdelete(c);
  1128. }
  1129. }
  1130. default_values_cached.insert(p_class);
  1131. }
  1132. if (!default_values.has(p_class)) {
  1133. if (r_valid != nullptr) {
  1134. *r_valid = false;
  1135. }
  1136. return Variant();
  1137. }
  1138. if (!default_values[p_class].has(p_property)) {
  1139. if (r_valid != nullptr) {
  1140. *r_valid = false;
  1141. }
  1142. return Variant();
  1143. }
  1144. if (r_valid != nullptr) {
  1145. *r_valid = true;
  1146. }
  1147. Variant var = default_values[p_class][p_property];
  1148. #ifdef DEBUG_ENABLED
  1149. // Some properties may have an instantiated Object as default value,
  1150. // (like Path2D's `curve` used to have), but that's not a good practice.
  1151. // Instead, those properties should use PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT
  1152. // to be auto-instantiated when created in the editor with the following method:
  1153. // EditorNode::get_editor_data().instantiate_object_properties(obj);
  1154. if (var.get_type() == Variant::OBJECT) {
  1155. Object *obj = var.get_validated_object();
  1156. if (obj) {
  1157. WARN_PRINT(vformat("Instantiated %s used as default value for %s's \"%s\" property.", obj->get_class(), p_class, p_property));
  1158. }
  1159. }
  1160. #endif
  1161. return var;
  1162. }
  1163. void ClassDB::register_extension_class(ObjectNativeExtension *p_extension) {
  1164. GLOBAL_LOCK_FUNCTION;
  1165. ERR_FAIL_COND_MSG(classes.has(p_extension->class_name), "Class already registered: " + String(p_extension->class_name));
  1166. ERR_FAIL_COND_MSG(!classes.has(p_extension->parent_class_name), "Parent class name for extension class not found: " + String(p_extension->parent_class_name));
  1167. ClassInfo *parent = classes.getptr(p_extension->parent_class_name);
  1168. ClassInfo c;
  1169. c.api = p_extension->editor_class ? API_EDITOR_EXTENSION : API_EXTENSION;
  1170. c.native_extension = p_extension;
  1171. c.name = p_extension->class_name;
  1172. c.creation_func = parent->creation_func;
  1173. c.inherits = parent->name;
  1174. c.class_ptr = parent->class_ptr;
  1175. c.inherits_ptr = parent;
  1176. c.exposed = true;
  1177. classes[p_extension->class_name] = c;
  1178. }
  1179. void ClassDB::unregister_extension_class(const StringName &p_class) {
  1180. ERR_FAIL_COND(!classes.has(p_class));
  1181. classes.erase(p_class);
  1182. }
  1183. HashMap<StringName, ClassDB::NativeStruct> ClassDB::native_structs;
  1184. void ClassDB::register_native_struct(const StringName &p_name, const String &p_code, uint64_t p_current_size) {
  1185. NativeStruct ns;
  1186. ns.ccode = p_code;
  1187. ns.struct_size = p_current_size;
  1188. native_structs[p_name] = ns;
  1189. }
  1190. void ClassDB::get_native_struct_list(List<StringName> *r_names) {
  1191. for (const KeyValue<StringName, NativeStruct> &E : native_structs) {
  1192. r_names->push_back(E.key);
  1193. }
  1194. }
  1195. String ClassDB::get_native_struct_code(const StringName &p_name) {
  1196. ERR_FAIL_COND_V(!native_structs.has(p_name), String());
  1197. return native_structs[p_name].ccode;
  1198. }
  1199. uint64_t ClassDB::get_native_struct_size(const StringName &p_name) {
  1200. ERR_FAIL_COND_V(!native_structs.has(p_name), 0);
  1201. return native_structs[p_name].struct_size;
  1202. }
  1203. RWLock ClassDB::lock;
  1204. void ClassDB::cleanup_defaults() {
  1205. default_values.clear();
  1206. default_values_cached.clear();
  1207. }
  1208. void ClassDB::cleanup() {
  1209. //OBJTYPE_LOCK; hah not here
  1210. for (KeyValue<StringName, ClassInfo> &E : classes) {
  1211. ClassInfo &ti = E.value;
  1212. for (KeyValue<StringName, MethodBind *> &F : ti.method_map) {
  1213. memdelete(F.value);
  1214. }
  1215. }
  1216. classes.clear();
  1217. resource_base_extensions.clear();
  1218. compat_classes.clear();
  1219. native_structs.clear();
  1220. }
  1221. //