object.cpp 57 KB

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