object.cpp 57 KB

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