object.cpp 54 KB

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