class_db.cpp 43 KB

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