object.cpp 59 KB

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