object.cpp 46 KB

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