object.cpp 58 KB

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