object.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431
  1. /**************************************************************************/
  2. /* object.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 "object.h"
  31. #include "core/extension/gdextension_manager.h"
  32. #include "core/io/resource.h"
  33. #include "core/object/class_db.h"
  34. #include "core/object/message_queue.h"
  35. #include "core/object/script_language.h"
  36. #include "core/os/os.h"
  37. #include "core/string/print_string.h"
  38. #include "core/string/translation_server.h"
  39. #include "core/variant/typed_array.h"
  40. #ifdef DEBUG_ENABLED
  41. struct _ObjectDebugLock {
  42. ObjectID obj_id;
  43. _ObjectDebugLock(Object *p_obj) {
  44. obj_id = p_obj->get_instance_id();
  45. p_obj->_lock_index.ref();
  46. }
  47. ~_ObjectDebugLock() {
  48. Object *obj_ptr = ObjectDB::get_instance(obj_id);
  49. if (likely(obj_ptr)) {
  50. obj_ptr->_lock_index.unref();
  51. }
  52. }
  53. };
  54. #define OBJ_DEBUG_LOCK _ObjectDebugLock _debug_lock(this);
  55. #else
  56. #define OBJ_DEBUG_LOCK
  57. #endif
  58. PropertyInfo::operator Dictionary() const {
  59. Dictionary d;
  60. d["name"] = name;
  61. d["class_name"] = class_name;
  62. d["type"] = type;
  63. d["hint"] = hint;
  64. d["hint_string"] = hint_string;
  65. d["usage"] = usage;
  66. return d;
  67. }
  68. PropertyInfo PropertyInfo::from_dict(const Dictionary &p_dict) {
  69. PropertyInfo pi;
  70. if (p_dict.has("type")) {
  71. pi.type = Variant::Type(int(p_dict["type"]));
  72. }
  73. if (p_dict.has("name")) {
  74. pi.name = p_dict["name"];
  75. }
  76. if (p_dict.has("class_name")) {
  77. pi.class_name = p_dict["class_name"];
  78. }
  79. if (p_dict.has("hint")) {
  80. pi.hint = PropertyHint(int(p_dict["hint"]));
  81. }
  82. if (p_dict.has("hint_string")) {
  83. pi.hint_string = p_dict["hint_string"];
  84. }
  85. if (p_dict.has("usage")) {
  86. pi.usage = p_dict["usage"];
  87. }
  88. return pi;
  89. }
  90. TypedArray<Dictionary> convert_property_list(const List<PropertyInfo> *p_list) {
  91. TypedArray<Dictionary> va;
  92. for (const List<PropertyInfo>::Element *E = p_list->front(); E; E = E->next()) {
  93. va.push_back(Dictionary(E->get()));
  94. }
  95. return va;
  96. }
  97. TypedArray<Dictionary> convert_property_list(const Vector<PropertyInfo> &p_vector) {
  98. TypedArray<Dictionary> va;
  99. for (const PropertyInfo &E : p_vector) {
  100. va.push_back(Dictionary(E));
  101. }
  102. return va;
  103. }
  104. MethodInfo::operator Dictionary() const {
  105. Dictionary d;
  106. d["name"] = name;
  107. d["args"] = convert_property_list(arguments);
  108. Array da;
  109. for (int i = 0; i < default_arguments.size(); i++) {
  110. da.push_back(default_arguments[i]);
  111. }
  112. d["default_args"] = da;
  113. d["flags"] = flags;
  114. d["id"] = id;
  115. Dictionary r = return_val;
  116. d["return"] = r;
  117. return d;
  118. }
  119. MethodInfo MethodInfo::from_dict(const Dictionary &p_dict) {
  120. MethodInfo mi;
  121. if (p_dict.has("name")) {
  122. mi.name = p_dict["name"];
  123. }
  124. Array args;
  125. if (p_dict.has("args")) {
  126. args = p_dict["args"];
  127. }
  128. for (const Variant &arg : args) {
  129. Dictionary d = arg;
  130. mi.arguments.push_back(PropertyInfo::from_dict(d));
  131. }
  132. Array defargs;
  133. if (p_dict.has("default_args")) {
  134. defargs = p_dict["default_args"];
  135. }
  136. for (const Variant &defarg : defargs) {
  137. mi.default_arguments.push_back(defarg);
  138. }
  139. if (p_dict.has("return")) {
  140. mi.return_val = PropertyInfo::from_dict(p_dict["return"]);
  141. }
  142. if (p_dict.has("flags")) {
  143. mi.flags = p_dict["flags"];
  144. }
  145. return mi;
  146. }
  147. uint32_t MethodInfo::get_compatibility_hash() const {
  148. bool has_return = (return_val.type != Variant::NIL) || (return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);
  149. uint32_t hash = hash_murmur3_one_32(has_return);
  150. hash = hash_murmur3_one_32(arguments.size(), hash);
  151. if (has_return) {
  152. hash = hash_murmur3_one_32(return_val.type, hash);
  153. if (return_val.class_name != StringName()) {
  154. hash = hash_murmur3_one_32(return_val.class_name.hash(), hash);
  155. }
  156. }
  157. for (const PropertyInfo &arg : arguments) {
  158. hash = hash_murmur3_one_32(arg.type, hash);
  159. if (arg.class_name != StringName()) {
  160. hash = hash_murmur3_one_32(arg.class_name.hash(), hash);
  161. }
  162. }
  163. hash = hash_murmur3_one_32(default_arguments.size(), hash);
  164. for (const Variant &v : default_arguments) {
  165. hash = hash_murmur3_one_32(v.hash(), hash);
  166. }
  167. hash = hash_murmur3_one_32(flags & METHOD_FLAG_CONST ? 1 : 0, hash);
  168. hash = hash_murmur3_one_32(flags & METHOD_FLAG_VARARG ? 1 : 0, hash);
  169. return hash_fmix32(hash);
  170. }
  171. Object::Connection::operator Variant() const {
  172. Dictionary d;
  173. d["signal"] = signal;
  174. d["callable"] = callable;
  175. d["flags"] = flags;
  176. return d;
  177. }
  178. bool Object::Connection::operator<(const Connection &p_conn) const {
  179. if (signal == p_conn.signal) {
  180. return callable < p_conn.callable;
  181. } else {
  182. return signal < p_conn.signal;
  183. }
  184. }
  185. Object::Connection::Connection(const Variant &p_variant) {
  186. Dictionary d = p_variant;
  187. if (d.has("signal")) {
  188. signal = d["signal"];
  189. }
  190. if (d.has("callable")) {
  191. callable = d["callable"];
  192. }
  193. if (d.has("flags")) {
  194. flags = d["flags"];
  195. }
  196. }
  197. bool Object::_predelete() {
  198. _predelete_ok = 1;
  199. notification(NOTIFICATION_PREDELETE, true);
  200. if (_predelete_ok) {
  201. _class_name_ptr = nullptr; // Must restore, so constructors/destructors have proper class name access at each stage.
  202. notification(NOTIFICATION_PREDELETE_CLEANUP, true);
  203. }
  204. return _predelete_ok;
  205. }
  206. void Object::cancel_free() {
  207. _predelete_ok = false;
  208. }
  209. void Object::_initialize() {
  210. _class_name_ptr = _get_class_namev(); // Set the direct pointer, which is much faster to obtain, but can only happen after _initialize.
  211. _initialize_classv();
  212. _class_name_ptr = nullptr; // May have been called from a constructor.
  213. }
  214. void Object::_postinitialize() {
  215. notification(NOTIFICATION_POSTINITIALIZE);
  216. }
  217. void Object::set(const StringName &p_name, const Variant &p_value, bool *r_valid) {
  218. #ifdef TOOLS_ENABLED
  219. _edited = true;
  220. #endif
  221. if (script_instance) {
  222. if (script_instance->set(p_name, p_value)) {
  223. if (r_valid) {
  224. *r_valid = true;
  225. }
  226. return;
  227. }
  228. }
  229. if (_extension && _extension->set) {
  230. if (_extension->set(_extension_instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionConstVariantPtr)&p_value)) {
  231. if (r_valid) {
  232. *r_valid = true;
  233. }
  234. return;
  235. }
  236. }
  237. // Try built-in setter.
  238. {
  239. if (ClassDB::set_property(this, p_name, p_value, r_valid)) {
  240. return;
  241. }
  242. }
  243. if (p_name == CoreStringName(script)) {
  244. set_script(p_value);
  245. if (r_valid) {
  246. *r_valid = true;
  247. }
  248. return;
  249. } else {
  250. Variant **V = metadata_properties.getptr(p_name);
  251. if (V) {
  252. **V = p_value;
  253. if (r_valid) {
  254. *r_valid = true;
  255. }
  256. return;
  257. } else if (p_name.operator String().begins_with("metadata/")) {
  258. // Must exist, otherwise duplicate() will not work.
  259. set_meta(p_name.operator String().replace_first("metadata/", ""), p_value);
  260. if (r_valid) {
  261. *r_valid = true;
  262. }
  263. return;
  264. }
  265. }
  266. #ifdef TOOLS_ENABLED
  267. if (script_instance) {
  268. bool valid;
  269. script_instance->property_set_fallback(p_name, p_value, &valid);
  270. if (valid) {
  271. if (r_valid) {
  272. *r_valid = true;
  273. }
  274. return;
  275. }
  276. }
  277. #endif
  278. // Something inside the object... :|
  279. bool success = _setv(p_name, p_value);
  280. if (success) {
  281. if (r_valid) {
  282. *r_valid = true;
  283. }
  284. return;
  285. }
  286. if (r_valid) {
  287. *r_valid = false;
  288. }
  289. }
  290. Variant Object::get(const StringName &p_name, bool *r_valid) const {
  291. Variant ret;
  292. if (script_instance) {
  293. if (script_instance->get(p_name, ret)) {
  294. if (r_valid) {
  295. *r_valid = true;
  296. }
  297. return ret;
  298. }
  299. }
  300. if (_extension && _extension->get) {
  301. if (_extension->get(_extension_instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&ret)) {
  302. if (r_valid) {
  303. *r_valid = true;
  304. }
  305. return ret;
  306. }
  307. }
  308. // Try built-in getter.
  309. {
  310. if (ClassDB::get_property(const_cast<Object *>(this), p_name, ret)) {
  311. if (r_valid) {
  312. *r_valid = true;
  313. }
  314. return ret;
  315. }
  316. }
  317. if (p_name == CoreStringName(script)) {
  318. ret = get_script();
  319. if (r_valid) {
  320. *r_valid = true;
  321. }
  322. return ret;
  323. }
  324. const Variant *const *V = metadata_properties.getptr(p_name);
  325. if (V) {
  326. ret = **V;
  327. if (r_valid) {
  328. *r_valid = true;
  329. }
  330. return ret;
  331. } else {
  332. #ifdef TOOLS_ENABLED
  333. if (script_instance) {
  334. bool valid;
  335. ret = script_instance->property_get_fallback(p_name, &valid);
  336. if (valid) {
  337. if (r_valid) {
  338. *r_valid = true;
  339. }
  340. return ret;
  341. }
  342. }
  343. #endif
  344. // Something inside the object... :|
  345. bool success = _getv(p_name, ret);
  346. if (success) {
  347. if (r_valid) {
  348. *r_valid = true;
  349. }
  350. return ret;
  351. }
  352. if (r_valid) {
  353. *r_valid = false;
  354. }
  355. return Variant();
  356. }
  357. }
  358. void Object::set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid) {
  359. if (p_names.is_empty()) {
  360. if (r_valid) {
  361. *r_valid = false;
  362. }
  363. return;
  364. }
  365. if (p_names.size() == 1) {
  366. set(p_names[0], p_value, r_valid);
  367. return;
  368. }
  369. bool valid = false;
  370. if (!r_valid) {
  371. r_valid = &valid;
  372. }
  373. List<Variant> value_stack;
  374. value_stack.push_back(get(p_names[0], r_valid));
  375. if (!*r_valid) {
  376. value_stack.clear();
  377. return;
  378. }
  379. for (int i = 1; i < p_names.size() - 1; i++) {
  380. value_stack.push_back(value_stack.back()->get().get_named(p_names[i], valid));
  381. if (r_valid) {
  382. *r_valid = valid;
  383. }
  384. if (!valid) {
  385. value_stack.clear();
  386. return;
  387. }
  388. }
  389. value_stack.push_back(p_value); // p_names[p_names.size() - 1]
  390. for (int i = p_names.size() - 1; i > 0; i--) {
  391. value_stack.back()->prev()->get().set_named(p_names[i], value_stack.back()->get(), valid);
  392. value_stack.pop_back();
  393. if (r_valid) {
  394. *r_valid = valid;
  395. }
  396. if (!valid) {
  397. value_stack.clear();
  398. return;
  399. }
  400. }
  401. set(p_names[0], value_stack.back()->get(), r_valid);
  402. value_stack.pop_back();
  403. ERR_FAIL_COND(!value_stack.is_empty());
  404. }
  405. Variant Object::get_indexed(const Vector<StringName> &p_names, bool *r_valid) const {
  406. if (p_names.is_empty()) {
  407. if (r_valid) {
  408. *r_valid = false;
  409. }
  410. return Variant();
  411. }
  412. bool valid = false;
  413. Variant current_value = get(p_names[0], &valid);
  414. for (int i = 1; i < p_names.size(); i++) {
  415. current_value = current_value.get_named(p_names[i], valid);
  416. if (!valid) {
  417. break;
  418. }
  419. }
  420. if (r_valid) {
  421. *r_valid = valid;
  422. }
  423. return current_value;
  424. }
  425. void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) const {
  426. if (script_instance && p_reversed) {
  427. script_instance->get_property_list(p_list);
  428. }
  429. if (_extension) {
  430. const ObjectGDExtension *current_extension = _extension;
  431. while (current_extension) {
  432. p_list->push_back(PropertyInfo(Variant::NIL, current_extension->class_name, PROPERTY_HINT_NONE, current_extension->class_name, PROPERTY_USAGE_CATEGORY));
  433. ClassDB::get_property_list(current_extension->class_name, p_list, true, this);
  434. if (current_extension->get_property_list) {
  435. #ifdef TOOLS_ENABLED
  436. // If this is a placeholder, we can't call into the GDExtension on the parent class,
  437. // because we don't have a real instance of the class to give it.
  438. if (likely(!_extension->is_placeholder)) {
  439. #endif
  440. uint32_t pcount;
  441. const GDExtensionPropertyInfo *pinfo = current_extension->get_property_list(_extension_instance, &pcount);
  442. for (uint32_t i = 0; i < pcount; i++) {
  443. p_list->push_back(PropertyInfo(pinfo[i]));
  444. }
  445. if (current_extension->free_property_list2) {
  446. current_extension->free_property_list2(_extension_instance, pinfo, pcount);
  447. }
  448. #ifndef DISABLE_DEPRECATED
  449. else if (current_extension->free_property_list) {
  450. current_extension->free_property_list(_extension_instance, pinfo);
  451. }
  452. #endif // DISABLE_DEPRECATED
  453. #ifdef TOOLS_ENABLED
  454. }
  455. #endif
  456. }
  457. current_extension = current_extension->parent;
  458. }
  459. }
  460. _get_property_listv(p_list, p_reversed);
  461. if (!is_class("Script")) { // can still be set, but this is for user-friendliness
  462. p_list->push_back(PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NEVER_DUPLICATE));
  463. }
  464. if (script_instance && !p_reversed) {
  465. script_instance->get_property_list(p_list);
  466. }
  467. for (const KeyValue<StringName, Variant> &K : metadata) {
  468. PropertyInfo pi = PropertyInfo(K.value.get_type(), "metadata/" + K.key.operator String());
  469. if (K.value.get_type() == Variant::OBJECT) {
  470. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  471. Object *obj = K.value;
  472. if (Object::cast_to<Script>(obj)) {
  473. pi.hint_string = "Script";
  474. pi.usage |= PROPERTY_USAGE_NEVER_DUPLICATE;
  475. } else {
  476. pi.hint_string = "Resource";
  477. }
  478. }
  479. p_list->push_back(pi);
  480. }
  481. }
  482. void Object::validate_property(PropertyInfo &p_property) const {
  483. _validate_propertyv(p_property);
  484. if (_extension && _extension->validate_property) {
  485. // GDExtension uses a StringName rather than a String for property name.
  486. StringName prop_name = p_property.name;
  487. GDExtensionPropertyInfo gdext_prop = {
  488. (GDExtensionVariantType)p_property.type,
  489. &prop_name,
  490. &p_property.class_name,
  491. (uint32_t)p_property.hint,
  492. &p_property.hint_string,
  493. p_property.usage,
  494. };
  495. if (_extension->validate_property(_extension_instance, &gdext_prop)) {
  496. p_property.type = (Variant::Type)gdext_prop.type;
  497. p_property.name = *reinterpret_cast<StringName *>(gdext_prop.name);
  498. p_property.class_name = *reinterpret_cast<StringName *>(gdext_prop.class_name);
  499. p_property.hint = (PropertyHint)gdext_prop.hint;
  500. p_property.hint_string = *reinterpret_cast<String *>(gdext_prop.hint_string);
  501. p_property.usage = gdext_prop.usage;
  502. };
  503. }
  504. if (script_instance) { // Call it last to allow user altering already validated properties.
  505. script_instance->validate_property(p_property);
  506. }
  507. }
  508. bool Object::property_can_revert(const StringName &p_name) const {
  509. if (script_instance) {
  510. if (script_instance->property_can_revert(p_name)) {
  511. return true;
  512. }
  513. }
  514. if (_extension && _extension->property_can_revert) {
  515. if (_extension->property_can_revert(_extension_instance, (GDExtensionConstStringNamePtr)&p_name)) {
  516. return true;
  517. }
  518. }
  519. return _property_can_revertv(p_name);
  520. }
  521. Variant Object::property_get_revert(const StringName &p_name) const {
  522. Variant ret;
  523. if (script_instance) {
  524. if (script_instance->property_get_revert(p_name, ret)) {
  525. return ret;
  526. }
  527. }
  528. if (_extension && _extension->property_get_revert) {
  529. if (_extension->property_get_revert(_extension_instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&ret)) {
  530. return ret;
  531. }
  532. }
  533. if (_property_get_revertv(p_name, ret)) {
  534. return ret;
  535. }
  536. return Variant();
  537. }
  538. void Object::get_method_list(List<MethodInfo> *p_list) const {
  539. ClassDB::get_method_list(get_class_name(), p_list);
  540. if (script_instance) {
  541. script_instance->get_method_list(p_list);
  542. }
  543. }
  544. Variant Object::_call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  545. if (p_argcount < 1) {
  546. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  547. r_error.expected = 1;
  548. return Variant();
  549. }
  550. if (!p_args[0]->is_string()) {
  551. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  552. r_error.argument = 0;
  553. r_error.expected = Variant::STRING_NAME;
  554. return Variant();
  555. }
  556. StringName method = *p_args[0];
  557. return callp(method, &p_args[1], p_argcount - 1, r_error);
  558. }
  559. Variant Object::_call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  560. if (p_argcount < 1) {
  561. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  562. r_error.expected = 1;
  563. return Variant();
  564. }
  565. if (!p_args[0]->is_string()) {
  566. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  567. r_error.argument = 0;
  568. r_error.expected = Variant::STRING_NAME;
  569. return Variant();
  570. }
  571. r_error.error = Callable::CallError::CALL_OK;
  572. StringName method = *p_args[0];
  573. MessageQueue::get_singleton()->push_callp(get_instance_id(), method, &p_args[1], p_argcount - 1, true);
  574. return Variant();
  575. }
  576. bool Object::has_method(const StringName &p_method) const {
  577. if (p_method == CoreStringName(free_)) {
  578. return true;
  579. }
  580. if (script_instance && script_instance->has_method(p_method)) {
  581. return true;
  582. }
  583. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  584. if (method != nullptr) {
  585. return true;
  586. }
  587. const Script *scr = Object::cast_to<Script>(this);
  588. if (scr != nullptr) {
  589. return scr->has_static_method(p_method);
  590. }
  591. return false;
  592. }
  593. int Object::_get_method_argument_count_bind(const StringName &p_method) const {
  594. return get_method_argument_count(p_method);
  595. }
  596. int Object::get_method_argument_count(const StringName &p_method, bool *r_is_valid) const {
  597. if (p_method == CoreStringName(free_)) {
  598. if (r_is_valid) {
  599. *r_is_valid = true;
  600. }
  601. return 0;
  602. }
  603. if (script_instance) {
  604. bool valid = false;
  605. int ret = script_instance->get_method_argument_count(p_method, &valid);
  606. if (valid) {
  607. if (r_is_valid) {
  608. *r_is_valid = true;
  609. }
  610. return ret;
  611. }
  612. }
  613. {
  614. bool valid = false;
  615. int ret = ClassDB::get_method_argument_count(get_class_name(), p_method, &valid);
  616. if (valid) {
  617. if (r_is_valid) {
  618. *r_is_valid = true;
  619. }
  620. return ret;
  621. }
  622. }
  623. const Script *scr = Object::cast_to<Script>(this);
  624. while (scr != nullptr) {
  625. bool valid = false;
  626. int ret = scr->get_script_method_argument_count(p_method, &valid);
  627. if (valid) {
  628. if (r_is_valid) {
  629. *r_is_valid = true;
  630. }
  631. return ret;
  632. }
  633. scr = scr->get_base_script().ptr();
  634. }
  635. if (r_is_valid) {
  636. *r_is_valid = false;
  637. }
  638. return 0;
  639. }
  640. Variant Object::getvar(const Variant &p_key, bool *r_valid) const {
  641. if (r_valid) {
  642. *r_valid = false;
  643. }
  644. if (p_key.is_string()) {
  645. return get(p_key, r_valid);
  646. }
  647. return Variant();
  648. }
  649. void Object::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) {
  650. if (r_valid) {
  651. *r_valid = false;
  652. }
  653. if (p_key.is_string()) {
  654. return set(p_key, p_value, r_valid);
  655. }
  656. }
  657. Variant Object::callv(const StringName &p_method, const Array &p_args) {
  658. const Variant **argptrs = nullptr;
  659. if (p_args.size() > 0) {
  660. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_args.size());
  661. for (int i = 0; i < p_args.size(); i++) {
  662. argptrs[i] = &p_args[i];
  663. }
  664. }
  665. Callable::CallError ce;
  666. const Variant ret = callp(p_method, argptrs, p_args.size(), ce);
  667. if (ce.error != Callable::CallError::CALL_OK) {
  668. ERR_FAIL_V_MSG(Variant(), vformat("Error calling method from 'callv': %s.", Variant::get_call_error_text(this, p_method, argptrs, p_args.size(), ce)));
  669. }
  670. return ret;
  671. }
  672. Variant Object::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  673. r_error.error = Callable::CallError::CALL_OK;
  674. if (p_method == CoreStringName(free_)) {
  675. //free must be here, before anything, always ready
  676. #ifdef DEBUG_ENABLED
  677. if (p_argcount != 0) {
  678. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  679. r_error.expected = 0;
  680. return Variant();
  681. }
  682. if (is_ref_counted()) {
  683. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  684. ERR_FAIL_V_MSG(Variant(), "Can't free a RefCounted object.");
  685. }
  686. if (_lock_index.get() > 1) {
  687. r_error.argument = 0;
  688. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  689. ERR_FAIL_V_MSG(Variant(), "Object is locked and can't be freed.");
  690. }
  691. #endif
  692. //must be here, must be before everything,
  693. memdelete(this);
  694. r_error.error = Callable::CallError::CALL_OK;
  695. return Variant();
  696. }
  697. Variant ret;
  698. OBJ_DEBUG_LOCK
  699. if (script_instance) {
  700. ret = script_instance->callp(p_method, p_args, p_argcount, r_error);
  701. // Force jump table.
  702. switch (r_error.error) {
  703. case Callable::CallError::CALL_OK:
  704. return ret;
  705. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  706. break;
  707. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  708. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  709. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  710. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  711. return ret;
  712. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL: {
  713. }
  714. }
  715. }
  716. //extension does not need this, because all methods are registered in MethodBind
  717. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  718. if (method) {
  719. ret = method->call(this, p_args, p_argcount, r_error);
  720. } else {
  721. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  722. }
  723. return ret;
  724. }
  725. Variant Object::call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  726. r_error.error = Callable::CallError::CALL_OK;
  727. if (p_method == CoreStringName(free_)) {
  728. // Free is not const, so fail.
  729. r_error.error = Callable::CallError::CALL_ERROR_METHOD_NOT_CONST;
  730. return Variant();
  731. }
  732. Variant ret;
  733. OBJ_DEBUG_LOCK
  734. if (script_instance) {
  735. ret = script_instance->call_const(p_method, p_args, p_argcount, r_error);
  736. //force jumptable
  737. switch (r_error.error) {
  738. case Callable::CallError::CALL_OK:
  739. return ret;
  740. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  741. break;
  742. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  743. break;
  744. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  745. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  746. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  747. return ret;
  748. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL: {
  749. }
  750. }
  751. }
  752. //extension does not need this, because all methods are registered in MethodBind
  753. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  754. if (method) {
  755. if (!method->is_const()) {
  756. r_error.error = Callable::CallError::CALL_ERROR_METHOD_NOT_CONST;
  757. return ret;
  758. }
  759. ret = method->call(this, p_args, p_argcount, r_error);
  760. } else {
  761. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  762. }
  763. return ret;
  764. }
  765. void Object::_notification_forward(int p_notification) {
  766. // Notify classes starting with Object and ending with most derived subclass.
  767. // e.g. Object -> Node -> Node3D
  768. _notification_forwardv(p_notification);
  769. if (_extension) {
  770. if (_extension->notification2) {
  771. _extension->notification2(_extension_instance, p_notification, static_cast<GDExtensionBool>(false));
  772. #ifndef DISABLE_DEPRECATED
  773. } else if (_extension->notification) {
  774. _extension->notification(_extension_instance, p_notification);
  775. #endif // DISABLE_DEPRECATED
  776. }
  777. }
  778. if (script_instance) {
  779. script_instance->notification(p_notification, false);
  780. }
  781. }
  782. void Object::_notification_backward(int p_notification) {
  783. if (script_instance) {
  784. script_instance->notification(p_notification, true);
  785. }
  786. if (_extension) {
  787. if (_extension->notification2) {
  788. _extension->notification2(_extension_instance, p_notification, static_cast<GDExtensionBool>(true));
  789. #ifndef DISABLE_DEPRECATED
  790. } else if (_extension->notification) {
  791. _extension->notification(_extension_instance, p_notification);
  792. #endif // DISABLE_DEPRECATED
  793. }
  794. }
  795. // Notify classes starting with most derived subclass and ending in Object.
  796. // e.g. Node3D -> Node -> Object
  797. _notification_backwardv(p_notification);
  798. }
  799. String Object::to_string() {
  800. // Keep this method in sync with `Node::to_string`.
  801. if (script_instance) {
  802. bool valid;
  803. String ret = script_instance->to_string(&valid);
  804. if (valid) {
  805. return ret;
  806. }
  807. }
  808. if (_extension && _extension->to_string) {
  809. String ret;
  810. GDExtensionBool is_valid;
  811. _extension->to_string(_extension_instance, &is_valid, &ret);
  812. return ret;
  813. }
  814. return "<" + get_class() + "#" + itos(get_instance_id()) + ">";
  815. }
  816. void Object::set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance) {
  817. //this function is not meant to be used in any of these ways
  818. ERR_FAIL_COND(p_script.is_null());
  819. ERR_FAIL_NULL(p_instance);
  820. ERR_FAIL_COND(script_instance != nullptr || !script.is_null());
  821. script = p_script;
  822. script_instance = p_instance;
  823. }
  824. void Object::set_script(const Variant &p_script) {
  825. if (script == p_script) {
  826. return;
  827. }
  828. Ref<Script> s = p_script;
  829. if (!p_script.is_null()) {
  830. ERR_FAIL_COND_MSG(s.is_null(), "Cannot set object script. Parameter should be null or a reference to a valid script.");
  831. ERR_FAIL_COND_MSG(s->is_abstract(), vformat("Cannot set object script. Script '%s' should not be abstract.", s->get_path()));
  832. }
  833. script = p_script;
  834. if (script_instance) {
  835. memdelete(script_instance);
  836. script_instance = nullptr;
  837. }
  838. if (s.is_valid()) {
  839. if (s->can_instantiate()) {
  840. OBJ_DEBUG_LOCK
  841. script_instance = s->instance_create(this);
  842. } else if (Engine::get_singleton()->is_editor_hint()) {
  843. OBJ_DEBUG_LOCK
  844. script_instance = s->placeholder_instance_create(this);
  845. }
  846. }
  847. notify_property_list_changed(); //scripts may add variables, so refresh is desired
  848. emit_signal(CoreStringName(script_changed));
  849. }
  850. void Object::set_script_instance(ScriptInstance *p_instance) {
  851. if (script_instance == p_instance) {
  852. return;
  853. }
  854. if (script_instance) {
  855. memdelete(script_instance);
  856. }
  857. script_instance = p_instance;
  858. if (p_instance) {
  859. script = p_instance->get_script();
  860. } else {
  861. script = Variant();
  862. }
  863. }
  864. Variant Object::get_script() const {
  865. return script;
  866. }
  867. bool Object::has_meta(const StringName &p_name) const {
  868. return metadata.has(p_name);
  869. }
  870. void Object::set_meta(const StringName &p_name, const Variant &p_value) {
  871. if (p_value.get_type() == Variant::NIL) {
  872. if (metadata.has(p_name)) {
  873. metadata.erase(p_name);
  874. const String &sname = p_name;
  875. metadata_properties.erase("metadata/" + sname);
  876. if (!sname.begins_with("_")) {
  877. // Metadata starting with _ don't show up in the inspector, so no need to update.
  878. notify_property_list_changed();
  879. }
  880. }
  881. return;
  882. }
  883. HashMap<StringName, Variant>::Iterator E = metadata.find(p_name);
  884. if (E) {
  885. E->value = p_value;
  886. } else {
  887. ERR_FAIL_COND_MSG(!p_name.operator String().is_valid_ascii_identifier(), vformat("Invalid metadata identifier: '%s'.", p_name));
  888. Variant *V = &metadata.insert(p_name, p_value)->value;
  889. const String &sname = p_name;
  890. metadata_properties["metadata/" + sname] = V;
  891. if (!sname.begins_with("_")) {
  892. notify_property_list_changed();
  893. }
  894. }
  895. }
  896. Variant Object::get_meta(const StringName &p_name, const Variant &p_default) const {
  897. if (!metadata.has(p_name)) {
  898. if (p_default != Variant()) {
  899. return p_default;
  900. } else {
  901. ERR_FAIL_V_MSG(Variant(), vformat("The object does not have any 'meta' values with the key '%s'.", p_name));
  902. }
  903. }
  904. return metadata[p_name];
  905. }
  906. void Object::remove_meta(const StringName &p_name) {
  907. set_meta(p_name, Variant());
  908. }
  909. void Object::merge_meta_from(const Object *p_src) {
  910. List<StringName> meta_keys;
  911. p_src->get_meta_list(&meta_keys);
  912. for (const StringName &key : meta_keys) {
  913. set_meta(key, p_src->get_meta(key));
  914. }
  915. }
  916. TypedArray<Dictionary> Object::_get_property_list_bind() const {
  917. List<PropertyInfo> lpi;
  918. get_property_list(&lpi);
  919. return convert_property_list(&lpi);
  920. }
  921. TypedArray<Dictionary> Object::_get_method_list_bind() const {
  922. List<MethodInfo> ml;
  923. get_method_list(&ml);
  924. TypedArray<Dictionary> ret;
  925. for (const MethodInfo &mi : ml) {
  926. Dictionary d = mi;
  927. //va.push_back(d);
  928. ret.push_back(d);
  929. }
  930. return ret;
  931. }
  932. TypedArray<StringName> Object::_get_meta_list_bind() const {
  933. TypedArray<StringName> _metaret;
  934. for (const KeyValue<StringName, Variant> &K : metadata) {
  935. _metaret.push_back(K.key);
  936. }
  937. return _metaret;
  938. }
  939. void Object::get_meta_list(List<StringName> *p_list) const {
  940. for (const KeyValue<StringName, Variant> &K : metadata) {
  941. p_list->push_back(K.key);
  942. }
  943. }
  944. void Object::add_user_signal(const MethodInfo &p_signal) {
  945. ERR_FAIL_COND_MSG(p_signal.name.is_empty(), "Signal name cannot be empty.");
  946. ERR_FAIL_COND_MSG(ClassDB::has_signal(get_class_name(), p_signal.name), vformat("User signal's name conflicts with a built-in signal of '%s'.", get_class_name()));
  947. ERR_FAIL_COND_MSG(signal_map.has(p_signal.name), vformat("Trying to add already existing signal '%s'.", p_signal.name));
  948. SignalData s;
  949. s.user = p_signal;
  950. signal_map[p_signal.name] = s;
  951. }
  952. bool Object::_has_user_signal(const StringName &p_name) const {
  953. if (!signal_map.has(p_name)) {
  954. return false;
  955. }
  956. return signal_map[p_name].user.name.length() > 0;
  957. }
  958. void Object::_remove_user_signal(const StringName &p_name) {
  959. SignalData *s = signal_map.getptr(p_name);
  960. ERR_FAIL_NULL_MSG(s, "Provided signal does not exist.");
  961. ERR_FAIL_COND_MSG(!s->removable, "Signal is not removable (not added with add_user_signal).");
  962. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  963. Object *target = slot_kv.key.get_object();
  964. if (likely(target)) {
  965. target->connections.erase(slot_kv.value.cE);
  966. }
  967. }
  968. signal_map.erase(p_name);
  969. }
  970. Error Object::_emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  971. if (unlikely(p_argcount < 1)) {
  972. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  973. r_error.expected = 1;
  974. ERR_FAIL_V(Error::ERR_INVALID_PARAMETER);
  975. }
  976. if (unlikely(!p_args[0]->is_string())) {
  977. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  978. r_error.argument = 0;
  979. r_error.expected = Variant::STRING_NAME;
  980. ERR_FAIL_V(Error::ERR_INVALID_PARAMETER);
  981. }
  982. r_error.error = Callable::CallError::CALL_OK;
  983. StringName signal = *p_args[0];
  984. const Variant **args = nullptr;
  985. int argc = p_argcount - 1;
  986. if (argc) {
  987. args = &p_args[1];
  988. }
  989. return emit_signalp(signal, args, argc);
  990. }
  991. Error Object::emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount) {
  992. if (_block_signals) {
  993. return ERR_CANT_ACQUIRE_RESOURCE; //no emit, signals blocked
  994. }
  995. SignalData *s = signal_map.getptr(p_name);
  996. if (!s) {
  997. #ifdef DEBUG_ENABLED
  998. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_name);
  999. //check in script
  1000. ERR_FAIL_COND_V_MSG(!signal_is_valid && !script.is_null() && !Ref<Script>(script)->has_script_signal(p_name), ERR_UNAVAILABLE, vformat("Can't emit non-existing signal \"%s\".", p_name));
  1001. #endif
  1002. //not connected? just return
  1003. return ERR_UNAVAILABLE;
  1004. }
  1005. // If this is a ref-counted object, prevent it from being destroyed during signal emission,
  1006. // which is needed in certain edge cases; e.g., https://github.com/godotengine/godot/issues/73889.
  1007. Ref<RefCounted> rc = Ref<RefCounted>(Object::cast_to<RefCounted>(this));
  1008. // Ensure that disconnecting the signal or even deleting the object
  1009. // will not affect the signal calling.
  1010. Callable *slot_callables = (Callable *)alloca(sizeof(Callable) * s->slot_map.size());
  1011. uint32_t *slot_flags = (uint32_t *)alloca(sizeof(uint32_t) * s->slot_map.size());
  1012. uint32_t slot_count = 0;
  1013. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1014. memnew_placement(&slot_callables[slot_count], Callable(slot_kv.value.conn.callable));
  1015. slot_flags[slot_count] = slot_kv.value.conn.flags;
  1016. ++slot_count;
  1017. }
  1018. DEV_ASSERT(slot_count == s->slot_map.size());
  1019. // Disconnect all one-shot connections before emitting to prevent recursion.
  1020. for (uint32_t i = 0; i < slot_count; ++i) {
  1021. bool disconnect = slot_flags[i] & CONNECT_ONE_SHOT;
  1022. #ifdef TOOLS_ENABLED
  1023. if (disconnect && (slot_flags[i] & CONNECT_PERSIST) && Engine::get_singleton()->is_editor_hint()) {
  1024. // This signal was connected from the editor, and is being edited. Just don't disconnect for now.
  1025. disconnect = false;
  1026. }
  1027. #endif
  1028. if (disconnect) {
  1029. _disconnect(p_name, slot_callables[i]);
  1030. }
  1031. }
  1032. OBJ_DEBUG_LOCK
  1033. Error err = OK;
  1034. for (uint32_t i = 0; i < slot_count; ++i) {
  1035. const Callable &callable = slot_callables[i];
  1036. const uint32_t &flags = slot_flags[i];
  1037. if (!callable.is_valid()) {
  1038. // Target might have been deleted during signal callback, this is expected and OK.
  1039. continue;
  1040. }
  1041. const Variant **args = p_args;
  1042. int argc = p_argcount;
  1043. if (flags & CONNECT_DEFERRED) {
  1044. MessageQueue::get_singleton()->push_callablep(callable, args, argc, true);
  1045. } else {
  1046. Callable::CallError ce;
  1047. _emitting = true;
  1048. Variant ret;
  1049. callable.callp(args, argc, ret, ce);
  1050. _emitting = false;
  1051. if (ce.error != Callable::CallError::CALL_OK) {
  1052. #ifdef DEBUG_ENABLED
  1053. if (flags & CONNECT_PERSIST && Engine::get_singleton()->is_editor_hint() && (script.is_null() || !Ref<Script>(script)->is_tool())) {
  1054. continue;
  1055. }
  1056. #endif
  1057. Object *target = callable.get_object();
  1058. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD && target && !ClassDB::class_exists(target->get_class_name())) {
  1059. //most likely object is not initialized yet, do not throw error.
  1060. } else {
  1061. ERR_PRINT(vformat("Error calling from signal '%s' to callable: %s.", String(p_name), Variant::get_callable_error_text(callable, args, argc, ce)));
  1062. err = ERR_METHOD_NOT_FOUND;
  1063. }
  1064. }
  1065. }
  1066. }
  1067. for (uint32_t i = 0; i < slot_count; ++i) {
  1068. slot_callables[i].~Callable();
  1069. }
  1070. return err;
  1071. }
  1072. void Object::_add_user_signal(const String &p_name, const Array &p_args) {
  1073. // this version of add_user_signal is meant to be used from scripts or external apis
  1074. // without access to ADD_SIGNAL in bind_methods
  1075. // added events are per instance, as opposed to the other ones, which are global
  1076. MethodInfo mi;
  1077. mi.name = p_name;
  1078. for (const Variant &arg : p_args) {
  1079. Dictionary d = arg;
  1080. PropertyInfo param;
  1081. if (d.has("name")) {
  1082. param.name = d["name"];
  1083. }
  1084. if (d.has("type")) {
  1085. param.type = (Variant::Type)(int)d["type"];
  1086. }
  1087. mi.arguments.push_back(param);
  1088. }
  1089. add_user_signal(mi);
  1090. if (signal_map.has(p_name)) {
  1091. signal_map.getptr(p_name)->removable = true;
  1092. }
  1093. }
  1094. TypedArray<Dictionary> Object::_get_signal_list() const {
  1095. List<MethodInfo> signal_list;
  1096. get_signal_list(&signal_list);
  1097. TypedArray<Dictionary> ret;
  1098. for (const MethodInfo &E : signal_list) {
  1099. ret.push_back(Dictionary(E));
  1100. }
  1101. return ret;
  1102. }
  1103. TypedArray<Dictionary> Object::_get_signal_connection_list(const StringName &p_signal) const {
  1104. List<Connection> conns;
  1105. get_all_signal_connections(&conns);
  1106. TypedArray<Dictionary> ret;
  1107. for (const Connection &c : conns) {
  1108. if (c.signal.get_name() == p_signal) {
  1109. ret.push_back(c);
  1110. }
  1111. }
  1112. return ret;
  1113. }
  1114. TypedArray<Dictionary> Object::_get_incoming_connections() const {
  1115. TypedArray<Dictionary> ret;
  1116. for (const Object::Connection &connection : connections) {
  1117. ret.push_back(connection);
  1118. }
  1119. return ret;
  1120. }
  1121. bool Object::has_signal(const StringName &p_name) const {
  1122. if (!script.is_null()) {
  1123. Ref<Script> scr = script;
  1124. if (scr.is_valid() && scr->has_script_signal(p_name)) {
  1125. return true;
  1126. }
  1127. }
  1128. if (ClassDB::has_signal(get_class_name(), p_name)) {
  1129. return true;
  1130. }
  1131. if (_has_user_signal(p_name)) {
  1132. return true;
  1133. }
  1134. return false;
  1135. }
  1136. void Object::get_signal_list(List<MethodInfo> *p_signals) const {
  1137. if (!script.is_null()) {
  1138. Ref<Script> scr = script;
  1139. if (scr.is_valid()) {
  1140. scr->get_script_signal_list(p_signals);
  1141. }
  1142. }
  1143. ClassDB::get_signal_list(get_class_name(), p_signals);
  1144. //find maybe usersignals?
  1145. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  1146. if (!E.value.user.name.is_empty()) {
  1147. //user signal
  1148. p_signals->push_back(E.value.user);
  1149. }
  1150. }
  1151. }
  1152. void Object::get_all_signal_connections(List<Connection> *p_connections) const {
  1153. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  1154. const SignalData *s = &E.value;
  1155. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1156. p_connections->push_back(slot_kv.value.conn);
  1157. }
  1158. }
  1159. }
  1160. void Object::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
  1161. const SignalData *s = signal_map.getptr(p_signal);
  1162. if (!s) {
  1163. return; //nothing
  1164. }
  1165. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1166. p_connections->push_back(slot_kv.value.conn);
  1167. }
  1168. }
  1169. int Object::get_persistent_signal_connection_count() const {
  1170. int count = 0;
  1171. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  1172. const SignalData *s = &E.value;
  1173. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1174. if (slot_kv.value.conn.flags & CONNECT_PERSIST) {
  1175. count += 1;
  1176. }
  1177. }
  1178. }
  1179. return count;
  1180. }
  1181. void Object::get_signals_connected_to_this(List<Connection> *p_connections) const {
  1182. for (const Connection &E : connections) {
  1183. p_connections->push_back(E);
  1184. }
  1185. }
  1186. Error Object::connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags) {
  1187. ERR_FAIL_COND_V_MSG(p_callable.is_null(), ERR_INVALID_PARAMETER, vformat("Cannot connect to '%s': the provided callable is null.", p_signal));
  1188. if (p_callable.is_standard()) {
  1189. // FIXME: This branch should probably removed in favor of the `is_valid()` branch, but there exist some classes
  1190. // that call `connect()` before they are fully registered with ClassDB. Until all such classes can be found
  1191. // and registered soon enough this branch is needed to allow `connect()` to succeed.
  1192. ERR_FAIL_NULL_V_MSG(p_callable.get_object(), ERR_INVALID_PARAMETER, vformat("Cannot connect to '%s' to callable '%s': the callable object is null.", p_signal, p_callable));
  1193. } else {
  1194. ERR_FAIL_COND_V_MSG(!p_callable.is_valid(), ERR_INVALID_PARAMETER, vformat("Cannot connect to '%s': the provided callable is not valid: '%s'.", p_signal, p_callable));
  1195. }
  1196. SignalData *s = signal_map.getptr(p_signal);
  1197. if (!s) {
  1198. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1199. //check in script
  1200. if (!signal_is_valid && !script.is_null()) {
  1201. if (Ref<Script>(script)->has_script_signal(p_signal)) {
  1202. signal_is_valid = true;
  1203. }
  1204. #ifdef TOOLS_ENABLED
  1205. else {
  1206. //allow connecting signals anyway if script is invalid, see issue #17070
  1207. if (!Ref<Script>(script)->is_valid()) {
  1208. signal_is_valid = true;
  1209. }
  1210. }
  1211. #endif
  1212. }
  1213. ERR_FAIL_COND_V_MSG(!signal_is_valid, ERR_INVALID_PARAMETER, vformat("In Object of type '%s': Attempt to connect nonexistent signal '%s' to callable '%s'.", String(get_class()), p_signal, p_callable));
  1214. signal_map[p_signal] = SignalData();
  1215. s = &signal_map[p_signal];
  1216. }
  1217. //compare with the base callable, so binds can be ignored
  1218. if (s->slot_map.has(*p_callable.get_base_comparator())) {
  1219. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1220. s->slot_map[*p_callable.get_base_comparator()].reference_count++;
  1221. return OK;
  1222. } else {
  1223. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, vformat("Signal '%s' is already connected to given callable '%s' in that object.", p_signal, p_callable));
  1224. }
  1225. }
  1226. Object *target_object = p_callable.get_object();
  1227. SignalData::Slot slot;
  1228. Connection conn;
  1229. conn.callable = p_callable;
  1230. conn.signal = ::Signal(this, p_signal);
  1231. conn.flags = p_flags;
  1232. slot.conn = conn;
  1233. if (target_object) {
  1234. slot.cE = target_object->connections.push_back(conn);
  1235. }
  1236. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1237. slot.reference_count = 1;
  1238. }
  1239. //use callable version as key, so binds can be ignored
  1240. s->slot_map[*p_callable.get_base_comparator()] = slot;
  1241. return OK;
  1242. }
  1243. bool Object::is_connected(const StringName &p_signal, const Callable &p_callable) const {
  1244. ERR_FAIL_COND_V_MSG(p_callable.is_null(), false, vformat("Cannot determine if connected to '%s': the provided callable is null.", p_signal)); // Should use `is_null`, see note in `connect` about the use of `is_valid`.
  1245. const SignalData *s = signal_map.getptr(p_signal);
  1246. if (!s) {
  1247. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1248. if (signal_is_valid) {
  1249. return false;
  1250. }
  1251. if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal)) {
  1252. return false;
  1253. }
  1254. ERR_FAIL_V_MSG(false, vformat("Nonexistent signal: '%s'.", p_signal));
  1255. }
  1256. return s->slot_map.has(*p_callable.get_base_comparator());
  1257. }
  1258. bool Object::has_connections(const StringName &p_signal) const {
  1259. const SignalData *s = signal_map.getptr(p_signal);
  1260. if (!s) {
  1261. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1262. if (signal_is_valid) {
  1263. return false;
  1264. }
  1265. if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal)) {
  1266. return false;
  1267. }
  1268. ERR_FAIL_V_MSG(false, vformat("Nonexistent signal: '%s'.", p_signal));
  1269. }
  1270. return !s->slot_map.is_empty();
  1271. }
  1272. void Object::disconnect(const StringName &p_signal, const Callable &p_callable) {
  1273. _disconnect(p_signal, p_callable);
  1274. }
  1275. bool Object::_disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force) {
  1276. ERR_FAIL_COND_V_MSG(p_callable.is_null(), false, vformat("Cannot disconnect from '%s': the provided callable is null.", p_signal)); // Should use `is_null`, see note in `connect` about the use of `is_valid`.
  1277. SignalData *s = signal_map.getptr(p_signal);
  1278. if (!s) {
  1279. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal) ||
  1280. (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal));
  1281. ERR_FAIL_COND_V_MSG(signal_is_valid, false, vformat("Attempt to disconnect a nonexistent connection from '%s'. Signal: '%s', callable: '%s'.", to_string(), p_signal, p_callable));
  1282. }
  1283. ERR_FAIL_NULL_V_MSG(s, false, vformat("Disconnecting nonexistent signal '%s' in '%s'.", p_signal, to_string()));
  1284. ERR_FAIL_COND_V_MSG(!s->slot_map.has(*p_callable.get_base_comparator()), false, vformat("Attempt to disconnect a nonexistent connection from '%s'. Signal: '%s', callable: '%s'.", to_string(), p_signal, p_callable));
  1285. SignalData::Slot *slot = &s->slot_map[*p_callable.get_base_comparator()];
  1286. if (!p_force) {
  1287. slot->reference_count--; // by default is zero, if it was not referenced it will go below it
  1288. if (slot->reference_count > 0) {
  1289. return false;
  1290. }
  1291. }
  1292. if (slot->cE) {
  1293. Object *target_object = p_callable.get_object();
  1294. if (target_object) {
  1295. target_object->connections.erase(slot->cE);
  1296. }
  1297. }
  1298. s->slot_map.erase(*p_callable.get_base_comparator());
  1299. if (s->slot_map.is_empty() && ClassDB::has_signal(get_class_name(), p_signal)) {
  1300. //not user signal, delete
  1301. signal_map.erase(p_signal);
  1302. }
  1303. return true;
  1304. }
  1305. void Object::_set_bind(const StringName &p_set, const Variant &p_value) {
  1306. set(p_set, p_value);
  1307. }
  1308. Variant Object::_get_bind(const StringName &p_name) const {
  1309. return get(p_name);
  1310. }
  1311. void Object::_set_indexed_bind(const NodePath &p_name, const Variant &p_value) {
  1312. set_indexed(p_name.get_as_property_path().get_subnames(), p_value);
  1313. }
  1314. Variant Object::_get_indexed_bind(const NodePath &p_name) const {
  1315. return get_indexed(p_name.get_as_property_path().get_subnames());
  1316. }
  1317. void Object::initialize_class() {
  1318. static bool initialized = false;
  1319. if (initialized) {
  1320. return;
  1321. }
  1322. ClassDB::_add_class<Object>();
  1323. _bind_methods();
  1324. _bind_compatibility_methods();
  1325. initialized = true;
  1326. }
  1327. StringName Object::get_translation_domain() const {
  1328. return _translation_domain;
  1329. }
  1330. void Object::set_translation_domain(const StringName &p_domain) {
  1331. _translation_domain = p_domain;
  1332. }
  1333. String Object::tr(const StringName &p_message, const StringName &p_context) const {
  1334. if (!_can_translate || !TranslationServer::get_singleton()) {
  1335. return p_message;
  1336. }
  1337. const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain(get_translation_domain());
  1338. return domain->translate(p_message, p_context);
  1339. }
  1340. String Object::tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  1341. if (!_can_translate || !TranslationServer::get_singleton()) {
  1342. // Return message based on English plural rule if translation is not possible.
  1343. if (p_n == 1) {
  1344. return p_message;
  1345. }
  1346. return p_message_plural;
  1347. }
  1348. const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain(get_translation_domain());
  1349. return domain->translate_plural(p_message, p_message_plural, p_n, p_context);
  1350. }
  1351. void Object::_clear_internal_resource_paths(const Variant &p_var) {
  1352. switch (p_var.get_type()) {
  1353. case Variant::OBJECT: {
  1354. Ref<Resource> r = p_var;
  1355. if (r.is_null()) {
  1356. return;
  1357. }
  1358. if (!r->is_built_in()) {
  1359. return; //not an internal resource
  1360. }
  1361. Object *object = p_var;
  1362. if (!object) {
  1363. return;
  1364. }
  1365. r->set_path("");
  1366. r->clear_internal_resource_paths();
  1367. } break;
  1368. case Variant::ARRAY: {
  1369. Array a = p_var;
  1370. for (const Variant &var : a) {
  1371. _clear_internal_resource_paths(var);
  1372. }
  1373. } break;
  1374. case Variant::DICTIONARY: {
  1375. Dictionary d = p_var;
  1376. for (const KeyValue<Variant, Variant> &kv : d) {
  1377. _clear_internal_resource_paths(kv.key);
  1378. _clear_internal_resource_paths(kv.value);
  1379. }
  1380. } break;
  1381. default: {
  1382. }
  1383. }
  1384. }
  1385. #ifdef TOOLS_ENABLED
  1386. void Object::editor_set_section_unfold(const String &p_section, bool p_unfolded, bool p_initializing) {
  1387. if (!p_initializing) {
  1388. set_edited(true);
  1389. }
  1390. if (p_unfolded) {
  1391. editor_section_folding.insert(p_section);
  1392. } else {
  1393. editor_section_folding.erase(p_section);
  1394. }
  1395. }
  1396. bool Object::editor_is_section_unfolded(const String &p_section) {
  1397. return editor_section_folding.has(p_section);
  1398. }
  1399. #endif
  1400. void Object::clear_internal_resource_paths() {
  1401. List<PropertyInfo> pinfo;
  1402. get_property_list(&pinfo);
  1403. for (const PropertyInfo &E : pinfo) {
  1404. _clear_internal_resource_paths(get(E.name));
  1405. }
  1406. }
  1407. void Object::notify_property_list_changed() {
  1408. emit_signal(CoreStringName(property_list_changed));
  1409. }
  1410. void Object::_bind_methods() {
  1411. ClassDB::bind_method(D_METHOD("get_class"), &Object::get_class);
  1412. ClassDB::bind_method(D_METHOD("is_class", "class"), &Object::is_class);
  1413. ClassDB::bind_method(D_METHOD("set", "property", "value"), &Object::_set_bind);
  1414. ClassDB::bind_method(D_METHOD("get", "property"), &Object::_get_bind);
  1415. ClassDB::bind_method(D_METHOD("set_indexed", "property_path", "value"), &Object::_set_indexed_bind);
  1416. ClassDB::bind_method(D_METHOD("get_indexed", "property_path"), &Object::_get_indexed_bind);
  1417. ClassDB::bind_method(D_METHOD("get_property_list"), &Object::_get_property_list_bind);
  1418. ClassDB::bind_method(D_METHOD("get_method_list"), &Object::_get_method_list_bind);
  1419. ClassDB::bind_method(D_METHOD("property_can_revert", "property"), &Object::property_can_revert);
  1420. ClassDB::bind_method(D_METHOD("property_get_revert", "property"), &Object::property_get_revert);
  1421. ClassDB::bind_method(D_METHOD("notification", "what", "reversed"), &Object::notification, DEFVAL(false));
  1422. ClassDB::bind_method(D_METHOD("to_string"), &Object::to_string);
  1423. ClassDB::bind_method(D_METHOD("get_instance_id"), &Object::get_instance_id);
  1424. ClassDB::bind_method(D_METHOD("set_script", "script"), &Object::set_script);
  1425. ClassDB::bind_method(D_METHOD("get_script"), &Object::get_script);
  1426. ClassDB::bind_method(D_METHOD("set_meta", "name", "value"), &Object::set_meta);
  1427. ClassDB::bind_method(D_METHOD("remove_meta", "name"), &Object::remove_meta);
  1428. ClassDB::bind_method(D_METHOD("get_meta", "name", "default"), &Object::get_meta, DEFVAL(Variant()));
  1429. ClassDB::bind_method(D_METHOD("has_meta", "name"), &Object::has_meta);
  1430. ClassDB::bind_method(D_METHOD("get_meta_list"), &Object::_get_meta_list_bind);
  1431. ClassDB::bind_method(D_METHOD("add_user_signal", "signal", "arguments"), &Object::_add_user_signal, DEFVAL(Array()));
  1432. ClassDB::bind_method(D_METHOD("has_user_signal", "signal"), &Object::_has_user_signal);
  1433. ClassDB::bind_method(D_METHOD("remove_user_signal", "signal"), &Object::_remove_user_signal);
  1434. {
  1435. MethodInfo mi;
  1436. mi.name = "emit_signal";
  1437. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "signal"));
  1438. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "emit_signal", &Object::_emit_signal, mi, varray(), false);
  1439. }
  1440. {
  1441. MethodInfo mi;
  1442. mi.name = "call";
  1443. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1444. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call", &Object::_call_bind, mi);
  1445. }
  1446. {
  1447. MethodInfo mi;
  1448. mi.name = "call_deferred";
  1449. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1450. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_deferred", &Object::_call_deferred_bind, mi, varray(), false);
  1451. }
  1452. ClassDB::bind_method(D_METHOD("set_deferred", "property", "value"), &Object::set_deferred);
  1453. ClassDB::bind_method(D_METHOD("callv", "method", "arg_array"), &Object::callv);
  1454. ClassDB::bind_method(D_METHOD("has_method", "method"), &Object::has_method);
  1455. ClassDB::bind_method(D_METHOD("get_method_argument_count", "method"), &Object::_get_method_argument_count_bind);
  1456. ClassDB::bind_method(D_METHOD("has_signal", "signal"), &Object::has_signal);
  1457. ClassDB::bind_method(D_METHOD("get_signal_list"), &Object::_get_signal_list);
  1458. ClassDB::bind_method(D_METHOD("get_signal_connection_list", "signal"), &Object::_get_signal_connection_list);
  1459. ClassDB::bind_method(D_METHOD("get_incoming_connections"), &Object::_get_incoming_connections);
  1460. ClassDB::bind_method(D_METHOD("connect", "signal", "callable", "flags"), &Object::connect, DEFVAL(0));
  1461. ClassDB::bind_method(D_METHOD("disconnect", "signal", "callable"), &Object::disconnect);
  1462. ClassDB::bind_method(D_METHOD("is_connected", "signal", "callable"), &Object::is_connected);
  1463. ClassDB::bind_method(D_METHOD("has_connections", "signal"), &Object::has_connections);
  1464. ClassDB::bind_method(D_METHOD("set_block_signals", "enable"), &Object::set_block_signals);
  1465. ClassDB::bind_method(D_METHOD("is_blocking_signals"), &Object::is_blocking_signals);
  1466. ClassDB::bind_method(D_METHOD("notify_property_list_changed"), &Object::notify_property_list_changed);
  1467. ClassDB::bind_method(D_METHOD("set_message_translation", "enable"), &Object::set_message_translation);
  1468. ClassDB::bind_method(D_METHOD("can_translate_messages"), &Object::can_translate_messages);
  1469. ClassDB::bind_method(D_METHOD("tr", "message", "context"), &Object::tr, DEFVAL(StringName()));
  1470. ClassDB::bind_method(D_METHOD("tr_n", "message", "plural_message", "n", "context"), &Object::tr_n, DEFVAL(StringName()));
  1471. ClassDB::bind_method(D_METHOD("get_translation_domain"), &Object::get_translation_domain);
  1472. ClassDB::bind_method(D_METHOD("set_translation_domain", "domain"), &Object::set_translation_domain);
  1473. ClassDB::bind_method(D_METHOD("is_queued_for_deletion"), &Object::is_queued_for_deletion);
  1474. ClassDB::bind_method(D_METHOD("cancel_free"), &Object::cancel_free);
  1475. ClassDB::add_virtual_method("Object", MethodInfo("free"), false);
  1476. ADD_SIGNAL(MethodInfo("script_changed"));
  1477. ADD_SIGNAL(MethodInfo("property_list_changed"));
  1478. #define BIND_OBJ_CORE_METHOD(m_method) \
  1479. ::ClassDB::add_virtual_method(get_class_static(), m_method, true, Vector<String>(), true);
  1480. BIND_OBJ_CORE_METHOD(MethodInfo("_init"));
  1481. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::STRING, "_to_string"));
  1482. {
  1483. MethodInfo mi("_notification");
  1484. mi.arguments.push_back(PropertyInfo(Variant::INT, "what"));
  1485. mi.arguments_metadata.push_back(GodotTypeInfo::Metadata::METADATA_INT_IS_INT32);
  1486. BIND_OBJ_CORE_METHOD(mi);
  1487. }
  1488. {
  1489. MethodInfo mi("_set");
  1490. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "property"));
  1491. mi.arguments.push_back(PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1492. mi.return_val.type = Variant::BOOL;
  1493. BIND_OBJ_CORE_METHOD(mi);
  1494. }
  1495. #ifdef TOOLS_ENABLED
  1496. {
  1497. MethodInfo mi("_get");
  1498. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "property"));
  1499. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1500. BIND_OBJ_CORE_METHOD(mi);
  1501. }
  1502. {
  1503. MethodInfo mi("_get_property_list");
  1504. mi.return_val.type = Variant::ARRAY;
  1505. mi.return_val.hint = PROPERTY_HINT_ARRAY_TYPE;
  1506. mi.return_val.hint_string = "Dictionary";
  1507. BIND_OBJ_CORE_METHOD(mi);
  1508. }
  1509. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::NIL, "_validate_property", PropertyInfo(Variant::DICTIONARY, "property")));
  1510. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_property_can_revert", PropertyInfo(Variant::STRING_NAME, "property")));
  1511. {
  1512. MethodInfo mi("_property_get_revert");
  1513. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "property"));
  1514. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1515. BIND_OBJ_CORE_METHOD(mi);
  1516. }
  1517. // These are actually `Variant` methods, but that doesn't matter since scripts can't inherit built-in types.
  1518. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_iter_init", PropertyInfo(Variant::ARRAY, "iter")));
  1519. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_iter_next", PropertyInfo(Variant::ARRAY, "iter")));
  1520. {
  1521. MethodInfo mi("_iter_get");
  1522. mi.arguments.push_back(PropertyInfo(Variant::NIL, "iter", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1523. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1524. BIND_OBJ_CORE_METHOD(mi);
  1525. }
  1526. #endif
  1527. BIND_CONSTANT(NOTIFICATION_POSTINITIALIZE);
  1528. BIND_CONSTANT(NOTIFICATION_PREDELETE);
  1529. BIND_CONSTANT(NOTIFICATION_EXTENSION_RELOADED);
  1530. BIND_ENUM_CONSTANT(CONNECT_DEFERRED);
  1531. BIND_ENUM_CONSTANT(CONNECT_PERSIST);
  1532. BIND_ENUM_CONSTANT(CONNECT_ONE_SHOT);
  1533. BIND_ENUM_CONSTANT(CONNECT_REFERENCE_COUNTED);
  1534. }
  1535. void Object::set_deferred(const StringName &p_property, const Variant &p_value) {
  1536. MessageQueue::get_singleton()->push_set(this, p_property, p_value);
  1537. }
  1538. void Object::set_block_signals(bool p_block) {
  1539. _block_signals = p_block;
  1540. }
  1541. bool Object::is_blocking_signals() const {
  1542. return _block_signals;
  1543. }
  1544. Variant::Type Object::get_static_property_type(const StringName &p_property, bool *r_valid) const {
  1545. bool valid;
  1546. Variant::Type t = ClassDB::get_property_type(get_class_name(), p_property, &valid);
  1547. if (valid) {
  1548. if (r_valid) {
  1549. *r_valid = true;
  1550. }
  1551. return t;
  1552. }
  1553. if (get_script_instance()) {
  1554. return get_script_instance()->get_property_type(p_property, r_valid);
  1555. }
  1556. if (r_valid) {
  1557. *r_valid = false;
  1558. }
  1559. return Variant::NIL;
  1560. }
  1561. Variant::Type Object::get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid) const {
  1562. if (p_path.is_empty()) {
  1563. if (r_valid) {
  1564. *r_valid = false;
  1565. }
  1566. return Variant::NIL;
  1567. }
  1568. bool valid = false;
  1569. Variant::Type t = get_static_property_type(p_path[0], &valid);
  1570. if (!valid) {
  1571. if (r_valid) {
  1572. *r_valid = false;
  1573. }
  1574. return Variant::NIL;
  1575. }
  1576. Callable::CallError ce;
  1577. Variant check;
  1578. Variant::construct(t, check, nullptr, 0, ce);
  1579. for (int i = 1; i < p_path.size(); i++) {
  1580. if (check.get_type() == Variant::OBJECT || check.get_type() == Variant::DICTIONARY || check.get_type() == Variant::ARRAY) {
  1581. // We cannot be sure about the type of properties this type can have
  1582. if (r_valid) {
  1583. *r_valid = false;
  1584. }
  1585. return Variant::NIL;
  1586. }
  1587. check = check.get_named(p_path[i], valid);
  1588. if (!valid) {
  1589. if (r_valid) {
  1590. *r_valid = false;
  1591. }
  1592. return Variant::NIL;
  1593. }
  1594. }
  1595. if (r_valid) {
  1596. *r_valid = true;
  1597. }
  1598. return check.get_type();
  1599. }
  1600. bool Object::is_queued_for_deletion() const {
  1601. return _is_queued_for_deletion;
  1602. }
  1603. #ifdef TOOLS_ENABLED
  1604. void Object::set_edited(bool p_edited) {
  1605. _edited = p_edited;
  1606. _edited_version++;
  1607. }
  1608. bool Object::is_edited() const {
  1609. return _edited;
  1610. }
  1611. uint32_t Object::get_edited_version() const {
  1612. return _edited_version;
  1613. }
  1614. #endif
  1615. StringName Object::get_class_name_for_extension(const GDExtension *p_library) const {
  1616. #ifdef TOOLS_ENABLED
  1617. // If this is the library this extension comes from and it's a placeholder, we
  1618. // have to return the closest native parent's class name, so that it doesn't try to
  1619. // use this like the real object.
  1620. if (unlikely(_extension && _extension->library == p_library && _extension->is_placeholder)) {
  1621. const StringName *class_name = _get_class_namev();
  1622. return *class_name;
  1623. }
  1624. #endif
  1625. // Only return the class name per the extension if it matches the given p_library.
  1626. if (_extension && _extension->library == p_library) {
  1627. return _extension->class_name;
  1628. }
  1629. // Extensions only have wrapper classes for classes exposed in ClassDB.
  1630. const StringName *class_name = _get_class_namev();
  1631. if (ClassDB::is_class_exposed(*class_name)) {
  1632. return *class_name;
  1633. }
  1634. // Find the nearest parent class that's exposed.
  1635. StringName parent_class = ClassDB::get_parent_class(*class_name);
  1636. while (parent_class != StringName()) {
  1637. if (ClassDB::is_class_exposed(parent_class)) {
  1638. return parent_class;
  1639. }
  1640. parent_class = ClassDB::get_parent_class(parent_class);
  1641. }
  1642. return SNAME("Object");
  1643. }
  1644. void Object::set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
  1645. // This is only meant to be used on creation by the binder, but we also
  1646. // need to account for reloading (where the 'binding' will be cleared).
  1647. ERR_FAIL_COND(_instance_bindings != nullptr && _instance_bindings[0].binding != nullptr);
  1648. if (_instance_bindings == nullptr) {
  1649. _instance_bindings = (InstanceBinding *)memalloc(sizeof(InstanceBinding));
  1650. _instance_binding_count = 1;
  1651. }
  1652. _instance_bindings[0].binding = p_binding;
  1653. _instance_bindings[0].free_callback = p_callbacks->free_callback;
  1654. _instance_bindings[0].reference_callback = p_callbacks->reference_callback;
  1655. _instance_bindings[0].token = p_token;
  1656. }
  1657. void *Object::get_instance_binding(void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
  1658. void *binding = nullptr;
  1659. MutexLock instance_binding_lock(_instance_binding_mutex);
  1660. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1661. if (_instance_bindings[i].token == p_token) {
  1662. binding = _instance_bindings[i].binding;
  1663. break;
  1664. }
  1665. }
  1666. if (unlikely(!binding && p_callbacks)) {
  1667. uint32_t current_size = next_power_of_2(_instance_binding_count);
  1668. uint32_t new_size = next_power_of_2(_instance_binding_count + 1);
  1669. if (current_size == 0 || new_size > current_size) {
  1670. _instance_bindings = (InstanceBinding *)memrealloc(_instance_bindings, new_size * sizeof(InstanceBinding));
  1671. }
  1672. _instance_bindings[_instance_binding_count].free_callback = p_callbacks->free_callback;
  1673. _instance_bindings[_instance_binding_count].reference_callback = p_callbacks->reference_callback;
  1674. _instance_bindings[_instance_binding_count].token = p_token;
  1675. binding = p_callbacks->create_callback(p_token, this);
  1676. _instance_bindings[_instance_binding_count].binding = binding;
  1677. #ifdef TOOLS_ENABLED
  1678. if (!_extension && Engine::get_singleton()->is_extension_reloading_enabled()) {
  1679. GDExtensionManager::get_singleton()->track_instance_binding(p_token, this);
  1680. }
  1681. #endif
  1682. _instance_binding_count++;
  1683. }
  1684. return binding;
  1685. }
  1686. bool Object::has_instance_binding(void *p_token) {
  1687. bool found = false;
  1688. MutexLock instance_binding_lock(_instance_binding_mutex);
  1689. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1690. if (_instance_bindings[i].token == p_token) {
  1691. found = true;
  1692. break;
  1693. }
  1694. }
  1695. return found;
  1696. }
  1697. void Object::free_instance_binding(void *p_token) {
  1698. bool found = false;
  1699. MutexLock instance_binding_lock(_instance_binding_mutex);
  1700. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1701. if (!found && _instance_bindings[i].token == p_token) {
  1702. if (_instance_bindings[i].free_callback) {
  1703. _instance_bindings[i].free_callback(_instance_bindings[i].token, this, _instance_bindings[i].binding);
  1704. }
  1705. found = true;
  1706. }
  1707. if (found) {
  1708. if (i + 1 < _instance_binding_count) {
  1709. _instance_bindings[i] = _instance_bindings[i + 1];
  1710. } else {
  1711. _instance_bindings[i] = { nullptr };
  1712. }
  1713. }
  1714. }
  1715. if (found) {
  1716. _instance_binding_count--;
  1717. }
  1718. }
  1719. #ifdef TOOLS_ENABLED
  1720. void Object::clear_internal_extension() {
  1721. ERR_FAIL_NULL(_extension);
  1722. // Free the instance inside the GDExtension.
  1723. if (_extension->free_instance) {
  1724. _extension->free_instance(_extension->class_userdata, _extension_instance);
  1725. }
  1726. _extension = nullptr;
  1727. _extension_instance = nullptr;
  1728. // Clear the instance bindings.
  1729. _instance_binding_mutex.lock();
  1730. if (_instance_bindings) {
  1731. if (_instance_bindings[0].free_callback) {
  1732. _instance_bindings[0].free_callback(_instance_bindings[0].token, this, _instance_bindings[0].binding);
  1733. }
  1734. _instance_bindings[0].binding = nullptr;
  1735. _instance_bindings[0].token = nullptr;
  1736. _instance_bindings[0].free_callback = nullptr;
  1737. _instance_bindings[0].reference_callback = nullptr;
  1738. }
  1739. _instance_binding_mutex.unlock();
  1740. // Clear the virtual methods.
  1741. while (virtual_method_list) {
  1742. (*virtual_method_list->method) = nullptr;
  1743. (*virtual_method_list->initialized) = false;
  1744. virtual_method_list = virtual_method_list->next;
  1745. }
  1746. }
  1747. void Object::reset_internal_extension(ObjectGDExtension *p_extension) {
  1748. ERR_FAIL_COND(_extension != nullptr);
  1749. if (p_extension) {
  1750. _extension_instance = p_extension->recreate_instance ? p_extension->recreate_instance(p_extension->class_userdata, (GDExtensionObjectPtr)this) : nullptr;
  1751. ERR_FAIL_NULL_MSG(_extension_instance, "Unable to recreate GDExtension instance - does this extension support hot reloading?");
  1752. _extension = p_extension;
  1753. }
  1754. }
  1755. #endif
  1756. void Object::_construct_object(bool p_reference) {
  1757. type_is_reference = p_reference;
  1758. _instance_id = ObjectDB::add_instance(this);
  1759. #ifdef DEBUG_ENABLED
  1760. _lock_index.init(1);
  1761. #endif
  1762. }
  1763. Object::Object(bool p_reference) {
  1764. _construct_object(p_reference);
  1765. }
  1766. Object::Object() {
  1767. _construct_object(false);
  1768. }
  1769. void Object::detach_from_objectdb() {
  1770. if (_instance_id != ObjectID()) {
  1771. ObjectDB::remove_instance(this);
  1772. _instance_id = ObjectID();
  1773. }
  1774. }
  1775. Object::~Object() {
  1776. if (script_instance) {
  1777. memdelete(script_instance);
  1778. }
  1779. script_instance = nullptr;
  1780. if (_extension) {
  1781. #ifdef TOOLS_ENABLED
  1782. if (_extension->untrack_instance) {
  1783. _extension->untrack_instance(_extension->tracking_userdata, this);
  1784. }
  1785. #endif
  1786. if (_extension->free_instance) {
  1787. _extension->free_instance(_extension->class_userdata, _extension_instance);
  1788. }
  1789. _extension = nullptr;
  1790. _extension_instance = nullptr;
  1791. }
  1792. #ifdef TOOLS_ENABLED
  1793. else if (_instance_bindings != nullptr) {
  1794. Engine *engine = Engine::get_singleton();
  1795. GDExtensionManager *gdextension_manager = GDExtensionManager::get_singleton();
  1796. if (engine && gdextension_manager && engine->is_extension_reloading_enabled()) {
  1797. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1798. gdextension_manager->untrack_instance_binding(_instance_bindings[i].token, this);
  1799. }
  1800. }
  1801. }
  1802. #endif
  1803. if (_emitting) {
  1804. //@todo this may need to actually reach the debugger prioritarily somehow because it may crash before
  1805. ERR_PRINT(vformat("Object '%s' was freed or unreferenced while a signal is being emitted from it. Try connecting to the signal using 'CONNECT_DEFERRED' flag, or use queue_free() to free the object (if this object is a Node) to avoid this error and potential crashes.", to_string()));
  1806. }
  1807. // Drop all connections to the signals of this object.
  1808. while (signal_map.size()) {
  1809. // Avoid regular iteration so erasing is safe.
  1810. KeyValue<StringName, SignalData> &E = *signal_map.begin();
  1811. SignalData *s = &E.value;
  1812. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1813. Object *target = slot_kv.value.conn.callable.get_object();
  1814. if (likely(target)) {
  1815. target->connections.erase(slot_kv.value.cE);
  1816. }
  1817. }
  1818. signal_map.erase(E.key);
  1819. }
  1820. // Disconnect signals that connect to this object.
  1821. while (connections.size()) {
  1822. Connection c = connections.front()->get();
  1823. Object *obj = c.callable.get_object();
  1824. bool disconnected = false;
  1825. if (likely(obj)) {
  1826. disconnected = c.signal.get_object()->_disconnect(c.signal.get_name(), c.callable, true);
  1827. }
  1828. if (unlikely(!disconnected)) {
  1829. // If the disconnect has failed, abandon the connection to avoid getting trapped in an infinite loop here.
  1830. connections.pop_front();
  1831. }
  1832. }
  1833. if (_instance_id != ObjectID()) {
  1834. ObjectDB::remove_instance(this);
  1835. _instance_id = ObjectID();
  1836. }
  1837. _predelete_ok = 2;
  1838. if (_instance_bindings != nullptr) {
  1839. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1840. if (_instance_bindings[i].free_callback) {
  1841. _instance_bindings[i].free_callback(_instance_bindings[i].token, this, _instance_bindings[i].binding);
  1842. }
  1843. }
  1844. memfree(_instance_bindings);
  1845. }
  1846. }
  1847. bool predelete_handler(Object *p_object) {
  1848. return p_object->_predelete();
  1849. }
  1850. void postinitialize_handler(Object *p_object) {
  1851. p_object->_initialize();
  1852. p_object->_postinitialize();
  1853. }
  1854. void ObjectDB::debug_objects(DebugFunc p_func) {
  1855. spin_lock.lock();
  1856. for (uint32_t i = 0, count = slot_count; i < slot_max && count != 0; i++) {
  1857. if (object_slots[i].validator) {
  1858. p_func(object_slots[i].object);
  1859. count--;
  1860. }
  1861. }
  1862. spin_lock.unlock();
  1863. }
  1864. #ifdef TOOLS_ENABLED
  1865. void Object::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1866. const String pf = p_function;
  1867. if (p_idx == 0) {
  1868. if (pf == "connect" || pf == "is_connected" || pf == "disconnect" || pf == "emit_signal" || pf == "has_signal") {
  1869. List<MethodInfo> signals;
  1870. get_signal_list(&signals);
  1871. for (const MethodInfo &E : signals) {
  1872. r_options->push_back(E.name.quote());
  1873. }
  1874. } else if (pf == "call" || pf == "call_deferred" || pf == "callv" || pf == "has_method") {
  1875. List<MethodInfo> methods;
  1876. get_method_list(&methods);
  1877. for (const MethodInfo &E : methods) {
  1878. if (E.name.begins_with("_") && !(E.flags & METHOD_FLAG_VIRTUAL)) {
  1879. continue;
  1880. }
  1881. r_options->push_back(E.name.quote());
  1882. }
  1883. } else if (pf == "set" || pf == "set_deferred" || pf == "get") {
  1884. List<PropertyInfo> properties;
  1885. get_property_list(&properties);
  1886. for (const PropertyInfo &E : properties) {
  1887. if (E.usage & PROPERTY_USAGE_DEFAULT && !(E.usage & PROPERTY_USAGE_INTERNAL)) {
  1888. r_options->push_back(E.name.quote());
  1889. }
  1890. }
  1891. } else if (pf == "set_meta" || pf == "get_meta" || pf == "has_meta" || pf == "remove_meta") {
  1892. for (const KeyValue<StringName, Variant> &K : metadata) {
  1893. r_options->push_back(String(K.key).quote());
  1894. }
  1895. }
  1896. } else if (p_idx == 2) {
  1897. if (pf == "connect") {
  1898. // Ideally, the constants should be inferred by the parameter.
  1899. // But a parameter's PropertyInfo does not store the enum they come from, so this will do for now.
  1900. List<StringName> constants;
  1901. ClassDB::get_enum_constants("Object", "ConnectFlags", &constants);
  1902. for (const StringName &E : constants) {
  1903. r_options->push_back(String(E));
  1904. }
  1905. }
  1906. }
  1907. }
  1908. #endif
  1909. SpinLock ObjectDB::spin_lock;
  1910. uint32_t ObjectDB::slot_count = 0;
  1911. uint32_t ObjectDB::slot_max = 0;
  1912. ObjectDB::ObjectSlot *ObjectDB::object_slots = nullptr;
  1913. uint64_t ObjectDB::validator_counter = 0;
  1914. int ObjectDB::get_object_count() {
  1915. return slot_count;
  1916. }
  1917. ObjectID ObjectDB::add_instance(Object *p_object) {
  1918. spin_lock.lock();
  1919. if (unlikely(slot_count == slot_max)) {
  1920. CRASH_COND(slot_count == (1 << OBJECTDB_SLOT_MAX_COUNT_BITS));
  1921. uint32_t new_slot_max = slot_max > 0 ? slot_max * 2 : 1;
  1922. object_slots = (ObjectSlot *)memrealloc(object_slots, sizeof(ObjectSlot) * new_slot_max);
  1923. for (uint32_t i = slot_max; i < new_slot_max; i++) {
  1924. object_slots[i].object = nullptr;
  1925. object_slots[i].is_ref_counted = false;
  1926. object_slots[i].next_free = i;
  1927. object_slots[i].validator = 0;
  1928. }
  1929. slot_max = new_slot_max;
  1930. }
  1931. uint32_t slot = object_slots[slot_count].next_free;
  1932. if (object_slots[slot].object != nullptr) {
  1933. spin_lock.unlock();
  1934. ERR_FAIL_COND_V(object_slots[slot].object != nullptr, ObjectID());
  1935. }
  1936. object_slots[slot].object = p_object;
  1937. object_slots[slot].is_ref_counted = p_object->is_ref_counted();
  1938. validator_counter = (validator_counter + 1) & OBJECTDB_VALIDATOR_MASK;
  1939. if (unlikely(validator_counter == 0)) {
  1940. validator_counter = 1;
  1941. }
  1942. object_slots[slot].validator = validator_counter;
  1943. uint64_t id = validator_counter;
  1944. id <<= OBJECTDB_SLOT_MAX_COUNT_BITS;
  1945. id |= uint64_t(slot);
  1946. if (p_object->is_ref_counted()) {
  1947. id |= OBJECTDB_REFERENCE_BIT;
  1948. }
  1949. slot_count++;
  1950. spin_lock.unlock();
  1951. return ObjectID(id);
  1952. }
  1953. void ObjectDB::remove_instance(Object *p_object) {
  1954. uint64_t t = p_object->get_instance_id();
  1955. uint32_t slot = t & OBJECTDB_SLOT_MAX_COUNT_MASK; //slot is always valid on valid object
  1956. spin_lock.lock();
  1957. #ifdef DEBUG_ENABLED
  1958. if (object_slots[slot].object != p_object) {
  1959. spin_lock.unlock();
  1960. ERR_FAIL_COND(object_slots[slot].object != p_object);
  1961. }
  1962. {
  1963. uint64_t validator = (t >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
  1964. if (object_slots[slot].validator != validator) {
  1965. spin_lock.unlock();
  1966. ERR_FAIL_COND(object_slots[slot].validator != validator);
  1967. }
  1968. }
  1969. #endif
  1970. //decrease slot count
  1971. slot_count--;
  1972. //set the free slot properly
  1973. object_slots[slot_count].next_free = slot;
  1974. //invalidate, so checks against it fail
  1975. object_slots[slot].validator = 0;
  1976. object_slots[slot].is_ref_counted = false;
  1977. object_slots[slot].object = nullptr;
  1978. spin_lock.unlock();
  1979. }
  1980. void ObjectDB::setup() {
  1981. //nothing to do now
  1982. }
  1983. void ObjectDB::cleanup() {
  1984. spin_lock.lock();
  1985. if (slot_count > 0) {
  1986. WARN_PRINT("ObjectDB instances leaked at exit (run with --verbose for details).");
  1987. if (OS::get_singleton()->is_stdout_verbose()) {
  1988. // Ensure calling the native classes because if a leaked instance has a script
  1989. // that overrides any of those methods, it'd not be OK to call them at this point,
  1990. // now the scripting languages have already been terminated.
  1991. MethodBind *node_get_path = ClassDB::get_method("Node", "get_path");
  1992. MethodBind *resource_get_path = ClassDB::get_method("Resource", "get_path");
  1993. Callable::CallError call_error;
  1994. for (uint32_t i = 0, count = slot_count; i < slot_max && count != 0; i++) {
  1995. if (object_slots[i].validator) {
  1996. Object *obj = object_slots[i].object;
  1997. String extra_info;
  1998. if (obj->is_class("Node")) {
  1999. extra_info = " - Node path: " + String(node_get_path->call(obj, nullptr, 0, call_error));
  2000. }
  2001. if (obj->is_class("Resource")) {
  2002. extra_info = " - Resource path: " + String(resource_get_path->call(obj, nullptr, 0, call_error));
  2003. }
  2004. uint64_t id = uint64_t(i) | (uint64_t(object_slots[i].validator) << OBJECTDB_SLOT_MAX_COUNT_BITS) | (object_slots[i].is_ref_counted ? OBJECTDB_REFERENCE_BIT : 0);
  2005. DEV_ASSERT(id == (uint64_t)obj->get_instance_id()); // We could just use the id from the object, but this check may help catching memory corruption catastrophes.
  2006. print_line("Leaked instance: " + String(obj->get_class()) + ":" + uitos(id) + extra_info);
  2007. count--;
  2008. }
  2009. }
  2010. print_line("Hint: Leaked instances typically happen when nodes are removed from the scene tree (with `remove_child()`) but not freed (with `free()` or `queue_free()`).");
  2011. }
  2012. }
  2013. if (object_slots) {
  2014. memfree(object_slots);
  2015. }
  2016. spin_lock.unlock();
  2017. }