variant.cpp 95 KB

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