class_db.cpp 68 KB

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