object.cpp 56 KB

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