class_db.cpp 50 KB

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