class_db.cpp 66 KB

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