object.cpp 46 KB

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