object.cpp 53 KB

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