object.cpp 54 KB

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