object.cpp 54 KB

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