object.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  1. /*************************************************************************/
  2. /* object.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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["source"] = source;
  286. d["signal"] = signal;
  287. d["target"] = target;
  288. d["method"] = method;
  289. d["flags"] = flags;
  290. d["binds"] = binds;
  291. return d;
  292. }
  293. bool Object::Connection::operator<(const Connection &p_conn) const {
  294. if (source == p_conn.source) {
  295. if (signal == p_conn.signal) {
  296. if (target == p_conn.target) {
  297. return method < p_conn.method;
  298. } else {
  299. return target < p_conn.target;
  300. }
  301. } else
  302. return signal < p_conn.signal;
  303. } else {
  304. return source < p_conn.source;
  305. }
  306. }
  307. Object::Connection::Connection(const Variant &p_variant) {
  308. Dictionary d = p_variant;
  309. if (d.has("source"))
  310. source = d["source"];
  311. if (d.has("signal"))
  312. signal = d["signal"];
  313. if (d.has("target"))
  314. target = d["target"];
  315. if (d.has("method"))
  316. method = d["method"];
  317. if (d.has("flags"))
  318. flags = d["flags"];
  319. if (d.has("binds"))
  320. binds = d["binds"];
  321. }
  322. bool Object::_predelete() {
  323. _predelete_ok = 1;
  324. notification(NOTIFICATION_PREDELETE, true);
  325. if (_predelete_ok) {
  326. _class_ptr = NULL; //must restore so destructors can access class ptr correctly
  327. }
  328. return _predelete_ok;
  329. }
  330. void Object::_postinitialize() {
  331. _class_ptr = _get_class_namev();
  332. _initialize_classv();
  333. notification(NOTIFICATION_POSTINITIALIZE);
  334. }
  335. void Object::get_valid_parents_static(List<String> *p_parents) {
  336. }
  337. void Object::_get_valid_parents_static(List<String> *p_parents) {
  338. }
  339. void Object::set(const StringName &p_name, const Variant &p_value, bool *r_valid) {
  340. #ifdef TOOLS_ENABLED
  341. _edited = true;
  342. #endif
  343. if (script_instance) {
  344. if (script_instance->set(p_name, p_value)) {
  345. if (r_valid)
  346. *r_valid = true;
  347. return;
  348. }
  349. }
  350. //try built-in setgetter
  351. {
  352. if (ClassDB::set_property(this, p_name, p_value, r_valid)) {
  353. /*
  354. if (r_valid)
  355. *r_valid=true;
  356. */
  357. return;
  358. }
  359. }
  360. if (p_name == CoreStringNames::get_singleton()->_script) {
  361. set_script(p_value);
  362. if (r_valid)
  363. *r_valid = true;
  364. return;
  365. } else if (p_name == CoreStringNames::get_singleton()->_meta) {
  366. //set_meta(p_name,p_value);
  367. metadata = p_value;
  368. if (r_valid)
  369. *r_valid = true;
  370. return;
  371. }
  372. //something inside the object... :|
  373. bool success = _setv(p_name, p_value);
  374. if (success) {
  375. if (r_valid)
  376. *r_valid = true;
  377. return;
  378. }
  379. {
  380. bool valid;
  381. setvar(p_name, p_value, &valid);
  382. if (valid) {
  383. if (r_valid)
  384. *r_valid = true;
  385. return;
  386. }
  387. }
  388. #ifdef TOOLS_ENABLED
  389. if (script_instance) {
  390. bool valid;
  391. script_instance->property_set_fallback(p_name, p_value, &valid);
  392. if (valid) {
  393. if (r_valid)
  394. *r_valid = true;
  395. return;
  396. }
  397. }
  398. #endif
  399. if (r_valid)
  400. *r_valid = false;
  401. return;
  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]);
  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. if (r_valid)
  514. *r_valid = false;
  515. return Variant();
  516. }
  517. }
  518. if (r_valid)
  519. *r_valid = true;
  520. return current_value;
  521. }
  522. void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) const {
  523. if (script_instance && p_reversed) {
  524. p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  525. script_instance->get_property_list(p_list);
  526. }
  527. _get_property_listv(p_list, p_reversed);
  528. if (!is_class("Script")) { // can still be set, but this is for userfriendlyness
  529. #ifdef TOOLS_ENABLED
  530. p_list->push_back(PropertyInfo(Variant::NIL, "Script", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  531. #endif
  532. p_list->push_back(PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT));
  533. }
  534. if (!metadata.empty()) {
  535. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "__meta__", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  536. }
  537. if (script_instance && !p_reversed) {
  538. p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  539. script_instance->get_property_list(p_list);
  540. }
  541. }
  542. void Object::_validate_property(PropertyInfo &property) const {
  543. }
  544. void Object::get_method_list(List<MethodInfo> *p_list) const {
  545. ClassDB::get_method_list(get_class_name(), p_list);
  546. if (script_instance) {
  547. script_instance->get_method_list(p_list);
  548. }
  549. }
  550. Variant Object::_call_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  551. if (p_argcount < 1) {
  552. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  553. r_error.argument = 0;
  554. return Variant();
  555. }
  556. if (p_args[0]->get_type() != Variant::STRING) {
  557. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  558. r_error.argument = 0;
  559. r_error.expected = Variant::STRING;
  560. return Variant();
  561. }
  562. StringName method = *p_args[0];
  563. return call(method, &p_args[1], p_argcount - 1, r_error);
  564. }
  565. Variant Object::_call_deferred_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  566. if (p_argcount < 1) {
  567. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  568. r_error.argument = 0;
  569. return Variant();
  570. }
  571. if (p_args[0]->get_type() != Variant::STRING) {
  572. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  573. r_error.argument = 0;
  574. r_error.expected = Variant::STRING;
  575. return Variant();
  576. }
  577. r_error.error = Variant::CallError::CALL_OK;
  578. StringName method = *p_args[0];
  579. MessageQueue::get_singleton()->push_call(get_instance_id(), method, &p_args[1], p_argcount - 1);
  580. return Variant();
  581. }
  582. #ifdef DEBUG_ENABLED
  583. static void _test_call_error(const StringName &p_func, const Variant::CallError &error) {
  584. switch (error.error) {
  585. case Variant::CallError::CALL_OK:
  586. case Variant::CallError::CALL_ERROR_INVALID_METHOD:
  587. break;
  588. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  589. ERR_EXPLAIN("Error Calling Function: " + String(p_func) + " - Invalid type for argument " + itos(error.argument) + ", expected " + Variant::get_type_name(error.expected));
  590. ERR_FAIL();
  591. break;
  592. }
  593. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  594. ERR_EXPLAIN("Error Calling Function: " + String(p_func) + " - Too many arguments, expected " + itos(error.argument));
  595. ERR_FAIL();
  596. break;
  597. }
  598. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  599. ERR_EXPLAIN("Error Calling Function: " + String(p_func) + " - Too few arguments, expected " + itos(error.argument));
  600. ERR_FAIL();
  601. break;
  602. }
  603. case Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  604. break;
  605. }
  606. }
  607. #else
  608. #define _test_call_error(m_str, m_err)
  609. #endif
  610. void Object::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  611. if (p_method == CoreStringNames::get_singleton()->_free) {
  612. #ifdef DEBUG_ENABLED
  613. if (Object::cast_to<Reference>(this)) {
  614. ERR_EXPLAIN("Can't 'free' a reference.");
  615. ERR_FAIL();
  616. return;
  617. }
  618. if (_lock_index.get() > 1) {
  619. ERR_EXPLAIN("Object is locked and can't be freed.");
  620. ERR_FAIL();
  621. return;
  622. }
  623. #endif
  624. //must be here, must be before everything,
  625. memdelete(this);
  626. return;
  627. }
  628. //Variant ret;
  629. OBJ_DEBUG_LOCK
  630. Variant::CallError error;
  631. if (script_instance) {
  632. script_instance->call_multilevel(p_method, p_args, p_argcount);
  633. //_test_call_error(p_method,error);
  634. }
  635. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  636. if (method) {
  637. method->call(this, p_args, p_argcount, error);
  638. _test_call_error(p_method, error);
  639. }
  640. }
  641. void Object::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  642. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  643. Variant::CallError error;
  644. OBJ_DEBUG_LOCK
  645. if (method) {
  646. method->call(this, p_args, p_argcount, error);
  647. _test_call_error(p_method, error);
  648. }
  649. //Variant ret;
  650. if (script_instance) {
  651. script_instance->call_multilevel_reversed(p_method, p_args, p_argcount);
  652. //_test_call_error(p_method,error);
  653. }
  654. }
  655. bool Object::has_method(const StringName &p_method) const {
  656. if (p_method == CoreStringNames::get_singleton()->_free) {
  657. return true;
  658. }
  659. if (script_instance && script_instance->has_method(p_method)) {
  660. return true;
  661. }
  662. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  663. if (method) {
  664. return true;
  665. }
  666. return false;
  667. }
  668. Variant Object::getvar(const Variant &p_key, bool *r_valid) const {
  669. if (r_valid)
  670. *r_valid = false;
  671. return Variant();
  672. }
  673. void Object::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) {
  674. if (r_valid)
  675. *r_valid = false;
  676. }
  677. Variant Object::callv(const StringName &p_method, const Array &p_args) {
  678. const Variant **argptrs = NULL;
  679. if (p_args.size() > 0) {
  680. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_args.size());
  681. for (int i = 0; i < p_args.size(); i++) {
  682. argptrs[i] = &p_args[i];
  683. }
  684. }
  685. Variant::CallError ce;
  686. Variant ret = call(p_method, argptrs, p_args.size(), ce);
  687. if (ce.error != Variant::CallError::CALL_OK) {
  688. ERR_EXPLAIN("Error calling method from 'callv': " + Variant::get_call_error_text(this, p_method, argptrs, p_args.size(), ce));
  689. ERR_FAIL_V(Variant());
  690. }
  691. return ret;
  692. }
  693. Variant Object::call(const StringName &p_name, VARIANT_ARG_DECLARE) {
  694. VARIANT_ARGPTRS;
  695. int argc = 0;
  696. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  697. if (argptr[i]->get_type() == Variant::NIL)
  698. break;
  699. argc++;
  700. }
  701. Variant::CallError error;
  702. Variant ret = call(p_name, argptr, argc, error);
  703. return ret;
  704. }
  705. void Object::call_multilevel(const StringName &p_name, VARIANT_ARG_DECLARE) {
  706. VARIANT_ARGPTRS;
  707. int argc = 0;
  708. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  709. if (argptr[i]->get_type() == Variant::NIL)
  710. break;
  711. argc++;
  712. }
  713. //Variant::CallError error;
  714. call_multilevel(p_name, argptr, argc);
  715. }
  716. Variant Object::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  717. r_error.error = Variant::CallError::CALL_OK;
  718. if (p_method == CoreStringNames::get_singleton()->_free) {
  719. //free must be here, before anything, always ready
  720. #ifdef DEBUG_ENABLED
  721. if (p_argcount != 0) {
  722. r_error.argument = 0;
  723. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  724. return Variant();
  725. }
  726. if (Object::cast_to<Reference>(this)) {
  727. r_error.argument = 0;
  728. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  729. ERR_EXPLAIN("Can't 'free' a reference.");
  730. ERR_FAIL_V(Variant());
  731. }
  732. if (_lock_index.get() > 1) {
  733. r_error.argument = 0;
  734. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  735. ERR_EXPLAIN("Object is locked and can't be freed.");
  736. ERR_FAIL_V(Variant());
  737. }
  738. #endif
  739. //must be here, must be before everything,
  740. memdelete(this);
  741. r_error.error = Variant::CallError::CALL_OK;
  742. return Variant();
  743. }
  744. Variant ret;
  745. OBJ_DEBUG_LOCK
  746. if (script_instance) {
  747. ret = script_instance->call(p_method, p_args, p_argcount, r_error);
  748. //force jumptable
  749. switch (r_error.error) {
  750. case Variant::CallError::CALL_OK:
  751. return ret;
  752. case Variant::CallError::CALL_ERROR_INVALID_METHOD:
  753. break;
  754. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT:
  755. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  756. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  757. return ret;
  758. case Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL: {
  759. }
  760. }
  761. }
  762. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  763. if (method) {
  764. ret = method->call(this, p_args, p_argcount, r_error);
  765. } else {
  766. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  767. }
  768. return ret;
  769. }
  770. void Object::notification(int p_notification, bool p_reversed) {
  771. _notificationv(p_notification, p_reversed);
  772. if (script_instance) {
  773. script_instance->notification(p_notification);
  774. }
  775. }
  776. void Object::_changed_callback(Object *p_changed, const char *p_prop) {
  777. }
  778. void Object::add_change_receptor(Object *p_receptor) {
  779. change_receptors.insert(p_receptor);
  780. }
  781. void Object::remove_change_receptor(Object *p_receptor) {
  782. change_receptors.erase(p_receptor);
  783. }
  784. void Object::property_list_changed_notify() {
  785. _change_notify();
  786. }
  787. void Object::cancel_delete() {
  788. _predelete_ok = true;
  789. }
  790. void Object::set_script_and_instance(const RefPtr &p_script, ScriptInstance *p_instance) {
  791. //this function is not meant to be used in any of these ways
  792. ERR_FAIL_COND(p_script.is_null());
  793. ERR_FAIL_COND(!p_instance);
  794. ERR_FAIL_COND(script_instance != NULL || !script.is_null());
  795. script = p_script;
  796. script_instance = p_instance;
  797. }
  798. void Object::set_script(const RefPtr &p_script) {
  799. if (script == p_script)
  800. return;
  801. if (script_instance) {
  802. memdelete(script_instance);
  803. script_instance = NULL;
  804. }
  805. script = p_script;
  806. Ref<Script> s(script);
  807. if (!s.is_null()) {
  808. if (s->can_instance()) {
  809. OBJ_DEBUG_LOCK
  810. script_instance = s->instance_create(this);
  811. } else if (Engine::get_singleton()->is_editor_hint()) {
  812. OBJ_DEBUG_LOCK
  813. script_instance = s->placeholder_instance_create(this);
  814. }
  815. }
  816. _change_notify(); //scripts may add variables, so refresh is desired
  817. emit_signal(CoreStringNames::get_singleton()->script_changed);
  818. }
  819. void Object::set_script_instance(ScriptInstance *p_instance) {
  820. if (script_instance == p_instance)
  821. return;
  822. if (script_instance)
  823. memdelete(script_instance);
  824. script_instance = p_instance;
  825. if (p_instance)
  826. script = p_instance->get_script().get_ref_ptr();
  827. else
  828. script = RefPtr();
  829. }
  830. RefPtr Object::get_script() const {
  831. return script;
  832. }
  833. bool Object::has_meta(const String &p_name) const {
  834. return metadata.has(p_name);
  835. }
  836. void Object::set_meta(const String &p_name, const Variant &p_value) {
  837. if (p_value.get_type() == Variant::NIL) {
  838. metadata.erase(p_name);
  839. return;
  840. };
  841. metadata[p_name] = p_value;
  842. }
  843. Variant Object::get_meta(const String &p_name) const {
  844. ERR_FAIL_COND_V(!metadata.has(p_name), Variant());
  845. return metadata[p_name];
  846. }
  847. void Object::remove_meta(const String &p_name) {
  848. metadata.erase(p_name);
  849. }
  850. Array Object::_get_property_list_bind() const {
  851. List<PropertyInfo> lpi;
  852. get_property_list(&lpi);
  853. return convert_property_list(&lpi);
  854. }
  855. Array Object::_get_method_list_bind() const {
  856. List<MethodInfo> ml;
  857. get_method_list(&ml);
  858. Array ret;
  859. for (List<MethodInfo>::Element *E = ml.front(); E; E = E->next()) {
  860. Dictionary d = E->get();
  861. //va.push_back(d);
  862. ret.push_back(d);
  863. }
  864. return ret;
  865. }
  866. PoolVector<String> Object::_get_meta_list_bind() const {
  867. PoolVector<String> _metaret;
  868. List<Variant> keys;
  869. metadata.get_key_list(&keys);
  870. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  871. _metaret.push_back(E->get());
  872. }
  873. return _metaret;
  874. }
  875. void Object::get_meta_list(List<String> *p_list) const {
  876. List<Variant> keys;
  877. metadata.get_key_list(&keys);
  878. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  879. p_list->push_back(E->get());
  880. }
  881. }
  882. void Object::add_user_signal(const MethodInfo &p_signal) {
  883. ERR_FAIL_COND(p_signal.name == "");
  884. ERR_FAIL_COND(ClassDB::has_signal(get_class_name(), p_signal.name));
  885. ERR_FAIL_COND(signal_map.has(p_signal.name));
  886. Signal s;
  887. s.user = p_signal;
  888. signal_map[p_signal.name] = s;
  889. }
  890. bool Object::_has_user_signal(const StringName &p_name) const {
  891. if (!signal_map.has(p_name))
  892. return false;
  893. return signal_map[p_name].user.name.length() > 0;
  894. }
  895. struct _ObjectSignalDisconnectData {
  896. StringName signal;
  897. Object *target;
  898. StringName method;
  899. };
  900. Variant Object::_emit_signal(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  901. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  902. ERR_FAIL_COND_V(p_argcount < 1, Variant());
  903. if (p_args[0]->get_type() != Variant::STRING) {
  904. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  905. r_error.argument = 0;
  906. r_error.expected = Variant::STRING;
  907. ERR_FAIL_COND_V(p_args[0]->get_type() != Variant::STRING, Variant());
  908. }
  909. r_error.error = Variant::CallError::CALL_OK;
  910. StringName signal = *p_args[0];
  911. const Variant **args = NULL;
  912. int argc = p_argcount - 1;
  913. if (argc) {
  914. args = &p_args[1];
  915. }
  916. emit_signal(signal, args, argc);
  917. return Variant();
  918. }
  919. Error Object::emit_signal(const StringName &p_name, const Variant **p_args, int p_argcount) {
  920. if (_block_signals)
  921. return ERR_CANT_ACQUIRE_RESOURCE; //no emit, signals blocked
  922. Signal *s = signal_map.getptr(p_name);
  923. if (!s) {
  924. #ifdef DEBUG_ENABLED
  925. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_name);
  926. //check in script
  927. if (!signal_is_valid && !script.is_null() && !Ref<Script>(script)->has_script_signal(p_name)) {
  928. ERR_EXPLAIN("Can't emit non-existing signal " + String("\"") + p_name + "\".");
  929. ERR_FAIL_V(ERR_UNAVAILABLE);
  930. }
  931. #endif
  932. //not connected? just return
  933. return ERR_UNAVAILABLE;
  934. }
  935. List<_ObjectSignalDisconnectData> disconnect_data;
  936. //copy on write will ensure that disconnecting the signal or even deleting the object will not affect the signal calling.
  937. //this happens automatically and will not change the performance of calling.
  938. //awesome, isn't it?
  939. VMap<Signal::Target, Signal::Slot> slot_map = s->slot_map;
  940. int ssize = slot_map.size();
  941. OBJ_DEBUG_LOCK
  942. Vector<const Variant *> bind_mem;
  943. Error err = OK;
  944. for (int i = 0; i < ssize; i++) {
  945. const Connection &c = slot_map.getv(i).conn;
  946. Object *target;
  947. #ifdef DEBUG_ENABLED
  948. target = ObjectDB::get_instance(slot_map.getk(i)._id);
  949. ERR_CONTINUE(!target);
  950. #else
  951. target = c.target;
  952. #endif
  953. const Variant **args = p_args;
  954. int argc = p_argcount;
  955. if (c.binds.size()) {
  956. //handle binds
  957. bind_mem.resize(p_argcount + c.binds.size());
  958. for (int j = 0; j < p_argcount; j++) {
  959. bind_mem.write[j] = p_args[j];
  960. }
  961. for (int j = 0; j < c.binds.size(); j++) {
  962. bind_mem.write[p_argcount + j] = &c.binds[j];
  963. }
  964. args = (const Variant **)bind_mem.ptr();
  965. argc = bind_mem.size();
  966. }
  967. if (c.flags & CONNECT_DEFERRED) {
  968. MessageQueue::get_singleton()->push_call(target->get_instance_id(), c.method, args, argc, true);
  969. } else {
  970. Variant::CallError ce;
  971. target->call(c.method, args, argc, ce);
  972. if (ce.error != Variant::CallError::CALL_OK) {
  973. #ifdef DEBUG_ENABLED
  974. if (c.flags & CONNECT_PERSIST && Engine::get_singleton()->is_editor_hint() && (script.is_null() || !Ref<Script>(script)->is_tool()))
  975. continue;
  976. #endif
  977. if (ce.error == Variant::CallError::CALL_ERROR_INVALID_METHOD && !ClassDB::class_exists(target->get_class_name())) {
  978. //most likely object is not initialized yet, do not throw error.
  979. } else {
  980. ERR_PRINTS("Error calling method from signal '" + String(p_name) + "': " + Variant::get_call_error_text(target, c.method, args, argc, ce));
  981. err = ERR_METHOD_NOT_FOUND;
  982. }
  983. }
  984. }
  985. bool disconnect = c.flags & CONNECT_ONESHOT;
  986. #ifdef TOOLS_ENABLED
  987. if (disconnect && (c.flags & CONNECT_PERSIST) && Engine::get_singleton()->is_editor_hint()) {
  988. //this signal was connected from the editor, and is being edited. just don't disconnect for now
  989. disconnect = false;
  990. }
  991. #endif
  992. if (disconnect) {
  993. _ObjectSignalDisconnectData dd;
  994. dd.signal = p_name;
  995. dd.target = target;
  996. dd.method = c.method;
  997. disconnect_data.push_back(dd);
  998. }
  999. }
  1000. while (!disconnect_data.empty()) {
  1001. const _ObjectSignalDisconnectData &dd = disconnect_data.front()->get();
  1002. disconnect(dd.signal, dd.target, dd.method);
  1003. disconnect_data.pop_front();
  1004. }
  1005. return err;
  1006. }
  1007. Error Object::emit_signal(const StringName &p_name, VARIANT_ARG_DECLARE) {
  1008. VARIANT_ARGPTRS;
  1009. int argc = 0;
  1010. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1011. if (argptr[i]->get_type() == Variant::NIL)
  1012. break;
  1013. argc++;
  1014. }
  1015. return emit_signal(p_name, argptr, argc);
  1016. }
  1017. void Object::_add_user_signal(const String &p_name, const Array &p_args) {
  1018. // this version of add_user_signal is meant to be used from scripts or external apis
  1019. // without access to ADD_SIGNAL in bind_methods
  1020. // added events are per instance, as opposed to the other ones, which are global
  1021. MethodInfo mi;
  1022. mi.name = p_name;
  1023. for (int i = 0; i < p_args.size(); i++) {
  1024. Dictionary d = p_args[i];
  1025. PropertyInfo param;
  1026. if (d.has("name"))
  1027. param.name = d["name"];
  1028. if (d.has("type"))
  1029. param.type = (Variant::Type)(int)d["type"];
  1030. mi.arguments.push_back(param);
  1031. }
  1032. add_user_signal(mi);
  1033. }
  1034. Array Object::_get_signal_list() const {
  1035. List<MethodInfo> signal_list;
  1036. get_signal_list(&signal_list);
  1037. Array ret;
  1038. for (List<MethodInfo>::Element *E = signal_list.front(); E; E = E->next()) {
  1039. ret.push_back(Dictionary(E->get()));
  1040. }
  1041. return ret;
  1042. }
  1043. Array Object::_get_signal_connection_list(const String &p_signal) const {
  1044. List<Connection> conns;
  1045. get_all_signal_connections(&conns);
  1046. Array ret;
  1047. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  1048. Connection &c = E->get();
  1049. if (c.signal == p_signal) {
  1050. Dictionary rc;
  1051. rc["signal"] = c.signal;
  1052. rc["method"] = c.method;
  1053. rc["source"] = c.source;
  1054. rc["target"] = c.target;
  1055. rc["binds"] = c.binds;
  1056. rc["flags"] = c.flags;
  1057. ret.push_back(rc);
  1058. }
  1059. }
  1060. return ret;
  1061. }
  1062. Array Object::_get_incoming_connections() const {
  1063. Array ret;
  1064. int connections_amount = connections.size();
  1065. for (int idx_conn = 0; idx_conn < connections_amount; idx_conn++) {
  1066. Dictionary conn_data;
  1067. conn_data["source"] = connections[idx_conn].source;
  1068. conn_data["signal_name"] = connections[idx_conn].signal;
  1069. conn_data["method_name"] = connections[idx_conn].method;
  1070. ret.push_back(conn_data);
  1071. }
  1072. return ret;
  1073. }
  1074. void Object::get_signal_list(List<MethodInfo> *p_signals) const {
  1075. if (!script.is_null()) {
  1076. Ref<Script> scr = script;
  1077. if (scr.is_valid()) {
  1078. scr->get_script_signal_list(p_signals);
  1079. }
  1080. }
  1081. ClassDB::get_signal_list(get_class_name(), p_signals);
  1082. //find maybe usersignals?
  1083. const StringName *S = NULL;
  1084. while ((S = signal_map.next(S))) {
  1085. if (signal_map[*S].user.name != "") {
  1086. //user signal
  1087. p_signals->push_back(signal_map[*S].user);
  1088. }
  1089. }
  1090. }
  1091. void Object::get_all_signal_connections(List<Connection> *p_connections) const {
  1092. const StringName *S = NULL;
  1093. while ((S = signal_map.next(S))) {
  1094. const Signal *s = &signal_map[*S];
  1095. for (int i = 0; i < s->slot_map.size(); i++) {
  1096. p_connections->push_back(s->slot_map.getv(i).conn);
  1097. }
  1098. }
  1099. }
  1100. void Object::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
  1101. const Signal *s = signal_map.getptr(p_signal);
  1102. if (!s)
  1103. return; //nothing
  1104. for (int i = 0; i < s->slot_map.size(); i++)
  1105. p_connections->push_back(s->slot_map.getv(i).conn);
  1106. }
  1107. bool Object::has_persistent_signal_connections() const {
  1108. const StringName *S = NULL;
  1109. while ((S = signal_map.next(S))) {
  1110. const Signal *s = &signal_map[*S];
  1111. for (int i = 0; i < s->slot_map.size(); i++) {
  1112. if (s->slot_map.getv(i).conn.flags & CONNECT_PERSIST)
  1113. return true;
  1114. }
  1115. }
  1116. return false;
  1117. }
  1118. void Object::get_signals_connected_to_this(List<Connection> *p_connections) const {
  1119. for (const List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1120. p_connections->push_back(E->get());
  1121. }
  1122. }
  1123. 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) {
  1124. ERR_FAIL_NULL_V(p_to_object, ERR_INVALID_PARAMETER);
  1125. Signal *s = signal_map.getptr(p_signal);
  1126. if (!s) {
  1127. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1128. //check in script
  1129. if (!signal_is_valid && !script.is_null()) {
  1130. if (Ref<Script>(script)->has_script_signal(p_signal)) {
  1131. signal_is_valid = true;
  1132. }
  1133. #ifdef TOOLS_ENABLED
  1134. else {
  1135. //allow connecting signals anyway if script is invalid, see issue #17070
  1136. if (!Ref<Script>(script)->is_valid()) {
  1137. signal_is_valid = true;
  1138. }
  1139. }
  1140. #endif
  1141. }
  1142. if (!signal_is_valid) {
  1143. ERR_EXPLAIN("In Object of type '" + String(get_class()) + "': Attempt to connect nonexistent signal '" + p_signal + "' to method '" + p_to_object->get_class() + "." + p_to_method + "'");
  1144. ERR_FAIL_COND_V(!signal_is_valid, ERR_INVALID_PARAMETER);
  1145. }
  1146. signal_map[p_signal] = Signal();
  1147. s = &signal_map[p_signal];
  1148. }
  1149. Signal::Target target(p_to_object->get_instance_id(), p_to_method);
  1150. if (s->slot_map.has(target)) {
  1151. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1152. s->slot_map[target].reference_count++;
  1153. return OK;
  1154. } else {
  1155. ERR_EXPLAIN("Signal '" + p_signal + "' is already connected to given method '" + p_to_method + "' in that object.");
  1156. ERR_FAIL_COND_V(s->slot_map.has(target), ERR_INVALID_PARAMETER);
  1157. }
  1158. }
  1159. Signal::Slot slot;
  1160. Connection conn;
  1161. conn.source = this;
  1162. conn.target = p_to_object;
  1163. conn.method = p_to_method;
  1164. conn.signal = p_signal;
  1165. conn.flags = p_flags;
  1166. conn.binds = p_binds;
  1167. slot.conn = conn;
  1168. slot.cE = p_to_object->connections.push_back(conn);
  1169. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1170. slot.reference_count = 1;
  1171. }
  1172. s->slot_map[target] = slot;
  1173. return OK;
  1174. }
  1175. bool Object::is_connected(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method) const {
  1176. ERR_FAIL_NULL_V(p_to_object, false);
  1177. const Signal *s = signal_map.getptr(p_signal);
  1178. if (!s) {
  1179. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1180. if (signal_is_valid)
  1181. return false;
  1182. if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal))
  1183. return false;
  1184. ERR_EXPLAIN("Nonexistent signal: " + p_signal);
  1185. ERR_FAIL_COND_V(!s, false);
  1186. }
  1187. Signal::Target target(p_to_object->get_instance_id(), p_to_method);
  1188. return s->slot_map.has(target);
  1189. //const Map<Signal::Target,Signal::Slot>::Element *E = s->slot_map.find(target);
  1190. //return (E!=NULL);
  1191. }
  1192. void Object::disconnect(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method) {
  1193. _disconnect(p_signal, p_to_object, p_to_method);
  1194. }
  1195. void Object::_disconnect(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method, bool p_force) {
  1196. ERR_FAIL_NULL(p_to_object);
  1197. Signal *s = signal_map.getptr(p_signal);
  1198. if (!s) {
  1199. ERR_EXPLAIN("Nonexistent signal: " + p_signal);
  1200. ERR_FAIL_COND(!s);
  1201. }
  1202. if (s->lock > 0) {
  1203. ERR_EXPLAIN("Attempt to disconnect signal '" + p_signal + "' while emitting (locks: " + itos(s->lock) + ")");
  1204. ERR_FAIL_COND(s->lock > 0);
  1205. }
  1206. Signal::Target target(p_to_object->get_instance_id(), p_to_method);
  1207. if (!s->slot_map.has(target)) {
  1208. ERR_EXPLAIN("Disconnecting nonexistent signal '" + p_signal + "', slot: " + itos(target._id) + ":" + target.method);
  1209. ERR_FAIL();
  1210. }
  1211. Signal::Slot *slot = &s->slot_map[target];
  1212. if (!p_force) {
  1213. slot->reference_count--; // by default is zero, if it was not referenced it will go below it
  1214. if (slot->reference_count >= 0) {
  1215. return;
  1216. }
  1217. }
  1218. p_to_object->connections.erase(slot->cE);
  1219. s->slot_map.erase(target);
  1220. if (s->slot_map.empty() && ClassDB::has_signal(get_class_name(), p_signal)) {
  1221. //not user signal, delete
  1222. signal_map.erase(p_signal);
  1223. }
  1224. }
  1225. void Object::_set_bind(const String &p_set, const Variant &p_value) {
  1226. set(p_set, p_value);
  1227. }
  1228. Variant Object::_get_bind(const String &p_name) const {
  1229. return get(p_name);
  1230. }
  1231. void Object::_set_indexed_bind(const NodePath &p_name, const Variant &p_value) {
  1232. set_indexed(p_name.get_as_property_path().get_subnames(), p_value);
  1233. }
  1234. Variant Object::_get_indexed_bind(const NodePath &p_name) const {
  1235. return get_indexed(p_name.get_as_property_path().get_subnames());
  1236. }
  1237. void Object::initialize_class() {
  1238. static bool initialized = false;
  1239. if (initialized)
  1240. return;
  1241. ClassDB::_add_class<Object>();
  1242. _bind_methods();
  1243. initialized = true;
  1244. }
  1245. StringName Object::tr(const StringName &p_message) const {
  1246. if (!_can_translate || !TranslationServer::get_singleton())
  1247. return p_message;
  1248. return TranslationServer::get_singleton()->translate(p_message);
  1249. }
  1250. void Object::_clear_internal_resource_paths(const Variant &p_var) {
  1251. switch (p_var.get_type()) {
  1252. case Variant::OBJECT: {
  1253. RES r = p_var;
  1254. if (!r.is_valid())
  1255. return;
  1256. if (!r->get_path().begins_with("res://") || r->get_path().find("::") == -1)
  1257. return; //not an internal resource
  1258. Object *object = p_var;
  1259. if (!object)
  1260. return;
  1261. r->set_path("");
  1262. r->clear_internal_resource_paths();
  1263. } break;
  1264. case Variant::ARRAY: {
  1265. Array a = p_var;
  1266. for (int i = 0; i < a.size(); i++) {
  1267. _clear_internal_resource_paths(a[i]);
  1268. }
  1269. } break;
  1270. case Variant::DICTIONARY: {
  1271. Dictionary d = p_var;
  1272. List<Variant> keys;
  1273. d.get_key_list(&keys);
  1274. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1275. _clear_internal_resource_paths(E->get());
  1276. _clear_internal_resource_paths(d[E->get()]);
  1277. }
  1278. } break;
  1279. default: {
  1280. }
  1281. }
  1282. }
  1283. #ifdef TOOLS_ENABLED
  1284. void Object::editor_set_section_unfold(const String &p_section, bool p_unfolded) {
  1285. set_edited(true);
  1286. if (p_unfolded)
  1287. editor_section_folding.insert(p_section);
  1288. else
  1289. editor_section_folding.erase(p_section);
  1290. }
  1291. bool Object::editor_is_section_unfolded(const String &p_section) {
  1292. return editor_section_folding.has(p_section);
  1293. }
  1294. #endif
  1295. void Object::clear_internal_resource_paths() {
  1296. List<PropertyInfo> pinfo;
  1297. get_property_list(&pinfo);
  1298. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1299. _clear_internal_resource_paths(get(E->get().name));
  1300. }
  1301. }
  1302. void Object::_bind_methods() {
  1303. ClassDB::bind_method(D_METHOD("get_class"), &Object::get_class);
  1304. ClassDB::bind_method(D_METHOD("is_class", "type"), &Object::is_class);
  1305. ClassDB::bind_method(D_METHOD("set", "property", "value"), &Object::_set_bind);
  1306. ClassDB::bind_method(D_METHOD("get", "property"), &Object::_get_bind);
  1307. ClassDB::bind_method(D_METHOD("set_indexed", "property", "value"), &Object::_set_indexed_bind);
  1308. ClassDB::bind_method(D_METHOD("get_indexed", "property"), &Object::_get_indexed_bind);
  1309. ClassDB::bind_method(D_METHOD("get_property_list"), &Object::_get_property_list_bind);
  1310. ClassDB::bind_method(D_METHOD("get_method_list"), &Object::_get_method_list_bind);
  1311. ClassDB::bind_method(D_METHOD("notification", "what", "reversed"), &Object::notification, DEFVAL(false));
  1312. ClassDB::bind_method(D_METHOD("get_instance_id"), &Object::get_instance_id);
  1313. ClassDB::bind_method(D_METHOD("set_script", "script"), &Object::set_script);
  1314. ClassDB::bind_method(D_METHOD("get_script"), &Object::get_script);
  1315. ClassDB::bind_method(D_METHOD("set_meta", "name", "value"), &Object::set_meta);
  1316. ClassDB::bind_method(D_METHOD("remove_meta", "name"), &Object::remove_meta);
  1317. ClassDB::bind_method(D_METHOD("get_meta", "name"), &Object::get_meta);
  1318. ClassDB::bind_method(D_METHOD("has_meta", "name"), &Object::has_meta);
  1319. ClassDB::bind_method(D_METHOD("get_meta_list"), &Object::_get_meta_list_bind);
  1320. //todo reimplement this per language so all 5 arguments can be called
  1321. //ClassDB::bind_method(D_METHOD("call","method","arg1","arg2","arg3","arg4"),&Object::_call_bind,DEFVAL(Variant()),DEFVAL(Variant()),DEFVAL(Variant()),DEFVAL(Variant()));
  1322. //ClassDB::bind_method(D_METHOD("call_deferred","method","arg1","arg2","arg3","arg4"),&Object::_call_deferred_bind,DEFVAL(Variant()),DEFVAL(Variant()),DEFVAL(Variant()),DEFVAL(Variant()));
  1323. ClassDB::bind_method(D_METHOD("add_user_signal", "signal", "arguments"), &Object::_add_user_signal, DEFVAL(Array()));
  1324. ClassDB::bind_method(D_METHOD("has_user_signal", "signal"), &Object::_has_user_signal);
  1325. //ClassDB::bind_method(D_METHOD("emit_signal","signal","arguments"),&Object::_emit_signal,DEFVAL(Array()));
  1326. {
  1327. MethodInfo mi;
  1328. mi.name = "emit_signal";
  1329. mi.arguments.push_back(PropertyInfo(Variant::STRING, "signal"));
  1330. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "emit_signal", &Object::_emit_signal, mi);
  1331. }
  1332. {
  1333. MethodInfo mi;
  1334. mi.name = "call";
  1335. mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  1336. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call", &Object::_call_bind, mi);
  1337. }
  1338. {
  1339. MethodInfo mi;
  1340. mi.name = "call_deferred";
  1341. mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  1342. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_deferred", &Object::_call_deferred_bind, mi);
  1343. }
  1344. ClassDB::bind_method(D_METHOD("set_deferred", "property", "value"), &Object::set_deferred);
  1345. ClassDB::bind_method(D_METHOD("callv", "method", "arg_array"), &Object::callv);
  1346. ClassDB::bind_method(D_METHOD("has_method", "method"), &Object::has_method);
  1347. ClassDB::bind_method(D_METHOD("get_signal_list"), &Object::_get_signal_list);
  1348. ClassDB::bind_method(D_METHOD("get_signal_connection_list", "signal"), &Object::_get_signal_connection_list);
  1349. ClassDB::bind_method(D_METHOD("get_incoming_connections"), &Object::_get_incoming_connections);
  1350. ClassDB::bind_method(D_METHOD("connect", "signal", "target", "method", "binds", "flags"), &Object::connect, DEFVAL(Array()), DEFVAL(0));
  1351. ClassDB::bind_method(D_METHOD("disconnect", "signal", "target", "method"), &Object::disconnect);
  1352. ClassDB::bind_method(D_METHOD("is_connected", "signal", "target", "method"), &Object::is_connected);
  1353. ClassDB::bind_method(D_METHOD("set_block_signals", "enable"), &Object::set_block_signals);
  1354. ClassDB::bind_method(D_METHOD("is_blocking_signals"), &Object::is_blocking_signals);
  1355. ClassDB::bind_method(D_METHOD("property_list_changed_notify"), &Object::property_list_changed_notify);
  1356. ClassDB::bind_method(D_METHOD("set_message_translation", "enable"), &Object::set_message_translation);
  1357. ClassDB::bind_method(D_METHOD("can_translate_messages"), &Object::can_translate_messages);
  1358. ClassDB::bind_method(D_METHOD("tr", "message"), &Object::tr);
  1359. ClassDB::bind_method(D_METHOD("is_queued_for_deletion"), &Object::is_queued_for_deletion);
  1360. ClassDB::add_virtual_method("Object", MethodInfo("free"), false);
  1361. ADD_SIGNAL(MethodInfo("script_changed"));
  1362. BIND_VMETHOD(MethodInfo("_notification", PropertyInfo(Variant::INT, "what")));
  1363. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_set", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::NIL, "value")));
  1364. #ifdef TOOLS_ENABLED
  1365. MethodInfo miget("_get", PropertyInfo(Variant::STRING, "property"));
  1366. miget.return_val.name = "Variant";
  1367. miget.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1368. BIND_VMETHOD(miget);
  1369. MethodInfo plget("_get_property_list");
  1370. plget.return_val.type = Variant::ARRAY;
  1371. BIND_VMETHOD(plget);
  1372. #endif
  1373. BIND_VMETHOD(MethodInfo("_init"));
  1374. BIND_CONSTANT(NOTIFICATION_POSTINITIALIZE);
  1375. BIND_CONSTANT(NOTIFICATION_PREDELETE);
  1376. BIND_ENUM_CONSTANT(CONNECT_DEFERRED);
  1377. BIND_ENUM_CONSTANT(CONNECT_PERSIST);
  1378. BIND_ENUM_CONSTANT(CONNECT_ONESHOT);
  1379. BIND_ENUM_CONSTANT(CONNECT_REFERENCE_COUNTED);
  1380. }
  1381. void Object::call_deferred(const StringName &p_method, VARIANT_ARG_DECLARE) {
  1382. MessageQueue::get_singleton()->push_call(this, p_method, VARIANT_ARG_PASS);
  1383. }
  1384. void Object::set_deferred(const StringName &p_property, const Variant &p_value) {
  1385. MessageQueue::get_singleton()->push_set(this, p_property, p_value);
  1386. }
  1387. void Object::set_block_signals(bool p_block) {
  1388. _block_signals = p_block;
  1389. }
  1390. bool Object::is_blocking_signals() const {
  1391. return _block_signals;
  1392. }
  1393. void Object::get_translatable_strings(List<String> *p_strings) const {
  1394. List<PropertyInfo> plist;
  1395. get_property_list(&plist);
  1396. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1397. if (!(E->get().usage & PROPERTY_USAGE_INTERNATIONALIZED))
  1398. continue;
  1399. String text = get(E->get().name);
  1400. if (text == "")
  1401. continue;
  1402. p_strings->push_back(text);
  1403. }
  1404. }
  1405. Variant::Type Object::get_static_property_type(const StringName &p_property, bool *r_valid) const {
  1406. bool valid;
  1407. Variant::Type t = ClassDB::get_property_type(get_class_name(), p_property, &valid);
  1408. if (valid) {
  1409. if (r_valid)
  1410. *r_valid = true;
  1411. return t;
  1412. }
  1413. if (get_script_instance()) {
  1414. return get_script_instance()->get_property_type(p_property, r_valid);
  1415. }
  1416. if (r_valid)
  1417. *r_valid = false;
  1418. return Variant::NIL;
  1419. }
  1420. Variant::Type Object::get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid) const {
  1421. if (p_path.size() == 0) {
  1422. if (r_valid)
  1423. *r_valid = false;
  1424. return Variant::NIL;
  1425. }
  1426. bool valid = false;
  1427. Variant::Type t = get_static_property_type(p_path[0], &valid);
  1428. if (!valid) {
  1429. if (r_valid)
  1430. *r_valid = false;
  1431. return Variant::NIL;
  1432. }
  1433. Variant::CallError ce;
  1434. Variant check = Variant::construct(t, NULL, 0, ce);
  1435. for (int i = 1; i < p_path.size(); i++) {
  1436. if (check.get_type() == Variant::OBJECT || check.get_type() == Variant::DICTIONARY || check.get_type() == Variant::ARRAY) {
  1437. // We cannot be sure about the type of properties this types can have
  1438. if (r_valid)
  1439. *r_valid = false;
  1440. return Variant::NIL;
  1441. }
  1442. check = check.get_named(p_path[i], &valid);
  1443. if (!valid) {
  1444. if (r_valid)
  1445. *r_valid = false;
  1446. return Variant::NIL;
  1447. }
  1448. }
  1449. if (r_valid)
  1450. *r_valid = true;
  1451. return check.get_type();
  1452. }
  1453. bool Object::is_queued_for_deletion() const {
  1454. return _is_queued_for_deletion;
  1455. }
  1456. #ifdef TOOLS_ENABLED
  1457. void Object::set_edited(bool p_edited) {
  1458. _edited = p_edited;
  1459. _edited_version++;
  1460. }
  1461. bool Object::is_edited() const {
  1462. return _edited;
  1463. }
  1464. uint32_t Object::get_edited_version() const {
  1465. return _edited_version;
  1466. }
  1467. #endif
  1468. void *Object::get_script_instance_binding(int p_script_language_index) {
  1469. #ifdef DEBUG_ENABLED
  1470. ERR_FAIL_INDEX_V(p_script_language_index, MAX_SCRIPT_INSTANCE_BINDINGS, NULL);
  1471. #endif
  1472. //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
  1473. //just return the same pointer.
  1474. //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
  1475. //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
  1476. if (!_script_instance_bindings[p_script_language_index]) {
  1477. void *script_data = ScriptServer::get_language(p_script_language_index)->alloc_instance_binding_data(this);
  1478. if (script_data) {
  1479. atomic_increment(&instance_binding_count);
  1480. _script_instance_bindings[p_script_language_index] = script_data;
  1481. }
  1482. }
  1483. return _script_instance_bindings[p_script_language_index];
  1484. }
  1485. bool Object::has_script_instance_binding(int p_script_language_index) {
  1486. return _script_instance_bindings[p_script_language_index] != NULL;
  1487. }
  1488. void Object::set_script_instance_binding(int p_script_language_index, void *p_data) {
  1489. #ifdef DEBUG_ENABLED
  1490. CRASH_COND(_script_instance_bindings[p_script_language_index] != NULL);
  1491. #endif
  1492. _script_instance_bindings[p_script_language_index] = p_data;
  1493. }
  1494. Object::Object() {
  1495. _class_ptr = NULL;
  1496. _block_signals = false;
  1497. _predelete_ok = 0;
  1498. _instance_id = 0;
  1499. _instance_id = ObjectDB::add_instance(this);
  1500. _can_translate = true;
  1501. _is_queued_for_deletion = false;
  1502. instance_binding_count = 0;
  1503. memset(_script_instance_bindings, 0, sizeof(void *) * MAX_SCRIPT_INSTANCE_BINDINGS);
  1504. script_instance = NULL;
  1505. #ifdef TOOLS_ENABLED
  1506. _edited = false;
  1507. _edited_version = 0;
  1508. #endif
  1509. #ifdef DEBUG_ENABLED
  1510. _lock_index.init(1);
  1511. #endif
  1512. }
  1513. Object::~Object() {
  1514. if (script_instance)
  1515. memdelete(script_instance);
  1516. script_instance = NULL;
  1517. const StringName *S = NULL;
  1518. while ((S = signal_map.next(NULL))) {
  1519. Signal *s = &signal_map[*S];
  1520. if (s->lock) {
  1521. ERR_EXPLAIN("Attempt to delete an object in the middle of a signal emission from it");
  1522. ERR_CONTINUE(s->lock > 0);
  1523. }
  1524. //brute force disconnect for performance
  1525. int slot_count = s->slot_map.size();
  1526. const VMap<Signal::Target, Signal::Slot>::Pair *slot_list = s->slot_map.get_array();
  1527. for (int i = 0; i < slot_count; i++) {
  1528. slot_list[i].value.conn.target->connections.erase(slot_list[i].value.cE);
  1529. }
  1530. signal_map.erase(*S);
  1531. }
  1532. //signals from nodes that connect to this node
  1533. while (connections.size()) {
  1534. Connection c = connections.front()->get();
  1535. c.source->_disconnect(c.signal, c.target, c.method, true);
  1536. }
  1537. ObjectDB::remove_instance(this);
  1538. _instance_id = 0;
  1539. _predelete_ok = 2;
  1540. if (!ScriptServer::are_languages_finished()) {
  1541. for (int i = 0; i < MAX_SCRIPT_INSTANCE_BINDINGS; i++) {
  1542. if (_script_instance_bindings[i]) {
  1543. ScriptServer::get_language(i)->free_instance_binding_data(_script_instance_bindings[i]);
  1544. }
  1545. }
  1546. }
  1547. }
  1548. bool predelete_handler(Object *p_object) {
  1549. return p_object->_predelete();
  1550. }
  1551. void postinitialize_handler(Object *p_object) {
  1552. p_object->_postinitialize();
  1553. }
  1554. HashMap<ObjectID, Object *> ObjectDB::instances;
  1555. ObjectID ObjectDB::instance_counter = 1;
  1556. HashMap<Object *, ObjectID, ObjectDB::ObjectPtrHash> ObjectDB::instance_checks;
  1557. ObjectID ObjectDB::add_instance(Object *p_object) {
  1558. ERR_FAIL_COND_V(p_object->get_instance_id() != 0, 0);
  1559. rw_lock->write_lock();
  1560. ObjectID instance_id = ++instance_counter;
  1561. instances[instance_id] = p_object;
  1562. instance_checks[p_object] = instance_id;
  1563. rw_lock->write_unlock();
  1564. return instance_id;
  1565. }
  1566. void ObjectDB::remove_instance(Object *p_object) {
  1567. rw_lock->write_lock();
  1568. instances.erase(p_object->get_instance_id());
  1569. instance_checks.erase(p_object);
  1570. rw_lock->write_unlock();
  1571. }
  1572. Object *ObjectDB::get_instance(ObjectID p_instance_id) {
  1573. rw_lock->read_lock();
  1574. Object **obj = instances.getptr(p_instance_id);
  1575. rw_lock->read_unlock();
  1576. if (!obj)
  1577. return NULL;
  1578. return *obj;
  1579. }
  1580. void ObjectDB::debug_objects(DebugFunc p_func) {
  1581. rw_lock->read_lock();
  1582. const ObjectID *K = NULL;
  1583. while ((K = instances.next(K))) {
  1584. p_func(instances[*K]);
  1585. }
  1586. rw_lock->read_unlock();
  1587. }
  1588. void Object::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1589. }
  1590. int ObjectDB::get_object_count() {
  1591. rw_lock->read_lock();
  1592. int count = instances.size();
  1593. rw_lock->read_unlock();
  1594. return count;
  1595. }
  1596. RWLock *ObjectDB::rw_lock = NULL;
  1597. void ObjectDB::setup() {
  1598. rw_lock = RWLock::create();
  1599. }
  1600. void ObjectDB::cleanup() {
  1601. rw_lock->write_lock();
  1602. if (instances.size()) {
  1603. WARN_PRINT("ObjectDB Instances still exist!");
  1604. if (OS::get_singleton()->is_stdout_verbose()) {
  1605. const ObjectID *K = NULL;
  1606. while ((K = instances.next(K))) {
  1607. String node_name;
  1608. if (instances[*K]->is_class("Node"))
  1609. node_name = " - Node name: " + String(instances[*K]->call("get_name"));
  1610. if (instances[*K]->is_class("Resource"))
  1611. node_name = " - Resource name: " + String(instances[*K]->call("get_name")) + " Path: " + String(instances[*K]->call("get_path"));
  1612. print_line("Leaked instance: " + String(instances[*K]->get_class()) + ":" + itos(*K) + node_name);
  1613. }
  1614. }
  1615. }
  1616. instances.clear();
  1617. instance_checks.clear();
  1618. rw_lock->write_unlock();
  1619. memdelete(rw_lock);
  1620. }