object.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  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. if (_extension && _extension->set) {
  331. // C style pointer casts should never trigger a compiler warning because the risk is assumed by the user, so GCC should keep quiet about it.
  332. #if defined(__GNUC__) && !defined(__clang__)
  333. #pragma GCC diagnostic push
  334. #pragma GCC diagnostic ignored "-Wignored-qualifiers"
  335. #endif
  336. if (_extension->set(_extension_instance, (const GDNativeStringNamePtr)&p_name, (const GDNativeVariantPtr)&p_value)) {
  337. if (r_valid) {
  338. *r_valid = true;
  339. }
  340. return;
  341. }
  342. #if defined(__GNUC__) && !defined(__clang__)
  343. #pragma GCC diagnostic pop
  344. #endif
  345. }
  346. //try built-in setgetter
  347. {
  348. if (ClassDB::set_property(this, p_name, p_value, r_valid)) {
  349. /*
  350. if (r_valid)
  351. *r_valid=true;
  352. */
  353. return;
  354. }
  355. }
  356. if (p_name == CoreStringNames::get_singleton()->_script) {
  357. set_script(p_value);
  358. if (r_valid) {
  359. *r_valid = true;
  360. }
  361. return;
  362. } else if (p_name == CoreStringNames::get_singleton()->_meta) {
  363. //set_meta(p_name,p_value);
  364. metadata = p_value.duplicate();
  365. if (r_valid) {
  366. *r_valid = true;
  367. }
  368. return;
  369. }
  370. //something inside the object... :|
  371. bool success = _setv(p_name, p_value);
  372. if (success) {
  373. if (r_valid) {
  374. *r_valid = true;
  375. }
  376. return;
  377. }
  378. #ifdef TOOLS_ENABLED
  379. if (script_instance) {
  380. bool valid;
  381. script_instance->property_set_fallback(p_name, p_value, &valid);
  382. if (valid) {
  383. if (r_valid) {
  384. *r_valid = true;
  385. }
  386. return;
  387. }
  388. }
  389. #endif
  390. if (r_valid) {
  391. *r_valid = false;
  392. }
  393. }
  394. Variant Object::get(const StringName &p_name, bool *r_valid) const {
  395. Variant ret;
  396. if (script_instance) {
  397. if (script_instance->get(p_name, ret)) {
  398. if (r_valid) {
  399. *r_valid = true;
  400. }
  401. return ret;
  402. }
  403. }
  404. if (_extension && _extension->get) {
  405. // C style pointer casts should never trigger a compiler warning because the risk is assumed by the user, so GCC should keep quiet about it.
  406. #if defined(__GNUC__) && !defined(__clang__)
  407. #pragma GCC diagnostic push
  408. #pragma GCC diagnostic ignored "-Wignored-qualifiers"
  409. #endif
  410. if (_extension->get(_extension_instance, (const GDNativeStringNamePtr)&p_name, (GDNativeVariantPtr)&ret)) {
  411. if (r_valid) {
  412. *r_valid = true;
  413. }
  414. return ret;
  415. }
  416. #if defined(__GNUC__) && !defined(__clang__)
  417. #pragma GCC diagnostic pop
  418. #endif
  419. }
  420. //try built-in setgetter
  421. {
  422. if (ClassDB::get_property(const_cast<Object *>(this), p_name, ret)) {
  423. if (r_valid) {
  424. *r_valid = true;
  425. }
  426. return ret;
  427. }
  428. }
  429. if (p_name == CoreStringNames::get_singleton()->_script) {
  430. ret = get_script();
  431. if (r_valid) {
  432. *r_valid = true;
  433. }
  434. return ret;
  435. } else if (p_name == CoreStringNames::get_singleton()->_meta) {
  436. ret = metadata;
  437. if (r_valid) {
  438. *r_valid = true;
  439. }
  440. return ret;
  441. } else {
  442. //something inside the object... :|
  443. bool success = _getv(p_name, ret);
  444. if (success) {
  445. if (r_valid) {
  446. *r_valid = true;
  447. }
  448. return ret;
  449. }
  450. #ifdef TOOLS_ENABLED
  451. if (script_instance) {
  452. bool valid;
  453. ret = script_instance->property_get_fallback(p_name, &valid);
  454. if (valid) {
  455. if (r_valid) {
  456. *r_valid = true;
  457. }
  458. return ret;
  459. }
  460. }
  461. #endif
  462. if (r_valid) {
  463. *r_valid = false;
  464. }
  465. return Variant();
  466. }
  467. }
  468. void Object::set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid) {
  469. if (p_names.is_empty()) {
  470. if (r_valid) {
  471. *r_valid = false;
  472. }
  473. return;
  474. }
  475. if (p_names.size() == 1) {
  476. set(p_names[0], p_value, r_valid);
  477. return;
  478. }
  479. bool valid = false;
  480. if (!r_valid) {
  481. r_valid = &valid;
  482. }
  483. List<Variant> value_stack;
  484. value_stack.push_back(get(p_names[0], r_valid));
  485. if (!*r_valid) {
  486. value_stack.clear();
  487. return;
  488. }
  489. for (int i = 1; i < p_names.size() - 1; i++) {
  490. value_stack.push_back(value_stack.back()->get().get_named(p_names[i], valid));
  491. if (r_valid) {
  492. *r_valid = valid;
  493. }
  494. if (!valid) {
  495. value_stack.clear();
  496. return;
  497. }
  498. }
  499. value_stack.push_back(p_value); // p_names[p_names.size() - 1]
  500. for (int i = p_names.size() - 1; i > 0; i--) {
  501. value_stack.back()->prev()->get().set_named(p_names[i], value_stack.back()->get(), valid);
  502. value_stack.pop_back();
  503. if (r_valid) {
  504. *r_valid = valid;
  505. }
  506. if (!valid) {
  507. value_stack.clear();
  508. return;
  509. }
  510. }
  511. set(p_names[0], value_stack.back()->get(), r_valid);
  512. value_stack.pop_back();
  513. ERR_FAIL_COND(!value_stack.is_empty());
  514. }
  515. Variant Object::get_indexed(const Vector<StringName> &p_names, bool *r_valid) const {
  516. if (p_names.is_empty()) {
  517. if (r_valid) {
  518. *r_valid = false;
  519. }
  520. return Variant();
  521. }
  522. bool valid = false;
  523. Variant current_value = get(p_names[0], &valid);
  524. for (int i = 1; i < p_names.size(); i++) {
  525. current_value = current_value.get_named(p_names[i], valid);
  526. if (!valid) {
  527. break;
  528. }
  529. }
  530. if (r_valid) {
  531. *r_valid = valid;
  532. }
  533. return current_value;
  534. }
  535. void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) const {
  536. if (script_instance && p_reversed) {
  537. p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  538. script_instance->get_property_list(p_list);
  539. }
  540. _get_property_listv(p_list, p_reversed);
  541. if (_extension && _extension->get_property_list) {
  542. uint32_t pcount;
  543. const GDNativePropertyInfo *pinfo = _extension->get_property_list(_extension_instance, &pcount);
  544. for (uint32_t i = 0; i < pcount; i++) {
  545. p_list->push_back(PropertyInfo(Variant::Type(pinfo[i].type), pinfo[i].class_name, PropertyHint(pinfo[i].hint), pinfo[i].hint_string, pinfo[i].usage, pinfo[i].class_name));
  546. }
  547. if (_extension->free_property_list) {
  548. _extension->free_property_list(_extension_instance, pinfo);
  549. }
  550. }
  551. if (!is_class("Script")) { // can still be set, but this is for user-friendliness
  552. p_list->push_back(PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT));
  553. }
  554. if (!metadata.is_empty()) {
  555. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "__meta__", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  556. }
  557. if (script_instance && !p_reversed) {
  558. p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  559. script_instance->get_property_list(p_list);
  560. }
  561. }
  562. void Object::_validate_property(PropertyInfo &property) const {
  563. }
  564. void Object::get_method_list(List<MethodInfo> *p_list) const {
  565. ClassDB::get_method_list(get_class_name(), p_list);
  566. if (script_instance) {
  567. script_instance->get_method_list(p_list);
  568. }
  569. }
  570. Variant Object::_call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  571. if (p_argcount < 1) {
  572. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  573. r_error.argument = 0;
  574. return Variant();
  575. }
  576. if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
  577. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  578. r_error.argument = 0;
  579. r_error.expected = Variant::STRING_NAME;
  580. return Variant();
  581. }
  582. StringName method = *p_args[0];
  583. return call(method, &p_args[1], p_argcount - 1, r_error);
  584. }
  585. Variant Object::_call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  586. if (p_argcount < 1) {
  587. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  588. r_error.argument = 0;
  589. return Variant();
  590. }
  591. if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
  592. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  593. r_error.argument = 0;
  594. r_error.expected = Variant::STRING_NAME;
  595. return Variant();
  596. }
  597. r_error.error = Callable::CallError::CALL_OK;
  598. StringName method = *p_args[0];
  599. MessageQueue::get_singleton()->push_call(get_instance_id(), method, &p_args[1], p_argcount - 1, true);
  600. return Variant();
  601. }
  602. bool Object::has_method(const StringName &p_method) const {
  603. if (p_method == CoreStringNames::get_singleton()->_free) {
  604. return true;
  605. }
  606. if (script_instance && script_instance->has_method(p_method)) {
  607. return true;
  608. }
  609. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  610. return method != nullptr;
  611. }
  612. Variant Object::getvar(const Variant &p_key, bool *r_valid) const {
  613. if (r_valid) {
  614. *r_valid = false;
  615. }
  616. if (p_key.get_type() == Variant::STRING_NAME || p_key.get_type() == Variant::STRING) {
  617. return get(p_key, r_valid);
  618. }
  619. return Variant();
  620. }
  621. void Object::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) {
  622. if (r_valid) {
  623. *r_valid = false;
  624. }
  625. if (p_key.get_type() == Variant::STRING_NAME || p_key.get_type() == Variant::STRING) {
  626. return set(p_key, p_value, r_valid);
  627. }
  628. }
  629. Variant Object::callv(const StringName &p_method, const Array &p_args) {
  630. const Variant **argptrs = nullptr;
  631. if (p_args.size() > 0) {
  632. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_args.size());
  633. for (int i = 0; i < p_args.size(); i++) {
  634. argptrs[i] = &p_args[i];
  635. }
  636. }
  637. Callable::CallError ce;
  638. Variant ret = call(p_method, argptrs, p_args.size(), ce);
  639. if (ce.error != Callable::CallError::CALL_OK) {
  640. ERR_FAIL_V_MSG(Variant(), "Error calling method from 'callv': " + Variant::get_call_error_text(this, p_method, argptrs, p_args.size(), ce) + ".");
  641. }
  642. return ret;
  643. }
  644. Variant Object::call(const StringName &p_name, VARIANT_ARG_DECLARE) {
  645. VARIANT_ARGPTRS;
  646. int argc = 0;
  647. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  648. if (argptr[i]->get_type() == Variant::NIL) {
  649. break;
  650. }
  651. argc++;
  652. }
  653. Callable::CallError error;
  654. Variant ret = call(p_name, argptr, argc, error);
  655. return ret;
  656. }
  657. Variant Object::call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  658. r_error.error = Callable::CallError::CALL_OK;
  659. if (p_method == CoreStringNames::get_singleton()->_free) {
  660. //free must be here, before anything, always ready
  661. #ifdef DEBUG_ENABLED
  662. if (p_argcount != 0) {
  663. r_error.argument = 0;
  664. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  665. return Variant();
  666. }
  667. if (Object::cast_to<RefCounted>(this)) {
  668. r_error.argument = 0;
  669. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  670. ERR_FAIL_V_MSG(Variant(), "Can't 'free' a reference.");
  671. }
  672. if (_lock_index.get() > 1) {
  673. r_error.argument = 0;
  674. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  675. ERR_FAIL_V_MSG(Variant(), "Object is locked and can't be freed.");
  676. }
  677. #endif
  678. //must be here, must be before everything,
  679. memdelete(this);
  680. r_error.error = Callable::CallError::CALL_OK;
  681. return Variant();
  682. }
  683. Variant ret;
  684. OBJ_DEBUG_LOCK
  685. if (script_instance) {
  686. ret = script_instance->call(p_method, p_args, p_argcount, r_error);
  687. //force jumptable
  688. switch (r_error.error) {
  689. case Callable::CallError::CALL_OK:
  690. return ret;
  691. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  692. break;
  693. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  694. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  695. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  696. return ret;
  697. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL: {
  698. }
  699. }
  700. }
  701. //extension does not need this, because all methods are registered in MethodBind
  702. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  703. if (method) {
  704. ret = method->call(this, p_args, p_argcount, r_error);
  705. } else {
  706. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  707. }
  708. return ret;
  709. }
  710. void Object::notification(int p_notification, bool p_reversed) {
  711. _notificationv(p_notification, p_reversed);
  712. if (script_instance) {
  713. script_instance->notification(p_notification);
  714. }
  715. if (_extension && _extension->notification) {
  716. _extension->notification(_extension_instance, p_notification);
  717. }
  718. }
  719. String Object::to_string() {
  720. if (script_instance) {
  721. bool valid;
  722. String ret = script_instance->to_string(&valid);
  723. if (valid) {
  724. return ret;
  725. }
  726. }
  727. if (_extension && _extension->to_string) {
  728. return _extension->to_string(_extension_instance);
  729. }
  730. return "[" + get_class() + ":" + itos(get_instance_id()) + "]";
  731. }
  732. void Object::set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance) {
  733. //this function is not meant to be used in any of these ways
  734. ERR_FAIL_COND(p_script.is_null());
  735. ERR_FAIL_COND(!p_instance);
  736. ERR_FAIL_COND(script_instance != nullptr || !script.is_null());
  737. script = p_script;
  738. script_instance = p_instance;
  739. }
  740. void Object::set_script(const Variant &p_script) {
  741. if (script == p_script) {
  742. return;
  743. }
  744. if (script_instance) {
  745. memdelete(script_instance);
  746. script_instance = nullptr;
  747. }
  748. script = p_script;
  749. Ref<Script> s = script;
  750. if (!s.is_null()) {
  751. if (s->can_instantiate()) {
  752. OBJ_DEBUG_LOCK
  753. script_instance = s->instance_create(this);
  754. } else if (Engine::get_singleton()->is_editor_hint()) {
  755. OBJ_DEBUG_LOCK
  756. script_instance = s->placeholder_instance_create(this);
  757. }
  758. }
  759. notify_property_list_changed(); //scripts may add variables, so refresh is desired
  760. emit_signal(CoreStringNames::get_singleton()->script_changed);
  761. }
  762. void Object::set_script_instance(ScriptInstance *p_instance) {
  763. if (script_instance == p_instance) {
  764. return;
  765. }
  766. if (script_instance) {
  767. memdelete(script_instance);
  768. }
  769. script_instance = p_instance;
  770. if (p_instance) {
  771. script = p_instance->get_script();
  772. } else {
  773. script = Variant();
  774. }
  775. }
  776. Variant Object::get_script() const {
  777. return script;
  778. }
  779. bool Object::has_meta(const StringName &p_name) const {
  780. return metadata.has(p_name);
  781. }
  782. void Object::set_meta(const StringName &p_name, const Variant &p_value) {
  783. if (p_value.get_type() == Variant::NIL) {
  784. metadata.erase(p_name);
  785. return;
  786. }
  787. metadata[p_name] = p_value;
  788. }
  789. Variant Object::get_meta(const StringName &p_name) const {
  790. ERR_FAIL_COND_V_MSG(!metadata.has(p_name), Variant(), "The object does not have any 'meta' values with the key '" + p_name + "'.");
  791. return metadata[p_name];
  792. }
  793. void Object::remove_meta(const StringName &p_name) {
  794. metadata.erase(p_name);
  795. }
  796. Array Object::_get_property_list_bind() const {
  797. List<PropertyInfo> lpi;
  798. get_property_list(&lpi);
  799. return convert_property_list(&lpi);
  800. }
  801. Array Object::_get_method_list_bind() const {
  802. List<MethodInfo> ml;
  803. get_method_list(&ml);
  804. Array ret;
  805. for (List<MethodInfo>::Element *E = ml.front(); E; E = E->next()) {
  806. Dictionary d = E->get();
  807. //va.push_back(d);
  808. ret.push_back(d);
  809. }
  810. return ret;
  811. }
  812. Vector<StringName> Object::_get_meta_list_bind() const {
  813. Vector<StringName> _metaret;
  814. List<Variant> keys;
  815. metadata.get_key_list(&keys);
  816. for (const Variant &E : keys) {
  817. _metaret.push_back(E);
  818. }
  819. return _metaret;
  820. }
  821. void Object::get_meta_list(List<StringName> *p_list) const {
  822. List<Variant> keys;
  823. metadata.get_key_list(&keys);
  824. for (const Variant &E : keys) {
  825. p_list->push_back(E);
  826. }
  827. }
  828. void Object::add_user_signal(const MethodInfo &p_signal) {
  829. ERR_FAIL_COND_MSG(p_signal.name == "", "Signal name cannot be empty.");
  830. 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() + "'.");
  831. ERR_FAIL_COND_MSG(signal_map.has(p_signal.name), "Trying to add already existing signal '" + p_signal.name + "'.");
  832. SignalData s;
  833. s.user = p_signal;
  834. signal_map[p_signal.name] = s;
  835. }
  836. bool Object::_has_user_signal(const StringName &p_name) const {
  837. if (!signal_map.has(p_name)) {
  838. return false;
  839. }
  840. return signal_map[p_name].user.name.length() > 0;
  841. }
  842. struct _ObjectSignalDisconnectData {
  843. StringName signal;
  844. Callable callable;
  845. };
  846. Variant Object::_emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  847. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  848. ERR_FAIL_COND_V(p_argcount < 1, Variant());
  849. if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
  850. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  851. r_error.argument = 0;
  852. r_error.expected = Variant::STRING_NAME;
  853. ERR_FAIL_COND_V(p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING, Variant());
  854. }
  855. r_error.error = Callable::CallError::CALL_OK;
  856. StringName signal = *p_args[0];
  857. const Variant **args = nullptr;
  858. int argc = p_argcount - 1;
  859. if (argc) {
  860. args = &p_args[1];
  861. }
  862. emit_signal(signal, args, argc);
  863. return Variant();
  864. }
  865. Error Object::emit_signal(const StringName &p_name, const Variant **p_args, int p_argcount) {
  866. if (_block_signals) {
  867. return ERR_CANT_ACQUIRE_RESOURCE; //no emit, signals blocked
  868. }
  869. SignalData *s = signal_map.getptr(p_name);
  870. if (!s) {
  871. #ifdef DEBUG_ENABLED
  872. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_name);
  873. //check in script
  874. 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 + "\".");
  875. #endif
  876. //not connected? just return
  877. return ERR_UNAVAILABLE;
  878. }
  879. List<_ObjectSignalDisconnectData> disconnect_data;
  880. //copy on write will ensure that disconnecting the signal or even deleting the object will not affect the signal calling.
  881. //this happens automatically and will not change the performance of calling.
  882. //awesome, isn't it?
  883. VMap<Callable, SignalData::Slot> slot_map = s->slot_map;
  884. int ssize = slot_map.size();
  885. OBJ_DEBUG_LOCK
  886. Vector<const Variant *> bind_mem;
  887. Error err = OK;
  888. for (int i = 0; i < ssize; i++) {
  889. const Connection &c = slot_map.getv(i).conn;
  890. Object *target = c.callable.get_object();
  891. if (!target) {
  892. // Target might have been deleted during signal callback, this is expected and OK.
  893. continue;
  894. }
  895. const Variant **args = p_args;
  896. int argc = p_argcount;
  897. if (c.binds.size()) {
  898. //handle binds
  899. bind_mem.resize(p_argcount + c.binds.size());
  900. for (int j = 0; j < p_argcount; j++) {
  901. bind_mem.write[j] = p_args[j];
  902. }
  903. for (int j = 0; j < c.binds.size(); j++) {
  904. bind_mem.write[p_argcount + j] = &c.binds[j];
  905. }
  906. args = (const Variant **)bind_mem.ptr();
  907. argc = bind_mem.size();
  908. }
  909. if (c.flags & CONNECT_DEFERRED) {
  910. MessageQueue::get_singleton()->push_callable(c.callable, args, argc, true);
  911. } else {
  912. Callable::CallError ce;
  913. _emitting = true;
  914. Variant ret;
  915. c.callable.call(args, argc, ret, ce);
  916. _emitting = false;
  917. if (ce.error != Callable::CallError::CALL_OK) {
  918. #ifdef DEBUG_ENABLED
  919. if (c.flags & CONNECT_PERSIST && Engine::get_singleton()->is_editor_hint() && (script.is_null() || !Ref<Script>(script)->is_tool())) {
  920. continue;
  921. }
  922. #endif
  923. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD && !ClassDB::class_exists(target->get_class_name())) {
  924. //most likely object is not initialized yet, do not throw error.
  925. } else {
  926. ERR_PRINT("Error calling from signal '" + String(p_name) + "' to callable: " + Variant::get_callable_error_text(c.callable, args, argc, ce) + ".");
  927. err = ERR_METHOD_NOT_FOUND;
  928. }
  929. }
  930. }
  931. bool disconnect = c.flags & CONNECT_ONESHOT;
  932. #ifdef TOOLS_ENABLED
  933. if (disconnect && (c.flags & CONNECT_PERSIST) && Engine::get_singleton()->is_editor_hint()) {
  934. //this signal was connected from the editor, and is being edited. just don't disconnect for now
  935. disconnect = false;
  936. }
  937. #endif
  938. if (disconnect) {
  939. _ObjectSignalDisconnectData dd;
  940. dd.signal = p_name;
  941. dd.callable = c.callable;
  942. disconnect_data.push_back(dd);
  943. }
  944. }
  945. while (!disconnect_data.is_empty()) {
  946. const _ObjectSignalDisconnectData &dd = disconnect_data.front()->get();
  947. _disconnect(dd.signal, dd.callable);
  948. disconnect_data.pop_front();
  949. }
  950. return err;
  951. }
  952. Error Object::emit_signal(const StringName &p_name, VARIANT_ARG_DECLARE) {
  953. VARIANT_ARGPTRS;
  954. int argc = 0;
  955. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  956. if (argptr[i]->get_type() == Variant::NIL) {
  957. break;
  958. }
  959. argc++;
  960. }
  961. return emit_signal(p_name, argptr, argc);
  962. }
  963. void Object::_add_user_signal(const String &p_name, const Array &p_args) {
  964. // this version of add_user_signal is meant to be used from scripts or external apis
  965. // without access to ADD_SIGNAL in bind_methods
  966. // added events are per instance, as opposed to the other ones, which are global
  967. MethodInfo mi;
  968. mi.name = p_name;
  969. for (int i = 0; i < p_args.size(); i++) {
  970. Dictionary d = p_args[i];
  971. PropertyInfo param;
  972. if (d.has("name")) {
  973. param.name = d["name"];
  974. }
  975. if (d.has("type")) {
  976. param.type = (Variant::Type)(int)d["type"];
  977. }
  978. mi.arguments.push_back(param);
  979. }
  980. add_user_signal(mi);
  981. }
  982. Array Object::_get_signal_list() const {
  983. List<MethodInfo> signal_list;
  984. get_signal_list(&signal_list);
  985. Array ret;
  986. for (const MethodInfo &E : signal_list) {
  987. ret.push_back(Dictionary(E));
  988. }
  989. return ret;
  990. }
  991. Array Object::_get_signal_connection_list(const String &p_signal) const {
  992. List<Connection> conns;
  993. get_all_signal_connections(&conns);
  994. Array ret;
  995. for (const Connection &c : conns) {
  996. if (c.signal.get_name() == p_signal) {
  997. ret.push_back(c);
  998. }
  999. }
  1000. return ret;
  1001. }
  1002. Array Object::_get_incoming_connections() const {
  1003. Array ret;
  1004. int connections_amount = connections.size();
  1005. for (int idx_conn = 0; idx_conn < connections_amount; idx_conn++) {
  1006. ret.push_back(connections[idx_conn]);
  1007. }
  1008. return ret;
  1009. }
  1010. bool Object::has_signal(const StringName &p_name) const {
  1011. if (!script.is_null()) {
  1012. Ref<Script> scr = script;
  1013. if (scr.is_valid() && scr->has_script_signal(p_name)) {
  1014. return true;
  1015. }
  1016. }
  1017. if (ClassDB::has_signal(get_class_name(), p_name)) {
  1018. return true;
  1019. }
  1020. if (_has_user_signal(p_name)) {
  1021. return true;
  1022. }
  1023. return false;
  1024. }
  1025. void Object::get_signal_list(List<MethodInfo> *p_signals) const {
  1026. if (!script.is_null()) {
  1027. Ref<Script> scr = script;
  1028. if (scr.is_valid()) {
  1029. scr->get_script_signal_list(p_signals);
  1030. }
  1031. }
  1032. ClassDB::get_signal_list(get_class_name(), p_signals);
  1033. //find maybe usersignals?
  1034. const StringName *S = nullptr;
  1035. while ((S = signal_map.next(S))) {
  1036. if (signal_map[*S].user.name != "") {
  1037. //user signal
  1038. p_signals->push_back(signal_map[*S].user);
  1039. }
  1040. }
  1041. }
  1042. void Object::get_all_signal_connections(List<Connection> *p_connections) const {
  1043. const StringName *S = nullptr;
  1044. while ((S = signal_map.next(S))) {
  1045. const SignalData *s = &signal_map[*S];
  1046. for (int i = 0; i < s->slot_map.size(); i++) {
  1047. p_connections->push_back(s->slot_map.getv(i).conn);
  1048. }
  1049. }
  1050. }
  1051. void Object::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
  1052. const SignalData *s = signal_map.getptr(p_signal);
  1053. if (!s) {
  1054. return; //nothing
  1055. }
  1056. for (int i = 0; i < s->slot_map.size(); i++) {
  1057. p_connections->push_back(s->slot_map.getv(i).conn);
  1058. }
  1059. }
  1060. int Object::get_persistent_signal_connection_count() const {
  1061. int count = 0;
  1062. const StringName *S = nullptr;
  1063. while ((S = signal_map.next(S))) {
  1064. const SignalData *s = &signal_map[*S];
  1065. for (int i = 0; i < s->slot_map.size(); i++) {
  1066. if (s->slot_map.getv(i).conn.flags & CONNECT_PERSIST) {
  1067. count += 1;
  1068. }
  1069. }
  1070. }
  1071. return count;
  1072. }
  1073. void Object::get_signals_connected_to_this(List<Connection> *p_connections) const {
  1074. for (const Connection &E : connections) {
  1075. p_connections->push_back(E);
  1076. }
  1077. }
  1078. Error Object::connect(const StringName &p_signal, const Callable &p_callable, const Vector<Variant> &p_binds, uint32_t p_flags) {
  1079. ERR_FAIL_COND_V_MSG(p_callable.is_null(), ERR_INVALID_PARAMETER, "Cannot connect to '" + p_signal + "': the provided callable is null.");
  1080. Object *target_object = p_callable.get_object();
  1081. ERR_FAIL_COND_V_MSG(!target_object, ERR_INVALID_PARAMETER, "Cannot connect to '" + p_signal + "' to callable '" + p_callable + "': the callable object is null.");
  1082. SignalData *s = signal_map.getptr(p_signal);
  1083. if (!s) {
  1084. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1085. //check in script
  1086. if (!signal_is_valid && !script.is_null()) {
  1087. if (Ref<Script>(script)->has_script_signal(p_signal)) {
  1088. signal_is_valid = true;
  1089. }
  1090. #ifdef TOOLS_ENABLED
  1091. else {
  1092. //allow connecting signals anyway if script is invalid, see issue #17070
  1093. if (!Ref<Script>(script)->is_valid()) {
  1094. signal_is_valid = true;
  1095. }
  1096. }
  1097. #endif
  1098. }
  1099. 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 + "'.");
  1100. signal_map[p_signal] = SignalData();
  1101. s = &signal_map[p_signal];
  1102. }
  1103. Callable target = p_callable;
  1104. //compare with the base callable, so binds can be ignored
  1105. if (s->slot_map.has(*target.get_base_comparator())) {
  1106. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1107. s->slot_map[*target.get_base_comparator()].reference_count++;
  1108. return OK;
  1109. } else {
  1110. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Signal '" + p_signal + "' is already connected to given callable '" + p_callable + "' in that object.");
  1111. }
  1112. }
  1113. SignalData::Slot slot;
  1114. Connection conn;
  1115. conn.callable = target;
  1116. conn.signal = ::Signal(this, p_signal);
  1117. conn.flags = p_flags;
  1118. conn.binds = p_binds;
  1119. slot.conn = conn;
  1120. slot.cE = target_object->connections.push_back(conn);
  1121. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1122. slot.reference_count = 1;
  1123. }
  1124. //use callable version as key, so binds can be ignored
  1125. s->slot_map[*target.get_base_comparator()] = slot;
  1126. return OK;
  1127. }
  1128. bool Object::is_connected(const StringName &p_signal, const Callable &p_callable) const {
  1129. ERR_FAIL_COND_V_MSG(p_callable.is_null(), false, "Cannot determine if connected to '" + p_signal + "': the provided callable is null.");
  1130. const SignalData *s = signal_map.getptr(p_signal);
  1131. if (!s) {
  1132. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1133. if (signal_is_valid) {
  1134. return false;
  1135. }
  1136. if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal)) {
  1137. return false;
  1138. }
  1139. ERR_FAIL_V_MSG(false, "Nonexistent signal: " + p_signal + ".");
  1140. }
  1141. Callable target = p_callable;
  1142. return s->slot_map.has(*target.get_base_comparator());
  1143. //const Map<Signal::Target,Signal::Slot>::Element *E = s->slot_map.find(target);
  1144. //return (E!=nullptr );
  1145. }
  1146. void Object::disconnect(const StringName &p_signal, const Callable &p_callable) {
  1147. _disconnect(p_signal, p_callable);
  1148. }
  1149. void Object::_disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force) {
  1150. ERR_FAIL_COND_MSG(p_callable.is_null(), "Cannot disconnect from '" + p_signal + "': the provided callable is null.");
  1151. Object *target_object = p_callable.get_object();
  1152. ERR_FAIL_COND_MSG(!target_object, "Cannot disconnect '" + p_signal + "' from callable '" + p_callable + "': the callable object is null.");
  1153. SignalData *s = signal_map.getptr(p_signal);
  1154. if (!s) {
  1155. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal) ||
  1156. (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal));
  1157. ERR_FAIL_COND_MSG(signal_is_valid, "Attempt to disconnect a nonexistent connection from '" + to_string() + "'. Signal: '" + p_signal + "', callable: '" + p_callable + "'.");
  1158. }
  1159. ERR_FAIL_COND_MSG(!s, vformat("Disconnecting nonexistent signal '%s' in %s.", p_signal, to_string()));
  1160. ERR_FAIL_COND_MSG(!s->slot_map.has(*p_callable.get_base_comparator()), "Disconnecting nonexistent signal '" + p_signal + "', callable: " + p_callable + ".");
  1161. SignalData::Slot *slot = &s->slot_map[p_callable];
  1162. if (!p_force) {
  1163. slot->reference_count--; // by default is zero, if it was not referenced it will go below it
  1164. if (slot->reference_count > 0) {
  1165. return;
  1166. }
  1167. }
  1168. target_object->connections.erase(slot->cE);
  1169. s->slot_map.erase(*p_callable.get_base_comparator());
  1170. if (s->slot_map.is_empty() && ClassDB::has_signal(get_class_name(), p_signal)) {
  1171. //not user signal, delete
  1172. signal_map.erase(p_signal);
  1173. }
  1174. }
  1175. void Object::_set_bind(const String &p_set, const Variant &p_value) {
  1176. set(p_set, p_value);
  1177. }
  1178. Variant Object::_get_bind(const String &p_name) const {
  1179. return get(p_name);
  1180. }
  1181. void Object::_set_indexed_bind(const NodePath &p_name, const Variant &p_value) {
  1182. set_indexed(p_name.get_as_property_path().get_subnames(), p_value);
  1183. }
  1184. Variant Object::_get_indexed_bind(const NodePath &p_name) const {
  1185. return get_indexed(p_name.get_as_property_path().get_subnames());
  1186. }
  1187. void Object::initialize_class() {
  1188. static bool initialized = false;
  1189. if (initialized) {
  1190. return;
  1191. }
  1192. ClassDB::_add_class<Object>();
  1193. _bind_methods();
  1194. initialized = true;
  1195. }
  1196. String Object::tr(const StringName &p_message, const StringName &p_context) const {
  1197. if (!_can_translate || !TranslationServer::get_singleton()) {
  1198. return p_message;
  1199. }
  1200. return TranslationServer::get_singleton()->translate(p_message, p_context);
  1201. }
  1202. String Object::tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  1203. if (!_can_translate || !TranslationServer::get_singleton()) {
  1204. // Return message based on English plural rule if translation is not possible.
  1205. if (p_n == 1) {
  1206. return p_message;
  1207. }
  1208. return p_message_plural;
  1209. }
  1210. return TranslationServer::get_singleton()->translate_plural(p_message, p_message_plural, p_n, p_context);
  1211. }
  1212. void Object::_clear_internal_resource_paths(const Variant &p_var) {
  1213. switch (p_var.get_type()) {
  1214. case Variant::OBJECT: {
  1215. RES r = p_var;
  1216. if (!r.is_valid()) {
  1217. return;
  1218. }
  1219. if (!r->is_built_in()) {
  1220. return; //not an internal resource
  1221. }
  1222. Object *object = p_var;
  1223. if (!object) {
  1224. return;
  1225. }
  1226. r->set_path("");
  1227. r->clear_internal_resource_paths();
  1228. } break;
  1229. case Variant::ARRAY: {
  1230. Array a = p_var;
  1231. for (int i = 0; i < a.size(); i++) {
  1232. _clear_internal_resource_paths(a[i]);
  1233. }
  1234. } break;
  1235. case Variant::DICTIONARY: {
  1236. Dictionary d = p_var;
  1237. List<Variant> keys;
  1238. d.get_key_list(&keys);
  1239. for (const Variant &E : keys) {
  1240. _clear_internal_resource_paths(E);
  1241. _clear_internal_resource_paths(d[E]);
  1242. }
  1243. } break;
  1244. default: {
  1245. }
  1246. }
  1247. }
  1248. #ifdef TOOLS_ENABLED
  1249. void Object::editor_set_section_unfold(const String &p_section, bool p_unfolded) {
  1250. set_edited(true);
  1251. if (p_unfolded) {
  1252. editor_section_folding.insert(p_section);
  1253. } else {
  1254. editor_section_folding.erase(p_section);
  1255. }
  1256. }
  1257. bool Object::editor_is_section_unfolded(const String &p_section) {
  1258. return editor_section_folding.has(p_section);
  1259. }
  1260. #endif
  1261. void Object::clear_internal_resource_paths() {
  1262. List<PropertyInfo> pinfo;
  1263. get_property_list(&pinfo);
  1264. for (const PropertyInfo &E : pinfo) {
  1265. _clear_internal_resource_paths(get(E.name));
  1266. }
  1267. }
  1268. void Object::notify_property_list_changed() {
  1269. emit_signal(CoreStringNames::get_singleton()->property_list_changed);
  1270. }
  1271. void Object::_bind_methods() {
  1272. ClassDB::bind_method(D_METHOD("get_class"), &Object::get_class);
  1273. ClassDB::bind_method(D_METHOD("is_class", "class"), &Object::is_class);
  1274. ClassDB::bind_method(D_METHOD("set", "property", "value"), &Object::_set_bind);
  1275. ClassDB::bind_method(D_METHOD("get", "property"), &Object::_get_bind);
  1276. ClassDB::bind_method(D_METHOD("set_indexed", "property", "value"), &Object::_set_indexed_bind);
  1277. ClassDB::bind_method(D_METHOD("get_indexed", "property"), &Object::_get_indexed_bind);
  1278. ClassDB::bind_method(D_METHOD("get_property_list"), &Object::_get_property_list_bind);
  1279. ClassDB::bind_method(D_METHOD("get_method_list"), &Object::_get_method_list_bind);
  1280. ClassDB::bind_method(D_METHOD("notification", "what", "reversed"), &Object::notification, DEFVAL(false));
  1281. ClassDB::bind_method(D_METHOD("to_string"), &Object::to_string);
  1282. ClassDB::bind_method(D_METHOD("get_instance_id"), &Object::get_instance_id);
  1283. ClassDB::bind_method(D_METHOD("set_script", "script"), &Object::set_script);
  1284. ClassDB::bind_method(D_METHOD("get_script"), &Object::get_script);
  1285. ClassDB::bind_method(D_METHOD("set_meta", "name", "value"), &Object::set_meta);
  1286. ClassDB::bind_method(D_METHOD("remove_meta", "name"), &Object::remove_meta);
  1287. ClassDB::bind_method(D_METHOD("get_meta", "name"), &Object::get_meta);
  1288. ClassDB::bind_method(D_METHOD("has_meta", "name"), &Object::has_meta);
  1289. ClassDB::bind_method(D_METHOD("get_meta_list"), &Object::_get_meta_list_bind);
  1290. ClassDB::bind_method(D_METHOD("add_user_signal", "signal", "arguments"), &Object::_add_user_signal, DEFVAL(Array()));
  1291. ClassDB::bind_method(D_METHOD("has_user_signal", "signal"), &Object::_has_user_signal);
  1292. {
  1293. MethodInfo mi;
  1294. mi.name = "emit_signal";
  1295. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "signal"));
  1296. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "emit_signal", &Object::_emit_signal, mi, varray(), false);
  1297. }
  1298. {
  1299. MethodInfo mi;
  1300. mi.name = "call";
  1301. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1302. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call", &Object::_call_bind, mi);
  1303. }
  1304. {
  1305. MethodInfo mi;
  1306. mi.name = "call_deferred";
  1307. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1308. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_deferred", &Object::_call_deferred_bind, mi, varray(), false);
  1309. }
  1310. ClassDB::bind_method(D_METHOD("set_deferred", "property", "value"), &Object::set_deferred);
  1311. ClassDB::bind_method(D_METHOD("callv", "method", "arg_array"), &Object::callv);
  1312. ClassDB::bind_method(D_METHOD("has_method", "method"), &Object::has_method);
  1313. ClassDB::bind_method(D_METHOD("has_signal", "signal"), &Object::has_signal);
  1314. ClassDB::bind_method(D_METHOD("get_signal_list"), &Object::_get_signal_list);
  1315. ClassDB::bind_method(D_METHOD("get_signal_connection_list", "signal"), &Object::_get_signal_connection_list);
  1316. ClassDB::bind_method(D_METHOD("get_incoming_connections"), &Object::_get_incoming_connections);
  1317. ClassDB::bind_method(D_METHOD("connect", "signal", "callable", "binds", "flags"), &Object::connect, DEFVAL(Array()), DEFVAL(0));
  1318. ClassDB::bind_method(D_METHOD("disconnect", "signal", "callable"), &Object::disconnect);
  1319. ClassDB::bind_method(D_METHOD("is_connected", "signal", "callable"), &Object::is_connected);
  1320. ClassDB::bind_method(D_METHOD("set_block_signals", "enable"), &Object::set_block_signals);
  1321. ClassDB::bind_method(D_METHOD("is_blocking_signals"), &Object::is_blocking_signals);
  1322. ClassDB::bind_method(D_METHOD("notify_property_list_changed"), &Object::notify_property_list_changed);
  1323. ClassDB::bind_method(D_METHOD("set_message_translation", "enable"), &Object::set_message_translation);
  1324. ClassDB::bind_method(D_METHOD("can_translate_messages"), &Object::can_translate_messages);
  1325. ClassDB::bind_method(D_METHOD("tr", "message", "context"), &Object::tr, DEFVAL(""));
  1326. ClassDB::bind_method(D_METHOD("tr_n", "message", "plural_message", "n", "context"), &Object::tr_n, DEFVAL(""));
  1327. ClassDB::bind_method(D_METHOD("is_queued_for_deletion"), &Object::is_queued_for_deletion);
  1328. ClassDB::add_virtual_method("Object", MethodInfo("free"), false);
  1329. ADD_SIGNAL(MethodInfo("script_changed"));
  1330. ADD_SIGNAL(MethodInfo("property_list_changed"));
  1331. #define BIND_OBJ_CORE_METHOD(m_method) \
  1332. ::ClassDB::add_virtual_method(get_class_static(), m_method, true, Vector<String>(), true);
  1333. BIND_OBJ_CORE_METHOD(MethodInfo("_notification", PropertyInfo(Variant::INT, "what")));
  1334. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_set", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value")));
  1335. #ifdef TOOLS_ENABLED
  1336. MethodInfo miget("_get", PropertyInfo(Variant::STRING_NAME, "property"));
  1337. miget.return_val.name = "Variant";
  1338. miget.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1339. BIND_OBJ_CORE_METHOD(miget);
  1340. MethodInfo plget("_get_property_list");
  1341. plget.return_val.type = Variant::ARRAY;
  1342. BIND_OBJ_CORE_METHOD(plget);
  1343. #endif
  1344. BIND_OBJ_CORE_METHOD(MethodInfo("_init"));
  1345. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::STRING, "_to_string"));
  1346. BIND_CONSTANT(NOTIFICATION_POSTINITIALIZE);
  1347. BIND_CONSTANT(NOTIFICATION_PREDELETE);
  1348. BIND_ENUM_CONSTANT(CONNECT_DEFERRED);
  1349. BIND_ENUM_CONSTANT(CONNECT_PERSIST);
  1350. BIND_ENUM_CONSTANT(CONNECT_ONESHOT);
  1351. BIND_ENUM_CONSTANT(CONNECT_REFERENCE_COUNTED);
  1352. }
  1353. void Object::call_deferred(const StringName &p_method, VARIANT_ARG_DECLARE) {
  1354. MessageQueue::get_singleton()->push_call(this, p_method, VARIANT_ARG_PASS);
  1355. }
  1356. void Object::set_deferred(const StringName &p_property, const Variant &p_value) {
  1357. MessageQueue::get_singleton()->push_set(this, p_property, p_value);
  1358. }
  1359. void Object::set_block_signals(bool p_block) {
  1360. _block_signals = p_block;
  1361. }
  1362. bool Object::is_blocking_signals() const {
  1363. return _block_signals;
  1364. }
  1365. void Object::get_translatable_strings(List<String> *p_strings) const {
  1366. List<PropertyInfo> plist;
  1367. get_property_list(&plist);
  1368. for (const PropertyInfo &E : plist) {
  1369. if (!(E.usage & PROPERTY_USAGE_INTERNATIONALIZED)) {
  1370. continue;
  1371. }
  1372. String text = get(E.name);
  1373. if (text == "") {
  1374. continue;
  1375. }
  1376. p_strings->push_back(text);
  1377. }
  1378. }
  1379. Variant::Type Object::get_static_property_type(const StringName &p_property, bool *r_valid) const {
  1380. bool valid;
  1381. Variant::Type t = ClassDB::get_property_type(get_class_name(), p_property, &valid);
  1382. if (valid) {
  1383. if (r_valid) {
  1384. *r_valid = true;
  1385. }
  1386. return t;
  1387. }
  1388. if (get_script_instance()) {
  1389. return get_script_instance()->get_property_type(p_property, r_valid);
  1390. }
  1391. if (r_valid) {
  1392. *r_valid = false;
  1393. }
  1394. return Variant::NIL;
  1395. }
  1396. Variant::Type Object::get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid) const {
  1397. if (p_path.size() == 0) {
  1398. if (r_valid) {
  1399. *r_valid = false;
  1400. }
  1401. return Variant::NIL;
  1402. }
  1403. bool valid = false;
  1404. Variant::Type t = get_static_property_type(p_path[0], &valid);
  1405. if (!valid) {
  1406. if (r_valid) {
  1407. *r_valid = false;
  1408. }
  1409. return Variant::NIL;
  1410. }
  1411. Callable::CallError ce;
  1412. Variant check;
  1413. Variant::construct(t, check, nullptr, 0, ce);
  1414. for (int i = 1; i < p_path.size(); i++) {
  1415. if (check.get_type() == Variant::OBJECT || check.get_type() == Variant::DICTIONARY || check.get_type() == Variant::ARRAY) {
  1416. // We cannot be sure about the type of properties this type can have
  1417. if (r_valid) {
  1418. *r_valid = false;
  1419. }
  1420. return Variant::NIL;
  1421. }
  1422. check = check.get_named(p_path[i], valid);
  1423. if (!valid) {
  1424. if (r_valid) {
  1425. *r_valid = false;
  1426. }
  1427. return Variant::NIL;
  1428. }
  1429. }
  1430. if (r_valid) {
  1431. *r_valid = true;
  1432. }
  1433. return check.get_type();
  1434. }
  1435. bool Object::is_queued_for_deletion() const {
  1436. return _is_queued_for_deletion;
  1437. }
  1438. #ifdef TOOLS_ENABLED
  1439. void Object::set_edited(bool p_edited) {
  1440. _edited = p_edited;
  1441. _edited_version++;
  1442. }
  1443. bool Object::is_edited() const {
  1444. return _edited;
  1445. }
  1446. uint32_t Object::get_edited_version() const {
  1447. return _edited_version;
  1448. }
  1449. #endif
  1450. void Object::set_instance_binding(void *p_token, void *p_binding, const GDNativeInstanceBindingCallbacks *p_callbacks) {
  1451. // This is only meant to be used on creation by the binder.
  1452. ERR_FAIL_COND(_instance_bindings != nullptr);
  1453. _instance_bindings = (InstanceBinding *)memalloc(sizeof(InstanceBinding));
  1454. _instance_bindings[0].binding = p_binding;
  1455. _instance_bindings[0].free_callback = p_callbacks->free_callback;
  1456. _instance_bindings[0].reference_callback = p_callbacks->reference_callback;
  1457. _instance_bindings[0].token = p_token;
  1458. _instance_binding_count = 1;
  1459. }
  1460. void *Object::get_instance_binding(void *p_token, const GDNativeInstanceBindingCallbacks *p_callbacks) {
  1461. void *binding = nullptr;
  1462. _instance_binding_mutex.lock();
  1463. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1464. if (_instance_bindings[i].token == p_token) {
  1465. binding = _instance_bindings[i].binding;
  1466. break;
  1467. }
  1468. }
  1469. if (unlikely(!binding)) {
  1470. uint32_t current_size = next_power_of_2(_instance_binding_count);
  1471. uint32_t new_size = next_power_of_2(_instance_binding_count + 1);
  1472. if (current_size == 0 || new_size > current_size) {
  1473. _instance_bindings = (InstanceBinding *)memrealloc(_instance_bindings, new_size * sizeof(InstanceBinding));
  1474. }
  1475. _instance_bindings[_instance_binding_count].free_callback = p_callbacks->free_callback;
  1476. _instance_bindings[_instance_binding_count].reference_callback = p_callbacks->reference_callback;
  1477. _instance_bindings[_instance_binding_count].token = p_token;
  1478. binding = p_callbacks->create_callback(p_token, this);
  1479. _instance_bindings[_instance_binding_count].binding = binding;
  1480. _instance_binding_count++;
  1481. }
  1482. _instance_binding_mutex.unlock();
  1483. return binding;
  1484. }
  1485. bool Object::has_instance_binding(void *p_token) {
  1486. bool found = false;
  1487. _instance_binding_mutex.lock();
  1488. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1489. if (_instance_bindings[i].token == p_token) {
  1490. found = true;
  1491. break;
  1492. }
  1493. }
  1494. _instance_binding_mutex.unlock();
  1495. return found;
  1496. }
  1497. void Object::_construct_object(bool p_reference) {
  1498. type_is_reference = p_reference;
  1499. _instance_id = ObjectDB::add_instance(this);
  1500. ClassDB::instance_get_native_extension_data(&_extension, &_extension_instance, this);
  1501. #ifdef DEBUG_ENABLED
  1502. _lock_index.init(1);
  1503. #endif
  1504. }
  1505. Object::Object(bool p_reference) {
  1506. _construct_object(p_reference);
  1507. }
  1508. Object::Object() {
  1509. _construct_object(false);
  1510. }
  1511. Object::~Object() {
  1512. if (script_instance) {
  1513. memdelete(script_instance);
  1514. }
  1515. script_instance = nullptr;
  1516. if (_extension && _extension->free_instance) {
  1517. _extension->free_instance(_extension->class_userdata, _extension_instance);
  1518. _extension = nullptr;
  1519. _extension_instance = nullptr;
  1520. }
  1521. const StringName *S = nullptr;
  1522. if (_emitting) {
  1523. //@todo this may need to actually reach the debugger prioritarily somehow because it may crash before
  1524. 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.");
  1525. }
  1526. while ((S = signal_map.next(nullptr))) {
  1527. SignalData *s = &signal_map[*S];
  1528. //brute force disconnect for performance
  1529. int slot_count = s->slot_map.size();
  1530. const VMap<Callable, SignalData::Slot>::Pair *slot_list = s->slot_map.get_array();
  1531. for (int i = 0; i < slot_count; i++) {
  1532. slot_list[i].value.conn.callable.get_object()->connections.erase(slot_list[i].value.cE);
  1533. }
  1534. signal_map.erase(*S);
  1535. }
  1536. //signals from nodes that connect to this node
  1537. while (connections.size()) {
  1538. Connection c = connections.front()->get();
  1539. c.signal.get_object()->_disconnect(c.signal.get_name(), c.callable, true);
  1540. }
  1541. ObjectDB::remove_instance(this);
  1542. _instance_id = ObjectID();
  1543. _predelete_ok = 2;
  1544. if (_instance_bindings != nullptr) {
  1545. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1546. if (_instance_bindings[i].free_callback) {
  1547. _instance_bindings[i].free_callback(_instance_bindings[i].token, this, _instance_bindings[i].binding);
  1548. }
  1549. }
  1550. memfree(_instance_bindings);
  1551. }
  1552. }
  1553. bool predelete_handler(Object *p_object) {
  1554. return p_object->_predelete();
  1555. }
  1556. void postinitialize_handler(Object *p_object) {
  1557. p_object->_postinitialize();
  1558. }
  1559. void ObjectDB::debug_objects(DebugFunc p_func) {
  1560. spin_lock.lock();
  1561. for (uint32_t i = 0, count = slot_count; i < slot_max && count != 0; i++) {
  1562. if (object_slots[i].validator) {
  1563. p_func(object_slots[i].object);
  1564. count--;
  1565. }
  1566. }
  1567. spin_lock.unlock();
  1568. }
  1569. void Object::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1570. }
  1571. SpinLock ObjectDB::spin_lock;
  1572. uint32_t ObjectDB::slot_count = 0;
  1573. uint32_t ObjectDB::slot_max = 0;
  1574. ObjectDB::ObjectSlot *ObjectDB::object_slots = nullptr;
  1575. uint64_t ObjectDB::validator_counter = 0;
  1576. int ObjectDB::get_object_count() {
  1577. return slot_count;
  1578. }
  1579. ObjectID ObjectDB::add_instance(Object *p_object) {
  1580. spin_lock.lock();
  1581. if (unlikely(slot_count == slot_max)) {
  1582. CRASH_COND(slot_count == (1 << OBJECTDB_SLOT_MAX_COUNT_BITS));
  1583. uint32_t new_slot_max = slot_max > 0 ? slot_max * 2 : 1;
  1584. object_slots = (ObjectSlot *)memrealloc(object_slots, sizeof(ObjectSlot) * new_slot_max);
  1585. for (uint32_t i = slot_max; i < new_slot_max; i++) {
  1586. object_slots[i].object = nullptr;
  1587. object_slots[i].is_ref_counted = false;
  1588. object_slots[i].next_free = i;
  1589. object_slots[i].validator = 0;
  1590. }
  1591. slot_max = new_slot_max;
  1592. }
  1593. uint32_t slot = object_slots[slot_count].next_free;
  1594. if (object_slots[slot].object != nullptr) {
  1595. spin_lock.unlock();
  1596. ERR_FAIL_COND_V(object_slots[slot].object != nullptr, ObjectID());
  1597. }
  1598. object_slots[slot].object = p_object;
  1599. object_slots[slot].is_ref_counted = p_object->is_ref_counted();
  1600. validator_counter = (validator_counter + 1) & OBJECTDB_VALIDATOR_MASK;
  1601. if (unlikely(validator_counter == 0)) {
  1602. validator_counter = 1;
  1603. }
  1604. object_slots[slot].validator = validator_counter;
  1605. uint64_t id = validator_counter;
  1606. id <<= OBJECTDB_SLOT_MAX_COUNT_BITS;
  1607. id |= uint64_t(slot);
  1608. if (p_object->is_ref_counted()) {
  1609. id |= OBJECTDB_REFERENCE_BIT;
  1610. }
  1611. slot_count++;
  1612. spin_lock.unlock();
  1613. return ObjectID(id);
  1614. }
  1615. void ObjectDB::remove_instance(Object *p_object) {
  1616. uint64_t t = p_object->get_instance_id();
  1617. uint32_t slot = t & OBJECTDB_SLOT_MAX_COUNT_MASK; //slot is always valid on valid object
  1618. spin_lock.lock();
  1619. #ifdef DEBUG_ENABLED
  1620. if (object_slots[slot].object != p_object) {
  1621. spin_lock.unlock();
  1622. ERR_FAIL_COND(object_slots[slot].object != p_object);
  1623. }
  1624. {
  1625. uint64_t validator = (t >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
  1626. if (object_slots[slot].validator != validator) {
  1627. spin_lock.unlock();
  1628. ERR_FAIL_COND(object_slots[slot].validator != validator);
  1629. }
  1630. }
  1631. #endif
  1632. //decrease slot count
  1633. slot_count--;
  1634. //set the free slot properly
  1635. object_slots[slot_count].next_free = slot;
  1636. //invalidate, so checks against it fail
  1637. object_slots[slot].validator = 0;
  1638. object_slots[slot].is_ref_counted = false;
  1639. object_slots[slot].object = nullptr;
  1640. spin_lock.unlock();
  1641. }
  1642. void ObjectDB::setup() {
  1643. //nothing to do now
  1644. }
  1645. void ObjectDB::cleanup() {
  1646. if (slot_count > 0) {
  1647. spin_lock.lock();
  1648. WARN_PRINT("ObjectDB instances leaked at exit (run with --verbose for details).");
  1649. if (OS::get_singleton()->is_stdout_verbose()) {
  1650. // Ensure calling the native classes because if a leaked instance has a script
  1651. // that overrides any of those methods, it'd not be OK to call them at this point,
  1652. // now the scripting languages have already been terminated.
  1653. MethodBind *node_get_name = ClassDB::get_method("Node", "get_name");
  1654. MethodBind *resource_get_path = ClassDB::get_method("Resource", "get_path");
  1655. Callable::CallError call_error;
  1656. for (uint32_t i = 0, count = slot_count; i < slot_max && count != 0; i++) {
  1657. if (object_slots[i].validator) {
  1658. Object *obj = object_slots[i].object;
  1659. String extra_info;
  1660. if (obj->is_class("Node")) {
  1661. extra_info = " - Node name: " + String(node_get_name->call(obj, nullptr, 0, call_error));
  1662. }
  1663. if (obj->is_class("Resource")) {
  1664. extra_info = " - Resource path: " + String(resource_get_path->call(obj, nullptr, 0, call_error));
  1665. }
  1666. uint64_t id = uint64_t(i) | (uint64_t(object_slots[i].validator) << OBJECTDB_VALIDATOR_BITS) | (object_slots[i].is_ref_counted ? OBJECTDB_REFERENCE_BIT : 0);
  1667. print_line("Leaked instance: " + String(obj->get_class()) + ":" + itos(id) + extra_info);
  1668. count--;
  1669. }
  1670. }
  1671. 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()`).");
  1672. }
  1673. spin_lock.unlock();
  1674. }
  1675. if (object_slots) {
  1676. memfree(object_slots);
  1677. }
  1678. }