object.cpp 57 KB

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