object.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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_string_names.h"
  32. #include "message_queue.h"
  33. #include "object_type_db.h"
  34. #include "os/os.h"
  35. #include "print_string.h"
  36. #include "resource.h"
  37. #include "script_language.h"
  38. #include "translation.h"
  39. #ifdef DEBUG_ENABLED
  40. struct _ObjectDebugLock {
  41. Object *obj;
  42. _ObjectDebugLock(Object *p_obj) {
  43. obj = p_obj;
  44. obj->_lock_index.ref();
  45. }
  46. ~_ObjectDebugLock() {
  47. obj->_lock_index.unref();
  48. }
  49. };
  50. #define OBJ_DEBUG_LOCK _ObjectDebugLock _debug_lock(this);
  51. #else
  52. #define OBJ_DEBUG_LOCK
  53. #endif
  54. Array convert_property_list(const List<PropertyInfo> *p_list) {
  55. Array va;
  56. for (const List<PropertyInfo>::Element *E = p_list->front(); E; E = E->next()) {
  57. const PropertyInfo &pi = E->get();
  58. Dictionary d;
  59. d["name"] = pi.name;
  60. d["type"] = pi.type;
  61. d["hint"] = pi.hint;
  62. d["hint_string"] = pi.hint_string;
  63. d["usage"] = pi.usage;
  64. va.push_back(d);
  65. }
  66. return va;
  67. }
  68. MethodInfo::MethodInfo() {
  69. id = 0;
  70. flags = METHOD_FLAG_NORMAL;
  71. }
  72. MethodInfo::MethodInfo(const String &p_name) {
  73. id = 0;
  74. name = p_name;
  75. flags = METHOD_FLAG_NORMAL;
  76. }
  77. MethodInfo::MethodInfo(const String &p_name, const PropertyInfo &p_param1) {
  78. id = 0;
  79. name = p_name;
  80. arguments.push_back(p_param1);
  81. flags = METHOD_FLAG_NORMAL;
  82. }
  83. MethodInfo::MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2) {
  84. id = 0;
  85. name = p_name;
  86. arguments.push_back(p_param1);
  87. arguments.push_back(p_param2);
  88. flags = METHOD_FLAG_NORMAL;
  89. }
  90. MethodInfo::MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3) {
  91. id = 0;
  92. name = p_name;
  93. arguments.push_back(p_param1);
  94. arguments.push_back(p_param2);
  95. arguments.push_back(p_param3);
  96. flags = METHOD_FLAG_NORMAL;
  97. }
  98. MethodInfo::MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4) {
  99. id = 0;
  100. name = p_name;
  101. arguments.push_back(p_param1);
  102. arguments.push_back(p_param2);
  103. arguments.push_back(p_param3);
  104. arguments.push_back(p_param4);
  105. flags = METHOD_FLAG_NORMAL;
  106. }
  107. 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) {
  108. id = 0;
  109. name = p_name;
  110. arguments.push_back(p_param1);
  111. arguments.push_back(p_param2);
  112. arguments.push_back(p_param3);
  113. arguments.push_back(p_param4);
  114. arguments.push_back(p_param5);
  115. flags = METHOD_FLAG_NORMAL;
  116. }
  117. MethodInfo::MethodInfo(Variant::Type ret) {
  118. id = 0;
  119. flags = METHOD_FLAG_NORMAL;
  120. return_val.type = ret;
  121. }
  122. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name) {
  123. id = 0;
  124. name = p_name;
  125. flags = METHOD_FLAG_NORMAL;
  126. return_val.type = ret;
  127. }
  128. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1) {
  129. id = 0;
  130. name = p_name;
  131. arguments.push_back(p_param1);
  132. flags = METHOD_FLAG_NORMAL;
  133. return_val.type = ret;
  134. }
  135. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2) {
  136. id = 0;
  137. name = p_name;
  138. arguments.push_back(p_param1);
  139. arguments.push_back(p_param2);
  140. flags = METHOD_FLAG_NORMAL;
  141. return_val.type = ret;
  142. }
  143. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3) {
  144. id = 0;
  145. name = p_name;
  146. arguments.push_back(p_param1);
  147. arguments.push_back(p_param2);
  148. arguments.push_back(p_param3);
  149. flags = METHOD_FLAG_NORMAL;
  150. return_val.type = ret;
  151. }
  152. 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) {
  153. id = 0;
  154. name = p_name;
  155. arguments.push_back(p_param1);
  156. arguments.push_back(p_param2);
  157. arguments.push_back(p_param3);
  158. arguments.push_back(p_param4);
  159. flags = METHOD_FLAG_NORMAL;
  160. return_val.type = ret;
  161. }
  162. 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) {
  163. id = 0;
  164. name = p_name;
  165. arguments.push_back(p_param1);
  166. arguments.push_back(p_param2);
  167. arguments.push_back(p_param3);
  168. arguments.push_back(p_param4);
  169. arguments.push_back(p_param5);
  170. flags = METHOD_FLAG_NORMAL;
  171. return_val.type = ret;
  172. }
  173. Object::Connection::operator Variant() const {
  174. Dictionary d;
  175. d["source"] = source;
  176. d["signal"] = signal;
  177. d["target"] = target;
  178. d["method"] = method;
  179. d["flags"] = flags;
  180. d["binds"] = binds;
  181. return d;
  182. }
  183. bool Object::Connection::operator<(const Connection &p_conn) const {
  184. if (source == p_conn.source) {
  185. if (signal == p_conn.signal) {
  186. if (target == p_conn.target) {
  187. return method < p_conn.method;
  188. } else {
  189. return target < p_conn.target;
  190. }
  191. } else
  192. return signal < p_conn.signal;
  193. } else {
  194. return source < p_conn.source;
  195. }
  196. }
  197. Object::Connection::Connection(const Variant &p_variant) {
  198. Dictionary d = p_variant;
  199. if (d.has("source"))
  200. source = d["source"];
  201. if (d.has("signal"))
  202. signal = d["signal"];
  203. if (d.has("target"))
  204. target = d["target"];
  205. if (d.has("method"))
  206. method = d["method"];
  207. if (d.has("flags"))
  208. flags = d["flags"];
  209. if (d.has("binds"))
  210. binds = d["binds"];
  211. }
  212. bool Object::_predelete() {
  213. _predelete_ok = 1;
  214. notification(NOTIFICATION_PREDELETE, true);
  215. if (_predelete_ok) {
  216. _type_ptr = NULL; //must restore so destructors can access type ptr correctly
  217. }
  218. return _predelete_ok;
  219. }
  220. void Object::_postinitialize() {
  221. _type_ptr = _get_type_namev();
  222. _initialize_typev();
  223. notification(NOTIFICATION_POSTINITIALIZE);
  224. }
  225. void Object::get_valid_parents_static(List<String> *p_parents) {
  226. }
  227. void Object::_get_valid_parents_static(List<String> *p_parents) {
  228. }
  229. #if 0
  230. //old style set, deprecated
  231. void Object::set(const String& p_name, const Variant& p_value) {
  232. _setv(p_name,p_value);
  233. //if (!_use_builtin_script())
  234. // return;
  235. bool success;
  236. ObjectTypeDB::set_property(this,p_name,p_value,success);
  237. if (success) {
  238. return;
  239. }
  240. if (p_name=="__meta__") {
  241. metadata=p_value;
  242. } else if (p_name=="script/script") {
  243. set_script(p_value);
  244. } else if (script_instance) {
  245. script_instance->set(p_name,p_value);
  246. }
  247. }
  248. #endif
  249. void Object::set(const StringName &p_name, const Variant &p_value, bool *r_valid) {
  250. #ifdef TOOLS_ENABLED
  251. _edited = true;
  252. #endif
  253. if (script_instance) {
  254. if (script_instance->set(p_name, p_value)) {
  255. if (r_valid)
  256. *r_valid = true;
  257. return;
  258. }
  259. }
  260. //try built-in setgetter
  261. {
  262. if (ObjectTypeDB::set_property(this, p_name, p_value, r_valid)) {
  263. //if (r_valid)
  264. // *r_valid=true;
  265. return;
  266. }
  267. }
  268. if (p_name == CoreStringNames::get_singleton()->_script) {
  269. set_script(p_value);
  270. if (r_valid)
  271. *r_valid = true;
  272. return;
  273. } else if (p_name == CoreStringNames::get_singleton()->_meta) {
  274. //set_meta(p_name,p_value);
  275. metadata = p_value;
  276. if (r_valid)
  277. *r_valid = true;
  278. return;
  279. } else {
  280. //something inside the object... :|
  281. bool success = _setv(p_name, p_value);
  282. if (success) {
  283. if (r_valid)
  284. *r_valid = true;
  285. return;
  286. }
  287. setvar(p_name, p_value, r_valid);
  288. }
  289. }
  290. Variant Object::get(const StringName &p_name, bool *r_valid) const {
  291. Variant ret;
  292. if (script_instance) {
  293. if (script_instance->get(p_name, ret)) {
  294. if (r_valid)
  295. *r_valid = true;
  296. return ret;
  297. }
  298. }
  299. //try built-in setgetter
  300. {
  301. if (ObjectTypeDB::get_property(const_cast<Object *>(this), p_name, ret)) {
  302. if (r_valid)
  303. *r_valid = true;
  304. return ret;
  305. }
  306. }
  307. if (p_name == CoreStringNames::get_singleton()->_script) {
  308. ret = get_script();
  309. if (r_valid)
  310. *r_valid = true;
  311. return ret;
  312. } else if (p_name == CoreStringNames::get_singleton()->_meta) {
  313. ret = metadata;
  314. if (r_valid)
  315. *r_valid = true;
  316. return ret;
  317. } else {
  318. //something inside the object... :|
  319. bool success = _getv(p_name, ret);
  320. if (success) {
  321. if (r_valid)
  322. *r_valid = true;
  323. return ret;
  324. }
  325. //if nothing else, use getvar
  326. return getvar(p_name, r_valid);
  327. }
  328. }
  329. #if 0
  330. //old style get, deprecated
  331. Variant Object::get(const String& p_name) const {
  332. Variant ret=_getv(p_name);
  333. if (ret.get_type()!=Variant::NIL)
  334. return ret;
  335. bool success;
  336. ObjectTypeDB::get_property(const_cast<Object*>(this),p_name,ret,success);
  337. if (success) {
  338. return ret;
  339. }
  340. if (p_name=="__meta__")
  341. return metadata;
  342. else if (p_name=="script/script")
  343. return script;
  344. if (script_instance) {
  345. return script_instance->get(p_name);
  346. }
  347. return Variant();
  348. }
  349. #endif
  350. void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) const {
  351. if (script_instance && p_reversed) {
  352. p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  353. script_instance->get_property_list(p_list);
  354. }
  355. _get_property_listv(p_list, p_reversed);
  356. if (!_use_builtin_script())
  357. return;
  358. if (!is_type("Script")) // can still be set, but this is for userfriendlyness
  359. p_list->push_back(PropertyInfo(Variant::OBJECT, "script/script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NONZERO));
  360. if (!metadata.empty())
  361. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "__meta__", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_STORE_IF_NONZERO));
  362. if (script_instance && !p_reversed) {
  363. p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  364. script_instance->get_property_list(p_list);
  365. }
  366. }
  367. void Object::_validate_property(PropertyInfo &property) const {
  368. }
  369. void Object::get_method_list(List<MethodInfo> *p_list) const {
  370. ObjectTypeDB::get_method_list(get_type_name(), p_list);
  371. if (script_instance) {
  372. script_instance->get_method_list(p_list);
  373. }
  374. }
  375. Variant Object::_call_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  376. if (p_argcount < 1) {
  377. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  378. r_error.argument = 0;
  379. return Variant();
  380. }
  381. if (p_args[0]->get_type() != Variant::STRING) {
  382. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  383. r_error.argument = 0;
  384. r_error.expected = Variant::STRING;
  385. return Variant();
  386. }
  387. StringName method = *p_args[0];
  388. return call(method, &p_args[1], p_argcount - 1, r_error);
  389. }
  390. Variant Object::_call_deferred_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  391. if (p_argcount < 1) {
  392. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  393. r_error.argument = 0;
  394. return Variant();
  395. }
  396. if (p_args[0]->get_type() != Variant::STRING) {
  397. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  398. r_error.argument = 0;
  399. r_error.expected = Variant::STRING;
  400. return Variant();
  401. }
  402. r_error.error = Variant::CallError::CALL_OK;
  403. StringName method = *p_args[0];
  404. MessageQueue::get_singleton()->push_call(get_instance_ID(), method, &p_args[1], p_argcount - 1);
  405. return Variant();
  406. }
  407. #if 0
  408. Variant Object::_call_bind(const StringName& p_name, const Variant& p_arg1, const Variant& p_arg2, const Variant& p_arg3, const Variant& p_arg4) {
  409. ERR_FAIL_COND_V(p_argcount<1,Variant());
  410. return call(p_name, p_arg1, p_arg2, p_arg3, p_arg4);
  411. };
  412. void Object::_call_deferred_bind(const StringName& p_name, const Variant& p_arg1, const Variant& p_arg2, const Variant& p_arg3, const Variant& p_arg4) {
  413. call_deferred(p_name, p_arg1, p_arg2, p_arg3, p_arg4);
  414. };
  415. #endif
  416. #ifdef DEBUG_ENABLED
  417. static bool _test_call_error(const StringName &p_func, const Variant::CallError &error) {
  418. switch (error.error) {
  419. case Variant::CallError::CALL_OK:
  420. return true;
  421. case Variant::CallError::CALL_ERROR_INVALID_METHOD:
  422. return false;
  423. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  424. ERR_EXPLAIN("Error Calling Function: " + String(p_func) + " - Invalid type for argument " + itos(error.argument) + ", expected " + Variant::get_type_name(error.expected));
  425. ERR_FAIL_V(true);
  426. } break;
  427. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  428. ERR_EXPLAIN("Error Calling Function: " + String(p_func) + " - Too many arguments, expected " + itos(error.argument));
  429. ERR_FAIL_V(true);
  430. } break;
  431. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  432. ERR_EXPLAIN("Error Calling Function: " + String(p_func) + " - Too few arguments, expected " + itos(error.argument));
  433. ERR_FAIL_V(true);
  434. } break;
  435. case Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL: {
  436. } //?
  437. }
  438. return true;
  439. }
  440. #else
  441. #define _test_call_error(m_str, m_err) ((m_err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) ? false : true)
  442. #endif
  443. void Object::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  444. if (p_method == CoreStringNames::get_singleton()->_free) {
  445. #ifdef DEBUG_ENABLED
  446. if (cast_to<Reference>()) {
  447. ERR_EXPLAIN("Can't 'free' a reference.");
  448. ERR_FAIL();
  449. return;
  450. }
  451. if (_lock_index.get() > 1) {
  452. ERR_EXPLAIN("Object is locked and can't be freed.");
  453. ERR_FAIL();
  454. return;
  455. }
  456. #endif
  457. //must be here, must be before everything,
  458. memdelete(this);
  459. return;
  460. }
  461. //Variant ret;
  462. OBJ_DEBUG_LOCK
  463. Variant::CallError error;
  464. if (script_instance) {
  465. script_instance->call_multilevel(p_method, p_args, p_argcount);
  466. //_test_call_error(p_method,error);
  467. }
  468. MethodBind *method = ObjectTypeDB::get_method(get_type_name(), p_method);
  469. if (method) {
  470. method->call(this, p_args, p_argcount, error);
  471. _test_call_error(p_method, error);
  472. }
  473. }
  474. void Object::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  475. MethodBind *method = ObjectTypeDB::get_method(get_type_name(), p_method);
  476. Variant::CallError error;
  477. OBJ_DEBUG_LOCK
  478. if (method) {
  479. method->call(this, p_args, p_argcount, error);
  480. _test_call_error(p_method, error);
  481. }
  482. //Variant ret;
  483. if (script_instance) {
  484. script_instance->call_multilevel_reversed(p_method, p_args, p_argcount);
  485. //_test_call_error(p_method,error);
  486. }
  487. }
  488. bool Object::has_method(const StringName &p_method) const {
  489. if (p_method == CoreStringNames::get_singleton()->_free) {
  490. return true;
  491. }
  492. if (script_instance && script_instance->has_method(p_method)) {
  493. return true;
  494. }
  495. MethodBind *method = ObjectTypeDB::get_method(get_type_name(), p_method);
  496. if (method) {
  497. return true;
  498. }
  499. return false;
  500. }
  501. Variant Object::getvar(const Variant &p_key, bool *r_valid) const {
  502. if (r_valid)
  503. *r_valid = false;
  504. return Variant();
  505. }
  506. void Object::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) {
  507. if (r_valid)
  508. *r_valid = false;
  509. }
  510. Variant Object::callv(const StringName &p_method, const Array &p_args) {
  511. if (p_args.size() == 0) {
  512. return call(p_method);
  513. }
  514. Vector<Variant> args;
  515. args.resize(p_args.size());
  516. Vector<const Variant *> argptrs;
  517. argptrs.resize(p_args.size());
  518. for (int i = 0; i < p_args.size(); i++) {
  519. args[i] = p_args[i];
  520. argptrs[i] = &args[i];
  521. }
  522. Variant::CallError ce;
  523. return call(p_method, argptrs.ptr(), p_args.size(), ce);
  524. }
  525. Variant Object::call(const StringName &p_name, VARIANT_ARG_DECLARE) {
  526. #if 0
  527. if (p_name==CoreStringNames::get_singleton()->_free) {
  528. #ifdef DEBUG_ENABLED
  529. if (cast_to<Reference>()) {
  530. ERR_EXPLAIN("Can't 'free' a reference.");
  531. ERR_FAIL_V(Variant());
  532. }
  533. #endif
  534. //must be here, must be before everything,
  535. memdelete(this);
  536. return Variant();
  537. }
  538. VARIANT_ARGPTRS;
  539. int argc=0;
  540. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  541. if (argptr[i]->get_type()==Variant::NIL)
  542. break;
  543. argc++;
  544. }
  545. Variant::CallError error;
  546. Variant ret;
  547. if (script_instance) {
  548. ret = script_instance->call(p_name,argptr,argc,error);
  549. if (_test_call_error(p_name,error))
  550. return ret;
  551. }
  552. MethodBind *method=ObjectTypeDB::get_method(get_type_name(),p_name);
  553. if (method) {
  554. Variant ret = method->call(this,argptr,argc,error);
  555. if (_test_call_error(p_name,error))
  556. return ret;
  557. return ret;
  558. } else {
  559. }
  560. return Variant();
  561. #else
  562. VARIANT_ARGPTRS;
  563. int argc = 0;
  564. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  565. if (argptr[i]->get_type() == Variant::NIL)
  566. break;
  567. argc++;
  568. }
  569. Variant::CallError error;
  570. Variant ret = call(p_name, argptr, argc, error);
  571. return ret;
  572. #endif
  573. }
  574. void Object::call_multilevel(const StringName &p_name, VARIANT_ARG_DECLARE) {
  575. #if 0
  576. if (p_name==CoreStringNames::get_singleton()->_free) {
  577. #ifdef DEBUG_ENABLED
  578. if (cast_to<Reference>()) {
  579. ERR_EXPLAIN("Can't 'free' a reference.");
  580. ERR_FAIL();
  581. return;
  582. }
  583. #endif
  584. //must be here, must be before everything,
  585. memdelete(this);
  586. return;
  587. }
  588. VARIANT_ARGPTRS;
  589. int argc=0;
  590. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  591. if (argptr[i]->get_type()==Variant::NIL)
  592. break;
  593. argc++;
  594. }
  595. Variant::CallError error;
  596. if (script_instance) {
  597. script_instance->call(p_name,argptr,argc,error);
  598. _test_call_error(p_name,error);
  599. }
  600. MethodBind *method=ObjectTypeDB::get_method(get_type_name(),p_name);
  601. if (method) {
  602. method->call(this,argptr,argc,error);
  603. _test_call_error(p_name,error);
  604. }
  605. #else
  606. VARIANT_ARGPTRS;
  607. int argc = 0;
  608. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  609. if (argptr[i]->get_type() == Variant::NIL)
  610. break;
  611. argc++;
  612. }
  613. //Variant::CallError error;
  614. call_multilevel(p_name, argptr, argc);
  615. #endif
  616. }
  617. Variant Object::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  618. r_error.error = Variant::CallError::CALL_OK;
  619. if (p_method == CoreStringNames::get_singleton()->_free) {
  620. //free must be here, before anything, always ready
  621. #ifdef DEBUG_ENABLED
  622. if (p_argcount != 0) {
  623. r_error.argument = 0;
  624. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  625. return Variant();
  626. }
  627. if (cast_to<Reference>()) {
  628. r_error.argument = 0;
  629. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  630. ERR_EXPLAIN("Can't 'free' a reference.");
  631. ERR_FAIL_V(Variant());
  632. }
  633. if (_lock_index.get() > 1) {
  634. r_error.argument = 0;
  635. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  636. ERR_EXPLAIN("Object is locked and can't be freed.");
  637. ERR_FAIL_V(Variant());
  638. }
  639. #endif
  640. //must be here, must be before everything,
  641. memdelete(this);
  642. r_error.error = Variant::CallError::CALL_OK;
  643. return Variant();
  644. }
  645. Variant ret;
  646. OBJ_DEBUG_LOCK
  647. if (script_instance) {
  648. ret = script_instance->call(p_method, p_args, p_argcount, r_error);
  649. //force jumptable
  650. switch (r_error.error) {
  651. case Variant::CallError::CALL_OK:
  652. return ret;
  653. case Variant::CallError::CALL_ERROR_INVALID_METHOD:
  654. break;
  655. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT:
  656. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  657. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  658. return ret;
  659. case Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL: {
  660. }
  661. }
  662. }
  663. MethodBind *method = ObjectTypeDB::get_method(get_type_name(), p_method);
  664. if (method) {
  665. ret = method->call(this, p_args, p_argcount, r_error);
  666. } else {
  667. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  668. }
  669. return ret;
  670. }
  671. void Object::notification(int p_notification, bool p_reversed) {
  672. _notificationv(p_notification, p_reversed);
  673. if (script_instance) {
  674. script_instance->notification(p_notification);
  675. }
  676. }
  677. void Object::_changed_callback(Object *p_changed, const char *p_prop) {
  678. }
  679. void Object::add_change_receptor(Object *p_receptor) {
  680. change_receptors.insert(p_receptor);
  681. }
  682. void Object::remove_change_receptor(Object *p_receptor) {
  683. change_receptors.erase(p_receptor);
  684. }
  685. void Object::property_list_changed_notify() {
  686. _change_notify();
  687. }
  688. void Object::cancel_delete() {
  689. _predelete_ok = true;
  690. }
  691. void Object::set_script(const RefPtr &p_script) {
  692. if (script == p_script)
  693. return;
  694. if (script_instance) {
  695. memdelete(script_instance);
  696. script_instance = NULL;
  697. }
  698. script = p_script;
  699. Ref<Script> s(script);
  700. if (!s.is_null() && s->can_instance()) {
  701. OBJ_DEBUG_LOCK
  702. script_instance = s->instance_create(this);
  703. }
  704. _change_notify("script/script");
  705. emit_signal(CoreStringNames::get_singleton()->script_changed);
  706. }
  707. void Object::set_script_instance(ScriptInstance *p_instance) {
  708. if (script_instance == p_instance)
  709. return;
  710. if (script_instance)
  711. memdelete(script_instance);
  712. script_instance = p_instance;
  713. if (p_instance)
  714. script = p_instance->get_script().get_ref_ptr();
  715. else
  716. script = RefPtr();
  717. }
  718. RefPtr Object::get_script() const {
  719. return script;
  720. }
  721. bool Object::has_meta(const String &p_name) const {
  722. return metadata.has(p_name);
  723. }
  724. void Object::set_meta(const String &p_name, const Variant &p_value) {
  725. if (p_value.get_type() == Variant::NIL) {
  726. metadata.erase(p_name);
  727. return;
  728. };
  729. metadata[p_name] = p_value;
  730. }
  731. Variant Object::get_meta(const String &p_name) const {
  732. ERR_FAIL_COND_V(!metadata.has(p_name), Variant());
  733. return metadata[p_name];
  734. }
  735. Array Object::_get_property_list_bind() const {
  736. List<PropertyInfo> lpi;
  737. get_property_list(&lpi);
  738. return convert_property_list(&lpi);
  739. }
  740. static Dictionary _get_dict_from_method(const MethodInfo &mi) {
  741. Dictionary d;
  742. d["name"] = mi.name;
  743. d["args"] = convert_property_list(&mi.arguments);
  744. Array da;
  745. for (int i = 0; i < mi.default_arguments.size(); i++)
  746. da.push_back(mi.default_arguments[i]);
  747. d["default_args"] = da;
  748. d["flags"] = mi.flags;
  749. d["id"] = mi.id;
  750. Dictionary r;
  751. r["type"] = mi.return_val.type;
  752. r["hint"] = mi.return_val.hint;
  753. r["hint_string"] = mi.return_val.hint_string;
  754. d["return_type"] = r;
  755. return d;
  756. }
  757. Array Object::_get_method_list_bind() const {
  758. List<MethodInfo> ml;
  759. get_method_list(&ml);
  760. Array ret;
  761. for (List<MethodInfo>::Element *E = ml.front(); E; E = E->next()) {
  762. Dictionary d = _get_dict_from_method(E->get());
  763. //va.push_back(d);
  764. ret.push_back(d);
  765. }
  766. return ret;
  767. }
  768. DVector<String> Object::_get_meta_list_bind() const {
  769. DVector<String> _metaret;
  770. List<Variant> keys;
  771. metadata.get_key_list(&keys);
  772. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  773. _metaret.push_back(E->get());
  774. }
  775. return _metaret;
  776. }
  777. void Object::get_meta_list(List<String> *p_list) const {
  778. List<Variant> keys;
  779. metadata.get_key_list(&keys);
  780. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  781. p_list->push_back(E->get());
  782. }
  783. }
  784. void Object::add_user_signal(const MethodInfo &p_signal) {
  785. ERR_FAIL_COND(p_signal.name == "");
  786. ERR_FAIL_COND(ObjectTypeDB::has_signal(get_type_name(), p_signal.name));
  787. ERR_FAIL_COND(signal_map.has(p_signal.name));
  788. Signal s;
  789. s.user = p_signal;
  790. signal_map[p_signal.name] = s;
  791. }
  792. bool Object::_has_user_signal(const StringName &p_name) const {
  793. if (!signal_map.has(p_name))
  794. return false;
  795. return signal_map[p_name].user.name.length() > 0;
  796. }
  797. struct _ObjectSignalDisconnectData {
  798. StringName signal;
  799. Object *target;
  800. StringName method;
  801. };
  802. #if 0
  803. void Object::_emit_signal(const StringName& p_name,const Array& p_pargs){
  804. Variant args[VARIANT_ARG_MAX];
  805. int count = p_pargs.size();
  806. for(int i=0;i<count;i++) {
  807. args[i]=p_pargs[i];
  808. }
  809. emit_signal(p_name,VARIANT_ARGS_FROM_ARRAY(args));
  810. }
  811. #endif
  812. Variant Object::_emit_signal(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  813. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  814. ERR_FAIL_COND_V(p_argcount < 1, Variant());
  815. if (p_args[0]->get_type() != Variant::STRING) {
  816. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  817. r_error.argument = 0;
  818. r_error.expected = Variant::STRING;
  819. ERR_FAIL_COND_V(p_args[0]->get_type() != Variant::STRING, Variant());
  820. }
  821. r_error.error = Variant::CallError::CALL_OK;
  822. StringName signal = *p_args[0];
  823. const Variant **args = NULL;
  824. int argc = p_argcount - 1;
  825. if (argc) {
  826. args = &p_args[1];
  827. }
  828. emit_signal(signal, args, argc);
  829. return Variant();
  830. }
  831. void Object::emit_signal(const StringName &p_name, const Variant **p_args, int p_argcount) {
  832. if (_block_signals)
  833. return; //no emit, signals blocked
  834. Signal *s = signal_map.getptr(p_name);
  835. if (!s) {
  836. #ifdef DEBUG_ENABLED
  837. bool signal_is_valid = ObjectTypeDB::has_signal(get_type_name(), p_name);
  838. //check in script
  839. if (!signal_is_valid && !script.is_null() && !Ref<Script>(script)->has_script_signal(p_name)) {
  840. ERR_EXPLAIN("Can't emit non-existing signal " + String("\"") + p_name + "\".");
  841. ERR_FAIL();
  842. }
  843. #endif
  844. //not connected? just return
  845. return;
  846. }
  847. List<_ObjectSignalDisconnectData> disconnect_data;
  848. //copy on write will ensure that disconnecting the signal or even deleting the object will not affect the signal calling.
  849. //this happens automatically and will not change the performance of calling.
  850. //awesome, isn't it?
  851. VMap<Signal::Target, Signal::Slot> slot_map = s->slot_map;
  852. int ssize = slot_map.size();
  853. OBJ_DEBUG_LOCK
  854. Vector<const Variant *> bind_mem;
  855. for (int i = 0; i < ssize; i++) {
  856. const Connection &c = slot_map.getv(i).conn;
  857. Object *target;
  858. #ifdef DEBUG_ENABLED
  859. target = ObjectDB::get_instance(slot_map.getk(i)._id);
  860. ERR_CONTINUE(!target);
  861. #else
  862. target = c.target;
  863. #endif
  864. const Variant **args = p_args;
  865. int argc = p_argcount;
  866. if (c.binds.size()) {
  867. //handle binds
  868. bind_mem.resize(p_argcount + c.binds.size());
  869. for (int j = 0; j < p_argcount; j++) {
  870. bind_mem[j] = p_args[j];
  871. }
  872. for (int j = 0; j < c.binds.size(); j++) {
  873. bind_mem[p_argcount + j] = &c.binds[j];
  874. }
  875. args = bind_mem.ptr();
  876. argc = bind_mem.size();
  877. }
  878. if (c.flags & CONNECT_DEFERRED) {
  879. MessageQueue::get_singleton()->push_call(target->get_instance_ID(), c.method, args, argc, true);
  880. } else {
  881. Variant::CallError ce;
  882. target->call(c.method, args, argc, ce);
  883. if (ce.error != Variant::CallError::CALL_OK) {
  884. if (ce.error == Variant::CallError::CALL_ERROR_INVALID_METHOD && !ObjectTypeDB::type_exists(target->get_type_name())) {
  885. //most likely object is not initialized yet, do not throw error.
  886. } else {
  887. ERR_PRINTS("Error calling method from signal '" + String(p_name) + "': " + Variant::get_call_error_text(target, c.method, args, argc, ce));
  888. }
  889. }
  890. }
  891. if (c.flags & CONNECT_ONESHOT) {
  892. _ObjectSignalDisconnectData dd;
  893. dd.signal = p_name;
  894. dd.target = target;
  895. dd.method = c.method;
  896. disconnect_data.push_back(dd);
  897. }
  898. }
  899. while (!disconnect_data.empty()) {
  900. const _ObjectSignalDisconnectData &dd = disconnect_data.front()->get();
  901. disconnect(dd.signal, dd.target, dd.method);
  902. disconnect_data.pop_front();
  903. }
  904. }
  905. void Object::emit_signal(const StringName &p_name, VARIANT_ARG_DECLARE) {
  906. VARIANT_ARGPTRS;
  907. int argc = 0;
  908. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  909. if (argptr[i]->get_type() == Variant::NIL)
  910. break;
  911. argc++;
  912. }
  913. emit_signal(p_name, argptr, argc);
  914. }
  915. void Object::_add_user_signal(const String &p_name, const Array &p_args) {
  916. // this version of add_user_signal is meant to be used from scripts or external apis
  917. // without access to ADD_SIGNAL in bind_methods
  918. // added events are per instance, as opposed to the other ones, which are global
  919. MethodInfo mi;
  920. mi.name = p_name;
  921. for (int i = 0; i < p_args.size(); i++) {
  922. Dictionary d = p_args[i];
  923. PropertyInfo param;
  924. if (d.has("name"))
  925. param.name = d["name"];
  926. if (d.has("type"))
  927. param.type = (Variant::Type)(int)d["type"];
  928. mi.arguments.push_back(param);
  929. }
  930. add_user_signal(mi);
  931. }
  932. #if 0
  933. void Object::_emit_signal(const StringName& p_name,const Array& p_pargs){
  934. Variant args[VARIANT_ARG_MAX];
  935. int count = p_pargs.size();
  936. for(int i=0;i<count;i++) {
  937. args[i]=p_pargs[i];
  938. }
  939. emit_signal(p_name,VARIANT_ARGS_FROM_ARRAY(args));
  940. }
  941. #endif
  942. Array Object::_get_signal_list() const {
  943. List<MethodInfo> signal_list;
  944. get_signal_list(&signal_list);
  945. Array ret;
  946. for (List<MethodInfo>::Element *E = signal_list.front(); E; E = E->next()) {
  947. ret.push_back(_get_dict_from_method(E->get()));
  948. }
  949. return ret;
  950. }
  951. Array Object::_get_signal_connection_list(const String &p_signal) const {
  952. List<Connection> conns;
  953. get_all_signal_connections(&conns);
  954. Array ret;
  955. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  956. Connection &c = E->get();
  957. if (c.signal == p_signal) {
  958. Dictionary rc;
  959. rc["signal"] = c.signal;
  960. rc["method"] = c.method;
  961. rc["source"] = c.source;
  962. rc["target"] = c.target;
  963. rc["binds"] = c.binds;
  964. rc["flags"] = c.flags;
  965. ret.push_back(rc);
  966. }
  967. }
  968. return ret;
  969. }
  970. void Object::get_signal_list(List<MethodInfo> *p_signals) const {
  971. if (!script.is_null()) {
  972. Ref<Script>(script)->get_script_signal_list(p_signals);
  973. }
  974. ObjectTypeDB::get_signal_list(get_type_name(), p_signals);
  975. //find maybe usersignals?
  976. const StringName *S = NULL;
  977. while ((S = signal_map.next(S))) {
  978. if (signal_map[*S].user.name != "") {
  979. //user signal
  980. p_signals->push_back(signal_map[*S].user);
  981. }
  982. }
  983. }
  984. void Object::get_all_signal_connections(List<Connection> *p_connections) const {
  985. const StringName *S = NULL;
  986. while ((S = signal_map.next(S))) {
  987. const Signal *s = &signal_map[*S];
  988. for (int i = 0; i < s->slot_map.size(); i++) {
  989. p_connections->push_back(s->slot_map.getv(i).conn);
  990. }
  991. }
  992. }
  993. void Object::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
  994. const Signal *s = signal_map.getptr(p_signal);
  995. if (!s)
  996. return; //nothing
  997. for (int i = 0; i < s->slot_map.size(); i++)
  998. p_connections->push_back(s->slot_map.getv(i).conn);
  999. }
  1000. bool Object::has_persistent_signal_connections() const {
  1001. const StringName *S = NULL;
  1002. while ((S = signal_map.next(S))) {
  1003. const Signal *s = &signal_map[*S];
  1004. for (int i = 0; i < s->slot_map.size(); i++) {
  1005. if (s->slot_map.getv(i).conn.flags & CONNECT_PERSIST)
  1006. return true;
  1007. }
  1008. }
  1009. return false;
  1010. }
  1011. void Object::get_signals_connected_to_this(List<Connection> *p_connections) const {
  1012. for (const List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1013. p_connections->push_back(E->get());
  1014. }
  1015. }
  1016. 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) {
  1017. ERR_FAIL_NULL_V(p_to_object, ERR_INVALID_PARAMETER);
  1018. Signal *s = signal_map.getptr(p_signal);
  1019. if (!s) {
  1020. bool signal_is_valid = ObjectTypeDB::has_signal(get_type_name(), p_signal);
  1021. //check in script
  1022. if (!signal_is_valid && !script.is_null() && Ref<Script>(script)->has_script_signal(p_signal))
  1023. signal_is_valid = true;
  1024. if (!signal_is_valid) {
  1025. ERR_EXPLAIN("In Object of type '" + String(get_type()) + "': Attempt to connect nonexistent signal '" + p_signal + "' to method '" + p_to_object->get_type() + "." + p_to_method + "'");
  1026. ERR_FAIL_COND_V(!signal_is_valid, ERR_INVALID_PARAMETER);
  1027. }
  1028. signal_map[p_signal] = Signal();
  1029. s = &signal_map[p_signal];
  1030. }
  1031. Signal::Target target(p_to_object->get_instance_ID(), p_to_method);
  1032. if (s->slot_map.has(target)) {
  1033. ERR_EXPLAIN("Signal '" + p_signal + "' is already connected to given method '" + p_to_method + "' in that object.");
  1034. ERR_FAIL_COND_V(s->slot_map.has(target), ERR_INVALID_PARAMETER);
  1035. }
  1036. Signal::Slot slot;
  1037. Connection conn;
  1038. conn.source = this;
  1039. conn.target = p_to_object;
  1040. conn.method = p_to_method;
  1041. conn.signal = p_signal;
  1042. conn.flags = p_flags;
  1043. conn.binds = p_binds;
  1044. slot.conn = conn;
  1045. slot.cE = p_to_object->connections.push_back(conn);
  1046. s->slot_map[target] = slot;
  1047. return OK;
  1048. }
  1049. bool Object::is_connected(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method) const {
  1050. ERR_FAIL_NULL_V(p_to_object, false);
  1051. const Signal *s = signal_map.getptr(p_signal);
  1052. if (!s) {
  1053. bool signal_is_valid = ObjectTypeDB::has_signal(get_type_name(), p_signal);
  1054. if (signal_is_valid)
  1055. return false;
  1056. if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal))
  1057. return false;
  1058. ERR_EXPLAIN("Nonexistent signal: " + p_signal);
  1059. ERR_FAIL_COND_V(!s, false);
  1060. }
  1061. Signal::Target target(p_to_object->get_instance_ID(), p_to_method);
  1062. return s->slot_map.has(target);
  1063. //const Map<Signal::Target,Signal::Slot>::Element *E = s->slot_map.find(target);
  1064. //return (E!=NULL);
  1065. }
  1066. void Object::disconnect(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method) {
  1067. ERR_FAIL_NULL(p_to_object);
  1068. Signal *s = signal_map.getptr(p_signal);
  1069. if (!s) {
  1070. ERR_EXPLAIN("Nonexistent signal: " + p_signal);
  1071. ERR_FAIL_COND(!s);
  1072. }
  1073. if (s->lock > 0) {
  1074. ERR_EXPLAIN("Attempt to disconnect signal '" + p_signal + "' while emitting (locks: " + itos(s->lock) + ")");
  1075. ERR_FAIL_COND(s->lock > 0);
  1076. }
  1077. Signal::Target target(p_to_object->get_instance_ID(), p_to_method);
  1078. if (!s->slot_map.has(target)) {
  1079. ERR_EXPLAIN("Disconnecting nonexistent signal '" + p_signal + "', slot: " + itos(target._id) + ":" + target.method);
  1080. ERR_FAIL();
  1081. }
  1082. p_to_object->connections.erase(s->slot_map[target].cE);
  1083. s->slot_map.erase(target);
  1084. if (s->slot_map.empty() && ObjectTypeDB::has_signal(get_type_name(), p_signal)) {
  1085. //not user signal, delete
  1086. signal_map.erase(p_signal);
  1087. }
  1088. }
  1089. void Object::_set_bind(const String &p_set, const Variant &p_value) {
  1090. set(p_set, p_value);
  1091. }
  1092. Variant Object::_get_bind(const String &p_name) const {
  1093. return get(p_name);
  1094. }
  1095. void Object::initialize_type() {
  1096. static bool initialized = false;
  1097. if (initialized)
  1098. return;
  1099. ObjectTypeDB::_add_type<Object>();
  1100. _bind_methods();
  1101. initialized = true;
  1102. }
  1103. StringName Object::XL_MESSAGE(const StringName &p_message) const {
  1104. if (!_can_translate || !TranslationServer::get_singleton())
  1105. return p_message;
  1106. return TranslationServer::get_singleton()->translate(p_message);
  1107. }
  1108. StringName Object::tr(const StringName &p_message) const {
  1109. return XL_MESSAGE(p_message);
  1110. }
  1111. void Object::_clear_internal_resource_paths(const Variant &p_var) {
  1112. switch (p_var.get_type()) {
  1113. case Variant::OBJECT: {
  1114. RES r = p_var;
  1115. if (!r.is_valid())
  1116. return;
  1117. if (!r->get_path().begins_with("res://") || r->get_path().find("::") == -1)
  1118. return; //not an internal resource
  1119. Object *object = p_var;
  1120. if (!object)
  1121. return;
  1122. r->set_path("");
  1123. r->clear_internal_resource_paths();
  1124. } break;
  1125. case Variant::ARRAY: {
  1126. Array a = p_var;
  1127. for (int i = 0; i < a.size(); i++) {
  1128. _clear_internal_resource_paths(a[i]);
  1129. }
  1130. } break;
  1131. case Variant::DICTIONARY: {
  1132. Dictionary d = p_var;
  1133. List<Variant> keys;
  1134. d.get_key_list(&keys);
  1135. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1136. _clear_internal_resource_paths(E->get());
  1137. _clear_internal_resource_paths(d[E->get()]);
  1138. }
  1139. } break;
  1140. }
  1141. }
  1142. void Object::clear_internal_resource_paths() {
  1143. List<PropertyInfo> pinfo;
  1144. get_property_list(&pinfo);
  1145. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1146. _clear_internal_resource_paths(get(E->get().name));
  1147. }
  1148. }
  1149. void Object::_bind_methods() {
  1150. ObjectTypeDB::bind_method(_MD("get_type"), &Object::get_type);
  1151. ObjectTypeDB::bind_method(_MD("is_type", "type"), &Object::is_type);
  1152. ObjectTypeDB::bind_method(_MD("set", "property", "value"), &Object::_set_bind);
  1153. ObjectTypeDB::bind_method(_MD("get", "property"), &Object::_get_bind);
  1154. ObjectTypeDB::bind_method(_MD("get_property_list"), &Object::_get_property_list_bind);
  1155. ObjectTypeDB::bind_method(_MD("get_method_list"), &Object::_get_method_list_bind);
  1156. ObjectTypeDB::bind_method(_MD("notification", "what", "reversed"), &Object::notification, DEFVAL(false));
  1157. ObjectTypeDB::bind_method(_MD("get_instance_ID"), &Object::get_instance_ID);
  1158. ObjectTypeDB::bind_method(_MD("set_script", "script:Script"), &Object::set_script);
  1159. ObjectTypeDB::bind_method(_MD("get_script:Script"), &Object::get_script);
  1160. ObjectTypeDB::bind_method(_MD("set_meta", "name", "value"), &Object::set_meta);
  1161. ObjectTypeDB::bind_method(_MD("get_meta:Variant", "name", "value"), &Object::get_meta);
  1162. ObjectTypeDB::bind_method(_MD("has_meta", "name"), &Object::has_meta);
  1163. ObjectTypeDB::bind_method(_MD("get_meta_list"), &Object::_get_meta_list_bind);
  1164. //todo reimplement this per language so all 5 arguments can be called
  1165. // ObjectTypeDB::bind_method(_MD("call","method","arg1","arg2","arg3","arg4"),&Object::_call_bind,DEFVAL(Variant()),DEFVAL(Variant()),DEFVAL(Variant()),DEFVAL(Variant()));
  1166. // ObjectTypeDB::bind_method(_MD("call_deferred","method","arg1","arg2","arg3","arg4"),&Object::_call_deferred_bind,DEFVAL(Variant()),DEFVAL(Variant()),DEFVAL(Variant()),DEFVAL(Variant()));
  1167. ObjectTypeDB::bind_method(_MD("add_user_signal", "signal", "arguments"), &Object::_add_user_signal, DEFVAL(Array()));
  1168. ObjectTypeDB::bind_method(_MD("has_user_signal", "signal"), &Object::_has_user_signal);
  1169. // ObjectTypeDB::bind_method(_MD("emit_signal","signal","arguments"),&Object::_emit_signal,DEFVAL(Array()));
  1170. {
  1171. MethodInfo mi;
  1172. mi.name = "emit_signal";
  1173. mi.arguments.push_back(PropertyInfo(Variant::STRING, "signal"));
  1174. Vector<Variant> defargs;
  1175. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1176. mi.arguments.push_back(PropertyInfo(Variant::NIL, "arg" + itos(i)));
  1177. defargs.push_back(Variant());
  1178. }
  1179. ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT, "emit_signal", &Object::_emit_signal, mi, defargs);
  1180. }
  1181. {
  1182. MethodInfo mi;
  1183. mi.name = "call";
  1184. mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  1185. Vector<Variant> defargs;
  1186. for (int i = 0; i < 10; i++) {
  1187. mi.arguments.push_back(PropertyInfo(Variant::NIL, "arg" + itos(i)));
  1188. defargs.push_back(Variant());
  1189. }
  1190. ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT, "call", &Object::_call_bind, mi, defargs);
  1191. }
  1192. {
  1193. MethodInfo mi;
  1194. mi.name = "call_deferred";
  1195. mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  1196. Vector<Variant> defargs;
  1197. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1198. mi.arguments.push_back(PropertyInfo(Variant::NIL, "arg" + itos(i)));
  1199. defargs.push_back(Variant());
  1200. }
  1201. ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT, "call_deferred", &Object::_call_deferred_bind, mi, defargs);
  1202. }
  1203. ObjectTypeDB::bind_method(_MD("callv:Variant", "method", "arg_array"), &Object::callv);
  1204. ObjectTypeDB::bind_method(_MD("has_method", "method"), &Object::has_method);
  1205. ObjectTypeDB::bind_method(_MD("get_signal_list"), &Object::_get_signal_list);
  1206. ObjectTypeDB::bind_method(_MD("get_signal_connection_list", "signal"), &Object::_get_signal_connection_list);
  1207. ObjectTypeDB::bind_method(_MD("connect", "signal", "target:Object", "method", "binds", "flags"), &Object::connect, DEFVAL(Array()), DEFVAL(0));
  1208. ObjectTypeDB::bind_method(_MD("disconnect", "signal", "target:Object", "method"), &Object::disconnect);
  1209. ObjectTypeDB::bind_method(_MD("is_connected", "signal", "target:Object", "method"), &Object::is_connected);
  1210. ObjectTypeDB::bind_method(_MD("set_block_signals", "enable"), &Object::set_block_signals);
  1211. ObjectTypeDB::bind_method(_MD("is_blocking_signals"), &Object::is_blocking_signals);
  1212. ObjectTypeDB::bind_method(_MD("set_message_translation", "enable"), &Object::set_message_translation);
  1213. ObjectTypeDB::bind_method(_MD("can_translate_messages"), &Object::can_translate_messages);
  1214. ObjectTypeDB::bind_method(_MD("property_list_changed_notify"), &Object::property_list_changed_notify);
  1215. ObjectTypeDB::bind_method(_MD("XL_MESSAGE", "message"), &Object::XL_MESSAGE);
  1216. ObjectTypeDB::bind_method(_MD("tr", "message"), &Object::tr);
  1217. ObjectTypeDB::bind_method(_MD("is_queued_for_deletion"), &Object::is_queued_for_deletion);
  1218. ObjectTypeDB::add_virtual_method("Object", MethodInfo("free"), false);
  1219. ADD_SIGNAL(MethodInfo("script_changed"));
  1220. BIND_VMETHOD(MethodInfo("_notification", PropertyInfo(Variant::INT, "what")));
  1221. BIND_VMETHOD(MethodInfo("_set", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::NIL, "value")));
  1222. #ifdef TOOLS_ENABLED
  1223. MethodInfo miget("_get", PropertyInfo(Variant::STRING, "property"));
  1224. miget.return_val.name = "var";
  1225. BIND_VMETHOD(miget);
  1226. MethodInfo plget("_get_property_list");
  1227. plget.return_val.type = Variant::ARRAY;
  1228. BIND_VMETHOD(plget);
  1229. #endif
  1230. BIND_VMETHOD(MethodInfo("_init"));
  1231. BIND_CONSTANT(NOTIFICATION_POSTINITIALIZE);
  1232. BIND_CONSTANT(NOTIFICATION_PREDELETE);
  1233. BIND_CONSTANT(CONNECT_DEFERRED);
  1234. BIND_CONSTANT(CONNECT_PERSIST);
  1235. BIND_CONSTANT(CONNECT_ONESHOT);
  1236. }
  1237. void Object::call_deferred(const StringName &p_method, VARIANT_ARG_DECLARE) {
  1238. MessageQueue::get_singleton()->push_call(this, p_method, VARIANT_ARG_PASS);
  1239. }
  1240. void Object::set_block_signals(bool p_block) {
  1241. _block_signals = p_block;
  1242. }
  1243. bool Object::is_blocking_signals() const {
  1244. return _block_signals;
  1245. }
  1246. void Object::get_translatable_strings(List<String> *p_strings) const {
  1247. List<PropertyInfo> plist;
  1248. get_property_list(&plist);
  1249. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1250. if (!(E->get().usage & PROPERTY_USAGE_INTERNATIONALIZED))
  1251. continue;
  1252. String text = get(E->get().name);
  1253. if (text == "")
  1254. continue;
  1255. p_strings->push_back(text);
  1256. }
  1257. }
  1258. Variant::Type Object::get_static_property_type(const StringName &p_property, bool *r_valid) const {
  1259. bool valid;
  1260. Variant::Type t = ObjectTypeDB::get_property_type(get_type_name(), p_property, &valid);
  1261. if (valid) {
  1262. if (r_valid)
  1263. *r_valid = true;
  1264. return t;
  1265. }
  1266. if (get_script_instance()) {
  1267. return get_script_instance()->get_property_type(p_property, r_valid);
  1268. }
  1269. if (r_valid)
  1270. *r_valid = false;
  1271. return Variant::NIL;
  1272. }
  1273. bool Object::is_queued_for_deletion() const {
  1274. return _is_queued_for_deletion;
  1275. }
  1276. #ifdef TOOLS_ENABLED
  1277. void Object::set_edited(bool p_edited) {
  1278. _edited = p_edited;
  1279. _edited_version++;
  1280. }
  1281. bool Object::is_edited() const {
  1282. return _edited;
  1283. }
  1284. uint32_t Object::get_edited_version() const {
  1285. return _edited_version;
  1286. }
  1287. #endif
  1288. Object::Object() {
  1289. _type_ptr = NULL;
  1290. _block_signals = false;
  1291. _predelete_ok = 0;
  1292. _instance_ID = 0;
  1293. _instance_ID = ObjectDB::add_instance(this);
  1294. _can_translate = true;
  1295. _is_queued_for_deletion = false;
  1296. script_instance = NULL;
  1297. #ifdef TOOLS_ENABLED
  1298. _edited = false;
  1299. _edited_version = 0;
  1300. #endif
  1301. #ifdef DEBUG_ENABLED
  1302. _lock_index.init(1);
  1303. #endif
  1304. }
  1305. Object::~Object() {
  1306. if (script_instance)
  1307. memdelete(script_instance);
  1308. script_instance = NULL;
  1309. List<Connection> sconnections;
  1310. const StringName *S = NULL;
  1311. while ((S = signal_map.next(S))) {
  1312. Signal *s = &signal_map[*S];
  1313. ERR_EXPLAIN("Attempt to delete an object in the middle of a signal emission from it");
  1314. ERR_CONTINUE(s->lock > 0);
  1315. for (int i = 0; i < s->slot_map.size(); i++) {
  1316. sconnections.push_back(s->slot_map.getv(i).conn);
  1317. }
  1318. }
  1319. for (List<Connection>::Element *E = sconnections.front(); E; E = E->next()) {
  1320. Connection &c = E->get();
  1321. ERR_CONTINUE(c.source != this); //bug?
  1322. this->disconnect(c.signal, c.target, c.method);
  1323. }
  1324. while (connections.size()) {
  1325. Connection c = connections.front()->get();
  1326. c.source->disconnect(c.signal, c.target, c.method);
  1327. }
  1328. ObjectDB::remove_instance(this);
  1329. _instance_ID = 0;
  1330. _predelete_ok = 2;
  1331. }
  1332. bool predelete_handler(Object *p_object) {
  1333. return p_object->_predelete();
  1334. }
  1335. void postinitialize_handler(Object *p_object) {
  1336. p_object->_postinitialize();
  1337. }
  1338. HashMap<uint32_t, Object *> ObjectDB::instances;
  1339. uint32_t ObjectDB::instance_counter = 1;
  1340. HashMap<Object *, ObjectID, ObjectDB::ObjectPtrHash> ObjectDB::instance_checks;
  1341. uint32_t ObjectDB::add_instance(Object *p_object) {
  1342. GLOBAL_LOCK_FUNCTION;
  1343. ERR_FAIL_COND_V(p_object->get_instance_ID() != 0, 0);
  1344. instances[++instance_counter] = p_object;
  1345. #ifdef DEBUG_ENABLED
  1346. instance_checks[p_object] = instance_counter;
  1347. #endif
  1348. return instance_counter;
  1349. }
  1350. void ObjectDB::remove_instance(Object *p_object) {
  1351. GLOBAL_LOCK_FUNCTION;
  1352. instances.erase(p_object->get_instance_ID());
  1353. #ifdef DEBUG_ENABLED
  1354. instance_checks.erase(p_object);
  1355. #endif
  1356. }
  1357. Object *ObjectDB::get_instance(uint32_t p_instance_ID) {
  1358. GLOBAL_LOCK_FUNCTION;
  1359. Object **obj = instances.getptr(p_instance_ID);
  1360. if (!obj)
  1361. return NULL;
  1362. return *obj;
  1363. }
  1364. void ObjectDB::debug_objects(DebugFunc p_func) {
  1365. GLOBAL_LOCK_FUNCTION;
  1366. const uint32_t *K = NULL;
  1367. while ((K = instances.next(K))) {
  1368. p_func(instances[*K]);
  1369. }
  1370. }
  1371. void Object::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1372. }
  1373. int ObjectDB::get_object_count() {
  1374. GLOBAL_LOCK_FUNCTION;
  1375. return instances.size();
  1376. }
  1377. void ObjectDB::cleanup() {
  1378. GLOBAL_LOCK_FUNCTION;
  1379. if (instances.size()) {
  1380. WARN_PRINT("ObjectDB Instances still exist!");
  1381. if (OS::get_singleton()->is_stdout_verbose()) {
  1382. const uint32_t *K = NULL;
  1383. while ((K = instances.next(K))) {
  1384. String node_name;
  1385. if (instances[*K]->is_type("Node"))
  1386. node_name = " - Node Name: " + String(instances[*K]->call("get_name"));
  1387. if (instances[*K]->is_type("Resource"))
  1388. node_name = " - Resource Name: " + String(instances[*K]->call("get_name")) + " Path: " + String(instances[*K]->call("get_path"));
  1389. print_line("Leaked Instance: " + String(instances[*K]->get_type()) + ":" + itos(*K) + node_name);
  1390. }
  1391. }
  1392. }
  1393. instances.clear();
  1394. instance_checks.clear();
  1395. }