object.cpp 57 KB

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