object.cpp 58 KB

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