object.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  1. /*************************************************************************/
  2. /* object.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "object.h"
  31. #include "core/class_db.h"
  32. #include "core/core_string_names.h"
  33. #include "core/message_queue.h"
  34. #include "core/os/os.h"
  35. #include "core/print_string.h"
  36. #include "core/resource.h"
  37. #include "core/script_language.h"
  38. #include "core/translation.h"
  39. #ifdef DEBUG_ENABLED
  40. struct _ObjectDebugLock {
  41. Object *obj;
  42. _ObjectDebugLock(Object *p_obj) {
  43. obj = p_obj;
  44. obj->_lock_index.ref();
  45. }
  46. ~_ObjectDebugLock() {
  47. obj->_lock_index.unref();
  48. }
  49. };
  50. #define OBJ_DEBUG_LOCK _ObjectDebugLock _debug_lock(this);
  51. #else
  52. #define OBJ_DEBUG_LOCK
  53. #endif
  54. PropertyInfo::operator Dictionary() const {
  55. Dictionary d;
  56. d["name"] = name;
  57. d["class_name"] = class_name;
  58. d["type"] = type;
  59. d["hint"] = hint;
  60. d["hint_string"] = hint_string;
  61. d["usage"] = usage;
  62. return d;
  63. }
  64. PropertyInfo PropertyInfo::from_dict(const Dictionary &p_dict) {
  65. PropertyInfo pi;
  66. if (p_dict.has("type")) {
  67. pi.type = Variant::Type(int(p_dict["type"]));
  68. }
  69. if (p_dict.has("name")) {
  70. pi.name = p_dict["name"];
  71. }
  72. if (p_dict.has("class_name")) {
  73. pi.class_name = p_dict["class_name"];
  74. }
  75. if (p_dict.has("hint")) {
  76. pi.hint = PropertyHint(int(p_dict["hint"]));
  77. }
  78. if (p_dict.has("hint_string")) {
  79. pi.hint_string = p_dict["hint_string"];
  80. }
  81. if (p_dict.has("usage")) {
  82. pi.usage = p_dict["usage"];
  83. }
  84. return pi;
  85. }
  86. Array convert_property_list(const List<PropertyInfo> *p_list) {
  87. Array va;
  88. for (const List<PropertyInfo>::Element *E = p_list->front(); E; E = E->next()) {
  89. va.push_back(Dictionary(E->get()));
  90. }
  91. return va;
  92. }
  93. MethodInfo::operator Dictionary() const {
  94. Dictionary d;
  95. d["name"] = name;
  96. d["args"] = convert_property_list(&arguments);
  97. Array da;
  98. for (int i = 0; i < default_arguments.size(); i++) {
  99. da.push_back(default_arguments[i]);
  100. }
  101. d["default_args"] = da;
  102. d["flags"] = flags;
  103. d["id"] = id;
  104. Dictionary r = return_val;
  105. d["return"] = r;
  106. return d;
  107. }
  108. MethodInfo MethodInfo::from_dict(const Dictionary &p_dict) {
  109. MethodInfo mi;
  110. if (p_dict.has("name")) {
  111. mi.name = p_dict["name"];
  112. }
  113. Array args;
  114. if (p_dict.has("args")) {
  115. args = p_dict["args"];
  116. }
  117. for (int i = 0; i < args.size(); i++) {
  118. Dictionary d = args[i];
  119. mi.arguments.push_back(PropertyInfo::from_dict(d));
  120. }
  121. Array defargs;
  122. if (p_dict.has("default_args")) {
  123. defargs = p_dict["default_args"];
  124. }
  125. for (int i = 0; i < defargs.size(); i++) {
  126. mi.default_arguments.push_back(defargs[i]);
  127. }
  128. if (p_dict.has("return")) {
  129. mi.return_val = PropertyInfo::from_dict(p_dict["return"]);
  130. }
  131. if (p_dict.has("flags")) {
  132. mi.flags = p_dict["flags"];
  133. }
  134. return mi;
  135. }
  136. MethodInfo::MethodInfo() :
  137. flags(METHOD_FLAG_NORMAL) {}
  138. MethodInfo::MethodInfo(const String &p_name) :
  139. name(p_name),
  140. flags(METHOD_FLAG_NORMAL) {
  141. }
  142. MethodInfo::MethodInfo(const String &p_name, const PropertyInfo &p_param1) :
  143. name(p_name),
  144. flags(METHOD_FLAG_NORMAL) {
  145. arguments.push_back(p_param1);
  146. }
  147. MethodInfo::MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2) :
  148. name(p_name),
  149. flags(METHOD_FLAG_NORMAL) {
  150. arguments.push_back(p_param1);
  151. arguments.push_back(p_param2);
  152. }
  153. MethodInfo::MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3) :
  154. name(p_name),
  155. flags(METHOD_FLAG_NORMAL) {
  156. arguments.push_back(p_param1);
  157. arguments.push_back(p_param2);
  158. arguments.push_back(p_param3);
  159. }
  160. MethodInfo::MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4) :
  161. name(p_name),
  162. flags(METHOD_FLAG_NORMAL) {
  163. arguments.push_back(p_param1);
  164. arguments.push_back(p_param2);
  165. arguments.push_back(p_param3);
  166. arguments.push_back(p_param4);
  167. }
  168. MethodInfo::MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4, const PropertyInfo &p_param5) :
  169. name(p_name),
  170. flags(METHOD_FLAG_NORMAL) {
  171. arguments.push_back(p_param1);
  172. arguments.push_back(p_param2);
  173. arguments.push_back(p_param3);
  174. arguments.push_back(p_param4);
  175. arguments.push_back(p_param5);
  176. }
  177. MethodInfo::MethodInfo(Variant::Type ret) :
  178. flags(METHOD_FLAG_NORMAL) {
  179. return_val.type = ret;
  180. }
  181. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name) :
  182. name(p_name),
  183. flags(METHOD_FLAG_NORMAL) {
  184. return_val.type = ret;
  185. }
  186. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1) :
  187. name(p_name),
  188. flags(METHOD_FLAG_NORMAL) {
  189. return_val.type = ret;
  190. arguments.push_back(p_param1);
  191. }
  192. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2) :
  193. name(p_name),
  194. flags(METHOD_FLAG_NORMAL) {
  195. return_val.type = ret;
  196. arguments.push_back(p_param1);
  197. arguments.push_back(p_param2);
  198. }
  199. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3) :
  200. name(p_name),
  201. flags(METHOD_FLAG_NORMAL) {
  202. return_val.type = ret;
  203. arguments.push_back(p_param1);
  204. arguments.push_back(p_param2);
  205. arguments.push_back(p_param3);
  206. }
  207. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4) :
  208. name(p_name),
  209. flags(METHOD_FLAG_NORMAL) {
  210. return_val.type = ret;
  211. arguments.push_back(p_param1);
  212. arguments.push_back(p_param2);
  213. arguments.push_back(p_param3);
  214. arguments.push_back(p_param4);
  215. }
  216. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4, const PropertyInfo &p_param5) :
  217. name(p_name),
  218. flags(METHOD_FLAG_NORMAL) {
  219. return_val.type = ret;
  220. arguments.push_back(p_param1);
  221. arguments.push_back(p_param2);
  222. arguments.push_back(p_param3);
  223. arguments.push_back(p_param4);
  224. arguments.push_back(p_param5);
  225. }
  226. MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name) :
  227. name(p_name),
  228. return_val(p_ret),
  229. flags(METHOD_FLAG_NORMAL) {
  230. }
  231. MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1) :
  232. name(p_name),
  233. return_val(p_ret),
  234. flags(METHOD_FLAG_NORMAL) {
  235. arguments.push_back(p_param1);
  236. }
  237. MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2) :
  238. name(p_name),
  239. return_val(p_ret),
  240. flags(METHOD_FLAG_NORMAL) {
  241. arguments.push_back(p_param1);
  242. arguments.push_back(p_param2);
  243. }
  244. MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3) :
  245. name(p_name),
  246. return_val(p_ret),
  247. flags(METHOD_FLAG_NORMAL) {
  248. arguments.push_back(p_param1);
  249. arguments.push_back(p_param2);
  250. arguments.push_back(p_param3);
  251. }
  252. MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4) :
  253. name(p_name),
  254. return_val(p_ret),
  255. flags(METHOD_FLAG_NORMAL) {
  256. arguments.push_back(p_param1);
  257. arguments.push_back(p_param2);
  258. arguments.push_back(p_param3);
  259. arguments.push_back(p_param4);
  260. }
  261. MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4, const PropertyInfo &p_param5) :
  262. name(p_name),
  263. return_val(p_ret),
  264. flags(METHOD_FLAG_NORMAL) {
  265. arguments.push_back(p_param1);
  266. arguments.push_back(p_param2);
  267. arguments.push_back(p_param3);
  268. arguments.push_back(p_param4);
  269. arguments.push_back(p_param5);
  270. }
  271. Object::Connection::operator Variant() const {
  272. Dictionary d;
  273. d["signal"] = signal;
  274. d["callable"] = callable;
  275. d["flags"] = flags;
  276. d["binds"] = binds;
  277. return d;
  278. }
  279. bool Object::Connection::operator<(const Connection &p_conn) const {
  280. if (signal == p_conn.signal) {
  281. return callable < p_conn.callable;
  282. } else {
  283. return signal < p_conn.signal;
  284. }
  285. }
  286. Object::Connection::Connection(const Variant &p_variant) {
  287. Dictionary d = p_variant;
  288. if (d.has("signal")) {
  289. signal = d["signal"];
  290. }
  291. if (d.has("callable")) {
  292. callable = d["callable"];
  293. }
  294. if (d.has("flags")) {
  295. flags = d["flags"];
  296. }
  297. if (d.has("binds")) {
  298. binds = d["binds"];
  299. }
  300. }
  301. bool Object::_predelete() {
  302. _predelete_ok = 1;
  303. notification(NOTIFICATION_PREDELETE, true);
  304. if (_predelete_ok) {
  305. _class_ptr = nullptr; //must restore so destructors can access class ptr correctly
  306. }
  307. return _predelete_ok;
  308. }
  309. void Object::_postinitialize() {
  310. _class_ptr = _get_class_namev();
  311. _initialize_classv();
  312. notification(NOTIFICATION_POSTINITIALIZE);
  313. }
  314. void Object::get_valid_parents_static(List<String> *p_parents) {
  315. }
  316. void Object::_get_valid_parents_static(List<String> *p_parents) {
  317. }
  318. void Object::set(const StringName &p_name, const Variant &p_value, bool *r_valid) {
  319. #ifdef TOOLS_ENABLED
  320. _edited = true;
  321. #endif
  322. if (script_instance) {
  323. if (script_instance->set(p_name, p_value)) {
  324. if (r_valid) {
  325. *r_valid = true;
  326. }
  327. return;
  328. }
  329. }
  330. //try built-in setgetter
  331. {
  332. if (ClassDB::set_property(this, p_name, p_value, r_valid)) {
  333. /*
  334. if (r_valid)
  335. *r_valid=true;
  336. */
  337. return;
  338. }
  339. }
  340. if (p_name == CoreStringNames::get_singleton()->_script) {
  341. set_script(p_value);
  342. if (r_valid) {
  343. *r_valid = true;
  344. }
  345. return;
  346. } else if (p_name == CoreStringNames::get_singleton()->_meta) {
  347. //set_meta(p_name,p_value);
  348. metadata = p_value.duplicate();
  349. if (r_valid) {
  350. *r_valid = true;
  351. }
  352. return;
  353. }
  354. //something inside the object... :|
  355. bool success = _setv(p_name, p_value);
  356. if (success) {
  357. if (r_valid) {
  358. *r_valid = true;
  359. }
  360. return;
  361. }
  362. {
  363. bool valid;
  364. setvar(p_name, p_value, &valid);
  365. if (valid) {
  366. if (r_valid) {
  367. *r_valid = true;
  368. }
  369. return;
  370. }
  371. }
  372. #ifdef TOOLS_ENABLED
  373. if (script_instance) {
  374. bool valid;
  375. script_instance->property_set_fallback(p_name, p_value, &valid);
  376. if (valid) {
  377. if (r_valid) {
  378. *r_valid = true;
  379. }
  380. return;
  381. }
  382. }
  383. #endif
  384. if (r_valid) {
  385. *r_valid = false;
  386. }
  387. }
  388. Variant Object::get(const StringName &p_name, bool *r_valid) const {
  389. Variant ret;
  390. if (script_instance) {
  391. if (script_instance->get(p_name, ret)) {
  392. if (r_valid) {
  393. *r_valid = true;
  394. }
  395. return ret;
  396. }
  397. }
  398. //try built-in setgetter
  399. {
  400. if (ClassDB::get_property(const_cast<Object *>(this), p_name, ret)) {
  401. if (r_valid) {
  402. *r_valid = true;
  403. }
  404. return ret;
  405. }
  406. }
  407. if (p_name == CoreStringNames::get_singleton()->_script) {
  408. ret = get_script();
  409. if (r_valid) {
  410. *r_valid = true;
  411. }
  412. return ret;
  413. } else if (p_name == CoreStringNames::get_singleton()->_meta) {
  414. ret = metadata;
  415. if (r_valid) {
  416. *r_valid = true;
  417. }
  418. return ret;
  419. } else {
  420. //something inside the object... :|
  421. bool success = _getv(p_name, ret);
  422. if (success) {
  423. if (r_valid) {
  424. *r_valid = true;
  425. }
  426. return ret;
  427. }
  428. //if nothing else, use getvar
  429. {
  430. bool valid;
  431. ret = getvar(p_name, &valid);
  432. if (valid) {
  433. if (r_valid) {
  434. *r_valid = true;
  435. }
  436. return ret;
  437. }
  438. }
  439. #ifdef TOOLS_ENABLED
  440. if (script_instance) {
  441. bool valid;
  442. ret = script_instance->property_get_fallback(p_name, &valid);
  443. if (valid) {
  444. if (r_valid) {
  445. *r_valid = true;
  446. }
  447. return ret;
  448. }
  449. }
  450. #endif
  451. if (r_valid) {
  452. *r_valid = false;
  453. }
  454. return Variant();
  455. }
  456. }
  457. void Object::set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid) {
  458. if (p_names.empty()) {
  459. if (r_valid) {
  460. *r_valid = false;
  461. }
  462. return;
  463. }
  464. if (p_names.size() == 1) {
  465. set(p_names[0], p_value, r_valid);
  466. return;
  467. }
  468. bool valid = false;
  469. if (!r_valid) {
  470. r_valid = &valid;
  471. }
  472. List<Variant> value_stack;
  473. value_stack.push_back(get(p_names[0], r_valid));
  474. if (!*r_valid) {
  475. value_stack.clear();
  476. return;
  477. }
  478. for (int i = 1; i < p_names.size() - 1; i++) {
  479. value_stack.push_back(value_stack.back()->get().get_named(p_names[i], r_valid));
  480. if (!*r_valid) {
  481. value_stack.clear();
  482. return;
  483. }
  484. }
  485. value_stack.push_back(p_value); // p_names[p_names.size() - 1]
  486. for (int i = p_names.size() - 1; i > 0; i--) {
  487. value_stack.back()->prev()->get().set_named(p_names[i], value_stack.back()->get(), r_valid);
  488. value_stack.pop_back();
  489. if (!*r_valid) {
  490. value_stack.clear();
  491. return;
  492. }
  493. }
  494. set(p_names[0], value_stack.back()->get(), r_valid);
  495. value_stack.pop_back();
  496. ERR_FAIL_COND(!value_stack.empty());
  497. }
  498. Variant Object::get_indexed(const Vector<StringName> &p_names, bool *r_valid) const {
  499. if (p_names.empty()) {
  500. if (r_valid) {
  501. *r_valid = false;
  502. }
  503. return Variant();
  504. }
  505. bool valid = false;
  506. Variant current_value = get(p_names[0], &valid);
  507. for (int i = 1; i < p_names.size(); i++) {
  508. current_value = current_value.get_named(p_names[i], &valid);
  509. if (!valid) {
  510. break;
  511. }
  512. }
  513. if (r_valid) {
  514. *r_valid = valid;
  515. }
  516. return current_value;
  517. }
  518. void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) const {
  519. if (script_instance && p_reversed) {
  520. p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  521. script_instance->get_property_list(p_list);
  522. }
  523. _get_property_listv(p_list, p_reversed);
  524. if (!is_class("Script")) { // can still be set, but this is for userfriendlyness
  525. #ifdef TOOLS_ENABLED
  526. p_list->push_back(PropertyInfo(Variant::NIL, "Script", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  527. #endif
  528. p_list->push_back(PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT));
  529. }
  530. if (!metadata.empty()) {
  531. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "__meta__", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  532. }
  533. if (script_instance && !p_reversed) {
  534. p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  535. script_instance->get_property_list(p_list);
  536. }
  537. }
  538. void Object::_validate_property(PropertyInfo &property) const {
  539. }
  540. void Object::get_method_list(List<MethodInfo> *p_list) const {
  541. ClassDB::get_method_list(get_class_name(), p_list);
  542. if (script_instance) {
  543. script_instance->get_method_list(p_list);
  544. }
  545. }
  546. Variant Object::_call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  547. if (p_argcount < 1) {
  548. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  549. r_error.argument = 0;
  550. return Variant();
  551. }
  552. if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
  553. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  554. r_error.argument = 0;
  555. r_error.expected = Variant::STRING_NAME;
  556. return Variant();
  557. }
  558. StringName method = *p_args[0];
  559. return call(method, &p_args[1], p_argcount - 1, r_error);
  560. }
  561. Variant Object::_call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  562. if (p_argcount < 1) {
  563. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  564. r_error.argument = 0;
  565. return Variant();
  566. }
  567. if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
  568. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  569. r_error.argument = 0;
  570. r_error.expected = Variant::STRING_NAME;
  571. return Variant();
  572. }
  573. r_error.error = Callable::CallError::CALL_OK;
  574. StringName method = *p_args[0];
  575. MessageQueue::get_singleton()->push_call(get_instance_id(), method, &p_args[1], p_argcount - 1, true);
  576. return Variant();
  577. }
  578. bool Object::has_method(const StringName &p_method) const {
  579. if (p_method == CoreStringNames::get_singleton()->_free) {
  580. return true;
  581. }
  582. if (script_instance && script_instance->has_method(p_method)) {
  583. return true;
  584. }
  585. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  586. return method != nullptr;
  587. }
  588. Variant Object::getvar(const Variant &p_key, bool *r_valid) const {
  589. if (r_valid) {
  590. *r_valid = false;
  591. }
  592. return Variant();
  593. }
  594. void Object::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) {
  595. if (r_valid) {
  596. *r_valid = false;
  597. }
  598. }
  599. Variant Object::callv(const StringName &p_method, const Array &p_args) {
  600. const Variant **argptrs = nullptr;
  601. if (p_args.size() > 0) {
  602. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_args.size());
  603. for (int i = 0; i < p_args.size(); i++) {
  604. argptrs[i] = &p_args[i];
  605. }
  606. }
  607. Callable::CallError ce;
  608. Variant ret = call(p_method, argptrs, p_args.size(), ce);
  609. if (ce.error != Callable::CallError::CALL_OK) {
  610. ERR_FAIL_V_MSG(Variant(), "Error calling method from 'callv': " + Variant::get_call_error_text(this, p_method, argptrs, p_args.size(), ce) + ".");
  611. }
  612. return ret;
  613. }
  614. Variant Object::call(const StringName &p_name, VARIANT_ARG_DECLARE) {
  615. VARIANT_ARGPTRS;
  616. int argc = 0;
  617. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  618. if (argptr[i]->get_type() == Variant::NIL) {
  619. break;
  620. }
  621. argc++;
  622. }
  623. Callable::CallError error;
  624. Variant ret = call(p_name, argptr, argc, error);
  625. return ret;
  626. }
  627. Variant Object::call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  628. r_error.error = Callable::CallError::CALL_OK;
  629. if (p_method == CoreStringNames::get_singleton()->_free) {
  630. //free must be here, before anything, always ready
  631. #ifdef DEBUG_ENABLED
  632. if (p_argcount != 0) {
  633. r_error.argument = 0;
  634. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  635. return Variant();
  636. }
  637. if (Object::cast_to<Reference>(this)) {
  638. r_error.argument = 0;
  639. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  640. ERR_FAIL_V_MSG(Variant(), "Can't 'free' a reference.");
  641. }
  642. if (_lock_index.get() > 1) {
  643. r_error.argument = 0;
  644. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  645. ERR_FAIL_V_MSG(Variant(), "Object is locked and can't be freed.");
  646. }
  647. #endif
  648. //must be here, must be before everything,
  649. memdelete(this);
  650. r_error.error = Callable::CallError::CALL_OK;
  651. return Variant();
  652. }
  653. Variant ret;
  654. OBJ_DEBUG_LOCK
  655. if (script_instance) {
  656. ret = script_instance->call(p_method, p_args, p_argcount, r_error);
  657. //force jumptable
  658. switch (r_error.error) {
  659. case Callable::CallError::CALL_OK:
  660. return ret;
  661. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  662. break;
  663. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  664. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  665. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  666. return ret;
  667. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL: {
  668. }
  669. }
  670. }
  671. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  672. if (method) {
  673. ret = method->call(this, p_args, p_argcount, r_error);
  674. } else {
  675. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  676. }
  677. return ret;
  678. }
  679. void Object::notification(int p_notification, bool p_reversed) {
  680. _notificationv(p_notification, p_reversed);
  681. if (script_instance) {
  682. script_instance->notification(p_notification);
  683. }
  684. }
  685. String Object::to_string() {
  686. if (script_instance) {
  687. bool valid;
  688. String ret = script_instance->to_string(&valid);
  689. if (valid) {
  690. return ret;
  691. }
  692. }
  693. return "[" + get_class() + ":" + itos(get_instance_id()) + "]";
  694. }
  695. void Object::_changed_callback(Object *p_changed, const char *p_prop) {
  696. }
  697. void Object::add_change_receptor(Object *p_receptor) {
  698. change_receptors.insert(p_receptor);
  699. }
  700. void Object::remove_change_receptor(Object *p_receptor) {
  701. change_receptors.erase(p_receptor);
  702. }
  703. void Object::property_list_changed_notify() {
  704. _change_notify();
  705. }
  706. void Object::cancel_delete() {
  707. _predelete_ok = true;
  708. }
  709. void Object::set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance) {
  710. //this function is not meant to be used in any of these ways
  711. ERR_FAIL_COND(p_script.is_null());
  712. ERR_FAIL_COND(!p_instance);
  713. ERR_FAIL_COND(script_instance != nullptr || !script.is_null());
  714. script = p_script;
  715. script_instance = p_instance;
  716. }
  717. void Object::set_script(const Variant &p_script) {
  718. if (script == p_script) {
  719. return;
  720. }
  721. if (script_instance) {
  722. memdelete(script_instance);
  723. script_instance = nullptr;
  724. }
  725. script = p_script;
  726. Ref<Script> s = script;
  727. if (!s.is_null()) {
  728. if (s->can_instance()) {
  729. OBJ_DEBUG_LOCK
  730. script_instance = s->instance_create(this);
  731. } else if (Engine::get_singleton()->is_editor_hint()) {
  732. OBJ_DEBUG_LOCK
  733. script_instance = s->placeholder_instance_create(this);
  734. }
  735. }
  736. _change_notify(); //scripts may add variables, so refresh is desired
  737. emit_signal(CoreStringNames::get_singleton()->script_changed);
  738. }
  739. void Object::set_script_instance(ScriptInstance *p_instance) {
  740. if (script_instance == p_instance) {
  741. return;
  742. }
  743. if (script_instance) {
  744. memdelete(script_instance);
  745. }
  746. script_instance = p_instance;
  747. if (p_instance) {
  748. script = p_instance->get_script();
  749. } else {
  750. script = Variant();
  751. }
  752. }
  753. Variant Object::get_script() const {
  754. return script;
  755. }
  756. bool Object::has_meta(const String &p_name) const {
  757. return metadata.has(p_name);
  758. }
  759. void Object::set_meta(const String &p_name, const Variant &p_value) {
  760. if (p_value.get_type() == Variant::NIL) {
  761. metadata.erase(p_name);
  762. return;
  763. }
  764. metadata[p_name] = p_value;
  765. }
  766. Variant Object::get_meta(const String &p_name) const {
  767. ERR_FAIL_COND_V(!metadata.has(p_name), Variant());
  768. return metadata[p_name];
  769. }
  770. void Object::remove_meta(const String &p_name) {
  771. metadata.erase(p_name);
  772. }
  773. Array Object::_get_property_list_bind() const {
  774. List<PropertyInfo> lpi;
  775. get_property_list(&lpi);
  776. return convert_property_list(&lpi);
  777. }
  778. Array Object::_get_method_list_bind() const {
  779. List<MethodInfo> ml;
  780. get_method_list(&ml);
  781. Array ret;
  782. for (List<MethodInfo>::Element *E = ml.front(); E; E = E->next()) {
  783. Dictionary d = E->get();
  784. //va.push_back(d);
  785. ret.push_back(d);
  786. }
  787. return ret;
  788. }
  789. Vector<String> Object::_get_meta_list_bind() const {
  790. Vector<String> _metaret;
  791. List<Variant> keys;
  792. metadata.get_key_list(&keys);
  793. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  794. _metaret.push_back(E->get());
  795. }
  796. return _metaret;
  797. }
  798. void Object::get_meta_list(List<String> *p_list) const {
  799. List<Variant> keys;
  800. metadata.get_key_list(&keys);
  801. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  802. p_list->push_back(E->get());
  803. }
  804. }
  805. void Object::add_user_signal(const MethodInfo &p_signal) {
  806. ERR_FAIL_COND_MSG(p_signal.name == "", "Signal name cannot be empty.");
  807. ERR_FAIL_COND_MSG(ClassDB::has_signal(get_class_name(), p_signal.name), "User signal's name conflicts with a built-in signal of '" + get_class_name() + "'.");
  808. ERR_FAIL_COND_MSG(signal_map.has(p_signal.name), "Trying to add already existing signal '" + p_signal.name + "'.");
  809. SignalData s;
  810. s.user = p_signal;
  811. signal_map[p_signal.name] = s;
  812. }
  813. bool Object::_has_user_signal(const StringName &p_name) const {
  814. if (!signal_map.has(p_name)) {
  815. return false;
  816. }
  817. return signal_map[p_name].user.name.length() > 0;
  818. }
  819. struct _ObjectSignalDisconnectData {
  820. StringName signal;
  821. Callable callable;
  822. };
  823. Variant Object::_emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  824. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  825. ERR_FAIL_COND_V(p_argcount < 1, Variant());
  826. if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
  827. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  828. r_error.argument = 0;
  829. r_error.expected = Variant::STRING_NAME;
  830. ERR_FAIL_COND_V(p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING, Variant());
  831. }
  832. r_error.error = Callable::CallError::CALL_OK;
  833. StringName signal = *p_args[0];
  834. const Variant **args = nullptr;
  835. int argc = p_argcount - 1;
  836. if (argc) {
  837. args = &p_args[1];
  838. }
  839. emit_signal(signal, args, argc);
  840. return Variant();
  841. }
  842. Error Object::emit_signal(const StringName &p_name, const Variant **p_args, int p_argcount) {
  843. if (_block_signals) {
  844. return ERR_CANT_ACQUIRE_RESOURCE; //no emit, signals blocked
  845. }
  846. SignalData *s = signal_map.getptr(p_name);
  847. if (!s) {
  848. #ifdef DEBUG_ENABLED
  849. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_name);
  850. //check in script
  851. ERR_FAIL_COND_V_MSG(!signal_is_valid && !script.is_null() && !Ref<Script>(script)->has_script_signal(p_name), ERR_UNAVAILABLE, "Can't emit non-existing signal " + String("\"") + p_name + "\".");
  852. #endif
  853. //not connected? just return
  854. return ERR_UNAVAILABLE;
  855. }
  856. List<_ObjectSignalDisconnectData> disconnect_data;
  857. //copy on write will ensure that disconnecting the signal or even deleting the object will not affect the signal calling.
  858. //this happens automatically and will not change the performance of calling.
  859. //awesome, isn't it?
  860. VMap<Callable, SignalData::Slot> slot_map = s->slot_map;
  861. int ssize = slot_map.size();
  862. OBJ_DEBUG_LOCK
  863. Vector<const Variant *> bind_mem;
  864. Error err = OK;
  865. for (int i = 0; i < ssize; i++) {
  866. const Connection &c = slot_map.getv(i).conn;
  867. Object *target = c.callable.get_object();
  868. if (!target) {
  869. // Target might have been deleted during signal callback, this is expected and OK.
  870. continue;
  871. }
  872. const Variant **args = p_args;
  873. int argc = p_argcount;
  874. if (c.binds.size()) {
  875. //handle binds
  876. bind_mem.resize(p_argcount + c.binds.size());
  877. for (int j = 0; j < p_argcount; j++) {
  878. bind_mem.write[j] = p_args[j];
  879. }
  880. for (int j = 0; j < c.binds.size(); j++) {
  881. bind_mem.write[p_argcount + j] = &c.binds[j];
  882. }
  883. args = (const Variant **)bind_mem.ptr();
  884. argc = bind_mem.size();
  885. }
  886. if (c.flags & CONNECT_DEFERRED) {
  887. MessageQueue::get_singleton()->push_callable(c.callable, args, argc, true);
  888. } else {
  889. Callable::CallError ce;
  890. _emitting = true;
  891. Variant ret;
  892. c.callable.call(args, argc, ret, ce);
  893. _emitting = false;
  894. if (ce.error != Callable::CallError::CALL_OK) {
  895. #ifdef DEBUG_ENABLED
  896. if (c.flags & CONNECT_PERSIST && Engine::get_singleton()->is_editor_hint() && (script.is_null() || !Ref<Script>(script)->is_tool())) {
  897. continue;
  898. }
  899. #endif
  900. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD && !ClassDB::class_exists(target->get_class_name())) {
  901. //most likely object is not initialized yet, do not throw error.
  902. } else {
  903. ERR_PRINT("Error calling from signal '" + String(p_name) + "' to callable: " + Variant::get_callable_error_text(c.callable, args, argc, ce) + ".");
  904. err = ERR_METHOD_NOT_FOUND;
  905. }
  906. }
  907. }
  908. bool disconnect = c.flags & CONNECT_ONESHOT;
  909. #ifdef TOOLS_ENABLED
  910. if (disconnect && (c.flags & CONNECT_PERSIST) && Engine::get_singleton()->is_editor_hint()) {
  911. //this signal was connected from the editor, and is being edited. just don't disconnect for now
  912. disconnect = false;
  913. }
  914. #endif
  915. if (disconnect) {
  916. _ObjectSignalDisconnectData dd;
  917. dd.signal = p_name;
  918. dd.callable = c.callable;
  919. disconnect_data.push_back(dd);
  920. }
  921. }
  922. while (!disconnect_data.empty()) {
  923. const _ObjectSignalDisconnectData &dd = disconnect_data.front()->get();
  924. _disconnect(dd.signal, dd.callable);
  925. disconnect_data.pop_front();
  926. }
  927. return err;
  928. }
  929. Error Object::emit_signal(const StringName &p_name, VARIANT_ARG_DECLARE) {
  930. VARIANT_ARGPTRS;
  931. int argc = 0;
  932. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  933. if (argptr[i]->get_type() == Variant::NIL) {
  934. break;
  935. }
  936. argc++;
  937. }
  938. return emit_signal(p_name, argptr, argc);
  939. }
  940. void Object::_add_user_signal(const String &p_name, const Array &p_args) {
  941. // this version of add_user_signal is meant to be used from scripts or external apis
  942. // without access to ADD_SIGNAL in bind_methods
  943. // added events are per instance, as opposed to the other ones, which are global
  944. MethodInfo mi;
  945. mi.name = p_name;
  946. for (int i = 0; i < p_args.size(); i++) {
  947. Dictionary d = p_args[i];
  948. PropertyInfo param;
  949. if (d.has("name")) {
  950. param.name = d["name"];
  951. }
  952. if (d.has("type")) {
  953. param.type = (Variant::Type)(int)d["type"];
  954. }
  955. mi.arguments.push_back(param);
  956. }
  957. add_user_signal(mi);
  958. }
  959. Array Object::_get_signal_list() const {
  960. List<MethodInfo> signal_list;
  961. get_signal_list(&signal_list);
  962. Array ret;
  963. for (List<MethodInfo>::Element *E = signal_list.front(); E; E = E->next()) {
  964. ret.push_back(Dictionary(E->get()));
  965. }
  966. return ret;
  967. }
  968. Array Object::_get_signal_connection_list(const String &p_signal) const {
  969. List<Connection> conns;
  970. get_all_signal_connections(&conns);
  971. Array ret;
  972. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  973. Connection &c = E->get();
  974. if (c.signal.get_name() == p_signal) {
  975. ret.push_back(c);
  976. }
  977. }
  978. return ret;
  979. }
  980. Array Object::_get_incoming_connections() const {
  981. Array ret;
  982. int connections_amount = connections.size();
  983. for (int idx_conn = 0; idx_conn < connections_amount; idx_conn++) {
  984. ret.push_back(connections[idx_conn]);
  985. }
  986. return ret;
  987. }
  988. bool Object::has_signal(const StringName &p_name) const {
  989. if (!script.is_null()) {
  990. Ref<Script> scr = script;
  991. if (scr.is_valid() && scr->has_script_signal(p_name)) {
  992. return true;
  993. }
  994. }
  995. if (ClassDB::has_signal(get_class_name(), p_name)) {
  996. return true;
  997. }
  998. if (_has_user_signal(p_name)) {
  999. return true;
  1000. }
  1001. return false;
  1002. }
  1003. void Object::get_signal_list(List<MethodInfo> *p_signals) const {
  1004. if (!script.is_null()) {
  1005. Ref<Script> scr = script;
  1006. if (scr.is_valid()) {
  1007. scr->get_script_signal_list(p_signals);
  1008. }
  1009. }
  1010. ClassDB::get_signal_list(get_class_name(), p_signals);
  1011. //find maybe usersignals?
  1012. const StringName *S = nullptr;
  1013. while ((S = signal_map.next(S))) {
  1014. if (signal_map[*S].user.name != "") {
  1015. //user signal
  1016. p_signals->push_back(signal_map[*S].user);
  1017. }
  1018. }
  1019. }
  1020. void Object::get_all_signal_connections(List<Connection> *p_connections) const {
  1021. const StringName *S = nullptr;
  1022. while ((S = signal_map.next(S))) {
  1023. const SignalData *s = &signal_map[*S];
  1024. for (int i = 0; i < s->slot_map.size(); i++) {
  1025. p_connections->push_back(s->slot_map.getv(i).conn);
  1026. }
  1027. }
  1028. }
  1029. void Object::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
  1030. const SignalData *s = signal_map.getptr(p_signal);
  1031. if (!s) {
  1032. return; //nothing
  1033. }
  1034. for (int i = 0; i < s->slot_map.size(); i++) {
  1035. p_connections->push_back(s->slot_map.getv(i).conn);
  1036. }
  1037. }
  1038. int Object::get_persistent_signal_connection_count() const {
  1039. int count = 0;
  1040. const StringName *S = nullptr;
  1041. while ((S = signal_map.next(S))) {
  1042. const SignalData *s = &signal_map[*S];
  1043. for (int i = 0; i < s->slot_map.size(); i++) {
  1044. if (s->slot_map.getv(i).conn.flags & CONNECT_PERSIST) {
  1045. count += 1;
  1046. }
  1047. }
  1048. }
  1049. return count;
  1050. }
  1051. void Object::get_signals_connected_to_this(List<Connection> *p_connections) const {
  1052. for (const List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1053. p_connections->push_back(E->get());
  1054. }
  1055. }
  1056. Error Object::connect_compat(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method, const Vector<Variant> &p_binds, uint32_t p_flags) {
  1057. return connect(p_signal, Callable(p_to_object, p_to_method), p_binds, p_flags);
  1058. }
  1059. Error Object::connect(const StringName &p_signal, const Callable &p_callable, const Vector<Variant> &p_binds, uint32_t p_flags) {
  1060. ERR_FAIL_COND_V(p_callable.is_null(), ERR_INVALID_PARAMETER);
  1061. Object *target_object = p_callable.get_object();
  1062. ERR_FAIL_COND_V(!target_object, ERR_INVALID_PARAMETER);
  1063. SignalData *s = signal_map.getptr(p_signal);
  1064. if (!s) {
  1065. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1066. //check in script
  1067. if (!signal_is_valid && !script.is_null()) {
  1068. if (Ref<Script>(script)->has_script_signal(p_signal)) {
  1069. signal_is_valid = true;
  1070. }
  1071. #ifdef TOOLS_ENABLED
  1072. else {
  1073. //allow connecting signals anyway if script is invalid, see issue #17070
  1074. if (!Ref<Script>(script)->is_valid()) {
  1075. signal_is_valid = true;
  1076. }
  1077. }
  1078. #endif
  1079. }
  1080. ERR_FAIL_COND_V_MSG(!signal_is_valid, ERR_INVALID_PARAMETER, "In Object of type '" + String(get_class()) + "': Attempt to connect nonexistent signal '" + p_signal + "' to callable '" + p_callable + "'.");
  1081. signal_map[p_signal] = SignalData();
  1082. s = &signal_map[p_signal];
  1083. }
  1084. Callable target = p_callable;
  1085. //compare with the base callable, so binds can be ignored
  1086. if (s->slot_map.has(*target.get_base_comparator())) {
  1087. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1088. s->slot_map[*target.get_base_comparator()].reference_count++;
  1089. return OK;
  1090. } else {
  1091. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Signal '" + p_signal + "' is already connected to given callable '" + p_callable + "' in that object.");
  1092. }
  1093. }
  1094. SignalData::Slot slot;
  1095. Connection conn;
  1096. conn.callable = target;
  1097. conn.signal = ::Signal(this, p_signal);
  1098. conn.flags = p_flags;
  1099. conn.binds = p_binds;
  1100. slot.conn = conn;
  1101. slot.cE = target_object->connections.push_back(conn);
  1102. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1103. slot.reference_count = 1;
  1104. }
  1105. //use callable version as key, so binds can be ignored
  1106. s->slot_map[*target.get_base_comparator()] = slot;
  1107. return OK;
  1108. }
  1109. bool Object::is_connected_compat(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method) const {
  1110. return is_connected(p_signal, Callable(p_to_object, p_to_method));
  1111. }
  1112. bool Object::is_connected(const StringName &p_signal, const Callable &p_callable) const {
  1113. ERR_FAIL_COND_V(p_callable.is_null(), false);
  1114. const SignalData *s = signal_map.getptr(p_signal);
  1115. if (!s) {
  1116. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1117. if (signal_is_valid) {
  1118. return false;
  1119. }
  1120. if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal)) {
  1121. return false;
  1122. }
  1123. ERR_FAIL_V_MSG(false, "Nonexistent signal: " + p_signal + ".");
  1124. }
  1125. Callable target = p_callable;
  1126. return s->slot_map.has(*target.get_base_comparator());
  1127. //const Map<Signal::Target,Signal::Slot>::Element *E = s->slot_map.find(target);
  1128. //return (E!=nullptr );
  1129. }
  1130. void Object::disconnect_compat(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method) {
  1131. _disconnect(p_signal, Callable(p_to_object, p_to_method));
  1132. }
  1133. void Object::disconnect(const StringName &p_signal, const Callable &p_callable) {
  1134. _disconnect(p_signal, p_callable);
  1135. }
  1136. void Object::_disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force) {
  1137. ERR_FAIL_COND(p_callable.is_null());
  1138. Object *target_object = p_callable.get_object();
  1139. ERR_FAIL_COND(!target_object);
  1140. SignalData *s = signal_map.getptr(p_signal);
  1141. ERR_FAIL_COND_MSG(!s, vformat("Nonexistent signal '%s' in %s.", p_signal, to_string()));
  1142. ERR_FAIL_COND_MSG(!s->slot_map.has(*p_callable.get_base_comparator()), "Disconnecting nonexistent signal '" + p_signal + "', callable: " + p_callable + ".");
  1143. SignalData::Slot *slot = &s->slot_map[p_callable];
  1144. if (!p_force) {
  1145. slot->reference_count--; // by default is zero, if it was not referenced it will go below it
  1146. if (slot->reference_count >= 0) {
  1147. return;
  1148. }
  1149. }
  1150. target_object->connections.erase(slot->cE);
  1151. s->slot_map.erase(*p_callable.get_base_comparator());
  1152. if (s->slot_map.empty() && ClassDB::has_signal(get_class_name(), p_signal)) {
  1153. //not user signal, delete
  1154. signal_map.erase(p_signal);
  1155. }
  1156. }
  1157. void Object::_set_bind(const String &p_set, const Variant &p_value) {
  1158. set(p_set, p_value);
  1159. }
  1160. Variant Object::_get_bind(const String &p_name) const {
  1161. return get(p_name);
  1162. }
  1163. void Object::_set_indexed_bind(const NodePath &p_name, const Variant &p_value) {
  1164. set_indexed(p_name.get_as_property_path().get_subnames(), p_value);
  1165. }
  1166. Variant Object::_get_indexed_bind(const NodePath &p_name) const {
  1167. return get_indexed(p_name.get_as_property_path().get_subnames());
  1168. }
  1169. void Object::initialize_class() {
  1170. static bool initialized = false;
  1171. if (initialized) {
  1172. return;
  1173. }
  1174. ClassDB::_add_class<Object>();
  1175. _bind_methods();
  1176. initialized = true;
  1177. }
  1178. String Object::tr(const StringName &p_message, const StringName &p_context) const {
  1179. if (!_can_translate || !TranslationServer::get_singleton()) {
  1180. return p_message;
  1181. }
  1182. return TranslationServer::get_singleton()->translate(p_message, p_context);
  1183. }
  1184. String Object::tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  1185. if (!_can_translate || !TranslationServer::get_singleton()) {
  1186. // Return message based on English plural rule if translation is not possible.
  1187. if (p_n == 1) {
  1188. return p_message;
  1189. }
  1190. return p_message_plural;
  1191. }
  1192. return TranslationServer::get_singleton()->translate_plural(p_message, p_message_plural, p_n, p_context);
  1193. }
  1194. void Object::_clear_internal_resource_paths(const Variant &p_var) {
  1195. switch (p_var.get_type()) {
  1196. case Variant::OBJECT: {
  1197. RES r = p_var;
  1198. if (!r.is_valid()) {
  1199. return;
  1200. }
  1201. if (!r->get_path().begins_with("res://") || r->get_path().find("::") == -1) {
  1202. return; //not an internal resource
  1203. }
  1204. Object *object = p_var;
  1205. if (!object) {
  1206. return;
  1207. }
  1208. r->set_path("");
  1209. r->clear_internal_resource_paths();
  1210. } break;
  1211. case Variant::ARRAY: {
  1212. Array a = p_var;
  1213. for (int i = 0; i < a.size(); i++) {
  1214. _clear_internal_resource_paths(a[i]);
  1215. }
  1216. } break;
  1217. case Variant::DICTIONARY: {
  1218. Dictionary d = p_var;
  1219. List<Variant> keys;
  1220. d.get_key_list(&keys);
  1221. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1222. _clear_internal_resource_paths(E->get());
  1223. _clear_internal_resource_paths(d[E->get()]);
  1224. }
  1225. } break;
  1226. default: {
  1227. }
  1228. }
  1229. }
  1230. #ifdef TOOLS_ENABLED
  1231. void Object::editor_set_section_unfold(const String &p_section, bool p_unfolded) {
  1232. set_edited(true);
  1233. if (p_unfolded) {
  1234. editor_section_folding.insert(p_section);
  1235. } else {
  1236. editor_section_folding.erase(p_section);
  1237. }
  1238. }
  1239. bool Object::editor_is_section_unfolded(const String &p_section) {
  1240. return editor_section_folding.has(p_section);
  1241. }
  1242. #endif
  1243. void Object::clear_internal_resource_paths() {
  1244. List<PropertyInfo> pinfo;
  1245. get_property_list(&pinfo);
  1246. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1247. _clear_internal_resource_paths(get(E->get().name));
  1248. }
  1249. }
  1250. void Object::_bind_methods() {
  1251. ClassDB::bind_method(D_METHOD("get_class"), &Object::get_class);
  1252. ClassDB::bind_method(D_METHOD("is_class", "class"), &Object::is_class);
  1253. ClassDB::bind_method(D_METHOD("set", "property", "value"), &Object::_set_bind);
  1254. ClassDB::bind_method(D_METHOD("get", "property"), &Object::_get_bind);
  1255. ClassDB::bind_method(D_METHOD("set_indexed", "property", "value"), &Object::_set_indexed_bind);
  1256. ClassDB::bind_method(D_METHOD("get_indexed", "property"), &Object::_get_indexed_bind);
  1257. ClassDB::bind_method(D_METHOD("get_property_list"), &Object::_get_property_list_bind);
  1258. ClassDB::bind_method(D_METHOD("get_method_list"), &Object::_get_method_list_bind);
  1259. ClassDB::bind_method(D_METHOD("notification", "what", "reversed"), &Object::notification, DEFVAL(false));
  1260. ClassDB::bind_method(D_METHOD("to_string"), &Object::to_string);
  1261. ClassDB::bind_method(D_METHOD("get_instance_id"), &Object::get_instance_id);
  1262. ClassDB::bind_method(D_METHOD("set_script", "script"), &Object::set_script);
  1263. ClassDB::bind_method(D_METHOD("get_script"), &Object::get_script);
  1264. ClassDB::bind_method(D_METHOD("set_meta", "name", "value"), &Object::set_meta);
  1265. ClassDB::bind_method(D_METHOD("remove_meta", "name"), &Object::remove_meta);
  1266. ClassDB::bind_method(D_METHOD("get_meta", "name"), &Object::get_meta);
  1267. ClassDB::bind_method(D_METHOD("has_meta", "name"), &Object::has_meta);
  1268. ClassDB::bind_method(D_METHOD("get_meta_list"), &Object::_get_meta_list_bind);
  1269. ClassDB::bind_method(D_METHOD("add_user_signal", "signal", "arguments"), &Object::_add_user_signal, DEFVAL(Array()));
  1270. ClassDB::bind_method(D_METHOD("has_user_signal", "signal"), &Object::_has_user_signal);
  1271. {
  1272. MethodInfo mi;
  1273. mi.name = "emit_signal";
  1274. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "signal"));
  1275. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "emit_signal", &Object::_emit_signal, mi, varray(), false);
  1276. }
  1277. {
  1278. MethodInfo mi;
  1279. mi.name = "call";
  1280. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1281. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call", &Object::_call_bind, mi);
  1282. }
  1283. {
  1284. MethodInfo mi;
  1285. mi.name = "call_deferred";
  1286. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1287. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_deferred", &Object::_call_deferred_bind, mi, varray(), false);
  1288. }
  1289. ClassDB::bind_method(D_METHOD("set_deferred", "property", "value"), &Object::set_deferred);
  1290. ClassDB::bind_method(D_METHOD("callv", "method", "arg_array"), &Object::callv);
  1291. ClassDB::bind_method(D_METHOD("has_method", "method"), &Object::has_method);
  1292. ClassDB::bind_method(D_METHOD("has_signal", "signal"), &Object::has_signal);
  1293. ClassDB::bind_method(D_METHOD("get_signal_list"), &Object::_get_signal_list);
  1294. ClassDB::bind_method(D_METHOD("get_signal_connection_list", "signal"), &Object::_get_signal_connection_list);
  1295. ClassDB::bind_method(D_METHOD("get_incoming_connections"), &Object::_get_incoming_connections);
  1296. ClassDB::bind_method(D_METHOD("connect", "signal", "callable", "binds", "flags"), &Object::connect, DEFVAL(Array()), DEFVAL(0));
  1297. ClassDB::bind_method(D_METHOD("disconnect", "signal", "callable"), &Object::disconnect);
  1298. ClassDB::bind_method(D_METHOD("is_connected", "signal", "callable"), &Object::is_connected);
  1299. ClassDB::bind_method(D_METHOD("set_block_signals", "enable"), &Object::set_block_signals);
  1300. ClassDB::bind_method(D_METHOD("is_blocking_signals"), &Object::is_blocking_signals);
  1301. ClassDB::bind_method(D_METHOD("property_list_changed_notify"), &Object::property_list_changed_notify);
  1302. ClassDB::bind_method(D_METHOD("set_message_translation", "enable"), &Object::set_message_translation);
  1303. ClassDB::bind_method(D_METHOD("can_translate_messages"), &Object::can_translate_messages);
  1304. ClassDB::bind_method(D_METHOD("tr", "message", "context"), &Object::tr, DEFVAL(""));
  1305. ClassDB::bind_method(D_METHOD("tr_n", "message", "plural_message", "n", "context"), &Object::tr_n, DEFVAL(""));
  1306. ClassDB::bind_method(D_METHOD("is_queued_for_deletion"), &Object::is_queued_for_deletion);
  1307. ClassDB::add_virtual_method("Object", MethodInfo("free"), false);
  1308. ADD_SIGNAL(MethodInfo("script_changed"));
  1309. BIND_VMETHOD(MethodInfo("_notification", PropertyInfo(Variant::INT, "what")));
  1310. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_set", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value")));
  1311. #ifdef TOOLS_ENABLED
  1312. MethodInfo miget("_get", PropertyInfo(Variant::STRING_NAME, "property"));
  1313. miget.return_val.name = "Variant";
  1314. miget.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1315. BIND_VMETHOD(miget);
  1316. MethodInfo plget("_get_property_list");
  1317. plget.return_val.type = Variant::ARRAY;
  1318. BIND_VMETHOD(plget);
  1319. #endif
  1320. BIND_VMETHOD(MethodInfo("_init"));
  1321. BIND_VMETHOD(MethodInfo(Variant::STRING, "_to_string"));
  1322. BIND_CONSTANT(NOTIFICATION_POSTINITIALIZE);
  1323. BIND_CONSTANT(NOTIFICATION_PREDELETE);
  1324. BIND_ENUM_CONSTANT(CONNECT_DEFERRED);
  1325. BIND_ENUM_CONSTANT(CONNECT_PERSIST);
  1326. BIND_ENUM_CONSTANT(CONNECT_ONESHOT);
  1327. BIND_ENUM_CONSTANT(CONNECT_REFERENCE_COUNTED);
  1328. }
  1329. void Object::call_deferred(const StringName &p_method, VARIANT_ARG_DECLARE) {
  1330. MessageQueue::get_singleton()->push_call(this, p_method, VARIANT_ARG_PASS);
  1331. }
  1332. void Object::set_deferred(const StringName &p_property, const Variant &p_value) {
  1333. MessageQueue::get_singleton()->push_set(this, p_property, p_value);
  1334. }
  1335. void Object::set_block_signals(bool p_block) {
  1336. _block_signals = p_block;
  1337. }
  1338. bool Object::is_blocking_signals() const {
  1339. return _block_signals;
  1340. }
  1341. void Object::get_translatable_strings(List<String> *p_strings) const {
  1342. List<PropertyInfo> plist;
  1343. get_property_list(&plist);
  1344. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1345. if (!(E->get().usage & PROPERTY_USAGE_INTERNATIONALIZED)) {
  1346. continue;
  1347. }
  1348. String text = get(E->get().name);
  1349. if (text == "") {
  1350. continue;
  1351. }
  1352. p_strings->push_back(text);
  1353. }
  1354. }
  1355. Variant::Type Object::get_static_property_type(const StringName &p_property, bool *r_valid) const {
  1356. bool valid;
  1357. Variant::Type t = ClassDB::get_property_type(get_class_name(), p_property, &valid);
  1358. if (valid) {
  1359. if (r_valid) {
  1360. *r_valid = true;
  1361. }
  1362. return t;
  1363. }
  1364. if (get_script_instance()) {
  1365. return get_script_instance()->get_property_type(p_property, r_valid);
  1366. }
  1367. if (r_valid) {
  1368. *r_valid = false;
  1369. }
  1370. return Variant::NIL;
  1371. }
  1372. Variant::Type Object::get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid) const {
  1373. if (p_path.size() == 0) {
  1374. if (r_valid) {
  1375. *r_valid = false;
  1376. }
  1377. return Variant::NIL;
  1378. }
  1379. bool valid = false;
  1380. Variant::Type t = get_static_property_type(p_path[0], &valid);
  1381. if (!valid) {
  1382. if (r_valid) {
  1383. *r_valid = false;
  1384. }
  1385. return Variant::NIL;
  1386. }
  1387. Callable::CallError ce;
  1388. Variant check = Variant::construct(t, nullptr, 0, ce);
  1389. for (int i = 1; i < p_path.size(); i++) {
  1390. if (check.get_type() == Variant::OBJECT || check.get_type() == Variant::DICTIONARY || check.get_type() == Variant::ARRAY) {
  1391. // We cannot be sure about the type of properties this types can have
  1392. if (r_valid) {
  1393. *r_valid = false;
  1394. }
  1395. return Variant::NIL;
  1396. }
  1397. check = check.get_named(p_path[i], &valid);
  1398. if (!valid) {
  1399. if (r_valid) {
  1400. *r_valid = false;
  1401. }
  1402. return Variant::NIL;
  1403. }
  1404. }
  1405. if (r_valid) {
  1406. *r_valid = true;
  1407. }
  1408. return check.get_type();
  1409. }
  1410. bool Object::is_queued_for_deletion() const {
  1411. return _is_queued_for_deletion;
  1412. }
  1413. #ifdef TOOLS_ENABLED
  1414. void Object::set_edited(bool p_edited) {
  1415. _edited = p_edited;
  1416. _edited_version++;
  1417. }
  1418. bool Object::is_edited() const {
  1419. return _edited;
  1420. }
  1421. uint32_t Object::get_edited_version() const {
  1422. return _edited_version;
  1423. }
  1424. #endif
  1425. void *Object::get_script_instance_binding(int p_script_language_index) {
  1426. #ifdef DEBUG_ENABLED
  1427. ERR_FAIL_INDEX_V(p_script_language_index, MAX_SCRIPT_INSTANCE_BINDINGS, nullptr);
  1428. #endif
  1429. //it's up to the script language to make this thread safe, if the function is called twice due to threads being out of syncro
  1430. //just return the same pointer.
  1431. //if you want to put a big lock in the entire function and keep allocated pointers in a map or something, feel free to do it
  1432. //as it should not really affect performance much (won't be called too often), as in far most caes the condition below will be false afterwards
  1433. if (!_script_instance_bindings[p_script_language_index]) {
  1434. void *script_data = ScriptServer::get_language(p_script_language_index)->alloc_instance_binding_data(this);
  1435. if (script_data) {
  1436. atomic_increment(&instance_binding_count);
  1437. _script_instance_bindings[p_script_language_index] = script_data;
  1438. }
  1439. }
  1440. return _script_instance_bindings[p_script_language_index];
  1441. }
  1442. bool Object::has_script_instance_binding(int p_script_language_index) {
  1443. return _script_instance_bindings[p_script_language_index] != nullptr;
  1444. }
  1445. void Object::set_script_instance_binding(int p_script_language_index, void *p_data) {
  1446. #ifdef DEBUG_ENABLED
  1447. CRASH_COND(_script_instance_bindings[p_script_language_index] != nullptr);
  1448. #endif
  1449. _script_instance_bindings[p_script_language_index] = p_data;
  1450. }
  1451. void Object::_construct_object(bool p_reference) {
  1452. type_is_reference = p_reference;
  1453. _instance_id = ObjectDB::add_instance(this);
  1454. memset(_script_instance_bindings, 0, sizeof(void *) * MAX_SCRIPT_INSTANCE_BINDINGS);
  1455. #ifdef DEBUG_ENABLED
  1456. _lock_index.init(1);
  1457. #endif
  1458. }
  1459. Object::Object(bool p_reference) {
  1460. _construct_object(p_reference);
  1461. }
  1462. Object::Object() {
  1463. _construct_object(false);
  1464. }
  1465. Object::~Object() {
  1466. if (script_instance) {
  1467. memdelete(script_instance);
  1468. }
  1469. script_instance = nullptr;
  1470. const StringName *S = nullptr;
  1471. if (_emitting) {
  1472. //@todo this may need to actually reach the debugger prioritarily somehow because it may crash before
  1473. ERR_PRINT("Object " + to_string() + " 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.");
  1474. }
  1475. while ((S = signal_map.next(nullptr))) {
  1476. SignalData *s = &signal_map[*S];
  1477. //brute force disconnect for performance
  1478. int slot_count = s->slot_map.size();
  1479. const VMap<Callable, SignalData::Slot>::Pair *slot_list = s->slot_map.get_array();
  1480. for (int i = 0; i < slot_count; i++) {
  1481. slot_list[i].value.conn.callable.get_object()->connections.erase(slot_list[i].value.cE);
  1482. }
  1483. signal_map.erase(*S);
  1484. }
  1485. //signals from nodes that connect to this node
  1486. while (connections.size()) {
  1487. Connection c = connections.front()->get();
  1488. c.signal.get_object()->_disconnect(c.signal.get_name(), c.callable, true);
  1489. }
  1490. ObjectDB::remove_instance(this);
  1491. _instance_id = ObjectID();
  1492. _predelete_ok = 2;
  1493. if (!ScriptServer::are_languages_finished()) {
  1494. for (int i = 0; i < MAX_SCRIPT_INSTANCE_BINDINGS; i++) {
  1495. if (_script_instance_bindings[i]) {
  1496. ScriptServer::get_language(i)->free_instance_binding_data(_script_instance_bindings[i]);
  1497. }
  1498. }
  1499. }
  1500. }
  1501. bool predelete_handler(Object *p_object) {
  1502. return p_object->_predelete();
  1503. }
  1504. void postinitialize_handler(Object *p_object) {
  1505. p_object->_postinitialize();
  1506. }
  1507. void ObjectDB::debug_objects(DebugFunc p_func) {
  1508. spin_lock.lock();
  1509. for (uint32_t i = 0; i < slot_count; i++) {
  1510. uint32_t slot = object_slots[i].next_free;
  1511. p_func(object_slots[slot].object);
  1512. }
  1513. spin_lock.unlock();
  1514. }
  1515. void Object::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1516. }
  1517. SpinLock ObjectDB::spin_lock;
  1518. uint32_t ObjectDB::slot_count = 0;
  1519. uint32_t ObjectDB::slot_max = 0;
  1520. ObjectDB::ObjectSlot *ObjectDB::object_slots = nullptr;
  1521. uint64_t ObjectDB::validator_counter = 0;
  1522. int ObjectDB::get_object_count() {
  1523. return slot_count;
  1524. }
  1525. ObjectID ObjectDB::add_instance(Object *p_object) {
  1526. spin_lock.lock();
  1527. if (unlikely(slot_count == slot_max)) {
  1528. CRASH_COND(slot_count == (1 << OBJECTDB_SLOT_MAX_COUNT_BITS));
  1529. uint32_t new_slot_max = slot_max > 0 ? slot_max * 2 : 1;
  1530. object_slots = (ObjectSlot *)memrealloc(object_slots, sizeof(ObjectSlot) * new_slot_max);
  1531. for (uint32_t i = slot_max; i < new_slot_max; i++) {
  1532. object_slots[i].object = nullptr;
  1533. object_slots[i].is_reference = false;
  1534. object_slots[i].next_free = i;
  1535. object_slots[i].validator = 0;
  1536. }
  1537. slot_max = new_slot_max;
  1538. }
  1539. uint32_t slot = object_slots[slot_count].next_free;
  1540. if (object_slots[slot].object != nullptr) {
  1541. spin_lock.unlock();
  1542. ERR_FAIL_COND_V(object_slots[slot].object != nullptr, ObjectID());
  1543. }
  1544. object_slots[slot].object = p_object;
  1545. object_slots[slot].is_reference = p_object->is_reference();
  1546. validator_counter = (validator_counter + 1) & OBJECTDB_VALIDATOR_MASK;
  1547. if (unlikely(validator_counter == 0)) {
  1548. validator_counter = 1;
  1549. }
  1550. object_slots[slot].validator = validator_counter;
  1551. uint64_t id = validator_counter;
  1552. id <<= OBJECTDB_SLOT_MAX_COUNT_BITS;
  1553. id |= uint64_t(slot);
  1554. if (p_object->is_reference()) {
  1555. id |= OBJECTDB_REFERENCE_BIT;
  1556. }
  1557. slot_count++;
  1558. spin_lock.unlock();
  1559. return ObjectID(id);
  1560. }
  1561. void ObjectDB::remove_instance(Object *p_object) {
  1562. uint64_t t = p_object->get_instance_id();
  1563. uint32_t slot = t & OBJECTDB_SLOT_MAX_COUNT_MASK; //slot is always valid on valid object
  1564. spin_lock.lock();
  1565. #ifdef DEBUG_ENABLED
  1566. if (object_slots[slot].object != p_object) {
  1567. spin_lock.unlock();
  1568. ERR_FAIL_COND(object_slots[slot].object != p_object);
  1569. }
  1570. {
  1571. uint64_t validator = (t >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
  1572. if (object_slots[slot].validator != validator) {
  1573. spin_lock.unlock();
  1574. ERR_FAIL_COND(object_slots[slot].validator != validator);
  1575. }
  1576. }
  1577. #endif
  1578. //decrease slot count
  1579. slot_count--;
  1580. //set the free slot properly
  1581. object_slots[slot_count].next_free = slot;
  1582. //invalidate, so checks against it fail
  1583. object_slots[slot].validator = 0;
  1584. object_slots[slot].is_reference = false;
  1585. object_slots[slot].object = nullptr;
  1586. spin_lock.unlock();
  1587. }
  1588. void ObjectDB::setup() {
  1589. //nothing to do now
  1590. }
  1591. void ObjectDB::cleanup() {
  1592. if (slot_count > 0) {
  1593. spin_lock.lock();
  1594. WARN_PRINT("ObjectDB instances leaked at exit (run with --verbose for details).");
  1595. if (OS::get_singleton()->is_stdout_verbose()) {
  1596. // Ensure calling the native classes because if a leaked instance has a script
  1597. // that overrides any of those methods, it'd not be OK to call them at this point,
  1598. // now the scripting languages have already been terminated.
  1599. MethodBind *node_get_name = ClassDB::get_method("Node", "get_name");
  1600. MethodBind *resource_get_path = ClassDB::get_method("Resource", "get_path");
  1601. Callable::CallError call_error;
  1602. for (uint32_t i = 0; i < slot_count; i++) {
  1603. uint32_t slot = object_slots[i].next_free;
  1604. Object *obj = object_slots[slot].object;
  1605. String extra_info;
  1606. if (obj->is_class("Node")) {
  1607. extra_info = " - Node name: " + String(node_get_name->call(obj, nullptr, 0, call_error));
  1608. }
  1609. if (obj->is_class("Resource")) {
  1610. extra_info = " - Resource path: " + String(resource_get_path->call(obj, nullptr, 0, call_error));
  1611. }
  1612. uint64_t id = uint64_t(slot) | (uint64_t(object_slots[slot].validator) << OBJECTDB_VALIDATOR_BITS) | (object_slots[slot].is_reference ? OBJECTDB_REFERENCE_BIT : 0);
  1613. print_line("Leaked instance: " + String(obj->get_class()) + ":" + itos(id) + extra_info);
  1614. }
  1615. 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()`).");
  1616. }
  1617. spin_lock.unlock();
  1618. }
  1619. if (object_slots) {
  1620. memfree(object_slots);
  1621. }
  1622. }