variant.cpp 92 KB

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