class_db.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. /*************************************************************************/
  2. /* class_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/os/mutex.h"
  33. #include "core/version.h"
  34. #define OBJTYPE_RLOCK RWLockRead _rw_lockr_(lock);
  35. #define OBJTYPE_WLOCK RWLockWrite _rw_lockw_(lock);
  36. #ifdef DEBUG_METHODS_ENABLED
  37. MethodDefinition D_METHODP(const char *p_name, const char *const **p_args, uint32_t p_argcount) {
  38. MethodDefinition md;
  39. md.name = StaticCString::create(p_name);
  40. md.args.resize(p_argcount);
  41. for (uint32_t i = 0; i < p_argcount; i++) {
  42. md.args.write[i] = StaticCString::create(*p_args[i]);
  43. }
  44. return md;
  45. }
  46. #endif
  47. ClassDB::APIType ClassDB::current_api = API_CORE;
  48. void ClassDB::set_current_api(APIType p_api) {
  49. current_api = p_api;
  50. }
  51. ClassDB::APIType ClassDB::get_current_api() {
  52. return current_api;
  53. }
  54. HashMap<StringName, ClassDB::ClassInfo> ClassDB::classes;
  55. HashMap<StringName, StringName> ClassDB::resource_base_extensions;
  56. HashMap<StringName, StringName> ClassDB::compat_classes;
  57. bool ClassDB::_is_parent_class(const StringName &p_class, const StringName &p_inherits) {
  58. if (!classes.has(p_class)) {
  59. return false;
  60. }
  61. StringName inherits = p_class;
  62. while (inherits.operator String().length()) {
  63. if (inherits == p_inherits) {
  64. return true;
  65. }
  66. inherits = _get_parent_class(inherits);
  67. }
  68. return false;
  69. }
  70. bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) {
  71. OBJTYPE_RLOCK;
  72. return _is_parent_class(p_class, p_inherits);
  73. }
  74. void ClassDB::get_class_list(List<StringName> *p_classes) {
  75. OBJTYPE_RLOCK;
  76. for (const KeyValue<StringName, ClassInfo> &E : classes) {
  77. p_classes->push_back(E.key);
  78. }
  79. p_classes->sort();
  80. }
  81. void ClassDB::get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
  82. OBJTYPE_RLOCK;
  83. for (const KeyValue<StringName, ClassInfo> &E : classes) {
  84. if (E.key != p_class && _is_parent_class(E.key, p_class)) {
  85. p_classes->push_back(E.key);
  86. }
  87. }
  88. }
  89. void ClassDB::get_direct_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes) {
  90. OBJTYPE_RLOCK;
  91. for (const KeyValue<StringName, ClassInfo> &E : classes) {
  92. if (E.key != p_class && _get_parent_class(E.key) == p_class) {
  93. p_classes->push_back(E.key);
  94. }
  95. }
  96. }
  97. StringName ClassDB::get_parent_class_nocheck(const StringName &p_class) {
  98. OBJTYPE_RLOCK;
  99. ClassInfo *ti = classes.getptr(p_class);
  100. if (!ti) {
  101. return StringName();
  102. }
  103. return ti->inherits;
  104. }
  105. StringName ClassDB::get_compatibility_remapped_class(const StringName &p_class) {
  106. if (classes.has(p_class)) {
  107. return p_class;
  108. }
  109. if (compat_classes.has(p_class)) {
  110. return compat_classes[p_class];
  111. }
  112. return p_class;
  113. }
  114. StringName ClassDB::_get_parent_class(const StringName &p_class) {
  115. ClassInfo *ti = classes.getptr(p_class);
  116. ERR_FAIL_COND_V_MSG(!ti, StringName(), "Cannot get class '" + String(p_class) + "'.");
  117. return ti->inherits;
  118. }
  119. StringName ClassDB::get_parent_class(const StringName &p_class) {
  120. OBJTYPE_RLOCK;
  121. return _get_parent_class(p_class);
  122. }
  123. ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) {
  124. OBJTYPE_RLOCK;
  125. ClassInfo *ti = classes.getptr(p_class);
  126. ERR_FAIL_COND_V_MSG(!ti, API_NONE, "Cannot get class '" + String(p_class) + "'.");
  127. return ti->api;
  128. }
  129. uint64_t ClassDB::get_api_hash(APIType p_api) {
  130. OBJTYPE_RLOCK;
  131. #ifdef DEBUG_METHODS_ENABLED
  132. uint64_t hash = hash_murmur3_one_64(HashMapHasherDefault::hash(VERSION_FULL_CONFIG));
  133. List<StringName> class_list;
  134. ClassDB::get_class_list(&class_list);
  135. // Must be alphabetically sorted for hash to compute.
  136. class_list.sort_custom<StringName::AlphCompare>();
  137. for (const StringName &E : class_list) {
  138. ClassInfo *t = classes.getptr(E);
  139. ERR_FAIL_COND_V_MSG(!t, 0, "Cannot get class '" + String(E) + "'.");
  140. if (t->api != p_api || !t->exposed) {
  141. continue;
  142. }
  143. hash = hash_murmur3_one_64(t->name.hash(), hash);
  144. hash = hash_murmur3_one_64(t->inherits.hash(), hash);
  145. { //methods
  146. List<StringName> snames;
  147. for (const KeyValue<StringName, MethodBind *> &F : t->method_map) {
  148. String name = F.key.operator String();
  149. ERR_CONTINUE(name.is_empty());
  150. if (name[0] == '_') {
  151. continue; // Ignore non-virtual methods that start with an underscore
  152. }
  153. snames.push_back(F.key);
  154. }
  155. snames.sort_custom<StringName::AlphCompare>();
  156. for (const StringName &F : snames) {
  157. MethodBind *mb = t->method_map[F];
  158. hash = hash_murmur3_one_64(mb->get_name().hash(), hash);
  159. hash = hash_murmur3_one_64(mb->get_argument_count(), hash);
  160. hash = hash_murmur3_one_64(mb->get_argument_type(-1), hash); //return
  161. for (int i = 0; i < mb->get_argument_count(); i++) {
  162. const PropertyInfo info = mb->get_argument_info(i);
  163. hash = hash_murmur3_one_64(info.type, hash);
  164. hash = hash_murmur3_one_64(info.name.hash(), hash);
  165. hash = hash_murmur3_one_64(info.hint, hash);
  166. hash = hash_murmur3_one_64(info.hint_string.hash(), hash);
  167. }
  168. hash = hash_murmur3_one_64(mb->get_default_argument_count(), hash);
  169. for (int i = 0; i < mb->get_default_argument_count(); i++) {
  170. //hash should not change, i hope for tis
  171. Variant da = mb->get_default_argument(i);
  172. hash = hash_murmur3_one_64(da.hash(), hash);
  173. }
  174. hash = hash_murmur3_one_64(mb->get_hint_flags(), hash);
  175. }
  176. }
  177. { //constants
  178. List<StringName> snames;
  179. for (const KeyValue<StringName, int64_t> &F : t->constant_map) {
  180. snames.push_back(F.key);
  181. }
  182. snames.sort_custom<StringName::AlphCompare>();
  183. for (const StringName &F : snames) {
  184. hash = hash_murmur3_one_64(F.hash(), hash);
  185. hash = hash_murmur3_one_64(t->constant_map[F], hash);
  186. }
  187. }
  188. { //signals
  189. List<StringName> snames;
  190. for (const KeyValue<StringName, MethodInfo> &F : t->signal_map) {
  191. snames.push_back(F.key);
  192. }
  193. snames.sort_custom<StringName::AlphCompare>();
  194. for (const StringName &F : snames) {
  195. MethodInfo &mi = t->signal_map[F];
  196. hash = hash_murmur3_one_64(F.hash(), hash);
  197. for (int i = 0; i < mi.arguments.size(); i++) {
  198. hash = hash_murmur3_one_64(mi.arguments[i].type, hash);
  199. }
  200. }
  201. }
  202. { //properties
  203. List<StringName> snames;
  204. for (const KeyValue<StringName, PropertySetGet> &F : t->property_setget) {
  205. snames.push_back(F.key);
  206. }
  207. snames.sort_custom<StringName::AlphCompare>();
  208. for (const StringName &F : snames) {
  209. PropertySetGet *psg = t->property_setget.getptr(F);
  210. ERR_FAIL_COND_V(!psg, 0);
  211. hash = hash_murmur3_one_64(F.hash(), hash);
  212. hash = hash_murmur3_one_64(psg->setter.hash(), hash);
  213. hash = hash_murmur3_one_64(psg->getter.hash(), hash);
  214. }
  215. }
  216. //property list
  217. for (const PropertyInfo &F : t->property_list) {
  218. hash = hash_murmur3_one_64(F.name.hash(), hash);
  219. hash = hash_murmur3_one_64(F.type, hash);
  220. hash = hash_murmur3_one_64(F.hint, hash);
  221. hash = hash_murmur3_one_64(F.hint_string.hash(), hash);
  222. hash = hash_murmur3_one_64(F.usage, hash);
  223. }
  224. }
  225. return hash_fmix32(hash);
  226. #else
  227. return 0;
  228. #endif
  229. }
  230. bool ClassDB::class_exists(const StringName &p_class) {
  231. OBJTYPE_RLOCK;
  232. return classes.has(p_class);
  233. }
  234. void ClassDB::add_compatibility_class(const StringName &p_class, const StringName &p_fallback) {
  235. OBJTYPE_WLOCK;
  236. compat_classes[p_class] = p_fallback;
  237. }
  238. StringName ClassDB::get_compatibility_class(const StringName &p_class) {
  239. if (compat_classes.has(p_class)) {
  240. return compat_classes[p_class];
  241. }
  242. return StringName();
  243. }
  244. Object *ClassDB::instantiate(const StringName &p_class) {
  245. ClassInfo *ti;
  246. {
  247. OBJTYPE_RLOCK;
  248. ti = classes.getptr(p_class);
  249. if (!ti || ti->disabled || !ti->creation_func || (ti->native_extension && !ti->native_extension->create_instance)) {
  250. if (compat_classes.has(p_class)) {
  251. ti = classes.getptr(compat_classes[p_class]);
  252. }
  253. }
  254. ERR_FAIL_COND_V_MSG(!ti, nullptr, "Cannot get class '" + String(p_class) + "'.");
  255. ERR_FAIL_COND_V_MSG(ti->disabled, nullptr, "Class '" + String(p_class) + "' is disabled.");
  256. ERR_FAIL_COND_V_MSG(!ti->creation_func, nullptr, "Class '" + String(p_class) + "' or its base class cannot be instantiated.");
  257. }
  258. #ifdef TOOLS_ENABLED
  259. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  260. ERR_PRINT("Class '" + String(p_class) + "' can only be instantiated by editor.");
  261. return nullptr;
  262. }
  263. #endif
  264. if (ti->native_extension && ti->native_extension->create_instance) {
  265. return (Object *)ti->native_extension->create_instance(ti->native_extension->class_userdata);
  266. } else {
  267. return ti->creation_func();
  268. }
  269. }
  270. void ClassDB::set_object_extension_instance(Object *p_object, const StringName &p_class, GDExtensionClassInstancePtr p_instance) {
  271. ERR_FAIL_COND(!p_object);
  272. ClassInfo *ti;
  273. {
  274. OBJTYPE_RLOCK;
  275. ti = classes.getptr(p_class);
  276. if (!ti || ti->disabled || !ti->creation_func || (ti->native_extension && !ti->native_extension->create_instance)) {
  277. if (compat_classes.has(p_class)) {
  278. ti = classes.getptr(compat_classes[p_class]);
  279. }
  280. }
  281. ERR_FAIL_COND_MSG(!ti, "Cannot get class '" + String(p_class) + "'.");
  282. ERR_FAIL_COND_MSG(ti->disabled, "Class '" + String(p_class) + "' is disabled.");
  283. ERR_FAIL_COND_MSG(!ti->native_extension, "Class '" + String(p_class) + "' has no native extension.");
  284. }
  285. p_object->_extension = ti->native_extension;
  286. p_object->_extension_instance = p_instance;
  287. }
  288. bool ClassDB::can_instantiate(const StringName &p_class) {
  289. OBJTYPE_RLOCK;
  290. ClassInfo *ti = classes.getptr(p_class);
  291. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  292. #ifdef TOOLS_ENABLED
  293. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  294. return false;
  295. }
  296. #endif
  297. return (!ti->disabled && ti->creation_func != nullptr && !(ti->native_extension && !ti->native_extension->create_instance));
  298. }
  299. bool ClassDB::is_virtual(const StringName &p_class) {
  300. OBJTYPE_RLOCK;
  301. ClassInfo *ti = classes.getptr(p_class);
  302. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  303. #ifdef TOOLS_ENABLED
  304. if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) {
  305. return false;
  306. }
  307. #endif
  308. return (!ti->disabled && ti->creation_func != nullptr && !(ti->native_extension && !ti->native_extension->create_instance) && ti->is_virtual);
  309. }
  310. void ClassDB::_add_class2(const StringName &p_class, const StringName &p_inherits) {
  311. OBJTYPE_WLOCK;
  312. const StringName &name = p_class;
  313. ERR_FAIL_COND_MSG(classes.has(name), "Class '" + String(p_class) + "' already exists.");
  314. classes[name] = ClassInfo();
  315. ClassInfo &ti = classes[name];
  316. ti.name = name;
  317. ti.inherits = p_inherits;
  318. ti.api = current_api;
  319. if (ti.inherits) {
  320. ERR_FAIL_COND(!classes.has(ti.inherits)); //it MUST be registered.
  321. ti.inherits_ptr = &classes[ti.inherits];
  322. } else {
  323. ti.inherits_ptr = nullptr;
  324. }
  325. }
  326. static MethodInfo info_from_bind(MethodBind *p_method) {
  327. MethodInfo minfo;
  328. minfo.name = p_method->get_name();
  329. minfo.id = p_method->get_method_id();
  330. for (int i = 0; i < p_method->get_argument_count(); i++) {
  331. minfo.arguments.push_back(p_method->get_argument_info(i));
  332. }
  333. minfo.return_val = p_method->get_return_info();
  334. minfo.flags = p_method->get_hint_flags();
  335. for (int i = 0; i < p_method->get_argument_count(); i++) {
  336. if (p_method->has_default_argument(i)) {
  337. minfo.default_arguments.push_back(p_method->get_default_argument(i));
  338. }
  339. }
  340. return minfo;
  341. }
  342. void ClassDB::get_method_list(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance, bool p_exclude_from_properties) {
  343. OBJTYPE_RLOCK;
  344. ClassInfo *type = classes.getptr(p_class);
  345. while (type) {
  346. if (type->disabled) {
  347. if (p_no_inheritance) {
  348. break;
  349. }
  350. type = type->inherits_ptr;
  351. continue;
  352. }
  353. #ifdef DEBUG_METHODS_ENABLED
  354. for (const MethodInfo &E : type->virtual_methods) {
  355. p_methods->push_back(E);
  356. }
  357. for (const StringName &E : type->method_order) {
  358. if (p_exclude_from_properties && type->methods_in_properties.has(E)) {
  359. continue;
  360. }
  361. MethodBind *method = type->method_map.get(E);
  362. MethodInfo minfo = info_from_bind(method);
  363. p_methods->push_back(minfo);
  364. }
  365. #else
  366. for (KeyValue<StringName, MethodBind *> &E : type->method_map) {
  367. MethodBind *m = E.value;
  368. MethodInfo minfo = info_from_bind(m);
  369. p_methods->push_back(minfo);
  370. }
  371. #endif
  372. if (p_no_inheritance) {
  373. break;
  374. }
  375. type = type->inherits_ptr;
  376. }
  377. }
  378. 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) {
  379. OBJTYPE_RLOCK;
  380. ClassInfo *type = classes.getptr(p_class);
  381. while (type) {
  382. if (type->disabled) {
  383. if (p_no_inheritance) {
  384. break;
  385. }
  386. type = type->inherits_ptr;
  387. continue;
  388. }
  389. #ifdef DEBUG_METHODS_ENABLED
  390. MethodBind **method = type->method_map.getptr(p_method);
  391. if (method && *method) {
  392. if (r_info != nullptr) {
  393. MethodInfo minfo = info_from_bind(*method);
  394. *r_info = minfo;
  395. }
  396. return true;
  397. } else if (type->virtual_methods_map.has(p_method)) {
  398. if (r_info) {
  399. *r_info = type->virtual_methods_map[p_method];
  400. }
  401. return true;
  402. }
  403. #else
  404. if (type->method_map.has(p_method)) {
  405. if (r_info) {
  406. MethodBind *m = type->method_map[p_method];
  407. MethodInfo minfo = info_from_bind(m);
  408. *r_info = minfo;
  409. }
  410. return true;
  411. }
  412. #endif
  413. if (p_no_inheritance) {
  414. break;
  415. }
  416. type = type->inherits_ptr;
  417. }
  418. return false;
  419. }
  420. MethodBind *ClassDB::get_method(const StringName &p_class, const StringName &p_name) {
  421. OBJTYPE_RLOCK;
  422. ClassInfo *type = classes.getptr(p_class);
  423. while (type) {
  424. MethodBind **method = type->method_map.getptr(p_name);
  425. if (method && *method) {
  426. return *method;
  427. }
  428. type = type->inherits_ptr;
  429. }
  430. return nullptr;
  431. }
  432. 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) {
  433. OBJTYPE_WLOCK;
  434. ClassInfo *type = classes.getptr(p_class);
  435. ERR_FAIL_COND(!type);
  436. if (type->constant_map.has(p_name)) {
  437. ERR_FAIL();
  438. }
  439. type->constant_map[p_name] = p_constant;
  440. String enum_name = p_enum;
  441. if (!enum_name.is_empty()) {
  442. if (enum_name.contains(".")) {
  443. enum_name = enum_name.get_slicec('.', 1);
  444. }
  445. ClassInfo::EnumInfo *constants_list = type->enum_map.getptr(enum_name);
  446. if (constants_list) {
  447. constants_list->constants.push_back(p_name);
  448. constants_list->is_bitfield = p_is_bitfield;
  449. } else {
  450. ClassInfo::EnumInfo new_list;
  451. new_list.is_bitfield = p_is_bitfield;
  452. new_list.constants.push_back(p_name);
  453. type->enum_map[enum_name] = new_list;
  454. }
  455. }
  456. #ifdef DEBUG_METHODS_ENABLED
  457. type->constant_order.push_back(p_name);
  458. #endif
  459. }
  460. void ClassDB::get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance) {
  461. OBJTYPE_RLOCK;
  462. ClassInfo *type = classes.getptr(p_class);
  463. while (type) {
  464. #ifdef DEBUG_METHODS_ENABLED
  465. for (const StringName &E : type->constant_order) {
  466. p_constants->push_back(E);
  467. }
  468. #else
  469. for (const KeyValue<StringName, int64_t> &E : type->constant_map) {
  470. p_constants->push_back(E.key);
  471. }
  472. #endif
  473. if (p_no_inheritance) {
  474. break;
  475. }
  476. type = type->inherits_ptr;
  477. }
  478. }
  479. int64_t ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success) {
  480. OBJTYPE_RLOCK;
  481. ClassInfo *type = classes.getptr(p_class);
  482. while (type) {
  483. int64_t *constant = type->constant_map.getptr(p_name);
  484. if (constant) {
  485. if (p_success) {
  486. *p_success = true;
  487. }
  488. return *constant;
  489. }
  490. type = type->inherits_ptr;
  491. }
  492. if (p_success) {
  493. *p_success = false;
  494. }
  495. return 0;
  496. }
  497. bool ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  498. OBJTYPE_RLOCK;
  499. ClassInfo *type = classes.getptr(p_class);
  500. while (type) {
  501. if (type->constant_map.has(p_name)) {
  502. return true;
  503. }
  504. if (p_no_inheritance) {
  505. return false;
  506. }
  507. type = type->inherits_ptr;
  508. }
  509. return false;
  510. }
  511. StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  512. OBJTYPE_RLOCK;
  513. ClassInfo *type = classes.getptr(p_class);
  514. while (type) {
  515. for (KeyValue<StringName, ClassInfo::EnumInfo> &E : type->enum_map) {
  516. List<StringName> &constants_list = E.value.constants;
  517. const List<StringName>::Element *found = constants_list.find(p_name);
  518. if (found) {
  519. return E.key;
  520. }
  521. }
  522. if (p_no_inheritance) {
  523. break;
  524. }
  525. type = type->inherits_ptr;
  526. }
  527. return StringName();
  528. }
  529. void ClassDB::get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance) {
  530. OBJTYPE_RLOCK;
  531. ClassInfo *type = classes.getptr(p_class);
  532. while (type) {
  533. for (KeyValue<StringName, ClassInfo::EnumInfo> &E : type->enum_map) {
  534. p_enums->push_back(E.key);
  535. }
  536. if (p_no_inheritance) {
  537. break;
  538. }
  539. type = type->inherits_ptr;
  540. }
  541. }
  542. void ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance) {
  543. OBJTYPE_RLOCK;
  544. ClassInfo *type = classes.getptr(p_class);
  545. while (type) {
  546. const ClassInfo::EnumInfo *constants = type->enum_map.getptr(p_enum);
  547. if (constants) {
  548. for (const List<StringName>::Element *E = constants->constants.front(); E; E = E->next()) {
  549. p_constants->push_back(E->get());
  550. }
  551. }
  552. if (p_no_inheritance) {
  553. break;
  554. }
  555. type = type->inherits_ptr;
  556. }
  557. }
  558. void ClassDB::set_method_error_return_values(const StringName &p_class, const StringName &p_method, const Vector<Error> &p_values) {
  559. OBJTYPE_RLOCK;
  560. #ifdef DEBUG_METHODS_ENABLED
  561. ClassInfo *type = classes.getptr(p_class);
  562. ERR_FAIL_COND(!type);
  563. type->method_error_values[p_method] = p_values;
  564. #endif
  565. }
  566. Vector<Error> ClassDB::get_method_error_return_values(const StringName &p_class, const StringName &p_method) {
  567. #ifdef DEBUG_METHODS_ENABLED
  568. ClassInfo *type = classes.getptr(p_class);
  569. ERR_FAIL_COND_V(!type, Vector<Error>());
  570. if (!type->method_error_values.has(p_method)) {
  571. return Vector<Error>();
  572. }
  573. return type->method_error_values[p_method];
  574. #else
  575. return Vector<Error>();
  576. #endif
  577. }
  578. bool ClassDB::has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  579. OBJTYPE_RLOCK;
  580. ClassInfo *type = classes.getptr(p_class);
  581. while (type) {
  582. if (type->enum_map.has(p_name)) {
  583. return true;
  584. }
  585. if (p_no_inheritance) {
  586. return false;
  587. }
  588. type = type->inherits_ptr;
  589. }
  590. return false;
  591. }
  592. bool ClassDB::is_enum_bitfield(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {
  593. OBJTYPE_RLOCK;
  594. ClassInfo *type = classes.getptr(p_class);
  595. while (type) {
  596. if (type->enum_map.has(p_name) && type->enum_map[p_name].is_bitfield) {
  597. return true;
  598. }
  599. if (p_no_inheritance) {
  600. return false;
  601. }
  602. type = type->inherits_ptr;
  603. }
  604. return false;
  605. }
  606. void ClassDB::add_signal(const StringName &p_class, const MethodInfo &p_signal) {
  607. OBJTYPE_WLOCK;
  608. ClassInfo *type = classes.getptr(p_class);
  609. ERR_FAIL_COND(!type);
  610. StringName sname = p_signal.name;
  611. #ifdef DEBUG_METHODS_ENABLED
  612. ClassInfo *check = type;
  613. while (check) {
  614. ERR_FAIL_COND_MSG(check->signal_map.has(sname), "Class '" + String(p_class) + "' already has signal '" + String(sname) + "'.");
  615. check = check->inherits_ptr;
  616. }
  617. #endif
  618. type->signal_map[sname] = p_signal;
  619. }
  620. void ClassDB::get_signal_list(const StringName &p_class, List<MethodInfo> *p_signals, bool p_no_inheritance) {
  621. OBJTYPE_RLOCK;
  622. ClassInfo *type = classes.getptr(p_class);
  623. ERR_FAIL_COND(!type);
  624. ClassInfo *check = type;
  625. while (check) {
  626. for (KeyValue<StringName, MethodInfo> &E : check->signal_map) {
  627. p_signals->push_back(E.value);
  628. }
  629. if (p_no_inheritance) {
  630. return;
  631. }
  632. check = check->inherits_ptr;
  633. }
  634. }
  635. bool ClassDB::has_signal(const StringName &p_class, const StringName &p_signal, bool p_no_inheritance) {
  636. OBJTYPE_RLOCK;
  637. ClassInfo *type = classes.getptr(p_class);
  638. ClassInfo *check = type;
  639. while (check) {
  640. if (check->signal_map.has(p_signal)) {
  641. return true;
  642. }
  643. if (p_no_inheritance) {
  644. return false;
  645. }
  646. check = check->inherits_ptr;
  647. }
  648. return false;
  649. }
  650. bool ClassDB::get_signal(const StringName &p_class, const StringName &p_signal, MethodInfo *r_signal) {
  651. OBJTYPE_RLOCK;
  652. ClassInfo *type = classes.getptr(p_class);
  653. ClassInfo *check = type;
  654. while (check) {
  655. if (check->signal_map.has(p_signal)) {
  656. if (r_signal) {
  657. *r_signal = check->signal_map[p_signal];
  658. }
  659. return true;
  660. }
  661. check = check->inherits_ptr;
  662. }
  663. return false;
  664. }
  665. void ClassDB::add_property_group(const StringName &p_class, const String &p_name, const String &p_prefix, int p_indent_depth) {
  666. OBJTYPE_WLOCK;
  667. ClassInfo *type = classes.getptr(p_class);
  668. ERR_FAIL_COND(!type);
  669. String prefix = p_prefix;
  670. if (p_indent_depth > 0) {
  671. prefix = vformat("%s,%d", p_prefix, p_indent_depth);
  672. }
  673. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, prefix, PROPERTY_USAGE_GROUP));
  674. }
  675. void ClassDB::add_property_subgroup(const StringName &p_class, const String &p_name, const String &p_prefix, int p_indent_depth) {
  676. OBJTYPE_WLOCK;
  677. ClassInfo *type = classes.getptr(p_class);
  678. ERR_FAIL_COND(!type);
  679. String prefix = p_prefix;
  680. if (p_indent_depth > 0) {
  681. prefix = vformat("%s,%d", p_prefix, p_indent_depth);
  682. }
  683. type->property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, prefix, PROPERTY_USAGE_SUBGROUP));
  684. }
  685. 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) {
  686. 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);
  687. }
  688. void ClassDB::add_property_array(const StringName &p_class, const StringName &p_path, const String &p_array_element_prefix) {
  689. OBJTYPE_WLOCK;
  690. ClassInfo *type = classes.getptr(p_class);
  691. ERR_FAIL_COND(!type);
  692. type->property_list.push_back(PropertyInfo(Variant::NIL, p_path, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY, p_array_element_prefix));
  693. }
  694. // NOTE: For implementation simplicity reasons, this method doesn't allow setters to have optional arguments at the end.
  695. void ClassDB::add_property(const StringName &p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index) {
  696. lock.read_lock();
  697. ClassInfo *type = classes.getptr(p_class);
  698. lock.read_unlock();
  699. ERR_FAIL_COND(!type);
  700. MethodBind *mb_set = nullptr;
  701. if (p_setter) {
  702. mb_set = get_method(p_class, p_setter);
  703. #ifdef DEBUG_METHODS_ENABLED
  704. ERR_FAIL_COND_MSG(!mb_set, "Invalid setter '" + p_class + "::" + p_setter + "' for property '" + p_pinfo.name + "'.");
  705. int exp_args = 1 + (p_index >= 0 ? 1 : 0);
  706. ERR_FAIL_COND_MSG(mb_set->get_argument_count() != exp_args, "Invalid function for setter '" + p_class + "::" + p_setter + " for property '" + p_pinfo.name + "'.");
  707. #endif
  708. }
  709. MethodBind *mb_get = nullptr;
  710. if (p_getter) {
  711. mb_get = get_method(p_class, p_getter);
  712. #ifdef DEBUG_METHODS_ENABLED
  713. ERR_FAIL_COND_MSG(!mb_get, "Invalid getter '" + p_class + "::" + p_getter + "' for property '" + p_pinfo.name + "'.");
  714. int exp_args = 0 + (p_index >= 0 ? 1 : 0);
  715. ERR_FAIL_COND_MSG(mb_get->get_argument_count() != exp_args, "Invalid function for getter '" + p_class + "::" + p_getter + "' for property: '" + p_pinfo.name + "'.");
  716. #endif
  717. }
  718. #ifdef DEBUG_METHODS_ENABLED
  719. ERR_FAIL_COND_MSG(type->property_setget.has(p_pinfo.name), "Object '" + p_class + "' already has property '" + p_pinfo.name + "'.");
  720. #endif
  721. OBJTYPE_WLOCK
  722. type->property_list.push_back(p_pinfo);
  723. type->property_map[p_pinfo.name] = p_pinfo;
  724. #ifdef DEBUG_METHODS_ENABLED
  725. if (mb_get) {
  726. type->methods_in_properties.insert(p_getter);
  727. }
  728. if (mb_set) {
  729. type->methods_in_properties.insert(p_setter);
  730. }
  731. #endif
  732. PropertySetGet psg;
  733. psg.setter = p_setter;
  734. psg.getter = p_getter;
  735. psg._setptr = mb_set;
  736. psg._getptr = mb_get;
  737. psg.index = p_index;
  738. psg.type = p_pinfo.type;
  739. type->property_setget[p_pinfo.name] = psg;
  740. }
  741. void ClassDB::set_property_default_value(const StringName &p_class, const StringName &p_name, const Variant &p_default) {
  742. if (!default_values.has(p_class)) {
  743. default_values[p_class] = HashMap<StringName, Variant>();
  744. }
  745. default_values[p_class][p_name] = p_default;
  746. }
  747. void ClassDB::add_linked_property(const StringName &p_class, const String &p_property, const String &p_linked_property) {
  748. #ifdef TOOLS_ENABLED
  749. OBJTYPE_WLOCK;
  750. ClassInfo *type = classes.getptr(p_class);
  751. ERR_FAIL_COND(!type);
  752. ERR_FAIL_COND(!type->property_map.has(p_property));
  753. ERR_FAIL_COND(!type->property_map.has(p_linked_property));
  754. if (!type->linked_properties.has(p_property)) {
  755. type->linked_properties.insert(p_property, List<StringName>());
  756. }
  757. type->linked_properties[p_property].push_back(p_linked_property);
  758. #endif
  759. }
  760. void ClassDB::get_property_list(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator) {
  761. OBJTYPE_RLOCK;
  762. ClassInfo *type = classes.getptr(p_class);
  763. ClassInfo *check = type;
  764. while (check) {
  765. for (const PropertyInfo &pi : check->property_list) {
  766. if (p_validator) {
  767. // Making a copy as we may modify it.
  768. PropertyInfo pi_mut = pi;
  769. p_validator->validate_property(pi_mut);
  770. p_list->push_back(pi_mut);
  771. } else {
  772. p_list->push_back(pi);
  773. }
  774. }
  775. if (p_no_inheritance) {
  776. return;
  777. }
  778. check = check->inherits_ptr;
  779. }
  780. }
  781. void ClassDB::get_linked_properties_info(const StringName &p_class, const StringName &p_property, List<StringName> *r_properties, bool p_no_inheritance) {
  782. #ifdef TOOLS_ENABLED
  783. ClassInfo *check = classes.getptr(p_class);
  784. while (check) {
  785. if (!check->linked_properties.has(p_property)) {
  786. return;
  787. }
  788. for (const StringName &E : check->linked_properties[p_property]) {
  789. r_properties->push_back(E);
  790. }
  791. if (p_no_inheritance) {
  792. break;
  793. }
  794. check = check->inherits_ptr;
  795. }
  796. #endif
  797. }
  798. bool ClassDB::get_property_info(const StringName &p_class, const StringName &p_property, PropertyInfo *r_info, bool p_no_inheritance, const Object *p_validator) {
  799. OBJTYPE_RLOCK;
  800. ClassInfo *check = classes.getptr(p_class);
  801. while (check) {
  802. if (check->property_map.has(p_property)) {
  803. PropertyInfo pinfo = check->property_map[p_property];
  804. if (p_validator) {
  805. p_validator->validate_property(pinfo);
  806. }
  807. if (r_info) {
  808. *r_info = pinfo;
  809. }
  810. return true;
  811. }
  812. if (p_no_inheritance) {
  813. break;
  814. }
  815. check = check->inherits_ptr;
  816. }
  817. return false;
  818. }
  819. bool ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid) {
  820. ERR_FAIL_NULL_V(p_object, false);
  821. ClassInfo *type = classes.getptr(p_object->get_class_name());
  822. ClassInfo *check = type;
  823. while (check) {
  824. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  825. if (psg) {
  826. if (!psg->setter) {
  827. if (r_valid) {
  828. *r_valid = false;
  829. }
  830. return true; //return true but do nothing
  831. }
  832. Callable::CallError ce;
  833. if (psg->index >= 0) {
  834. Variant index = psg->index;
  835. const Variant *arg[2] = { &index, &p_value };
  836. //p_object->call(psg->setter,arg,2,ce);
  837. if (psg->_setptr) {
  838. psg->_setptr->call(p_object, arg, 2, ce);
  839. } else {
  840. p_object->callp(psg->setter, arg, 2, ce);
  841. }
  842. } else {
  843. const Variant *arg[1] = { &p_value };
  844. if (psg->_setptr) {
  845. psg->_setptr->call(p_object, arg, 1, ce);
  846. } else {
  847. p_object->callp(psg->setter, arg, 1, ce);
  848. }
  849. }
  850. if (r_valid) {
  851. *r_valid = ce.error == Callable::CallError::CALL_OK;
  852. }
  853. return true;
  854. }
  855. check = check->inherits_ptr;
  856. }
  857. return false;
  858. }
  859. bool ClassDB::get_property(Object *p_object, const StringName &p_property, Variant &r_value) {
  860. ERR_FAIL_NULL_V(p_object, false);
  861. ClassInfo *type = classes.getptr(p_object->get_class_name());
  862. ClassInfo *check = type;
  863. while (check) {
  864. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  865. if (psg) {
  866. if (!psg->getter) {
  867. return true; //return true but do nothing
  868. }
  869. if (psg->index >= 0) {
  870. Variant index = psg->index;
  871. const Variant *arg[1] = { &index };
  872. Callable::CallError ce;
  873. r_value = p_object->callp(psg->getter, arg, 1, ce);
  874. } else {
  875. Callable::CallError ce;
  876. if (psg->_getptr) {
  877. r_value = psg->_getptr->call(p_object, nullptr, 0, ce);
  878. } else {
  879. r_value = p_object->callp(psg->getter, nullptr, 0, ce);
  880. }
  881. }
  882. return true;
  883. }
  884. const int64_t *c = check->constant_map.getptr(p_property); //constants count
  885. if (c) {
  886. r_value = *c;
  887. return true;
  888. }
  889. if (check->method_map.has(p_property)) { //methods count
  890. r_value = Callable(p_object, p_property);
  891. return true;
  892. }
  893. if (check->signal_map.has(p_property)) { //signals count
  894. r_value = Signal(p_object, p_property);
  895. return true;
  896. }
  897. check = check->inherits_ptr;
  898. }
  899. return false;
  900. }
  901. int ClassDB::get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  902. ClassInfo *type = classes.getptr(p_class);
  903. ClassInfo *check = type;
  904. while (check) {
  905. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  906. if (psg) {
  907. if (r_is_valid) {
  908. *r_is_valid = true;
  909. }
  910. return psg->index;
  911. }
  912. check = check->inherits_ptr;
  913. }
  914. if (r_is_valid) {
  915. *r_is_valid = false;
  916. }
  917. return -1;
  918. }
  919. Variant::Type ClassDB::get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid) {
  920. ClassInfo *type = classes.getptr(p_class);
  921. ClassInfo *check = type;
  922. while (check) {
  923. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  924. if (psg) {
  925. if (r_is_valid) {
  926. *r_is_valid = true;
  927. }
  928. return psg->type;
  929. }
  930. check = check->inherits_ptr;
  931. }
  932. if (r_is_valid) {
  933. *r_is_valid = false;
  934. }
  935. return Variant::NIL;
  936. }
  937. StringName ClassDB::get_property_setter(const StringName &p_class, const StringName &p_property) {
  938. ClassInfo *type = classes.getptr(p_class);
  939. ClassInfo *check = type;
  940. while (check) {
  941. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  942. if (psg) {
  943. return psg->setter;
  944. }
  945. check = check->inherits_ptr;
  946. }
  947. return StringName();
  948. }
  949. StringName ClassDB::get_property_getter(const StringName &p_class, const StringName &p_property) {
  950. ClassInfo *type = classes.getptr(p_class);
  951. ClassInfo *check = type;
  952. while (check) {
  953. const PropertySetGet *psg = check->property_setget.getptr(p_property);
  954. if (psg) {
  955. return psg->getter;
  956. }
  957. check = check->inherits_ptr;
  958. }
  959. return StringName();
  960. }
  961. bool ClassDB::has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance) {
  962. ClassInfo *type = classes.getptr(p_class);
  963. ClassInfo *check = type;
  964. while (check) {
  965. if (check->property_setget.has(p_property)) {
  966. return true;
  967. }
  968. if (p_no_inheritance) {
  969. break;
  970. }
  971. check = check->inherits_ptr;
  972. }
  973. return false;
  974. }
  975. void ClassDB::set_method_flags(const StringName &p_class, const StringName &p_method, int p_flags) {
  976. OBJTYPE_WLOCK;
  977. ClassInfo *type = classes.getptr(p_class);
  978. ClassInfo *check = type;
  979. ERR_FAIL_COND(!check);
  980. ERR_FAIL_COND(!check->method_map.has(p_method));
  981. check->method_map[p_method]->set_hint_flags(p_flags);
  982. }
  983. bool ClassDB::has_method(const StringName &p_class, const StringName &p_method, bool p_no_inheritance) {
  984. ClassInfo *type = classes.getptr(p_class);
  985. ClassInfo *check = type;
  986. while (check) {
  987. if (check->method_map.has(p_method)) {
  988. return true;
  989. }
  990. if (p_no_inheritance) {
  991. return false;
  992. }
  993. check = check->inherits_ptr;
  994. }
  995. return false;
  996. }
  997. void ClassDB::bind_method_custom(const StringName &p_class, MethodBind *p_method) {
  998. ClassInfo *type = classes.getptr(p_class);
  999. if (!type) {
  1000. ERR_FAIL_MSG("Couldn't bind custom method '" + p_method->get_name() + "' for instance '" + p_class + "'.");
  1001. }
  1002. if (type->method_map.has(p_method->get_name())) {
  1003. // overloading not supported
  1004. ERR_FAIL_MSG("Method already bound '" + p_class + "::" + p_method->get_name() + "'.");
  1005. }
  1006. #ifdef DEBUG_METHODS_ENABLED
  1007. type->method_order.push_back(p_method->get_name());
  1008. #endif
  1009. type->method_map[p_method->get_name()] = p_method;
  1010. }
  1011. #ifdef DEBUG_METHODS_ENABLED
  1012. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount) {
  1013. StringName mdname = method_name.name;
  1014. #else
  1015. MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const char *method_name, const Variant **p_defs, int p_defcount) {
  1016. StringName mdname = StaticCString::create(method_name);
  1017. #endif
  1018. OBJTYPE_WLOCK;
  1019. ERR_FAIL_COND_V(!p_bind, nullptr);
  1020. p_bind->set_name(mdname);
  1021. String instance_type = p_bind->get_instance_class();
  1022. #ifdef DEBUG_ENABLED
  1023. ERR_FAIL_COND_V_MSG(has_method(instance_type, mdname), nullptr, "Class " + String(instance_type) + " already has a method " + String(mdname) + ".");
  1024. #endif
  1025. ClassInfo *type = classes.getptr(instance_type);
  1026. if (!type) {
  1027. memdelete(p_bind);
  1028. ERR_FAIL_V_MSG(nullptr, "Couldn't bind method '" + mdname + "' for instance '" + instance_type + "'.");
  1029. }
  1030. if (type->method_map.has(mdname)) {
  1031. memdelete(p_bind);
  1032. // overloading not supported
  1033. ERR_FAIL_V_MSG(nullptr, "Method already bound '" + instance_type + "::" + mdname + "'.");
  1034. }
  1035. #ifdef DEBUG_METHODS_ENABLED
  1036. if (method_name.args.size() > p_bind->get_argument_count()) {
  1037. memdelete(p_bind);
  1038. ERR_FAIL_V_MSG(nullptr, "Method definition provides more arguments than the method actually has '" + instance_type + "::" + mdname + "'.");
  1039. }
  1040. p_bind->set_argument_names(method_name.args);
  1041. type->method_order.push_back(mdname);
  1042. #endif
  1043. type->method_map[mdname] = p_bind;
  1044. Vector<Variant> defvals;
  1045. defvals.resize(p_defcount);
  1046. for (int i = 0; i < p_defcount; i++) {
  1047. defvals.write[i] = *p_defs[i];
  1048. }
  1049. p_bind->set_default_arguments(defvals);
  1050. p_bind->set_hint_flags(p_flags);
  1051. return p_bind;
  1052. }
  1053. 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) {
  1054. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1055. OBJTYPE_WLOCK;
  1056. #ifdef DEBUG_METHODS_ENABLED
  1057. MethodInfo mi = p_method;
  1058. if (p_virtual) {
  1059. mi.flags |= METHOD_FLAG_VIRTUAL;
  1060. }
  1061. if (p_object_core) {
  1062. mi.flags |= METHOD_FLAG_OBJECT_CORE;
  1063. }
  1064. if (!p_object_core) {
  1065. if (p_arg_names.size() != mi.arguments.size()) {
  1066. WARN_PRINT("Mismatch argument name count for virtual method: " + String(p_class) + "::" + p_method.name);
  1067. } else {
  1068. for (int i = 0; i < p_arg_names.size(); i++) {
  1069. mi.arguments[i].name = p_arg_names[i];
  1070. }
  1071. }
  1072. }
  1073. if (classes[p_class].virtual_methods_map.has(p_method.name)) {
  1074. // overloading not supported
  1075. ERR_FAIL_MSG("Virtual method already bound '" + String(p_class) + "::" + p_method.name + "'.");
  1076. }
  1077. classes[p_class].virtual_methods.push_back(mi);
  1078. classes[p_class].virtual_methods_map[p_method.name] = mi;
  1079. #endif
  1080. }
  1081. void ClassDB::get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance) {
  1082. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1083. #ifdef DEBUG_METHODS_ENABLED
  1084. ClassInfo *type = classes.getptr(p_class);
  1085. ClassInfo *check = type;
  1086. while (check) {
  1087. for (const MethodInfo &E : check->virtual_methods) {
  1088. p_methods->push_back(E);
  1089. }
  1090. if (p_no_inheritance) {
  1091. return;
  1092. }
  1093. check = check->inherits_ptr;
  1094. }
  1095. #endif
  1096. }
  1097. void ClassDB::set_class_enabled(const StringName &p_class, bool p_enable) {
  1098. OBJTYPE_WLOCK;
  1099. ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'.");
  1100. classes[p_class].disabled = !p_enable;
  1101. }
  1102. bool ClassDB::is_class_enabled(const StringName &p_class) {
  1103. OBJTYPE_RLOCK;
  1104. ClassInfo *ti = classes.getptr(p_class);
  1105. if (!ti || !ti->creation_func) {
  1106. if (compat_classes.has(p_class)) {
  1107. ti = classes.getptr(compat_classes[p_class]);
  1108. }
  1109. }
  1110. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  1111. return !ti->disabled;
  1112. }
  1113. bool ClassDB::is_class_exposed(const StringName &p_class) {
  1114. OBJTYPE_RLOCK;
  1115. ClassInfo *ti = classes.getptr(p_class);
  1116. ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'.");
  1117. return ti->exposed;
  1118. }
  1119. void ClassDB::add_resource_base_extension(const StringName &p_extension, const StringName &p_class) {
  1120. if (resource_base_extensions.has(p_extension)) {
  1121. return;
  1122. }
  1123. resource_base_extensions[p_extension] = p_class;
  1124. }
  1125. void ClassDB::get_resource_base_extensions(List<String> *p_extensions) {
  1126. for (const KeyValue<StringName, StringName> &E : resource_base_extensions) {
  1127. p_extensions->push_back(E.key);
  1128. }
  1129. }
  1130. bool ClassDB::is_resource_extension(const StringName &p_extension) {
  1131. return resource_base_extensions.has(p_extension);
  1132. }
  1133. void ClassDB::get_extensions_for_type(const StringName &p_class, List<String> *p_extensions) {
  1134. for (const KeyValue<StringName, StringName> &E : resource_base_extensions) {
  1135. if (is_parent_class(p_class, E.value) || is_parent_class(E.value, p_class)) {
  1136. p_extensions->push_back(E.key);
  1137. }
  1138. }
  1139. }
  1140. HashMap<StringName, HashMap<StringName, Variant>> ClassDB::default_values;
  1141. HashSet<StringName> ClassDB::default_values_cached;
  1142. Variant ClassDB::class_get_default_property_value(const StringName &p_class, const StringName &p_property, bool *r_valid) {
  1143. if (!default_values_cached.has(p_class)) {
  1144. if (!default_values.has(p_class)) {
  1145. default_values[p_class] = HashMap<StringName, Variant>();
  1146. }
  1147. Object *c = nullptr;
  1148. bool cleanup_c = false;
  1149. if (Engine::get_singleton()->has_singleton(p_class)) {
  1150. c = Engine::get_singleton()->get_singleton_object(p_class);
  1151. cleanup_c = false;
  1152. } else if (ClassDB::can_instantiate(p_class) && !ClassDB::is_virtual(p_class)) {
  1153. c = ClassDB::instantiate(p_class);
  1154. cleanup_c = true;
  1155. }
  1156. if (c) {
  1157. List<PropertyInfo> plist;
  1158. c->get_property_list(&plist);
  1159. for (const PropertyInfo &E : plist) {
  1160. if (E.usage & (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR)) {
  1161. if (!default_values[p_class].has(E.name)) {
  1162. Variant v = c->get(E.name);
  1163. default_values[p_class][E.name] = v;
  1164. }
  1165. }
  1166. }
  1167. if (cleanup_c) {
  1168. memdelete(c);
  1169. }
  1170. }
  1171. default_values_cached.insert(p_class);
  1172. }
  1173. if (!default_values.has(p_class)) {
  1174. if (r_valid != nullptr) {
  1175. *r_valid = false;
  1176. }
  1177. return Variant();
  1178. }
  1179. if (!default_values[p_class].has(p_property)) {
  1180. if (r_valid != nullptr) {
  1181. *r_valid = false;
  1182. }
  1183. return Variant();
  1184. }
  1185. if (r_valid != nullptr) {
  1186. *r_valid = true;
  1187. }
  1188. Variant var = default_values[p_class][p_property];
  1189. #ifdef DEBUG_ENABLED
  1190. // Some properties may have an instantiated Object as default value,
  1191. // (like Path2D's `curve` used to have), but that's not a good practice.
  1192. // Instead, those properties should use PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT
  1193. // to be auto-instantiated when created in the editor with the following method:
  1194. // EditorNode::get_editor_data().instantiate_object_properties(obj);
  1195. if (var.get_type() == Variant::OBJECT) {
  1196. Object *obj = var.get_validated_object();
  1197. if (obj) {
  1198. WARN_PRINT(vformat("Instantiated %s used as default value for %s's \"%s\" property.", obj->get_class(), p_class, p_property));
  1199. }
  1200. }
  1201. #endif
  1202. return var;
  1203. }
  1204. void ClassDB::register_extension_class(ObjectNativeExtension *p_extension) {
  1205. GLOBAL_LOCK_FUNCTION;
  1206. ERR_FAIL_COND_MSG(classes.has(p_extension->class_name), "Class already registered: " + String(p_extension->class_name));
  1207. 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));
  1208. ClassInfo *parent = classes.getptr(p_extension->parent_class_name);
  1209. ClassInfo c;
  1210. c.api = p_extension->editor_class ? API_EDITOR_EXTENSION : API_EXTENSION;
  1211. c.native_extension = p_extension;
  1212. c.name = p_extension->class_name;
  1213. c.creation_func = parent->creation_func;
  1214. c.inherits = parent->name;
  1215. c.class_ptr = parent->class_ptr;
  1216. c.inherits_ptr = parent;
  1217. c.exposed = true;
  1218. classes[p_extension->class_name] = c;
  1219. }
  1220. void ClassDB::unregister_extension_class(const StringName &p_class) {
  1221. ERR_FAIL_COND(!classes.has(p_class));
  1222. classes.erase(p_class);
  1223. }
  1224. HashMap<StringName, ClassDB::NativeStruct> ClassDB::native_structs;
  1225. void ClassDB::register_native_struct(const StringName &p_name, const String &p_code, uint64_t p_current_size) {
  1226. NativeStruct ns;
  1227. ns.ccode = p_code;
  1228. ns.struct_size = p_current_size;
  1229. native_structs[p_name] = ns;
  1230. }
  1231. void ClassDB::get_native_struct_list(List<StringName> *r_names) {
  1232. for (const KeyValue<StringName, NativeStruct> &E : native_structs) {
  1233. r_names->push_back(E.key);
  1234. }
  1235. }
  1236. String ClassDB::get_native_struct_code(const StringName &p_name) {
  1237. ERR_FAIL_COND_V(!native_structs.has(p_name), String());
  1238. return native_structs[p_name].ccode;
  1239. }
  1240. uint64_t ClassDB::get_native_struct_size(const StringName &p_name) {
  1241. ERR_FAIL_COND_V(!native_structs.has(p_name), 0);
  1242. return native_structs[p_name].struct_size;
  1243. }
  1244. RWLock ClassDB::lock;
  1245. void ClassDB::cleanup_defaults() {
  1246. default_values.clear();
  1247. default_values_cached.clear();
  1248. }
  1249. void ClassDB::cleanup() {
  1250. //OBJTYPE_LOCK; hah not here
  1251. for (KeyValue<StringName, ClassInfo> &E : classes) {
  1252. ClassInfo &ti = E.value;
  1253. for (KeyValue<StringName, MethodBind *> &F : ti.method_map) {
  1254. memdelete(F.value);
  1255. }
  1256. }
  1257. classes.clear();
  1258. resource_base_extensions.clear();
  1259. compat_classes.clear();
  1260. native_structs.clear();
  1261. }
  1262. //