2
0

object.cpp 68 KB

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