variant.cpp 94 KB

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