variant.cpp 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721
  1. /**************************************************************************/
  2. /* variant.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "variant.h"
  31. #include "core/core_string_names.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/io/json.h"
  34. #include "core/io/marshalls.h"
  35. #include "core/io/resource.h"
  36. #include "core/math/math_funcs.h"
  37. #include "core/string/print_string.h"
  38. #include "core/variant/variant_parser.h"
  39. PagedAllocator<Variant::Pools::BucketSmall, true> Variant::Pools::_bucket_small;
  40. PagedAllocator<Variant::Pools::BucketMedium, true> Variant::Pools::_bucket_medium;
  41. PagedAllocator<Variant::Pools::BucketLarge, true> Variant::Pools::_bucket_large;
  42. String Variant::get_type_name(Variant::Type p_type) {
  43. switch (p_type) {
  44. case NIL: {
  45. return "Nil";
  46. }
  47. // Atomic types.
  48. case BOOL: {
  49. return "bool";
  50. }
  51. case INT: {
  52. return "int";
  53. }
  54. case FLOAT: {
  55. return "float";
  56. }
  57. case STRING: {
  58. return "String";
  59. }
  60. // Math types.
  61. case VECTOR2: {
  62. return "Vector2";
  63. }
  64. case VECTOR2I: {
  65. return "Vector2i";
  66. }
  67. case RECT2: {
  68. return "Rect2";
  69. }
  70. case RECT2I: {
  71. return "Rect2i";
  72. }
  73. case TRANSFORM2D: {
  74. return "Transform2D";
  75. }
  76. case VECTOR3: {
  77. return "Vector3";
  78. }
  79. case VECTOR3I: {
  80. return "Vector3i";
  81. }
  82. case VECTOR4: {
  83. return "Vector4";
  84. }
  85. case VECTOR4I: {
  86. return "Vector4i";
  87. }
  88. case PLANE: {
  89. return "Plane";
  90. }
  91. case AABB: {
  92. return "AABB";
  93. }
  94. case QUATERNION: {
  95. return "Quaternion";
  96. }
  97. case BASIS: {
  98. return "Basis";
  99. }
  100. case TRANSFORM3D: {
  101. return "Transform3D";
  102. }
  103. case PROJECTION: {
  104. return "Projection";
  105. }
  106. // Miscellaneous types.
  107. case COLOR: {
  108. return "Color";
  109. }
  110. case RID: {
  111. return "RID";
  112. }
  113. case OBJECT: {
  114. return "Object";
  115. }
  116. case CALLABLE: {
  117. return "Callable";
  118. }
  119. case SIGNAL: {
  120. return "Signal";
  121. }
  122. case STRING_NAME: {
  123. return "StringName";
  124. }
  125. case NODE_PATH: {
  126. return "NodePath";
  127. }
  128. case DICTIONARY: {
  129. return "Dictionary";
  130. }
  131. case ARRAY: {
  132. return "Array";
  133. }
  134. // Arrays.
  135. case PACKED_BYTE_ARRAY: {
  136. return "PackedByteArray";
  137. }
  138. case PACKED_INT32_ARRAY: {
  139. return "PackedInt32Array";
  140. }
  141. case PACKED_INT64_ARRAY: {
  142. return "PackedInt64Array";
  143. }
  144. case PACKED_FLOAT32_ARRAY: {
  145. return "PackedFloat32Array";
  146. }
  147. case PACKED_FLOAT64_ARRAY: {
  148. return "PackedFloat64Array";
  149. }
  150. case PACKED_STRING_ARRAY: {
  151. return "PackedStringArray";
  152. }
  153. case PACKED_VECTOR2_ARRAY: {
  154. return "PackedVector2Array";
  155. }
  156. case PACKED_VECTOR3_ARRAY: {
  157. return "PackedVector3Array";
  158. }
  159. case PACKED_COLOR_ARRAY: {
  160. return "PackedColorArray";
  161. }
  162. default: {
  163. }
  164. }
  165. return "";
  166. }
  167. bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {
  168. if (p_type_from == p_type_to) {
  169. return true;
  170. }
  171. if (p_type_to == NIL) { //nil can convert to anything
  172. return true;
  173. }
  174. if (p_type_from == NIL) {
  175. return (p_type_to == OBJECT);
  176. }
  177. const Type *valid_types = nullptr;
  178. const Type *invalid_types = nullptr;
  179. switch (p_type_to) {
  180. case BOOL: {
  181. static const Type valid[] = {
  182. INT,
  183. FLOAT,
  184. STRING,
  185. NIL,
  186. };
  187. valid_types = valid;
  188. } break;
  189. case INT: {
  190. static const Type valid[] = {
  191. BOOL,
  192. FLOAT,
  193. STRING,
  194. NIL,
  195. };
  196. valid_types = valid;
  197. } break;
  198. case FLOAT: {
  199. static const Type valid[] = {
  200. BOOL,
  201. INT,
  202. STRING,
  203. NIL,
  204. };
  205. valid_types = valid;
  206. } break;
  207. case STRING: {
  208. static const Type invalid[] = {
  209. OBJECT,
  210. NIL
  211. };
  212. invalid_types = invalid;
  213. } break;
  214. case VECTOR2: {
  215. static const Type valid[] = {
  216. VECTOR2I,
  217. NIL,
  218. };
  219. valid_types = valid;
  220. } break;
  221. case VECTOR2I: {
  222. static const Type valid[] = {
  223. VECTOR2,
  224. NIL,
  225. };
  226. valid_types = valid;
  227. } break;
  228. case RECT2: {
  229. static const Type valid[] = {
  230. RECT2I,
  231. NIL,
  232. };
  233. valid_types = valid;
  234. } break;
  235. case RECT2I: {
  236. static const Type valid[] = {
  237. RECT2,
  238. NIL,
  239. };
  240. valid_types = valid;
  241. } break;
  242. case TRANSFORM2D: {
  243. static const Type valid[] = {
  244. TRANSFORM3D,
  245. NIL
  246. };
  247. valid_types = valid;
  248. } break;
  249. case VECTOR3: {
  250. static const Type valid[] = {
  251. VECTOR3I,
  252. NIL,
  253. };
  254. valid_types = valid;
  255. } break;
  256. case VECTOR3I: {
  257. static const Type valid[] = {
  258. VECTOR3,
  259. NIL,
  260. };
  261. valid_types = valid;
  262. } break;
  263. case VECTOR4: {
  264. static const Type valid[] = {
  265. VECTOR4I,
  266. NIL,
  267. };
  268. valid_types = valid;
  269. } break;
  270. case VECTOR4I: {
  271. static const Type valid[] = {
  272. VECTOR4,
  273. NIL,
  274. };
  275. valid_types = valid;
  276. } break;
  277. case QUATERNION: {
  278. static const Type valid[] = {
  279. BASIS,
  280. NIL
  281. };
  282. valid_types = valid;
  283. } break;
  284. case BASIS: {
  285. static const Type valid[] = {
  286. QUATERNION,
  287. NIL
  288. };
  289. valid_types = valid;
  290. } break;
  291. case TRANSFORM3D: {
  292. static const Type valid[] = {
  293. TRANSFORM2D,
  294. QUATERNION,
  295. BASIS,
  296. PROJECTION,
  297. NIL
  298. };
  299. valid_types = valid;
  300. } break;
  301. case PROJECTION: {
  302. static const Type valid[] = {
  303. TRANSFORM3D,
  304. NIL
  305. };
  306. valid_types = valid;
  307. } break;
  308. case COLOR: {
  309. static const Type valid[] = {
  310. STRING,
  311. INT,
  312. NIL,
  313. };
  314. valid_types = valid;
  315. } break;
  316. case RID: {
  317. static const Type valid[] = {
  318. OBJECT,
  319. NIL
  320. };
  321. valid_types = valid;
  322. } break;
  323. case OBJECT: {
  324. static const Type valid[] = {
  325. NIL
  326. };
  327. valid_types = valid;
  328. } break;
  329. case STRING_NAME: {
  330. static const Type valid[] = {
  331. STRING,
  332. NIL
  333. };
  334. valid_types = valid;
  335. } break;
  336. case NODE_PATH: {
  337. static const Type valid[] = {
  338. STRING,
  339. NIL
  340. };
  341. valid_types = valid;
  342. } break;
  343. case ARRAY: {
  344. static const Type valid[] = {
  345. PACKED_BYTE_ARRAY,
  346. PACKED_INT32_ARRAY,
  347. PACKED_INT64_ARRAY,
  348. PACKED_FLOAT32_ARRAY,
  349. PACKED_FLOAT64_ARRAY,
  350. PACKED_STRING_ARRAY,
  351. PACKED_COLOR_ARRAY,
  352. PACKED_VECTOR2_ARRAY,
  353. PACKED_VECTOR3_ARRAY,
  354. NIL
  355. };
  356. valid_types = valid;
  357. } break;
  358. // arrays
  359. case PACKED_BYTE_ARRAY: {
  360. static const Type valid[] = {
  361. ARRAY,
  362. NIL
  363. };
  364. valid_types = valid;
  365. } break;
  366. case PACKED_INT32_ARRAY: {
  367. static const Type valid[] = {
  368. ARRAY,
  369. NIL
  370. };
  371. valid_types = valid;
  372. } break;
  373. case PACKED_INT64_ARRAY: {
  374. static const Type valid[] = {
  375. ARRAY,
  376. NIL
  377. };
  378. valid_types = valid;
  379. } break;
  380. case PACKED_FLOAT32_ARRAY: {
  381. static const Type valid[] = {
  382. ARRAY,
  383. NIL
  384. };
  385. valid_types = valid;
  386. } break;
  387. case PACKED_FLOAT64_ARRAY: {
  388. static const Type valid[] = {
  389. ARRAY,
  390. NIL
  391. };
  392. valid_types = valid;
  393. } break;
  394. case PACKED_STRING_ARRAY: {
  395. static const Type valid[] = {
  396. ARRAY,
  397. NIL
  398. };
  399. valid_types = valid;
  400. } break;
  401. case PACKED_VECTOR2_ARRAY: {
  402. static const Type valid[] = {
  403. ARRAY,
  404. NIL
  405. };
  406. valid_types = valid;
  407. } break;
  408. case PACKED_VECTOR3_ARRAY: {
  409. static const Type valid[] = {
  410. ARRAY,
  411. NIL
  412. };
  413. valid_types = valid;
  414. } break;
  415. case PACKED_COLOR_ARRAY: {
  416. static const Type valid[] = {
  417. ARRAY,
  418. NIL
  419. };
  420. valid_types = valid;
  421. } break;
  422. default: {
  423. }
  424. }
  425. if (valid_types) {
  426. int i = 0;
  427. while (valid_types[i] != NIL) {
  428. if (p_type_from == valid_types[i]) {
  429. return true;
  430. }
  431. i++;
  432. }
  433. } else if (invalid_types) {
  434. int i = 0;
  435. while (invalid_types[i] != NIL) {
  436. if (p_type_from == invalid_types[i]) {
  437. return false;
  438. }
  439. i++;
  440. }
  441. return true;
  442. }
  443. return false;
  444. }
  445. bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type_to) {
  446. if (p_type_from == p_type_to) {
  447. return true;
  448. }
  449. if (p_type_to == NIL) { //nil can convert to anything
  450. return true;
  451. }
  452. if (p_type_from == NIL) {
  453. return (p_type_to == OBJECT);
  454. }
  455. const Type *valid_types = nullptr;
  456. switch (p_type_to) {
  457. case BOOL: {
  458. static const Type valid[] = {
  459. INT,
  460. FLOAT,
  461. //STRING,
  462. NIL,
  463. };
  464. valid_types = valid;
  465. } break;
  466. case INT: {
  467. static const Type valid[] = {
  468. BOOL,
  469. FLOAT,
  470. //STRING,
  471. NIL,
  472. };
  473. valid_types = valid;
  474. } break;
  475. case FLOAT: {
  476. static const Type valid[] = {
  477. BOOL,
  478. INT,
  479. //STRING,
  480. NIL,
  481. };
  482. valid_types = valid;
  483. } break;
  484. case STRING: {
  485. static const Type valid[] = {
  486. NODE_PATH,
  487. STRING_NAME,
  488. NIL
  489. };
  490. valid_types = valid;
  491. } break;
  492. case VECTOR2: {
  493. static const Type valid[] = {
  494. VECTOR2I,
  495. NIL,
  496. };
  497. valid_types = valid;
  498. } break;
  499. case VECTOR2I: {
  500. static const Type valid[] = {
  501. VECTOR2,
  502. NIL,
  503. };
  504. valid_types = valid;
  505. } break;
  506. case RECT2: {
  507. static const Type valid[] = {
  508. RECT2I,
  509. NIL,
  510. };
  511. valid_types = valid;
  512. } break;
  513. case RECT2I: {
  514. static const Type valid[] = {
  515. RECT2,
  516. NIL,
  517. };
  518. valid_types = valid;
  519. } break;
  520. case TRANSFORM2D: {
  521. static const Type valid[] = {
  522. TRANSFORM3D,
  523. NIL
  524. };
  525. valid_types = valid;
  526. } break;
  527. case VECTOR3: {
  528. static const Type valid[] = {
  529. VECTOR3I,
  530. NIL,
  531. };
  532. valid_types = valid;
  533. } break;
  534. case VECTOR3I: {
  535. static const Type valid[] = {
  536. VECTOR3,
  537. NIL,
  538. };
  539. valid_types = valid;
  540. } break;
  541. case VECTOR4: {
  542. static const Type valid[] = {
  543. VECTOR4I,
  544. NIL,
  545. };
  546. valid_types = valid;
  547. } break;
  548. case VECTOR4I: {
  549. static const Type valid[] = {
  550. VECTOR4,
  551. NIL,
  552. };
  553. valid_types = valid;
  554. } break;
  555. case QUATERNION: {
  556. static const Type valid[] = {
  557. BASIS,
  558. NIL
  559. };
  560. valid_types = valid;
  561. } break;
  562. case BASIS: {
  563. static const Type valid[] = {
  564. QUATERNION,
  565. NIL
  566. };
  567. valid_types = valid;
  568. } break;
  569. case TRANSFORM3D: {
  570. static const Type valid[] = {
  571. TRANSFORM2D,
  572. QUATERNION,
  573. BASIS,
  574. PROJECTION,
  575. NIL
  576. };
  577. valid_types = valid;
  578. } break;
  579. case PROJECTION: {
  580. static const Type valid[] = {
  581. TRANSFORM3D,
  582. NIL
  583. };
  584. valid_types = valid;
  585. } break;
  586. case COLOR: {
  587. static const Type valid[] = {
  588. STRING,
  589. INT,
  590. NIL,
  591. };
  592. valid_types = valid;
  593. } break;
  594. case RID: {
  595. static const Type valid[] = {
  596. OBJECT,
  597. NIL
  598. };
  599. valid_types = valid;
  600. } break;
  601. case OBJECT: {
  602. static const Type valid[] = {
  603. NIL
  604. };
  605. valid_types = valid;
  606. } break;
  607. case STRING_NAME: {
  608. static const Type valid[] = {
  609. STRING,
  610. NIL
  611. };
  612. valid_types = valid;
  613. } break;
  614. case NODE_PATH: {
  615. static const Type valid[] = {
  616. STRING,
  617. NIL
  618. };
  619. valid_types = valid;
  620. } break;
  621. case ARRAY: {
  622. static const Type valid[] = {
  623. PACKED_BYTE_ARRAY,
  624. PACKED_INT32_ARRAY,
  625. PACKED_INT64_ARRAY,
  626. PACKED_FLOAT32_ARRAY,
  627. PACKED_FLOAT64_ARRAY,
  628. PACKED_STRING_ARRAY,
  629. PACKED_COLOR_ARRAY,
  630. PACKED_VECTOR2_ARRAY,
  631. PACKED_VECTOR3_ARRAY,
  632. NIL
  633. };
  634. valid_types = valid;
  635. } break;
  636. // arrays
  637. case PACKED_BYTE_ARRAY: {
  638. static const Type valid[] = {
  639. ARRAY,
  640. NIL
  641. };
  642. valid_types = valid;
  643. } break;
  644. case PACKED_INT32_ARRAY: {
  645. static const Type valid[] = {
  646. ARRAY,
  647. NIL
  648. };
  649. valid_types = valid;
  650. } break;
  651. case PACKED_INT64_ARRAY: {
  652. static const Type valid[] = {
  653. ARRAY,
  654. NIL
  655. };
  656. valid_types = valid;
  657. } break;
  658. case PACKED_FLOAT32_ARRAY: {
  659. static const Type valid[] = {
  660. ARRAY,
  661. NIL
  662. };
  663. valid_types = valid;
  664. } break;
  665. case PACKED_FLOAT64_ARRAY: {
  666. static const Type valid[] = {
  667. ARRAY,
  668. NIL
  669. };
  670. valid_types = valid;
  671. } break;
  672. case PACKED_STRING_ARRAY: {
  673. static const Type valid[] = {
  674. ARRAY,
  675. NIL
  676. };
  677. valid_types = valid;
  678. } break;
  679. case PACKED_VECTOR2_ARRAY: {
  680. static const Type valid[] = {
  681. ARRAY,
  682. NIL
  683. };
  684. valid_types = valid;
  685. } break;
  686. case PACKED_VECTOR3_ARRAY: {
  687. static const Type valid[] = {
  688. ARRAY,
  689. NIL
  690. };
  691. valid_types = valid;
  692. } break;
  693. case PACKED_COLOR_ARRAY: {
  694. static const Type valid[] = {
  695. ARRAY,
  696. NIL
  697. };
  698. valid_types = valid;
  699. } break;
  700. default: {
  701. }
  702. }
  703. if (valid_types) {
  704. int i = 0;
  705. while (valid_types[i] != NIL) {
  706. if (p_type_from == valid_types[i]) {
  707. return true;
  708. }
  709. i++;
  710. }
  711. }
  712. return false;
  713. }
  714. bool Variant::operator==(const Variant &p_variant) const {
  715. return hash_compare(p_variant);
  716. }
  717. bool Variant::operator!=(const Variant &p_variant) const {
  718. // Don't use `!hash_compare(p_variant)` given it makes use of OP_EQUAL
  719. if (type != p_variant.type) { //evaluation of operator== needs to be more strict
  720. return true;
  721. }
  722. bool v;
  723. Variant r;
  724. evaluate(OP_NOT_EQUAL, *this, p_variant, r, v);
  725. return r;
  726. }
  727. bool Variant::operator<(const Variant &p_variant) const {
  728. if (type != p_variant.type) { //if types differ, then order by type first
  729. return type < p_variant.type;
  730. }
  731. bool v;
  732. Variant r;
  733. evaluate(OP_LESS, *this, p_variant, r, v);
  734. return r;
  735. }
  736. bool Variant::is_zero() const {
  737. switch (type) {
  738. case NIL: {
  739. return true;
  740. }
  741. // Atomic types.
  742. case BOOL: {
  743. return !(_data._bool);
  744. }
  745. case INT: {
  746. return _data._int == 0;
  747. }
  748. case FLOAT: {
  749. return _data._float == 0;
  750. }
  751. case STRING: {
  752. return *reinterpret_cast<const String *>(_data._mem) == String();
  753. }
  754. // Math types.
  755. case VECTOR2: {
  756. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2();
  757. }
  758. case VECTOR2I: {
  759. return *reinterpret_cast<const Vector2i *>(_data._mem) == Vector2i();
  760. }
  761. case RECT2: {
  762. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2();
  763. }
  764. case RECT2I: {
  765. return *reinterpret_cast<const Rect2i *>(_data._mem) == Rect2i();
  766. }
  767. case TRANSFORM2D: {
  768. return *_data._transform2d == Transform2D();
  769. }
  770. case VECTOR3: {
  771. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3();
  772. }
  773. case VECTOR3I: {
  774. return *reinterpret_cast<const Vector3i *>(_data._mem) == Vector3i();
  775. }
  776. case VECTOR4: {
  777. return *reinterpret_cast<const Vector4 *>(_data._mem) == Vector4();
  778. }
  779. case VECTOR4I: {
  780. return *reinterpret_cast<const Vector4i *>(_data._mem) == Vector4i();
  781. }
  782. case PLANE: {
  783. return *reinterpret_cast<const Plane *>(_data._mem) == Plane();
  784. }
  785. case AABB: {
  786. return *_data._aabb == ::AABB();
  787. }
  788. case QUATERNION: {
  789. return *reinterpret_cast<const Quaternion *>(_data._mem) == Quaternion();
  790. }
  791. case BASIS: {
  792. return *_data._basis == Basis();
  793. }
  794. case TRANSFORM3D: {
  795. return *_data._transform3d == Transform3D();
  796. }
  797. case PROJECTION: {
  798. return *_data._projection == Projection();
  799. }
  800. // Miscellaneous types.
  801. case COLOR: {
  802. return *reinterpret_cast<const Color *>(_data._mem) == Color();
  803. }
  804. case RID: {
  805. return *reinterpret_cast<const ::RID *>(_data._mem) == ::RID();
  806. }
  807. case OBJECT: {
  808. return _get_obj().obj == nullptr;
  809. }
  810. case CALLABLE: {
  811. return reinterpret_cast<const Callable *>(_data._mem)->is_null();
  812. }
  813. case SIGNAL: {
  814. return reinterpret_cast<const Signal *>(_data._mem)->is_null();
  815. }
  816. case STRING_NAME: {
  817. return *reinterpret_cast<const StringName *>(_data._mem) == StringName();
  818. }
  819. case NODE_PATH: {
  820. return reinterpret_cast<const NodePath *>(_data._mem)->is_empty();
  821. }
  822. case DICTIONARY: {
  823. return reinterpret_cast<const Dictionary *>(_data._mem)->is_empty();
  824. }
  825. case ARRAY: {
  826. return reinterpret_cast<const Array *>(_data._mem)->is_empty();
  827. }
  828. // Arrays.
  829. case PACKED_BYTE_ARRAY: {
  830. return PackedArrayRef<uint8_t>::get_array(_data.packed_array).size() == 0;
  831. }
  832. case PACKED_INT32_ARRAY: {
  833. return PackedArrayRef<int32_t>::get_array(_data.packed_array).size() == 0;
  834. }
  835. case PACKED_INT64_ARRAY: {
  836. return PackedArrayRef<int64_t>::get_array(_data.packed_array).size() == 0;
  837. }
  838. case PACKED_FLOAT32_ARRAY: {
  839. return PackedArrayRef<float>::get_array(_data.packed_array).size() == 0;
  840. }
  841. case PACKED_FLOAT64_ARRAY: {
  842. return PackedArrayRef<double>::get_array(_data.packed_array).size() == 0;
  843. }
  844. case PACKED_STRING_ARRAY: {
  845. return PackedArrayRef<String>::get_array(_data.packed_array).size() == 0;
  846. }
  847. case PACKED_VECTOR2_ARRAY: {
  848. return PackedArrayRef<Vector2>::get_array(_data.packed_array).size() == 0;
  849. }
  850. case PACKED_VECTOR3_ARRAY: {
  851. return PackedArrayRef<Vector3>::get_array(_data.packed_array).size() == 0;
  852. }
  853. case PACKED_COLOR_ARRAY: {
  854. return PackedArrayRef<Color>::get_array(_data.packed_array).size() == 0;
  855. }
  856. default: {
  857. }
  858. }
  859. return false;
  860. }
  861. bool Variant::is_one() const {
  862. switch (type) {
  863. case NIL: {
  864. return true;
  865. }
  866. case BOOL: {
  867. return _data._bool;
  868. }
  869. case INT: {
  870. return _data._int == 1;
  871. }
  872. case FLOAT: {
  873. return _data._float == 1;
  874. }
  875. case VECTOR2: {
  876. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(1, 1);
  877. }
  878. case VECTOR2I: {
  879. return *reinterpret_cast<const Vector2i *>(_data._mem) == Vector2i(1, 1);
  880. }
  881. case RECT2: {
  882. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(1, 1, 1, 1);
  883. }
  884. case RECT2I: {
  885. return *reinterpret_cast<const Rect2i *>(_data._mem) == Rect2i(1, 1, 1, 1);
  886. }
  887. case VECTOR3: {
  888. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(1, 1, 1);
  889. }
  890. case VECTOR3I: {
  891. return *reinterpret_cast<const Vector3i *>(_data._mem) == Vector3i(1, 1, 1);
  892. }
  893. case VECTOR4: {
  894. return *reinterpret_cast<const Vector4 *>(_data._mem) == Vector4(1, 1, 1, 1);
  895. }
  896. case VECTOR4I: {
  897. return *reinterpret_cast<const Vector4i *>(_data._mem) == Vector4i(1, 1, 1, 1);
  898. }
  899. case PLANE: {
  900. return *reinterpret_cast<const Plane *>(_data._mem) == Plane(1, 1, 1, 1);
  901. }
  902. case COLOR: {
  903. return *reinterpret_cast<const Color *>(_data._mem) == Color(1, 1, 1, 1);
  904. }
  905. default: {
  906. return !is_zero();
  907. }
  908. }
  909. }
  910. bool Variant::is_null() const {
  911. if (type == OBJECT && _get_obj().obj) {
  912. return false;
  913. } else {
  914. return true;
  915. }
  916. }
  917. bool Variant::initialize_ref(Object *p_object) {
  918. RefCounted *ref_counted = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_object));
  919. if (!ref_counted->init_ref()) {
  920. return false;
  921. }
  922. return true;
  923. }
  924. void Variant::reference(const Variant &p_variant) {
  925. switch (type) {
  926. case NIL:
  927. case BOOL:
  928. case INT:
  929. case FLOAT:
  930. break;
  931. default:
  932. clear();
  933. }
  934. type = p_variant.type;
  935. switch (p_variant.type) {
  936. case NIL: {
  937. // None.
  938. } break;
  939. // Atomic types.
  940. case BOOL: {
  941. _data._bool = p_variant._data._bool;
  942. } break;
  943. case INT: {
  944. _data._int = p_variant._data._int;
  945. } break;
  946. case FLOAT: {
  947. _data._float = p_variant._data._float;
  948. } break;
  949. case STRING: {
  950. memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem)));
  951. } break;
  952. // Math types.
  953. case VECTOR2: {
  954. memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem)));
  955. } break;
  956. case VECTOR2I: {
  957. memnew_placement(_data._mem, Vector2i(*reinterpret_cast<const Vector2i *>(p_variant._data._mem)));
  958. } break;
  959. case RECT2: {
  960. memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem)));
  961. } break;
  962. case RECT2I: {
  963. memnew_placement(_data._mem, Rect2i(*reinterpret_cast<const Rect2i *>(p_variant._data._mem)));
  964. } break;
  965. case TRANSFORM2D: {
  966. _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
  967. memnew_placement(_data._transform2d, Transform2D(*p_variant._data._transform2d));
  968. } break;
  969. case VECTOR3: {
  970. memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(p_variant._data._mem)));
  971. } break;
  972. case VECTOR3I: {
  973. memnew_placement(_data._mem, Vector3i(*reinterpret_cast<const Vector3i *>(p_variant._data._mem)));
  974. } break;
  975. case VECTOR4: {
  976. memnew_placement(_data._mem, Vector4(*reinterpret_cast<const Vector4 *>(p_variant._data._mem)));
  977. } break;
  978. case VECTOR4I: {
  979. memnew_placement(_data._mem, Vector4i(*reinterpret_cast<const Vector4i *>(p_variant._data._mem)));
  980. } break;
  981. case PLANE: {
  982. memnew_placement(_data._mem, Plane(*reinterpret_cast<const Plane *>(p_variant._data._mem)));
  983. } break;
  984. case AABB: {
  985. _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
  986. memnew_placement(_data._aabb, ::AABB(*p_variant._data._aabb));
  987. } break;
  988. case QUATERNION: {
  989. memnew_placement(_data._mem, Quaternion(*reinterpret_cast<const Quaternion *>(p_variant._data._mem)));
  990. } break;
  991. case BASIS: {
  992. _data._basis = (Basis *)Pools::_bucket_medium.alloc();
  993. memnew_placement(_data._basis, Basis(*p_variant._data._basis));
  994. } break;
  995. case TRANSFORM3D: {
  996. _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
  997. memnew_placement(_data._transform3d, Transform3D(*p_variant._data._transform3d));
  998. } break;
  999. case PROJECTION: {
  1000. _data._projection = (Projection *)Pools::_bucket_large.alloc();
  1001. memnew_placement(_data._projection, Projection(*p_variant._data._projection));
  1002. } break;
  1003. // Miscellaneous types.
  1004. case COLOR: {
  1005. memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem)));
  1006. } break;
  1007. case RID: {
  1008. memnew_placement(_data._mem, ::RID(*reinterpret_cast<const ::RID *>(p_variant._data._mem)));
  1009. } break;
  1010. case OBJECT: {
  1011. memnew_placement(_data._mem, ObjData);
  1012. if (p_variant._get_obj().obj && p_variant._get_obj().id.is_ref_counted()) {
  1013. RefCounted *ref_counted = static_cast<RefCounted *>(p_variant._get_obj().obj);
  1014. if (!ref_counted->reference()) {
  1015. _get_obj().obj = nullptr;
  1016. _get_obj().id = ObjectID();
  1017. break;
  1018. }
  1019. }
  1020. _get_obj().obj = const_cast<Object *>(p_variant._get_obj().obj);
  1021. _get_obj().id = p_variant._get_obj().id;
  1022. } break;
  1023. case CALLABLE: {
  1024. memnew_placement(_data._mem, Callable(*reinterpret_cast<const Callable *>(p_variant._data._mem)));
  1025. } break;
  1026. case SIGNAL: {
  1027. memnew_placement(_data._mem, Signal(*reinterpret_cast<const Signal *>(p_variant._data._mem)));
  1028. } break;
  1029. case STRING_NAME: {
  1030. memnew_placement(_data._mem, StringName(*reinterpret_cast<const StringName *>(p_variant._data._mem)));
  1031. } break;
  1032. case NODE_PATH: {
  1033. memnew_placement(_data._mem, NodePath(*reinterpret_cast<const NodePath *>(p_variant._data._mem)));
  1034. } break;
  1035. case DICTIONARY: {
  1036. memnew_placement(_data._mem, Dictionary(*reinterpret_cast<const Dictionary *>(p_variant._data._mem)));
  1037. } break;
  1038. case ARRAY: {
  1039. memnew_placement(_data._mem, Array(*reinterpret_cast<const Array *>(p_variant._data._mem)));
  1040. } break;
  1041. // Arrays.
  1042. case PACKED_BYTE_ARRAY: {
  1043. _data.packed_array = static_cast<PackedArrayRef<uint8_t> *>(p_variant._data.packed_array)->reference();
  1044. if (!_data.packed_array) {
  1045. _data.packed_array = PackedArrayRef<uint8_t>::create();
  1046. }
  1047. } break;
  1048. case PACKED_INT32_ARRAY: {
  1049. _data.packed_array = static_cast<PackedArrayRef<int32_t> *>(p_variant._data.packed_array)->reference();
  1050. if (!_data.packed_array) {
  1051. _data.packed_array = PackedArrayRef<int32_t>::create();
  1052. }
  1053. } break;
  1054. case PACKED_INT64_ARRAY: {
  1055. _data.packed_array = static_cast<PackedArrayRef<int64_t> *>(p_variant._data.packed_array)->reference();
  1056. if (!_data.packed_array) {
  1057. _data.packed_array = PackedArrayRef<int64_t>::create();
  1058. }
  1059. } break;
  1060. case PACKED_FLOAT32_ARRAY: {
  1061. _data.packed_array = static_cast<PackedArrayRef<float> *>(p_variant._data.packed_array)->reference();
  1062. if (!_data.packed_array) {
  1063. _data.packed_array = PackedArrayRef<float>::create();
  1064. }
  1065. } break;
  1066. case PACKED_FLOAT64_ARRAY: {
  1067. _data.packed_array = static_cast<PackedArrayRef<double> *>(p_variant._data.packed_array)->reference();
  1068. if (!_data.packed_array) {
  1069. _data.packed_array = PackedArrayRef<double>::create();
  1070. }
  1071. } break;
  1072. case PACKED_STRING_ARRAY: {
  1073. _data.packed_array = static_cast<PackedArrayRef<String> *>(p_variant._data.packed_array)->reference();
  1074. if (!_data.packed_array) {
  1075. _data.packed_array = PackedArrayRef<String>::create();
  1076. }
  1077. } break;
  1078. case PACKED_VECTOR2_ARRAY: {
  1079. _data.packed_array = static_cast<PackedArrayRef<Vector2> *>(p_variant._data.packed_array)->reference();
  1080. if (!_data.packed_array) {
  1081. _data.packed_array = PackedArrayRef<Vector2>::create();
  1082. }
  1083. } break;
  1084. case PACKED_VECTOR3_ARRAY: {
  1085. _data.packed_array = static_cast<PackedArrayRef<Vector3> *>(p_variant._data.packed_array)->reference();
  1086. if (!_data.packed_array) {
  1087. _data.packed_array = PackedArrayRef<Vector3>::create();
  1088. }
  1089. } break;
  1090. case PACKED_COLOR_ARRAY: {
  1091. _data.packed_array = static_cast<PackedArrayRef<Color> *>(p_variant._data.packed_array)->reference();
  1092. if (!_data.packed_array) {
  1093. _data.packed_array = PackedArrayRef<Color>::create();
  1094. }
  1095. } break;
  1096. default: {
  1097. }
  1098. }
  1099. }
  1100. void Variant::zero() {
  1101. switch (type) {
  1102. case NIL:
  1103. break;
  1104. case BOOL:
  1105. this->_data._bool = false;
  1106. break;
  1107. case INT:
  1108. this->_data._int = 0;
  1109. break;
  1110. case FLOAT:
  1111. this->_data._float = 0;
  1112. break;
  1113. case VECTOR2:
  1114. *reinterpret_cast<Vector2 *>(this->_data._mem) = Vector2();
  1115. break;
  1116. case VECTOR2I:
  1117. *reinterpret_cast<Vector2i *>(this->_data._mem) = Vector2i();
  1118. break;
  1119. case RECT2:
  1120. *reinterpret_cast<Rect2 *>(this->_data._mem) = Rect2();
  1121. break;
  1122. case RECT2I:
  1123. *reinterpret_cast<Rect2i *>(this->_data._mem) = Rect2i();
  1124. break;
  1125. case VECTOR3:
  1126. *reinterpret_cast<Vector3 *>(this->_data._mem) = Vector3();
  1127. break;
  1128. case VECTOR3I:
  1129. *reinterpret_cast<Vector3i *>(this->_data._mem) = Vector3i();
  1130. break;
  1131. case VECTOR4:
  1132. *reinterpret_cast<Vector4 *>(this->_data._mem) = Vector4();
  1133. break;
  1134. case VECTOR4I:
  1135. *reinterpret_cast<Vector4i *>(this->_data._mem) = Vector4i();
  1136. break;
  1137. case PLANE:
  1138. *reinterpret_cast<Plane *>(this->_data._mem) = Plane();
  1139. break;
  1140. case QUATERNION:
  1141. *reinterpret_cast<Quaternion *>(this->_data._mem) = Quaternion();
  1142. break;
  1143. case COLOR:
  1144. *reinterpret_cast<Color *>(this->_data._mem) = Color();
  1145. break;
  1146. default:
  1147. this->clear();
  1148. break;
  1149. }
  1150. }
  1151. void Variant::_clear_internal() {
  1152. switch (type) {
  1153. case STRING: {
  1154. reinterpret_cast<String *>(_data._mem)->~String();
  1155. } break;
  1156. // Math types.
  1157. case TRANSFORM2D: {
  1158. if (_data._transform2d) {
  1159. _data._transform2d->~Transform2D();
  1160. Pools::_bucket_small.free((Pools::BucketSmall *)_data._transform2d);
  1161. _data._transform2d = nullptr;
  1162. }
  1163. } break;
  1164. case AABB: {
  1165. if (_data._aabb) {
  1166. _data._aabb->~AABB();
  1167. Pools::_bucket_small.free((Pools::BucketSmall *)_data._aabb);
  1168. _data._aabb = nullptr;
  1169. }
  1170. } break;
  1171. case BASIS: {
  1172. if (_data._basis) {
  1173. _data._basis->~Basis();
  1174. Pools::_bucket_medium.free((Pools::BucketMedium *)_data._basis);
  1175. _data._basis = nullptr;
  1176. }
  1177. } break;
  1178. case TRANSFORM3D: {
  1179. if (_data._transform3d) {
  1180. _data._transform3d->~Transform3D();
  1181. Pools::_bucket_medium.free((Pools::BucketMedium *)_data._transform3d);
  1182. _data._transform3d = nullptr;
  1183. }
  1184. } break;
  1185. case PROJECTION: {
  1186. if (_data._projection) {
  1187. _data._projection->~Projection();
  1188. Pools::_bucket_large.free((Pools::BucketLarge *)_data._projection);
  1189. _data._projection = nullptr;
  1190. }
  1191. } break;
  1192. // Miscellaneous types.
  1193. case STRING_NAME: {
  1194. reinterpret_cast<StringName *>(_data._mem)->~StringName();
  1195. } break;
  1196. case NODE_PATH: {
  1197. reinterpret_cast<NodePath *>(_data._mem)->~NodePath();
  1198. } break;
  1199. case OBJECT: {
  1200. if (_get_obj().id.is_ref_counted()) {
  1201. // We are safe that there is a reference here.
  1202. RefCounted *ref_counted = static_cast<RefCounted *>(_get_obj().obj);
  1203. if (ref_counted->unreference()) {
  1204. memdelete(ref_counted);
  1205. }
  1206. }
  1207. _get_obj().obj = nullptr;
  1208. _get_obj().id = ObjectID();
  1209. } break;
  1210. case RID: {
  1211. // Not much need probably.
  1212. // HACK: Can't seem to use destructor + scoping operator, so hack.
  1213. typedef ::RID RID_Class;
  1214. reinterpret_cast<RID_Class *>(_data._mem)->~RID_Class();
  1215. } break;
  1216. case CALLABLE: {
  1217. reinterpret_cast<Callable *>(_data._mem)->~Callable();
  1218. } break;
  1219. case SIGNAL: {
  1220. reinterpret_cast<Signal *>(_data._mem)->~Signal();
  1221. } break;
  1222. case DICTIONARY: {
  1223. reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary();
  1224. } break;
  1225. case ARRAY: {
  1226. reinterpret_cast<Array *>(_data._mem)->~Array();
  1227. } break;
  1228. // Arrays.
  1229. case PACKED_BYTE_ARRAY: {
  1230. PackedArrayRefBase::destroy(_data.packed_array);
  1231. } break;
  1232. case PACKED_INT32_ARRAY: {
  1233. PackedArrayRefBase::destroy(_data.packed_array);
  1234. } break;
  1235. case PACKED_INT64_ARRAY: {
  1236. PackedArrayRefBase::destroy(_data.packed_array);
  1237. } break;
  1238. case PACKED_FLOAT32_ARRAY: {
  1239. PackedArrayRefBase::destroy(_data.packed_array);
  1240. } break;
  1241. case PACKED_FLOAT64_ARRAY: {
  1242. PackedArrayRefBase::destroy(_data.packed_array);
  1243. } break;
  1244. case PACKED_STRING_ARRAY: {
  1245. PackedArrayRefBase::destroy(_data.packed_array);
  1246. } break;
  1247. case PACKED_VECTOR2_ARRAY: {
  1248. PackedArrayRefBase::destroy(_data.packed_array);
  1249. } break;
  1250. case PACKED_VECTOR3_ARRAY: {
  1251. PackedArrayRefBase::destroy(_data.packed_array);
  1252. } break;
  1253. case PACKED_COLOR_ARRAY: {
  1254. PackedArrayRefBase::destroy(_data.packed_array);
  1255. } break;
  1256. default: {
  1257. // Not needed, there is no point. The following do not allocate memory:
  1258. // VECTOR2, VECTOR3, RECT2, PLANE, QUATERNION, COLOR.
  1259. }
  1260. }
  1261. }
  1262. Variant::operator signed int() const {
  1263. switch (type) {
  1264. case NIL:
  1265. return 0;
  1266. case BOOL:
  1267. return _data._bool ? 1 : 0;
  1268. case INT:
  1269. return _data._int;
  1270. case FLOAT:
  1271. return _data._float;
  1272. case STRING:
  1273. return operator String().to_int();
  1274. default: {
  1275. return 0;
  1276. }
  1277. }
  1278. }
  1279. Variant::operator unsigned int() const {
  1280. switch (type) {
  1281. case NIL:
  1282. return 0;
  1283. case BOOL:
  1284. return _data._bool ? 1 : 0;
  1285. case INT:
  1286. return _data._int;
  1287. case FLOAT:
  1288. return _data._float;
  1289. case STRING:
  1290. return operator String().to_int();
  1291. default: {
  1292. return 0;
  1293. }
  1294. }
  1295. }
  1296. Variant::operator int64_t() const {
  1297. switch (type) {
  1298. case NIL:
  1299. return 0;
  1300. case BOOL:
  1301. return _data._bool ? 1 : 0;
  1302. case INT:
  1303. return _data._int;
  1304. case FLOAT:
  1305. return _data._float;
  1306. case STRING:
  1307. return operator String().to_int();
  1308. default: {
  1309. return 0;
  1310. }
  1311. }
  1312. }
  1313. Variant::operator uint64_t() const {
  1314. switch (type) {
  1315. case NIL:
  1316. return 0;
  1317. case BOOL:
  1318. return _data._bool ? 1 : 0;
  1319. case INT:
  1320. return _data._int;
  1321. case FLOAT:
  1322. return _data._float;
  1323. case STRING:
  1324. return operator String().to_int();
  1325. default: {
  1326. return 0;
  1327. }
  1328. }
  1329. }
  1330. Variant::operator ObjectID() const {
  1331. if (type == INT) {
  1332. return ObjectID(_data._int);
  1333. } else if (type == OBJECT) {
  1334. return _get_obj().id;
  1335. } else {
  1336. return ObjectID();
  1337. }
  1338. }
  1339. #ifdef NEED_LONG_INT
  1340. Variant::operator signed long() const {
  1341. switch (type) {
  1342. case NIL:
  1343. return 0;
  1344. case BOOL:
  1345. return _data._bool ? 1 : 0;
  1346. case INT:
  1347. return _data._int;
  1348. case FLOAT:
  1349. return _data._float;
  1350. case STRING:
  1351. return operator String().to_int();
  1352. default: {
  1353. return 0;
  1354. }
  1355. }
  1356. return 0;
  1357. }
  1358. Variant::operator unsigned long() const {
  1359. switch (type) {
  1360. case NIL:
  1361. return 0;
  1362. case BOOL:
  1363. return _data._bool ? 1 : 0;
  1364. case INT:
  1365. return _data._int;
  1366. case FLOAT:
  1367. return _data._float;
  1368. case STRING:
  1369. return operator String().to_int();
  1370. default: {
  1371. return 0;
  1372. }
  1373. }
  1374. return 0;
  1375. }
  1376. #endif
  1377. Variant::operator signed short() const {
  1378. switch (type) {
  1379. case NIL:
  1380. return 0;
  1381. case BOOL:
  1382. return _data._bool ? 1 : 0;
  1383. case INT:
  1384. return _data._int;
  1385. case FLOAT:
  1386. return _data._float;
  1387. case STRING:
  1388. return operator String().to_int();
  1389. default: {
  1390. return 0;
  1391. }
  1392. }
  1393. }
  1394. Variant::operator unsigned short() const {
  1395. switch (type) {
  1396. case NIL:
  1397. return 0;
  1398. case BOOL:
  1399. return _data._bool ? 1 : 0;
  1400. case INT:
  1401. return _data._int;
  1402. case FLOAT:
  1403. return _data._float;
  1404. case STRING:
  1405. return operator String().to_int();
  1406. default: {
  1407. return 0;
  1408. }
  1409. }
  1410. }
  1411. Variant::operator signed char() const {
  1412. switch (type) {
  1413. case NIL:
  1414. return 0;
  1415. case BOOL:
  1416. return _data._bool ? 1 : 0;
  1417. case INT:
  1418. return _data._int;
  1419. case FLOAT:
  1420. return _data._float;
  1421. case STRING:
  1422. return operator String().to_int();
  1423. default: {
  1424. return 0;
  1425. }
  1426. }
  1427. }
  1428. Variant::operator unsigned char() const {
  1429. switch (type) {
  1430. case NIL:
  1431. return 0;
  1432. case BOOL:
  1433. return _data._bool ? 1 : 0;
  1434. case INT:
  1435. return _data._int;
  1436. case FLOAT:
  1437. return _data._float;
  1438. case STRING:
  1439. return operator String().to_int();
  1440. default: {
  1441. return 0;
  1442. }
  1443. }
  1444. }
  1445. Variant::operator char32_t() const {
  1446. return operator unsigned int();
  1447. }
  1448. Variant::operator float() const {
  1449. switch (type) {
  1450. case NIL:
  1451. return 0;
  1452. case BOOL:
  1453. return _data._bool ? 1.0 : 0.0;
  1454. case INT:
  1455. return (float)_data._int;
  1456. case FLOAT:
  1457. return _data._float;
  1458. case STRING:
  1459. return operator String().to_float();
  1460. default: {
  1461. return 0;
  1462. }
  1463. }
  1464. }
  1465. Variant::operator double() const {
  1466. switch (type) {
  1467. case NIL:
  1468. return 0;
  1469. case BOOL:
  1470. return _data._bool ? 1.0 : 0.0;
  1471. case INT:
  1472. return (double)_data._int;
  1473. case FLOAT:
  1474. return _data._float;
  1475. case STRING:
  1476. return operator String().to_float();
  1477. default: {
  1478. return 0;
  1479. }
  1480. }
  1481. }
  1482. Variant::operator StringName() const {
  1483. if (type == STRING_NAME) {
  1484. return *reinterpret_cast<const StringName *>(_data._mem);
  1485. } else if (type == STRING) {
  1486. return *reinterpret_cast<const String *>(_data._mem);
  1487. }
  1488. return StringName();
  1489. }
  1490. struct _VariantStrPair {
  1491. String key;
  1492. String value;
  1493. bool operator<(const _VariantStrPair &p) const {
  1494. return key < p.key;
  1495. }
  1496. };
  1497. Variant::operator String() const {
  1498. return stringify(0);
  1499. }
  1500. String stringify_variant_clean(const Variant p_variant, int recursion_count) {
  1501. String s = p_variant.stringify(recursion_count);
  1502. // Wrap strings in quotes to avoid ambiguity.
  1503. switch (p_variant.get_type()) {
  1504. case Variant::STRING: {
  1505. s = s.c_escape().quote();
  1506. } break;
  1507. case Variant::STRING_NAME: {
  1508. s = "&" + s.c_escape().quote();
  1509. } break;
  1510. case Variant::NODE_PATH: {
  1511. s = "^" + s.c_escape().quote();
  1512. } break;
  1513. default: {
  1514. } break;
  1515. }
  1516. return s;
  1517. }
  1518. template <class T>
  1519. String stringify_vector(const T &vec, int recursion_count) {
  1520. String str("[");
  1521. for (int i = 0; i < vec.size(); i++) {
  1522. if (i > 0) {
  1523. str += ", ";
  1524. }
  1525. str += stringify_variant_clean(vec[i], recursion_count);
  1526. }
  1527. str += "]";
  1528. return str;
  1529. }
  1530. String Variant::stringify(int recursion_count) const {
  1531. switch (type) {
  1532. case NIL:
  1533. return "<null>";
  1534. case BOOL:
  1535. return _data._bool ? "true" : "false";
  1536. case INT:
  1537. return itos(_data._int);
  1538. case FLOAT:
  1539. return rtos(_data._float);
  1540. case STRING:
  1541. return *reinterpret_cast<const String *>(_data._mem);
  1542. case VECTOR2:
  1543. return operator Vector2();
  1544. case VECTOR2I:
  1545. return operator Vector2i();
  1546. case RECT2:
  1547. return operator Rect2();
  1548. case RECT2I:
  1549. return operator Rect2i();
  1550. case TRANSFORM2D:
  1551. return operator Transform2D();
  1552. case VECTOR3:
  1553. return operator Vector3();
  1554. case VECTOR3I:
  1555. return operator Vector3i();
  1556. case VECTOR4:
  1557. return operator Vector4();
  1558. case VECTOR4I:
  1559. return operator Vector4i();
  1560. case PLANE:
  1561. return operator Plane();
  1562. case AABB:
  1563. return operator ::AABB();
  1564. case QUATERNION:
  1565. return operator Quaternion();
  1566. case BASIS:
  1567. return operator Basis();
  1568. case TRANSFORM3D:
  1569. return operator Transform3D();
  1570. case PROJECTION:
  1571. return operator Projection();
  1572. case STRING_NAME:
  1573. return operator StringName();
  1574. case NODE_PATH:
  1575. return operator NodePath();
  1576. case COLOR:
  1577. return operator Color();
  1578. case DICTIONARY: {
  1579. const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
  1580. if (recursion_count > MAX_RECURSION) {
  1581. ERR_PRINT("Maximum dictionary recursion reached!");
  1582. return "{ ... }";
  1583. }
  1584. // Add leading and trailing space to Dictionary printing. This distinguishes it
  1585. // from array printing on fonts that have similar-looking {} and [] characters.
  1586. String str("{ ");
  1587. List<Variant> keys;
  1588. d.get_key_list(&keys);
  1589. Vector<_VariantStrPair> pairs;
  1590. recursion_count++;
  1591. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1592. _VariantStrPair sp;
  1593. sp.key = stringify_variant_clean(E->get(), recursion_count);
  1594. sp.value = stringify_variant_clean(d[E->get()], recursion_count);
  1595. pairs.push_back(sp);
  1596. }
  1597. for (int i = 0; i < pairs.size(); i++) {
  1598. if (i > 0) {
  1599. str += ", ";
  1600. }
  1601. str += pairs[i].key + ": " + pairs[i].value;
  1602. }
  1603. str += " }";
  1604. return str;
  1605. }
  1606. case PACKED_VECTOR2_ARRAY: {
  1607. return stringify_vector(operator Vector<Vector2>(), recursion_count);
  1608. }
  1609. case PACKED_VECTOR3_ARRAY: {
  1610. return stringify_vector(operator Vector<Vector3>(), recursion_count);
  1611. }
  1612. case PACKED_COLOR_ARRAY: {
  1613. return stringify_vector(operator Vector<Color>(), recursion_count);
  1614. }
  1615. case PACKED_STRING_ARRAY: {
  1616. return stringify_vector(operator Vector<String>(), recursion_count);
  1617. }
  1618. case PACKED_BYTE_ARRAY: {
  1619. return stringify_vector(operator Vector<uint8_t>(), recursion_count);
  1620. }
  1621. case PACKED_INT32_ARRAY: {
  1622. return stringify_vector(operator Vector<int32_t>(), recursion_count);
  1623. }
  1624. case PACKED_INT64_ARRAY: {
  1625. return stringify_vector(operator Vector<int64_t>(), recursion_count);
  1626. }
  1627. case PACKED_FLOAT32_ARRAY: {
  1628. return stringify_vector(operator Vector<float>(), recursion_count);
  1629. }
  1630. case PACKED_FLOAT64_ARRAY: {
  1631. return stringify_vector(operator Vector<double>(), recursion_count);
  1632. }
  1633. case ARRAY: {
  1634. Array arr = operator Array();
  1635. if (recursion_count > MAX_RECURSION) {
  1636. ERR_PRINT("Maximum array recursion reached!");
  1637. return "[...]";
  1638. }
  1639. return stringify_vector(arr, recursion_count);
  1640. }
  1641. case OBJECT: {
  1642. if (_get_obj().obj) {
  1643. if (!_get_obj().id.is_ref_counted() && ObjectDB::get_instance(_get_obj().id) == nullptr) {
  1644. return "<Freed Object>";
  1645. }
  1646. return _get_obj().obj->to_string();
  1647. } else {
  1648. return "<Object#null>";
  1649. }
  1650. }
  1651. case CALLABLE: {
  1652. const Callable &c = *reinterpret_cast<const Callable *>(_data._mem);
  1653. return c;
  1654. }
  1655. case SIGNAL: {
  1656. const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
  1657. return s;
  1658. }
  1659. case RID: {
  1660. const ::RID &s = *reinterpret_cast<const ::RID *>(_data._mem);
  1661. return "RID(" + itos(s.get_id()) + ")";
  1662. }
  1663. default: {
  1664. return "<" + get_type_name(type) + ">";
  1665. }
  1666. }
  1667. }
  1668. String Variant::to_json_string() const {
  1669. return JSON::stringify(*this);
  1670. }
  1671. Variant::operator Vector2() const {
  1672. if (type == VECTOR2) {
  1673. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1674. } else if (type == VECTOR2I) {
  1675. return *reinterpret_cast<const Vector2i *>(_data._mem);
  1676. } else if (type == VECTOR3) {
  1677. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1678. } else if (type == VECTOR3I) {
  1679. return Vector2(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y);
  1680. } else if (type == VECTOR4) {
  1681. return Vector2(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y);
  1682. } else if (type == VECTOR4I) {
  1683. return Vector2(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y);
  1684. } else {
  1685. return Vector2();
  1686. }
  1687. }
  1688. Variant::operator Vector2i() const {
  1689. if (type == VECTOR2I) {
  1690. return *reinterpret_cast<const Vector2i *>(_data._mem);
  1691. } else if (type == VECTOR2) {
  1692. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1693. } else if (type == VECTOR3) {
  1694. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1695. } else if (type == VECTOR3I) {
  1696. return Vector2(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y);
  1697. } else if (type == VECTOR4) {
  1698. return Vector2(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y);
  1699. } else if (type == VECTOR4I) {
  1700. return Vector2(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y);
  1701. } else {
  1702. return Vector2i();
  1703. }
  1704. }
  1705. Variant::operator Rect2() const {
  1706. if (type == RECT2) {
  1707. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1708. } else if (type == RECT2I) {
  1709. return *reinterpret_cast<const Rect2i *>(_data._mem);
  1710. } else {
  1711. return Rect2();
  1712. }
  1713. }
  1714. Variant::operator Rect2i() const {
  1715. if (type == RECT2I) {
  1716. return *reinterpret_cast<const Rect2i *>(_data._mem);
  1717. } else if (type == RECT2) {
  1718. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1719. } else {
  1720. return Rect2i();
  1721. }
  1722. }
  1723. Variant::operator Vector3() const {
  1724. if (type == VECTOR3) {
  1725. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1726. } else if (type == VECTOR3I) {
  1727. return *reinterpret_cast<const Vector3i *>(_data._mem);
  1728. } else if (type == VECTOR2) {
  1729. return Vector3(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1730. } else if (type == VECTOR2I) {
  1731. return Vector3(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0);
  1732. } else if (type == VECTOR4) {
  1733. return Vector3(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y, reinterpret_cast<const Vector4 *>(_data._mem)->z);
  1734. } else if (type == VECTOR4I) {
  1735. return Vector3(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y, reinterpret_cast<const Vector4i *>(_data._mem)->z);
  1736. } else {
  1737. return Vector3();
  1738. }
  1739. }
  1740. Variant::operator Vector3i() const {
  1741. if (type == VECTOR3I) {
  1742. return *reinterpret_cast<const Vector3i *>(_data._mem);
  1743. } else if (type == VECTOR3) {
  1744. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1745. } else if (type == VECTOR2) {
  1746. return Vector3i(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1747. } else if (type == VECTOR2I) {
  1748. return Vector3i(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0);
  1749. } else if (type == VECTOR4) {
  1750. return Vector3i(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y, reinterpret_cast<const Vector4 *>(_data._mem)->z);
  1751. } else if (type == VECTOR4I) {
  1752. return Vector3i(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y, reinterpret_cast<const Vector4i *>(_data._mem)->z);
  1753. } else {
  1754. return Vector3i();
  1755. }
  1756. }
  1757. Variant::operator Vector4() const {
  1758. if (type == VECTOR4) {
  1759. return *reinterpret_cast<const Vector4 *>(_data._mem);
  1760. } else if (type == VECTOR4I) {
  1761. return *reinterpret_cast<const Vector4i *>(_data._mem);
  1762. } else if (type == VECTOR2) {
  1763. return Vector4(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0, 0.0);
  1764. } else if (type == VECTOR2I) {
  1765. return Vector4(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0, 0.0);
  1766. } else if (type == VECTOR3) {
  1767. return Vector4(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y, reinterpret_cast<const Vector3 *>(_data._mem)->z, 0.0);
  1768. } else if (type == VECTOR3I) {
  1769. return Vector4(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y, reinterpret_cast<const Vector3i *>(_data._mem)->z, 0.0);
  1770. } else {
  1771. return Vector4();
  1772. }
  1773. }
  1774. Variant::operator Vector4i() const {
  1775. if (type == VECTOR4I) {
  1776. return *reinterpret_cast<const Vector4i *>(_data._mem);
  1777. } else if (type == VECTOR4) {
  1778. const Vector4 &v4 = *reinterpret_cast<const Vector4 *>(_data._mem);
  1779. return Vector4i(v4.x, v4.y, v4.z, v4.w);
  1780. } else if (type == VECTOR2) {
  1781. return Vector4i(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0, 0.0);
  1782. } else if (type == VECTOR2I) {
  1783. return Vector4i(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0, 0.0);
  1784. } else if (type == VECTOR3) {
  1785. return Vector4i(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y, reinterpret_cast<const Vector3 *>(_data._mem)->z, 0.0);
  1786. } else if (type == VECTOR3I) {
  1787. return Vector4i(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y, reinterpret_cast<const Vector3i *>(_data._mem)->z, 0.0);
  1788. } else {
  1789. return Vector4i();
  1790. }
  1791. }
  1792. Variant::operator Plane() const {
  1793. if (type == PLANE) {
  1794. return *reinterpret_cast<const Plane *>(_data._mem);
  1795. } else {
  1796. return Plane();
  1797. }
  1798. }
  1799. Variant::operator ::AABB() const {
  1800. if (type == AABB) {
  1801. return *_data._aabb;
  1802. } else {
  1803. return ::AABB();
  1804. }
  1805. }
  1806. Variant::operator Basis() const {
  1807. if (type == BASIS) {
  1808. return *_data._basis;
  1809. } else if (type == QUATERNION) {
  1810. return *reinterpret_cast<const Quaternion *>(_data._mem);
  1811. } else if (type == TRANSFORM3D) { // unexposed in Variant::can_convert?
  1812. return _data._transform3d->basis;
  1813. } else {
  1814. return Basis();
  1815. }
  1816. }
  1817. Variant::operator Quaternion() const {
  1818. if (type == QUATERNION) {
  1819. return *reinterpret_cast<const Quaternion *>(_data._mem);
  1820. } else if (type == BASIS) {
  1821. return *_data._basis;
  1822. } else if (type == TRANSFORM3D) {
  1823. return _data._transform3d->basis;
  1824. } else {
  1825. return Quaternion();
  1826. }
  1827. }
  1828. Variant::operator Transform3D() const {
  1829. if (type == TRANSFORM3D) {
  1830. return *_data._transform3d;
  1831. } else if (type == BASIS) {
  1832. return Transform3D(*_data._basis, Vector3());
  1833. } else if (type == QUATERNION) {
  1834. return Transform3D(Basis(*reinterpret_cast<const Quaternion *>(_data._mem)), Vector3());
  1835. } else if (type == TRANSFORM2D) {
  1836. const Transform2D &t = *_data._transform2d;
  1837. Transform3D m;
  1838. m.basis.rows[0][0] = t.columns[0][0];
  1839. m.basis.rows[1][0] = t.columns[0][1];
  1840. m.basis.rows[0][1] = t.columns[1][0];
  1841. m.basis.rows[1][1] = t.columns[1][1];
  1842. m.origin[0] = t.columns[2][0];
  1843. m.origin[1] = t.columns[2][1];
  1844. return m;
  1845. } else if (type == PROJECTION) {
  1846. return *_data._projection;
  1847. } else {
  1848. return Transform3D();
  1849. }
  1850. }
  1851. Variant::operator Projection() const {
  1852. if (type == TRANSFORM3D) {
  1853. return *_data._transform3d;
  1854. } else if (type == BASIS) {
  1855. return Transform3D(*_data._basis, Vector3());
  1856. } else if (type == QUATERNION) {
  1857. return Transform3D(Basis(*reinterpret_cast<const Quaternion *>(_data._mem)), Vector3());
  1858. } else if (type == TRANSFORM2D) {
  1859. const Transform2D &t = *_data._transform2d;
  1860. Transform3D m;
  1861. m.basis.rows[0][0] = t.columns[0][0];
  1862. m.basis.rows[1][0] = t.columns[0][1];
  1863. m.basis.rows[0][1] = t.columns[1][0];
  1864. m.basis.rows[1][1] = t.columns[1][1];
  1865. m.origin[0] = t.columns[2][0];
  1866. m.origin[1] = t.columns[2][1];
  1867. return m;
  1868. } else if (type == PROJECTION) {
  1869. return *_data._projection;
  1870. } else {
  1871. return Projection();
  1872. }
  1873. }
  1874. Variant::operator Transform2D() const {
  1875. if (type == TRANSFORM2D) {
  1876. return *_data._transform2d;
  1877. } else if (type == TRANSFORM3D) {
  1878. const Transform3D &t = *_data._transform3d;
  1879. Transform2D m;
  1880. m.columns[0][0] = t.basis.rows[0][0];
  1881. m.columns[0][1] = t.basis.rows[1][0];
  1882. m.columns[1][0] = t.basis.rows[0][1];
  1883. m.columns[1][1] = t.basis.rows[1][1];
  1884. m.columns[2][0] = t.origin[0];
  1885. m.columns[2][1] = t.origin[1];
  1886. return m;
  1887. } else {
  1888. return Transform2D();
  1889. }
  1890. }
  1891. Variant::operator Color() const {
  1892. if (type == COLOR) {
  1893. return *reinterpret_cast<const Color *>(_data._mem);
  1894. } else if (type == STRING) {
  1895. return Color(operator String());
  1896. } else if (type == INT) {
  1897. return Color::hex(operator int());
  1898. } else {
  1899. return Color();
  1900. }
  1901. }
  1902. Variant::operator NodePath() const {
  1903. if (type == NODE_PATH) {
  1904. return *reinterpret_cast<const NodePath *>(_data._mem);
  1905. } else if (type == STRING) {
  1906. return NodePath(operator String());
  1907. } else {
  1908. return NodePath();
  1909. }
  1910. }
  1911. Variant::operator ::RID() const {
  1912. if (type == RID) {
  1913. return *reinterpret_cast<const ::RID *>(_data._mem);
  1914. } else if (type == OBJECT && _get_obj().obj == nullptr) {
  1915. return ::RID();
  1916. } else if (type == OBJECT && _get_obj().obj) {
  1917. #ifdef DEBUG_ENABLED
  1918. if (EngineDebugger::is_active()) {
  1919. ERR_FAIL_COND_V_MSG(ObjectDB::get_instance(_get_obj().id) == nullptr, ::RID(), "Invalid pointer (object was freed).");
  1920. }
  1921. #endif
  1922. Callable::CallError ce;
  1923. Variant ret = _get_obj().obj->callp(CoreStringNames::get_singleton()->get_rid, nullptr, 0, ce);
  1924. if (ce.error == Callable::CallError::CALL_OK && ret.get_type() == Variant::RID) {
  1925. return ret;
  1926. }
  1927. return ::RID();
  1928. } else {
  1929. return ::RID();
  1930. }
  1931. }
  1932. Variant::operator Object *() const {
  1933. if (type == OBJECT) {
  1934. return _get_obj().obj;
  1935. } else {
  1936. return nullptr;
  1937. }
  1938. }
  1939. Object *Variant::get_validated_object_with_check(bool &r_previously_freed) const {
  1940. if (type == OBJECT) {
  1941. Object *instance = ObjectDB::get_instance(_get_obj().id);
  1942. r_previously_freed = !instance && _get_obj().id != ObjectID();
  1943. return instance;
  1944. } else {
  1945. r_previously_freed = false;
  1946. return nullptr;
  1947. }
  1948. }
  1949. Object *Variant::get_validated_object() const {
  1950. if (type == OBJECT) {
  1951. return ObjectDB::get_instance(_get_obj().id);
  1952. } else {
  1953. return nullptr;
  1954. }
  1955. }
  1956. Variant::operator Dictionary() const {
  1957. if (type == DICTIONARY) {
  1958. return *reinterpret_cast<const Dictionary *>(_data._mem);
  1959. } else {
  1960. return Dictionary();
  1961. }
  1962. }
  1963. Variant::operator Callable() const {
  1964. if (type == CALLABLE) {
  1965. return *reinterpret_cast<const Callable *>(_data._mem);
  1966. } else {
  1967. return Callable();
  1968. }
  1969. }
  1970. Variant::operator Signal() const {
  1971. if (type == SIGNAL) {
  1972. return *reinterpret_cast<const Signal *>(_data._mem);
  1973. } else {
  1974. return Signal();
  1975. }
  1976. }
  1977. template <class DA, class SA>
  1978. inline DA _convert_array(const SA &p_array) {
  1979. DA da;
  1980. da.resize(p_array.size());
  1981. for (int i = 0; i < p_array.size(); i++) {
  1982. da.set(i, Variant(p_array.get(i)));
  1983. }
  1984. return da;
  1985. }
  1986. template <class DA>
  1987. inline DA _convert_array_from_variant(const Variant &p_variant) {
  1988. switch (p_variant.get_type()) {
  1989. case Variant::ARRAY: {
  1990. return _convert_array<DA, Array>(p_variant.operator Array());
  1991. }
  1992. case Variant::PACKED_BYTE_ARRAY: {
  1993. return _convert_array<DA, Vector<uint8_t>>(p_variant.operator Vector<uint8_t>());
  1994. }
  1995. case Variant::PACKED_INT32_ARRAY: {
  1996. return _convert_array<DA, Vector<int32_t>>(p_variant.operator Vector<int32_t>());
  1997. }
  1998. case Variant::PACKED_INT64_ARRAY: {
  1999. return _convert_array<DA, Vector<int64_t>>(p_variant.operator Vector<int64_t>());
  2000. }
  2001. case Variant::PACKED_FLOAT32_ARRAY: {
  2002. return _convert_array<DA, Vector<float>>(p_variant.operator Vector<float>());
  2003. }
  2004. case Variant::PACKED_FLOAT64_ARRAY: {
  2005. return _convert_array<DA, Vector<double>>(p_variant.operator Vector<double>());
  2006. }
  2007. case Variant::PACKED_STRING_ARRAY: {
  2008. return _convert_array<DA, Vector<String>>(p_variant.operator Vector<String>());
  2009. }
  2010. case Variant::PACKED_VECTOR2_ARRAY: {
  2011. return _convert_array<DA, Vector<Vector2>>(p_variant.operator Vector<Vector2>());
  2012. }
  2013. case Variant::PACKED_VECTOR3_ARRAY: {
  2014. return _convert_array<DA, Vector<Vector3>>(p_variant.operator Vector<Vector3>());
  2015. }
  2016. case Variant::PACKED_COLOR_ARRAY: {
  2017. return _convert_array<DA, Vector<Color>>(p_variant.operator Vector<Color>());
  2018. }
  2019. default: {
  2020. return DA();
  2021. }
  2022. }
  2023. }
  2024. Variant::operator Array() const {
  2025. if (type == ARRAY) {
  2026. return *reinterpret_cast<const Array *>(_data._mem);
  2027. } else {
  2028. return _convert_array_from_variant<Array>(*this);
  2029. }
  2030. }
  2031. Variant::operator Vector<uint8_t>() const {
  2032. if (type == PACKED_BYTE_ARRAY) {
  2033. return static_cast<PackedArrayRef<uint8_t> *>(_data.packed_array)->array;
  2034. } else {
  2035. return _convert_array_from_variant<Vector<uint8_t>>(*this);
  2036. }
  2037. }
  2038. Variant::operator Vector<int32_t>() const {
  2039. if (type == PACKED_INT32_ARRAY) {
  2040. return static_cast<PackedArrayRef<int32_t> *>(_data.packed_array)->array;
  2041. } else {
  2042. return _convert_array_from_variant<Vector<int>>(*this);
  2043. }
  2044. }
  2045. Variant::operator Vector<int64_t>() const {
  2046. if (type == PACKED_INT64_ARRAY) {
  2047. return static_cast<PackedArrayRef<int64_t> *>(_data.packed_array)->array;
  2048. } else {
  2049. return _convert_array_from_variant<Vector<int64_t>>(*this);
  2050. }
  2051. }
  2052. Variant::operator Vector<float>() const {
  2053. if (type == PACKED_FLOAT32_ARRAY) {
  2054. return static_cast<PackedArrayRef<float> *>(_data.packed_array)->array;
  2055. } else {
  2056. return _convert_array_from_variant<Vector<float>>(*this);
  2057. }
  2058. }
  2059. Variant::operator Vector<double>() const {
  2060. if (type == PACKED_FLOAT64_ARRAY) {
  2061. return static_cast<PackedArrayRef<double> *>(_data.packed_array)->array;
  2062. } else {
  2063. return _convert_array_from_variant<Vector<double>>(*this);
  2064. }
  2065. }
  2066. Variant::operator Vector<String>() const {
  2067. if (type == PACKED_STRING_ARRAY) {
  2068. return static_cast<PackedArrayRef<String> *>(_data.packed_array)->array;
  2069. } else {
  2070. return _convert_array_from_variant<Vector<String>>(*this);
  2071. }
  2072. }
  2073. Variant::operator Vector<Vector3>() const {
  2074. if (type == PACKED_VECTOR3_ARRAY) {
  2075. return static_cast<PackedArrayRef<Vector3> *>(_data.packed_array)->array;
  2076. } else {
  2077. return _convert_array_from_variant<Vector<Vector3>>(*this);
  2078. }
  2079. }
  2080. Variant::operator Vector<Vector2>() const {
  2081. if (type == PACKED_VECTOR2_ARRAY) {
  2082. return static_cast<PackedArrayRef<Vector2> *>(_data.packed_array)->array;
  2083. } else {
  2084. return _convert_array_from_variant<Vector<Vector2>>(*this);
  2085. }
  2086. }
  2087. Variant::operator Vector<Color>() const {
  2088. if (type == PACKED_COLOR_ARRAY) {
  2089. return static_cast<PackedArrayRef<Color> *>(_data.packed_array)->array;
  2090. } else {
  2091. return _convert_array_from_variant<Vector<Color>>(*this);
  2092. }
  2093. }
  2094. /* helpers */
  2095. Variant::operator Vector<::RID>() const {
  2096. Array va = operator Array();
  2097. Vector<::RID> rids;
  2098. rids.resize(va.size());
  2099. for (int i = 0; i < rids.size(); i++) {
  2100. rids.write[i] = va[i];
  2101. }
  2102. return rids;
  2103. }
  2104. Variant::operator Vector<Plane>() const {
  2105. Array va = operator Array();
  2106. Vector<Plane> planes;
  2107. int va_size = va.size();
  2108. if (va_size == 0) {
  2109. return planes;
  2110. }
  2111. planes.resize(va_size);
  2112. Plane *w = planes.ptrw();
  2113. for (int i = 0; i < va_size; i++) {
  2114. w[i] = va[i];
  2115. }
  2116. return planes;
  2117. }
  2118. Variant::operator Vector<Face3>() const {
  2119. Vector<Vector3> va = operator Vector<Vector3>();
  2120. Vector<Face3> faces;
  2121. int va_size = va.size();
  2122. if (va_size == 0) {
  2123. return faces;
  2124. }
  2125. faces.resize(va_size / 3);
  2126. Face3 *w = faces.ptrw();
  2127. const Vector3 *r = va.ptr();
  2128. for (int i = 0; i < va_size; i++) {
  2129. w[i / 3].vertex[i % 3] = r[i];
  2130. }
  2131. return faces;
  2132. }
  2133. Variant::operator Vector<Variant>() const {
  2134. Array va = operator Array();
  2135. Vector<Variant> variants;
  2136. int va_size = va.size();
  2137. if (va_size == 0) {
  2138. return variants;
  2139. }
  2140. variants.resize(va_size);
  2141. Variant *w = variants.ptrw();
  2142. for (int i = 0; i < va_size; i++) {
  2143. w[i] = va[i];
  2144. }
  2145. return variants;
  2146. }
  2147. Variant::operator Vector<StringName>() const {
  2148. Vector<String> from = operator Vector<String>();
  2149. Vector<StringName> to;
  2150. int len = from.size();
  2151. to.resize(len);
  2152. for (int i = 0; i < len; i++) {
  2153. to.write[i] = from[i];
  2154. }
  2155. return to;
  2156. }
  2157. Variant::operator Side() const {
  2158. return (Side) operator int();
  2159. }
  2160. Variant::operator Orientation() const {
  2161. return (Orientation) operator int();
  2162. }
  2163. Variant::operator IPAddress() const {
  2164. if (type == PACKED_FLOAT32_ARRAY || type == PACKED_INT32_ARRAY || type == PACKED_FLOAT64_ARRAY || type == PACKED_INT64_ARRAY || type == PACKED_BYTE_ARRAY) {
  2165. Vector<int> addr = operator Vector<int>();
  2166. if (addr.size() == 4) {
  2167. return IPAddress(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
  2168. }
  2169. }
  2170. return IPAddress(operator String());
  2171. }
  2172. Variant::Variant(bool p_bool) {
  2173. type = BOOL;
  2174. _data._bool = p_bool;
  2175. }
  2176. Variant::Variant(signed int p_int) {
  2177. type = INT;
  2178. _data._int = p_int;
  2179. }
  2180. Variant::Variant(unsigned int p_int) {
  2181. type = INT;
  2182. _data._int = p_int;
  2183. }
  2184. #ifdef NEED_LONG_INT
  2185. Variant::Variant(signed long p_int) {
  2186. type = INT;
  2187. _data._int = p_int;
  2188. }
  2189. Variant::Variant(unsigned long p_int) {
  2190. type = INT;
  2191. _data._int = p_int;
  2192. }
  2193. #endif
  2194. Variant::Variant(int64_t p_int) {
  2195. type = INT;
  2196. _data._int = p_int;
  2197. }
  2198. Variant::Variant(uint64_t p_int) {
  2199. type = INT;
  2200. _data._int = p_int;
  2201. }
  2202. Variant::Variant(signed short p_short) {
  2203. type = INT;
  2204. _data._int = p_short;
  2205. }
  2206. Variant::Variant(unsigned short p_short) {
  2207. type = INT;
  2208. _data._int = p_short;
  2209. }
  2210. Variant::Variant(signed char p_char) {
  2211. type = INT;
  2212. _data._int = p_char;
  2213. }
  2214. Variant::Variant(unsigned char p_char) {
  2215. type = INT;
  2216. _data._int = p_char;
  2217. }
  2218. Variant::Variant(float p_float) {
  2219. type = FLOAT;
  2220. _data._float = p_float;
  2221. }
  2222. Variant::Variant(double p_double) {
  2223. type = FLOAT;
  2224. _data._float = p_double;
  2225. }
  2226. Variant::Variant(const ObjectID &p_id) {
  2227. type = INT;
  2228. _data._int = p_id;
  2229. }
  2230. Variant::Variant(const StringName &p_string) {
  2231. type = STRING_NAME;
  2232. memnew_placement(_data._mem, StringName(p_string));
  2233. }
  2234. Variant::Variant(const String &p_string) {
  2235. type = STRING;
  2236. memnew_placement(_data._mem, String(p_string));
  2237. }
  2238. Variant::Variant(const char *const p_cstring) {
  2239. type = STRING;
  2240. memnew_placement(_data._mem, String((const char *)p_cstring));
  2241. }
  2242. Variant::Variant(const char32_t *p_wstring) {
  2243. type = STRING;
  2244. memnew_placement(_data._mem, String(p_wstring));
  2245. }
  2246. Variant::Variant(const Vector3 &p_vector3) {
  2247. type = VECTOR3;
  2248. memnew_placement(_data._mem, Vector3(p_vector3));
  2249. }
  2250. Variant::Variant(const Vector3i &p_vector3i) {
  2251. type = VECTOR3I;
  2252. memnew_placement(_data._mem, Vector3i(p_vector3i));
  2253. }
  2254. Variant::Variant(const Vector4 &p_vector4) {
  2255. type = VECTOR4;
  2256. memnew_placement(_data._mem, Vector4(p_vector4));
  2257. }
  2258. Variant::Variant(const Vector4i &p_vector4i) {
  2259. type = VECTOR4I;
  2260. memnew_placement(_data._mem, Vector4i(p_vector4i));
  2261. }
  2262. Variant::Variant(const Vector2 &p_vector2) {
  2263. type = VECTOR2;
  2264. memnew_placement(_data._mem, Vector2(p_vector2));
  2265. }
  2266. Variant::Variant(const Vector2i &p_vector2i) {
  2267. type = VECTOR2I;
  2268. memnew_placement(_data._mem, Vector2i(p_vector2i));
  2269. }
  2270. Variant::Variant(const Rect2 &p_rect2) {
  2271. type = RECT2;
  2272. memnew_placement(_data._mem, Rect2(p_rect2));
  2273. }
  2274. Variant::Variant(const Rect2i &p_rect2i) {
  2275. type = RECT2I;
  2276. memnew_placement(_data._mem, Rect2i(p_rect2i));
  2277. }
  2278. Variant::Variant(const Plane &p_plane) {
  2279. type = PLANE;
  2280. memnew_placement(_data._mem, Plane(p_plane));
  2281. }
  2282. Variant::Variant(const ::AABB &p_aabb) {
  2283. type = AABB;
  2284. _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
  2285. memnew_placement(_data._aabb, ::AABB(p_aabb));
  2286. }
  2287. Variant::Variant(const Basis &p_matrix) {
  2288. type = BASIS;
  2289. _data._basis = (Basis *)Pools::_bucket_medium.alloc();
  2290. memnew_placement(_data._basis, Basis(p_matrix));
  2291. }
  2292. Variant::Variant(const Quaternion &p_quaternion) {
  2293. type = QUATERNION;
  2294. memnew_placement(_data._mem, Quaternion(p_quaternion));
  2295. }
  2296. Variant::Variant(const Transform3D &p_transform) {
  2297. type = TRANSFORM3D;
  2298. _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
  2299. memnew_placement(_data._transform3d, Transform3D(p_transform));
  2300. }
  2301. Variant::Variant(const Projection &pp_projection) {
  2302. type = PROJECTION;
  2303. _data._projection = (Projection *)Pools::_bucket_large.alloc();
  2304. memnew_placement(_data._projection, Projection(pp_projection));
  2305. }
  2306. Variant::Variant(const Transform2D &p_transform) {
  2307. type = TRANSFORM2D;
  2308. _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
  2309. memnew_placement(_data._transform2d, Transform2D(p_transform));
  2310. }
  2311. Variant::Variant(const Color &p_color) {
  2312. type = COLOR;
  2313. memnew_placement(_data._mem, Color(p_color));
  2314. }
  2315. Variant::Variant(const NodePath &p_node_path) {
  2316. type = NODE_PATH;
  2317. memnew_placement(_data._mem, NodePath(p_node_path));
  2318. }
  2319. Variant::Variant(const ::RID &p_rid) {
  2320. type = RID;
  2321. memnew_placement(_data._mem, ::RID(p_rid));
  2322. }
  2323. Variant::Variant(const Object *p_object) {
  2324. type = OBJECT;
  2325. memnew_placement(_data._mem, ObjData);
  2326. if (p_object) {
  2327. if (p_object->is_ref_counted()) {
  2328. RefCounted *ref_counted = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_object));
  2329. if (!ref_counted->init_ref()) {
  2330. _get_obj().obj = nullptr;
  2331. _get_obj().id = ObjectID();
  2332. return;
  2333. }
  2334. }
  2335. _get_obj().obj = const_cast<Object *>(p_object);
  2336. _get_obj().id = p_object->get_instance_id();
  2337. } else {
  2338. _get_obj().obj = nullptr;
  2339. _get_obj().id = ObjectID();
  2340. }
  2341. }
  2342. Variant::Variant(const Callable &p_callable) {
  2343. type = CALLABLE;
  2344. memnew_placement(_data._mem, Callable(p_callable));
  2345. }
  2346. Variant::Variant(const Signal &p_callable) {
  2347. type = SIGNAL;
  2348. memnew_placement(_data._mem, Signal(p_callable));
  2349. }
  2350. Variant::Variant(const Dictionary &p_dictionary) {
  2351. type = DICTIONARY;
  2352. memnew_placement(_data._mem, Dictionary(p_dictionary));
  2353. }
  2354. Variant::Variant(const Array &p_array) {
  2355. type = ARRAY;
  2356. memnew_placement(_data._mem, Array(p_array));
  2357. }
  2358. Variant::Variant(const Vector<Plane> &p_array) {
  2359. type = ARRAY;
  2360. Array *plane_array = memnew_placement(_data._mem, Array);
  2361. plane_array->resize(p_array.size());
  2362. for (int i = 0; i < p_array.size(); i++) {
  2363. plane_array->operator[](i) = Variant(p_array[i]);
  2364. }
  2365. }
  2366. Variant::Variant(const Vector<::RID> &p_array) {
  2367. type = ARRAY;
  2368. Array *rid_array = memnew_placement(_data._mem, Array);
  2369. rid_array->resize(p_array.size());
  2370. for (int i = 0; i < p_array.size(); i++) {
  2371. rid_array->set(i, Variant(p_array[i]));
  2372. }
  2373. }
  2374. Variant::Variant(const Vector<uint8_t> &p_byte_array) {
  2375. type = PACKED_BYTE_ARRAY;
  2376. _data.packed_array = PackedArrayRef<uint8_t>::create(p_byte_array);
  2377. }
  2378. Variant::Variant(const Vector<int32_t> &p_int32_array) {
  2379. type = PACKED_INT32_ARRAY;
  2380. _data.packed_array = PackedArrayRef<int32_t>::create(p_int32_array);
  2381. }
  2382. Variant::Variant(const Vector<int64_t> &p_int64_array) {
  2383. type = PACKED_INT64_ARRAY;
  2384. _data.packed_array = PackedArrayRef<int64_t>::create(p_int64_array);
  2385. }
  2386. Variant::Variant(const Vector<float> &p_float32_array) {
  2387. type = PACKED_FLOAT32_ARRAY;
  2388. _data.packed_array = PackedArrayRef<float>::create(p_float32_array);
  2389. }
  2390. Variant::Variant(const Vector<double> &p_float64_array) {
  2391. type = PACKED_FLOAT64_ARRAY;
  2392. _data.packed_array = PackedArrayRef<double>::create(p_float64_array);
  2393. }
  2394. Variant::Variant(const Vector<String> &p_string_array) {
  2395. type = PACKED_STRING_ARRAY;
  2396. _data.packed_array = PackedArrayRef<String>::create(p_string_array);
  2397. }
  2398. Variant::Variant(const Vector<Vector3> &p_vector3_array) {
  2399. type = PACKED_VECTOR3_ARRAY;
  2400. _data.packed_array = PackedArrayRef<Vector3>::create(p_vector3_array);
  2401. }
  2402. Variant::Variant(const Vector<Vector2> &p_vector2_array) {
  2403. type = PACKED_VECTOR2_ARRAY;
  2404. _data.packed_array = PackedArrayRef<Vector2>::create(p_vector2_array);
  2405. }
  2406. Variant::Variant(const Vector<Color> &p_color_array) {
  2407. type = PACKED_COLOR_ARRAY;
  2408. _data.packed_array = PackedArrayRef<Color>::create(p_color_array);
  2409. }
  2410. Variant::Variant(const Vector<Face3> &p_face_array) {
  2411. Vector<Vector3> vertices;
  2412. int face_count = p_face_array.size();
  2413. vertices.resize(face_count * 3);
  2414. if (face_count) {
  2415. const Face3 *r = p_face_array.ptr();
  2416. Vector3 *w = vertices.ptrw();
  2417. for (int i = 0; i < face_count; i++) {
  2418. for (int j = 0; j < 3; j++) {
  2419. w[i * 3 + j] = r[i].vertex[j];
  2420. }
  2421. }
  2422. }
  2423. type = NIL;
  2424. *this = vertices;
  2425. }
  2426. /* helpers */
  2427. Variant::Variant(const Vector<Variant> &p_array) {
  2428. type = NIL;
  2429. Array arr;
  2430. arr.resize(p_array.size());
  2431. for (int i = 0; i < p_array.size(); i++) {
  2432. arr[i] = p_array[i];
  2433. }
  2434. *this = arr;
  2435. }
  2436. Variant::Variant(const Vector<StringName> &p_array) {
  2437. type = NIL;
  2438. Vector<String> v;
  2439. int len = p_array.size();
  2440. v.resize(len);
  2441. for (int i = 0; i < len; i++) {
  2442. v.set(i, p_array[i]);
  2443. }
  2444. *this = v;
  2445. }
  2446. void Variant::operator=(const Variant &p_variant) {
  2447. if (unlikely(this == &p_variant)) {
  2448. return;
  2449. }
  2450. if (unlikely(type != p_variant.type)) {
  2451. reference(p_variant);
  2452. return;
  2453. }
  2454. switch (p_variant.type) {
  2455. case NIL: {
  2456. // none
  2457. } break;
  2458. // atomic types
  2459. case BOOL: {
  2460. _data._bool = p_variant._data._bool;
  2461. } break;
  2462. case INT: {
  2463. _data._int = p_variant._data._int;
  2464. } break;
  2465. case FLOAT: {
  2466. _data._float = p_variant._data._float;
  2467. } break;
  2468. case STRING: {
  2469. *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
  2470. } break;
  2471. // math types
  2472. case VECTOR2: {
  2473. *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2474. } break;
  2475. case VECTOR2I: {
  2476. *reinterpret_cast<Vector2i *>(_data._mem) = *reinterpret_cast<const Vector2i *>(p_variant._data._mem);
  2477. } break;
  2478. case RECT2: {
  2479. *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2480. } break;
  2481. case RECT2I: {
  2482. *reinterpret_cast<Rect2i *>(_data._mem) = *reinterpret_cast<const Rect2i *>(p_variant._data._mem);
  2483. } break;
  2484. case TRANSFORM2D: {
  2485. *_data._transform2d = *(p_variant._data._transform2d);
  2486. } break;
  2487. case VECTOR3: {
  2488. *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2489. } break;
  2490. case VECTOR3I: {
  2491. *reinterpret_cast<Vector3i *>(_data._mem) = *reinterpret_cast<const Vector3i *>(p_variant._data._mem);
  2492. } break;
  2493. case VECTOR4: {
  2494. *reinterpret_cast<Vector4 *>(_data._mem) = *reinterpret_cast<const Vector4 *>(p_variant._data._mem);
  2495. } break;
  2496. case VECTOR4I: {
  2497. *reinterpret_cast<Vector4i *>(_data._mem) = *reinterpret_cast<const Vector4i *>(p_variant._data._mem);
  2498. } break;
  2499. case PLANE: {
  2500. *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
  2501. } break;
  2502. case AABB: {
  2503. *_data._aabb = *(p_variant._data._aabb);
  2504. } break;
  2505. case QUATERNION: {
  2506. *reinterpret_cast<Quaternion *>(_data._mem) = *reinterpret_cast<const Quaternion *>(p_variant._data._mem);
  2507. } break;
  2508. case BASIS: {
  2509. *_data._basis = *(p_variant._data._basis);
  2510. } break;
  2511. case TRANSFORM3D: {
  2512. *_data._transform3d = *(p_variant._data._transform3d);
  2513. } break;
  2514. case PROJECTION: {
  2515. *_data._projection = *(p_variant._data._projection);
  2516. } break;
  2517. // misc types
  2518. case COLOR: {
  2519. *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
  2520. } break;
  2521. case RID: {
  2522. *reinterpret_cast<::RID *>(_data._mem) = *reinterpret_cast<const ::RID *>(p_variant._data._mem);
  2523. } break;
  2524. case OBJECT: {
  2525. if (_get_obj().id.is_ref_counted()) {
  2526. //we are safe that there is a reference here
  2527. RefCounted *ref_counted = static_cast<RefCounted *>(_get_obj().obj);
  2528. if (ref_counted->unreference()) {
  2529. memdelete(ref_counted);
  2530. }
  2531. }
  2532. if (p_variant._get_obj().obj && p_variant._get_obj().id.is_ref_counted()) {
  2533. RefCounted *ref_counted = static_cast<RefCounted *>(p_variant._get_obj().obj);
  2534. if (!ref_counted->reference()) {
  2535. _get_obj().obj = nullptr;
  2536. _get_obj().id = ObjectID();
  2537. break;
  2538. }
  2539. }
  2540. _get_obj().obj = const_cast<Object *>(p_variant._get_obj().obj);
  2541. _get_obj().id = p_variant._get_obj().id;
  2542. } break;
  2543. case CALLABLE: {
  2544. *reinterpret_cast<Callable *>(_data._mem) = *reinterpret_cast<const Callable *>(p_variant._data._mem);
  2545. } break;
  2546. case SIGNAL: {
  2547. *reinterpret_cast<Signal *>(_data._mem) = *reinterpret_cast<const Signal *>(p_variant._data._mem);
  2548. } break;
  2549. case STRING_NAME: {
  2550. *reinterpret_cast<StringName *>(_data._mem) = *reinterpret_cast<const StringName *>(p_variant._data._mem);
  2551. } break;
  2552. case NODE_PATH: {
  2553. *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
  2554. } break;
  2555. case DICTIONARY: {
  2556. *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
  2557. } break;
  2558. case ARRAY: {
  2559. *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
  2560. } break;
  2561. // arrays
  2562. case PACKED_BYTE_ARRAY: {
  2563. _data.packed_array = PackedArrayRef<uint8_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2564. } break;
  2565. case PACKED_INT32_ARRAY: {
  2566. _data.packed_array = PackedArrayRef<int32_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2567. } break;
  2568. case PACKED_INT64_ARRAY: {
  2569. _data.packed_array = PackedArrayRef<int64_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2570. } break;
  2571. case PACKED_FLOAT32_ARRAY: {
  2572. _data.packed_array = PackedArrayRef<float>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2573. } break;
  2574. case PACKED_FLOAT64_ARRAY: {
  2575. _data.packed_array = PackedArrayRef<double>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2576. } break;
  2577. case PACKED_STRING_ARRAY: {
  2578. _data.packed_array = PackedArrayRef<String>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2579. } break;
  2580. case PACKED_VECTOR2_ARRAY: {
  2581. _data.packed_array = PackedArrayRef<Vector2>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2582. } break;
  2583. case PACKED_VECTOR3_ARRAY: {
  2584. _data.packed_array = PackedArrayRef<Vector3>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2585. } break;
  2586. case PACKED_COLOR_ARRAY: {
  2587. _data.packed_array = PackedArrayRef<Color>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2588. } break;
  2589. default: {
  2590. }
  2591. }
  2592. }
  2593. Variant::Variant(const IPAddress &p_address) {
  2594. type = STRING;
  2595. memnew_placement(_data._mem, String(p_address));
  2596. }
  2597. Variant::Variant(const Variant &p_variant) {
  2598. reference(p_variant);
  2599. }
  2600. uint32_t Variant::hash() const {
  2601. return recursive_hash(0);
  2602. }
  2603. uint32_t Variant::recursive_hash(int recursion_count) const {
  2604. switch (type) {
  2605. case NIL: {
  2606. return 0;
  2607. } break;
  2608. case BOOL: {
  2609. return _data._bool ? 1 : 0;
  2610. } break;
  2611. case INT: {
  2612. return hash_one_uint64((uint64_t)_data._int);
  2613. } break;
  2614. case FLOAT: {
  2615. return hash_murmur3_one_double(_data._float);
  2616. } break;
  2617. case STRING: {
  2618. return reinterpret_cast<const String *>(_data._mem)->hash();
  2619. } break;
  2620. // math types
  2621. case VECTOR2: {
  2622. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2 *>(_data._mem));
  2623. } break;
  2624. case VECTOR2I: {
  2625. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2i *>(_data._mem));
  2626. } break;
  2627. case RECT2: {
  2628. return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2 *>(_data._mem));
  2629. } break;
  2630. case RECT2I: {
  2631. return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2i *>(_data._mem));
  2632. } break;
  2633. case TRANSFORM2D: {
  2634. uint32_t h = HASH_MURMUR3_SEED;
  2635. const Transform2D &t = *_data._transform2d;
  2636. h = hash_murmur3_one_real(t[0].x, h);
  2637. h = hash_murmur3_one_real(t[0].y, h);
  2638. h = hash_murmur3_one_real(t[1].x, h);
  2639. h = hash_murmur3_one_real(t[1].y, h);
  2640. h = hash_murmur3_one_real(t[2].x, h);
  2641. h = hash_murmur3_one_real(t[2].y, h);
  2642. return hash_fmix32(h);
  2643. } break;
  2644. case VECTOR3: {
  2645. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3 *>(_data._mem));
  2646. } break;
  2647. case VECTOR3I: {
  2648. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3i *>(_data._mem));
  2649. } break;
  2650. case VECTOR4: {
  2651. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4 *>(_data._mem));
  2652. } break;
  2653. case VECTOR4I: {
  2654. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4i *>(_data._mem));
  2655. } break;
  2656. case PLANE: {
  2657. uint32_t h = HASH_MURMUR3_SEED;
  2658. const Plane &p = *reinterpret_cast<const Plane *>(_data._mem);
  2659. h = hash_murmur3_one_real(p.normal.x, h);
  2660. h = hash_murmur3_one_real(p.normal.y, h);
  2661. h = hash_murmur3_one_real(p.normal.z, h);
  2662. h = hash_murmur3_one_real(p.d, h);
  2663. return hash_fmix32(h);
  2664. } break;
  2665. case AABB: {
  2666. return HashMapHasherDefault::hash(*_data._aabb);
  2667. } break;
  2668. case QUATERNION: {
  2669. uint32_t h = HASH_MURMUR3_SEED;
  2670. const Quaternion &q = *reinterpret_cast<const Quaternion *>(_data._mem);
  2671. h = hash_murmur3_one_real(q.x, h);
  2672. h = hash_murmur3_one_real(q.y, h);
  2673. h = hash_murmur3_one_real(q.z, h);
  2674. h = hash_murmur3_one_real(q.w, h);
  2675. return hash_fmix32(h);
  2676. } break;
  2677. case BASIS: {
  2678. uint32_t h = HASH_MURMUR3_SEED;
  2679. const Basis &b = *_data._basis;
  2680. h = hash_murmur3_one_real(b[0].x, h);
  2681. h = hash_murmur3_one_real(b[0].y, h);
  2682. h = hash_murmur3_one_real(b[0].z, h);
  2683. h = hash_murmur3_one_real(b[1].x, h);
  2684. h = hash_murmur3_one_real(b[1].y, h);
  2685. h = hash_murmur3_one_real(b[1].z, h);
  2686. h = hash_murmur3_one_real(b[2].x, h);
  2687. h = hash_murmur3_one_real(b[2].y, h);
  2688. h = hash_murmur3_one_real(b[2].z, h);
  2689. return hash_fmix32(h);
  2690. } break;
  2691. case TRANSFORM3D: {
  2692. uint32_t h = HASH_MURMUR3_SEED;
  2693. const Transform3D &t = *_data._transform3d;
  2694. h = hash_murmur3_one_real(t.basis[0].x, h);
  2695. h = hash_murmur3_one_real(t.basis[0].y, h);
  2696. h = hash_murmur3_one_real(t.basis[0].z, h);
  2697. h = hash_murmur3_one_real(t.basis[1].x, h);
  2698. h = hash_murmur3_one_real(t.basis[1].y, h);
  2699. h = hash_murmur3_one_real(t.basis[1].z, h);
  2700. h = hash_murmur3_one_real(t.basis[2].x, h);
  2701. h = hash_murmur3_one_real(t.basis[2].y, h);
  2702. h = hash_murmur3_one_real(t.basis[2].z, h);
  2703. h = hash_murmur3_one_real(t.origin.x, h);
  2704. h = hash_murmur3_one_real(t.origin.y, h);
  2705. h = hash_murmur3_one_real(t.origin.z, h);
  2706. return hash_fmix32(h);
  2707. } break;
  2708. case PROJECTION: {
  2709. uint32_t h = HASH_MURMUR3_SEED;
  2710. const Projection &t = *_data._projection;
  2711. h = hash_murmur3_one_real(t.columns[0].x, h);
  2712. h = hash_murmur3_one_real(t.columns[0].y, h);
  2713. h = hash_murmur3_one_real(t.columns[0].z, h);
  2714. h = hash_murmur3_one_real(t.columns[0].w, h);
  2715. h = hash_murmur3_one_real(t.columns[1].x, h);
  2716. h = hash_murmur3_one_real(t.columns[1].y, h);
  2717. h = hash_murmur3_one_real(t.columns[1].z, h);
  2718. h = hash_murmur3_one_real(t.columns[1].w, h);
  2719. h = hash_murmur3_one_real(t.columns[2].x, h);
  2720. h = hash_murmur3_one_real(t.columns[2].y, h);
  2721. h = hash_murmur3_one_real(t.columns[2].z, h);
  2722. h = hash_murmur3_one_real(t.columns[2].w, h);
  2723. h = hash_murmur3_one_real(t.columns[3].x, h);
  2724. h = hash_murmur3_one_real(t.columns[3].y, h);
  2725. h = hash_murmur3_one_real(t.columns[3].z, h);
  2726. h = hash_murmur3_one_real(t.columns[3].w, h);
  2727. return hash_fmix32(h);
  2728. } break;
  2729. // misc types
  2730. case COLOR: {
  2731. uint32_t h = HASH_MURMUR3_SEED;
  2732. const Color &c = *reinterpret_cast<const Color *>(_data._mem);
  2733. h = hash_murmur3_one_float(c.r, h);
  2734. h = hash_murmur3_one_float(c.g, h);
  2735. h = hash_murmur3_one_float(c.b, h);
  2736. h = hash_murmur3_one_float(c.a, h);
  2737. return hash_fmix32(h);
  2738. } break;
  2739. case RID: {
  2740. return hash_one_uint64(reinterpret_cast<const ::RID *>(_data._mem)->get_id());
  2741. } break;
  2742. case OBJECT: {
  2743. return hash_one_uint64(hash_make_uint64_t(_get_obj().obj));
  2744. } break;
  2745. case STRING_NAME: {
  2746. return reinterpret_cast<const StringName *>(_data._mem)->hash();
  2747. } break;
  2748. case NODE_PATH: {
  2749. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2750. } break;
  2751. case DICTIONARY: {
  2752. return reinterpret_cast<const Dictionary *>(_data._mem)->recursive_hash(recursion_count);
  2753. } break;
  2754. case CALLABLE: {
  2755. return reinterpret_cast<const Callable *>(_data._mem)->hash();
  2756. } break;
  2757. case SIGNAL: {
  2758. const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
  2759. uint32_t hash = s.get_name().hash();
  2760. return hash_murmur3_one_64(s.get_object_id(), hash);
  2761. } break;
  2762. case ARRAY: {
  2763. const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
  2764. return arr.recursive_hash(recursion_count);
  2765. } break;
  2766. case PACKED_BYTE_ARRAY: {
  2767. const Vector<uint8_t> &arr = PackedArrayRef<uint8_t>::get_array(_data.packed_array);
  2768. int len = arr.size();
  2769. if (likely(len)) {
  2770. const uint8_t *r = arr.ptr();
  2771. return hash_murmur3_buffer((uint8_t *)&r[0], len);
  2772. } else {
  2773. return hash_murmur3_one_64(0);
  2774. }
  2775. } break;
  2776. case PACKED_INT32_ARRAY: {
  2777. const Vector<int32_t> &arr = PackedArrayRef<int32_t>::get_array(_data.packed_array);
  2778. int len = arr.size();
  2779. if (likely(len)) {
  2780. const int32_t *r = arr.ptr();
  2781. return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int32_t));
  2782. } else {
  2783. return hash_murmur3_one_64(0);
  2784. }
  2785. } break;
  2786. case PACKED_INT64_ARRAY: {
  2787. const Vector<int64_t> &arr = PackedArrayRef<int64_t>::get_array(_data.packed_array);
  2788. int len = arr.size();
  2789. if (likely(len)) {
  2790. const int64_t *r = arr.ptr();
  2791. return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int64_t));
  2792. } else {
  2793. return hash_murmur3_one_64(0);
  2794. }
  2795. } break;
  2796. case PACKED_FLOAT32_ARRAY: {
  2797. const Vector<float> &arr = PackedArrayRef<float>::get_array(_data.packed_array);
  2798. int len = arr.size();
  2799. if (likely(len)) {
  2800. const float *r = arr.ptr();
  2801. uint32_t h = HASH_MURMUR3_SEED;
  2802. for (int32_t i = 0; i < len; i++) {
  2803. h = hash_murmur3_one_float(r[i], h);
  2804. }
  2805. return hash_fmix32(h);
  2806. } else {
  2807. return hash_murmur3_one_float(0.0);
  2808. }
  2809. } break;
  2810. case PACKED_FLOAT64_ARRAY: {
  2811. const Vector<double> &arr = PackedArrayRef<double>::get_array(_data.packed_array);
  2812. int len = arr.size();
  2813. if (likely(len)) {
  2814. const double *r = arr.ptr();
  2815. uint32_t h = HASH_MURMUR3_SEED;
  2816. for (int32_t i = 0; i < len; i++) {
  2817. h = hash_murmur3_one_double(r[i], h);
  2818. }
  2819. return hash_fmix32(h);
  2820. } else {
  2821. return hash_murmur3_one_double(0.0);
  2822. }
  2823. } break;
  2824. case PACKED_STRING_ARRAY: {
  2825. uint32_t hash = HASH_MURMUR3_SEED;
  2826. const Vector<String> &arr = PackedArrayRef<String>::get_array(_data.packed_array);
  2827. int len = arr.size();
  2828. if (likely(len)) {
  2829. const String *r = arr.ptr();
  2830. for (int i = 0; i < len; i++) {
  2831. hash = hash_murmur3_one_32(r[i].hash(), hash);
  2832. }
  2833. hash = hash_fmix32(hash);
  2834. }
  2835. return hash;
  2836. } break;
  2837. case PACKED_VECTOR2_ARRAY: {
  2838. uint32_t hash = HASH_MURMUR3_SEED;
  2839. const Vector<Vector2> &arr = PackedArrayRef<Vector2>::get_array(_data.packed_array);
  2840. int len = arr.size();
  2841. if (likely(len)) {
  2842. const Vector2 *r = arr.ptr();
  2843. for (int i = 0; i < len; i++) {
  2844. hash = hash_murmur3_one_real(r[i].x, hash);
  2845. hash = hash_murmur3_one_real(r[i].y, hash);
  2846. }
  2847. hash = hash_fmix32(hash);
  2848. }
  2849. return hash;
  2850. } break;
  2851. case PACKED_VECTOR3_ARRAY: {
  2852. uint32_t hash = HASH_MURMUR3_SEED;
  2853. const Vector<Vector3> &arr = PackedArrayRef<Vector3>::get_array(_data.packed_array);
  2854. int len = arr.size();
  2855. if (likely(len)) {
  2856. const Vector3 *r = arr.ptr();
  2857. for (int i = 0; i < len; i++) {
  2858. hash = hash_murmur3_one_real(r[i].x, hash);
  2859. hash = hash_murmur3_one_real(r[i].y, hash);
  2860. hash = hash_murmur3_one_real(r[i].z, hash);
  2861. }
  2862. hash = hash_fmix32(hash);
  2863. }
  2864. return hash;
  2865. } break;
  2866. case PACKED_COLOR_ARRAY: {
  2867. uint32_t hash = HASH_MURMUR3_SEED;
  2868. const Vector<Color> &arr = PackedArrayRef<Color>::get_array(_data.packed_array);
  2869. int len = arr.size();
  2870. if (likely(len)) {
  2871. const Color *r = arr.ptr();
  2872. for (int i = 0; i < len; i++) {
  2873. hash = hash_murmur3_one_float(r[i].r, hash);
  2874. hash = hash_murmur3_one_float(r[i].g, hash);
  2875. hash = hash_murmur3_one_float(r[i].b, hash);
  2876. hash = hash_murmur3_one_float(r[i].a, hash);
  2877. }
  2878. hash = hash_fmix32(hash);
  2879. }
  2880. return hash;
  2881. } break;
  2882. default: {
  2883. }
  2884. }
  2885. return 0;
  2886. }
  2887. #define hash_compare_scalar(p_lhs, p_rhs) \
  2888. (((p_lhs) == (p_rhs)) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs)))
  2889. #define hash_compare_vector2(p_lhs, p_rhs) \
  2890. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2891. hash_compare_scalar((p_lhs).y, (p_rhs).y))
  2892. #define hash_compare_vector3(p_lhs, p_rhs) \
  2893. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2894. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2895. hash_compare_scalar((p_lhs).z, (p_rhs).z))
  2896. #define hash_compare_vector4(p_lhs, p_rhs) \
  2897. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2898. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2899. hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
  2900. hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2901. #define hash_compare_quaternion(p_lhs, p_rhs) \
  2902. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2903. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2904. hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
  2905. hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2906. #define hash_compare_color(p_lhs, p_rhs) \
  2907. (hash_compare_scalar((p_lhs).r, (p_rhs).r) && \
  2908. hash_compare_scalar((p_lhs).g, (p_rhs).g) && \
  2909. hash_compare_scalar((p_lhs).b, (p_rhs).b) && \
  2910. hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2911. #define hash_compare_packed_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2912. const Vector<p_type> &l = PackedArrayRef<p_type>::get_array(p_lhs); \
  2913. const Vector<p_type> &r = PackedArrayRef<p_type>::get_array(p_rhs); \
  2914. \
  2915. if (l.size() != r.size()) \
  2916. return false; \
  2917. \
  2918. const p_type *lr = l.ptr(); \
  2919. const p_type *rr = r.ptr(); \
  2920. \
  2921. for (int i = 0; i < l.size(); ++i) { \
  2922. if (!p_compare_func((lr[i]), (rr[i]))) \
  2923. return false; \
  2924. } \
  2925. \
  2926. return true
  2927. bool Variant::hash_compare(const Variant &p_variant, int recursion_count) const {
  2928. if (type != p_variant.type) {
  2929. return false;
  2930. }
  2931. switch (type) {
  2932. case INT: {
  2933. return _data._int == p_variant._data._int;
  2934. } break;
  2935. case FLOAT: {
  2936. return hash_compare_scalar(_data._float, p_variant._data._float);
  2937. } break;
  2938. case STRING: {
  2939. return *reinterpret_cast<const String *>(_data._mem) == *reinterpret_cast<const String *>(p_variant._data._mem);
  2940. } break;
  2941. case STRING_NAME: {
  2942. return *reinterpret_cast<const StringName *>(_data._mem) == *reinterpret_cast<const StringName *>(p_variant._data._mem);
  2943. } break;
  2944. case VECTOR2: {
  2945. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2946. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2947. return hash_compare_vector2(*l, *r);
  2948. } break;
  2949. case VECTOR2I: {
  2950. const Vector2i *l = reinterpret_cast<const Vector2i *>(_data._mem);
  2951. const Vector2i *r = reinterpret_cast<const Vector2i *>(p_variant._data._mem);
  2952. return *l == *r;
  2953. } break;
  2954. case RECT2: {
  2955. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2956. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2957. return hash_compare_vector2(l->position, r->position) &&
  2958. hash_compare_vector2(l->size, r->size);
  2959. } break;
  2960. case RECT2I: {
  2961. const Rect2i *l = reinterpret_cast<const Rect2i *>(_data._mem);
  2962. const Rect2i *r = reinterpret_cast<const Rect2i *>(p_variant._data._mem);
  2963. return *l == *r;
  2964. } break;
  2965. case TRANSFORM2D: {
  2966. Transform2D *l = _data._transform2d;
  2967. Transform2D *r = p_variant._data._transform2d;
  2968. for (int i = 0; i < 3; i++) {
  2969. if (!hash_compare_vector2(l->columns[i], r->columns[i])) {
  2970. return false;
  2971. }
  2972. }
  2973. return true;
  2974. } break;
  2975. case VECTOR3: {
  2976. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2977. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2978. return hash_compare_vector3(*l, *r);
  2979. } break;
  2980. case VECTOR3I: {
  2981. const Vector3i *l = reinterpret_cast<const Vector3i *>(_data._mem);
  2982. const Vector3i *r = reinterpret_cast<const Vector3i *>(p_variant._data._mem);
  2983. return *l == *r;
  2984. } break;
  2985. case VECTOR4: {
  2986. const Vector4 *l = reinterpret_cast<const Vector4 *>(_data._mem);
  2987. const Vector4 *r = reinterpret_cast<const Vector4 *>(p_variant._data._mem);
  2988. return hash_compare_vector4(*l, *r);
  2989. } break;
  2990. case VECTOR4I: {
  2991. const Vector4i *l = reinterpret_cast<const Vector4i *>(_data._mem);
  2992. const Vector4i *r = reinterpret_cast<const Vector4i *>(p_variant._data._mem);
  2993. return *l == *r;
  2994. } break;
  2995. case PLANE: {
  2996. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  2997. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  2998. return hash_compare_vector3(l->normal, r->normal) &&
  2999. hash_compare_scalar(l->d, r->d);
  3000. } break;
  3001. case AABB: {
  3002. const ::AABB *l = _data._aabb;
  3003. const ::AABB *r = p_variant._data._aabb;
  3004. return hash_compare_vector3(l->position, r->position) &&
  3005. hash_compare_vector3(l->size, r->size);
  3006. } break;
  3007. case QUATERNION: {
  3008. const Quaternion *l = reinterpret_cast<const Quaternion *>(_data._mem);
  3009. const Quaternion *r = reinterpret_cast<const Quaternion *>(p_variant._data._mem);
  3010. return hash_compare_quaternion(*l, *r);
  3011. } break;
  3012. case BASIS: {
  3013. const Basis *l = _data._basis;
  3014. const Basis *r = p_variant._data._basis;
  3015. for (int i = 0; i < 3; i++) {
  3016. if (!hash_compare_vector3(l->rows[i], r->rows[i])) {
  3017. return false;
  3018. }
  3019. }
  3020. return true;
  3021. } break;
  3022. case TRANSFORM3D: {
  3023. const Transform3D *l = _data._transform3d;
  3024. const Transform3D *r = p_variant._data._transform3d;
  3025. for (int i = 0; i < 3; i++) {
  3026. if (!hash_compare_vector3(l->basis.rows[i], r->basis.rows[i])) {
  3027. return false;
  3028. }
  3029. }
  3030. return hash_compare_vector3(l->origin, r->origin);
  3031. } break;
  3032. case PROJECTION: {
  3033. const Projection *l = _data._projection;
  3034. const Projection *r = p_variant._data._projection;
  3035. for (int i = 0; i < 4; i++) {
  3036. if (!hash_compare_vector4(l->columns[i], r->columns[i])) {
  3037. return false;
  3038. }
  3039. }
  3040. return true;
  3041. } break;
  3042. case COLOR: {
  3043. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  3044. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  3045. return hash_compare_color(*l, *r);
  3046. } break;
  3047. case ARRAY: {
  3048. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  3049. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  3050. if (!l.recursive_equal(r, recursion_count + 1)) {
  3051. return false;
  3052. }
  3053. return true;
  3054. } break;
  3055. case DICTIONARY: {
  3056. const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
  3057. const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
  3058. if (!l.recursive_equal(r, recursion_count + 1)) {
  3059. return false;
  3060. }
  3061. return true;
  3062. } break;
  3063. // This is for floating point comparisons only.
  3064. case PACKED_FLOAT32_ARRAY: {
  3065. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, float, hash_compare_scalar);
  3066. } break;
  3067. case PACKED_FLOAT64_ARRAY: {
  3068. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, double, hash_compare_scalar);
  3069. } break;
  3070. case PACKED_VECTOR2_ARRAY: {
  3071. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector2, hash_compare_vector2);
  3072. } break;
  3073. case PACKED_VECTOR3_ARRAY: {
  3074. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector3, hash_compare_vector3);
  3075. } break;
  3076. case PACKED_COLOR_ARRAY: {
  3077. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Color, hash_compare_color);
  3078. } break;
  3079. default:
  3080. bool v;
  3081. Variant r;
  3082. evaluate(OP_EQUAL, *this, p_variant, r, v);
  3083. return r;
  3084. }
  3085. }
  3086. bool Variant::identity_compare(const Variant &p_variant) const {
  3087. if (type != p_variant.type) {
  3088. return false;
  3089. }
  3090. switch (type) {
  3091. case OBJECT: {
  3092. return _get_obj().id == p_variant._get_obj().id;
  3093. } break;
  3094. case DICTIONARY: {
  3095. const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
  3096. const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
  3097. return l.id() == r.id();
  3098. } break;
  3099. case ARRAY: {
  3100. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  3101. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  3102. return l.id() == r.id();
  3103. } break;
  3104. case PACKED_BYTE_ARRAY:
  3105. case PACKED_INT32_ARRAY:
  3106. case PACKED_INT64_ARRAY:
  3107. case PACKED_FLOAT32_ARRAY:
  3108. case PACKED_FLOAT64_ARRAY:
  3109. case PACKED_STRING_ARRAY:
  3110. case PACKED_VECTOR2_ARRAY:
  3111. case PACKED_VECTOR3_ARRAY:
  3112. case PACKED_COLOR_ARRAY: {
  3113. return _data.packed_array == p_variant._data.packed_array;
  3114. } break;
  3115. default: {
  3116. return hash_compare(p_variant);
  3117. }
  3118. }
  3119. }
  3120. bool StringLikeVariantComparator::compare(const Variant &p_lhs, const Variant &p_rhs) {
  3121. if (p_lhs.hash_compare(p_rhs)) {
  3122. return true;
  3123. }
  3124. if (p_lhs.get_type() == Variant::STRING && p_rhs.get_type() == Variant::STRING_NAME) {
  3125. return *VariantInternal::get_string(&p_lhs) == *VariantInternal::get_string_name(&p_rhs);
  3126. }
  3127. if (p_lhs.get_type() == Variant::STRING_NAME && p_rhs.get_type() == Variant::STRING) {
  3128. return *VariantInternal::get_string_name(&p_lhs) == *VariantInternal::get_string(&p_rhs);
  3129. }
  3130. return false;
  3131. }
  3132. bool Variant::is_ref_counted() const {
  3133. return type == OBJECT && _get_obj().id.is_ref_counted();
  3134. }
  3135. Vector<Variant> varray() {
  3136. return Vector<Variant>();
  3137. }
  3138. Vector<Variant> varray(const Variant &p_arg1) {
  3139. Vector<Variant> v;
  3140. v.push_back(p_arg1);
  3141. return v;
  3142. }
  3143. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2) {
  3144. Vector<Variant> v;
  3145. v.push_back(p_arg1);
  3146. v.push_back(p_arg2);
  3147. return v;
  3148. }
  3149. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3) {
  3150. Vector<Variant> v;
  3151. v.push_back(p_arg1);
  3152. v.push_back(p_arg2);
  3153. v.push_back(p_arg3);
  3154. return v;
  3155. }
  3156. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4) {
  3157. Vector<Variant> v;
  3158. v.push_back(p_arg1);
  3159. v.push_back(p_arg2);
  3160. v.push_back(p_arg3);
  3161. v.push_back(p_arg4);
  3162. return v;
  3163. }
  3164. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5) {
  3165. Vector<Variant> v;
  3166. v.push_back(p_arg1);
  3167. v.push_back(p_arg2);
  3168. v.push_back(p_arg3);
  3169. v.push_back(p_arg4);
  3170. v.push_back(p_arg5);
  3171. return v;
  3172. }
  3173. void Variant::static_assign(const Variant &p_variant) {
  3174. }
  3175. bool Variant::is_type_shared(Variant::Type p_type) {
  3176. switch (p_type) {
  3177. case OBJECT:
  3178. case ARRAY:
  3179. case DICTIONARY:
  3180. return true;
  3181. default: {
  3182. }
  3183. }
  3184. return false;
  3185. }
  3186. bool Variant::is_shared() const {
  3187. return is_type_shared(type);
  3188. }
  3189. void Variant::_variant_call_error(const String &p_method, Callable::CallError &error) {
  3190. switch (error.error) {
  3191. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  3192. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(Variant::Type(error.expected)) + "'.";
  3193. ERR_PRINT(err.utf8().get_data());
  3194. } break;
  3195. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  3196. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  3197. ERR_PRINT(err.utf8().get_data());
  3198. } break;
  3199. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  3200. String err = "Too many arguments for method '" + p_method + "'";
  3201. ERR_PRINT(err.utf8().get_data());
  3202. } break;
  3203. default: {
  3204. }
  3205. }
  3206. }
  3207. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  3208. r_value = Variant();
  3209. }
  3210. String Variant::get_construct_string() const {
  3211. String vars;
  3212. VariantWriter::write_to_string(*this, vars);
  3213. return vars;
  3214. }
  3215. String Variant::get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3216. return get_call_error_text(nullptr, p_method, p_argptrs, p_argcount, ce);
  3217. }
  3218. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3219. String err_text;
  3220. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  3221. int errorarg = ce.argument;
  3222. if (p_argptrs) {
  3223. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(p_argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(Variant::Type(ce.expected));
  3224. } else {
  3225. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(Variant::Type(ce.expected));
  3226. }
  3227. } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  3228. err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
  3229. } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  3230. err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
  3231. } else if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  3232. err_text = "Method not found";
  3233. } else if (ce.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  3234. err_text = "Instance is null";
  3235. } else if (ce.error == Callable::CallError::CALL_ERROR_METHOD_NOT_CONST) {
  3236. err_text = "Method not const in const instance";
  3237. } else if (ce.error == Callable::CallError::CALL_OK) {
  3238. return "Call OK";
  3239. }
  3240. String base_text;
  3241. if (p_base) {
  3242. base_text = p_base->get_class();
  3243. Ref<Resource> script = p_base->get_script();
  3244. if (script.is_valid() && script->get_path().is_resource_file()) {
  3245. base_text += "(" + script->get_path().get_file() + ")";
  3246. }
  3247. base_text += "::";
  3248. }
  3249. return "'" + base_text + String(p_method) + "': " + err_text;
  3250. }
  3251. String Variant::get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3252. Vector<Variant> binds;
  3253. int args_bound;
  3254. p_callable.get_bound_arguments_ref(binds, args_bound);
  3255. if (args_bound <= 0) {
  3256. return get_call_error_text(p_callable.get_object(), p_callable.get_method(), p_argptrs, MAX(0, p_argcount + args_bound), ce);
  3257. } else {
  3258. Vector<const Variant *> argptrs;
  3259. argptrs.resize(p_argcount + binds.size());
  3260. for (int i = 0; i < p_argcount; i++) {
  3261. argptrs.write[i] = p_argptrs[i];
  3262. }
  3263. for (int i = 0; i < binds.size(); i++) {
  3264. argptrs.write[i + p_argcount] = &binds[i];
  3265. }
  3266. return get_call_error_text(p_callable.get_object(), p_callable.get_method(), (const Variant **)argptrs.ptr(), argptrs.size(), ce);
  3267. }
  3268. }
  3269. void Variant::register_types() {
  3270. _register_variant_operators();
  3271. _register_variant_methods();
  3272. _register_variant_setters_getters();
  3273. _register_variant_constructors();
  3274. _register_variant_destructors();
  3275. _register_variant_utility_functions();
  3276. }
  3277. void Variant::unregister_types() {
  3278. _unregister_variant_operators();
  3279. _unregister_variant_methods();
  3280. _unregister_variant_setters_getters();
  3281. _unregister_variant_destructors();
  3282. _unregister_variant_utility_functions();
  3283. }