variant.cpp 94 KB

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