class_db.cpp 63 KB

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