object.cpp 56 KB

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