2
0

class_db.cpp 63 KB

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