object.cpp 54 KB

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