object.cpp 56 KB

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