variant.cpp 70 KB

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