object.cpp 57 KB

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