object.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  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/variant/typed_array.h"
  40. #ifdef DEBUG_ENABLED
  41. struct _ObjectDebugLock {
  42. Object *obj;
  43. _ObjectDebugLock(Object *p_obj) {
  44. obj = p_obj;
  45. obj->_lock_index.ref();
  46. }
  47. ~_ObjectDebugLock() {
  48. obj->_lock_index.unref();
  49. }
  50. };
  51. #define OBJ_DEBUG_LOCK _ObjectDebugLock _debug_lock(this);
  52. #else
  53. #define OBJ_DEBUG_LOCK
  54. #endif
  55. PropertyInfo::operator Dictionary() const {
  56. Dictionary d;
  57. d["name"] = name;
  58. d["class_name"] = class_name;
  59. d["type"] = type;
  60. d["hint"] = hint;
  61. d["hint_string"] = hint_string;
  62. d["usage"] = usage;
  63. return d;
  64. }
  65. PropertyInfo PropertyInfo::from_dict(const Dictionary &p_dict) {
  66. PropertyInfo pi;
  67. if (p_dict.has("type")) {
  68. pi.type = Variant::Type(int(p_dict["type"]));
  69. }
  70. if (p_dict.has("name")) {
  71. pi.name = p_dict["name"];
  72. }
  73. if (p_dict.has("class_name")) {
  74. pi.class_name = p_dict["class_name"];
  75. }
  76. if (p_dict.has("hint")) {
  77. pi.hint = PropertyHint(int(p_dict["hint"]));
  78. }
  79. if (p_dict.has("hint_string")) {
  80. pi.hint_string = p_dict["hint_string"];
  81. }
  82. if (p_dict.has("usage")) {
  83. pi.usage = p_dict["usage"];
  84. }
  85. return pi;
  86. }
  87. TypedArray<Dictionary> convert_property_list(const List<PropertyInfo> *p_list) {
  88. TypedArray<Dictionary> va;
  89. for (const List<PropertyInfo>::Element *E = p_list->front(); E; E = E->next()) {
  90. va.push_back(Dictionary(E->get()));
  91. }
  92. return va;
  93. }
  94. MethodInfo::operator Dictionary() const {
  95. Dictionary d;
  96. d["name"] = name;
  97. d["args"] = convert_property_list(&arguments);
  98. Array da;
  99. for (int i = 0; i < default_arguments.size(); i++) {
  100. da.push_back(default_arguments[i]);
  101. }
  102. d["default_args"] = da;
  103. d["flags"] = flags;
  104. d["id"] = id;
  105. Dictionary r = return_val;
  106. d["return"] = r;
  107. return d;
  108. }
  109. MethodInfo MethodInfo::from_dict(const Dictionary &p_dict) {
  110. MethodInfo mi;
  111. if (p_dict.has("name")) {
  112. mi.name = p_dict["name"];
  113. }
  114. Array args;
  115. if (p_dict.has("args")) {
  116. args = p_dict["args"];
  117. }
  118. for (int i = 0; i < args.size(); i++) {
  119. Dictionary d = args[i];
  120. mi.arguments.push_back(PropertyInfo::from_dict(d));
  121. }
  122. Array defargs;
  123. if (p_dict.has("default_args")) {
  124. defargs = p_dict["default_args"];
  125. }
  126. for (int i = 0; i < defargs.size(); i++) {
  127. mi.default_arguments.push_back(defargs[i]);
  128. }
  129. if (p_dict.has("return")) {
  130. mi.return_val = PropertyInfo::from_dict(p_dict["return"]);
  131. }
  132. if (p_dict.has("flags")) {
  133. mi.flags = p_dict["flags"];
  134. }
  135. return mi;
  136. }
  137. Object::Connection::operator Variant() const {
  138. Dictionary d;
  139. d["signal"] = signal;
  140. d["callable"] = callable;
  141. d["flags"] = flags;
  142. return d;
  143. }
  144. bool Object::Connection::operator<(const Connection &p_conn) const {
  145. if (signal == p_conn.signal) {
  146. return callable < p_conn.callable;
  147. } else {
  148. return signal < p_conn.signal;
  149. }
  150. }
  151. Object::Connection::Connection(const Variant &p_variant) {
  152. Dictionary d = p_variant;
  153. if (d.has("signal")) {
  154. signal = d["signal"];
  155. }
  156. if (d.has("callable")) {
  157. callable = d["callable"];
  158. }
  159. if (d.has("flags")) {
  160. flags = d["flags"];
  161. }
  162. }
  163. bool Object::_predelete() {
  164. _predelete_ok = 1;
  165. notification(NOTIFICATION_PREDELETE, true);
  166. if (_predelete_ok) {
  167. _class_name_ptr = nullptr; // Must restore, so constructors/destructors have proper class name access at each stage.
  168. }
  169. return _predelete_ok;
  170. }
  171. void Object::_postinitialize() {
  172. _class_name_ptr = _get_class_namev(); // Set the direct pointer, which is much faster to obtain, but can only happen after postinitialize.
  173. _initialize_classv();
  174. _class_name_ptr = nullptr; // May have been called from a constructor.
  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. //copy on write will ensure that disconnecting the signal or even deleting the object will not affect the signal calling.
  853. //this happens automatically and will not change the performance of calling.
  854. //awesome, isn't it?
  855. VMap<Callable, SignalData::Slot> slot_map = s->slot_map;
  856. int ssize = slot_map.size();
  857. OBJ_DEBUG_LOCK
  858. Error err = OK;
  859. for (int i = 0; i < ssize; i++) {
  860. const Connection &c = slot_map.getv(i).conn;
  861. Object *target = c.callable.get_object();
  862. if (!target) {
  863. // Target might have been deleted during signal callback, this is expected and OK.
  864. continue;
  865. }
  866. const Variant **args = p_args;
  867. int argc = p_argcount;
  868. if (c.flags & CONNECT_DEFERRED) {
  869. MessageQueue::get_singleton()->push_callablep(c.callable, args, argc, true);
  870. } else {
  871. Callable::CallError ce;
  872. _emitting = true;
  873. Variant ret;
  874. c.callable.callp(args, argc, ret, ce);
  875. _emitting = false;
  876. if (ce.error != Callable::CallError::CALL_OK) {
  877. #ifdef DEBUG_ENABLED
  878. if (c.flags & CONNECT_PERSIST && Engine::get_singleton()->is_editor_hint() && (script.is_null() || !Ref<Script>(script)->is_tool())) {
  879. continue;
  880. }
  881. #endif
  882. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD && !ClassDB::class_exists(target->get_class_name())) {
  883. //most likely object is not initialized yet, do not throw error.
  884. } else {
  885. ERR_PRINT("Error calling from signal '" + String(p_name) + "' to callable: " + Variant::get_callable_error_text(c.callable, args, argc, ce) + ".");
  886. err = ERR_METHOD_NOT_FOUND;
  887. }
  888. }
  889. }
  890. bool disconnect = c.flags & CONNECT_ONE_SHOT;
  891. #ifdef TOOLS_ENABLED
  892. if (disconnect && (c.flags & CONNECT_PERSIST) && Engine::get_singleton()->is_editor_hint()) {
  893. //this signal was connected from the editor, and is being edited. just don't disconnect for now
  894. disconnect = false;
  895. }
  896. #endif
  897. if (disconnect) {
  898. _ObjectSignalDisconnectData dd;
  899. dd.signal = p_name;
  900. dd.callable = c.callable;
  901. disconnect_data.push_back(dd);
  902. }
  903. }
  904. while (!disconnect_data.is_empty()) {
  905. const _ObjectSignalDisconnectData &dd = disconnect_data.front()->get();
  906. _disconnect(dd.signal, dd.callable);
  907. disconnect_data.pop_front();
  908. }
  909. return err;
  910. }
  911. void Object::_add_user_signal(const String &p_name, const Array &p_args) {
  912. // this version of add_user_signal is meant to be used from scripts or external apis
  913. // without access to ADD_SIGNAL in bind_methods
  914. // added events are per instance, as opposed to the other ones, which are global
  915. MethodInfo mi;
  916. mi.name = p_name;
  917. for (int i = 0; i < p_args.size(); i++) {
  918. Dictionary d = p_args[i];
  919. PropertyInfo param;
  920. if (d.has("name")) {
  921. param.name = d["name"];
  922. }
  923. if (d.has("type")) {
  924. param.type = (Variant::Type)(int)d["type"];
  925. }
  926. mi.arguments.push_back(param);
  927. }
  928. add_user_signal(mi);
  929. }
  930. TypedArray<Dictionary> Object::_get_signal_list() const {
  931. List<MethodInfo> signal_list;
  932. get_signal_list(&signal_list);
  933. TypedArray<Dictionary> ret;
  934. for (const MethodInfo &E : signal_list) {
  935. ret.push_back(Dictionary(E));
  936. }
  937. return ret;
  938. }
  939. TypedArray<Dictionary> Object::_get_signal_connection_list(const StringName &p_signal) const {
  940. List<Connection> conns;
  941. get_all_signal_connections(&conns);
  942. TypedArray<Dictionary> ret;
  943. for (const Connection &c : conns) {
  944. if (c.signal.get_name() == p_signal) {
  945. ret.push_back(c);
  946. }
  947. }
  948. return ret;
  949. }
  950. TypedArray<Dictionary> Object::_get_incoming_connections() const {
  951. TypedArray<Dictionary> ret;
  952. int connections_amount = connections.size();
  953. for (int idx_conn = 0; idx_conn < connections_amount; idx_conn++) {
  954. ret.push_back(connections[idx_conn]);
  955. }
  956. return ret;
  957. }
  958. bool Object::has_signal(const StringName &p_name) const {
  959. if (!script.is_null()) {
  960. Ref<Script> scr = script;
  961. if (scr.is_valid() && scr->has_script_signal(p_name)) {
  962. return true;
  963. }
  964. }
  965. if (ClassDB::has_signal(get_class_name(), p_name)) {
  966. return true;
  967. }
  968. if (_has_user_signal(p_name)) {
  969. return true;
  970. }
  971. return false;
  972. }
  973. void Object::get_signal_list(List<MethodInfo> *p_signals) const {
  974. if (!script.is_null()) {
  975. Ref<Script> scr = script;
  976. if (scr.is_valid()) {
  977. scr->get_script_signal_list(p_signals);
  978. }
  979. }
  980. ClassDB::get_signal_list(get_class_name(), p_signals);
  981. //find maybe usersignals?
  982. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  983. if (!E.value.user.name.is_empty()) {
  984. //user signal
  985. p_signals->push_back(E.value.user);
  986. }
  987. }
  988. }
  989. void Object::get_all_signal_connections(List<Connection> *p_connections) const {
  990. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  991. const SignalData *s = &E.value;
  992. for (int i = 0; i < s->slot_map.size(); i++) {
  993. p_connections->push_back(s->slot_map.getv(i).conn);
  994. }
  995. }
  996. }
  997. void Object::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
  998. const SignalData *s = signal_map.getptr(p_signal);
  999. if (!s) {
  1000. return; //nothing
  1001. }
  1002. for (int i = 0; i < s->slot_map.size(); i++) {
  1003. p_connections->push_back(s->slot_map.getv(i).conn);
  1004. }
  1005. }
  1006. int Object::get_persistent_signal_connection_count() const {
  1007. int count = 0;
  1008. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  1009. const SignalData *s = &E.value;
  1010. for (int i = 0; i < s->slot_map.size(); i++) {
  1011. if (s->slot_map.getv(i).conn.flags & CONNECT_PERSIST) {
  1012. count += 1;
  1013. }
  1014. }
  1015. }
  1016. return count;
  1017. }
  1018. void Object::get_signals_connected_to_this(List<Connection> *p_connections) const {
  1019. for (const Connection &E : connections) {
  1020. p_connections->push_back(E);
  1021. }
  1022. }
  1023. Error Object::connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags) {
  1024. ERR_FAIL_COND_V_MSG(p_callable.is_null(), ERR_INVALID_PARAMETER, "Cannot connect to '" + p_signal + "': the provided callable is null.");
  1025. Object *target_object = p_callable.get_object();
  1026. ERR_FAIL_COND_V_MSG(!target_object, ERR_INVALID_PARAMETER, "Cannot connect to '" + p_signal + "' to callable '" + p_callable + "': the callable object is null.");
  1027. SignalData *s = signal_map.getptr(p_signal);
  1028. if (!s) {
  1029. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1030. //check in script
  1031. if (!signal_is_valid && !script.is_null()) {
  1032. if (Ref<Script>(script)->has_script_signal(p_signal)) {
  1033. signal_is_valid = true;
  1034. }
  1035. #ifdef TOOLS_ENABLED
  1036. else {
  1037. //allow connecting signals anyway if script is invalid, see issue #17070
  1038. if (!Ref<Script>(script)->is_valid()) {
  1039. signal_is_valid = true;
  1040. }
  1041. }
  1042. #endif
  1043. }
  1044. 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 + "'.");
  1045. signal_map[p_signal] = SignalData();
  1046. s = &signal_map[p_signal];
  1047. }
  1048. Callable target = p_callable;
  1049. //compare with the base callable, so binds can be ignored
  1050. if (s->slot_map.has(*target.get_base_comparator())) {
  1051. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1052. s->slot_map[*target.get_base_comparator()].reference_count++;
  1053. return OK;
  1054. } else {
  1055. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Signal '" + p_signal + "' is already connected to given callable '" + p_callable + "' in that object.");
  1056. }
  1057. }
  1058. SignalData::Slot slot;
  1059. Connection conn;
  1060. conn.callable = target;
  1061. conn.signal = ::Signal(this, p_signal);
  1062. conn.flags = p_flags;
  1063. slot.conn = conn;
  1064. slot.cE = target_object->connections.push_back(conn);
  1065. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1066. slot.reference_count = 1;
  1067. }
  1068. //use callable version as key, so binds can be ignored
  1069. s->slot_map[*target.get_base_comparator()] = slot;
  1070. return OK;
  1071. }
  1072. bool Object::is_connected(const StringName &p_signal, const Callable &p_callable) const {
  1073. ERR_FAIL_COND_V_MSG(p_callable.is_null(), false, "Cannot determine if connected to '" + p_signal + "': the provided callable is null.");
  1074. const SignalData *s = signal_map.getptr(p_signal);
  1075. if (!s) {
  1076. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1077. if (signal_is_valid) {
  1078. return false;
  1079. }
  1080. if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal)) {
  1081. return false;
  1082. }
  1083. ERR_FAIL_V_MSG(false, "Nonexistent signal: " + p_signal + ".");
  1084. }
  1085. Callable target = p_callable;
  1086. return s->slot_map.has(*target.get_base_comparator());
  1087. }
  1088. void Object::disconnect(const StringName &p_signal, const Callable &p_callable) {
  1089. _disconnect(p_signal, p_callable);
  1090. }
  1091. void Object::_disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force) {
  1092. ERR_FAIL_COND_MSG(p_callable.is_null(), "Cannot disconnect from '" + p_signal + "': the provided callable is null.");
  1093. Object *target_object = p_callable.get_object();
  1094. ERR_FAIL_COND_MSG(!target_object, "Cannot disconnect '" + p_signal + "' from callable '" + p_callable + "': the callable object is null.");
  1095. SignalData *s = signal_map.getptr(p_signal);
  1096. if (!s) {
  1097. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal) ||
  1098. (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal));
  1099. ERR_FAIL_COND_MSG(signal_is_valid, "Attempt to disconnect a nonexistent connection from '" + to_string() + "'. Signal: '" + p_signal + "', callable: '" + p_callable + "'.");
  1100. }
  1101. ERR_FAIL_COND_MSG(!s, vformat("Disconnecting nonexistent signal '%s' in %s.", p_signal, to_string()));
  1102. ERR_FAIL_COND_MSG(!s->slot_map.has(*p_callable.get_base_comparator()), "Disconnecting nonexistent signal '" + p_signal + "', callable: " + p_callable + ".");
  1103. SignalData::Slot *slot = &s->slot_map[*p_callable.get_base_comparator()];
  1104. if (!p_force) {
  1105. slot->reference_count--; // by default is zero, if it was not referenced it will go below it
  1106. if (slot->reference_count > 0) {
  1107. return;
  1108. }
  1109. }
  1110. target_object->connections.erase(slot->cE);
  1111. s->slot_map.erase(*p_callable.get_base_comparator());
  1112. if (s->slot_map.is_empty() && ClassDB::has_signal(get_class_name(), p_signal)) {
  1113. //not user signal, delete
  1114. signal_map.erase(p_signal);
  1115. }
  1116. }
  1117. void Object::_set_bind(const StringName &p_set, const Variant &p_value) {
  1118. set(p_set, p_value);
  1119. }
  1120. Variant Object::_get_bind(const StringName &p_name) const {
  1121. return get(p_name);
  1122. }
  1123. void Object::_set_indexed_bind(const NodePath &p_name, const Variant &p_value) {
  1124. set_indexed(p_name.get_as_property_path().get_subnames(), p_value);
  1125. }
  1126. Variant Object::_get_indexed_bind(const NodePath &p_name) const {
  1127. return get_indexed(p_name.get_as_property_path().get_subnames());
  1128. }
  1129. void Object::initialize_class() {
  1130. static bool initialized = false;
  1131. if (initialized) {
  1132. return;
  1133. }
  1134. ClassDB::_add_class<Object>();
  1135. _bind_methods();
  1136. initialized = true;
  1137. }
  1138. String Object::tr(const StringName &p_message, const StringName &p_context) const {
  1139. if (!_can_translate || !TranslationServer::get_singleton()) {
  1140. return p_message;
  1141. }
  1142. if (Engine::get_singleton()->is_editor_hint()) {
  1143. return TranslationServer::get_singleton()->tool_translate(p_message, p_context);
  1144. } else {
  1145. return TranslationServer::get_singleton()->translate(p_message, p_context);
  1146. }
  1147. }
  1148. String Object::tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  1149. if (!_can_translate || !TranslationServer::get_singleton()) {
  1150. // Return message based on English plural rule if translation is not possible.
  1151. if (p_n == 1) {
  1152. return p_message;
  1153. }
  1154. return p_message_plural;
  1155. }
  1156. if (Engine::get_singleton()->is_editor_hint()) {
  1157. return TranslationServer::get_singleton()->tool_translate_plural(p_message, p_message_plural, p_n, p_context);
  1158. } else {
  1159. return TranslationServer::get_singleton()->translate_plural(p_message, p_message_plural, p_n, p_context);
  1160. }
  1161. }
  1162. void Object::_clear_internal_resource_paths(const Variant &p_var) {
  1163. switch (p_var.get_type()) {
  1164. case Variant::OBJECT: {
  1165. Ref<Resource> r = p_var;
  1166. if (!r.is_valid()) {
  1167. return;
  1168. }
  1169. if (!r->is_built_in()) {
  1170. return; //not an internal resource
  1171. }
  1172. Object *object = p_var;
  1173. if (!object) {
  1174. return;
  1175. }
  1176. r->set_path("");
  1177. r->clear_internal_resource_paths();
  1178. } break;
  1179. case Variant::ARRAY: {
  1180. Array a = p_var;
  1181. for (int i = 0; i < a.size(); i++) {
  1182. _clear_internal_resource_paths(a[i]);
  1183. }
  1184. } break;
  1185. case Variant::DICTIONARY: {
  1186. Dictionary d = p_var;
  1187. List<Variant> keys;
  1188. d.get_key_list(&keys);
  1189. for (const Variant &E : keys) {
  1190. _clear_internal_resource_paths(E);
  1191. _clear_internal_resource_paths(d[E]);
  1192. }
  1193. } break;
  1194. default: {
  1195. }
  1196. }
  1197. }
  1198. #ifdef TOOLS_ENABLED
  1199. void Object::editor_set_section_unfold(const String &p_section, bool p_unfolded) {
  1200. set_edited(true);
  1201. if (p_unfolded) {
  1202. editor_section_folding.insert(p_section);
  1203. } else {
  1204. editor_section_folding.erase(p_section);
  1205. }
  1206. }
  1207. bool Object::editor_is_section_unfolded(const String &p_section) {
  1208. return editor_section_folding.has(p_section);
  1209. }
  1210. #endif
  1211. void Object::clear_internal_resource_paths() {
  1212. List<PropertyInfo> pinfo;
  1213. get_property_list(&pinfo);
  1214. for (const PropertyInfo &E : pinfo) {
  1215. _clear_internal_resource_paths(get(E.name));
  1216. }
  1217. }
  1218. void Object::notify_property_list_changed() {
  1219. emit_signal(CoreStringNames::get_singleton()->property_list_changed);
  1220. }
  1221. void Object::_bind_methods() {
  1222. ClassDB::bind_method(D_METHOD("get_class"), &Object::get_class);
  1223. ClassDB::bind_method(D_METHOD("is_class", "class"), &Object::is_class);
  1224. ClassDB::bind_method(D_METHOD("set", "property", "value"), &Object::_set_bind);
  1225. ClassDB::bind_method(D_METHOD("get", "property"), &Object::_get_bind);
  1226. ClassDB::bind_method(D_METHOD("set_indexed", "property_path", "value"), &Object::_set_indexed_bind);
  1227. ClassDB::bind_method(D_METHOD("get_indexed", "property_path"), &Object::_get_indexed_bind);
  1228. ClassDB::bind_method(D_METHOD("get_property_list"), &Object::_get_property_list_bind);
  1229. ClassDB::bind_method(D_METHOD("get_method_list"), &Object::_get_method_list_bind);
  1230. ClassDB::bind_method(D_METHOD("property_can_revert", "property"), &Object::property_can_revert);
  1231. ClassDB::bind_method(D_METHOD("property_get_revert", "property"), &Object::property_get_revert);
  1232. ClassDB::bind_method(D_METHOD("notification", "what", "reversed"), &Object::notification, DEFVAL(false));
  1233. ClassDB::bind_method(D_METHOD("to_string"), &Object::to_string);
  1234. ClassDB::bind_method(D_METHOD("get_instance_id"), &Object::get_instance_id);
  1235. ClassDB::bind_method(D_METHOD("set_script", "script"), &Object::set_script);
  1236. ClassDB::bind_method(D_METHOD("get_script"), &Object::get_script);
  1237. ClassDB::bind_method(D_METHOD("set_meta", "name", "value"), &Object::set_meta);
  1238. ClassDB::bind_method(D_METHOD("remove_meta", "name"), &Object::remove_meta);
  1239. ClassDB::bind_method(D_METHOD("get_meta", "name", "default"), &Object::get_meta, DEFVAL(Variant()));
  1240. ClassDB::bind_method(D_METHOD("has_meta", "name"), &Object::has_meta);
  1241. ClassDB::bind_method(D_METHOD("get_meta_list"), &Object::_get_meta_list_bind);
  1242. ClassDB::bind_method(D_METHOD("add_user_signal", "signal", "arguments"), &Object::_add_user_signal, DEFVAL(Array()));
  1243. ClassDB::bind_method(D_METHOD("has_user_signal", "signal"), &Object::_has_user_signal);
  1244. {
  1245. MethodInfo mi;
  1246. mi.name = "emit_signal";
  1247. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "signal"));
  1248. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "emit_signal", &Object::_emit_signal, mi, varray(), false);
  1249. }
  1250. {
  1251. MethodInfo mi;
  1252. mi.name = "call";
  1253. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1254. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call", &Object::_call_bind, mi);
  1255. }
  1256. {
  1257. MethodInfo mi;
  1258. mi.name = "call_deferred";
  1259. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1260. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_deferred", &Object::_call_deferred_bind, mi, varray(), false);
  1261. }
  1262. ClassDB::bind_method(D_METHOD("set_deferred", "property", "value"), &Object::set_deferred);
  1263. ClassDB::bind_method(D_METHOD("callv", "method", "arg_array"), &Object::callv);
  1264. ClassDB::bind_method(D_METHOD("has_method", "method"), &Object::has_method);
  1265. ClassDB::bind_method(D_METHOD("has_signal", "signal"), &Object::has_signal);
  1266. ClassDB::bind_method(D_METHOD("get_signal_list"), &Object::_get_signal_list);
  1267. ClassDB::bind_method(D_METHOD("get_signal_connection_list", "signal"), &Object::_get_signal_connection_list);
  1268. ClassDB::bind_method(D_METHOD("get_incoming_connections"), &Object::_get_incoming_connections);
  1269. ClassDB::bind_method(D_METHOD("connect", "signal", "callable", "flags"), &Object::connect, DEFVAL(0));
  1270. ClassDB::bind_method(D_METHOD("disconnect", "signal", "callable"), &Object::disconnect);
  1271. ClassDB::bind_method(D_METHOD("is_connected", "signal", "callable"), &Object::is_connected);
  1272. ClassDB::bind_method(D_METHOD("set_block_signals", "enable"), &Object::set_block_signals);
  1273. ClassDB::bind_method(D_METHOD("is_blocking_signals"), &Object::is_blocking_signals);
  1274. ClassDB::bind_method(D_METHOD("notify_property_list_changed"), &Object::notify_property_list_changed);
  1275. ClassDB::bind_method(D_METHOD("set_message_translation", "enable"), &Object::set_message_translation);
  1276. ClassDB::bind_method(D_METHOD("can_translate_messages"), &Object::can_translate_messages);
  1277. ClassDB::bind_method(D_METHOD("tr", "message", "context"), &Object::tr, DEFVAL(""));
  1278. ClassDB::bind_method(D_METHOD("tr_n", "message", "plural_message", "n", "context"), &Object::tr_n, DEFVAL(""));
  1279. ClassDB::bind_method(D_METHOD("is_queued_for_deletion"), &Object::is_queued_for_deletion);
  1280. ClassDB::add_virtual_method("Object", MethodInfo("free"), false);
  1281. ADD_SIGNAL(MethodInfo("script_changed"));
  1282. ADD_SIGNAL(MethodInfo("property_list_changed"));
  1283. #define BIND_OBJ_CORE_METHOD(m_method) \
  1284. ::ClassDB::add_virtual_method(get_class_static(), m_method, true, Vector<String>(), true);
  1285. MethodInfo notification_mi("_notification", PropertyInfo(Variant::INT, "what"));
  1286. notification_mi.arguments_metadata.push_back(GodotTypeInfo::Metadata::METADATA_INT_IS_INT32);
  1287. BIND_OBJ_CORE_METHOD(notification_mi);
  1288. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_set", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value")));
  1289. #ifdef TOOLS_ENABLED
  1290. MethodInfo miget("_get", PropertyInfo(Variant::STRING_NAME, "property"));
  1291. miget.return_val.name = "Variant";
  1292. miget.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1293. BIND_OBJ_CORE_METHOD(miget);
  1294. MethodInfo plget("_get_property_list");
  1295. plget.return_val.type = Variant::ARRAY;
  1296. plget.return_val.hint = PROPERTY_HINT_ARRAY_TYPE;
  1297. plget.return_val.hint_string = "Dictionary";
  1298. BIND_OBJ_CORE_METHOD(plget);
  1299. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_property_can_revert", PropertyInfo(Variant::STRING_NAME, "property")));
  1300. MethodInfo mipgr("_property_get_revert", PropertyInfo(Variant::STRING_NAME, "property"));
  1301. mipgr.return_val.name = "Variant";
  1302. mipgr.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1303. BIND_OBJ_CORE_METHOD(mipgr);
  1304. #endif
  1305. BIND_OBJ_CORE_METHOD(MethodInfo("_init"));
  1306. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::STRING, "_to_string"));
  1307. BIND_CONSTANT(NOTIFICATION_POSTINITIALIZE);
  1308. BIND_CONSTANT(NOTIFICATION_PREDELETE);
  1309. BIND_ENUM_CONSTANT(CONNECT_DEFERRED);
  1310. BIND_ENUM_CONSTANT(CONNECT_PERSIST);
  1311. BIND_ENUM_CONSTANT(CONNECT_ONE_SHOT);
  1312. BIND_ENUM_CONSTANT(CONNECT_REFERENCE_COUNTED);
  1313. }
  1314. void Object::set_deferred(const StringName &p_property, const Variant &p_value) {
  1315. MessageQueue::get_singleton()->push_set(this, p_property, p_value);
  1316. }
  1317. void Object::set_block_signals(bool p_block) {
  1318. _block_signals = p_block;
  1319. }
  1320. bool Object::is_blocking_signals() const {
  1321. return _block_signals;
  1322. }
  1323. Variant::Type Object::get_static_property_type(const StringName &p_property, bool *r_valid) const {
  1324. bool valid;
  1325. Variant::Type t = ClassDB::get_property_type(get_class_name(), p_property, &valid);
  1326. if (valid) {
  1327. if (r_valid) {
  1328. *r_valid = true;
  1329. }
  1330. return t;
  1331. }
  1332. if (get_script_instance()) {
  1333. return get_script_instance()->get_property_type(p_property, r_valid);
  1334. }
  1335. if (r_valid) {
  1336. *r_valid = false;
  1337. }
  1338. return Variant::NIL;
  1339. }
  1340. Variant::Type Object::get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid) const {
  1341. if (p_path.size() == 0) {
  1342. if (r_valid) {
  1343. *r_valid = false;
  1344. }
  1345. return Variant::NIL;
  1346. }
  1347. bool valid = false;
  1348. Variant::Type t = get_static_property_type(p_path[0], &valid);
  1349. if (!valid) {
  1350. if (r_valid) {
  1351. *r_valid = false;
  1352. }
  1353. return Variant::NIL;
  1354. }
  1355. Callable::CallError ce;
  1356. Variant check;
  1357. Variant::construct(t, check, nullptr, 0, ce);
  1358. for (int i = 1; i < p_path.size(); i++) {
  1359. if (check.get_type() == Variant::OBJECT || check.get_type() == Variant::DICTIONARY || check.get_type() == Variant::ARRAY) {
  1360. // We cannot be sure about the type of properties this type can have
  1361. if (r_valid) {
  1362. *r_valid = false;
  1363. }
  1364. return Variant::NIL;
  1365. }
  1366. check = check.get_named(p_path[i], valid);
  1367. if (!valid) {
  1368. if (r_valid) {
  1369. *r_valid = false;
  1370. }
  1371. return Variant::NIL;
  1372. }
  1373. }
  1374. if (r_valid) {
  1375. *r_valid = true;
  1376. }
  1377. return check.get_type();
  1378. }
  1379. bool Object::is_queued_for_deletion() const {
  1380. return _is_queued_for_deletion;
  1381. }
  1382. #ifdef TOOLS_ENABLED
  1383. void Object::set_edited(bool p_edited) {
  1384. _edited = p_edited;
  1385. _edited_version++;
  1386. }
  1387. bool Object::is_edited() const {
  1388. return _edited;
  1389. }
  1390. uint32_t Object::get_edited_version() const {
  1391. return _edited_version;
  1392. }
  1393. #endif
  1394. void Object::set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
  1395. // This is only meant to be used on creation by the binder.
  1396. ERR_FAIL_COND(_instance_bindings != nullptr);
  1397. _instance_bindings = (InstanceBinding *)memalloc(sizeof(InstanceBinding));
  1398. _instance_bindings[0].binding = p_binding;
  1399. _instance_bindings[0].free_callback = p_callbacks->free_callback;
  1400. _instance_bindings[0].reference_callback = p_callbacks->reference_callback;
  1401. _instance_bindings[0].token = p_token;
  1402. _instance_binding_count = 1;
  1403. }
  1404. void *Object::get_instance_binding(void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
  1405. void *binding = nullptr;
  1406. _instance_binding_mutex.lock();
  1407. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1408. if (_instance_bindings[i].token == p_token) {
  1409. binding = _instance_bindings[i].binding;
  1410. break;
  1411. }
  1412. }
  1413. if (unlikely(!binding)) {
  1414. uint32_t current_size = next_power_of_2(_instance_binding_count);
  1415. uint32_t new_size = next_power_of_2(_instance_binding_count + 1);
  1416. if (current_size == 0 || new_size > current_size) {
  1417. _instance_bindings = (InstanceBinding *)memrealloc(_instance_bindings, new_size * sizeof(InstanceBinding));
  1418. }
  1419. _instance_bindings[_instance_binding_count].free_callback = p_callbacks->free_callback;
  1420. _instance_bindings[_instance_binding_count].reference_callback = p_callbacks->reference_callback;
  1421. _instance_bindings[_instance_binding_count].token = p_token;
  1422. binding = p_callbacks->create_callback(p_token, this);
  1423. _instance_bindings[_instance_binding_count].binding = binding;
  1424. _instance_binding_count++;
  1425. }
  1426. _instance_binding_mutex.unlock();
  1427. return binding;
  1428. }
  1429. bool Object::has_instance_binding(void *p_token) {
  1430. bool found = false;
  1431. _instance_binding_mutex.lock();
  1432. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1433. if (_instance_bindings[i].token == p_token) {
  1434. found = true;
  1435. break;
  1436. }
  1437. }
  1438. _instance_binding_mutex.unlock();
  1439. return found;
  1440. }
  1441. void Object::_construct_object(bool p_reference) {
  1442. type_is_reference = p_reference;
  1443. _instance_id = ObjectDB::add_instance(this);
  1444. #ifdef DEBUG_ENABLED
  1445. _lock_index.init(1);
  1446. #endif
  1447. }
  1448. Object::Object(bool p_reference) {
  1449. _construct_object(p_reference);
  1450. }
  1451. Object::Object() {
  1452. _construct_object(false);
  1453. }
  1454. void Object::detach_from_objectdb() {
  1455. if (_instance_id != ObjectID()) {
  1456. ObjectDB::remove_instance(this);
  1457. _instance_id = ObjectID();
  1458. }
  1459. }
  1460. Object::~Object() {
  1461. if (script_instance) {
  1462. memdelete(script_instance);
  1463. }
  1464. script_instance = nullptr;
  1465. if (_extension && _extension->free_instance) {
  1466. _extension->free_instance(_extension->class_userdata, _extension_instance);
  1467. _extension = nullptr;
  1468. _extension_instance = nullptr;
  1469. }
  1470. if (_emitting) {
  1471. //@todo this may need to actually reach the debugger prioritarily somehow because it may crash before
  1472. 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.");
  1473. }
  1474. while (signal_map.size()) {
  1475. // Avoid regular iteration so erasing is safe.
  1476. KeyValue<StringName, SignalData> &E = *signal_map.begin();
  1477. SignalData *s = &E.value;
  1478. //brute force disconnect for performance
  1479. int slot_count = s->slot_map.size();
  1480. const VMap<Callable, SignalData::Slot>::Pair *slot_list = s->slot_map.get_array();
  1481. for (int i = 0; i < slot_count; i++) {
  1482. slot_list[i].value.conn.callable.get_object()->connections.erase(slot_list[i].value.cE);
  1483. }
  1484. signal_map.erase(E.key);
  1485. }
  1486. //signals from nodes that connect to this node
  1487. while (connections.size()) {
  1488. Connection c = connections.front()->get();
  1489. c.signal.get_object()->_disconnect(c.signal.get_name(), c.callable, true);
  1490. }
  1491. if (_instance_id != ObjectID()) {
  1492. ObjectDB::remove_instance(this);
  1493. _instance_id = ObjectID();
  1494. }
  1495. _predelete_ok = 2;
  1496. if (_instance_bindings != nullptr) {
  1497. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1498. if (_instance_bindings[i].free_callback) {
  1499. _instance_bindings[i].free_callback(_instance_bindings[i].token, this, _instance_bindings[i].binding);
  1500. }
  1501. }
  1502. memfree(_instance_bindings);
  1503. }
  1504. }
  1505. bool predelete_handler(Object *p_object) {
  1506. return p_object->_predelete();
  1507. }
  1508. void postinitialize_handler(Object *p_object) {
  1509. p_object->_postinitialize();
  1510. }
  1511. void ObjectDB::debug_objects(DebugFunc p_func) {
  1512. spin_lock.lock();
  1513. for (uint32_t i = 0, count = slot_count; i < slot_max && count != 0; i++) {
  1514. if (object_slots[i].validator) {
  1515. p_func(object_slots[i].object);
  1516. count--;
  1517. }
  1518. }
  1519. spin_lock.unlock();
  1520. }
  1521. void Object::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1522. if (p_idx == 0) {
  1523. if (p_function == "connect" || p_function == "is_connected" || p_function == "disconnect" || p_function == "emit_signal" || p_function == "has_signal") {
  1524. List<MethodInfo> signals;
  1525. get_signal_list(&signals);
  1526. for (const MethodInfo &E : signals) {
  1527. r_options->push_back(E.name.quote());
  1528. }
  1529. } else if (p_function == "call" || p_function == "call_deferred" || p_function == "callv" || p_function == "has_method") {
  1530. List<MethodInfo> methods;
  1531. get_method_list(&methods);
  1532. for (const MethodInfo &E : methods) {
  1533. if (E.name.begins_with("_") && !(E.flags & METHOD_FLAG_VIRTUAL)) {
  1534. continue;
  1535. }
  1536. r_options->push_back(E.name.quote());
  1537. }
  1538. } else if (p_function == "set" || p_function == "set_deferred" || p_function == "get") {
  1539. List<PropertyInfo> properties;
  1540. get_property_list(&properties);
  1541. for (const PropertyInfo &E : properties) {
  1542. if (E.usage & PROPERTY_USAGE_DEFAULT && !(E.usage & PROPERTY_USAGE_INTERNAL)) {
  1543. r_options->push_back(E.name.quote());
  1544. }
  1545. }
  1546. } else if (p_function == "set_meta" || p_function == "get_meta" || p_function == "has_meta" || p_function == "remove_meta") {
  1547. for (const KeyValue<StringName, Variant> &K : metadata) {
  1548. r_options->push_back(String(K.key).quote());
  1549. }
  1550. }
  1551. } else if (p_idx == 2) {
  1552. if (p_function == "connect") {
  1553. // Ideally, the constants should be inferred by the parameter.
  1554. // But a parameter's PropertyInfo does not store the enum they come from, so this will do for now.
  1555. List<StringName> constants;
  1556. ClassDB::get_enum_constants("Object", "ConnectFlags", &constants);
  1557. for (const StringName &E : constants) {
  1558. r_options->push_back(String(E));
  1559. }
  1560. }
  1561. }
  1562. }
  1563. SpinLock ObjectDB::spin_lock;
  1564. uint32_t ObjectDB::slot_count = 0;
  1565. uint32_t ObjectDB::slot_max = 0;
  1566. ObjectDB::ObjectSlot *ObjectDB::object_slots = nullptr;
  1567. uint64_t ObjectDB::validator_counter = 0;
  1568. int ObjectDB::get_object_count() {
  1569. return slot_count;
  1570. }
  1571. ObjectID ObjectDB::add_instance(Object *p_object) {
  1572. spin_lock.lock();
  1573. if (unlikely(slot_count == slot_max)) {
  1574. CRASH_COND(slot_count == (1 << OBJECTDB_SLOT_MAX_COUNT_BITS));
  1575. uint32_t new_slot_max = slot_max > 0 ? slot_max * 2 : 1;
  1576. object_slots = (ObjectSlot *)memrealloc(object_slots, sizeof(ObjectSlot) * new_slot_max);
  1577. for (uint32_t i = slot_max; i < new_slot_max; i++) {
  1578. object_slots[i].object = nullptr;
  1579. object_slots[i].is_ref_counted = false;
  1580. object_slots[i].next_free = i;
  1581. object_slots[i].validator = 0;
  1582. }
  1583. slot_max = new_slot_max;
  1584. }
  1585. uint32_t slot = object_slots[slot_count].next_free;
  1586. if (object_slots[slot].object != nullptr) {
  1587. spin_lock.unlock();
  1588. ERR_FAIL_COND_V(object_slots[slot].object != nullptr, ObjectID());
  1589. }
  1590. object_slots[slot].object = p_object;
  1591. object_slots[slot].is_ref_counted = p_object->is_ref_counted();
  1592. validator_counter = (validator_counter + 1) & OBJECTDB_VALIDATOR_MASK;
  1593. if (unlikely(validator_counter == 0)) {
  1594. validator_counter = 1;
  1595. }
  1596. object_slots[slot].validator = validator_counter;
  1597. uint64_t id = validator_counter;
  1598. id <<= OBJECTDB_SLOT_MAX_COUNT_BITS;
  1599. id |= uint64_t(slot);
  1600. if (p_object->is_ref_counted()) {
  1601. id |= OBJECTDB_REFERENCE_BIT;
  1602. }
  1603. slot_count++;
  1604. spin_lock.unlock();
  1605. return ObjectID(id);
  1606. }
  1607. void ObjectDB::remove_instance(Object *p_object) {
  1608. uint64_t t = p_object->get_instance_id();
  1609. uint32_t slot = t & OBJECTDB_SLOT_MAX_COUNT_MASK; //slot is always valid on valid object
  1610. spin_lock.lock();
  1611. #ifdef DEBUG_ENABLED
  1612. if (object_slots[slot].object != p_object) {
  1613. spin_lock.unlock();
  1614. ERR_FAIL_COND(object_slots[slot].object != p_object);
  1615. }
  1616. {
  1617. uint64_t validator = (t >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
  1618. if (object_slots[slot].validator != validator) {
  1619. spin_lock.unlock();
  1620. ERR_FAIL_COND(object_slots[slot].validator != validator);
  1621. }
  1622. }
  1623. #endif
  1624. //decrease slot count
  1625. slot_count--;
  1626. //set the free slot properly
  1627. object_slots[slot_count].next_free = slot;
  1628. //invalidate, so checks against it fail
  1629. object_slots[slot].validator = 0;
  1630. object_slots[slot].is_ref_counted = false;
  1631. object_slots[slot].object = nullptr;
  1632. spin_lock.unlock();
  1633. }
  1634. void ObjectDB::setup() {
  1635. //nothing to do now
  1636. }
  1637. void ObjectDB::cleanup() {
  1638. if (slot_count > 0) {
  1639. spin_lock.lock();
  1640. WARN_PRINT("ObjectDB instances leaked at exit (run with --verbose for details).");
  1641. if (OS::get_singleton()->is_stdout_verbose()) {
  1642. // Ensure calling the native classes because if a leaked instance has a script
  1643. // that overrides any of those methods, it'd not be OK to call them at this point,
  1644. // now the scripting languages have already been terminated.
  1645. MethodBind *node_get_name = ClassDB::get_method("Node", "get_name");
  1646. MethodBind *resource_get_path = ClassDB::get_method("Resource", "get_path");
  1647. Callable::CallError call_error;
  1648. for (uint32_t i = 0, count = slot_count; i < slot_max && count != 0; i++) {
  1649. if (object_slots[i].validator) {
  1650. Object *obj = object_slots[i].object;
  1651. String extra_info;
  1652. if (obj->is_class("Node")) {
  1653. extra_info = " - Node name: " + String(node_get_name->call(obj, nullptr, 0, call_error));
  1654. }
  1655. if (obj->is_class("Resource")) {
  1656. extra_info = " - Resource path: " + String(resource_get_path->call(obj, nullptr, 0, call_error));
  1657. }
  1658. 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);
  1659. print_line("Leaked instance: " + String(obj->get_class()) + ":" + itos(id) + extra_info);
  1660. count--;
  1661. }
  1662. }
  1663. 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()`).");
  1664. }
  1665. spin_lock.unlock();
  1666. }
  1667. if (object_slots) {
  1668. memfree(object_slots);
  1669. }
  1670. }