variant.cpp 69 KB

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