class_db.cpp 63 KB

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