class_db.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  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_murmur3_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_murmur3_one_64(t->name.hash(), hash);
  144. hash = hash_murmur3_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_murmur3_one_64(mb->get_name().hash(), hash);
  159. hash = hash_murmur3_one_64(mb->get_argument_count(), hash);
  160. hash = hash_murmur3_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_murmur3_one_64(info.type, hash);
  164. hash = hash_murmur3_one_64(info.name.hash(), hash);
  165. hash = hash_murmur3_one_64(info.hint, hash);
  166. hash = hash_murmur3_one_64(info.hint_string.hash(), hash);
  167. }
  168. hash = hash_murmur3_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_murmur3_one_64(da.hash(), hash);
  173. }
  174. hash = hash_murmur3_one_64(mb->get_hint_flags(), hash);
  175. }
  176. }
  177. { //constants
  178. List<StringName> snames;
  179. for (const KeyValue<StringName, int64_t> &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_murmur3_one_64(F.hash(), hash);
  185. hash = hash_murmur3_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_murmur3_one_64(F.hash(), hash);
  197. for (int i = 0; i < mi.arguments.size(); i++) {
  198. hash = hash_murmur3_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_murmur3_one_64(F.hash(), hash);
  212. hash = hash_murmur3_one_64(psg->setter.hash(), hash);
  213. hash = hash_murmur3_one_64(psg->getter.hash(), hash);
  214. }
  215. }
  216. //property list
  217. for (const PropertyInfo &F : t->property_list) {
  218. hash = hash_murmur3_one_64(F.name.hash(), hash);
  219. hash = hash_murmur3_one_64(F.type, hash);
  220. hash = hash_murmur3_one_64(F.hint, hash);
  221. hash = hash_murmur3_one_64(F.hint_string.hash(), hash);
  222. hash = hash_murmur3_one_64(F.usage, hash);
  223. }
  224. }
  225. return hash_fmix32(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, int64_t p_constant, bool p_is_bitfield) {
  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. ClassInfo::EnumInfo *constants_list = type->enum_map.getptr(enum_name);
  440. if (constants_list) {
  441. constants_list->constants.push_back(p_name);
  442. constants_list->is_bitfield = p_is_bitfield;
  443. } else {
  444. ClassInfo::EnumInfo new_list;
  445. new_list.is_bitfield = p_is_bitfield;
  446. new_list.constants.push_back(p_name);
  447. type->enum_map[enum_name] = new_list;
  448. }
  449. }
  450. #ifdef DEBUG_METHODS_ENABLED
  451. type->constant_order.push_back(p_name);
  452. #endif
  453. }
  454. void ClassDB::get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance) {
  455. OBJTYPE_RLOCK;
  456. ClassInfo *type = classes.getptr(p_class);
  457. while (type) {
  458. #ifdef DEBUG_METHODS_ENABLED
  459. for (const StringName &E : type->constant_order) {
  460. p_constants->push_back(E);
  461. }
  462. #else
  463. for (const KeyValue<StringName, int64_t> &E : type->constant_map) {
  464. p_constants->push_back(E.key);
  465. }
  466. #endif
  467. if (p_no_inheritance) {
  468. break;
  469. }
  470. type = type->inherits_ptr;
  471. }
  472. }
  473. int64_t ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success) {
  474. OBJTYPE_RLOCK;
  475. ClassInfo *type = classes.getptr(p_class);
  476. while (type) {
  477. int64_t *constant = type->constant_map.getptr(p_name);
  478. if (constant) {
  479. if (p_success) {
  480. *p_success = true;
  481. }
  482. return *constant;
  483. }
  484. type = type->inherits_ptr;
  485. }
  486. if (p_success) {
  487. *p_success = false;
  488. }
  489. return 0;
  490. }
  491. bool ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  492. OBJTYPE_RLOCK;
  493. ClassInfo *type = classes.getptr(p_class);
  494. while (type) {
  495. if (type->constant_map.has(p_name)) {
  496. return true;
  497. }
  498. if (p_no_inheritance) {
  499. return false;
  500. }
  501. type = type->inherits_ptr;
  502. }
  503. return false;
  504. }
  505. StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  506. OBJTYPE_RLOCK;
  507. ClassInfo *type = classes.getptr(p_class);
  508. while (type) {
  509. for (KeyValue<StringName, ClassInfo::EnumInfo> &E : type->enum_map) {
  510. List<StringName> &constants_list = E.value.constants;
  511. const List<StringName>::Element *found = constants_list.find(p_name);
  512. if (found) {
  513. return E.key;
  514. }
  515. }
  516. if (p_no_inheritance) {
  517. break;
  518. }
  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. for (KeyValue<StringName, ClassInfo::EnumInfo> &E : type->enum_map) {
  528. p_enums->push_back(E.key);
  529. }
  530. if (p_no_inheritance) {
  531. break;
  532. }
  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 ClassInfo::EnumInfo *constants = type->enum_map.getptr(p_enum);
  541. if (constants) {
  542. for (const List<StringName>::Element *E = constants->constants.front(); E; E = E->next()) {
  543. p_constants->push_back(E->get());
  544. }
  545. }
  546. if (p_no_inheritance) {
  547. break;
  548. }
  549. type = type->inherits_ptr;
  550. }
  551. }
  552. void ClassDB::set_method_error_return_values(const StringName &p_class, const StringName &p_method, const Vector<Error> &p_values) {
  553. OBJTYPE_RLOCK;
  554. #ifdef DEBUG_METHODS_ENABLED
  555. ClassInfo *type = classes.getptr(p_class);
  556. ERR_FAIL_COND(!type);
  557. type->method_error_values[p_method] = p_values;
  558. #endif
  559. }
  560. Vector<Error> ClassDB::get_method_error_return_values(const StringName &p_class, const StringName &p_method) {
  561. #ifdef DEBUG_METHODS_ENABLED
  562. ClassInfo *type = classes.getptr(p_class);
  563. ERR_FAIL_COND_V(!type, Vector<Error>());
  564. if (!type->method_error_values.has(p_method)) {
  565. return Vector<Error>();
  566. }
  567. return type->method_error_values[p_method];
  568. #else
  569. return Vector<Error>();
  570. #endif
  571. }
  572. bool ClassDB::has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  573. OBJTYPE_RLOCK;
  574. ClassInfo *type = classes.getptr(p_class);
  575. while (type) {
  576. if (type->enum_map.has(p_name)) {
  577. return true;
  578. }
  579. if (p_no_inheritance) {
  580. return false;
  581. }
  582. type = type->inherits_ptr;
  583. }
  584. return false;
  585. }
  586. bool ClassDB::is_enum_bitfield(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  587. OBJTYPE_RLOCK;
  588. ClassInfo *type = classes.getptr(p_class);
  589. while (type) {
  590. if (type->enum_map.has(p_name) && type->enum_map[p_name].is_bitfield) {
  591. return true;
  592. }
  593. if (p_no_inheritance) {
  594. return false;
  595. }
  596. type = type->inherits_ptr;
  597. }
  598. return false;
  599. }
  600. void ClassDB::add_signal(const StringName &p_class, const MethodInfo &p_signal) {
  601. OBJTYPE_WLOCK;
  602. ClassInfo *type = classes.getptr(p_class);
  603. ERR_FAIL_COND(!type);
  604. StringName sname = p_signal.name;
  605. #ifdef DEBUG_METHODS_ENABLED
  606. ClassInfo *check = type;
  607. while (check) {
  608. ERR_FAIL_COND_MSG(check->signal_map.has(sname), "Class '" + String(p_class) + "' already has signal '" + String(sname) + "'.");
  609. check = check->inherits_ptr;
  610. }
  611. #endif
  612. type->signal_map[sname] = p_signal;
  613. }
  614. void ClassDB::get_signal_list(const StringName &p_class, List<MethodInfo> *p_signals, bool p_no_inheritance) {
  615. OBJTYPE_RLOCK;
  616. ClassInfo *type = classes.getptr(p_class);
  617. ERR_FAIL_COND(!type);
  618. ClassInfo *check = type;
  619. while (check) {
  620. for (KeyValue<StringName, MethodInfo> &E : check->signal_map) {
  621. p_signals->push_back(E.value);
  622. }
  623. if (p_no_inheritance) {
  624. return;
  625. }
  626. check = check->inherits_ptr;
  627. }
  628. }
  629. bool ClassDB::has_signal(const StringName &p_class, const StringName &p_signal, bool p_no_inheritance) {
  630. OBJTYPE_RLOCK;
  631. ClassInfo *type = classes.getptr(p_class);
  632. ClassInfo *check = type;
  633. while (check) {
  634. if (check->signal_map.has(p_signal)) {
  635. return true;
  636. }
  637. if (p_no_inheritance) {
  638. return false;
  639. }
  640. check = check->inherits_ptr;
  641. }
  642. return false;
  643. }
  644. bool ClassDB::get_signal(const StringName &p_class, const StringName &p_signal, MethodInfo *r_signal) {
  645. OBJTYPE_RLOCK;
  646. ClassInfo *type = classes.getptr(p_class);
  647. ClassInfo *check = type;
  648. while (check) {
  649. if (check->signal_map.has(p_signal)) {
  650. if (r_signal) {
  651. *r_signal = check->signal_map[p_signal];
  652. }
  653. return true;
  654. }
  655. check = check->inherits_ptr;
  656. }
  657. return false;
  658. }
  659. void ClassDB::add_property_group(const StringName &p_class, const String &p_name, const String &p_prefix, int p_indent_depth) {
  660. OBJTYPE_WLOCK;
  661. ClassInfo *type = classes.getptr(p_class);
  662. ERR_FAIL_COND(!type);
  663. String prefix = p_prefix;
  664. if (p_indent_depth > 0) {
  665. prefix = vformat("%s,%d", p_prefix, p_indent_depth);
  666. }
  667. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, prefix, PROPERTY_USAGE_GROUP));
  668. }
  669. void ClassDB::add_property_subgroup(const StringName &p_class, const String &p_name, const String &p_prefix, int p_indent_depth) {
  670. OBJTYPE_WLOCK;
  671. ClassInfo *type = classes.getptr(p_class);
  672. ERR_FAIL_COND(!type);
  673. String prefix = p_prefix;
  674. if (p_indent_depth > 0) {
  675. prefix = vformat("%s,%d", p_prefix, p_indent_depth);
  676. }
  677. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, prefix, PROPERTY_USAGE_SUBGROUP));
  678. }
  679. 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) {
  680. 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);
  681. }
  682. void ClassDB::add_property_array(const StringName &p_class, const StringName &p_path, const String &p_array_element_prefix) {
  683. OBJTYPE_WLOCK;
  684. ClassInfo *type = classes.getptr(p_class);
  685. ERR_FAIL_COND(!type);
  686. type->property_list.push_back(PropertyInfo(Variant::NIL, p_path, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY, p_array_element_prefix));
  687. }
  688. // NOTE: For implementation simplicity reasons, this method doesn't allow setters to have optional arguments at the end.
  689. void ClassDB::add_property(const StringName &p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index) {
  690. lock.read_lock();
  691. ClassInfo *type = classes.getptr(p_class);
  692. lock.read_unlock();
  693. ERR_FAIL_COND(!type);
  694. MethodBind *mb_set = nullptr;
  695. if (p_setter) {
  696. mb_set = get_method(p_class, p_setter);
  697. #ifdef DEBUG_METHODS_ENABLED
  698. ERR_FAIL_COND_MSG(!mb_set, "Invalid setter '" + p_class + "::" + p_setter + "' for property '" + p_pinfo.name + "'.");
  699. int exp_args = 1 + (p_index >= 0 ? 1 : 0);
  700. ERR_FAIL_COND_MSG(mb_set->get_argument_count() != exp_args, "Invalid function for setter '" + p_class + "::" + p_setter + " for property '" + p_pinfo.name + "'.");
  701. #endif
  702. }
  703. MethodBind *mb_get = nullptr;
  704. if (p_getter) {
  705. mb_get = get_method(p_class, p_getter);
  706. #ifdef DEBUG_METHODS_ENABLED
  707. ERR_FAIL_COND_MSG(!mb_get, "Invalid getter '" + p_class + "::" + p_getter + "' for property '" + p_pinfo.name + "'.");
  708. int exp_args = 0 + (p_index >= 0 ? 1 : 0);
  709. ERR_FAIL_COND_MSG(mb_get->get_argument_count() != exp_args, "Invalid function for getter '" + p_class + "::" + p_getter + "' for property: '" + p_pinfo.name + "'.");
  710. #endif
  711. }
  712. #ifdef DEBUG_METHODS_ENABLED
  713. ERR_FAIL_COND_MSG(type->property_setget.has(p_pinfo.name), "Object '" + p_class + "' already has property '" + p_pinfo.name + "'.");
  714. #endif
  715. OBJTYPE_WLOCK
  716. type->property_list.push_back(p_pinfo);
  717. type->property_map[p_pinfo.name] = p_pinfo;
  718. #ifdef DEBUG_METHODS_ENABLED
  719. if (mb_get) {
  720. type->methods_in_properties.insert(p_getter);
  721. }
  722. if (mb_set) {
  723. type->methods_in_properties.insert(p_setter);
  724. }
  725. #endif
  726. PropertySetGet psg;
  727. psg.setter = p_setter;
  728. psg.getter = p_getter;
  729. psg._setptr = mb_set;
  730. psg._getptr = mb_get;
  731. psg.index = p_index;
  732. psg.type = p_pinfo.type;
  733. type->property_setget[p_pinfo.name] = psg;
  734. }
  735. void ClassDB::set_property_default_value(const StringName &p_class, const StringName &p_name, const Variant &p_default) {
  736. if (!default_values.has(p_class)) {
  737. default_values[p_class] = HashMap<StringName, Variant>();
  738. }
  739. default_values[p_class][p_name] = p_default;
  740. }
  741. void ClassDB::add_linked_property(const StringName &p_class, const String &p_property, const String &p_linked_property) {
  742. #ifdef TOOLS_ENABLED
  743. OBJTYPE_WLOCK;
  744. ClassInfo *type = classes.getptr(p_class);
  745. ERR_FAIL_COND(!type);
  746. ERR_FAIL_COND(!type->property_map.has(p_property));
  747. ERR_FAIL_COND(!type->property_map.has(p_linked_property));
  748. PropertyInfo &pinfo = type->property_map[p_property];
  749. pinfo.linked_properties.push_back(p_linked_property);
  750. #endif
  751. }
  752. void ClassDB::get_property_list(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator) {
  753. OBJTYPE_RLOCK;
  754. ClassInfo *type = classes.getptr(p_class);
  755. ClassInfo *check = type;
  756. while (check) {
  757. for (const PropertyInfo &pi : check->property_list) {
  758. if (p_validator) {
  759. // Making a copy as we may modify it.
  760. PropertyInfo pi_mut = pi;
  761. p_validator->_validate_property(pi_mut);
  762. p_list->push_back(pi_mut);
  763. } else {
  764. p_list->push_back(pi);
  765. }
  766. }
  767. if (p_no_inheritance) {
  768. return;
  769. }
  770. check = check->inherits_ptr;
  771. }
  772. }
  773. bool ClassDB::get_property_info(const StringName &p_class, const StringName &p_property, PropertyInfo *r_info, bool p_no_inheritance, const Object *p_validator) {
  774. OBJTYPE_RLOCK;
  775. ClassInfo *check = classes.getptr(p_class);
  776. while (check) {
  777. if (check->property_map.has(p_property)) {
  778. PropertyInfo pinfo = check->property_map[p_property];
  779. if (p_validator) {
  780. p_validator->_validate_property(pinfo);
  781. }
  782. if (r_info) {
  783. *r_info = pinfo;
  784. }
  785. return true;
  786. }
  787. if (p_no_inheritance) {
  788. break;
  789. }
  790. check = check->inherits_ptr;
  791. }
  792. return false;
  793. }
  794. bool ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid) {
  795. ERR_FAIL_NULL_V(p_object, false);
  796. ClassInfo *type = classes.getptr(p_object->get_class_name());
  797. ClassInfo *check = type;
  798. while (check) {
  799. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  800. if (psg) {
  801. if (!psg->setter) {
  802. if (r_valid) {
  803. *r_valid = false;
  804. }
  805. return true; //return true but do nothing
  806. }
  807. Callable::CallError ce;
  808. if (psg->index >= 0) {
  809. Variant index = psg->index;
  810. const Variant *arg[2] = { &index, &p_value };
  811. //p_object->call(psg->setter,arg,2,ce);
  812. if (psg->_setptr) {
  813. psg->_setptr->call(p_object, arg, 2, ce);
  814. } else {
  815. p_object->callp(psg->setter, arg, 2, ce);
  816. }
  817. } else {
  818. const Variant *arg[1] = { &p_value };
  819. if (psg->_setptr) {
  820. psg->_setptr->call(p_object, arg, 1, ce);
  821. } else {
  822. p_object->callp(psg->setter, arg, 1, ce);
  823. }
  824. }
  825. if (r_valid) {
  826. *r_valid = ce.error == Callable::CallError::CALL_OK;
  827. }
  828. return true;
  829. }
  830. check = check->inherits_ptr;
  831. }
  832. return false;
  833. }
  834. bool ClassDB::get_property(Object *p_object, const StringName &p_property, Variant &r_value) {
  835. ERR_FAIL_NULL_V(p_object, false);
  836. ClassInfo *type = classes.getptr(p_object->get_class_name());
  837. ClassInfo *check = type;
  838. while (check) {
  839. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  840. if (psg) {
  841. if (!psg->getter) {
  842. return true; //return true but do nothing
  843. }
  844. if (psg->index >= 0) {
  845. Variant index = psg->index;
  846. const Variant *arg[1] = { &index };
  847. Callable::CallError ce;
  848. r_value = p_object->callp(psg->getter, arg, 1, ce);
  849. } else {
  850. Callable::CallError ce;
  851. if (psg->_getptr) {
  852. r_value = psg->_getptr->call(p_object, nullptr, 0, ce);
  853. } else {
  854. r_value = p_object->callp(psg->getter, nullptr, 0, ce);
  855. }
  856. }
  857. return true;
  858. }
  859. const int64_t *c = check->constant_map.getptr(p_property); //constants count
  860. if (c) {
  861. r_value = *c;
  862. return true;
  863. }
  864. if (check->method_map.has(p_property)) { //methods count
  865. r_value = Callable(p_object, p_property);
  866. return true;
  867. }
  868. if (check->signal_map.has(p_property)) { //signals count
  869. r_value = Signal(p_object, p_property);
  870. return true;
  871. }
  872. check = check->inherits_ptr;
  873. }
  874. return false;
  875. }
  876. int ClassDB::get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  877. ClassInfo *type = classes.getptr(p_class);
  878. ClassInfo *check = type;
  879. while (check) {
  880. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  881. if (psg) {
  882. if (r_is_valid) {
  883. *r_is_valid = true;
  884. }
  885. return psg->index;
  886. }
  887. check = check->inherits_ptr;
  888. }
  889. if (r_is_valid) {
  890. *r_is_valid = false;
  891. }
  892. return -1;
  893. }
  894. Variant::Type ClassDB::get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  895. ClassInfo *type = classes.getptr(p_class);
  896. ClassInfo *check = type;
  897. while (check) {
  898. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  899. if (psg) {
  900. if (r_is_valid) {
  901. *r_is_valid = true;
  902. }
  903. return psg->type;
  904. }
  905. check = check->inherits_ptr;
  906. }
  907. if (r_is_valid) {
  908. *r_is_valid = false;
  909. }
  910. return Variant::NIL;
  911. }
  912. StringName ClassDB::get_property_setter(const StringName &p_class, const StringName &p_property) {
  913. ClassInfo *type = classes.getptr(p_class);
  914. ClassInfo *check = type;
  915. while (check) {
  916. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  917. if (psg) {
  918. return psg->setter;
  919. }
  920. check = check->inherits_ptr;
  921. }
  922. return StringName();
  923. }
  924. StringName ClassDB::get_property_getter(const StringName &p_class, const StringName &p_property) {
  925. ClassInfo *type = classes.getptr(p_class);
  926. ClassInfo *check = type;
  927. while (check) {
  928. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  929. if (psg) {
  930. return psg->getter;
  931. }
  932. check = check->inherits_ptr;
  933. }
  934. return StringName();
  935. }
  936. bool ClassDB::has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance) {
  937. ClassInfo *type = classes.getptr(p_class);
  938. ClassInfo *check = type;
  939. while (check) {
  940. if (check->property_setget.has(p_property)) {
  941. return true;
  942. }
  943. if (p_no_inheritance) {
  944. break;
  945. }
  946. check = check->inherits_ptr;
  947. }
  948. return false;
  949. }
  950. void ClassDB::set_method_flags(const StringName &p_class, const StringName &p_method, int p_flags) {
  951. OBJTYPE_WLOCK;
  952. ClassInfo *type = classes.getptr(p_class);
  953. ClassInfo *check = type;
  954. ERR_FAIL_COND(!check);
  955. ERR_FAIL_COND(!check->method_map.has(p_method));
  956. check->method_map[p_method]->set_hint_flags(p_flags);
  957. }
  958. bool ClassDB::has_method(const StringName &p_class, const StringName &p_method, bool p_no_inheritance) {
  959. ClassInfo *type = classes.getptr(p_class);
  960. ClassInfo *check = type;
  961. while (check) {
  962. if (check->method_map.has(p_method)) {
  963. return true;
  964. }
  965. if (p_no_inheritance) {
  966. return false;
  967. }
  968. check = check->inherits_ptr;
  969. }
  970. return false;
  971. }
  972. void ClassDB::bind_method_custom(const StringName &p_class, MethodBind *p_method) {
  973. ClassInfo *type = classes.getptr(p_class);
  974. if (!type) {
  975. ERR_FAIL_MSG("Couldn't bind custom method '" + p_method->get_name() + "' for instance '" + p_class + "'.");
  976. }
  977. if (type->method_map.has(p_method->get_name())) {
  978. // overloading not supported
  979. ERR_FAIL_MSG("Method already bound '" + p_class + "::" + p_method->get_name() + "'.");
  980. }
  981. #ifdef DEBUG_METHODS_ENABLED
  982. type->method_order.push_back(p_method->get_name());
  983. #endif
  984. type->method_map[p_method->get_name()] = p_method;
  985. }
  986. #ifdef DEBUG_METHODS_ENABLED
  987. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount) {
  988. StringName mdname = method_name.name;
  989. #else
  990. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const char *method_name, const Variant **p_defs, int p_defcount) {
  991. StringName mdname = StaticCString::create(method_name);
  992. #endif
  993. OBJTYPE_WLOCK;
  994. ERR_FAIL_COND_V(!p_bind, nullptr);
  995. p_bind->set_name(mdname);
  996. String instance_type = p_bind->get_instance_class();
  997. #ifdef DEBUG_ENABLED
  998. ERR_FAIL_COND_V_MSG(has_method(instance_type, mdname), nullptr, "Class " + String(instance_type) + " already has a method " + String(mdname) + ".");
  999. #endif
  1000. ClassInfo *type = classes.getptr(instance_type);
  1001. if (!type) {
  1002. memdelete(p_bind);
  1003. ERR_FAIL_V_MSG(nullptr, "Couldn't bind method '" + mdname + "' for instance '" + instance_type + "'.");
  1004. }
  1005. if (type->method_map.has(mdname)) {
  1006. memdelete(p_bind);
  1007. // overloading not supported
  1008. ERR_FAIL_V_MSG(nullptr, "Method already bound '" + instance_type + "::" + mdname + "'.");
  1009. }
  1010. #ifdef DEBUG_METHODS_ENABLED
  1011. if (method_name.args.size() > p_bind->get_argument_count()) {
  1012. memdelete(p_bind);
  1013. ERR_FAIL_V_MSG(nullptr, "Method definition provides more arguments than the method actually has '" + instance_type + "::" + mdname + "'.");
  1014. }
  1015. p_bind->set_argument_names(method_name.args);
  1016. type->method_order.push_back(mdname);
  1017. #endif
  1018. type->method_map[mdname] = p_bind;
  1019. Vector<Variant> defvals;
  1020. defvals.resize(p_defcount);
  1021. for (int i = 0; i < p_defcount; i++) {
  1022. defvals.write[i] = *p_defs[i];
  1023. }
  1024. p_bind->set_default_arguments(defvals);
  1025. p_bind->set_hint_flags(p_flags);
  1026. return p_bind;
  1027. }
  1028. 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) {
  1029. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1030. OBJTYPE_WLOCK;
  1031. #ifdef DEBUG_METHODS_ENABLED
  1032. MethodInfo mi = p_method;
  1033. if (p_virtual) {
  1034. mi.flags |= METHOD_FLAG_VIRTUAL;
  1035. }
  1036. if (p_object_core) {
  1037. mi.flags |= METHOD_FLAG_OBJECT_CORE;
  1038. }
  1039. if (!p_object_core) {
  1040. if (p_arg_names.size() != mi.arguments.size()) {
  1041. WARN_PRINT("Mismatch argument name count for virtual method: " + String(p_class) + "::" + p_method.name);
  1042. } else {
  1043. for (int i = 0; i < p_arg_names.size(); i++) {
  1044. mi.arguments[i].name = p_arg_names[i];
  1045. }
  1046. }
  1047. }
  1048. if (classes[p_class].virtual_methods_map.has(p_method.name)) {
  1049. // overloading not supported
  1050. ERR_FAIL_MSG("Virtual method already bound '" + String(p_class) + "::" + p_method.name + "'.");
  1051. }
  1052. classes[p_class].virtual_methods.push_back(mi);
  1053. classes[p_class].virtual_methods_map[p_method.name] = mi;
  1054. #endif
  1055. }
  1056. void ClassDB::get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance) {
  1057. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1058. #ifdef DEBUG_METHODS_ENABLED
  1059. ClassInfo *type = classes.getptr(p_class);
  1060. ClassInfo *check = type;
  1061. while (check) {
  1062. for (const MethodInfo &E : check->virtual_methods) {
  1063. p_methods->push_back(E);
  1064. }
  1065. if (p_no_inheritance) {
  1066. return;
  1067. }
  1068. check = check->inherits_ptr;
  1069. }
  1070. #endif
  1071. }
  1072. void ClassDB::set_class_enabled(const StringName &p_class, bool p_enable) {
  1073. OBJTYPE_WLOCK;
  1074. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1075. classes[p_class].disabled = !p_enable;
  1076. }
  1077. bool ClassDB::is_class_enabled(const StringName &p_class) {
  1078. OBJTYPE_RLOCK;
  1079. ClassInfo *ti = classes.getptr(p_class);
  1080. if (!ti || !ti->creation_func) {
  1081. if (compat_classes.has(p_class)) {
  1082. ti = classes.getptr(compat_classes[p_class]);
  1083. }
  1084. }
  1085. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  1086. return !ti->disabled;
  1087. }
  1088. bool ClassDB::is_class_exposed(const StringName &p_class) {
  1089. OBJTYPE_RLOCK;
  1090. ClassInfo *ti = classes.getptr(p_class);
  1091. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  1092. return ti->exposed;
  1093. }
  1094. void ClassDB::add_resource_base_extension(const StringName &p_extension, const StringName &p_class) {
  1095. if (resource_base_extensions.has(p_extension)) {
  1096. return;
  1097. }
  1098. resource_base_extensions[p_extension] = p_class;
  1099. }
  1100. void ClassDB::get_resource_base_extensions(List<String> *p_extensions) {
  1101. for (const KeyValue<StringName, StringName> &E : resource_base_extensions) {
  1102. p_extensions->push_back(E.key);
  1103. }
  1104. }
  1105. bool ClassDB::is_resource_extension(const StringName &p_extension) {
  1106. return resource_base_extensions.has(p_extension);
  1107. }
  1108. void ClassDB::get_extensions_for_type(const StringName &p_class, List<String> *p_extensions) {
  1109. for (const KeyValue<StringName, StringName> &E : resource_base_extensions) {
  1110. if (is_parent_class(p_class, E.value) || is_parent_class(E.value, p_class)) {
  1111. p_extensions->push_back(E.key);
  1112. }
  1113. }
  1114. }
  1115. HashMap<StringName, HashMap<StringName, Variant>> ClassDB::default_values;
  1116. HashSet<StringName> ClassDB::default_values_cached;
  1117. Variant ClassDB::class_get_default_property_value(const StringName &p_class, const StringName &p_property, bool *r_valid) {
  1118. if (!default_values_cached.has(p_class)) {
  1119. if (!default_values.has(p_class)) {
  1120. default_values[p_class] = HashMap<StringName, Variant>();
  1121. }
  1122. Object *c = nullptr;
  1123. bool cleanup_c = false;
  1124. if (Engine::get_singleton()->has_singleton(p_class)) {
  1125. c = Engine::get_singleton()->get_singleton_object(p_class);
  1126. cleanup_c = false;
  1127. } else if (ClassDB::can_instantiate(p_class) && !ClassDB::is_virtual(p_class)) {
  1128. c = ClassDB::instantiate(p_class);
  1129. cleanup_c = true;
  1130. }
  1131. if (c) {
  1132. List<PropertyInfo> plist;
  1133. c->get_property_list(&plist);
  1134. for (const PropertyInfo &E : plist) {
  1135. if (E.usage & (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR)) {
  1136. if (!default_values[p_class].has(E.name)) {
  1137. Variant v = c->get(E.name);
  1138. default_values[p_class][E.name] = v;
  1139. }
  1140. }
  1141. }
  1142. if (cleanup_c) {
  1143. memdelete(c);
  1144. }
  1145. }
  1146. default_values_cached.insert(p_class);
  1147. }
  1148. if (!default_values.has(p_class)) {
  1149. if (r_valid != nullptr) {
  1150. *r_valid = false;
  1151. }
  1152. return Variant();
  1153. }
  1154. if (!default_values[p_class].has(p_property)) {
  1155. if (r_valid != nullptr) {
  1156. *r_valid = false;
  1157. }
  1158. return Variant();
  1159. }
  1160. if (r_valid != nullptr) {
  1161. *r_valid = true;
  1162. }
  1163. Variant var = default_values[p_class][p_property];
  1164. #ifdef DEBUG_ENABLED
  1165. // Some properties may have an instantiated Object as default value,
  1166. // (like Path2D's `curve` used to have), but that's not a good practice.
  1167. // Instead, those properties should use PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT
  1168. // to be auto-instantiated when created in the editor with the following method:
  1169. // EditorNode::get_editor_data().instantiate_object_properties(obj);
  1170. if (var.get_type() == Variant::OBJECT) {
  1171. Object *obj = var.get_validated_object();
  1172. if (obj) {
  1173. WARN_PRINT(vformat("Instantiated %s used as default value for %s's \"%s\" property.", obj->get_class(), p_class, p_property));
  1174. }
  1175. }
  1176. #endif
  1177. return var;
  1178. }
  1179. void ClassDB::register_extension_class(ObjectNativeExtension *p_extension) {
  1180. GLOBAL_LOCK_FUNCTION;
  1181. ERR_FAIL_COND_MSG(classes.has(p_extension->class_name), "Class already registered: " + String(p_extension->class_name));
  1182. 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));
  1183. ClassInfo *parent = classes.getptr(p_extension->parent_class_name);
  1184. ClassInfo c;
  1185. c.api = p_extension->editor_class ? API_EDITOR_EXTENSION : API_EXTENSION;
  1186. c.native_extension = p_extension;
  1187. c.name = p_extension->class_name;
  1188. c.creation_func = parent->creation_func;
  1189. c.inherits = parent->name;
  1190. c.class_ptr = parent->class_ptr;
  1191. c.inherits_ptr = parent;
  1192. c.exposed = true;
  1193. classes[p_extension->class_name] = c;
  1194. }
  1195. void ClassDB::unregister_extension_class(const StringName &p_class) {
  1196. ERR_FAIL_COND(!classes.has(p_class));
  1197. classes.erase(p_class);
  1198. }
  1199. HashMap<StringName, ClassDB::NativeStruct> ClassDB::native_structs;
  1200. void ClassDB::register_native_struct(const StringName &p_name, const String &p_code, uint64_t p_current_size) {
  1201. NativeStruct ns;
  1202. ns.ccode = p_code;
  1203. ns.struct_size = p_current_size;
  1204. native_structs[p_name] = ns;
  1205. }
  1206. void ClassDB::get_native_struct_list(List<StringName> *r_names) {
  1207. for (const KeyValue<StringName, NativeStruct> &E : native_structs) {
  1208. r_names->push_back(E.key);
  1209. }
  1210. }
  1211. String ClassDB::get_native_struct_code(const StringName &p_name) {
  1212. ERR_FAIL_COND_V(!native_structs.has(p_name), String());
  1213. return native_structs[p_name].ccode;
  1214. }
  1215. uint64_t ClassDB::get_native_struct_size(const StringName &p_name) {
  1216. ERR_FAIL_COND_V(!native_structs.has(p_name), 0);
  1217. return native_structs[p_name].struct_size;
  1218. }
  1219. RWLock ClassDB::lock;
  1220. void ClassDB::cleanup_defaults() {
  1221. default_values.clear();
  1222. default_values_cached.clear();
  1223. }
  1224. void ClassDB::cleanup() {
  1225. //OBJTYPE_LOCK; hah not here
  1226. for (KeyValue<StringName, ClassInfo> &E : classes) {
  1227. ClassInfo &ti = E.value;
  1228. for (KeyValue<StringName, MethodBind *> &F : ti.method_map) {
  1229. memdelete(F.value);
  1230. }
  1231. }
  1232. classes.clear();
  1233. resource_base_extensions.clear();
  1234. compat_classes.clear();
  1235. native_structs.clear();
  1236. }
  1237. //