variant.cpp 78 KB

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