variant.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070
  1. /*************************************************************************/
  2. /* variant.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 "variant.h"
  31. #include "core/core_string_names.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/math/math_funcs.h"
  34. #include "core/object_rc.h"
  35. #include "core/print_string.h"
  36. #include "core/resource.h"
  37. #include "core/variant_parser.h"
  38. #include "scene/gui/control.h"
  39. #include "scene/main/node.h"
  40. String Variant::get_type_name(Variant::Type p_type) {
  41. switch (p_type) {
  42. case NIL: {
  43. return "Nil";
  44. } break;
  45. // atomic types
  46. case BOOL: {
  47. return "bool";
  48. } break;
  49. case INT: {
  50. return "int";
  51. } break;
  52. case REAL: {
  53. return "float";
  54. } break;
  55. case STRING: {
  56. return "String";
  57. } break;
  58. // math types
  59. case VECTOR2: {
  60. return "Vector2";
  61. } break;
  62. case RECT2: {
  63. return "Rect2";
  64. } break;
  65. case TRANSFORM2D: {
  66. return "Transform2D";
  67. } break;
  68. case VECTOR3: {
  69. return "Vector3";
  70. } break;
  71. case PLANE: {
  72. return "Plane";
  73. } break;
  74. /*
  75. case QUAT: {
  76. } break;*/
  77. case AABB: {
  78. return "AABB";
  79. } break;
  80. case QUAT: {
  81. return "Quat";
  82. } break;
  83. case BASIS: {
  84. return "Basis";
  85. } break;
  86. case TRANSFORM: {
  87. return "Transform";
  88. } break;
  89. // misc types
  90. case COLOR: {
  91. return "Color";
  92. } break;
  93. case _RID: {
  94. return "RID";
  95. } break;
  96. case OBJECT: {
  97. return "Object";
  98. } break;
  99. case NODE_PATH: {
  100. return "NodePath";
  101. } break;
  102. case DICTIONARY: {
  103. return "Dictionary";
  104. } break;
  105. case ARRAY: {
  106. return "Array";
  107. } break;
  108. // arrays
  109. case POOL_BYTE_ARRAY: {
  110. return "PoolByteArray";
  111. } break;
  112. case POOL_INT_ARRAY: {
  113. return "PoolIntArray";
  114. } break;
  115. case POOL_REAL_ARRAY: {
  116. return "PoolRealArray";
  117. } break;
  118. case POOL_STRING_ARRAY: {
  119. return "PoolStringArray";
  120. } break;
  121. case POOL_VECTOR2_ARRAY: {
  122. return "PoolVector2Array";
  123. } break;
  124. case POOL_VECTOR3_ARRAY: {
  125. return "PoolVector3Array";
  126. } break;
  127. case POOL_COLOR_ARRAY: {
  128. return "PoolColorArray";
  129. } break;
  130. default: {
  131. }
  132. }
  133. return "";
  134. }
  135. bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {
  136. if (p_type_from == p_type_to) {
  137. return true;
  138. }
  139. if (p_type_to == NIL && p_type_from != NIL) { //nil can convert to anything
  140. return true;
  141. }
  142. if (p_type_from == NIL) {
  143. return (p_type_to == OBJECT);
  144. };
  145. const Type *valid_types = nullptr;
  146. const Type *invalid_types = nullptr;
  147. switch (p_type_to) {
  148. case BOOL: {
  149. static const Type valid[] = {
  150. INT,
  151. REAL,
  152. STRING,
  153. NIL,
  154. };
  155. valid_types = valid;
  156. } break;
  157. case INT: {
  158. static const Type valid[] = {
  159. BOOL,
  160. REAL,
  161. STRING,
  162. NIL,
  163. };
  164. valid_types = valid;
  165. } break;
  166. case REAL: {
  167. static const Type valid[] = {
  168. BOOL,
  169. INT,
  170. STRING,
  171. NIL,
  172. };
  173. valid_types = valid;
  174. } break;
  175. case STRING: {
  176. static const Type invalid[] = {
  177. OBJECT,
  178. NIL
  179. };
  180. invalid_types = invalid;
  181. } break;
  182. case TRANSFORM2D: {
  183. static const Type valid[] = {
  184. TRANSFORM,
  185. NIL
  186. };
  187. valid_types = valid;
  188. } break;
  189. case QUAT: {
  190. static const Type valid[] = {
  191. BASIS,
  192. NIL
  193. };
  194. valid_types = valid;
  195. } break;
  196. case BASIS: {
  197. static const Type valid[] = {
  198. QUAT,
  199. VECTOR3,
  200. NIL
  201. };
  202. valid_types = valid;
  203. } break;
  204. case TRANSFORM: {
  205. static const Type valid[] = {
  206. TRANSFORM2D,
  207. QUAT,
  208. BASIS,
  209. NIL
  210. };
  211. valid_types = valid;
  212. } break;
  213. case COLOR: {
  214. static const Type valid[] = {
  215. STRING,
  216. INT,
  217. NIL,
  218. };
  219. valid_types = valid;
  220. } break;
  221. case _RID: {
  222. static const Type valid[] = {
  223. OBJECT,
  224. NIL
  225. };
  226. valid_types = valid;
  227. } break;
  228. case OBJECT: {
  229. static const Type valid[] = {
  230. NIL
  231. };
  232. valid_types = valid;
  233. } break;
  234. case NODE_PATH: {
  235. static const Type valid[] = {
  236. STRING,
  237. NIL
  238. };
  239. valid_types = valid;
  240. } break;
  241. case ARRAY: {
  242. static const Type valid[] = {
  243. POOL_BYTE_ARRAY,
  244. POOL_INT_ARRAY,
  245. POOL_STRING_ARRAY,
  246. POOL_REAL_ARRAY,
  247. POOL_COLOR_ARRAY,
  248. POOL_VECTOR2_ARRAY,
  249. POOL_VECTOR3_ARRAY,
  250. NIL
  251. };
  252. valid_types = valid;
  253. } break;
  254. // arrays
  255. case POOL_BYTE_ARRAY: {
  256. static const Type valid[] = {
  257. ARRAY,
  258. NIL
  259. };
  260. valid_types = valid;
  261. } break;
  262. case POOL_INT_ARRAY: {
  263. static const Type valid[] = {
  264. ARRAY,
  265. NIL
  266. };
  267. valid_types = valid;
  268. } break;
  269. case POOL_REAL_ARRAY: {
  270. static const Type valid[] = {
  271. ARRAY,
  272. NIL
  273. };
  274. valid_types = valid;
  275. } break;
  276. case POOL_STRING_ARRAY: {
  277. static const Type valid[] = {
  278. ARRAY,
  279. NIL
  280. };
  281. valid_types = valid;
  282. } break;
  283. case POOL_VECTOR2_ARRAY: {
  284. static const Type valid[] = {
  285. ARRAY,
  286. NIL
  287. };
  288. valid_types = valid;
  289. } break;
  290. case POOL_VECTOR3_ARRAY: {
  291. static const Type valid[] = {
  292. ARRAY,
  293. NIL
  294. };
  295. valid_types = valid;
  296. } break;
  297. case POOL_COLOR_ARRAY: {
  298. static const Type valid[] = {
  299. ARRAY,
  300. NIL
  301. };
  302. valid_types = valid;
  303. } break;
  304. default: {
  305. }
  306. }
  307. if (valid_types) {
  308. int i = 0;
  309. while (valid_types[i] != NIL) {
  310. if (p_type_from == valid_types[i]) {
  311. return true;
  312. }
  313. i++;
  314. }
  315. } else if (invalid_types) {
  316. int i = 0;
  317. while (invalid_types[i] != NIL) {
  318. if (p_type_from == invalid_types[i]) {
  319. return false;
  320. }
  321. i++;
  322. }
  323. return true;
  324. }
  325. return false;
  326. }
  327. bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type_to) {
  328. if (p_type_from == p_type_to) {
  329. return true;
  330. }
  331. if (p_type_to == NIL && p_type_from != NIL) { //nil can convert to anything
  332. return true;
  333. }
  334. if (p_type_from == NIL) {
  335. return (p_type_to == OBJECT);
  336. };
  337. const Type *valid_types = nullptr;
  338. switch (p_type_to) {
  339. case BOOL: {
  340. static const Type valid[] = {
  341. INT,
  342. REAL,
  343. //STRING,
  344. NIL,
  345. };
  346. valid_types = valid;
  347. } break;
  348. case INT: {
  349. static const Type valid[] = {
  350. BOOL,
  351. REAL,
  352. //STRING,
  353. NIL,
  354. };
  355. valid_types = valid;
  356. } break;
  357. case REAL: {
  358. static const Type valid[] = {
  359. BOOL,
  360. INT,
  361. //STRING,
  362. NIL,
  363. };
  364. valid_types = valid;
  365. } break;
  366. case STRING: {
  367. static const Type valid[] = {
  368. NODE_PATH,
  369. NIL
  370. };
  371. valid_types = valid;
  372. } break;
  373. case TRANSFORM2D: {
  374. static const Type valid[] = {
  375. TRANSFORM,
  376. NIL
  377. };
  378. valid_types = valid;
  379. } break;
  380. case QUAT: {
  381. static const Type valid[] = {
  382. BASIS,
  383. NIL
  384. };
  385. valid_types = valid;
  386. } break;
  387. case BASIS: {
  388. static const Type valid[] = {
  389. QUAT,
  390. VECTOR3,
  391. NIL
  392. };
  393. valid_types = valid;
  394. } break;
  395. case TRANSFORM: {
  396. static const Type valid[] = {
  397. TRANSFORM2D,
  398. QUAT,
  399. BASIS,
  400. NIL
  401. };
  402. valid_types = valid;
  403. } break;
  404. case COLOR: {
  405. static const Type valid[] = {
  406. STRING,
  407. INT,
  408. NIL,
  409. };
  410. valid_types = valid;
  411. } break;
  412. case _RID: {
  413. static const Type valid[] = {
  414. OBJECT,
  415. NIL
  416. };
  417. valid_types = valid;
  418. } break;
  419. case OBJECT: {
  420. static const Type valid[] = {
  421. NIL
  422. };
  423. valid_types = valid;
  424. } break;
  425. case NODE_PATH: {
  426. static const Type valid[] = {
  427. STRING,
  428. NIL
  429. };
  430. valid_types = valid;
  431. } break;
  432. case ARRAY: {
  433. static const Type valid[] = {
  434. POOL_BYTE_ARRAY,
  435. POOL_INT_ARRAY,
  436. POOL_STRING_ARRAY,
  437. POOL_REAL_ARRAY,
  438. POOL_COLOR_ARRAY,
  439. POOL_VECTOR2_ARRAY,
  440. POOL_VECTOR3_ARRAY,
  441. NIL
  442. };
  443. valid_types = valid;
  444. } break;
  445. // arrays
  446. case POOL_BYTE_ARRAY: {
  447. static const Type valid[] = {
  448. ARRAY,
  449. NIL
  450. };
  451. valid_types = valid;
  452. } break;
  453. case POOL_INT_ARRAY: {
  454. static const Type valid[] = {
  455. ARRAY,
  456. NIL
  457. };
  458. valid_types = valid;
  459. } break;
  460. case POOL_REAL_ARRAY: {
  461. static const Type valid[] = {
  462. ARRAY,
  463. NIL
  464. };
  465. valid_types = valid;
  466. } break;
  467. case POOL_STRING_ARRAY: {
  468. static const Type valid[] = {
  469. ARRAY,
  470. NIL
  471. };
  472. valid_types = valid;
  473. } break;
  474. case POOL_VECTOR2_ARRAY: {
  475. static const Type valid[] = {
  476. ARRAY,
  477. NIL
  478. };
  479. valid_types = valid;
  480. } break;
  481. case POOL_VECTOR3_ARRAY: {
  482. static const Type valid[] = {
  483. ARRAY,
  484. NIL
  485. };
  486. valid_types = valid;
  487. } break;
  488. case POOL_COLOR_ARRAY: {
  489. static const Type valid[] = {
  490. ARRAY,
  491. NIL
  492. };
  493. valid_types = valid;
  494. } break;
  495. default: {
  496. }
  497. }
  498. if (valid_types) {
  499. int i = 0;
  500. while (valid_types[i] != NIL) {
  501. if (p_type_from == valid_types[i]) {
  502. return true;
  503. }
  504. i++;
  505. }
  506. }
  507. return false;
  508. }
  509. bool Variant::operator==(const Variant &p_variant) const {
  510. if (type != p_variant.type) { //evaluation of operator== needs to be more strict
  511. return false;
  512. }
  513. bool v;
  514. Variant r;
  515. evaluate(OP_EQUAL, *this, p_variant, r, v);
  516. return r;
  517. }
  518. bool Variant::operator!=(const Variant &p_variant) const {
  519. if (type != p_variant.type) { //evaluation of operator== needs to be more strict
  520. return true;
  521. }
  522. bool v;
  523. Variant r;
  524. evaluate(OP_NOT_EQUAL, *this, p_variant, r, v);
  525. return r;
  526. }
  527. bool Variant::operator<(const Variant &p_variant) const {
  528. if (type != p_variant.type) { //if types differ, then order by type first
  529. return type < p_variant.type;
  530. }
  531. bool v;
  532. Variant r;
  533. evaluate(OP_LESS, *this, p_variant, r, v);
  534. return r;
  535. }
  536. bool Variant::is_zero() const {
  537. switch (type) {
  538. case NIL: {
  539. return true;
  540. } break;
  541. // atomic types
  542. case BOOL: {
  543. return !(_data._bool);
  544. } break;
  545. case INT: {
  546. return _data._int == 0;
  547. } break;
  548. case REAL: {
  549. return _data._real == 0;
  550. } break;
  551. case STRING: {
  552. return *reinterpret_cast<const String *>(_data._mem) == String();
  553. } break;
  554. // math types
  555. case VECTOR2: {
  556. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2();
  557. } break;
  558. case RECT2: {
  559. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2();
  560. } break;
  561. case TRANSFORM2D: {
  562. return *_data._transform2d == Transform2D();
  563. } break;
  564. case VECTOR3: {
  565. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3();
  566. } break;
  567. case PLANE: {
  568. return *reinterpret_cast<const Plane *>(_data._mem) == Plane();
  569. } break;
  570. /*
  571. case QUAT: {
  572. } break;*/
  573. case AABB: {
  574. return *_data._aabb == ::AABB();
  575. } break;
  576. case QUAT: {
  577. return *reinterpret_cast<const Quat *>(_data._mem) == Quat();
  578. } break;
  579. case BASIS: {
  580. return *_data._basis == Basis();
  581. } break;
  582. case TRANSFORM: {
  583. return *_data._transform == Transform();
  584. } break;
  585. // misc types
  586. case COLOR: {
  587. return *reinterpret_cast<const Color *>(_data._mem) == Color();
  588. } break;
  589. case _RID: {
  590. return *reinterpret_cast<const RID *>(_data._mem) == RID();
  591. } break;
  592. case OBJECT: {
  593. return _UNSAFE_OBJ_PROXY_PTR(*this) == nullptr;
  594. } break;
  595. case NODE_PATH: {
  596. return reinterpret_cast<const NodePath *>(_data._mem)->is_empty();
  597. } break;
  598. case DICTIONARY: {
  599. return reinterpret_cast<const Dictionary *>(_data._mem)->empty();
  600. } break;
  601. case ARRAY: {
  602. return reinterpret_cast<const Array *>(_data._mem)->empty();
  603. } break;
  604. // arrays
  605. case POOL_BYTE_ARRAY: {
  606. return reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem)->size() == 0;
  607. } break;
  608. case POOL_INT_ARRAY: {
  609. return reinterpret_cast<const PoolVector<int> *>(_data._mem)->size() == 0;
  610. } break;
  611. case POOL_REAL_ARRAY: {
  612. return reinterpret_cast<const PoolVector<real_t> *>(_data._mem)->size() == 0;
  613. } break;
  614. case POOL_STRING_ARRAY: {
  615. return reinterpret_cast<const PoolVector<String> *>(_data._mem)->size() == 0;
  616. } break;
  617. case POOL_VECTOR2_ARRAY: {
  618. return reinterpret_cast<const PoolVector<Vector2> *>(_data._mem)->size() == 0;
  619. } break;
  620. case POOL_VECTOR3_ARRAY: {
  621. return reinterpret_cast<const PoolVector<Vector3> *>(_data._mem)->size() == 0;
  622. } break;
  623. case POOL_COLOR_ARRAY: {
  624. return reinterpret_cast<const PoolVector<Color> *>(_data._mem)->size() == 0;
  625. } break;
  626. default: {
  627. }
  628. }
  629. return false;
  630. }
  631. bool Variant::is_one() const {
  632. switch (type) {
  633. case NIL: {
  634. return true;
  635. } break;
  636. // atomic types
  637. case BOOL: {
  638. return _data._bool;
  639. } break;
  640. case INT: {
  641. return _data._int == 1;
  642. } break;
  643. case REAL: {
  644. return _data._real == 1;
  645. } break;
  646. case VECTOR2: {
  647. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(1, 1);
  648. } break;
  649. case RECT2: {
  650. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(1, 1, 1, 1);
  651. } break;
  652. case VECTOR3: {
  653. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(1, 1, 1);
  654. } break;
  655. case PLANE: {
  656. return *reinterpret_cast<const Plane *>(_data._mem) == Plane(1, 1, 1, 1);
  657. } break;
  658. case COLOR: {
  659. return *reinterpret_cast<const Color *>(_data._mem) == Color(1, 1, 1, 1);
  660. } break;
  661. default: {
  662. return !is_zero();
  663. }
  664. }
  665. return false;
  666. }
  667. ObjectID Variant::get_object_instance_id() const {
  668. if (unlikely(type != OBJECT)) {
  669. return 0;
  670. } else if (likely(_get_obj().rc)) {
  671. return _get_obj().rc->instance_id;
  672. } else if (likely(!_get_obj().ref.is_null())) {
  673. return _REF_OBJ_PTR(*this)->get_instance_id();
  674. } else {
  675. return 0;
  676. }
  677. }
  678. bool Variant::is_invalid_object() const {
  679. return type == OBJECT && _get_obj().rc && !_get_obj().rc->get_ptr();
  680. }
  681. void Variant::reference(const Variant &p_variant) {
  682. switch (type) {
  683. case NIL:
  684. case BOOL:
  685. case INT:
  686. case REAL:
  687. break;
  688. default:
  689. clear();
  690. }
  691. type = p_variant.type;
  692. switch (p_variant.type) {
  693. case NIL: {
  694. // none
  695. } break;
  696. // atomic types
  697. case BOOL: {
  698. _data._bool = p_variant._data._bool;
  699. } break;
  700. case INT: {
  701. _data._int = p_variant._data._int;
  702. } break;
  703. case REAL: {
  704. _data._real = p_variant._data._real;
  705. } break;
  706. case STRING: {
  707. memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem)));
  708. } break;
  709. // math types
  710. case VECTOR2: {
  711. memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem)));
  712. } break;
  713. case RECT2: {
  714. memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem)));
  715. } break;
  716. case TRANSFORM2D: {
  717. _data._transform2d = memnew(Transform2D(*p_variant._data._transform2d));
  718. } break;
  719. case VECTOR3: {
  720. memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(p_variant._data._mem)));
  721. } break;
  722. case PLANE: {
  723. memnew_placement(_data._mem, Plane(*reinterpret_cast<const Plane *>(p_variant._data._mem)));
  724. } break;
  725. case AABB: {
  726. _data._aabb = memnew(::AABB(*p_variant._data._aabb));
  727. } break;
  728. case QUAT: {
  729. memnew_placement(_data._mem, Quat(*reinterpret_cast<const Quat *>(p_variant._data._mem)));
  730. } break;
  731. case BASIS: {
  732. _data._basis = memnew(Basis(*p_variant._data._basis));
  733. } break;
  734. case TRANSFORM: {
  735. _data._transform = memnew(Transform(*p_variant._data._transform));
  736. } break;
  737. // misc types
  738. case COLOR: {
  739. memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem)));
  740. } break;
  741. case _RID: {
  742. memnew_placement(_data._mem, RID(*reinterpret_cast<const RID *>(p_variant._data._mem)));
  743. } break;
  744. case OBJECT: {
  745. memnew_placement(_data._mem, ObjData(p_variant._get_obj()));
  746. if (likely(_get_obj().rc)) {
  747. _get_obj().rc->increment();
  748. }
  749. } break;
  750. case NODE_PATH: {
  751. memnew_placement(_data._mem, NodePath(*reinterpret_cast<const NodePath *>(p_variant._data._mem)));
  752. } break;
  753. case DICTIONARY: {
  754. memnew_placement(_data._mem, Dictionary(*reinterpret_cast<const Dictionary *>(p_variant._data._mem)));
  755. } break;
  756. case ARRAY: {
  757. memnew_placement(_data._mem, Array(*reinterpret_cast<const Array *>(p_variant._data._mem)));
  758. } break;
  759. // arrays
  760. case POOL_BYTE_ARRAY: {
  761. memnew_placement(_data._mem, PoolVector<uint8_t>(*reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem)));
  762. } break;
  763. case POOL_INT_ARRAY: {
  764. memnew_placement(_data._mem, PoolVector<int>(*reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem)));
  765. } break;
  766. case POOL_REAL_ARRAY: {
  767. memnew_placement(_data._mem, PoolVector<real_t>(*reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem)));
  768. } break;
  769. case POOL_STRING_ARRAY: {
  770. memnew_placement(_data._mem, PoolVector<String>(*reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem)));
  771. } break;
  772. case POOL_VECTOR2_ARRAY: {
  773. memnew_placement(_data._mem, PoolVector<Vector2>(*reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem)));
  774. } break;
  775. case POOL_VECTOR3_ARRAY: {
  776. memnew_placement(_data._mem, PoolVector<Vector3>(*reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem)));
  777. } break;
  778. case POOL_COLOR_ARRAY: {
  779. memnew_placement(_data._mem, PoolVector<Color>(*reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem)));
  780. } break;
  781. default: {
  782. }
  783. }
  784. }
  785. void Variant::zero() {
  786. switch (type) {
  787. case NIL:
  788. break;
  789. case BOOL:
  790. this->_data._bool = false;
  791. break;
  792. case INT:
  793. this->_data._int = 0;
  794. break;
  795. case REAL:
  796. this->_data._real = 0;
  797. break;
  798. case VECTOR2:
  799. *reinterpret_cast<Vector2 *>(this->_data._mem) = Vector2();
  800. break;
  801. case RECT2:
  802. *reinterpret_cast<Rect2 *>(this->_data._mem) = Rect2();
  803. break;
  804. case VECTOR3:
  805. *reinterpret_cast<Vector3 *>(this->_data._mem) = Vector3();
  806. break;
  807. case PLANE:
  808. *reinterpret_cast<Plane *>(this->_data._mem) = Plane();
  809. break;
  810. case QUAT:
  811. *reinterpret_cast<Quat *>(this->_data._mem) = Quat();
  812. break;
  813. case COLOR:
  814. *reinterpret_cast<Color *>(this->_data._mem) = Color();
  815. break;
  816. default:
  817. this->clear();
  818. break;
  819. }
  820. }
  821. void Variant::clear() {
  822. switch (type) {
  823. case STRING: {
  824. reinterpret_cast<String *>(_data._mem)->~String();
  825. } break;
  826. /*
  827. // no point, they don't allocate memory
  828. VECTOR3,
  829. PLANE,
  830. QUAT,
  831. COLOR,
  832. VECTOR2,
  833. RECT2
  834. */
  835. case TRANSFORM2D: {
  836. memdelete(_data._transform2d);
  837. } break;
  838. case AABB: {
  839. memdelete(_data._aabb);
  840. } break;
  841. case BASIS: {
  842. memdelete(_data._basis);
  843. } break;
  844. case TRANSFORM: {
  845. memdelete(_data._transform);
  846. } break;
  847. // misc types
  848. case NODE_PATH: {
  849. reinterpret_cast<NodePath *>(_data._mem)->~NodePath();
  850. } break;
  851. case OBJECT: {
  852. if (likely(_get_obj().rc)) {
  853. if (unlikely(_get_obj().rc->decrement())) {
  854. memdelete(_get_obj().rc);
  855. }
  856. } else {
  857. _get_obj().ref.unref();
  858. }
  859. } break;
  860. case _RID: {
  861. // not much need probably
  862. reinterpret_cast<RID *>(_data._mem)->~RID();
  863. } break;
  864. case DICTIONARY: {
  865. reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary();
  866. } break;
  867. case ARRAY: {
  868. reinterpret_cast<Array *>(_data._mem)->~Array();
  869. } break;
  870. // arrays
  871. case POOL_BYTE_ARRAY: {
  872. reinterpret_cast<PoolVector<uint8_t> *>(_data._mem)->~PoolVector<uint8_t>();
  873. } break;
  874. case POOL_INT_ARRAY: {
  875. reinterpret_cast<PoolVector<int> *>(_data._mem)->~PoolVector<int>();
  876. } break;
  877. case POOL_REAL_ARRAY: {
  878. reinterpret_cast<PoolVector<real_t> *>(_data._mem)->~PoolVector<real_t>();
  879. } break;
  880. case POOL_STRING_ARRAY: {
  881. reinterpret_cast<PoolVector<String> *>(_data._mem)->~PoolVector<String>();
  882. } break;
  883. case POOL_VECTOR2_ARRAY: {
  884. reinterpret_cast<PoolVector<Vector2> *>(_data._mem)->~PoolVector<Vector2>();
  885. } break;
  886. case POOL_VECTOR3_ARRAY: {
  887. reinterpret_cast<PoolVector<Vector3> *>(_data._mem)->~PoolVector<Vector3>();
  888. } break;
  889. case POOL_COLOR_ARRAY: {
  890. reinterpret_cast<PoolVector<Color> *>(_data._mem)->~PoolVector<Color>();
  891. } break;
  892. default: {
  893. } /* not needed */
  894. }
  895. type = NIL;
  896. }
  897. Variant::operator signed int() const {
  898. switch (type) {
  899. case NIL:
  900. return 0;
  901. case BOOL:
  902. return _data._bool ? 1 : 0;
  903. case INT:
  904. return _data._int;
  905. case REAL:
  906. return _data._real;
  907. case STRING:
  908. return operator String().to_int();
  909. default: {
  910. return 0;
  911. }
  912. }
  913. }
  914. Variant::operator unsigned int() const {
  915. switch (type) {
  916. case NIL:
  917. return 0;
  918. case BOOL:
  919. return _data._bool ? 1 : 0;
  920. case INT:
  921. return _data._int;
  922. case REAL:
  923. return _data._real;
  924. case STRING:
  925. return operator String().to_int();
  926. default: {
  927. return 0;
  928. }
  929. }
  930. }
  931. Variant::operator int64_t() const {
  932. switch (type) {
  933. case NIL:
  934. return 0;
  935. case BOOL:
  936. return _data._bool ? 1 : 0;
  937. case INT:
  938. return _data._int;
  939. case REAL:
  940. return _data._real;
  941. case STRING:
  942. return operator String().to_int64();
  943. default: {
  944. return 0;
  945. }
  946. }
  947. }
  948. /*
  949. Variant::operator long unsigned int() const {
  950. switch( type ) {
  951. case NIL: return 0;
  952. case BOOL: return _data._bool ? 1 : 0;
  953. case INT: return _data._int;
  954. case REAL: return _data._real;
  955. case STRING: return operator String().to_int();
  956. default: {
  957. return 0;
  958. }
  959. }
  960. return 0;
  961. };
  962. */
  963. Variant::operator uint64_t() const {
  964. switch (type) {
  965. case NIL:
  966. return 0;
  967. case BOOL:
  968. return _data._bool ? 1 : 0;
  969. case INT:
  970. return _data._int;
  971. case REAL:
  972. return _data._real;
  973. case STRING:
  974. return operator String().to_int();
  975. default: {
  976. return 0;
  977. }
  978. }
  979. }
  980. #ifdef NEED_LONG_INT
  981. Variant::operator signed long() const {
  982. switch (type) {
  983. case NIL:
  984. return 0;
  985. case BOOL:
  986. return _data._bool ? 1 : 0;
  987. case INT:
  988. return _data._int;
  989. case REAL:
  990. return _data._real;
  991. case STRING:
  992. return operator String().to_int();
  993. default: {
  994. return 0;
  995. }
  996. }
  997. return 0;
  998. };
  999. Variant::operator unsigned long() const {
  1000. switch (type) {
  1001. case NIL:
  1002. return 0;
  1003. case BOOL:
  1004. return _data._bool ? 1 : 0;
  1005. case INT:
  1006. return _data._int;
  1007. case REAL:
  1008. return _data._real;
  1009. case STRING:
  1010. return operator String().to_int();
  1011. default: {
  1012. return 0;
  1013. }
  1014. }
  1015. return 0;
  1016. };
  1017. #endif
  1018. Variant::operator signed short() const {
  1019. switch (type) {
  1020. case NIL:
  1021. return 0;
  1022. case BOOL:
  1023. return _data._bool ? 1 : 0;
  1024. case INT:
  1025. return _data._int;
  1026. case REAL:
  1027. return _data._real;
  1028. case STRING:
  1029. return operator String().to_int();
  1030. default: {
  1031. return 0;
  1032. }
  1033. }
  1034. }
  1035. Variant::operator unsigned short() const {
  1036. switch (type) {
  1037. case NIL:
  1038. return 0;
  1039. case BOOL:
  1040. return _data._bool ? 1 : 0;
  1041. case INT:
  1042. return _data._int;
  1043. case REAL:
  1044. return _data._real;
  1045. case STRING:
  1046. return operator String().to_int();
  1047. default: {
  1048. return 0;
  1049. }
  1050. }
  1051. }
  1052. Variant::operator signed char() const {
  1053. switch (type) {
  1054. case NIL:
  1055. return 0;
  1056. case BOOL:
  1057. return _data._bool ? 1 : 0;
  1058. case INT:
  1059. return _data._int;
  1060. case REAL:
  1061. return _data._real;
  1062. case STRING:
  1063. return operator String().to_int();
  1064. default: {
  1065. return 0;
  1066. }
  1067. }
  1068. }
  1069. Variant::operator unsigned char() const {
  1070. switch (type) {
  1071. case NIL:
  1072. return 0;
  1073. case BOOL:
  1074. return _data._bool ? 1 : 0;
  1075. case INT:
  1076. return _data._int;
  1077. case REAL:
  1078. return _data._real;
  1079. case STRING:
  1080. return operator String().to_int();
  1081. default: {
  1082. return 0;
  1083. }
  1084. }
  1085. }
  1086. Variant::operator CharType() const {
  1087. return operator unsigned int();
  1088. }
  1089. Variant::operator float() const {
  1090. switch (type) {
  1091. case NIL:
  1092. return 0;
  1093. case BOOL:
  1094. return _data._bool ? 1.0 : 0.0;
  1095. case INT:
  1096. return (float)_data._int;
  1097. case REAL:
  1098. return _data._real;
  1099. case STRING:
  1100. return operator String().to_double();
  1101. default: {
  1102. return 0;
  1103. }
  1104. }
  1105. }
  1106. Variant::operator double() const {
  1107. switch (type) {
  1108. case NIL:
  1109. return 0;
  1110. case BOOL:
  1111. return _data._bool ? 1.0 : 0.0;
  1112. case INT:
  1113. return (double)_data._int;
  1114. case REAL:
  1115. return _data._real;
  1116. case STRING:
  1117. return operator String().to_double();
  1118. default: {
  1119. return 0;
  1120. }
  1121. }
  1122. }
  1123. Variant::operator StringName() const {
  1124. if (type == NODE_PATH) {
  1125. return reinterpret_cast<const NodePath *>(_data._mem)->get_sname();
  1126. }
  1127. return StringName(operator String());
  1128. }
  1129. struct _VariantStrPair {
  1130. String key;
  1131. String value;
  1132. bool operator<(const _VariantStrPair &p) const {
  1133. return key < p.key;
  1134. }
  1135. };
  1136. Variant::operator String() const {
  1137. List<const void *> stack;
  1138. return stringify(stack);
  1139. }
  1140. template <class T>
  1141. String stringify_vector(const T &vec, List<const void *> &stack) {
  1142. String str("[");
  1143. for (int i = 0; i < vec.size(); i++) {
  1144. if (i > 0) {
  1145. str += ", ";
  1146. }
  1147. str = str + Variant(vec[i]).stringify(stack);
  1148. }
  1149. str += "]";
  1150. return str;
  1151. }
  1152. String Variant::stringify(List<const void *> &stack) const {
  1153. switch (type) {
  1154. case NIL:
  1155. return "Null";
  1156. case BOOL:
  1157. return _data._bool ? "True" : "False";
  1158. case INT:
  1159. return itos(_data._int);
  1160. case REAL:
  1161. return rtos(_data._real);
  1162. case STRING:
  1163. return *reinterpret_cast<const String *>(_data._mem);
  1164. case VECTOR2:
  1165. return "(" + operator Vector2() + ")";
  1166. case RECT2:
  1167. return "(" + operator Rect2() + ")";
  1168. case TRANSFORM2D: {
  1169. Transform2D mat32 = operator Transform2D();
  1170. return "(" + Variant(mat32.elements[0]).operator String() + ", " + Variant(mat32.elements[1]).operator String() + ", " + Variant(mat32.elements[2]).operator String() + ")";
  1171. } break;
  1172. case VECTOR3:
  1173. return "(" + operator Vector3() + ")";
  1174. case PLANE:
  1175. return operator Plane();
  1176. //case QUAT:
  1177. case AABB:
  1178. return operator ::AABB();
  1179. case QUAT:
  1180. return "(" + operator Quat() + ")";
  1181. case BASIS: {
  1182. Basis mat3 = operator Basis();
  1183. String mtx("(");
  1184. for (int i = 0; i < 3; i++) {
  1185. if (i != 0) {
  1186. mtx += ", ";
  1187. }
  1188. mtx += "(";
  1189. for (int j = 0; j < 3; j++) {
  1190. if (j != 0) {
  1191. mtx += ", ";
  1192. }
  1193. mtx += Variant(mat3.elements[i][j]).operator String();
  1194. }
  1195. mtx += ")";
  1196. }
  1197. return mtx + ")";
  1198. } break;
  1199. case TRANSFORM:
  1200. return operator Transform();
  1201. case NODE_PATH:
  1202. return operator NodePath();
  1203. case COLOR:
  1204. return String::num(operator Color().r) + "," + String::num(operator Color().g) + "," + String::num(operator Color().b) + "," + String::num(operator Color().a);
  1205. case DICTIONARY: {
  1206. const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
  1207. if (stack.find(d.id())) {
  1208. return "{...}";
  1209. }
  1210. stack.push_back(d.id());
  1211. //const String *K=NULL;
  1212. String str("{");
  1213. List<Variant> keys;
  1214. d.get_key_list(&keys);
  1215. Vector<_VariantStrPair> pairs;
  1216. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1217. _VariantStrPair sp;
  1218. sp.key = E->get().stringify(stack);
  1219. sp.value = d[E->get()].stringify(stack);
  1220. pairs.push_back(sp);
  1221. }
  1222. pairs.sort();
  1223. for (int i = 0; i < pairs.size(); i++) {
  1224. if (i > 0) {
  1225. str += ", ";
  1226. }
  1227. str += pairs[i].key + ":" + pairs[i].value;
  1228. }
  1229. str += "}";
  1230. stack.erase(d.id());
  1231. return str;
  1232. } break;
  1233. case POOL_VECTOR2_ARRAY: {
  1234. return stringify_vector(operator PoolVector<Vector2>(), stack);
  1235. } break;
  1236. case POOL_VECTOR3_ARRAY: {
  1237. return stringify_vector(operator PoolVector<Vector3>(), stack);
  1238. } break;
  1239. case POOL_COLOR_ARRAY: {
  1240. return stringify_vector(operator PoolVector<Color>(), stack);
  1241. } break;
  1242. case POOL_STRING_ARRAY: {
  1243. return stringify_vector(operator PoolVector<String>(), stack);
  1244. } break;
  1245. case POOL_BYTE_ARRAY: {
  1246. return stringify_vector(operator PoolVector<uint8_t>(), stack);
  1247. } break;
  1248. case POOL_INT_ARRAY: {
  1249. return stringify_vector(operator PoolVector<int>(), stack);
  1250. } break;
  1251. case POOL_REAL_ARRAY: {
  1252. return stringify_vector(operator PoolVector<real_t>(), stack);
  1253. } break;
  1254. case ARRAY: {
  1255. Array arr = operator Array();
  1256. if (stack.find(arr.id())) {
  1257. return "[...]";
  1258. }
  1259. stack.push_back(arr.id());
  1260. String str = stringify_vector(arr, stack);
  1261. stack.erase(arr.id());
  1262. return str;
  1263. } break;
  1264. case OBJECT: {
  1265. Object *obj = _OBJ_PTR(*this);
  1266. if (likely(obj)) {
  1267. return obj->to_string();
  1268. } else {
  1269. if (_get_obj().rc) {
  1270. return "[Deleted Object]";
  1271. }
  1272. return "[Object:null]";
  1273. }
  1274. } break;
  1275. default: {
  1276. return "[" + get_type_name(type) + "]";
  1277. }
  1278. }
  1279. return "";
  1280. }
  1281. Variant::operator Vector2() const {
  1282. if (type == VECTOR2) {
  1283. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1284. } else if (type == VECTOR3) {
  1285. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1286. } else {
  1287. return Vector2();
  1288. }
  1289. }
  1290. Variant::operator Rect2() const {
  1291. if (type == RECT2) {
  1292. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1293. } else {
  1294. return Rect2();
  1295. }
  1296. }
  1297. Variant::operator Vector3() const {
  1298. if (type == VECTOR3) {
  1299. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1300. } else if (type == VECTOR2) {
  1301. return Vector3(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1302. } else {
  1303. return Vector3();
  1304. }
  1305. }
  1306. Variant::operator Plane() const {
  1307. if (type == PLANE) {
  1308. return *reinterpret_cast<const Plane *>(_data._mem);
  1309. } else {
  1310. return Plane();
  1311. }
  1312. }
  1313. Variant::operator ::AABB() const {
  1314. if (type == AABB) {
  1315. return *_data._aabb;
  1316. } else {
  1317. return ::AABB();
  1318. }
  1319. }
  1320. Variant::operator Basis() const {
  1321. if (type == BASIS) {
  1322. return *_data._basis;
  1323. } else if (type == QUAT) {
  1324. return *reinterpret_cast<const Quat *>(_data._mem);
  1325. } else if (type == VECTOR3) {
  1326. return Basis(*reinterpret_cast<const Vector3 *>(_data._mem));
  1327. } else if (type == TRANSFORM) { // unexposed in Variant::can_convert?
  1328. return _data._transform->basis;
  1329. } else {
  1330. return Basis();
  1331. }
  1332. }
  1333. Variant::operator Quat() const {
  1334. if (type == QUAT) {
  1335. return *reinterpret_cast<const Quat *>(_data._mem);
  1336. } else if (type == BASIS) {
  1337. return *_data._basis;
  1338. } else if (type == TRANSFORM) {
  1339. return _data._transform->basis;
  1340. } else {
  1341. return Quat();
  1342. }
  1343. }
  1344. Variant::operator Transform() const {
  1345. if (type == TRANSFORM) {
  1346. return *_data._transform;
  1347. } else if (type == BASIS) {
  1348. return Transform(*_data._basis, Vector3());
  1349. } else if (type == QUAT) {
  1350. return Transform(Basis(*reinterpret_cast<const Quat *>(_data._mem)), Vector3());
  1351. } else if (type == TRANSFORM2D) {
  1352. const Transform2D &t = *_data._transform2d;
  1353. Transform m;
  1354. m.basis.elements[0][0] = t.elements[0][0];
  1355. m.basis.elements[1][0] = t.elements[0][1];
  1356. m.basis.elements[0][1] = t.elements[1][0];
  1357. m.basis.elements[1][1] = t.elements[1][1];
  1358. m.origin[0] = t.elements[2][0];
  1359. m.origin[1] = t.elements[2][1];
  1360. return m;
  1361. } else {
  1362. return Transform();
  1363. }
  1364. }
  1365. Variant::operator Transform2D() const {
  1366. if (type == TRANSFORM2D) {
  1367. return *_data._transform2d;
  1368. } else if (type == TRANSFORM) {
  1369. const Transform &t = *_data._transform;
  1370. Transform2D m;
  1371. m.elements[0][0] = t.basis.elements[0][0];
  1372. m.elements[0][1] = t.basis.elements[1][0];
  1373. m.elements[1][0] = t.basis.elements[0][1];
  1374. m.elements[1][1] = t.basis.elements[1][1];
  1375. m.elements[2][0] = t.origin[0];
  1376. m.elements[2][1] = t.origin[1];
  1377. return m;
  1378. } else {
  1379. return Transform2D();
  1380. }
  1381. }
  1382. Variant::operator Color() const {
  1383. if (type == COLOR) {
  1384. return *reinterpret_cast<const Color *>(_data._mem);
  1385. } else if (type == STRING) {
  1386. return Color::html(operator String());
  1387. } else if (type == INT) {
  1388. return Color::hex(operator int());
  1389. } else {
  1390. return Color();
  1391. }
  1392. }
  1393. Variant::operator NodePath() const {
  1394. if (type == NODE_PATH) {
  1395. return *reinterpret_cast<const NodePath *>(_data._mem);
  1396. } else if (type == STRING) {
  1397. return NodePath(operator String());
  1398. } else {
  1399. return NodePath();
  1400. }
  1401. }
  1402. Variant::operator RefPtr() const {
  1403. if (type == OBJECT) {
  1404. return _get_obj().ref;
  1405. } else {
  1406. return RefPtr();
  1407. }
  1408. }
  1409. Variant::operator RID() const {
  1410. if (type == _RID) {
  1411. return *reinterpret_cast<const RID *>(_data._mem);
  1412. } else if (type == OBJECT) {
  1413. if (!_get_obj().ref.is_null()) {
  1414. return _get_obj().ref.get_rid();
  1415. } else {
  1416. Object *obj = likely(_get_obj().rc) ? _get_obj().rc->get_ptr() : nullptr;
  1417. if (unlikely(!obj)) {
  1418. if (_get_obj().rc) {
  1419. ERR_PRINT("Attempted get RID on a deleted object.");
  1420. }
  1421. return RID();
  1422. }
  1423. Variant::CallError ce;
  1424. Variant ret = obj->call(CoreStringNames::get_singleton()->get_rid, nullptr, 0, ce);
  1425. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() == Variant::_RID) {
  1426. return ret;
  1427. } else {
  1428. return RID();
  1429. }
  1430. }
  1431. } else {
  1432. return RID();
  1433. }
  1434. }
  1435. Variant::operator Object *() const {
  1436. if (type == OBJECT) {
  1437. return _OBJ_PTR(*this);
  1438. } else {
  1439. return nullptr;
  1440. }
  1441. }
  1442. Variant::operator Node *() const {
  1443. if (type == OBJECT) {
  1444. Object *obj = _get_obj().rc ? _get_obj().rc->get_ptr() : nullptr;
  1445. return Object::cast_to<Node>(obj);
  1446. }
  1447. return nullptr;
  1448. }
  1449. Variant::operator Control *() const {
  1450. if (type == OBJECT) {
  1451. Object *obj = _get_obj().rc ? _get_obj().rc->get_ptr() : nullptr;
  1452. return Object::cast_to<Control>(obj);
  1453. }
  1454. return nullptr;
  1455. }
  1456. Variant::operator Dictionary() const {
  1457. if (type == DICTIONARY) {
  1458. return *reinterpret_cast<const Dictionary *>(_data._mem);
  1459. } else {
  1460. return Dictionary();
  1461. }
  1462. }
  1463. template <class DA, class SA>
  1464. inline DA _convert_array(const SA &p_array) {
  1465. DA da;
  1466. da.resize(p_array.size());
  1467. for (int i = 0; i < p_array.size(); i++) {
  1468. da.set(i, Variant(p_array.get(i)));
  1469. }
  1470. return da;
  1471. }
  1472. template <class DA>
  1473. inline DA _convert_array_from_variant(const Variant &p_variant) {
  1474. switch (p_variant.get_type()) {
  1475. case Variant::ARRAY: {
  1476. return _convert_array<DA, Array>(p_variant.operator Array());
  1477. }
  1478. case Variant::POOL_BYTE_ARRAY: {
  1479. return _convert_array<DA, PoolVector<uint8_t>>(p_variant.operator PoolVector<uint8_t>());
  1480. }
  1481. case Variant::POOL_INT_ARRAY: {
  1482. return _convert_array<DA, PoolVector<int>>(p_variant.operator PoolVector<int>());
  1483. }
  1484. case Variant::POOL_REAL_ARRAY: {
  1485. return _convert_array<DA, PoolVector<real_t>>(p_variant.operator PoolVector<real_t>());
  1486. }
  1487. case Variant::POOL_STRING_ARRAY: {
  1488. return _convert_array<DA, PoolVector<String>>(p_variant.operator PoolVector<String>());
  1489. }
  1490. case Variant::POOL_VECTOR2_ARRAY: {
  1491. return _convert_array<DA, PoolVector<Vector2>>(p_variant.operator PoolVector<Vector2>());
  1492. }
  1493. case Variant::POOL_VECTOR3_ARRAY: {
  1494. return _convert_array<DA, PoolVector<Vector3>>(p_variant.operator PoolVector<Vector3>());
  1495. }
  1496. case Variant::POOL_COLOR_ARRAY: {
  1497. return _convert_array<DA, PoolVector<Color>>(p_variant.operator PoolVector<Color>());
  1498. }
  1499. default: {
  1500. return DA();
  1501. }
  1502. }
  1503. }
  1504. Variant::operator Array() const {
  1505. if (type == ARRAY) {
  1506. return *reinterpret_cast<const Array *>(_data._mem);
  1507. } else {
  1508. return _convert_array_from_variant<Array>(*this);
  1509. }
  1510. }
  1511. Variant::operator PoolVector<uint8_t>() const {
  1512. if (type == POOL_BYTE_ARRAY) {
  1513. return *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  1514. } else {
  1515. return _convert_array_from_variant<PoolVector<uint8_t>>(*this);
  1516. }
  1517. }
  1518. Variant::operator PoolVector<int>() const {
  1519. if (type == POOL_INT_ARRAY) {
  1520. return *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  1521. } else {
  1522. return _convert_array_from_variant<PoolVector<int>>(*this);
  1523. }
  1524. }
  1525. Variant::operator PoolVector<real_t>() const {
  1526. if (type == POOL_REAL_ARRAY) {
  1527. return *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  1528. } else {
  1529. return _convert_array_from_variant<PoolVector<real_t>>(*this);
  1530. }
  1531. }
  1532. Variant::operator PoolVector<String>() const {
  1533. if (type == POOL_STRING_ARRAY) {
  1534. return *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  1535. } else {
  1536. return _convert_array_from_variant<PoolVector<String>>(*this);
  1537. }
  1538. }
  1539. Variant::operator PoolVector<Vector3>() const {
  1540. if (type == POOL_VECTOR3_ARRAY) {
  1541. return *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  1542. } else {
  1543. return _convert_array_from_variant<PoolVector<Vector3>>(*this);
  1544. }
  1545. }
  1546. Variant::operator PoolVector<Vector2>() const {
  1547. if (type == POOL_VECTOR2_ARRAY) {
  1548. return *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  1549. } else {
  1550. return _convert_array_from_variant<PoolVector<Vector2>>(*this);
  1551. }
  1552. }
  1553. Variant::operator PoolVector<Color>() const {
  1554. if (type == POOL_COLOR_ARRAY) {
  1555. return *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  1556. } else {
  1557. return _convert_array_from_variant<PoolVector<Color>>(*this);
  1558. }
  1559. }
  1560. /* helpers */
  1561. Variant::operator Vector<RID>() const {
  1562. Array va = operator Array();
  1563. Vector<RID> rids;
  1564. rids.resize(va.size());
  1565. for (int i = 0; i < rids.size(); i++) {
  1566. rids.write[i] = va[i];
  1567. }
  1568. return rids;
  1569. }
  1570. Variant::operator Vector<Vector2>() const {
  1571. PoolVector<Vector2> from = operator PoolVector<Vector2>();
  1572. Vector<Vector2> to;
  1573. int len = from.size();
  1574. if (len == 0) {
  1575. return Vector<Vector2>();
  1576. }
  1577. to.resize(len);
  1578. PoolVector<Vector2>::Read r = from.read();
  1579. Vector2 *w = to.ptrw();
  1580. for (int i = 0; i < len; i++) {
  1581. w[i] = r[i];
  1582. }
  1583. return to;
  1584. }
  1585. Variant::operator PoolVector<Plane>() const {
  1586. Array va = operator Array();
  1587. PoolVector<Plane> planes;
  1588. int va_size = va.size();
  1589. if (va_size == 0) {
  1590. return planes;
  1591. }
  1592. planes.resize(va_size);
  1593. PoolVector<Plane>::Write w = planes.write();
  1594. for (int i = 0; i < va_size; i++) {
  1595. w[i] = va[i];
  1596. }
  1597. return planes;
  1598. }
  1599. Variant::operator PoolVector<Face3>() const {
  1600. PoolVector<Vector3> va = operator PoolVector<Vector3>();
  1601. PoolVector<Face3> faces;
  1602. int va_size = va.size();
  1603. if (va_size == 0) {
  1604. return faces;
  1605. }
  1606. faces.resize(va_size / 3);
  1607. PoolVector<Face3>::Write w = faces.write();
  1608. PoolVector<Vector3>::Read r = va.read();
  1609. for (int i = 0; i < va_size; i++) {
  1610. w[i / 3].vertex[i % 3] = r[i];
  1611. }
  1612. return faces;
  1613. }
  1614. Variant::operator Vector<Plane>() const {
  1615. Array va = operator Array();
  1616. Vector<Plane> planes;
  1617. int va_size = va.size();
  1618. if (va_size == 0) {
  1619. return planes;
  1620. }
  1621. planes.resize(va_size);
  1622. for (int i = 0; i < va_size; i++) {
  1623. planes.write[i] = va[i];
  1624. }
  1625. return planes;
  1626. }
  1627. Variant::operator Vector<Variant>() const {
  1628. Array from = operator Array();
  1629. Vector<Variant> to;
  1630. int len = from.size();
  1631. to.resize(len);
  1632. for (int i = 0; i < len; i++) {
  1633. to.write[i] = from[i];
  1634. }
  1635. return to;
  1636. }
  1637. Variant::operator Vector<uint8_t>() const {
  1638. PoolVector<uint8_t> from = operator PoolVector<uint8_t>();
  1639. Vector<uint8_t> to;
  1640. int len = from.size();
  1641. to.resize(len);
  1642. for (int i = 0; i < len; i++) {
  1643. to.write[i] = from[i];
  1644. }
  1645. return to;
  1646. }
  1647. Variant::operator Vector<int>() const {
  1648. PoolVector<int> from = operator PoolVector<int>();
  1649. Vector<int> to;
  1650. int len = from.size();
  1651. to.resize(len);
  1652. for (int i = 0; i < len; i++) {
  1653. to.write[i] = from[i];
  1654. }
  1655. return to;
  1656. }
  1657. Variant::operator Vector<real_t>() const {
  1658. PoolVector<real_t> from = operator PoolVector<real_t>();
  1659. Vector<real_t> to;
  1660. int len = from.size();
  1661. to.resize(len);
  1662. for (int i = 0; i < len; i++) {
  1663. to.write[i] = from[i];
  1664. }
  1665. return to;
  1666. }
  1667. Variant::operator Vector<String>() const {
  1668. PoolVector<String> from = operator PoolVector<String>();
  1669. Vector<String> to;
  1670. int len = from.size();
  1671. to.resize(len);
  1672. for (int i = 0; i < len; i++) {
  1673. to.write[i] = from[i];
  1674. }
  1675. return to;
  1676. }
  1677. Variant::operator Vector<StringName>() const {
  1678. PoolVector<String> from = operator PoolVector<String>();
  1679. Vector<StringName> to;
  1680. int len = from.size();
  1681. to.resize(len);
  1682. for (int i = 0; i < len; i++) {
  1683. to.write[i] = from[i];
  1684. }
  1685. return to;
  1686. }
  1687. Variant::operator Vector<Vector3>() const {
  1688. PoolVector<Vector3> from = operator PoolVector<Vector3>();
  1689. Vector<Vector3> to;
  1690. int len = from.size();
  1691. if (len == 0) {
  1692. return Vector<Vector3>();
  1693. }
  1694. to.resize(len);
  1695. PoolVector<Vector3>::Read r = from.read();
  1696. Vector3 *w = to.ptrw();
  1697. for (int i = 0; i < len; i++) {
  1698. w[i] = r[i];
  1699. }
  1700. return to;
  1701. }
  1702. Variant::operator Vector<Color>() const {
  1703. PoolVector<Color> from = operator PoolVector<Color>();
  1704. Vector<Color> to;
  1705. int len = from.size();
  1706. if (len == 0) {
  1707. return Vector<Color>();
  1708. }
  1709. to.resize(len);
  1710. PoolVector<Color>::Read r = from.read();
  1711. Color *w = to.ptrw();
  1712. for (int i = 0; i < len; i++) {
  1713. w[i] = r[i];
  1714. }
  1715. return to;
  1716. }
  1717. Variant::operator Margin() const {
  1718. return (Margin) operator int();
  1719. }
  1720. Variant::operator Orientation() const {
  1721. return (Orientation) operator int();
  1722. }
  1723. Variant::operator IP_Address() const {
  1724. if (type == POOL_REAL_ARRAY || type == POOL_INT_ARRAY || type == POOL_BYTE_ARRAY) {
  1725. PoolVector<int> addr = operator PoolVector<int>();
  1726. if (addr.size() == 4) {
  1727. return IP_Address(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
  1728. }
  1729. }
  1730. return IP_Address(operator String());
  1731. }
  1732. Variant::Variant(bool p_bool) {
  1733. type = BOOL;
  1734. _data._bool = p_bool;
  1735. }
  1736. /*
  1737. Variant::Variant(long unsigned int p_long) {
  1738. type=INT;
  1739. _data._int=p_long;
  1740. };
  1741. */
  1742. Variant::Variant(signed int p_int) {
  1743. type = INT;
  1744. _data._int = p_int;
  1745. }
  1746. Variant::Variant(unsigned int p_int) {
  1747. type = INT;
  1748. _data._int = p_int;
  1749. }
  1750. #ifdef NEED_LONG_INT
  1751. Variant::Variant(signed long p_int) {
  1752. type = INT;
  1753. _data._int = p_int;
  1754. }
  1755. Variant::Variant(unsigned long p_int) {
  1756. type = INT;
  1757. _data._int = p_int;
  1758. }
  1759. #endif
  1760. Variant::Variant(int64_t p_int) {
  1761. type = INT;
  1762. _data._int = p_int;
  1763. }
  1764. Variant::Variant(uint64_t p_int) {
  1765. type = INT;
  1766. _data._int = p_int;
  1767. }
  1768. Variant::Variant(signed short p_short) {
  1769. type = INT;
  1770. _data._int = p_short;
  1771. }
  1772. Variant::Variant(unsigned short p_short) {
  1773. type = INT;
  1774. _data._int = p_short;
  1775. }
  1776. Variant::Variant(signed char p_char) {
  1777. type = INT;
  1778. _data._int = p_char;
  1779. }
  1780. Variant::Variant(unsigned char p_char) {
  1781. type = INT;
  1782. _data._int = p_char;
  1783. }
  1784. Variant::Variant(float p_float) {
  1785. type = REAL;
  1786. _data._real = p_float;
  1787. }
  1788. Variant::Variant(double p_double) {
  1789. type = REAL;
  1790. _data._real = p_double;
  1791. }
  1792. Variant::Variant(const StringName &p_string) {
  1793. type = STRING;
  1794. memnew_placement(_data._mem, String(p_string.operator String()));
  1795. }
  1796. Variant::Variant(const String &p_string) {
  1797. type = STRING;
  1798. memnew_placement(_data._mem, String(p_string));
  1799. }
  1800. Variant::Variant(const char *const p_cstring) {
  1801. type = STRING;
  1802. memnew_placement(_data._mem, String((const char *)p_cstring));
  1803. }
  1804. Variant::Variant(const CharType *p_wstring) {
  1805. type = STRING;
  1806. memnew_placement(_data._mem, String(p_wstring));
  1807. }
  1808. Variant::Variant(const Vector3 &p_vector3) {
  1809. type = VECTOR3;
  1810. memnew_placement(_data._mem, Vector3(p_vector3));
  1811. }
  1812. Variant::Variant(const Vector2 &p_vector2) {
  1813. type = VECTOR2;
  1814. memnew_placement(_data._mem, Vector2(p_vector2));
  1815. }
  1816. Variant::Variant(const Rect2 &p_rect2) {
  1817. type = RECT2;
  1818. memnew_placement(_data._mem, Rect2(p_rect2));
  1819. }
  1820. Variant::Variant(const Plane &p_plane) {
  1821. type = PLANE;
  1822. memnew_placement(_data._mem, Plane(p_plane));
  1823. }
  1824. Variant::Variant(const ::AABB &p_aabb) {
  1825. type = AABB;
  1826. _data._aabb = memnew(::AABB(p_aabb));
  1827. }
  1828. Variant::Variant(const Basis &p_matrix) {
  1829. type = BASIS;
  1830. _data._basis = memnew(Basis(p_matrix));
  1831. }
  1832. Variant::Variant(const Quat &p_quat) {
  1833. type = QUAT;
  1834. memnew_placement(_data._mem, Quat(p_quat));
  1835. }
  1836. Variant::Variant(const Transform &p_transform) {
  1837. type = TRANSFORM;
  1838. _data._transform = memnew(Transform(p_transform));
  1839. }
  1840. Variant::Variant(const Transform2D &p_transform) {
  1841. type = TRANSFORM2D;
  1842. _data._transform2d = memnew(Transform2D(p_transform));
  1843. }
  1844. Variant::Variant(const Color &p_color) {
  1845. type = COLOR;
  1846. memnew_placement(_data._mem, Color(p_color));
  1847. }
  1848. Variant::Variant(const NodePath &p_node_path) {
  1849. type = NODE_PATH;
  1850. memnew_placement(_data._mem, NodePath(p_node_path));
  1851. }
  1852. Variant::Variant(const RefPtr &p_resource) {
  1853. type = OBJECT;
  1854. memnew_placement(_data._mem, ObjData);
  1855. _get_obj().rc = nullptr;
  1856. _get_obj().ref = p_resource;
  1857. }
  1858. Variant::Variant(const RID &p_rid) {
  1859. type = _RID;
  1860. memnew_placement(_data._mem, RID(p_rid));
  1861. }
  1862. Variant::Variant(const Object *p_object) {
  1863. type = OBJECT;
  1864. Object *obj = const_cast<Object *>(p_object);
  1865. memnew_placement(_data._mem, ObjData);
  1866. Reference *ref = Object::cast_to<Reference>(obj);
  1867. if (unlikely(ref)) {
  1868. *reinterpret_cast<Ref<Reference> *>(_get_obj().ref.get_data()) = Ref<Reference>(ref);
  1869. _get_obj().rc = nullptr;
  1870. } else {
  1871. _get_obj().rc = likely(obj) ? obj->_use_rc() : nullptr;
  1872. }
  1873. }
  1874. Variant::Variant(const Dictionary &p_dictionary) {
  1875. type = DICTIONARY;
  1876. memnew_placement(_data._mem, Dictionary(p_dictionary));
  1877. }
  1878. Variant::Variant(const Array &p_array) {
  1879. type = ARRAY;
  1880. memnew_placement(_data._mem, Array(p_array));
  1881. }
  1882. Variant::Variant(const PoolVector<Plane> &p_array) {
  1883. type = ARRAY;
  1884. Array *plane_array = memnew_placement(_data._mem, Array);
  1885. plane_array->resize(p_array.size());
  1886. for (int i = 0; i < p_array.size(); i++) {
  1887. plane_array->operator[](i) = Variant(p_array[i]);
  1888. }
  1889. }
  1890. Variant::Variant(const Vector<Plane> &p_array) {
  1891. type = ARRAY;
  1892. Array *plane_array = memnew_placement(_data._mem, Array);
  1893. plane_array->resize(p_array.size());
  1894. for (int i = 0; i < p_array.size(); i++) {
  1895. plane_array->operator[](i) = Variant(p_array[i]);
  1896. }
  1897. }
  1898. Variant::Variant(const Vector<RID> &p_array) {
  1899. type = ARRAY;
  1900. Array *rid_array = memnew_placement(_data._mem, Array);
  1901. rid_array->resize(p_array.size());
  1902. for (int i = 0; i < p_array.size(); i++) {
  1903. rid_array->set(i, Variant(p_array[i]));
  1904. }
  1905. }
  1906. Variant::Variant(const Vector<Vector2> &p_array) {
  1907. type = NIL;
  1908. PoolVector<Vector2> v;
  1909. int len = p_array.size();
  1910. if (len > 0) {
  1911. v.resize(len);
  1912. PoolVector<Vector2>::Write w = v.write();
  1913. const Vector2 *r = p_array.ptr();
  1914. for (int i = 0; i < len; i++) {
  1915. w[i] = r[i];
  1916. }
  1917. }
  1918. *this = v;
  1919. }
  1920. Variant::Variant(const PoolVector<uint8_t> &p_raw_array) {
  1921. type = POOL_BYTE_ARRAY;
  1922. memnew_placement(_data._mem, PoolVector<uint8_t>(p_raw_array));
  1923. }
  1924. Variant::Variant(const PoolVector<int> &p_int_array) {
  1925. type = POOL_INT_ARRAY;
  1926. memnew_placement(_data._mem, PoolVector<int>(p_int_array));
  1927. }
  1928. Variant::Variant(const PoolVector<real_t> &p_real_array) {
  1929. type = POOL_REAL_ARRAY;
  1930. memnew_placement(_data._mem, PoolVector<real_t>(p_real_array));
  1931. }
  1932. Variant::Variant(const PoolVector<String> &p_string_array) {
  1933. type = POOL_STRING_ARRAY;
  1934. memnew_placement(_data._mem, PoolVector<String>(p_string_array));
  1935. }
  1936. Variant::Variant(const PoolVector<Vector3> &p_vector3_array) {
  1937. type = POOL_VECTOR3_ARRAY;
  1938. memnew_placement(_data._mem, PoolVector<Vector3>(p_vector3_array));
  1939. }
  1940. Variant::Variant(const PoolVector<Vector2> &p_vector2_array) {
  1941. type = POOL_VECTOR2_ARRAY;
  1942. memnew_placement(_data._mem, PoolVector<Vector2>(p_vector2_array));
  1943. }
  1944. Variant::Variant(const PoolVector<Color> &p_color_array) {
  1945. type = POOL_COLOR_ARRAY;
  1946. memnew_placement(_data._mem, PoolVector<Color>(p_color_array));
  1947. }
  1948. Variant::Variant(const PoolVector<Face3> &p_face_array) {
  1949. PoolVector<Vector3> vertices;
  1950. int face_count = p_face_array.size();
  1951. vertices.resize(face_count * 3);
  1952. if (face_count) {
  1953. PoolVector<Face3>::Read r = p_face_array.read();
  1954. PoolVector<Vector3>::Write w = vertices.write();
  1955. for (int i = 0; i < face_count; i++) {
  1956. for (int j = 0; j < 3; j++) {
  1957. w[i * 3 + j] = r[i].vertex[j];
  1958. }
  1959. }
  1960. }
  1961. type = NIL;
  1962. *this = vertices;
  1963. }
  1964. /* helpers */
  1965. Variant::Variant(const Vector<Variant> &p_array) {
  1966. type = NIL;
  1967. Array v;
  1968. int len = p_array.size();
  1969. v.resize(len);
  1970. for (int i = 0; i < len; i++) {
  1971. v.set(i, p_array[i]);
  1972. }
  1973. *this = v;
  1974. }
  1975. Variant::Variant(const Vector<uint8_t> &p_array) {
  1976. type = NIL;
  1977. PoolVector<uint8_t> v;
  1978. int len = p_array.size();
  1979. v.resize(len);
  1980. for (int i = 0; i < len; i++) {
  1981. v.set(i, p_array[i]);
  1982. }
  1983. *this = v;
  1984. }
  1985. Variant::Variant(const Vector<int> &p_array) {
  1986. type = NIL;
  1987. PoolVector<int> v;
  1988. int len = p_array.size();
  1989. v.resize(len);
  1990. for (int i = 0; i < len; i++) {
  1991. v.set(i, p_array[i]);
  1992. }
  1993. *this = v;
  1994. }
  1995. Variant::Variant(const Vector<real_t> &p_array) {
  1996. type = NIL;
  1997. PoolVector<real_t> v;
  1998. int len = p_array.size();
  1999. v.resize(len);
  2000. for (int i = 0; i < len; i++) {
  2001. v.set(i, p_array[i]);
  2002. }
  2003. *this = v;
  2004. }
  2005. Variant::Variant(const Vector<String> &p_array) {
  2006. type = NIL;
  2007. PoolVector<String> v;
  2008. int len = p_array.size();
  2009. v.resize(len);
  2010. for (int i = 0; i < len; i++) {
  2011. v.set(i, p_array[i]);
  2012. }
  2013. *this = v;
  2014. }
  2015. Variant::Variant(const Vector<StringName> &p_array) {
  2016. type = NIL;
  2017. PoolVector<String> v;
  2018. int len = p_array.size();
  2019. v.resize(len);
  2020. for (int i = 0; i < len; i++) {
  2021. v.set(i, p_array[i]);
  2022. }
  2023. *this = v;
  2024. }
  2025. Variant::Variant(const Vector<Vector3> &p_array) {
  2026. type = NIL;
  2027. PoolVector<Vector3> v;
  2028. int len = p_array.size();
  2029. if (len > 0) {
  2030. v.resize(len);
  2031. PoolVector<Vector3>::Write w = v.write();
  2032. const Vector3 *r = p_array.ptr();
  2033. for (int i = 0; i < len; i++) {
  2034. w[i] = r[i];
  2035. }
  2036. }
  2037. *this = v;
  2038. }
  2039. Variant::Variant(const Vector<Color> &p_array) {
  2040. type = NIL;
  2041. PoolVector<Color> v;
  2042. int len = p_array.size();
  2043. v.resize(len);
  2044. for (int i = 0; i < len; i++) {
  2045. v.set(i, p_array[i]);
  2046. }
  2047. *this = v;
  2048. }
  2049. void Variant::operator=(const Variant &p_variant) {
  2050. if (unlikely(this == &p_variant)) {
  2051. return;
  2052. }
  2053. if (unlikely(type != p_variant.type)) {
  2054. reference(p_variant);
  2055. return;
  2056. }
  2057. switch (p_variant.type) {
  2058. case NIL: {
  2059. // none
  2060. } break;
  2061. // atomic types
  2062. case BOOL: {
  2063. _data._bool = p_variant._data._bool;
  2064. } break;
  2065. case INT: {
  2066. _data._int = p_variant._data._int;
  2067. } break;
  2068. case REAL: {
  2069. _data._real = p_variant._data._real;
  2070. } break;
  2071. case STRING: {
  2072. *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
  2073. } break;
  2074. // math types
  2075. case VECTOR2: {
  2076. *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2077. } break;
  2078. case RECT2: {
  2079. *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2080. } break;
  2081. case TRANSFORM2D: {
  2082. *_data._transform2d = *(p_variant._data._transform2d);
  2083. } break;
  2084. case VECTOR3: {
  2085. *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2086. } break;
  2087. case PLANE: {
  2088. *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
  2089. } break;
  2090. case AABB: {
  2091. *_data._aabb = *(p_variant._data._aabb);
  2092. } break;
  2093. case QUAT: {
  2094. *reinterpret_cast<Quat *>(_data._mem) = *reinterpret_cast<const Quat *>(p_variant._data._mem);
  2095. } break;
  2096. case BASIS: {
  2097. *_data._basis = *(p_variant._data._basis);
  2098. } break;
  2099. case TRANSFORM: {
  2100. *_data._transform = *(p_variant._data._transform);
  2101. } break;
  2102. // misc types
  2103. case COLOR: {
  2104. *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
  2105. } break;
  2106. case _RID: {
  2107. *reinterpret_cast<RID *>(_data._mem) = *reinterpret_cast<const RID *>(p_variant._data._mem);
  2108. } break;
  2109. case OBJECT: {
  2110. if (likely(_get_obj().rc)) {
  2111. if (unlikely(_get_obj().rc->decrement())) {
  2112. memdelete(_get_obj().rc);
  2113. }
  2114. }
  2115. *reinterpret_cast<ObjData *>(_data._mem) = p_variant._get_obj();
  2116. if (likely(_get_obj().rc)) {
  2117. _get_obj().rc->increment();
  2118. }
  2119. } break;
  2120. case NODE_PATH: {
  2121. *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
  2122. } break;
  2123. case DICTIONARY: {
  2124. *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
  2125. } break;
  2126. case ARRAY: {
  2127. *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
  2128. } break;
  2129. // arrays
  2130. case POOL_BYTE_ARRAY: {
  2131. *reinterpret_cast<PoolVector<uint8_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem);
  2132. } break;
  2133. case POOL_INT_ARRAY: {
  2134. *reinterpret_cast<PoolVector<int> *>(_data._mem) = *reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem);
  2135. } break;
  2136. case POOL_REAL_ARRAY: {
  2137. *reinterpret_cast<PoolVector<real_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem);
  2138. } break;
  2139. case POOL_STRING_ARRAY: {
  2140. *reinterpret_cast<PoolVector<String> *>(_data._mem) = *reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem);
  2141. } break;
  2142. case POOL_VECTOR2_ARRAY: {
  2143. *reinterpret_cast<PoolVector<Vector2> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem);
  2144. } break;
  2145. case POOL_VECTOR3_ARRAY: {
  2146. *reinterpret_cast<PoolVector<Vector3> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem);
  2147. } break;
  2148. case POOL_COLOR_ARRAY: {
  2149. *reinterpret_cast<PoolVector<Color> *>(_data._mem) = *reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem);
  2150. } break;
  2151. default: {
  2152. }
  2153. }
  2154. }
  2155. Variant::Variant(const IP_Address &p_address) {
  2156. type = STRING;
  2157. memnew_placement(_data._mem, String(p_address));
  2158. }
  2159. Variant::Variant(const Variant &p_variant) {
  2160. type = NIL;
  2161. reference(p_variant);
  2162. }
  2163. /*
  2164. Variant::~Variant() {
  2165. clear();
  2166. }*/
  2167. uint32_t Variant::hash() const {
  2168. switch (type) {
  2169. case NIL: {
  2170. return 0;
  2171. } break;
  2172. case BOOL: {
  2173. return _data._bool ? 1 : 0;
  2174. } break;
  2175. case INT: {
  2176. return _data._int;
  2177. } break;
  2178. case REAL: {
  2179. return hash_djb2_one_float(_data._real);
  2180. } break;
  2181. case STRING: {
  2182. return reinterpret_cast<const String *>(_data._mem)->hash();
  2183. } break;
  2184. // math types
  2185. case VECTOR2: {
  2186. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->x);
  2187. return hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->y, hash);
  2188. } break;
  2189. case RECT2: {
  2190. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.x);
  2191. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.y, hash);
  2192. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.x, hash);
  2193. return hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.y, hash);
  2194. } break;
  2195. case TRANSFORM2D: {
  2196. uint32_t hash = 5831;
  2197. for (int i = 0; i < 3; i++) {
  2198. for (int j = 0; j < 2; j++) {
  2199. hash = hash_djb2_one_float(_data._transform2d->elements[i][j], hash);
  2200. }
  2201. }
  2202. return hash;
  2203. } break;
  2204. case VECTOR3: {
  2205. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->x);
  2206. hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->y, hash);
  2207. return hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->z, hash);
  2208. } break;
  2209. case PLANE: {
  2210. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.x);
  2211. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.y, hash);
  2212. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.z, hash);
  2213. return hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->d, hash);
  2214. } break;
  2215. /*
  2216. case QUAT: {
  2217. } break;*/
  2218. case AABB: {
  2219. uint32_t hash = 5831;
  2220. for (int i = 0; i < 3; i++) {
  2221. hash = hash_djb2_one_float(_data._aabb->position[i], hash);
  2222. hash = hash_djb2_one_float(_data._aabb->size[i], hash);
  2223. }
  2224. return hash;
  2225. } break;
  2226. case QUAT: {
  2227. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->x);
  2228. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->y, hash);
  2229. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->z, hash);
  2230. return hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->w, hash);
  2231. } break;
  2232. case BASIS: {
  2233. uint32_t hash = 5831;
  2234. for (int i = 0; i < 3; i++) {
  2235. for (int j = 0; j < 3; j++) {
  2236. hash = hash_djb2_one_float(_data._basis->elements[i][j], hash);
  2237. }
  2238. }
  2239. return hash;
  2240. } break;
  2241. case TRANSFORM: {
  2242. uint32_t hash = 5831;
  2243. for (int i = 0; i < 3; i++) {
  2244. for (int j = 0; j < 3; j++) {
  2245. hash = hash_djb2_one_float(_data._transform->basis.elements[i][j], hash);
  2246. }
  2247. hash = hash_djb2_one_float(_data._transform->origin[i], hash);
  2248. }
  2249. return hash;
  2250. } break;
  2251. // misc types
  2252. case COLOR: {
  2253. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->r);
  2254. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->g, hash);
  2255. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->b, hash);
  2256. return hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->a, hash);
  2257. } break;
  2258. case _RID: {
  2259. return hash_djb2_one_64(reinterpret_cast<const RID *>(_data._mem)->get_id());
  2260. } break;
  2261. case OBJECT: {
  2262. return hash_djb2_one_64(make_uint64_t(_UNSAFE_OBJ_PROXY_PTR(*this)));
  2263. } break;
  2264. case NODE_PATH: {
  2265. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2266. } break;
  2267. case DICTIONARY: {
  2268. return reinterpret_cast<const Dictionary *>(_data._mem)->hash();
  2269. } break;
  2270. case ARRAY: {
  2271. const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
  2272. return arr.hash();
  2273. } break;
  2274. case POOL_BYTE_ARRAY: {
  2275. const PoolVector<uint8_t> &arr = *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  2276. int len = arr.size();
  2277. if (likely(len)) {
  2278. PoolVector<uint8_t>::Read r = arr.read();
  2279. return hash_djb2_buffer((uint8_t *)&r[0], len);
  2280. } else {
  2281. return hash_djb2_one_64(0);
  2282. }
  2283. } break;
  2284. case POOL_INT_ARRAY: {
  2285. const PoolVector<int> &arr = *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  2286. int len = arr.size();
  2287. if (likely(len)) {
  2288. PoolVector<int>::Read r = arr.read();
  2289. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(int));
  2290. } else {
  2291. return hash_djb2_one_64(0);
  2292. }
  2293. } break;
  2294. case POOL_REAL_ARRAY: {
  2295. const PoolVector<real_t> &arr = *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  2296. int len = arr.size();
  2297. if (likely(len)) {
  2298. PoolVector<real_t>::Read r = arr.read();
  2299. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(real_t));
  2300. } else {
  2301. return hash_djb2_one_float(0.0);
  2302. }
  2303. } break;
  2304. case POOL_STRING_ARRAY: {
  2305. uint32_t hash = 5831;
  2306. const PoolVector<String> &arr = *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  2307. int len = arr.size();
  2308. if (likely(len)) {
  2309. PoolVector<String>::Read r = arr.read();
  2310. for (int i = 0; i < len; i++) {
  2311. hash = hash_djb2_one_32(r[i].hash(), hash);
  2312. }
  2313. }
  2314. return hash;
  2315. } break;
  2316. case POOL_VECTOR2_ARRAY: {
  2317. uint32_t hash = 5831;
  2318. const PoolVector<Vector2> &arr = *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  2319. int len = arr.size();
  2320. if (likely(len)) {
  2321. PoolVector<Vector2>::Read r = arr.read();
  2322. for (int i = 0; i < len; i++) {
  2323. hash = hash_djb2_one_float(r[i].x, hash);
  2324. hash = hash_djb2_one_float(r[i].y, hash);
  2325. }
  2326. }
  2327. return hash;
  2328. } break;
  2329. case POOL_VECTOR3_ARRAY: {
  2330. uint32_t hash = 5831;
  2331. const PoolVector<Vector3> &arr = *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  2332. int len = arr.size();
  2333. if (likely(len)) {
  2334. PoolVector<Vector3>::Read r = arr.read();
  2335. for (int i = 0; i < len; i++) {
  2336. hash = hash_djb2_one_float(r[i].x, hash);
  2337. hash = hash_djb2_one_float(r[i].y, hash);
  2338. hash = hash_djb2_one_float(r[i].z, hash);
  2339. }
  2340. }
  2341. return hash;
  2342. } break;
  2343. case POOL_COLOR_ARRAY: {
  2344. uint32_t hash = 5831;
  2345. const PoolVector<Color> &arr = *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  2346. int len = arr.size();
  2347. if (likely(len)) {
  2348. PoolVector<Color>::Read r = arr.read();
  2349. for (int i = 0; i < len; i++) {
  2350. hash = hash_djb2_one_float(r[i].r, hash);
  2351. hash = hash_djb2_one_float(r[i].g, hash);
  2352. hash = hash_djb2_one_float(r[i].b, hash);
  2353. hash = hash_djb2_one_float(r[i].a, hash);
  2354. }
  2355. }
  2356. return hash;
  2357. } break;
  2358. default: {
  2359. }
  2360. }
  2361. return 0;
  2362. }
  2363. #define hash_compare_scalar(p_lhs, p_rhs) \
  2364. ((p_lhs) == (p_rhs)) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs))
  2365. #define hash_compare_vector2(p_lhs, p_rhs) \
  2366. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2367. (hash_compare_scalar((p_lhs).y, (p_rhs).y))
  2368. #define hash_compare_vector3(p_lhs, p_rhs) \
  2369. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2370. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2371. (hash_compare_scalar((p_lhs).z, (p_rhs).z))
  2372. #define hash_compare_quat(p_lhs, p_rhs) \
  2373. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2374. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2375. (hash_compare_scalar((p_lhs).z, (p_rhs).z)) && \
  2376. (hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2377. #define hash_compare_color(p_lhs, p_rhs) \
  2378. (hash_compare_scalar((p_lhs).r, (p_rhs).r)) && \
  2379. (hash_compare_scalar((p_lhs).g, (p_rhs).g)) && \
  2380. (hash_compare_scalar((p_lhs).b, (p_rhs).b)) && \
  2381. (hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2382. #define hash_compare_pool_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2383. const PoolVector<p_type> &l = *reinterpret_cast<const PoolVector<p_type> *>(p_lhs); \
  2384. const PoolVector<p_type> &r = *reinterpret_cast<const PoolVector<p_type> *>(p_rhs); \
  2385. \
  2386. if (l.size() != r.size()) \
  2387. return false; \
  2388. \
  2389. PoolVector<p_type>::Read lr = l.read(); \
  2390. PoolVector<p_type>::Read rr = r.read(); \
  2391. \
  2392. for (int i = 0; i < l.size(); ++i) { \
  2393. if (!p_compare_func((lr[i]), (rr[i]))) \
  2394. return false; \
  2395. } \
  2396. \
  2397. return true
  2398. bool Variant::hash_compare(const Variant &p_variant) const {
  2399. if (type != p_variant.type) {
  2400. return false;
  2401. }
  2402. switch (type) {
  2403. case INT: {
  2404. return _data._int == p_variant._data._int;
  2405. } break;
  2406. case REAL: {
  2407. return hash_compare_scalar(_data._real, p_variant._data._real);
  2408. } break;
  2409. case STRING: {
  2410. return *reinterpret_cast<const String *>(_data._mem) == *reinterpret_cast<const String *>(p_variant._data._mem);
  2411. } break;
  2412. case VECTOR2: {
  2413. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2414. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2415. return hash_compare_vector2(*l, *r);
  2416. } break;
  2417. case RECT2: {
  2418. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2419. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2420. return (hash_compare_vector2(l->position, r->position)) &&
  2421. (hash_compare_vector2(l->size, r->size));
  2422. } break;
  2423. case TRANSFORM2D: {
  2424. Transform2D *l = _data._transform2d;
  2425. Transform2D *r = p_variant._data._transform2d;
  2426. for (int i = 0; i < 3; i++) {
  2427. if (!(hash_compare_vector2(l->elements[i], r->elements[i]))) {
  2428. return false;
  2429. }
  2430. }
  2431. return true;
  2432. } break;
  2433. case VECTOR3: {
  2434. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2435. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2436. return hash_compare_vector3(*l, *r);
  2437. } break;
  2438. case PLANE: {
  2439. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  2440. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  2441. return (hash_compare_vector3(l->normal, r->normal)) &&
  2442. (hash_compare_scalar(l->d, r->d));
  2443. } break;
  2444. case AABB: {
  2445. const ::AABB *l = _data._aabb;
  2446. const ::AABB *r = p_variant._data._aabb;
  2447. return (hash_compare_vector3(l->position, r->position) &&
  2448. (hash_compare_vector3(l->size, r->size)));
  2449. } break;
  2450. case QUAT: {
  2451. const Quat *l = reinterpret_cast<const Quat *>(_data._mem);
  2452. const Quat *r = reinterpret_cast<const Quat *>(p_variant._data._mem);
  2453. return hash_compare_quat(*l, *r);
  2454. } break;
  2455. case BASIS: {
  2456. const Basis *l = _data._basis;
  2457. const Basis *r = p_variant._data._basis;
  2458. for (int i = 0; i < 3; i++) {
  2459. if (!(hash_compare_vector3(l->elements[i], r->elements[i]))) {
  2460. return false;
  2461. }
  2462. }
  2463. return true;
  2464. } break;
  2465. case TRANSFORM: {
  2466. const Transform *l = _data._transform;
  2467. const Transform *r = p_variant._data._transform;
  2468. for (int i = 0; i < 3; i++) {
  2469. if (!(hash_compare_vector3(l->basis.elements[i], r->basis.elements[i]))) {
  2470. return false;
  2471. }
  2472. }
  2473. return hash_compare_vector3(l->origin, r->origin);
  2474. } break;
  2475. case COLOR: {
  2476. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  2477. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  2478. return hash_compare_color(*l, *r);
  2479. } break;
  2480. case ARRAY: {
  2481. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  2482. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  2483. if (l.size() != r.size()) {
  2484. return false;
  2485. }
  2486. for (int i = 0; i < l.size(); ++i) {
  2487. if (!l[i].hash_compare(r[i])) {
  2488. return false;
  2489. }
  2490. }
  2491. return true;
  2492. } break;
  2493. case POOL_REAL_ARRAY: {
  2494. hash_compare_pool_array(_data._mem, p_variant._data._mem, real_t, hash_compare_scalar);
  2495. } break;
  2496. case POOL_VECTOR2_ARRAY: {
  2497. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector2, hash_compare_vector2);
  2498. } break;
  2499. case POOL_VECTOR3_ARRAY: {
  2500. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector3, hash_compare_vector3);
  2501. } break;
  2502. case POOL_COLOR_ARRAY: {
  2503. hash_compare_pool_array(_data._mem, p_variant._data._mem, Color, hash_compare_color);
  2504. } break;
  2505. default:
  2506. bool v;
  2507. Variant r;
  2508. evaluate(OP_EQUAL, *this, p_variant, r, v);
  2509. return r;
  2510. }
  2511. return false;
  2512. }
  2513. bool Variant::is_ref() const {
  2514. return type == OBJECT && !_get_obj().ref.is_null();
  2515. }
  2516. Vector<Variant> varray() {
  2517. return Vector<Variant>();
  2518. }
  2519. Vector<Variant> varray(const Variant &p_arg1) {
  2520. Vector<Variant> v;
  2521. v.push_back(p_arg1);
  2522. return v;
  2523. }
  2524. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2) {
  2525. Vector<Variant> v;
  2526. v.push_back(p_arg1);
  2527. v.push_back(p_arg2);
  2528. return v;
  2529. }
  2530. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3) {
  2531. Vector<Variant> v;
  2532. v.push_back(p_arg1);
  2533. v.push_back(p_arg2);
  2534. v.push_back(p_arg3);
  2535. return v;
  2536. }
  2537. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4) {
  2538. Vector<Variant> v;
  2539. v.push_back(p_arg1);
  2540. v.push_back(p_arg2);
  2541. v.push_back(p_arg3);
  2542. v.push_back(p_arg4);
  2543. return v;
  2544. }
  2545. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5) {
  2546. Vector<Variant> v;
  2547. v.push_back(p_arg1);
  2548. v.push_back(p_arg2);
  2549. v.push_back(p_arg3);
  2550. v.push_back(p_arg4);
  2551. v.push_back(p_arg5);
  2552. return v;
  2553. }
  2554. void Variant::static_assign(const Variant &p_variant) {
  2555. }
  2556. bool Variant::is_shared() const {
  2557. switch (type) {
  2558. case OBJECT:
  2559. return true;
  2560. case ARRAY:
  2561. return true;
  2562. case DICTIONARY:
  2563. return true;
  2564. default: {
  2565. }
  2566. }
  2567. return false;
  2568. }
  2569. Variant Variant::call(const StringName &p_method, VARIANT_ARG_DECLARE) {
  2570. VARIANT_ARGPTRS;
  2571. int argc = 0;
  2572. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  2573. if (argptr[i]->get_type() == Variant::NIL) {
  2574. break;
  2575. }
  2576. argc++;
  2577. }
  2578. CallError error;
  2579. Variant ret = call(p_method, argptr, argc, error);
  2580. switch (error.error) {
  2581. case CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2582. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(error.expected) + "'.";
  2583. ERR_PRINT(err.utf8().get_data());
  2584. } break;
  2585. case CallError::CALL_ERROR_INVALID_METHOD: {
  2586. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  2587. ERR_PRINT(err.utf8().get_data());
  2588. } break;
  2589. case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  2590. String err = "Too many arguments for method '" + p_method + "'";
  2591. ERR_PRINT(err.utf8().get_data());
  2592. } break;
  2593. default: {
  2594. }
  2595. }
  2596. return ret;
  2597. }
  2598. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  2599. r_value = Variant();
  2600. }
  2601. String Variant::get_construct_string() const {
  2602. String vars;
  2603. VariantWriter::write_to_string(*this, vars);
  2604. return vars;
  2605. }
  2606. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Variant::CallError &ce) {
  2607. String err_text;
  2608. if (ce.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  2609. int errorarg = ce.argument;
  2610. if (p_argptrs) {
  2611. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(p_argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(ce.expected) + ".";
  2612. } else {
  2613. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(ce.expected) + ".";
  2614. }
  2615. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  2616. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2617. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  2618. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2619. } else if (ce.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  2620. err_text = "Method not found.";
  2621. } else if (ce.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  2622. err_text = "Instance is null";
  2623. } else if (ce.error == Variant::CallError::CALL_OK) {
  2624. return "Call OK";
  2625. }
  2626. String class_name = p_base->get_class();
  2627. Ref<Script> script = p_base->get_script();
  2628. if (script.is_valid() && script->get_path().is_resource_file()) {
  2629. class_name += "(" + script->get_path().get_file() + ")";
  2630. }
  2631. return "'" + class_name + "::" + String(p_method) + "': " + err_text;
  2632. }
  2633. String vformat(const String &p_text, const Variant &p1, const Variant &p2, const Variant &p3, const Variant &p4, const Variant &p5) {
  2634. Array args;
  2635. if (p1.get_type() != Variant::NIL) {
  2636. args.push_back(p1);
  2637. if (p2.get_type() != Variant::NIL) {
  2638. args.push_back(p2);
  2639. if (p3.get_type() != Variant::NIL) {
  2640. args.push_back(p3);
  2641. if (p4.get_type() != Variant::NIL) {
  2642. args.push_back(p4);
  2643. if (p5.get_type() != Variant::NIL) {
  2644. args.push_back(p5);
  2645. }
  2646. }
  2647. }
  2648. }
  2649. }
  2650. bool error = false;
  2651. String fmt = p_text.sprintf(args, &error);
  2652. ERR_FAIL_COND_V_MSG(error, String(), fmt);
  2653. return fmt;
  2654. }