object.cpp 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329
  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 "object.compat.inc"
  32. #include "core/extension/gdextension_manager.h"
  33. #include "core/io/resource.h"
  34. #include "core/object/class_db.h"
  35. #include "core/object/message_queue.h"
  36. #include "core/object/script_language.h"
  37. #include "core/os/os.h"
  38. #include "core/string/print_string.h"
  39. #include "core/string/translation_server.h"
  40. #include "core/templates/local_vector.h"
  41. #include "core/variant/typed_array.h"
  42. #ifdef DEBUG_ENABLED
  43. struct _ObjectDebugLock {
  44. Object *obj;
  45. _ObjectDebugLock(Object *p_obj) {
  46. obj = p_obj;
  47. obj->_lock_index.ref();
  48. }
  49. ~_ObjectDebugLock() {
  50. obj->_lock_index.unref();
  51. }
  52. };
  53. #define OBJ_DEBUG_LOCK _ObjectDebugLock _debug_lock(this);
  54. #else
  55. #define OBJ_DEBUG_LOCK
  56. #endif
  57. PropertyInfo::operator Dictionary() const {
  58. Dictionary d;
  59. d["name"] = name;
  60. d["class_name"] = class_name;
  61. d["type"] = type;
  62. d["hint"] = hint;
  63. d["hint_string"] = hint_string;
  64. d["usage"] = usage;
  65. return d;
  66. }
  67. PropertyInfo PropertyInfo::from_dict(const Dictionary &p_dict) {
  68. PropertyInfo pi;
  69. if (p_dict.has("type")) {
  70. pi.type = Variant::Type(int(p_dict["type"]));
  71. }
  72. if (p_dict.has("name")) {
  73. pi.name = p_dict["name"];
  74. }
  75. if (p_dict.has("class_name")) {
  76. pi.class_name = p_dict["class_name"];
  77. }
  78. if (p_dict.has("hint")) {
  79. pi.hint = PropertyHint(int(p_dict["hint"]));
  80. }
  81. if (p_dict.has("hint_string")) {
  82. pi.hint_string = p_dict["hint_string"];
  83. }
  84. if (p_dict.has("usage")) {
  85. pi.usage = p_dict["usage"];
  86. }
  87. return pi;
  88. }
  89. TypedArray<Dictionary> convert_property_list(const List<PropertyInfo> *p_list) {
  90. TypedArray<Dictionary> va;
  91. for (const List<PropertyInfo>::Element *E = p_list->front(); E; E = E->next()) {
  92. va.push_back(Dictionary(E->get()));
  93. }
  94. return va;
  95. }
  96. MethodInfo::operator Dictionary() const {
  97. Dictionary d;
  98. d["name"] = name;
  99. d["args"] = convert_property_list(&arguments);
  100. Array da;
  101. for (int i = 0; i < default_arguments.size(); i++) {
  102. da.push_back(default_arguments[i]);
  103. }
  104. d["default_args"] = da;
  105. d["flags"] = flags;
  106. d["id"] = id;
  107. Dictionary r = return_val;
  108. d["return"] = r;
  109. return d;
  110. }
  111. MethodInfo MethodInfo::from_dict(const Dictionary &p_dict) {
  112. MethodInfo mi;
  113. if (p_dict.has("name")) {
  114. mi.name = p_dict["name"];
  115. }
  116. Array args;
  117. if (p_dict.has("args")) {
  118. args = p_dict["args"];
  119. }
  120. for (const Variant &arg : args) {
  121. Dictionary d = arg;
  122. mi.arguments.push_back(PropertyInfo::from_dict(d));
  123. }
  124. Array defargs;
  125. if (p_dict.has("default_args")) {
  126. defargs = p_dict["default_args"];
  127. }
  128. for (const Variant &defarg : defargs) {
  129. mi.default_arguments.push_back(defarg);
  130. }
  131. if (p_dict.has("return")) {
  132. mi.return_val = PropertyInfo::from_dict(p_dict["return"]);
  133. }
  134. if (p_dict.has("flags")) {
  135. mi.flags = p_dict["flags"];
  136. }
  137. return mi;
  138. }
  139. Object::Connection::operator Variant() const {
  140. Dictionary d;
  141. d["signal"] = signal;
  142. d["callable"] = callable;
  143. d["flags"] = flags;
  144. return d;
  145. }
  146. bool Object::Connection::operator<(const Connection &p_conn) const {
  147. if (signal == p_conn.signal) {
  148. return callable < p_conn.callable;
  149. } else {
  150. return signal < p_conn.signal;
  151. }
  152. }
  153. Object::Connection::Connection(const Variant &p_variant) {
  154. Dictionary d = p_variant;
  155. if (d.has("signal")) {
  156. signal = d["signal"];
  157. }
  158. if (d.has("callable")) {
  159. callable = d["callable"];
  160. }
  161. if (d.has("flags")) {
  162. flags = d["flags"];
  163. }
  164. }
  165. bool Object::_predelete() {
  166. _predelete_ok = 1;
  167. notification(NOTIFICATION_PREDELETE, true);
  168. if (_predelete_ok) {
  169. _class_name_ptr = nullptr; // Must restore, so constructors/destructors have proper class name access at each stage.
  170. notification(NOTIFICATION_PREDELETE_CLEANUP, true);
  171. }
  172. return _predelete_ok;
  173. }
  174. void Object::cancel_free() {
  175. _predelete_ok = false;
  176. }
  177. void Object::_initialize() {
  178. _class_name_ptr = _get_class_namev(); // Set the direct pointer, which is much faster to obtain, but can only happen after _initialize.
  179. _initialize_classv();
  180. _class_name_ptr = nullptr; // May have been called from a constructor.
  181. }
  182. void Object::_postinitialize() {
  183. notification(NOTIFICATION_POSTINITIALIZE);
  184. }
  185. void Object::get_valid_parents_static(List<String> *p_parents) {
  186. }
  187. void Object::_get_valid_parents_static(List<String> *p_parents) {
  188. }
  189. void Object::set(const StringName &p_name, const Variant &p_value, bool *r_valid) {
  190. #ifdef TOOLS_ENABLED
  191. _edited = true;
  192. #endif
  193. if (script_instance) {
  194. if (script_instance->set(p_name, p_value)) {
  195. if (r_valid) {
  196. *r_valid = true;
  197. }
  198. return;
  199. }
  200. }
  201. if (_extension && _extension->set) {
  202. if (_extension->set(_extension_instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionConstVariantPtr)&p_value)) {
  203. if (r_valid) {
  204. *r_valid = true;
  205. }
  206. return;
  207. }
  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 == CoreStringName(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. if (_extension->get(_extension_instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&ret)) {
  274. if (r_valid) {
  275. *r_valid = true;
  276. }
  277. return ret;
  278. }
  279. }
  280. // Try built-in getter.
  281. {
  282. if (ClassDB::get_property(const_cast<Object *>(this), p_name, ret)) {
  283. if (r_valid) {
  284. *r_valid = true;
  285. }
  286. return ret;
  287. }
  288. }
  289. if (p_name == CoreStringName(script)) {
  290. ret = get_script();
  291. if (r_valid) {
  292. *r_valid = true;
  293. }
  294. return ret;
  295. }
  296. const Variant *const *V = metadata_properties.getptr(p_name);
  297. if (V) {
  298. ret = **V;
  299. if (r_valid) {
  300. *r_valid = true;
  301. }
  302. return ret;
  303. } else {
  304. #ifdef TOOLS_ENABLED
  305. if (script_instance) {
  306. bool valid;
  307. ret = script_instance->property_get_fallback(p_name, &valid);
  308. if (valid) {
  309. if (r_valid) {
  310. *r_valid = true;
  311. }
  312. return ret;
  313. }
  314. }
  315. #endif
  316. // Something inside the object... :|
  317. bool success = _getv(p_name, ret);
  318. if (success) {
  319. if (r_valid) {
  320. *r_valid = true;
  321. }
  322. return ret;
  323. }
  324. if (r_valid) {
  325. *r_valid = false;
  326. }
  327. return Variant();
  328. }
  329. }
  330. void Object::set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid) {
  331. if (p_names.is_empty()) {
  332. if (r_valid) {
  333. *r_valid = false;
  334. }
  335. return;
  336. }
  337. if (p_names.size() == 1) {
  338. set(p_names[0], p_value, r_valid);
  339. return;
  340. }
  341. bool valid = false;
  342. if (!r_valid) {
  343. r_valid = &valid;
  344. }
  345. List<Variant> value_stack;
  346. value_stack.push_back(get(p_names[0], r_valid));
  347. if (!*r_valid) {
  348. value_stack.clear();
  349. return;
  350. }
  351. for (int i = 1; i < p_names.size() - 1; i++) {
  352. value_stack.push_back(value_stack.back()->get().get_named(p_names[i], valid));
  353. if (r_valid) {
  354. *r_valid = valid;
  355. }
  356. if (!valid) {
  357. value_stack.clear();
  358. return;
  359. }
  360. }
  361. value_stack.push_back(p_value); // p_names[p_names.size() - 1]
  362. for (int i = p_names.size() - 1; i > 0; i--) {
  363. value_stack.back()->prev()->get().set_named(p_names[i], value_stack.back()->get(), valid);
  364. value_stack.pop_back();
  365. if (r_valid) {
  366. *r_valid = valid;
  367. }
  368. if (!valid) {
  369. value_stack.clear();
  370. return;
  371. }
  372. }
  373. set(p_names[0], value_stack.back()->get(), r_valid);
  374. value_stack.pop_back();
  375. ERR_FAIL_COND(!value_stack.is_empty());
  376. }
  377. Variant Object::get_indexed(const Vector<StringName> &p_names, bool *r_valid) const {
  378. if (p_names.is_empty()) {
  379. if (r_valid) {
  380. *r_valid = false;
  381. }
  382. return Variant();
  383. }
  384. bool valid = false;
  385. Variant current_value = get(p_names[0], &valid);
  386. for (int i = 1; i < p_names.size(); i++) {
  387. current_value = current_value.get_named(p_names[i], valid);
  388. if (!valid) {
  389. break;
  390. }
  391. }
  392. if (r_valid) {
  393. *r_valid = valid;
  394. }
  395. return current_value;
  396. }
  397. void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) const {
  398. if (script_instance && p_reversed) {
  399. script_instance->get_property_list(p_list);
  400. }
  401. if (_extension) {
  402. const ObjectGDExtension *current_extension = _extension;
  403. while (current_extension) {
  404. p_list->push_back(PropertyInfo(Variant::NIL, current_extension->class_name, PROPERTY_HINT_NONE, current_extension->class_name, PROPERTY_USAGE_CATEGORY));
  405. ClassDB::get_property_list(current_extension->class_name, p_list, true, this);
  406. if (current_extension->get_property_list) {
  407. #ifdef TOOLS_ENABLED
  408. // If this is a placeholder, we can't call into the GDExtension on the parent class,
  409. // because we don't have a real instance of the class to give it.
  410. if (likely(!_extension->is_placeholder)) {
  411. #endif
  412. uint32_t pcount;
  413. const GDExtensionPropertyInfo *pinfo = current_extension->get_property_list(_extension_instance, &pcount);
  414. for (uint32_t i = 0; i < pcount; i++) {
  415. p_list->push_back(PropertyInfo(pinfo[i]));
  416. }
  417. if (current_extension->free_property_list2) {
  418. current_extension->free_property_list2(_extension_instance, pinfo, pcount);
  419. }
  420. #ifndef DISABLE_DEPRECATED
  421. else if (current_extension->free_property_list) {
  422. current_extension->free_property_list(_extension_instance, pinfo);
  423. }
  424. #endif // DISABLE_DEPRECATED
  425. #ifdef TOOLS_ENABLED
  426. }
  427. #endif
  428. }
  429. current_extension = current_extension->parent;
  430. }
  431. }
  432. _get_property_listv(p_list, p_reversed);
  433. if (!is_class("Script")) { // can still be set, but this is for user-friendliness
  434. p_list->push_back(PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NEVER_DUPLICATE));
  435. }
  436. if (script_instance && !p_reversed) {
  437. script_instance->get_property_list(p_list);
  438. }
  439. for (const KeyValue<StringName, Variant> &K : metadata) {
  440. PropertyInfo pi = PropertyInfo(K.value.get_type(), "metadata/" + K.key.operator String());
  441. if (K.value.get_type() == Variant::OBJECT) {
  442. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  443. pi.hint_string = "Resource";
  444. }
  445. p_list->push_back(pi);
  446. }
  447. }
  448. void Object::validate_property(PropertyInfo &p_property) const {
  449. _validate_propertyv(p_property);
  450. if (_extension && _extension->validate_property) {
  451. // GDExtension uses a StringName rather than a String for property name.
  452. StringName prop_name = p_property.name;
  453. GDExtensionPropertyInfo gdext_prop = {
  454. (GDExtensionVariantType)p_property.type,
  455. &prop_name,
  456. &p_property.class_name,
  457. (uint32_t)p_property.hint,
  458. &p_property.hint_string,
  459. p_property.usage,
  460. };
  461. if (_extension->validate_property(_extension_instance, &gdext_prop)) {
  462. p_property.type = (Variant::Type)gdext_prop.type;
  463. p_property.name = *reinterpret_cast<StringName *>(gdext_prop.name);
  464. p_property.class_name = *reinterpret_cast<StringName *>(gdext_prop.class_name);
  465. p_property.hint = (PropertyHint)gdext_prop.hint;
  466. p_property.hint_string = *reinterpret_cast<String *>(gdext_prop.hint_string);
  467. p_property.usage = gdext_prop.usage;
  468. };
  469. }
  470. if (script_instance) { // Call it last to allow user altering already validated properties.
  471. script_instance->validate_property(p_property);
  472. }
  473. }
  474. bool Object::property_can_revert(const StringName &p_name) const {
  475. if (script_instance) {
  476. if (script_instance->property_can_revert(p_name)) {
  477. return true;
  478. }
  479. }
  480. if (_extension && _extension->property_can_revert) {
  481. if (_extension->property_can_revert(_extension_instance, (GDExtensionConstStringNamePtr)&p_name)) {
  482. return true;
  483. }
  484. }
  485. return _property_can_revertv(p_name);
  486. }
  487. Variant Object::property_get_revert(const StringName &p_name) const {
  488. Variant ret;
  489. if (script_instance) {
  490. if (script_instance->property_get_revert(p_name, ret)) {
  491. return ret;
  492. }
  493. }
  494. if (_extension && _extension->property_get_revert) {
  495. if (_extension->property_get_revert(_extension_instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&ret)) {
  496. return ret;
  497. }
  498. }
  499. if (_property_get_revertv(p_name, ret)) {
  500. return ret;
  501. }
  502. return Variant();
  503. }
  504. void Object::get_method_list(List<MethodInfo> *p_list) const {
  505. ClassDB::get_method_list(get_class_name(), p_list);
  506. if (script_instance) {
  507. script_instance->get_method_list(p_list);
  508. }
  509. }
  510. Variant Object::_call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  511. if (p_argcount < 1) {
  512. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  513. r_error.expected = 1;
  514. return Variant();
  515. }
  516. if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
  517. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  518. r_error.argument = 0;
  519. r_error.expected = Variant::STRING_NAME;
  520. return Variant();
  521. }
  522. StringName method = *p_args[0];
  523. return callp(method, &p_args[1], p_argcount - 1, r_error);
  524. }
  525. Variant Object::_call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  526. if (p_argcount < 1) {
  527. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  528. r_error.expected = 1;
  529. return Variant();
  530. }
  531. if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
  532. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  533. r_error.argument = 0;
  534. r_error.expected = Variant::STRING_NAME;
  535. return Variant();
  536. }
  537. r_error.error = Callable::CallError::CALL_OK;
  538. StringName method = *p_args[0];
  539. MessageQueue::get_singleton()->push_callp(get_instance_id(), method, &p_args[1], p_argcount - 1, true);
  540. return Variant();
  541. }
  542. bool Object::has_method(const StringName &p_method) const {
  543. if (p_method == CoreStringName(free_)) {
  544. return true;
  545. }
  546. if (script_instance && script_instance->has_method(p_method)) {
  547. return true;
  548. }
  549. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  550. if (method != nullptr) {
  551. return true;
  552. }
  553. const Script *scr = Object::cast_to<Script>(this);
  554. if (scr != nullptr) {
  555. return scr->has_static_method(p_method);
  556. }
  557. return false;
  558. }
  559. int Object::_get_method_argument_count_bind(const StringName &p_method) const {
  560. return get_method_argument_count(p_method);
  561. }
  562. int Object::get_method_argument_count(const StringName &p_method, bool *r_is_valid) const {
  563. if (p_method == CoreStringName(free_)) {
  564. if (r_is_valid) {
  565. *r_is_valid = true;
  566. }
  567. return 0;
  568. }
  569. if (script_instance) {
  570. bool valid = false;
  571. int ret = script_instance->get_method_argument_count(p_method, &valid);
  572. if (valid) {
  573. if (r_is_valid) {
  574. *r_is_valid = true;
  575. }
  576. return ret;
  577. }
  578. }
  579. {
  580. bool valid = false;
  581. int ret = ClassDB::get_method_argument_count(get_class_name(), p_method, &valid);
  582. if (valid) {
  583. if (r_is_valid) {
  584. *r_is_valid = true;
  585. }
  586. return ret;
  587. }
  588. }
  589. const Script *scr = Object::cast_to<Script>(this);
  590. while (scr != nullptr) {
  591. bool valid = false;
  592. int ret = scr->get_script_method_argument_count(p_method, &valid);
  593. if (valid) {
  594. if (r_is_valid) {
  595. *r_is_valid = true;
  596. }
  597. return ret;
  598. }
  599. scr = scr->get_base_script().ptr();
  600. }
  601. if (r_is_valid) {
  602. *r_is_valid = false;
  603. }
  604. return 0;
  605. }
  606. Variant Object::getvar(const Variant &p_key, bool *r_valid) const {
  607. if (r_valid) {
  608. *r_valid = false;
  609. }
  610. if (p_key.get_type() == Variant::STRING_NAME || p_key.get_type() == Variant::STRING) {
  611. return get(p_key, r_valid);
  612. }
  613. return Variant();
  614. }
  615. void Object::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) {
  616. if (r_valid) {
  617. *r_valid = false;
  618. }
  619. if (p_key.get_type() == Variant::STRING_NAME || p_key.get_type() == Variant::STRING) {
  620. return set(p_key, p_value, r_valid);
  621. }
  622. }
  623. Variant Object::callv(const StringName &p_method, const Array &p_args) {
  624. const Variant **argptrs = nullptr;
  625. if (p_args.size() > 0) {
  626. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_args.size());
  627. for (int i = 0; i < p_args.size(); i++) {
  628. argptrs[i] = &p_args[i];
  629. }
  630. }
  631. Callable::CallError ce;
  632. Variant ret = callp(p_method, argptrs, p_args.size(), ce);
  633. if (ce.error != Callable::CallError::CALL_OK) {
  634. ERR_FAIL_V_MSG(Variant(), "Error calling method from 'callv': " + Variant::get_call_error_text(this, p_method, argptrs, p_args.size(), ce) + ".");
  635. }
  636. return ret;
  637. }
  638. Variant Object::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  639. r_error.error = Callable::CallError::CALL_OK;
  640. if (p_method == CoreStringName(free_)) {
  641. //free must be here, before anything, always ready
  642. #ifdef DEBUG_ENABLED
  643. if (p_argcount != 0) {
  644. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  645. r_error.expected = 0;
  646. return Variant();
  647. }
  648. if (is_ref_counted()) {
  649. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  650. ERR_FAIL_V_MSG(Variant(), "Can't free a RefCounted object.");
  651. }
  652. if (_lock_index.get() > 1) {
  653. r_error.argument = 0;
  654. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  655. ERR_FAIL_V_MSG(Variant(), "Object is locked and can't be freed.");
  656. }
  657. #endif
  658. //must be here, must be before everything,
  659. memdelete(this);
  660. r_error.error = Callable::CallError::CALL_OK;
  661. return Variant();
  662. }
  663. Variant ret;
  664. OBJ_DEBUG_LOCK
  665. if (script_instance) {
  666. ret = script_instance->callp(p_method, p_args, p_argcount, r_error);
  667. //force jumptable
  668. switch (r_error.error) {
  669. case Callable::CallError::CALL_OK:
  670. return ret;
  671. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  672. break;
  673. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  674. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  675. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  676. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  677. return ret;
  678. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL: {
  679. }
  680. }
  681. }
  682. //extension does not need this, because all methods are registered in MethodBind
  683. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  684. if (method) {
  685. ret = method->call(this, p_args, p_argcount, r_error);
  686. } else {
  687. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  688. }
  689. return ret;
  690. }
  691. Variant Object::call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  692. r_error.error = Callable::CallError::CALL_OK;
  693. if (p_method == CoreStringName(free_)) {
  694. // Free is not const, so fail.
  695. r_error.error = Callable::CallError::CALL_ERROR_METHOD_NOT_CONST;
  696. return Variant();
  697. }
  698. Variant ret;
  699. OBJ_DEBUG_LOCK
  700. if (script_instance) {
  701. ret = script_instance->call_const(p_method, p_args, p_argcount, r_error);
  702. //force jumptable
  703. switch (r_error.error) {
  704. case Callable::CallError::CALL_OK:
  705. return ret;
  706. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  707. break;
  708. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  709. break;
  710. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  711. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  712. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  713. return ret;
  714. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL: {
  715. }
  716. }
  717. }
  718. //extension does not need this, because all methods are registered in MethodBind
  719. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  720. if (method) {
  721. if (!method->is_const()) {
  722. r_error.error = Callable::CallError::CALL_ERROR_METHOD_NOT_CONST;
  723. return ret;
  724. }
  725. ret = method->call(this, p_args, p_argcount, r_error);
  726. } else {
  727. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  728. }
  729. return ret;
  730. }
  731. void Object::notification(int p_notification, bool p_reversed) {
  732. if (p_reversed) {
  733. if (script_instance) {
  734. script_instance->notification(p_notification, p_reversed);
  735. }
  736. } else {
  737. _notificationv(p_notification, p_reversed);
  738. }
  739. if (_extension) {
  740. if (_extension->notification2) {
  741. _extension->notification2(_extension_instance, p_notification, static_cast<GDExtensionBool>(p_reversed));
  742. #ifndef DISABLE_DEPRECATED
  743. } else if (_extension->notification) {
  744. _extension->notification(_extension_instance, p_notification);
  745. #endif // DISABLE_DEPRECATED
  746. }
  747. }
  748. if (p_reversed) {
  749. _notificationv(p_notification, p_reversed);
  750. } else {
  751. if (script_instance) {
  752. script_instance->notification(p_notification, p_reversed);
  753. }
  754. }
  755. }
  756. String Object::to_string() {
  757. // Keep this method in sync with `Node::to_string`.
  758. if (script_instance) {
  759. bool valid;
  760. String ret = script_instance->to_string(&valid);
  761. if (valid) {
  762. return ret;
  763. }
  764. }
  765. if (_extension && _extension->to_string) {
  766. String ret;
  767. GDExtensionBool is_valid;
  768. _extension->to_string(_extension_instance, &is_valid, &ret);
  769. return ret;
  770. }
  771. return "<" + get_class() + "#" + itos(get_instance_id()) + ">";
  772. }
  773. void Object::set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance) {
  774. //this function is not meant to be used in any of these ways
  775. ERR_FAIL_COND(p_script.is_null());
  776. ERR_FAIL_NULL(p_instance);
  777. ERR_FAIL_COND(script_instance != nullptr || !script.is_null());
  778. script = p_script;
  779. script_instance = p_instance;
  780. }
  781. void Object::set_script(const Variant &p_script) {
  782. if (script == p_script) {
  783. return;
  784. }
  785. Ref<Script> s = p_script;
  786. if (!p_script.is_null()) {
  787. ERR_FAIL_COND_MSG(s.is_null(), "Cannot set object script. Parameter should be null or a reference to a valid script.");
  788. ERR_FAIL_COND_MSG(s->is_abstract(), vformat("Cannot set object script. Script '%s' should not be abstract.", s->get_path()));
  789. }
  790. script = p_script;
  791. if (script_instance) {
  792. memdelete(script_instance);
  793. script_instance = nullptr;
  794. }
  795. if (!s.is_null()) {
  796. if (s->can_instantiate()) {
  797. OBJ_DEBUG_LOCK
  798. script_instance = s->instance_create(this);
  799. } else if (Engine::get_singleton()->is_editor_hint()) {
  800. OBJ_DEBUG_LOCK
  801. script_instance = s->placeholder_instance_create(this);
  802. }
  803. }
  804. notify_property_list_changed(); //scripts may add variables, so refresh is desired
  805. emit_signal(CoreStringName(script_changed));
  806. }
  807. void Object::set_script_instance(ScriptInstance *p_instance) {
  808. if (script_instance == p_instance) {
  809. return;
  810. }
  811. if (script_instance) {
  812. memdelete(script_instance);
  813. }
  814. script_instance = p_instance;
  815. if (p_instance) {
  816. script = p_instance->get_script();
  817. } else {
  818. script = Variant();
  819. }
  820. }
  821. Variant Object::get_script() const {
  822. return script;
  823. }
  824. bool Object::has_meta(const StringName &p_name) const {
  825. return metadata.has(p_name);
  826. }
  827. void Object::set_meta(const StringName &p_name, const Variant &p_value) {
  828. if (p_value.get_type() == Variant::NIL) {
  829. if (metadata.has(p_name)) {
  830. metadata.erase(p_name);
  831. const String &sname = p_name;
  832. metadata_properties.erase("metadata/" + sname);
  833. if (!sname.begins_with("_")) {
  834. // Metadata starting with _ don't show up in the inspector, so no need to update.
  835. notify_property_list_changed();
  836. }
  837. }
  838. return;
  839. }
  840. HashMap<StringName, Variant>::Iterator E = metadata.find(p_name);
  841. if (E) {
  842. E->value = p_value;
  843. } else {
  844. ERR_FAIL_COND_MSG(!p_name.operator String().is_valid_identifier(), "Invalid metadata identifier: '" + p_name + "'.");
  845. Variant *V = &metadata.insert(p_name, p_value)->value;
  846. const String &sname = p_name;
  847. metadata_properties["metadata/" + sname] = V;
  848. if (!sname.begins_with("_")) {
  849. notify_property_list_changed();
  850. }
  851. }
  852. }
  853. Variant Object::get_meta(const StringName &p_name, const Variant &p_default) const {
  854. if (!metadata.has(p_name)) {
  855. if (p_default != Variant()) {
  856. return p_default;
  857. } else {
  858. ERR_FAIL_V_MSG(Variant(), "The object does not have any 'meta' values with the key '" + p_name + "'.");
  859. }
  860. }
  861. return metadata[p_name];
  862. }
  863. void Object::remove_meta(const StringName &p_name) {
  864. set_meta(p_name, Variant());
  865. }
  866. TypedArray<Dictionary> Object::_get_property_list_bind() const {
  867. List<PropertyInfo> lpi;
  868. get_property_list(&lpi);
  869. return convert_property_list(&lpi);
  870. }
  871. TypedArray<Dictionary> Object::_get_method_list_bind() const {
  872. List<MethodInfo> ml;
  873. get_method_list(&ml);
  874. TypedArray<Dictionary> ret;
  875. for (List<MethodInfo>::Element *E = ml.front(); E; E = E->next()) {
  876. Dictionary d = E->get();
  877. //va.push_back(d);
  878. ret.push_back(d);
  879. }
  880. return ret;
  881. }
  882. TypedArray<StringName> Object::_get_meta_list_bind() const {
  883. TypedArray<StringName> _metaret;
  884. for (const KeyValue<StringName, Variant> &K : metadata) {
  885. _metaret.push_back(K.key);
  886. }
  887. return _metaret;
  888. }
  889. void Object::get_meta_list(List<StringName> *p_list) const {
  890. for (const KeyValue<StringName, Variant> &K : metadata) {
  891. p_list->push_back(K.key);
  892. }
  893. }
  894. void Object::add_user_signal(const MethodInfo &p_signal) {
  895. ERR_FAIL_COND_MSG(p_signal.name.is_empty(), "Signal name cannot be empty.");
  896. 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() + "'.");
  897. ERR_FAIL_COND_MSG(signal_map.has(p_signal.name), "Trying to add already existing signal '" + p_signal.name + "'.");
  898. SignalData s;
  899. s.user = p_signal;
  900. signal_map[p_signal.name] = s;
  901. }
  902. bool Object::_has_user_signal(const StringName &p_name) const {
  903. if (!signal_map.has(p_name)) {
  904. return false;
  905. }
  906. return signal_map[p_name].user.name.length() > 0;
  907. }
  908. void Object::_remove_user_signal(const StringName &p_name) {
  909. SignalData *s = signal_map.getptr(p_name);
  910. ERR_FAIL_NULL_MSG(s, "Provided signal does not exist.");
  911. ERR_FAIL_COND_MSG(!s->removable, "Signal is not removable (not added with add_user_signal).");
  912. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  913. Object *target = slot_kv.key.get_object();
  914. if (likely(target)) {
  915. target->connections.erase(slot_kv.value.cE);
  916. }
  917. }
  918. signal_map.erase(p_name);
  919. }
  920. Error Object::_emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  921. if (unlikely(p_argcount < 1)) {
  922. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  923. r_error.expected = 1;
  924. ERR_FAIL_V(Error::ERR_INVALID_PARAMETER);
  925. }
  926. if (unlikely(p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING)) {
  927. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  928. r_error.argument = 0;
  929. r_error.expected = Variant::STRING_NAME;
  930. ERR_FAIL_V(Error::ERR_INVALID_PARAMETER);
  931. }
  932. r_error.error = Callable::CallError::CALL_OK;
  933. StringName signal = *p_args[0];
  934. const Variant **args = nullptr;
  935. int argc = p_argcount - 1;
  936. if (argc) {
  937. args = &p_args[1];
  938. }
  939. return emit_signalp(signal, args, argc);
  940. }
  941. Error Object::emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount) {
  942. if (_block_signals) {
  943. return ERR_CANT_ACQUIRE_RESOURCE; //no emit, signals blocked
  944. }
  945. SignalData *s = signal_map.getptr(p_name);
  946. if (!s) {
  947. #ifdef DEBUG_ENABLED
  948. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_name);
  949. //check in script
  950. 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 + "\".");
  951. #endif
  952. //not connected? just return
  953. return ERR_UNAVAILABLE;
  954. }
  955. // If this is a ref-counted object, prevent it from being destroyed during signal emission,
  956. // which is needed in certain edge cases; e.g., https://github.com/godotengine/godot/issues/73889.
  957. Ref<RefCounted> rc = Ref<RefCounted>(Object::cast_to<RefCounted>(this));
  958. // Ensure that disconnecting the signal or even deleting the object
  959. // will not affect the signal calling.
  960. Callable *slot_callables = (Callable *)alloca(sizeof(Callable) * s->slot_map.size());
  961. uint32_t *slot_flags = (uint32_t *)alloca(sizeof(uint32_t) * s->slot_map.size());
  962. uint32_t slot_count = 0;
  963. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  964. memnew_placement(&slot_callables[slot_count], Callable(slot_kv.value.conn.callable));
  965. slot_flags[slot_count] = slot_kv.value.conn.flags;
  966. ++slot_count;
  967. }
  968. DEV_ASSERT(slot_count == s->slot_map.size());
  969. // Disconnect all one-shot connections before emitting to prevent recursion.
  970. for (uint32_t i = 0; i < slot_count; ++i) {
  971. bool disconnect = slot_flags[i] & CONNECT_ONE_SHOT;
  972. #ifdef TOOLS_ENABLED
  973. if (disconnect && (slot_flags[i] & CONNECT_PERSIST) && Engine::get_singleton()->is_editor_hint()) {
  974. // This signal was connected from the editor, and is being edited. Just don't disconnect for now.
  975. disconnect = false;
  976. }
  977. #endif
  978. if (disconnect) {
  979. _disconnect(p_name, slot_callables[i]);
  980. }
  981. }
  982. OBJ_DEBUG_LOCK
  983. Error err = OK;
  984. for (uint32_t i = 0; i < slot_count; ++i) {
  985. const Callable &callable = slot_callables[i];
  986. const uint32_t &flags = slot_flags[i];
  987. if (!callable.is_valid()) {
  988. // Target might have been deleted during signal callback, this is expected and OK.
  989. continue;
  990. }
  991. const Variant **args = p_args;
  992. int argc = p_argcount;
  993. if (flags & CONNECT_DEFERRED) {
  994. MessageQueue::get_singleton()->push_callablep(callable, args, argc, true);
  995. } else {
  996. Callable::CallError ce;
  997. _emitting = true;
  998. Variant ret;
  999. callable.callp(args, argc, ret, ce);
  1000. _emitting = false;
  1001. if (ce.error != Callable::CallError::CALL_OK) {
  1002. #ifdef DEBUG_ENABLED
  1003. if (flags & CONNECT_PERSIST && Engine::get_singleton()->is_editor_hint() && (script.is_null() || !Ref<Script>(script)->is_tool())) {
  1004. continue;
  1005. }
  1006. #endif
  1007. Object *target = callable.get_object();
  1008. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD && target && !ClassDB::class_exists(target->get_class_name())) {
  1009. //most likely object is not initialized yet, do not throw error.
  1010. } else {
  1011. ERR_PRINT("Error calling from signal '" + String(p_name) + "' to callable: " + Variant::get_callable_error_text(callable, args, argc, ce) + ".");
  1012. err = ERR_METHOD_NOT_FOUND;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. for (uint32_t i = 0; i < slot_count; ++i) {
  1018. slot_callables[i].~Callable();
  1019. }
  1020. return err;
  1021. }
  1022. void Object::_add_user_signal(const String &p_name, const Array &p_args) {
  1023. // this version of add_user_signal is meant to be used from scripts or external apis
  1024. // without access to ADD_SIGNAL in bind_methods
  1025. // added events are per instance, as opposed to the other ones, which are global
  1026. MethodInfo mi;
  1027. mi.name = p_name;
  1028. for (const Variant &arg : p_args) {
  1029. Dictionary d = arg;
  1030. PropertyInfo param;
  1031. if (d.has("name")) {
  1032. param.name = d["name"];
  1033. }
  1034. if (d.has("type")) {
  1035. param.type = (Variant::Type)(int)d["type"];
  1036. }
  1037. mi.arguments.push_back(param);
  1038. }
  1039. add_user_signal(mi);
  1040. if (signal_map.has(p_name)) {
  1041. signal_map.getptr(p_name)->removable = true;
  1042. }
  1043. }
  1044. TypedArray<Dictionary> Object::_get_signal_list() const {
  1045. List<MethodInfo> signal_list;
  1046. get_signal_list(&signal_list);
  1047. TypedArray<Dictionary> ret;
  1048. for (const MethodInfo &E : signal_list) {
  1049. ret.push_back(Dictionary(E));
  1050. }
  1051. return ret;
  1052. }
  1053. TypedArray<Dictionary> Object::_get_signal_connection_list(const StringName &p_signal) const {
  1054. List<Connection> conns;
  1055. get_all_signal_connections(&conns);
  1056. TypedArray<Dictionary> ret;
  1057. for (const Connection &c : conns) {
  1058. if (c.signal.get_name() == p_signal) {
  1059. ret.push_back(c);
  1060. }
  1061. }
  1062. return ret;
  1063. }
  1064. TypedArray<Dictionary> Object::_get_incoming_connections() const {
  1065. TypedArray<Dictionary> ret;
  1066. for (const Object::Connection &connection : connections) {
  1067. ret.push_back(connection);
  1068. }
  1069. return ret;
  1070. }
  1071. bool Object::has_signal(const StringName &p_name) const {
  1072. if (!script.is_null()) {
  1073. Ref<Script> scr = script;
  1074. if (scr.is_valid() && scr->has_script_signal(p_name)) {
  1075. return true;
  1076. }
  1077. }
  1078. if (ClassDB::has_signal(get_class_name(), p_name)) {
  1079. return true;
  1080. }
  1081. if (_has_user_signal(p_name)) {
  1082. return true;
  1083. }
  1084. return false;
  1085. }
  1086. void Object::get_signal_list(List<MethodInfo> *p_signals) const {
  1087. if (!script.is_null()) {
  1088. Ref<Script> scr = script;
  1089. if (scr.is_valid()) {
  1090. scr->get_script_signal_list(p_signals);
  1091. }
  1092. }
  1093. ClassDB::get_signal_list(get_class_name(), p_signals);
  1094. //find maybe usersignals?
  1095. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  1096. if (!E.value.user.name.is_empty()) {
  1097. //user signal
  1098. p_signals->push_back(E.value.user);
  1099. }
  1100. }
  1101. }
  1102. void Object::get_all_signal_connections(List<Connection> *p_connections) const {
  1103. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  1104. const SignalData *s = &E.value;
  1105. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1106. p_connections->push_back(slot_kv.value.conn);
  1107. }
  1108. }
  1109. }
  1110. void Object::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
  1111. const SignalData *s = signal_map.getptr(p_signal);
  1112. if (!s) {
  1113. return; //nothing
  1114. }
  1115. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1116. p_connections->push_back(slot_kv.value.conn);
  1117. }
  1118. }
  1119. int Object::get_persistent_signal_connection_count() const {
  1120. int count = 0;
  1121. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  1122. const SignalData *s = &E.value;
  1123. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1124. if (slot_kv.value.conn.flags & CONNECT_PERSIST) {
  1125. count += 1;
  1126. }
  1127. }
  1128. }
  1129. return count;
  1130. }
  1131. void Object::get_signals_connected_to_this(List<Connection> *p_connections) const {
  1132. for (const Connection &E : connections) {
  1133. p_connections->push_back(E);
  1134. }
  1135. }
  1136. Error Object::connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags) {
  1137. ERR_FAIL_COND_V_MSG(p_callable.is_null(), ERR_INVALID_PARAMETER, "Cannot connect to '" + p_signal + "': the provided callable is null.");
  1138. if (p_callable.is_standard()) {
  1139. // FIXME: This branch should probably removed in favor of the `is_valid()` branch, but there exist some classes
  1140. // that call `connect()` before they are fully registered with ClassDB. Until all such classes can be found
  1141. // and registered soon enough this branch is needed to allow `connect()` to succeed.
  1142. ERR_FAIL_NULL_V_MSG(p_callable.get_object(), ERR_INVALID_PARAMETER, "Cannot connect to '" + p_signal + "' to callable '" + p_callable + "': the callable object is null.");
  1143. } else {
  1144. ERR_FAIL_COND_V_MSG(!p_callable.is_valid(), ERR_INVALID_PARAMETER, "Cannot connect to '" + p_signal + "': the provided callable is not valid: " + p_callable);
  1145. }
  1146. SignalData *s = signal_map.getptr(p_signal);
  1147. if (!s) {
  1148. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1149. //check in script
  1150. if (!signal_is_valid && !script.is_null()) {
  1151. if (Ref<Script>(script)->has_script_signal(p_signal)) {
  1152. signal_is_valid = true;
  1153. }
  1154. #ifdef TOOLS_ENABLED
  1155. else {
  1156. //allow connecting signals anyway if script is invalid, see issue #17070
  1157. if (!Ref<Script>(script)->is_valid()) {
  1158. signal_is_valid = true;
  1159. }
  1160. }
  1161. #endif
  1162. }
  1163. 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 + "'.");
  1164. signal_map[p_signal] = SignalData();
  1165. s = &signal_map[p_signal];
  1166. }
  1167. //compare with the base callable, so binds can be ignored
  1168. if (s->slot_map.has(*p_callable.get_base_comparator())) {
  1169. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1170. s->slot_map[*p_callable.get_base_comparator()].reference_count++;
  1171. return OK;
  1172. } else {
  1173. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Signal '" + p_signal + "' is already connected to given callable '" + p_callable + "' in that object.");
  1174. }
  1175. }
  1176. Object *target_object = p_callable.get_object();
  1177. SignalData::Slot slot;
  1178. Connection conn;
  1179. conn.callable = p_callable;
  1180. conn.signal = ::Signal(this, p_signal);
  1181. conn.flags = p_flags;
  1182. slot.conn = conn;
  1183. if (target_object) {
  1184. slot.cE = target_object->connections.push_back(conn);
  1185. }
  1186. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1187. slot.reference_count = 1;
  1188. }
  1189. //use callable version as key, so binds can be ignored
  1190. s->slot_map[*p_callable.get_base_comparator()] = slot;
  1191. return OK;
  1192. }
  1193. bool Object::is_connected(const StringName &p_signal, const Callable &p_callable) const {
  1194. ERR_FAIL_COND_V_MSG(p_callable.is_null(), false, "Cannot determine if connected to '" + p_signal + "': the provided callable is null."); // Should use `is_null`, see note in `connect` about the use of `is_valid`.
  1195. const SignalData *s = signal_map.getptr(p_signal);
  1196. if (!s) {
  1197. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1198. if (signal_is_valid) {
  1199. return false;
  1200. }
  1201. if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal)) {
  1202. return false;
  1203. }
  1204. ERR_FAIL_V_MSG(false, "Nonexistent signal: " + p_signal + ".");
  1205. }
  1206. return s->slot_map.has(*p_callable.get_base_comparator());
  1207. }
  1208. void Object::disconnect(const StringName &p_signal, const Callable &p_callable) {
  1209. _disconnect(p_signal, p_callable);
  1210. }
  1211. bool Object::_disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force) {
  1212. ERR_FAIL_COND_V_MSG(p_callable.is_null(), false, "Cannot disconnect from '" + p_signal + "': the provided callable is null."); // Should use `is_null`, see note in `connect` about the use of `is_valid`.
  1213. SignalData *s = signal_map.getptr(p_signal);
  1214. if (!s) {
  1215. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal) ||
  1216. (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal));
  1217. ERR_FAIL_COND_V_MSG(signal_is_valid, false, "Attempt to disconnect a nonexistent connection from '" + to_string() + "'. Signal: '" + p_signal + "', callable: '" + p_callable + "'.");
  1218. }
  1219. ERR_FAIL_NULL_V_MSG(s, false, vformat("Disconnecting nonexistent signal '%s' in %s.", p_signal, to_string()));
  1220. ERR_FAIL_COND_V_MSG(!s->slot_map.has(*p_callable.get_base_comparator()), false, "Attempt to disconnect a nonexistent connection from '" + to_string() + "'. Signal: '" + p_signal + "', callable: '" + p_callable + "'.");
  1221. SignalData::Slot *slot = &s->slot_map[*p_callable.get_base_comparator()];
  1222. if (!p_force) {
  1223. slot->reference_count--; // by default is zero, if it was not referenced it will go below it
  1224. if (slot->reference_count > 0) {
  1225. return false;
  1226. }
  1227. }
  1228. if (slot->cE) {
  1229. Object *target_object = p_callable.get_object();
  1230. if (target_object) {
  1231. target_object->connections.erase(slot->cE);
  1232. }
  1233. }
  1234. s->slot_map.erase(*p_callable.get_base_comparator());
  1235. if (s->slot_map.is_empty() && ClassDB::has_signal(get_class_name(), p_signal)) {
  1236. //not user signal, delete
  1237. signal_map.erase(p_signal);
  1238. }
  1239. return true;
  1240. }
  1241. void Object::_set_bind(const StringName &p_set, const Variant &p_value) {
  1242. set(p_set, p_value);
  1243. }
  1244. Variant Object::_get_bind(const StringName &p_name) const {
  1245. return get(p_name);
  1246. }
  1247. void Object::_set_indexed_bind(const NodePath &p_name, const Variant &p_value) {
  1248. set_indexed(p_name.get_as_property_path().get_subnames(), p_value);
  1249. }
  1250. Variant Object::_get_indexed_bind(const NodePath &p_name) const {
  1251. return get_indexed(p_name.get_as_property_path().get_subnames());
  1252. }
  1253. void Object::initialize_class() {
  1254. static bool initialized = false;
  1255. if (initialized) {
  1256. return;
  1257. }
  1258. ClassDB::_add_class<Object>();
  1259. _bind_methods();
  1260. _bind_compatibility_methods();
  1261. initialized = true;
  1262. }
  1263. String Object::tr(const StringName &p_message, const StringName &p_context) const {
  1264. if (!_can_translate || !TranslationServer::get_singleton()) {
  1265. return p_message;
  1266. }
  1267. if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
  1268. String tr_msg = TranslationServer::get_singleton()->extractable_translate(p_message, p_context);
  1269. if (!tr_msg.is_empty() && tr_msg != p_message) {
  1270. return tr_msg;
  1271. }
  1272. return TranslationServer::get_singleton()->tool_translate(p_message, p_context);
  1273. }
  1274. return TranslationServer::get_singleton()->translate(p_message, p_context);
  1275. }
  1276. String Object::tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  1277. if (!_can_translate || !TranslationServer::get_singleton()) {
  1278. // Return message based on English plural rule if translation is not possible.
  1279. if (p_n == 1) {
  1280. return p_message;
  1281. }
  1282. return p_message_plural;
  1283. }
  1284. if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
  1285. String tr_msg = TranslationServer::get_singleton()->extractable_translate_plural(p_message, p_message_plural, p_n, p_context);
  1286. if (!tr_msg.is_empty() && tr_msg != p_message && tr_msg != p_message_plural) {
  1287. return tr_msg;
  1288. }
  1289. return TranslationServer::get_singleton()->tool_translate_plural(p_message, p_message_plural, p_n, p_context);
  1290. }
  1291. return TranslationServer::get_singleton()->translate_plural(p_message, p_message_plural, p_n, p_context);
  1292. }
  1293. void Object::_clear_internal_resource_paths(const Variant &p_var) {
  1294. switch (p_var.get_type()) {
  1295. case Variant::OBJECT: {
  1296. Ref<Resource> r = p_var;
  1297. if (!r.is_valid()) {
  1298. return;
  1299. }
  1300. if (!r->is_built_in()) {
  1301. return; //not an internal resource
  1302. }
  1303. Object *object = p_var;
  1304. if (!object) {
  1305. return;
  1306. }
  1307. r->set_path("");
  1308. r->clear_internal_resource_paths();
  1309. } break;
  1310. case Variant::ARRAY: {
  1311. Array a = p_var;
  1312. for (const Variant &var : a) {
  1313. _clear_internal_resource_paths(var);
  1314. }
  1315. } break;
  1316. case Variant::DICTIONARY: {
  1317. Dictionary d = p_var;
  1318. List<Variant> keys;
  1319. d.get_key_list(&keys);
  1320. for (const Variant &E : keys) {
  1321. _clear_internal_resource_paths(E);
  1322. _clear_internal_resource_paths(d[E]);
  1323. }
  1324. } break;
  1325. default: {
  1326. }
  1327. }
  1328. }
  1329. #ifdef TOOLS_ENABLED
  1330. void Object::editor_set_section_unfold(const String &p_section, bool p_unfolded) {
  1331. set_edited(true);
  1332. if (p_unfolded) {
  1333. editor_section_folding.insert(p_section);
  1334. } else {
  1335. editor_section_folding.erase(p_section);
  1336. }
  1337. }
  1338. bool Object::editor_is_section_unfolded(const String &p_section) {
  1339. return editor_section_folding.has(p_section);
  1340. }
  1341. #endif
  1342. void Object::clear_internal_resource_paths() {
  1343. List<PropertyInfo> pinfo;
  1344. get_property_list(&pinfo);
  1345. for (const PropertyInfo &E : pinfo) {
  1346. _clear_internal_resource_paths(get(E.name));
  1347. }
  1348. }
  1349. void Object::notify_property_list_changed() {
  1350. emit_signal(CoreStringName(property_list_changed));
  1351. }
  1352. void Object::_bind_methods() {
  1353. ClassDB::bind_method(D_METHOD("get_class"), &Object::get_class);
  1354. ClassDB::bind_method(D_METHOD("is_class", "class"), &Object::is_class);
  1355. ClassDB::bind_method(D_METHOD("set", "property", "value"), &Object::_set_bind);
  1356. ClassDB::bind_method(D_METHOD("get", "property"), &Object::_get_bind);
  1357. ClassDB::bind_method(D_METHOD("set_indexed", "property_path", "value"), &Object::_set_indexed_bind);
  1358. ClassDB::bind_method(D_METHOD("get_indexed", "property_path"), &Object::_get_indexed_bind);
  1359. ClassDB::bind_method(D_METHOD("get_property_list"), &Object::_get_property_list_bind);
  1360. ClassDB::bind_method(D_METHOD("get_method_list"), &Object::_get_method_list_bind);
  1361. ClassDB::bind_method(D_METHOD("property_can_revert", "property"), &Object::property_can_revert);
  1362. ClassDB::bind_method(D_METHOD("property_get_revert", "property"), &Object::property_get_revert);
  1363. ClassDB::bind_method(D_METHOD("notification", "what", "reversed"), &Object::notification, DEFVAL(false));
  1364. ClassDB::bind_method(D_METHOD("to_string"), &Object::to_string);
  1365. ClassDB::bind_method(D_METHOD("get_instance_id"), &Object::get_instance_id);
  1366. ClassDB::bind_method(D_METHOD("set_script", "script"), &Object::set_script);
  1367. ClassDB::bind_method(D_METHOD("get_script"), &Object::get_script);
  1368. ClassDB::bind_method(D_METHOD("set_meta", "name", "value"), &Object::set_meta);
  1369. ClassDB::bind_method(D_METHOD("remove_meta", "name"), &Object::remove_meta);
  1370. ClassDB::bind_method(D_METHOD("get_meta", "name", "default"), &Object::get_meta, DEFVAL(Variant()));
  1371. ClassDB::bind_method(D_METHOD("has_meta", "name"), &Object::has_meta);
  1372. ClassDB::bind_method(D_METHOD("get_meta_list"), &Object::_get_meta_list_bind);
  1373. ClassDB::bind_method(D_METHOD("add_user_signal", "signal", "arguments"), &Object::_add_user_signal, DEFVAL(Array()));
  1374. ClassDB::bind_method(D_METHOD("has_user_signal", "signal"), &Object::_has_user_signal);
  1375. ClassDB::bind_method(D_METHOD("remove_user_signal", "signal"), &Object::_remove_user_signal);
  1376. {
  1377. MethodInfo mi;
  1378. mi.name = "emit_signal";
  1379. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "signal"));
  1380. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "emit_signal", &Object::_emit_signal, mi, varray(), false);
  1381. }
  1382. {
  1383. MethodInfo mi;
  1384. mi.name = "call";
  1385. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1386. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call", &Object::_call_bind, mi);
  1387. }
  1388. {
  1389. MethodInfo mi;
  1390. mi.name = "call_deferred";
  1391. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1392. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_deferred", &Object::_call_deferred_bind, mi, varray(), false);
  1393. }
  1394. ClassDB::bind_method(D_METHOD("set_deferred", "property", "value"), &Object::set_deferred);
  1395. ClassDB::bind_method(D_METHOD("callv", "method", "arg_array"), &Object::callv);
  1396. ClassDB::bind_method(D_METHOD("has_method", "method"), &Object::has_method);
  1397. ClassDB::bind_method(D_METHOD("get_method_argument_count", "method"), &Object::_get_method_argument_count_bind);
  1398. ClassDB::bind_method(D_METHOD("has_signal", "signal"), &Object::has_signal);
  1399. ClassDB::bind_method(D_METHOD("get_signal_list"), &Object::_get_signal_list);
  1400. ClassDB::bind_method(D_METHOD("get_signal_connection_list", "signal"), &Object::_get_signal_connection_list);
  1401. ClassDB::bind_method(D_METHOD("get_incoming_connections"), &Object::_get_incoming_connections);
  1402. ClassDB::bind_method(D_METHOD("connect", "signal", "callable", "flags"), &Object::connect, DEFVAL(0));
  1403. ClassDB::bind_method(D_METHOD("disconnect", "signal", "callable"), &Object::disconnect);
  1404. ClassDB::bind_method(D_METHOD("is_connected", "signal", "callable"), &Object::is_connected);
  1405. ClassDB::bind_method(D_METHOD("set_block_signals", "enable"), &Object::set_block_signals);
  1406. ClassDB::bind_method(D_METHOD("is_blocking_signals"), &Object::is_blocking_signals);
  1407. ClassDB::bind_method(D_METHOD("notify_property_list_changed"), &Object::notify_property_list_changed);
  1408. ClassDB::bind_method(D_METHOD("set_message_translation", "enable"), &Object::set_message_translation);
  1409. ClassDB::bind_method(D_METHOD("can_translate_messages"), &Object::can_translate_messages);
  1410. ClassDB::bind_method(D_METHOD("tr", "message", "context"), &Object::tr, DEFVAL(StringName()));
  1411. ClassDB::bind_method(D_METHOD("tr_n", "message", "plural_message", "n", "context"), &Object::tr_n, DEFVAL(StringName()));
  1412. ClassDB::bind_method(D_METHOD("is_queued_for_deletion"), &Object::is_queued_for_deletion);
  1413. ClassDB::bind_method(D_METHOD("cancel_free"), &Object::cancel_free);
  1414. ClassDB::add_virtual_method("Object", MethodInfo("free"), false);
  1415. ADD_SIGNAL(MethodInfo("script_changed"));
  1416. ADD_SIGNAL(MethodInfo("property_list_changed"));
  1417. #define BIND_OBJ_CORE_METHOD(m_method) \
  1418. ::ClassDB::add_virtual_method(get_class_static(), m_method, true, Vector<String>(), true);
  1419. MethodInfo notification_mi("_notification", PropertyInfo(Variant::INT, "what"));
  1420. notification_mi.arguments_metadata.push_back(GodotTypeInfo::Metadata::METADATA_INT_IS_INT32);
  1421. BIND_OBJ_CORE_METHOD(notification_mi);
  1422. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_set", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value")));
  1423. #ifdef TOOLS_ENABLED
  1424. MethodInfo miget("_get", PropertyInfo(Variant::STRING_NAME, "property"));
  1425. miget.return_val.name = "Variant";
  1426. miget.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1427. BIND_OBJ_CORE_METHOD(miget);
  1428. MethodInfo plget("_get_property_list");
  1429. plget.return_val.type = Variant::ARRAY;
  1430. plget.return_val.hint = PROPERTY_HINT_ARRAY_TYPE;
  1431. plget.return_val.hint_string = "Dictionary";
  1432. BIND_OBJ_CORE_METHOD(plget);
  1433. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::NIL, "_validate_property", PropertyInfo(Variant::DICTIONARY, "property")));
  1434. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_property_can_revert", PropertyInfo(Variant::STRING_NAME, "property")));
  1435. MethodInfo mipgr("_property_get_revert", PropertyInfo(Variant::STRING_NAME, "property"));
  1436. mipgr.return_val.name = "Variant";
  1437. mipgr.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1438. BIND_OBJ_CORE_METHOD(mipgr);
  1439. #endif
  1440. BIND_OBJ_CORE_METHOD(MethodInfo("_init"));
  1441. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::STRING, "_to_string"));
  1442. BIND_CONSTANT(NOTIFICATION_POSTINITIALIZE);
  1443. BIND_CONSTANT(NOTIFICATION_PREDELETE);
  1444. BIND_CONSTANT(NOTIFICATION_EXTENSION_RELOADED);
  1445. BIND_ENUM_CONSTANT(CONNECT_DEFERRED);
  1446. BIND_ENUM_CONSTANT(CONNECT_PERSIST);
  1447. BIND_ENUM_CONSTANT(CONNECT_ONE_SHOT);
  1448. BIND_ENUM_CONSTANT(CONNECT_REFERENCE_COUNTED);
  1449. }
  1450. void Object::set_deferred(const StringName &p_property, const Variant &p_value) {
  1451. MessageQueue::get_singleton()->push_set(this, p_property, p_value);
  1452. }
  1453. void Object::set_block_signals(bool p_block) {
  1454. _block_signals = p_block;
  1455. }
  1456. bool Object::is_blocking_signals() const {
  1457. return _block_signals;
  1458. }
  1459. Variant::Type Object::get_static_property_type(const StringName &p_property, bool *r_valid) const {
  1460. bool valid;
  1461. Variant::Type t = ClassDB::get_property_type(get_class_name(), p_property, &valid);
  1462. if (valid) {
  1463. if (r_valid) {
  1464. *r_valid = true;
  1465. }
  1466. return t;
  1467. }
  1468. if (get_script_instance()) {
  1469. return get_script_instance()->get_property_type(p_property, r_valid);
  1470. }
  1471. if (r_valid) {
  1472. *r_valid = false;
  1473. }
  1474. return Variant::NIL;
  1475. }
  1476. Variant::Type Object::get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid) const {
  1477. if (p_path.size() == 0) {
  1478. if (r_valid) {
  1479. *r_valid = false;
  1480. }
  1481. return Variant::NIL;
  1482. }
  1483. bool valid = false;
  1484. Variant::Type t = get_static_property_type(p_path[0], &valid);
  1485. if (!valid) {
  1486. if (r_valid) {
  1487. *r_valid = false;
  1488. }
  1489. return Variant::NIL;
  1490. }
  1491. Callable::CallError ce;
  1492. Variant check;
  1493. Variant::construct(t, check, nullptr, 0, ce);
  1494. for (int i = 1; i < p_path.size(); i++) {
  1495. if (check.get_type() == Variant::OBJECT || check.get_type() == Variant::DICTIONARY || check.get_type() == Variant::ARRAY) {
  1496. // We cannot be sure about the type of properties this type can have
  1497. if (r_valid) {
  1498. *r_valid = false;
  1499. }
  1500. return Variant::NIL;
  1501. }
  1502. check = check.get_named(p_path[i], valid);
  1503. if (!valid) {
  1504. if (r_valid) {
  1505. *r_valid = false;
  1506. }
  1507. return Variant::NIL;
  1508. }
  1509. }
  1510. if (r_valid) {
  1511. *r_valid = true;
  1512. }
  1513. return check.get_type();
  1514. }
  1515. bool Object::is_queued_for_deletion() const {
  1516. return _is_queued_for_deletion;
  1517. }
  1518. #ifdef TOOLS_ENABLED
  1519. void Object::set_edited(bool p_edited) {
  1520. _edited = p_edited;
  1521. _edited_version++;
  1522. }
  1523. bool Object::is_edited() const {
  1524. return _edited;
  1525. }
  1526. uint32_t Object::get_edited_version() const {
  1527. return _edited_version;
  1528. }
  1529. #endif
  1530. StringName Object::get_class_name_for_extension(const GDExtension *p_library) const {
  1531. #ifdef TOOLS_ENABLED
  1532. // If this is the library this extension comes from and it's a placeholder, we
  1533. // have to return the closest native parent's class name, so that it doesn't try to
  1534. // use this like the real object.
  1535. if (unlikely(_extension && _extension->library == p_library && _extension->is_placeholder)) {
  1536. const StringName *class_name = _get_class_namev();
  1537. return *class_name;
  1538. }
  1539. #endif
  1540. // Only return the class name per the extension if it matches the given p_library.
  1541. if (_extension && _extension->library == p_library) {
  1542. return _extension->class_name;
  1543. }
  1544. // Extensions only have wrapper classes for classes exposed in ClassDB.
  1545. const StringName *class_name = _get_class_namev();
  1546. if (ClassDB::is_class_exposed(*class_name)) {
  1547. return *class_name;
  1548. }
  1549. // Find the nearest parent class that's exposed.
  1550. StringName parent_class = ClassDB::get_parent_class(*class_name);
  1551. while (parent_class != StringName()) {
  1552. if (ClassDB::is_class_exposed(parent_class)) {
  1553. return parent_class;
  1554. }
  1555. parent_class = ClassDB::get_parent_class(parent_class);
  1556. }
  1557. return SNAME("Object");
  1558. }
  1559. void Object::set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
  1560. // This is only meant to be used on creation by the binder, but we also
  1561. // need to account for reloading (where the 'binding' will be cleared).
  1562. ERR_FAIL_COND(_instance_bindings != nullptr && _instance_bindings[0].binding != nullptr);
  1563. if (_instance_bindings == nullptr) {
  1564. _instance_bindings = (InstanceBinding *)memalloc(sizeof(InstanceBinding));
  1565. _instance_binding_count = 1;
  1566. }
  1567. _instance_bindings[0].binding = p_binding;
  1568. _instance_bindings[0].free_callback = p_callbacks->free_callback;
  1569. _instance_bindings[0].reference_callback = p_callbacks->reference_callback;
  1570. _instance_bindings[0].token = p_token;
  1571. }
  1572. void *Object::get_instance_binding(void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
  1573. void *binding = nullptr;
  1574. _instance_binding_mutex.lock();
  1575. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1576. if (_instance_bindings[i].token == p_token) {
  1577. binding = _instance_bindings[i].binding;
  1578. break;
  1579. }
  1580. }
  1581. if (unlikely(!binding && p_callbacks)) {
  1582. uint32_t current_size = next_power_of_2(_instance_binding_count);
  1583. uint32_t new_size = next_power_of_2(_instance_binding_count + 1);
  1584. if (current_size == 0 || new_size > current_size) {
  1585. _instance_bindings = (InstanceBinding *)memrealloc(_instance_bindings, new_size * sizeof(InstanceBinding));
  1586. }
  1587. _instance_bindings[_instance_binding_count].free_callback = p_callbacks->free_callback;
  1588. _instance_bindings[_instance_binding_count].reference_callback = p_callbacks->reference_callback;
  1589. _instance_bindings[_instance_binding_count].token = p_token;
  1590. binding = p_callbacks->create_callback(p_token, this);
  1591. _instance_bindings[_instance_binding_count].binding = binding;
  1592. #ifdef TOOLS_ENABLED
  1593. if (!_extension && Engine::get_singleton()->is_extension_reloading_enabled()) {
  1594. GDExtensionManager::get_singleton()->track_instance_binding(p_token, this);
  1595. }
  1596. #endif
  1597. _instance_binding_count++;
  1598. }
  1599. _instance_binding_mutex.unlock();
  1600. return binding;
  1601. }
  1602. bool Object::has_instance_binding(void *p_token) {
  1603. bool found = false;
  1604. _instance_binding_mutex.lock();
  1605. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1606. if (_instance_bindings[i].token == p_token) {
  1607. found = true;
  1608. break;
  1609. }
  1610. }
  1611. _instance_binding_mutex.unlock();
  1612. return found;
  1613. }
  1614. void Object::free_instance_binding(void *p_token) {
  1615. bool found = false;
  1616. _instance_binding_mutex.lock();
  1617. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1618. if (!found && _instance_bindings[i].token == p_token) {
  1619. if (_instance_bindings[i].free_callback) {
  1620. _instance_bindings[i].free_callback(_instance_bindings[i].token, this, _instance_bindings[i].binding);
  1621. }
  1622. found = true;
  1623. }
  1624. if (found) {
  1625. if (i + 1 < _instance_binding_count) {
  1626. _instance_bindings[i] = _instance_bindings[i + 1];
  1627. } else {
  1628. _instance_bindings[i] = { nullptr };
  1629. }
  1630. }
  1631. }
  1632. if (found) {
  1633. _instance_binding_count--;
  1634. }
  1635. _instance_binding_mutex.unlock();
  1636. }
  1637. #ifdef TOOLS_ENABLED
  1638. void Object::clear_internal_extension() {
  1639. ERR_FAIL_NULL(_extension);
  1640. // Free the instance inside the GDExtension.
  1641. if (_extension->free_instance) {
  1642. _extension->free_instance(_extension->class_userdata, _extension_instance);
  1643. }
  1644. _extension = nullptr;
  1645. _extension_instance = nullptr;
  1646. // Clear the instance bindings.
  1647. _instance_binding_mutex.lock();
  1648. if (_instance_bindings) {
  1649. if (_instance_bindings[0].free_callback) {
  1650. _instance_bindings[0].free_callback(_instance_bindings[0].token, this, _instance_bindings[0].binding);
  1651. }
  1652. _instance_bindings[0].binding = nullptr;
  1653. _instance_bindings[0].token = nullptr;
  1654. _instance_bindings[0].free_callback = nullptr;
  1655. _instance_bindings[0].reference_callback = nullptr;
  1656. }
  1657. _instance_binding_mutex.unlock();
  1658. // Clear the virtual methods.
  1659. while (virtual_method_list) {
  1660. (*virtual_method_list->method) = nullptr;
  1661. (*virtual_method_list->initialized) = false;
  1662. virtual_method_list = virtual_method_list->next;
  1663. }
  1664. }
  1665. void Object::reset_internal_extension(ObjectGDExtension *p_extension) {
  1666. ERR_FAIL_COND(_extension != nullptr);
  1667. if (p_extension) {
  1668. _extension_instance = p_extension->recreate_instance ? p_extension->recreate_instance(p_extension->class_userdata, (GDExtensionObjectPtr)this) : nullptr;
  1669. ERR_FAIL_NULL_MSG(_extension_instance, "Unable to recreate GDExtension instance - does this extension support hot reloading?");
  1670. _extension = p_extension;
  1671. }
  1672. }
  1673. #endif
  1674. void Object::_construct_object(bool p_reference) {
  1675. type_is_reference = p_reference;
  1676. _instance_id = ObjectDB::add_instance(this);
  1677. #ifdef DEBUG_ENABLED
  1678. _lock_index.init(1);
  1679. #endif
  1680. }
  1681. Object::Object(bool p_reference) {
  1682. _construct_object(p_reference);
  1683. }
  1684. Object::Object() {
  1685. _construct_object(false);
  1686. }
  1687. void Object::detach_from_objectdb() {
  1688. if (_instance_id != ObjectID()) {
  1689. ObjectDB::remove_instance(this);
  1690. _instance_id = ObjectID();
  1691. }
  1692. }
  1693. Object::~Object() {
  1694. if (script_instance) {
  1695. memdelete(script_instance);
  1696. }
  1697. script_instance = nullptr;
  1698. if (_extension) {
  1699. #ifdef TOOLS_ENABLED
  1700. if (_extension->untrack_instance) {
  1701. _extension->untrack_instance(_extension->tracking_userdata, this);
  1702. }
  1703. #endif
  1704. if (_extension->free_instance) {
  1705. _extension->free_instance(_extension->class_userdata, _extension_instance);
  1706. }
  1707. _extension = nullptr;
  1708. _extension_instance = nullptr;
  1709. }
  1710. #ifdef TOOLS_ENABLED
  1711. else if (_instance_bindings != nullptr) {
  1712. Engine *engine = Engine::get_singleton();
  1713. GDExtensionManager *gdextension_manager = GDExtensionManager::get_singleton();
  1714. if (engine && gdextension_manager && engine->is_extension_reloading_enabled()) {
  1715. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1716. gdextension_manager->untrack_instance_binding(_instance_bindings[i].token, this);
  1717. }
  1718. }
  1719. }
  1720. #endif
  1721. if (_emitting) {
  1722. //@todo this may need to actually reach the debugger prioritarily somehow because it may crash before
  1723. 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.");
  1724. }
  1725. // Drop all connections to the signals of this object.
  1726. while (signal_map.size()) {
  1727. // Avoid regular iteration so erasing is safe.
  1728. KeyValue<StringName, SignalData> &E = *signal_map.begin();
  1729. SignalData *s = &E.value;
  1730. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1731. Object *target = slot_kv.value.conn.callable.get_object();
  1732. if (likely(target)) {
  1733. target->connections.erase(slot_kv.value.cE);
  1734. }
  1735. }
  1736. signal_map.erase(E.key);
  1737. }
  1738. // Disconnect signals that connect to this object.
  1739. while (connections.size()) {
  1740. Connection c = connections.front()->get();
  1741. Object *obj = c.callable.get_object();
  1742. bool disconnected = false;
  1743. if (likely(obj)) {
  1744. disconnected = c.signal.get_object()->_disconnect(c.signal.get_name(), c.callable, true);
  1745. }
  1746. if (unlikely(!disconnected)) {
  1747. // If the disconnect has failed, abandon the connection to avoid getting trapped in an infinite loop here.
  1748. connections.pop_front();
  1749. }
  1750. }
  1751. if (_instance_id != ObjectID()) {
  1752. ObjectDB::remove_instance(this);
  1753. _instance_id = ObjectID();
  1754. }
  1755. _predelete_ok = 2;
  1756. if (_instance_bindings != nullptr) {
  1757. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1758. if (_instance_bindings[i].free_callback) {
  1759. _instance_bindings[i].free_callback(_instance_bindings[i].token, this, _instance_bindings[i].binding);
  1760. }
  1761. }
  1762. memfree(_instance_bindings);
  1763. }
  1764. }
  1765. bool predelete_handler(Object *p_object) {
  1766. return p_object->_predelete();
  1767. }
  1768. void postinitialize_handler(Object *p_object) {
  1769. p_object->_initialize();
  1770. p_object->_postinitialize();
  1771. }
  1772. void ObjectDB::debug_objects(DebugFunc p_func) {
  1773. spin_lock.lock();
  1774. for (uint32_t i = 0, count = slot_count; i < slot_max && count != 0; i++) {
  1775. if (object_slots[i].validator) {
  1776. p_func(object_slots[i].object);
  1777. count--;
  1778. }
  1779. }
  1780. spin_lock.unlock();
  1781. }
  1782. #ifdef TOOLS_ENABLED
  1783. void Object::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1784. const String pf = p_function;
  1785. if (p_idx == 0) {
  1786. if (pf == "connect" || pf == "is_connected" || pf == "disconnect" || pf == "emit_signal" || pf == "has_signal") {
  1787. List<MethodInfo> signals;
  1788. get_signal_list(&signals);
  1789. for (const MethodInfo &E : signals) {
  1790. r_options->push_back(E.name.quote());
  1791. }
  1792. } else if (pf == "call" || pf == "call_deferred" || pf == "callv" || pf == "has_method") {
  1793. List<MethodInfo> methods;
  1794. get_method_list(&methods);
  1795. for (const MethodInfo &E : methods) {
  1796. if (E.name.begins_with("_") && !(E.flags & METHOD_FLAG_VIRTUAL)) {
  1797. continue;
  1798. }
  1799. r_options->push_back(E.name.quote());
  1800. }
  1801. } else if (pf == "set" || pf == "set_deferred" || pf == "get") {
  1802. List<PropertyInfo> properties;
  1803. get_property_list(&properties);
  1804. for (const PropertyInfo &E : properties) {
  1805. if (E.usage & PROPERTY_USAGE_DEFAULT && !(E.usage & PROPERTY_USAGE_INTERNAL)) {
  1806. r_options->push_back(E.name.quote());
  1807. }
  1808. }
  1809. } else if (pf == "set_meta" || pf == "get_meta" || pf == "has_meta" || pf == "remove_meta") {
  1810. for (const KeyValue<StringName, Variant> &K : metadata) {
  1811. r_options->push_back(String(K.key).quote());
  1812. }
  1813. }
  1814. } else if (p_idx == 2) {
  1815. if (pf == "connect") {
  1816. // Ideally, the constants should be inferred by the parameter.
  1817. // But a parameter's PropertyInfo does not store the enum they come from, so this will do for now.
  1818. List<StringName> constants;
  1819. ClassDB::get_enum_constants("Object", "ConnectFlags", &constants);
  1820. for (const StringName &E : constants) {
  1821. r_options->push_back(String(E));
  1822. }
  1823. }
  1824. }
  1825. }
  1826. #endif
  1827. SpinLock ObjectDB::spin_lock;
  1828. uint32_t ObjectDB::slot_count = 0;
  1829. uint32_t ObjectDB::slot_max = 0;
  1830. ObjectDB::ObjectSlot *ObjectDB::object_slots = nullptr;
  1831. uint64_t ObjectDB::validator_counter = 0;
  1832. int ObjectDB::get_object_count() {
  1833. return slot_count;
  1834. }
  1835. ObjectID ObjectDB::add_instance(Object *p_object) {
  1836. spin_lock.lock();
  1837. if (unlikely(slot_count == slot_max)) {
  1838. CRASH_COND(slot_count == (1 << OBJECTDB_SLOT_MAX_COUNT_BITS));
  1839. uint32_t new_slot_max = slot_max > 0 ? slot_max * 2 : 1;
  1840. object_slots = (ObjectSlot *)memrealloc(object_slots, sizeof(ObjectSlot) * new_slot_max);
  1841. for (uint32_t i = slot_max; i < new_slot_max; i++) {
  1842. object_slots[i].object = nullptr;
  1843. object_slots[i].is_ref_counted = false;
  1844. object_slots[i].next_free = i;
  1845. object_slots[i].validator = 0;
  1846. }
  1847. slot_max = new_slot_max;
  1848. }
  1849. uint32_t slot = object_slots[slot_count].next_free;
  1850. if (object_slots[slot].object != nullptr) {
  1851. spin_lock.unlock();
  1852. ERR_FAIL_COND_V(object_slots[slot].object != nullptr, ObjectID());
  1853. }
  1854. object_slots[slot].object = p_object;
  1855. object_slots[slot].is_ref_counted = p_object->is_ref_counted();
  1856. validator_counter = (validator_counter + 1) & OBJECTDB_VALIDATOR_MASK;
  1857. if (unlikely(validator_counter == 0)) {
  1858. validator_counter = 1;
  1859. }
  1860. object_slots[slot].validator = validator_counter;
  1861. uint64_t id = validator_counter;
  1862. id <<= OBJECTDB_SLOT_MAX_COUNT_BITS;
  1863. id |= uint64_t(slot);
  1864. if (p_object->is_ref_counted()) {
  1865. id |= OBJECTDB_REFERENCE_BIT;
  1866. }
  1867. slot_count++;
  1868. spin_lock.unlock();
  1869. return ObjectID(id);
  1870. }
  1871. void ObjectDB::remove_instance(Object *p_object) {
  1872. uint64_t t = p_object->get_instance_id();
  1873. uint32_t slot = t & OBJECTDB_SLOT_MAX_COUNT_MASK; //slot is always valid on valid object
  1874. spin_lock.lock();
  1875. #ifdef DEBUG_ENABLED
  1876. if (object_slots[slot].object != p_object) {
  1877. spin_lock.unlock();
  1878. ERR_FAIL_COND(object_slots[slot].object != p_object);
  1879. }
  1880. {
  1881. uint64_t validator = (t >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
  1882. if (object_slots[slot].validator != validator) {
  1883. spin_lock.unlock();
  1884. ERR_FAIL_COND(object_slots[slot].validator != validator);
  1885. }
  1886. }
  1887. #endif
  1888. //decrease slot count
  1889. slot_count--;
  1890. //set the free slot properly
  1891. object_slots[slot_count].next_free = slot;
  1892. //invalidate, so checks against it fail
  1893. object_slots[slot].validator = 0;
  1894. object_slots[slot].is_ref_counted = false;
  1895. object_slots[slot].object = nullptr;
  1896. spin_lock.unlock();
  1897. }
  1898. void ObjectDB::setup() {
  1899. //nothing to do now
  1900. }
  1901. void ObjectDB::cleanup() {
  1902. spin_lock.lock();
  1903. if (slot_count > 0) {
  1904. WARN_PRINT("ObjectDB instances leaked at exit (run with --verbose for details).");
  1905. if (OS::get_singleton()->is_stdout_verbose()) {
  1906. // Ensure calling the native classes because if a leaked instance has a script
  1907. // that overrides any of those methods, it'd not be OK to call them at this point,
  1908. // now the scripting languages have already been terminated.
  1909. MethodBind *node_get_name = ClassDB::get_method("Node", "get_name");
  1910. MethodBind *resource_get_path = ClassDB::get_method("Resource", "get_path");
  1911. Callable::CallError call_error;
  1912. for (uint32_t i = 0, count = slot_count; i < slot_max && count != 0; i++) {
  1913. if (object_slots[i].validator) {
  1914. Object *obj = object_slots[i].object;
  1915. String extra_info;
  1916. if (obj->is_class("Node")) {
  1917. extra_info = " - Node name: " + String(node_get_name->call(obj, nullptr, 0, call_error));
  1918. }
  1919. if (obj->is_class("Resource")) {
  1920. extra_info = " - Resource path: " + String(resource_get_path->call(obj, nullptr, 0, call_error));
  1921. }
  1922. uint64_t id = uint64_t(i) | (uint64_t(object_slots[i].validator) << OBJECTDB_SLOT_MAX_COUNT_BITS) | (object_slots[i].is_ref_counted ? OBJECTDB_REFERENCE_BIT : 0);
  1923. DEV_ASSERT(id == (uint64_t)obj->get_instance_id()); // We could just use the id from the object, but this check may help catching memory corruption catastrophes.
  1924. print_line("Leaked instance: " + String(obj->get_class()) + ":" + uitos(id) + extra_info);
  1925. count--;
  1926. }
  1927. }
  1928. 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()`).");
  1929. }
  1930. }
  1931. if (object_slots) {
  1932. memfree(object_slots);
  1933. }
  1934. spin_lock.unlock();
  1935. }