object.cpp 55 KB

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