object.cpp 55 KB

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