object.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996
  1. /*************************************************************************/
  2. /* object.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/class_db.h"
  32. #include "core/core_string_names.h"
  33. #include "core/message_queue.h"
  34. #include "core/os/os.h"
  35. #include "core/print_string.h"
  36. #include "core/resource.h"
  37. #include "core/script_language.h"
  38. #include "core/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. if (p_dict.has("name"))
  69. pi.name = p_dict["name"];
  70. if (p_dict.has("class_name"))
  71. pi.class_name = p_dict["class_name"];
  72. if (p_dict.has("hint"))
  73. pi.hint = PropertyHint(int(p_dict["hint"]));
  74. if (p_dict.has("hint_string"))
  75. pi.hint_string = p_dict["hint_string"];
  76. if (p_dict.has("usage"))
  77. pi.usage = p_dict["usage"];
  78. return pi;
  79. }
  80. Array convert_property_list(const List<PropertyInfo> *p_list) {
  81. Array va;
  82. for (const List<PropertyInfo>::Element *E = p_list->front(); E; E = E->next()) {
  83. va.push_back(Dictionary(E->get()));
  84. }
  85. return va;
  86. }
  87. MethodInfo::operator Dictionary() const {
  88. Dictionary d;
  89. d["name"] = name;
  90. d["args"] = convert_property_list(&arguments);
  91. Array da;
  92. for (int i = 0; i < default_arguments.size(); i++)
  93. da.push_back(default_arguments[i]);
  94. d["default_args"] = da;
  95. d["flags"] = flags;
  96. d["id"] = id;
  97. Dictionary r = return_val;
  98. d["return"] = r;
  99. return d;
  100. }
  101. MethodInfo MethodInfo::from_dict(const Dictionary &p_dict) {
  102. MethodInfo mi;
  103. if (p_dict.has("name"))
  104. mi.name = p_dict["name"];
  105. Array args;
  106. if (p_dict.has("args")) {
  107. args = p_dict["args"];
  108. }
  109. for (int i = 0; i < args.size(); i++) {
  110. Dictionary d = args[i];
  111. mi.arguments.push_back(PropertyInfo::from_dict(d));
  112. }
  113. Array defargs;
  114. if (p_dict.has("default_args")) {
  115. defargs = p_dict["default_args"];
  116. }
  117. for (int i = 0; i < defargs.size(); i++) {
  118. mi.default_arguments.push_back(defargs[i]);
  119. }
  120. if (p_dict.has("return")) {
  121. mi.return_val = PropertyInfo::from_dict(p_dict["return"]);
  122. }
  123. if (p_dict.has("flags"))
  124. mi.flags = p_dict["flags"];
  125. return mi;
  126. }
  127. MethodInfo::MethodInfo() :
  128. flags(METHOD_FLAG_NORMAL) {}
  129. MethodInfo::MethodInfo(const String &p_name) :
  130. name(p_name),
  131. flags(METHOD_FLAG_NORMAL) {
  132. }
  133. MethodInfo::MethodInfo(const String &p_name, const PropertyInfo &p_param1) :
  134. name(p_name),
  135. flags(METHOD_FLAG_NORMAL) {
  136. arguments.push_back(p_param1);
  137. }
  138. MethodInfo::MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2) :
  139. name(p_name),
  140. flags(METHOD_FLAG_NORMAL) {
  141. arguments.push_back(p_param1);
  142. arguments.push_back(p_param2);
  143. }
  144. MethodInfo::MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3) :
  145. name(p_name),
  146. flags(METHOD_FLAG_NORMAL) {
  147. arguments.push_back(p_param1);
  148. arguments.push_back(p_param2);
  149. arguments.push_back(p_param3);
  150. }
  151. MethodInfo::MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4) :
  152. name(p_name),
  153. flags(METHOD_FLAG_NORMAL) {
  154. arguments.push_back(p_param1);
  155. arguments.push_back(p_param2);
  156. arguments.push_back(p_param3);
  157. arguments.push_back(p_param4);
  158. }
  159. 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) :
  160. name(p_name),
  161. flags(METHOD_FLAG_NORMAL) {
  162. arguments.push_back(p_param1);
  163. arguments.push_back(p_param2);
  164. arguments.push_back(p_param3);
  165. arguments.push_back(p_param4);
  166. arguments.push_back(p_param5);
  167. }
  168. MethodInfo::MethodInfo(Variant::Type ret) :
  169. flags(METHOD_FLAG_NORMAL) {
  170. return_val.type = ret;
  171. }
  172. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name) :
  173. name(p_name),
  174. flags(METHOD_FLAG_NORMAL) {
  175. return_val.type = ret;
  176. }
  177. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1) :
  178. name(p_name),
  179. flags(METHOD_FLAG_NORMAL) {
  180. return_val.type = ret;
  181. arguments.push_back(p_param1);
  182. }
  183. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2) :
  184. name(p_name),
  185. flags(METHOD_FLAG_NORMAL) {
  186. return_val.type = ret;
  187. arguments.push_back(p_param1);
  188. arguments.push_back(p_param2);
  189. }
  190. MethodInfo::MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3) :
  191. name(p_name),
  192. flags(METHOD_FLAG_NORMAL) {
  193. return_val.type = ret;
  194. arguments.push_back(p_param1);
  195. arguments.push_back(p_param2);
  196. arguments.push_back(p_param3);
  197. }
  198. 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) :
  199. name(p_name),
  200. flags(METHOD_FLAG_NORMAL) {
  201. return_val.type = ret;
  202. arguments.push_back(p_param1);
  203. arguments.push_back(p_param2);
  204. arguments.push_back(p_param3);
  205. arguments.push_back(p_param4);
  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, const PropertyInfo &p_param5) :
  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. arguments.push_back(p_param5);
  216. }
  217. MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name) :
  218. name(p_name),
  219. return_val(p_ret),
  220. flags(METHOD_FLAG_NORMAL) {
  221. }
  222. MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1) :
  223. name(p_name),
  224. return_val(p_ret),
  225. flags(METHOD_FLAG_NORMAL) {
  226. arguments.push_back(p_param1);
  227. }
  228. MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2) :
  229. name(p_name),
  230. return_val(p_ret),
  231. flags(METHOD_FLAG_NORMAL) {
  232. arguments.push_back(p_param1);
  233. arguments.push_back(p_param2);
  234. }
  235. MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3) :
  236. name(p_name),
  237. return_val(p_ret),
  238. flags(METHOD_FLAG_NORMAL) {
  239. arguments.push_back(p_param1);
  240. arguments.push_back(p_param2);
  241. arguments.push_back(p_param3);
  242. }
  243. 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) :
  244. name(p_name),
  245. return_val(p_ret),
  246. flags(METHOD_FLAG_NORMAL) {
  247. arguments.push_back(p_param1);
  248. arguments.push_back(p_param2);
  249. arguments.push_back(p_param3);
  250. arguments.push_back(p_param4);
  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, const PropertyInfo &p_param5) :
  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. arguments.push_back(p_param5);
  261. }
  262. Object::Connection::operator Variant() const {
  263. Dictionary d;
  264. d["signal"] = signal;
  265. d["callable"] = callable;
  266. d["flags"] = flags;
  267. d["binds"] = binds;
  268. return d;
  269. }
  270. bool Object::Connection::operator<(const Connection &p_conn) const {
  271. if (signal == p_conn.signal) {
  272. return callable < p_conn.callable;
  273. } else {
  274. return signal < p_conn.signal;
  275. }
  276. }
  277. Object::Connection::Connection(const Variant &p_variant) {
  278. Dictionary d = p_variant;
  279. if (d.has("signal"))
  280. signal = d["signal"];
  281. if (d.has("callable"))
  282. callable = d["callable"];
  283. if (d.has("flags"))
  284. flags = d["flags"];
  285. if (d.has("binds"))
  286. binds = d["binds"];
  287. }
  288. bool Object::_predelete() {
  289. _predelete_ok = 1;
  290. notification(NOTIFICATION_PREDELETE, true);
  291. if (_predelete_ok) {
  292. _class_ptr = nullptr; //must restore so destructors can access class ptr correctly
  293. }
  294. return _predelete_ok;
  295. }
  296. void Object::_postinitialize() {
  297. _class_ptr = _get_class_namev();
  298. _initialize_classv();
  299. notification(NOTIFICATION_POSTINITIALIZE);
  300. }
  301. void Object::get_valid_parents_static(List<String> *p_parents) {
  302. }
  303. void Object::_get_valid_parents_static(List<String> *p_parents) {
  304. }
  305. void Object::set(const StringName &p_name, const Variant &p_value, bool *r_valid) {
  306. #ifdef TOOLS_ENABLED
  307. _edited = true;
  308. #endif
  309. if (script_instance) {
  310. if (script_instance->set(p_name, p_value)) {
  311. if (r_valid)
  312. *r_valid = true;
  313. return;
  314. }
  315. }
  316. //try built-in setgetter
  317. {
  318. if (ClassDB::set_property(this, p_name, p_value, r_valid)) {
  319. /*
  320. if (r_valid)
  321. *r_valid=true;
  322. */
  323. return;
  324. }
  325. }
  326. if (p_name == CoreStringNames::get_singleton()->_script) {
  327. set_script(p_value);
  328. if (r_valid)
  329. *r_valid = true;
  330. return;
  331. } else if (p_name == CoreStringNames::get_singleton()->_meta) {
  332. //set_meta(p_name,p_value);
  333. metadata = p_value.duplicate();
  334. if (r_valid)
  335. *r_valid = true;
  336. return;
  337. }
  338. //something inside the object... :|
  339. bool success = _setv(p_name, p_value);
  340. if (success) {
  341. if (r_valid)
  342. *r_valid = true;
  343. return;
  344. }
  345. {
  346. bool valid;
  347. setvar(p_name, p_value, &valid);
  348. if (valid) {
  349. if (r_valid)
  350. *r_valid = true;
  351. return;
  352. }
  353. }
  354. #ifdef TOOLS_ENABLED
  355. if (script_instance) {
  356. bool valid;
  357. script_instance->property_set_fallback(p_name, p_value, &valid);
  358. if (valid) {
  359. if (r_valid)
  360. *r_valid = true;
  361. return;
  362. }
  363. }
  364. #endif
  365. if (r_valid)
  366. *r_valid = false;
  367. }
  368. Variant Object::get(const StringName &p_name, bool *r_valid) const {
  369. Variant ret;
  370. if (script_instance) {
  371. if (script_instance->get(p_name, ret)) {
  372. if (r_valid)
  373. *r_valid = true;
  374. return ret;
  375. }
  376. }
  377. //try built-in setgetter
  378. {
  379. if (ClassDB::get_property(const_cast<Object *>(this), p_name, ret)) {
  380. if (r_valid)
  381. *r_valid = true;
  382. return ret;
  383. }
  384. }
  385. if (p_name == CoreStringNames::get_singleton()->_script) {
  386. ret = get_script();
  387. if (r_valid)
  388. *r_valid = true;
  389. return ret;
  390. } else if (p_name == CoreStringNames::get_singleton()->_meta) {
  391. ret = metadata;
  392. if (r_valid)
  393. *r_valid = true;
  394. return ret;
  395. } else {
  396. //something inside the object... :|
  397. bool success = _getv(p_name, ret);
  398. if (success) {
  399. if (r_valid)
  400. *r_valid = true;
  401. return ret;
  402. }
  403. //if nothing else, use getvar
  404. {
  405. bool valid;
  406. ret = getvar(p_name, &valid);
  407. if (valid) {
  408. if (r_valid)
  409. *r_valid = true;
  410. return ret;
  411. }
  412. }
  413. #ifdef TOOLS_ENABLED
  414. if (script_instance) {
  415. bool valid;
  416. ret = script_instance->property_get_fallback(p_name, &valid);
  417. if (valid) {
  418. if (r_valid)
  419. *r_valid = true;
  420. return ret;
  421. }
  422. }
  423. #endif
  424. if (r_valid)
  425. *r_valid = false;
  426. return Variant();
  427. }
  428. }
  429. void Object::set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid) {
  430. if (p_names.empty()) {
  431. if (r_valid)
  432. *r_valid = false;
  433. return;
  434. }
  435. if (p_names.size() == 1) {
  436. set(p_names[0], p_value, r_valid);
  437. return;
  438. }
  439. bool valid = false;
  440. if (!r_valid)
  441. r_valid = &valid;
  442. List<Variant> value_stack;
  443. value_stack.push_back(get(p_names[0], r_valid));
  444. if (!*r_valid) {
  445. value_stack.clear();
  446. return;
  447. }
  448. for (int i = 1; i < p_names.size() - 1; i++) {
  449. value_stack.push_back(value_stack.back()->get().get_named(p_names[i], r_valid));
  450. if (!*r_valid) {
  451. value_stack.clear();
  452. return;
  453. }
  454. }
  455. value_stack.push_back(p_value); // p_names[p_names.size() - 1]
  456. for (int i = p_names.size() - 1; i > 0; i--) {
  457. value_stack.back()->prev()->get().set_named(p_names[i], value_stack.back()->get(), r_valid);
  458. value_stack.pop_back();
  459. if (!*r_valid) {
  460. value_stack.clear();
  461. return;
  462. }
  463. }
  464. set(p_names[0], value_stack.back()->get(), r_valid);
  465. value_stack.pop_back();
  466. ERR_FAIL_COND(!value_stack.empty());
  467. }
  468. Variant Object::get_indexed(const Vector<StringName> &p_names, bool *r_valid) const {
  469. if (p_names.empty()) {
  470. if (r_valid)
  471. *r_valid = false;
  472. return Variant();
  473. }
  474. bool valid = false;
  475. Variant current_value = get(p_names[0], &valid);
  476. for (int i = 1; i < p_names.size(); i++) {
  477. current_value = current_value.get_named(p_names[i], &valid);
  478. if (!valid)
  479. break;
  480. }
  481. if (r_valid)
  482. *r_valid = valid;
  483. return current_value;
  484. }
  485. void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) const {
  486. if (script_instance && p_reversed) {
  487. p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  488. script_instance->get_property_list(p_list);
  489. }
  490. _get_property_listv(p_list, p_reversed);
  491. if (!is_class("Script")) { // can still be set, but this is for userfriendlyness
  492. #ifdef TOOLS_ENABLED
  493. p_list->push_back(PropertyInfo(Variant::NIL, "Script", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  494. #endif
  495. p_list->push_back(PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT));
  496. }
  497. if (!metadata.empty()) {
  498. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "__meta__", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  499. }
  500. if (script_instance && !p_reversed) {
  501. p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  502. script_instance->get_property_list(p_list);
  503. }
  504. }
  505. void Object::_validate_property(PropertyInfo &property) const {
  506. }
  507. void Object::get_method_list(List<MethodInfo> *p_list) const {
  508. ClassDB::get_method_list(get_class_name(), p_list);
  509. if (script_instance) {
  510. script_instance->get_method_list(p_list);
  511. }
  512. }
  513. Variant Object::_call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  514. if (p_argcount < 1) {
  515. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  516. r_error.argument = 0;
  517. return Variant();
  518. }
  519. if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
  520. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  521. r_error.argument = 0;
  522. r_error.expected = Variant::STRING_NAME;
  523. return Variant();
  524. }
  525. StringName method = *p_args[0];
  526. return call(method, &p_args[1], p_argcount - 1, r_error);
  527. }
  528. Variant Object::_call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  529. if (p_argcount < 1) {
  530. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  531. r_error.argument = 0;
  532. return Variant();
  533. }
  534. if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
  535. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  536. r_error.argument = 0;
  537. r_error.expected = Variant::STRING_NAME;
  538. return Variant();
  539. }
  540. r_error.error = Callable::CallError::CALL_OK;
  541. StringName method = *p_args[0];
  542. MessageQueue::get_singleton()->push_call(get_instance_id(), method, &p_args[1], p_argcount - 1);
  543. return Variant();
  544. }
  545. #ifdef DEBUG_ENABLED
  546. static void _test_call_error(const StringName &p_func, const Callable::CallError &error) {
  547. switch (error.error) {
  548. case Callable::CallError::CALL_OK:
  549. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  550. break;
  551. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  552. ERR_FAIL_MSG("Error calling function: " + String(p_func) + " - Invalid type for argument " + itos(error.argument) + ", expected " + Variant::get_type_name(Variant::Type(error.expected)) + ".");
  553. break;
  554. }
  555. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  556. ERR_FAIL_MSG("Error calling function: " + String(p_func) + " - Too many arguments, expected " + itos(error.argument) + ".");
  557. break;
  558. }
  559. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  560. ERR_FAIL_MSG("Error calling function: " + String(p_func) + " - Too few arguments, expected " + itos(error.argument) + ".");
  561. break;
  562. }
  563. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  564. break;
  565. }
  566. }
  567. #else
  568. #define _test_call_error(m_str, m_err)
  569. #endif
  570. void Object::call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount) {
  571. if (p_method == CoreStringNames::get_singleton()->_free) {
  572. #ifdef DEBUG_ENABLED
  573. ERR_FAIL_COND_MSG(Object::cast_to<Reference>(this), "Can't 'free' a reference.");
  574. ERR_FAIL_COND_MSG(_lock_index.get() > 1, "Object is locked and can't be freed.");
  575. #endif
  576. //must be here, must be before everything,
  577. memdelete(this);
  578. return;
  579. }
  580. //Variant ret;
  581. OBJ_DEBUG_LOCK
  582. Callable::CallError error;
  583. if (script_instance) {
  584. script_instance->call_multilevel(p_method, p_args, p_argcount);
  585. //_test_call_error(p_method,error);
  586. }
  587. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  588. if (method) {
  589. method->call(this, p_args, p_argcount, error);
  590. _test_call_error(p_method, error);
  591. }
  592. }
  593. void Object::call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount) {
  594. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  595. Callable::CallError error;
  596. OBJ_DEBUG_LOCK
  597. if (method) {
  598. method->call(this, p_args, p_argcount, error);
  599. _test_call_error(p_method, error);
  600. }
  601. //Variant ret;
  602. if (script_instance) {
  603. script_instance->call_multilevel_reversed(p_method, p_args, p_argcount);
  604. //_test_call_error(p_method,error);
  605. }
  606. }
  607. bool Object::has_method(const StringName &p_method) const {
  608. if (p_method == CoreStringNames::get_singleton()->_free) {
  609. return true;
  610. }
  611. if (script_instance && script_instance->has_method(p_method)) {
  612. return true;
  613. }
  614. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  615. return method != nullptr;
  616. }
  617. Variant Object::getvar(const Variant &p_key, bool *r_valid) const {
  618. if (r_valid)
  619. *r_valid = false;
  620. return Variant();
  621. }
  622. void Object::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) {
  623. if (r_valid)
  624. *r_valid = false;
  625. }
  626. Variant Object::callv(const StringName &p_method, const Array &p_args) {
  627. const Variant **argptrs = nullptr;
  628. if (p_args.size() > 0) {
  629. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_args.size());
  630. for (int i = 0; i < p_args.size(); i++) {
  631. argptrs[i] = &p_args[i];
  632. }
  633. }
  634. Callable::CallError ce;
  635. Variant ret = call(p_method, argptrs, p_args.size(), ce);
  636. if (ce.error != Callable::CallError::CALL_OK) {
  637. ERR_FAIL_V_MSG(Variant(), "Error calling method from 'callv': " + Variant::get_call_error_text(this, p_method, argptrs, p_args.size(), ce) + ".");
  638. }
  639. return ret;
  640. }
  641. Variant Object::call(const StringName &p_name, VARIANT_ARG_DECLARE) {
  642. VARIANT_ARGPTRS;
  643. int argc = 0;
  644. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  645. if (argptr[i]->get_type() == Variant::NIL)
  646. break;
  647. argc++;
  648. }
  649. Callable::CallError error;
  650. Variant ret = call(p_name, argptr, argc, error);
  651. return ret;
  652. }
  653. void Object::call_multilevel(const StringName &p_name, VARIANT_ARG_DECLARE) {
  654. VARIANT_ARGPTRS;
  655. int argc = 0;
  656. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  657. if (argptr[i]->get_type() == Variant::NIL)
  658. break;
  659. argc++;
  660. }
  661. //Callable::CallError error;
  662. call_multilevel(p_name, argptr, argc);
  663. }
  664. Variant Object::call(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<Reference>(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->call(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. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  709. if (method) {
  710. ret = method->call(this, p_args, p_argcount, r_error);
  711. } else {
  712. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  713. }
  714. return ret;
  715. }
  716. void Object::notification(int p_notification, bool p_reversed) {
  717. _notificationv(p_notification, p_reversed);
  718. if (script_instance) {
  719. script_instance->notification(p_notification);
  720. }
  721. }
  722. String Object::to_string() {
  723. if (script_instance) {
  724. bool valid;
  725. String ret = script_instance->to_string(&valid);
  726. if (valid)
  727. return ret;
  728. }
  729. return "[" + get_class() + ":" + itos(get_instance_id()) + "]";
  730. }
  731. void Object::_changed_callback(Object *p_changed, const char *p_prop) {
  732. }
  733. void Object::add_change_receptor(Object *p_receptor) {
  734. change_receptors.insert(p_receptor);
  735. }
  736. void Object::remove_change_receptor(Object *p_receptor) {
  737. change_receptors.erase(p_receptor);
  738. }
  739. void Object::property_list_changed_notify() {
  740. _change_notify();
  741. }
  742. void Object::cancel_delete() {
  743. _predelete_ok = true;
  744. }
  745. void Object::set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance) {
  746. //this function is not meant to be used in any of these ways
  747. ERR_FAIL_COND(p_script.is_null());
  748. ERR_FAIL_COND(!p_instance);
  749. ERR_FAIL_COND(script_instance != nullptr || !script.is_null());
  750. script = p_script;
  751. script_instance = p_instance;
  752. }
  753. void Object::set_script(const Variant &p_script) {
  754. if (script == p_script)
  755. return;
  756. if (script_instance) {
  757. memdelete(script_instance);
  758. script_instance = nullptr;
  759. }
  760. script = p_script;
  761. Ref<Script> s = script;
  762. if (!s.is_null()) {
  763. if (s->can_instance()) {
  764. OBJ_DEBUG_LOCK
  765. script_instance = s->instance_create(this);
  766. } else if (Engine::get_singleton()->is_editor_hint()) {
  767. OBJ_DEBUG_LOCK
  768. script_instance = s->placeholder_instance_create(this);
  769. }
  770. }
  771. _change_notify(); //scripts may add variables, so refresh is desired
  772. emit_signal(CoreStringNames::get_singleton()->script_changed);
  773. }
  774. void Object::set_script_instance(ScriptInstance *p_instance) {
  775. if (script_instance == p_instance)
  776. return;
  777. if (script_instance)
  778. memdelete(script_instance);
  779. script_instance = p_instance;
  780. if (p_instance)
  781. script = p_instance->get_script();
  782. else
  783. script = Variant();
  784. }
  785. Variant Object::get_script() const {
  786. return script;
  787. }
  788. bool Object::has_meta(const String &p_name) const {
  789. return metadata.has(p_name);
  790. }
  791. void Object::set_meta(const String &p_name, const Variant &p_value) {
  792. if (p_value.get_type() == Variant::NIL) {
  793. metadata.erase(p_name);
  794. return;
  795. };
  796. metadata[p_name] = p_value;
  797. }
  798. Variant Object::get_meta(const String &p_name) const {
  799. ERR_FAIL_COND_V(!metadata.has(p_name), Variant());
  800. return metadata[p_name];
  801. }
  802. void Object::remove_meta(const String &p_name) {
  803. metadata.erase(p_name);
  804. }
  805. Array Object::_get_property_list_bind() const {
  806. List<PropertyInfo> lpi;
  807. get_property_list(&lpi);
  808. return convert_property_list(&lpi);
  809. }
  810. Array Object::_get_method_list_bind() const {
  811. List<MethodInfo> ml;
  812. get_method_list(&ml);
  813. Array ret;
  814. for (List<MethodInfo>::Element *E = ml.front(); E; E = E->next()) {
  815. Dictionary d = E->get();
  816. //va.push_back(d);
  817. ret.push_back(d);
  818. }
  819. return ret;
  820. }
  821. Vector<String> Object::_get_meta_list_bind() const {
  822. Vector<String> _metaret;
  823. List<Variant> keys;
  824. metadata.get_key_list(&keys);
  825. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  826. _metaret.push_back(E->get());
  827. }
  828. return _metaret;
  829. }
  830. void Object::get_meta_list(List<String> *p_list) const {
  831. List<Variant> keys;
  832. metadata.get_key_list(&keys);
  833. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  834. p_list->push_back(E->get());
  835. }
  836. }
  837. void Object::add_user_signal(const MethodInfo &p_signal) {
  838. ERR_FAIL_COND_MSG(p_signal.name == "", "Signal name cannot be empty.");
  839. 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() + "'.");
  840. ERR_FAIL_COND_MSG(signal_map.has(p_signal.name), "Trying to add already existing signal '" + p_signal.name + "'.");
  841. SignalData s;
  842. s.user = p_signal;
  843. signal_map[p_signal.name] = s;
  844. }
  845. bool Object::_has_user_signal(const StringName &p_name) const {
  846. if (!signal_map.has(p_name))
  847. return false;
  848. return signal_map[p_name].user.name.length() > 0;
  849. }
  850. struct _ObjectSignalDisconnectData {
  851. StringName signal;
  852. Callable callable;
  853. };
  854. Variant Object::_emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  855. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  856. ERR_FAIL_COND_V(p_argcount < 1, Variant());
  857. if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
  858. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  859. r_error.argument = 0;
  860. r_error.expected = Variant::STRING_NAME;
  861. ERR_FAIL_COND_V(p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING, Variant());
  862. }
  863. r_error.error = Callable::CallError::CALL_OK;
  864. StringName signal = *p_args[0];
  865. const Variant **args = nullptr;
  866. int argc = p_argcount - 1;
  867. if (argc) {
  868. args = &p_args[1];
  869. }
  870. emit_signal(signal, args, argc);
  871. return Variant();
  872. }
  873. Error Object::emit_signal(const StringName &p_name, const Variant **p_args, int p_argcount) {
  874. if (_block_signals)
  875. return ERR_CANT_ACQUIRE_RESOURCE; //no emit, signals blocked
  876. SignalData *s = signal_map.getptr(p_name);
  877. if (!s) {
  878. #ifdef DEBUG_ENABLED
  879. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_name);
  880. //check in script
  881. 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 + "\".");
  882. #endif
  883. //not connected? just return
  884. return ERR_UNAVAILABLE;
  885. }
  886. List<_ObjectSignalDisconnectData> disconnect_data;
  887. //copy on write will ensure that disconnecting the signal or even deleting the object will not affect the signal calling.
  888. //this happens automatically and will not change the performance of calling.
  889. //awesome, isn't it?
  890. VMap<Callable, SignalData::Slot> slot_map = s->slot_map;
  891. int ssize = slot_map.size();
  892. OBJ_DEBUG_LOCK
  893. Vector<const Variant *> bind_mem;
  894. Error err = OK;
  895. for (int i = 0; i < ssize; i++) {
  896. const Connection &c = slot_map.getv(i).conn;
  897. Object *target = c.callable.get_object();
  898. if (!target) {
  899. // Target might have been deleted during signal callback, this is expected and OK.
  900. continue;
  901. }
  902. const Variant **args = p_args;
  903. int argc = p_argcount;
  904. if (c.binds.size()) {
  905. //handle binds
  906. bind_mem.resize(p_argcount + c.binds.size());
  907. for (int j = 0; j < p_argcount; j++) {
  908. bind_mem.write[j] = p_args[j];
  909. }
  910. for (int j = 0; j < c.binds.size(); j++) {
  911. bind_mem.write[p_argcount + j] = &c.binds[j];
  912. }
  913. args = (const Variant **)bind_mem.ptr();
  914. argc = bind_mem.size();
  915. }
  916. if (c.flags & CONNECT_DEFERRED) {
  917. MessageQueue::get_singleton()->push_callable(c.callable, args, argc, true);
  918. } else {
  919. Callable::CallError ce;
  920. _emitting = true;
  921. Variant ret;
  922. c.callable.call(args, argc, ret, ce);
  923. _emitting = false;
  924. if (ce.error != Callable::CallError::CALL_OK) {
  925. #ifdef DEBUG_ENABLED
  926. if (c.flags & CONNECT_PERSIST && Engine::get_singleton()->is_editor_hint() && (script.is_null() || !Ref<Script>(script)->is_tool()))
  927. continue;
  928. #endif
  929. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD && !ClassDB::class_exists(target->get_class_name())) {
  930. //most likely object is not initialized yet, do not throw error.
  931. } else {
  932. ERR_PRINT("Error calling from signal '" + String(p_name) + "' to callable: " + Variant::get_callable_error_text(c.callable, args, argc, ce) + ".");
  933. err = ERR_METHOD_NOT_FOUND;
  934. }
  935. }
  936. }
  937. bool disconnect = c.flags & CONNECT_ONESHOT;
  938. #ifdef TOOLS_ENABLED
  939. if (disconnect && (c.flags & CONNECT_PERSIST) && Engine::get_singleton()->is_editor_hint()) {
  940. //this signal was connected from the editor, and is being edited. just don't disconnect for now
  941. disconnect = false;
  942. }
  943. #endif
  944. if (disconnect) {
  945. _ObjectSignalDisconnectData dd;
  946. dd.signal = p_name;
  947. dd.callable = c.callable;
  948. disconnect_data.push_back(dd);
  949. }
  950. }
  951. while (!disconnect_data.empty()) {
  952. const _ObjectSignalDisconnectData &dd = disconnect_data.front()->get();
  953. _disconnect(dd.signal, dd.callable);
  954. disconnect_data.pop_front();
  955. }
  956. return err;
  957. }
  958. Error Object::emit_signal(const StringName &p_name, VARIANT_ARG_DECLARE) {
  959. VARIANT_ARGPTRS;
  960. int argc = 0;
  961. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  962. if (argptr[i]->get_type() == Variant::NIL)
  963. break;
  964. argc++;
  965. }
  966. return emit_signal(p_name, argptr, argc);
  967. }
  968. void Object::_add_user_signal(const String &p_name, const Array &p_args) {
  969. // this version of add_user_signal is meant to be used from scripts or external apis
  970. // without access to ADD_SIGNAL in bind_methods
  971. // added events are per instance, as opposed to the other ones, which are global
  972. MethodInfo mi;
  973. mi.name = p_name;
  974. for (int i = 0; i < p_args.size(); i++) {
  975. Dictionary d = p_args[i];
  976. PropertyInfo param;
  977. if (d.has("name"))
  978. param.name = d["name"];
  979. if (d.has("type"))
  980. param.type = (Variant::Type)(int)d["type"];
  981. mi.arguments.push_back(param);
  982. }
  983. add_user_signal(mi);
  984. }
  985. Array Object::_get_signal_list() const {
  986. List<MethodInfo> signal_list;
  987. get_signal_list(&signal_list);
  988. Array ret;
  989. for (List<MethodInfo>::Element *E = signal_list.front(); E; E = E->next()) {
  990. ret.push_back(Dictionary(E->get()));
  991. }
  992. return ret;
  993. }
  994. Array Object::_get_signal_connection_list(const String &p_signal) const {
  995. List<Connection> conns;
  996. get_all_signal_connections(&conns);
  997. Array ret;
  998. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  999. Connection &c = E->get();
  1000. if (c.signal.get_name() == p_signal) {
  1001. ret.push_back(c);
  1002. }
  1003. }
  1004. return ret;
  1005. }
  1006. Array Object::_get_incoming_connections() const {
  1007. Array ret;
  1008. int connections_amount = connections.size();
  1009. for (int idx_conn = 0; idx_conn < connections_amount; idx_conn++) {
  1010. ret.push_back(connections[idx_conn]);
  1011. }
  1012. return ret;
  1013. }
  1014. bool Object::has_signal(const StringName &p_name) const {
  1015. if (!script.is_null()) {
  1016. Ref<Script> scr = script;
  1017. if (scr.is_valid() && scr->has_script_signal(p_name)) {
  1018. return true;
  1019. }
  1020. }
  1021. if (ClassDB::has_signal(get_class_name(), p_name)) {
  1022. return true;
  1023. }
  1024. if (_has_user_signal(p_name)) {
  1025. return true;
  1026. }
  1027. return false;
  1028. }
  1029. void Object::get_signal_list(List<MethodInfo> *p_signals) const {
  1030. if (!script.is_null()) {
  1031. Ref<Script> scr = script;
  1032. if (scr.is_valid()) {
  1033. scr->get_script_signal_list(p_signals);
  1034. }
  1035. }
  1036. ClassDB::get_signal_list(get_class_name(), p_signals);
  1037. //find maybe usersignals?
  1038. const StringName *S = nullptr;
  1039. while ((S = signal_map.next(S))) {
  1040. if (signal_map[*S].user.name != "") {
  1041. //user signal
  1042. p_signals->push_back(signal_map[*S].user);
  1043. }
  1044. }
  1045. }
  1046. void Object::get_all_signal_connections(List<Connection> *p_connections) const {
  1047. const StringName *S = nullptr;
  1048. while ((S = signal_map.next(S))) {
  1049. const SignalData *s = &signal_map[*S];
  1050. for (int i = 0; i < s->slot_map.size(); i++) {
  1051. p_connections->push_back(s->slot_map.getv(i).conn);
  1052. }
  1053. }
  1054. }
  1055. void Object::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
  1056. const SignalData *s = signal_map.getptr(p_signal);
  1057. if (!s)
  1058. return; //nothing
  1059. for (int i = 0; i < s->slot_map.size(); i++)
  1060. p_connections->push_back(s->slot_map.getv(i).conn);
  1061. }
  1062. int Object::get_persistent_signal_connection_count() const {
  1063. int count = 0;
  1064. const StringName *S = nullptr;
  1065. while ((S = signal_map.next(S))) {
  1066. const SignalData *s = &signal_map[*S];
  1067. for (int i = 0; i < s->slot_map.size(); i++) {
  1068. if (s->slot_map.getv(i).conn.flags & CONNECT_PERSIST) {
  1069. count += 1;
  1070. }
  1071. }
  1072. }
  1073. return count;
  1074. }
  1075. void Object::get_signals_connected_to_this(List<Connection> *p_connections) const {
  1076. for (const List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1077. p_connections->push_back(E->get());
  1078. }
  1079. }
  1080. Error Object::connect_compat(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method, const Vector<Variant> &p_binds, uint32_t p_flags) {
  1081. return connect(p_signal, Callable(p_to_object, p_to_method), p_binds, p_flags);
  1082. }
  1083. Error Object::connect(const StringName &p_signal, const Callable &p_callable, const Vector<Variant> &p_binds, uint32_t p_flags) {
  1084. ERR_FAIL_COND_V(p_callable.is_null(), ERR_INVALID_PARAMETER);
  1085. Object *target_object = p_callable.get_object();
  1086. ERR_FAIL_COND_V(!target_object, ERR_INVALID_PARAMETER);
  1087. SignalData *s = signal_map.getptr(p_signal);
  1088. if (!s) {
  1089. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1090. //check in script
  1091. if (!signal_is_valid && !script.is_null()) {
  1092. if (Ref<Script>(script)->has_script_signal(p_signal)) {
  1093. signal_is_valid = true;
  1094. }
  1095. #ifdef TOOLS_ENABLED
  1096. else {
  1097. //allow connecting signals anyway if script is invalid, see issue #17070
  1098. if (!Ref<Script>(script)->is_valid()) {
  1099. signal_is_valid = true;
  1100. }
  1101. }
  1102. #endif
  1103. }
  1104. 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 + "'.");
  1105. signal_map[p_signal] = SignalData();
  1106. s = &signal_map[p_signal];
  1107. }
  1108. Callable target = p_callable;
  1109. if (s->slot_map.has(target)) {
  1110. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1111. s->slot_map[target].reference_count++;
  1112. return OK;
  1113. } else {
  1114. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Signal '" + p_signal + "' is already connected to given callable '" + p_callable + "' in that object.");
  1115. }
  1116. }
  1117. SignalData::Slot slot;
  1118. Connection conn;
  1119. conn.callable = target;
  1120. conn.signal = ::Signal(this, p_signal);
  1121. conn.flags = p_flags;
  1122. conn.binds = p_binds;
  1123. slot.conn = conn;
  1124. slot.cE = target_object->connections.push_back(conn);
  1125. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1126. slot.reference_count = 1;
  1127. }
  1128. s->slot_map[target] = slot;
  1129. return OK;
  1130. }
  1131. bool Object::is_connected_compat(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method) const {
  1132. return is_connected(p_signal, Callable(p_to_object, p_to_method));
  1133. }
  1134. bool Object::is_connected(const StringName &p_signal, const Callable &p_callable) const {
  1135. ERR_FAIL_COND_V(p_callable.is_null(), false);
  1136. const SignalData *s = signal_map.getptr(p_signal);
  1137. if (!s) {
  1138. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1139. if (signal_is_valid)
  1140. return false;
  1141. if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal))
  1142. return false;
  1143. ERR_FAIL_V_MSG(false, "Nonexistent signal: " + p_signal + ".");
  1144. }
  1145. Callable target = p_callable;
  1146. return s->slot_map.has(target);
  1147. //const Map<Signal::Target,Signal::Slot>::Element *E = s->slot_map.find(target);
  1148. //return (E!=nullptr );
  1149. }
  1150. void Object::disconnect_compat(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method) {
  1151. _disconnect(p_signal, Callable(p_to_object, p_to_method));
  1152. }
  1153. void Object::disconnect(const StringName &p_signal, const Callable &p_callable) {
  1154. _disconnect(p_signal, p_callable);
  1155. }
  1156. void Object::_disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force) {
  1157. ERR_FAIL_COND(p_callable.is_null());
  1158. Object *target_object = p_callable.get_object();
  1159. ERR_FAIL_COND(!target_object);
  1160. SignalData *s = signal_map.getptr(p_signal);
  1161. ERR_FAIL_COND_MSG(!s, vformat("Nonexistent signal '%s' in %s.", p_signal, to_string()));
  1162. ERR_FAIL_COND_MSG(!s->slot_map.has(p_callable), "Disconnecting nonexistent signal '" + p_signal + "', callable: " + p_callable + ".");
  1163. SignalData::Slot *slot = &s->slot_map[p_callable];
  1164. if (!p_force) {
  1165. slot->reference_count--; // by default is zero, if it was not referenced it will go below it
  1166. if (slot->reference_count >= 0) {
  1167. return;
  1168. }
  1169. }
  1170. target_object->connections.erase(slot->cE);
  1171. s->slot_map.erase(p_callable);
  1172. if (s->slot_map.empty() && ClassDB::has_signal(get_class_name(), p_signal)) {
  1173. //not user signal, delete
  1174. signal_map.erase(p_signal);
  1175. }
  1176. }
  1177. void Object::_set_bind(const String &p_set, const Variant &p_value) {
  1178. set(p_set, p_value);
  1179. }
  1180. Variant Object::_get_bind(const String &p_name) const {
  1181. return get(p_name);
  1182. }
  1183. void Object::_set_indexed_bind(const NodePath &p_name, const Variant &p_value) {
  1184. set_indexed(p_name.get_as_property_path().get_subnames(), p_value);
  1185. }
  1186. Variant Object::_get_indexed_bind(const NodePath &p_name) const {
  1187. return get_indexed(p_name.get_as_property_path().get_subnames());
  1188. }
  1189. void Object::initialize_class() {
  1190. static bool initialized = false;
  1191. if (initialized)
  1192. return;
  1193. ClassDB::_add_class<Object>();
  1194. _bind_methods();
  1195. initialized = true;
  1196. }
  1197. StringName Object::tr(const StringName &p_message) const {
  1198. if (!_can_translate || !TranslationServer::get_singleton())
  1199. return p_message;
  1200. return TranslationServer::get_singleton()->translate(p_message);
  1201. }
  1202. void Object::_clear_internal_resource_paths(const Variant &p_var) {
  1203. switch (p_var.get_type()) {
  1204. case Variant::OBJECT: {
  1205. RES r = p_var;
  1206. if (!r.is_valid())
  1207. return;
  1208. if (!r->get_path().begins_with("res://") || r->get_path().find("::") == -1)
  1209. return; //not an internal resource
  1210. Object *object = p_var;
  1211. if (!object)
  1212. return;
  1213. r->set_path("");
  1214. r->clear_internal_resource_paths();
  1215. } break;
  1216. case Variant::ARRAY: {
  1217. Array a = p_var;
  1218. for (int i = 0; i < a.size(); i++) {
  1219. _clear_internal_resource_paths(a[i]);
  1220. }
  1221. } break;
  1222. case Variant::DICTIONARY: {
  1223. Dictionary d = p_var;
  1224. List<Variant> keys;
  1225. d.get_key_list(&keys);
  1226. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1227. _clear_internal_resource_paths(E->get());
  1228. _clear_internal_resource_paths(d[E->get()]);
  1229. }
  1230. } break;
  1231. default: {
  1232. }
  1233. }
  1234. }
  1235. #ifdef TOOLS_ENABLED
  1236. void Object::editor_set_section_unfold(const String &p_section, bool p_unfolded) {
  1237. set_edited(true);
  1238. if (p_unfolded)
  1239. editor_section_folding.insert(p_section);
  1240. else
  1241. editor_section_folding.erase(p_section);
  1242. }
  1243. bool Object::editor_is_section_unfolded(const String &p_section) {
  1244. return editor_section_folding.has(p_section);
  1245. }
  1246. #endif
  1247. void Object::clear_internal_resource_paths() {
  1248. List<PropertyInfo> pinfo;
  1249. get_property_list(&pinfo);
  1250. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1251. _clear_internal_resource_paths(get(E->get().name));
  1252. }
  1253. }
  1254. void Object::_bind_methods() {
  1255. ClassDB::bind_method(D_METHOD("get_class"), &Object::get_class);
  1256. ClassDB::bind_method(D_METHOD("is_class", "class"), &Object::is_class);
  1257. ClassDB::bind_method(D_METHOD("set", "property", "value"), &Object::_set_bind);
  1258. ClassDB::bind_method(D_METHOD("get", "property"), &Object::_get_bind);
  1259. ClassDB::bind_method(D_METHOD("set_indexed", "property", "value"), &Object::_set_indexed_bind);
  1260. ClassDB::bind_method(D_METHOD("get_indexed", "property"), &Object::_get_indexed_bind);
  1261. ClassDB::bind_method(D_METHOD("get_property_list"), &Object::_get_property_list_bind);
  1262. ClassDB::bind_method(D_METHOD("get_method_list"), &Object::_get_method_list_bind);
  1263. ClassDB::bind_method(D_METHOD("notification", "what", "reversed"), &Object::notification, DEFVAL(false));
  1264. ClassDB::bind_method(D_METHOD("to_string"), &Object::to_string);
  1265. ClassDB::bind_method(D_METHOD("get_instance_id"), &Object::get_instance_id);
  1266. ClassDB::bind_method(D_METHOD("set_script", "script"), &Object::set_script);
  1267. ClassDB::bind_method(D_METHOD("get_script"), &Object::get_script);
  1268. ClassDB::bind_method(D_METHOD("set_meta", "name", "value"), &Object::set_meta);
  1269. ClassDB::bind_method(D_METHOD("remove_meta", "name"), &Object::remove_meta);
  1270. ClassDB::bind_method(D_METHOD("get_meta", "name"), &Object::get_meta);
  1271. ClassDB::bind_method(D_METHOD("has_meta", "name"), &Object::has_meta);
  1272. ClassDB::bind_method(D_METHOD("get_meta_list"), &Object::_get_meta_list_bind);
  1273. ClassDB::bind_method(D_METHOD("add_user_signal", "signal", "arguments"), &Object::_add_user_signal, DEFVAL(Array()));
  1274. ClassDB::bind_method(D_METHOD("has_user_signal", "signal"), &Object::_has_user_signal);
  1275. {
  1276. MethodInfo mi;
  1277. mi.name = "emit_signal";
  1278. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "signal"));
  1279. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "emit_signal", &Object::_emit_signal, mi, varray(), false);
  1280. }
  1281. {
  1282. MethodInfo mi;
  1283. mi.name = "call";
  1284. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1285. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call", &Object::_call_bind, mi);
  1286. }
  1287. {
  1288. MethodInfo mi;
  1289. mi.name = "call_deferred";
  1290. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1291. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_deferred", &Object::_call_deferred_bind, mi, varray(), false);
  1292. }
  1293. ClassDB::bind_method(D_METHOD("set_deferred", "property", "value"), &Object::set_deferred);
  1294. ClassDB::bind_method(D_METHOD("callv", "method", "arg_array"), &Object::callv);
  1295. ClassDB::bind_method(D_METHOD("has_method", "method"), &Object::has_method);
  1296. ClassDB::bind_method(D_METHOD("has_signal", "signal"), &Object::has_signal);
  1297. ClassDB::bind_method(D_METHOD("get_signal_list"), &Object::_get_signal_list);
  1298. ClassDB::bind_method(D_METHOD("get_signal_connection_list", "signal"), &Object::_get_signal_connection_list);
  1299. ClassDB::bind_method(D_METHOD("get_incoming_connections"), &Object::_get_incoming_connections);
  1300. ClassDB::bind_method(D_METHOD("connect", "signal", "callable", "binds", "flags"), &Object::connect, DEFVAL(Array()), DEFVAL(0));
  1301. ClassDB::bind_method(D_METHOD("disconnect", "signal", "callable"), &Object::disconnect);
  1302. ClassDB::bind_method(D_METHOD("is_connected", "signal", "callable"), &Object::is_connected);
  1303. ClassDB::bind_method(D_METHOD("set_block_signals", "enable"), &Object::set_block_signals);
  1304. ClassDB::bind_method(D_METHOD("is_blocking_signals"), &Object::is_blocking_signals);
  1305. ClassDB::bind_method(D_METHOD("property_list_changed_notify"), &Object::property_list_changed_notify);
  1306. ClassDB::bind_method(D_METHOD("set_message_translation", "enable"), &Object::set_message_translation);
  1307. ClassDB::bind_method(D_METHOD("can_translate_messages"), &Object::can_translate_messages);
  1308. ClassDB::bind_method(D_METHOD("tr", "message"), &Object::tr);
  1309. ClassDB::bind_method(D_METHOD("is_queued_for_deletion"), &Object::is_queued_for_deletion);
  1310. ClassDB::add_virtual_method("Object", MethodInfo("free"), false);
  1311. ADD_SIGNAL(MethodInfo("script_changed"));
  1312. BIND_VMETHOD(MethodInfo("_notification", PropertyInfo(Variant::INT, "what")));
  1313. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_set", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value")));
  1314. #ifdef TOOLS_ENABLED
  1315. MethodInfo miget("_get", PropertyInfo(Variant::STRING_NAME, "property"));
  1316. miget.return_val.name = "Variant";
  1317. miget.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1318. BIND_VMETHOD(miget);
  1319. MethodInfo plget("_get_property_list");
  1320. plget.return_val.type = Variant::ARRAY;
  1321. BIND_VMETHOD(plget);
  1322. #endif
  1323. BIND_VMETHOD(MethodInfo("_init"));
  1324. BIND_VMETHOD(MethodInfo(Variant::STRING, "_to_string"));
  1325. BIND_CONSTANT(NOTIFICATION_POSTINITIALIZE);
  1326. BIND_CONSTANT(NOTIFICATION_PREDELETE);
  1327. BIND_ENUM_CONSTANT(CONNECT_DEFERRED);
  1328. BIND_ENUM_CONSTANT(CONNECT_PERSIST);
  1329. BIND_ENUM_CONSTANT(CONNECT_ONESHOT);
  1330. BIND_ENUM_CONSTANT(CONNECT_REFERENCE_COUNTED);
  1331. }
  1332. void Object::call_deferred(const StringName &p_method, VARIANT_ARG_DECLARE) {
  1333. MessageQueue::get_singleton()->push_call(this, p_method, VARIANT_ARG_PASS);
  1334. }
  1335. void Object::set_deferred(const StringName &p_property, const Variant &p_value) {
  1336. MessageQueue::get_singleton()->push_set(this, p_property, p_value);
  1337. }
  1338. void Object::set_block_signals(bool p_block) {
  1339. _block_signals = p_block;
  1340. }
  1341. bool Object::is_blocking_signals() const {
  1342. return _block_signals;
  1343. }
  1344. void Object::get_translatable_strings(List<String> *p_strings) const {
  1345. List<PropertyInfo> plist;
  1346. get_property_list(&plist);
  1347. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1348. if (!(E->get().usage & PROPERTY_USAGE_INTERNATIONALIZED))
  1349. continue;
  1350. String text = get(E->get().name);
  1351. if (text == "")
  1352. continue;
  1353. p_strings->push_back(text);
  1354. }
  1355. }
  1356. Variant::Type Object::get_static_property_type(const StringName &p_property, bool *r_valid) const {
  1357. bool valid;
  1358. Variant::Type t = ClassDB::get_property_type(get_class_name(), p_property, &valid);
  1359. if (valid) {
  1360. if (r_valid)
  1361. *r_valid = true;
  1362. return t;
  1363. }
  1364. if (get_script_instance()) {
  1365. return get_script_instance()->get_property_type(p_property, r_valid);
  1366. }
  1367. if (r_valid)
  1368. *r_valid = false;
  1369. return Variant::NIL;
  1370. }
  1371. Variant::Type Object::get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid) const {
  1372. if (p_path.size() == 0) {
  1373. if (r_valid)
  1374. *r_valid = false;
  1375. return Variant::NIL;
  1376. }
  1377. bool valid = false;
  1378. Variant::Type t = get_static_property_type(p_path[0], &valid);
  1379. if (!valid) {
  1380. if (r_valid)
  1381. *r_valid = false;
  1382. return Variant::NIL;
  1383. }
  1384. Callable::CallError ce;
  1385. Variant check = Variant::construct(t, nullptr, 0, ce);
  1386. for (int i = 1; i < p_path.size(); i++) {
  1387. if (check.get_type() == Variant::OBJECT || check.get_type() == Variant::DICTIONARY || check.get_type() == Variant::ARRAY) {
  1388. // We cannot be sure about the type of properties this types can have
  1389. if (r_valid)
  1390. *r_valid = false;
  1391. return Variant::NIL;
  1392. }
  1393. check = check.get_named(p_path[i], &valid);
  1394. if (!valid) {
  1395. if (r_valid)
  1396. *r_valid = false;
  1397. return Variant::NIL;
  1398. }
  1399. }
  1400. if (r_valid)
  1401. *r_valid = true;
  1402. return check.get_type();
  1403. }
  1404. bool Object::is_queued_for_deletion() const {
  1405. return _is_queued_for_deletion;
  1406. }
  1407. #ifdef TOOLS_ENABLED
  1408. void Object::set_edited(bool p_edited) {
  1409. _edited = p_edited;
  1410. _edited_version++;
  1411. }
  1412. bool Object::is_edited() const {
  1413. return _edited;
  1414. }
  1415. uint32_t Object::get_edited_version() const {
  1416. return _edited_version;
  1417. }
  1418. #endif
  1419. void *Object::get_script_instance_binding(int p_script_language_index) {
  1420. #ifdef DEBUG_ENABLED
  1421. ERR_FAIL_INDEX_V(p_script_language_index, MAX_SCRIPT_INSTANCE_BINDINGS, nullptr);
  1422. #endif
  1423. //it's up to the script language to make this thread safe, if the function is called twice due to threads being out of syncro
  1424. //just return the same pointer.
  1425. //if you want to put a big lock in the entire function and keep allocated pointers in a map or something, feel free to do it
  1426. //as it should not really affect performance much (won't be called too often), as in far most caes the condition below will be false afterwards
  1427. if (!_script_instance_bindings[p_script_language_index]) {
  1428. void *script_data = ScriptServer::get_language(p_script_language_index)->alloc_instance_binding_data(this);
  1429. if (script_data) {
  1430. atomic_increment(&instance_binding_count);
  1431. _script_instance_bindings[p_script_language_index] = script_data;
  1432. }
  1433. }
  1434. return _script_instance_bindings[p_script_language_index];
  1435. }
  1436. bool Object::has_script_instance_binding(int p_script_language_index) {
  1437. return _script_instance_bindings[p_script_language_index] != nullptr;
  1438. }
  1439. void Object::set_script_instance_binding(int p_script_language_index, void *p_data) {
  1440. #ifdef DEBUG_ENABLED
  1441. CRASH_COND(_script_instance_bindings[p_script_language_index] != nullptr);
  1442. #endif
  1443. _script_instance_bindings[p_script_language_index] = p_data;
  1444. }
  1445. void Object::_construct_object(bool p_reference) {
  1446. type_is_reference = p_reference;
  1447. _instance_id = ObjectDB::add_instance(this);
  1448. memset(_script_instance_bindings, 0, sizeof(void *) * MAX_SCRIPT_INSTANCE_BINDINGS);
  1449. #ifdef DEBUG_ENABLED
  1450. _lock_index.init(1);
  1451. #endif
  1452. }
  1453. Object::Object(bool p_reference) {
  1454. _construct_object(p_reference);
  1455. }
  1456. Object::Object() {
  1457. _construct_object(false);
  1458. }
  1459. Object::~Object() {
  1460. if (script_instance)
  1461. memdelete(script_instance);
  1462. script_instance = nullptr;
  1463. const StringName *S = nullptr;
  1464. if (_emitting) {
  1465. //@todo this may need to actually reach the debugger prioritarily somehow because it may crash before
  1466. 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.");
  1467. }
  1468. while ((S = signal_map.next(nullptr))) {
  1469. SignalData *s = &signal_map[*S];
  1470. //brute force disconnect for performance
  1471. int slot_count = s->slot_map.size();
  1472. const VMap<Callable, SignalData::Slot>::Pair *slot_list = s->slot_map.get_array();
  1473. for (int i = 0; i < slot_count; i++) {
  1474. slot_list[i].value.conn.callable.get_object()->connections.erase(slot_list[i].value.cE);
  1475. }
  1476. signal_map.erase(*S);
  1477. }
  1478. //signals from nodes that connect to this node
  1479. while (connections.size()) {
  1480. Connection c = connections.front()->get();
  1481. c.signal.get_object()->_disconnect(c.signal.get_name(), c.callable, true);
  1482. }
  1483. ObjectDB::remove_instance(this);
  1484. _instance_id = ObjectID();
  1485. _predelete_ok = 2;
  1486. if (!ScriptServer::are_languages_finished()) {
  1487. for (int i = 0; i < MAX_SCRIPT_INSTANCE_BINDINGS; i++) {
  1488. if (_script_instance_bindings[i]) {
  1489. ScriptServer::get_language(i)->free_instance_binding_data(_script_instance_bindings[i]);
  1490. }
  1491. }
  1492. }
  1493. }
  1494. bool predelete_handler(Object *p_object) {
  1495. return p_object->_predelete();
  1496. }
  1497. void postinitialize_handler(Object *p_object) {
  1498. p_object->_postinitialize();
  1499. }
  1500. void ObjectDB::debug_objects(DebugFunc p_func) {
  1501. spin_lock.lock();
  1502. for (uint32_t i = 0; i < slot_count; i++) {
  1503. uint32_t slot = object_slots[i].next_free;
  1504. p_func(object_slots[slot].object);
  1505. }
  1506. spin_lock.unlock();
  1507. }
  1508. void Object::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1509. }
  1510. SpinLock ObjectDB::spin_lock;
  1511. uint32_t ObjectDB::slot_count = 0;
  1512. uint32_t ObjectDB::slot_max = 0;
  1513. ObjectDB::ObjectSlot *ObjectDB::object_slots = nullptr;
  1514. uint64_t ObjectDB::validator_counter = 0;
  1515. int ObjectDB::get_object_count() {
  1516. return slot_count;
  1517. }
  1518. ObjectID ObjectDB::add_instance(Object *p_object) {
  1519. spin_lock.lock();
  1520. if (unlikely(slot_count == slot_max)) {
  1521. CRASH_COND(slot_count == (1 << OBJECTDB_SLOT_MAX_COUNT_BITS));
  1522. uint32_t new_slot_max = slot_max > 0 ? slot_max * 2 : 1;
  1523. object_slots = (ObjectSlot *)memrealloc(object_slots, sizeof(ObjectSlot) * new_slot_max);
  1524. for (uint32_t i = slot_max; i < new_slot_max; i++) {
  1525. object_slots[i].object = nullptr;
  1526. object_slots[i].is_reference = false;
  1527. object_slots[i].next_free = i;
  1528. object_slots[i].validator = 0;
  1529. }
  1530. slot_max = new_slot_max;
  1531. }
  1532. uint32_t slot = object_slots[slot_count].next_free;
  1533. if (object_slots[slot].object != nullptr) {
  1534. spin_lock.unlock();
  1535. ERR_FAIL_COND_V(object_slots[slot].object != nullptr, ObjectID());
  1536. }
  1537. object_slots[slot].object = p_object;
  1538. object_slots[slot].is_reference = p_object->is_reference();
  1539. validator_counter = (validator_counter + 1) & OBJECTDB_VALIDATOR_MASK;
  1540. if (unlikely(validator_counter == 0)) {
  1541. validator_counter = 1;
  1542. }
  1543. object_slots[slot].validator = validator_counter;
  1544. uint64_t id = validator_counter;
  1545. id <<= OBJECTDB_SLOT_MAX_COUNT_BITS;
  1546. id |= uint64_t(slot);
  1547. if (p_object->is_reference()) {
  1548. id |= OBJECTDB_REFERENCE_BIT;
  1549. }
  1550. slot_count++;
  1551. spin_lock.unlock();
  1552. return ObjectID(id);
  1553. }
  1554. void ObjectDB::remove_instance(Object *p_object) {
  1555. uint64_t t = p_object->get_instance_id();
  1556. uint32_t slot = t & OBJECTDB_SLOT_MAX_COUNT_MASK; //slot is always valid on valid object
  1557. spin_lock.lock();
  1558. #ifdef DEBUG_ENABLED
  1559. if (object_slots[slot].object != p_object) {
  1560. spin_lock.unlock();
  1561. ERR_FAIL_COND(object_slots[slot].object != p_object);
  1562. }
  1563. {
  1564. uint64_t validator = (t >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
  1565. if (object_slots[slot].validator != validator) {
  1566. spin_lock.unlock();
  1567. ERR_FAIL_COND(object_slots[slot].validator != validator);
  1568. }
  1569. }
  1570. #endif
  1571. //decrease slot count
  1572. slot_count--;
  1573. //set the free slot properly
  1574. object_slots[slot_count].next_free = slot;
  1575. //invalidate, so checks against it fail
  1576. object_slots[slot].validator = 0;
  1577. object_slots[slot].is_reference = false;
  1578. object_slots[slot].object = nullptr;
  1579. spin_lock.unlock();
  1580. }
  1581. void ObjectDB::setup() {
  1582. //nothing to do now
  1583. }
  1584. void ObjectDB::cleanup() {
  1585. if (slot_count > 0) {
  1586. spin_lock.lock();
  1587. WARN_PRINT("ObjectDB Instances still exist!");
  1588. if (OS::get_singleton()->is_stdout_verbose()) {
  1589. for (uint32_t i = 0; i < slot_count; i++) {
  1590. uint32_t slot = object_slots[i].next_free;
  1591. Object *obj = object_slots[slot].object;
  1592. String node_name;
  1593. if (obj->is_class("Node"))
  1594. node_name = " - Node name: " + String(obj->call("get_name"));
  1595. if (obj->is_class("Resource"))
  1596. node_name = " - Resource name: " + String(obj->call("get_name")) + " Path: " + String(obj->call("get_path"));
  1597. uint64_t id = uint64_t(slot) | (uint64_t(object_slots[slot].validator) << OBJECTDB_VALIDATOR_BITS) | (object_slots[slot].is_reference ? OBJECTDB_REFERENCE_BIT : 0);
  1598. print_line("Leaked instance: " + String(obj->get_class()) + ":" + itos(id) + node_name);
  1599. }
  1600. }
  1601. spin_lock.unlock();
  1602. }
  1603. if (object_slots) {
  1604. memfree(object_slots);
  1605. }
  1606. }