class_db.cpp 67 KB

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