class_db.cpp 62 KB

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