object.cpp 56 KB

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