object.cpp 54 KB

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