object.cpp 70 KB

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