2
0

variant.cpp 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702
  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_obj().obj == 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. type(NIL) {
  2417. PackedVector3Array vertices;
  2418. int face_count = p_face_array.size();
  2419. vertices.resize(face_count * 3);
  2420. if (face_count) {
  2421. const Face3 *r = p_face_array.ptr();
  2422. Vector3 *w = vertices.ptrw();
  2423. for (int i = 0; i < face_count; i++) {
  2424. for (int j = 0; j < 3; j++) {
  2425. w[i * 3 + j] = r[i].vertex[j];
  2426. }
  2427. }
  2428. }
  2429. *this = vertices;
  2430. }
  2431. Variant::Variant(const Vector<Variant> &p_array) :
  2432. type(NIL) {
  2433. Array arr;
  2434. arr.resize(p_array.size());
  2435. for (int i = 0; i < p_array.size(); i++) {
  2436. arr[i] = p_array[i];
  2437. }
  2438. *this = arr;
  2439. }
  2440. Variant::Variant(const Vector<StringName> &p_array) :
  2441. type(NIL) {
  2442. PackedStringArray v;
  2443. int len = p_array.size();
  2444. v.resize(len);
  2445. for (int i = 0; i < len; i++) {
  2446. v.set(i, p_array[i]);
  2447. }
  2448. *this = v;
  2449. }
  2450. void Variant::operator=(const Variant &p_variant) {
  2451. if (unlikely(this == &p_variant)) {
  2452. return;
  2453. }
  2454. if (unlikely(type != p_variant.type)) {
  2455. reference(p_variant);
  2456. return;
  2457. }
  2458. switch (p_variant.type) {
  2459. case NIL: {
  2460. // none
  2461. } break;
  2462. // atomic types
  2463. case BOOL: {
  2464. _data._bool = p_variant._data._bool;
  2465. } break;
  2466. case INT: {
  2467. _data._int = p_variant._data._int;
  2468. } break;
  2469. case FLOAT: {
  2470. _data._float = p_variant._data._float;
  2471. } break;
  2472. case STRING: {
  2473. *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
  2474. } break;
  2475. // math types
  2476. case VECTOR2: {
  2477. *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2478. } break;
  2479. case VECTOR2I: {
  2480. *reinterpret_cast<Vector2i *>(_data._mem) = *reinterpret_cast<const Vector2i *>(p_variant._data._mem);
  2481. } break;
  2482. case RECT2: {
  2483. *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2484. } break;
  2485. case RECT2I: {
  2486. *reinterpret_cast<Rect2i *>(_data._mem) = *reinterpret_cast<const Rect2i *>(p_variant._data._mem);
  2487. } break;
  2488. case TRANSFORM2D: {
  2489. *_data._transform2d = *(p_variant._data._transform2d);
  2490. } break;
  2491. case VECTOR3: {
  2492. *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2493. } break;
  2494. case VECTOR3I: {
  2495. *reinterpret_cast<Vector3i *>(_data._mem) = *reinterpret_cast<const Vector3i *>(p_variant._data._mem);
  2496. } break;
  2497. case VECTOR4: {
  2498. *reinterpret_cast<Vector4 *>(_data._mem) = *reinterpret_cast<const Vector4 *>(p_variant._data._mem);
  2499. } break;
  2500. case VECTOR4I: {
  2501. *reinterpret_cast<Vector4i *>(_data._mem) = *reinterpret_cast<const Vector4i *>(p_variant._data._mem);
  2502. } break;
  2503. case PLANE: {
  2504. *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
  2505. } break;
  2506. case AABB: {
  2507. *_data._aabb = *(p_variant._data._aabb);
  2508. } break;
  2509. case QUATERNION: {
  2510. *reinterpret_cast<Quaternion *>(_data._mem) = *reinterpret_cast<const Quaternion *>(p_variant._data._mem);
  2511. } break;
  2512. case BASIS: {
  2513. *_data._basis = *(p_variant._data._basis);
  2514. } break;
  2515. case TRANSFORM3D: {
  2516. *_data._transform3d = *(p_variant._data._transform3d);
  2517. } break;
  2518. case PROJECTION: {
  2519. *_data._projection = *(p_variant._data._projection);
  2520. } break;
  2521. // misc types
  2522. case COLOR: {
  2523. *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
  2524. } break;
  2525. case RID: {
  2526. *reinterpret_cast<::RID *>(_data._mem) = *reinterpret_cast<const ::RID *>(p_variant._data._mem);
  2527. } break;
  2528. case OBJECT: {
  2529. _get_obj().ref(p_variant._get_obj());
  2530. } break;
  2531. case CALLABLE: {
  2532. *reinterpret_cast<Callable *>(_data._mem) = *reinterpret_cast<const Callable *>(p_variant._data._mem);
  2533. } break;
  2534. case SIGNAL: {
  2535. *reinterpret_cast<Signal *>(_data._mem) = *reinterpret_cast<const Signal *>(p_variant._data._mem);
  2536. } break;
  2537. case STRING_NAME: {
  2538. *reinterpret_cast<StringName *>(_data._mem) = *reinterpret_cast<const StringName *>(p_variant._data._mem);
  2539. } break;
  2540. case NODE_PATH: {
  2541. *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
  2542. } break;
  2543. case DICTIONARY: {
  2544. *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
  2545. } break;
  2546. case ARRAY: {
  2547. *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
  2548. } break;
  2549. // arrays
  2550. case PACKED_BYTE_ARRAY: {
  2551. _data.packed_array = PackedArrayRef<uint8_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2552. } break;
  2553. case PACKED_INT32_ARRAY: {
  2554. _data.packed_array = PackedArrayRef<int32_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2555. } break;
  2556. case PACKED_INT64_ARRAY: {
  2557. _data.packed_array = PackedArrayRef<int64_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2558. } break;
  2559. case PACKED_FLOAT32_ARRAY: {
  2560. _data.packed_array = PackedArrayRef<float>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2561. } break;
  2562. case PACKED_FLOAT64_ARRAY: {
  2563. _data.packed_array = PackedArrayRef<double>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2564. } break;
  2565. case PACKED_STRING_ARRAY: {
  2566. _data.packed_array = PackedArrayRef<String>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2567. } break;
  2568. case PACKED_VECTOR2_ARRAY: {
  2569. _data.packed_array = PackedArrayRef<Vector2>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2570. } break;
  2571. case PACKED_VECTOR3_ARRAY: {
  2572. _data.packed_array = PackedArrayRef<Vector3>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2573. } break;
  2574. case PACKED_COLOR_ARRAY: {
  2575. _data.packed_array = PackedArrayRef<Color>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2576. } break;
  2577. case PACKED_VECTOR4_ARRAY: {
  2578. _data.packed_array = PackedArrayRef<Vector4>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2579. } break;
  2580. default: {
  2581. }
  2582. }
  2583. }
  2584. Variant::Variant(const IPAddress &p_address) :
  2585. type(STRING) {
  2586. memnew_placement(_data._mem, String(p_address));
  2587. }
  2588. Variant::Variant(const Variant &p_variant) :
  2589. type(NIL) {
  2590. reference(p_variant);
  2591. }
  2592. uint32_t Variant::hash() const {
  2593. return recursive_hash(0);
  2594. }
  2595. uint32_t Variant::recursive_hash(int recursion_count) const {
  2596. switch (type) {
  2597. case NIL: {
  2598. return 0;
  2599. } break;
  2600. case BOOL: {
  2601. return _data._bool ? 1 : 0;
  2602. } break;
  2603. case INT: {
  2604. return hash_one_uint64((uint64_t)_data._int);
  2605. } break;
  2606. case FLOAT: {
  2607. return hash_murmur3_one_double(_data._float);
  2608. } break;
  2609. case STRING: {
  2610. return reinterpret_cast<const String *>(_data._mem)->hash();
  2611. } break;
  2612. // math types
  2613. case VECTOR2: {
  2614. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2 *>(_data._mem));
  2615. } break;
  2616. case VECTOR2I: {
  2617. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2i *>(_data._mem));
  2618. } break;
  2619. case RECT2: {
  2620. return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2 *>(_data._mem));
  2621. } break;
  2622. case RECT2I: {
  2623. return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2i *>(_data._mem));
  2624. } break;
  2625. case TRANSFORM2D: {
  2626. uint32_t h = HASH_MURMUR3_SEED;
  2627. const Transform2D &t = *_data._transform2d;
  2628. h = hash_murmur3_one_real(t[0].x, h);
  2629. h = hash_murmur3_one_real(t[0].y, h);
  2630. h = hash_murmur3_one_real(t[1].x, h);
  2631. h = hash_murmur3_one_real(t[1].y, h);
  2632. h = hash_murmur3_one_real(t[2].x, h);
  2633. h = hash_murmur3_one_real(t[2].y, h);
  2634. return hash_fmix32(h);
  2635. } break;
  2636. case VECTOR3: {
  2637. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3 *>(_data._mem));
  2638. } break;
  2639. case VECTOR3I: {
  2640. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3i *>(_data._mem));
  2641. } break;
  2642. case VECTOR4: {
  2643. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4 *>(_data._mem));
  2644. } break;
  2645. case VECTOR4I: {
  2646. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4i *>(_data._mem));
  2647. } break;
  2648. case PLANE: {
  2649. uint32_t h = HASH_MURMUR3_SEED;
  2650. const Plane &p = *reinterpret_cast<const Plane *>(_data._mem);
  2651. h = hash_murmur3_one_real(p.normal.x, h);
  2652. h = hash_murmur3_one_real(p.normal.y, h);
  2653. h = hash_murmur3_one_real(p.normal.z, h);
  2654. h = hash_murmur3_one_real(p.d, h);
  2655. return hash_fmix32(h);
  2656. } break;
  2657. case AABB: {
  2658. return HashMapHasherDefault::hash(*_data._aabb);
  2659. } break;
  2660. case QUATERNION: {
  2661. uint32_t h = HASH_MURMUR3_SEED;
  2662. const Quaternion &q = *reinterpret_cast<const Quaternion *>(_data._mem);
  2663. h = hash_murmur3_one_real(q.x, h);
  2664. h = hash_murmur3_one_real(q.y, h);
  2665. h = hash_murmur3_one_real(q.z, h);
  2666. h = hash_murmur3_one_real(q.w, h);
  2667. return hash_fmix32(h);
  2668. } break;
  2669. case BASIS: {
  2670. uint32_t h = HASH_MURMUR3_SEED;
  2671. const Basis &b = *_data._basis;
  2672. h = hash_murmur3_one_real(b[0].x, h);
  2673. h = hash_murmur3_one_real(b[0].y, h);
  2674. h = hash_murmur3_one_real(b[0].z, h);
  2675. h = hash_murmur3_one_real(b[1].x, h);
  2676. h = hash_murmur3_one_real(b[1].y, h);
  2677. h = hash_murmur3_one_real(b[1].z, h);
  2678. h = hash_murmur3_one_real(b[2].x, h);
  2679. h = hash_murmur3_one_real(b[2].y, h);
  2680. h = hash_murmur3_one_real(b[2].z, h);
  2681. return hash_fmix32(h);
  2682. } break;
  2683. case TRANSFORM3D: {
  2684. uint32_t h = HASH_MURMUR3_SEED;
  2685. const Transform3D &t = *_data._transform3d;
  2686. h = hash_murmur3_one_real(t.basis[0].x, h);
  2687. h = hash_murmur3_one_real(t.basis[0].y, h);
  2688. h = hash_murmur3_one_real(t.basis[0].z, h);
  2689. h = hash_murmur3_one_real(t.basis[1].x, h);
  2690. h = hash_murmur3_one_real(t.basis[1].y, h);
  2691. h = hash_murmur3_one_real(t.basis[1].z, h);
  2692. h = hash_murmur3_one_real(t.basis[2].x, h);
  2693. h = hash_murmur3_one_real(t.basis[2].y, h);
  2694. h = hash_murmur3_one_real(t.basis[2].z, h);
  2695. h = hash_murmur3_one_real(t.origin.x, h);
  2696. h = hash_murmur3_one_real(t.origin.y, h);
  2697. h = hash_murmur3_one_real(t.origin.z, h);
  2698. return hash_fmix32(h);
  2699. } break;
  2700. case PROJECTION: {
  2701. uint32_t h = HASH_MURMUR3_SEED;
  2702. const Projection &t = *_data._projection;
  2703. h = hash_murmur3_one_real(t.columns[0].x, h);
  2704. h = hash_murmur3_one_real(t.columns[0].y, h);
  2705. h = hash_murmur3_one_real(t.columns[0].z, h);
  2706. h = hash_murmur3_one_real(t.columns[0].w, h);
  2707. h = hash_murmur3_one_real(t.columns[1].x, h);
  2708. h = hash_murmur3_one_real(t.columns[1].y, h);
  2709. h = hash_murmur3_one_real(t.columns[1].z, h);
  2710. h = hash_murmur3_one_real(t.columns[1].w, h);
  2711. h = hash_murmur3_one_real(t.columns[2].x, h);
  2712. h = hash_murmur3_one_real(t.columns[2].y, h);
  2713. h = hash_murmur3_one_real(t.columns[2].z, h);
  2714. h = hash_murmur3_one_real(t.columns[2].w, h);
  2715. h = hash_murmur3_one_real(t.columns[3].x, h);
  2716. h = hash_murmur3_one_real(t.columns[3].y, h);
  2717. h = hash_murmur3_one_real(t.columns[3].z, h);
  2718. h = hash_murmur3_one_real(t.columns[3].w, h);
  2719. return hash_fmix32(h);
  2720. } break;
  2721. // misc types
  2722. case COLOR: {
  2723. uint32_t h = HASH_MURMUR3_SEED;
  2724. const Color &c = *reinterpret_cast<const Color *>(_data._mem);
  2725. h = hash_murmur3_one_float(c.r, h);
  2726. h = hash_murmur3_one_float(c.g, h);
  2727. h = hash_murmur3_one_float(c.b, h);
  2728. h = hash_murmur3_one_float(c.a, h);
  2729. return hash_fmix32(h);
  2730. } break;
  2731. case RID: {
  2732. return hash_one_uint64(reinterpret_cast<const ::RID *>(_data._mem)->get_id());
  2733. } break;
  2734. case OBJECT: {
  2735. return hash_one_uint64(hash_make_uint64_t(_get_obj().obj));
  2736. } break;
  2737. case STRING_NAME: {
  2738. return reinterpret_cast<const StringName *>(_data._mem)->hash();
  2739. } break;
  2740. case NODE_PATH: {
  2741. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2742. } break;
  2743. case DICTIONARY: {
  2744. return reinterpret_cast<const Dictionary *>(_data._mem)->recursive_hash(recursion_count);
  2745. } break;
  2746. case CALLABLE: {
  2747. return reinterpret_cast<const Callable *>(_data._mem)->hash();
  2748. } break;
  2749. case SIGNAL: {
  2750. const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
  2751. uint32_t hash = s.get_name().hash();
  2752. return hash_murmur3_one_64(s.get_object_id(), hash);
  2753. } break;
  2754. case ARRAY: {
  2755. const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
  2756. return arr.recursive_hash(recursion_count);
  2757. } break;
  2758. case PACKED_BYTE_ARRAY: {
  2759. const PackedByteArray &arr = PackedArrayRef<uint8_t>::get_array(_data.packed_array);
  2760. int len = arr.size();
  2761. if (likely(len)) {
  2762. const uint8_t *r = arr.ptr();
  2763. return hash_murmur3_buffer((uint8_t *)&r[0], len);
  2764. } else {
  2765. return hash_murmur3_one_64(0);
  2766. }
  2767. } break;
  2768. case PACKED_INT32_ARRAY: {
  2769. const PackedInt32Array &arr = PackedArrayRef<int32_t>::get_array(_data.packed_array);
  2770. int len = arr.size();
  2771. if (likely(len)) {
  2772. const int32_t *r = arr.ptr();
  2773. return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int32_t));
  2774. } else {
  2775. return hash_murmur3_one_64(0);
  2776. }
  2777. } break;
  2778. case PACKED_INT64_ARRAY: {
  2779. const PackedInt64Array &arr = PackedArrayRef<int64_t>::get_array(_data.packed_array);
  2780. int len = arr.size();
  2781. if (likely(len)) {
  2782. const int64_t *r = arr.ptr();
  2783. return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int64_t));
  2784. } else {
  2785. return hash_murmur3_one_64(0);
  2786. }
  2787. } break;
  2788. case PACKED_FLOAT32_ARRAY: {
  2789. const PackedFloat32Array &arr = PackedArrayRef<float>::get_array(_data.packed_array);
  2790. int len = arr.size();
  2791. if (likely(len)) {
  2792. const float *r = arr.ptr();
  2793. uint32_t h = HASH_MURMUR3_SEED;
  2794. for (int32_t i = 0; i < len; i++) {
  2795. h = hash_murmur3_one_float(r[i], h);
  2796. }
  2797. return hash_fmix32(h);
  2798. } else {
  2799. return hash_murmur3_one_float(0.0);
  2800. }
  2801. } break;
  2802. case PACKED_FLOAT64_ARRAY: {
  2803. const PackedFloat64Array &arr = PackedArrayRef<double>::get_array(_data.packed_array);
  2804. int len = arr.size();
  2805. if (likely(len)) {
  2806. const double *r = arr.ptr();
  2807. uint32_t h = HASH_MURMUR3_SEED;
  2808. for (int32_t i = 0; i < len; i++) {
  2809. h = hash_murmur3_one_double(r[i], h);
  2810. }
  2811. return hash_fmix32(h);
  2812. } else {
  2813. return hash_murmur3_one_double(0.0);
  2814. }
  2815. } break;
  2816. case PACKED_STRING_ARRAY: {
  2817. uint32_t hash = HASH_MURMUR3_SEED;
  2818. const PackedStringArray &arr = PackedArrayRef<String>::get_array(_data.packed_array);
  2819. int len = arr.size();
  2820. if (likely(len)) {
  2821. const String *r = arr.ptr();
  2822. for (int i = 0; i < len; i++) {
  2823. hash = hash_murmur3_one_32(r[i].hash(), hash);
  2824. }
  2825. hash = hash_fmix32(hash);
  2826. }
  2827. return hash;
  2828. } break;
  2829. case PACKED_VECTOR2_ARRAY: {
  2830. uint32_t hash = HASH_MURMUR3_SEED;
  2831. const PackedVector2Array &arr = PackedArrayRef<Vector2>::get_array(_data.packed_array);
  2832. int len = arr.size();
  2833. if (likely(len)) {
  2834. const Vector2 *r = arr.ptr();
  2835. for (int i = 0; i < len; i++) {
  2836. hash = hash_murmur3_one_real(r[i].x, hash);
  2837. hash = hash_murmur3_one_real(r[i].y, hash);
  2838. }
  2839. hash = hash_fmix32(hash);
  2840. }
  2841. return hash;
  2842. } break;
  2843. case PACKED_VECTOR3_ARRAY: {
  2844. uint32_t hash = HASH_MURMUR3_SEED;
  2845. const PackedVector3Array &arr = PackedArrayRef<Vector3>::get_array(_data.packed_array);
  2846. int len = arr.size();
  2847. if (likely(len)) {
  2848. const Vector3 *r = arr.ptr();
  2849. for (int i = 0; i < len; i++) {
  2850. hash = hash_murmur3_one_real(r[i].x, hash);
  2851. hash = hash_murmur3_one_real(r[i].y, hash);
  2852. hash = hash_murmur3_one_real(r[i].z, hash);
  2853. }
  2854. hash = hash_fmix32(hash);
  2855. }
  2856. return hash;
  2857. } break;
  2858. case PACKED_COLOR_ARRAY: {
  2859. uint32_t hash = HASH_MURMUR3_SEED;
  2860. const PackedColorArray &arr = PackedArrayRef<Color>::get_array(_data.packed_array);
  2861. int len = arr.size();
  2862. if (likely(len)) {
  2863. const Color *r = arr.ptr();
  2864. for (int i = 0; i < len; i++) {
  2865. hash = hash_murmur3_one_float(r[i].r, hash);
  2866. hash = hash_murmur3_one_float(r[i].g, hash);
  2867. hash = hash_murmur3_one_float(r[i].b, hash);
  2868. hash = hash_murmur3_one_float(r[i].a, hash);
  2869. }
  2870. hash = hash_fmix32(hash);
  2871. }
  2872. return hash;
  2873. } break;
  2874. case PACKED_VECTOR4_ARRAY: {
  2875. uint32_t hash = HASH_MURMUR3_SEED;
  2876. const PackedVector4Array &arr = PackedArrayRef<Vector4>::get_array(_data.packed_array);
  2877. int len = arr.size();
  2878. if (likely(len)) {
  2879. const Vector4 *r = arr.ptr();
  2880. for (int i = 0; i < len; i++) {
  2881. hash = hash_murmur3_one_real(r[i].x, hash);
  2882. hash = hash_murmur3_one_real(r[i].y, hash);
  2883. hash = hash_murmur3_one_real(r[i].z, hash);
  2884. hash = hash_murmur3_one_real(r[i].w, hash);
  2885. }
  2886. hash = hash_fmix32(hash);
  2887. }
  2888. return hash;
  2889. } break;
  2890. default: {
  2891. }
  2892. }
  2893. return 0;
  2894. }
  2895. #define hash_compare_scalar_base(p_lhs, p_rhs, semantic_comparison) \
  2896. (((p_lhs) == (p_rhs)) || (semantic_comparison && Math::is_nan(p_lhs) && Math::is_nan(p_rhs)))
  2897. #define hash_compare_scalar(p_lhs, p_rhs) \
  2898. (hash_compare_scalar_base(p_lhs, p_rhs, true))
  2899. #define hash_compare_vector2(p_lhs, p_rhs) \
  2900. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2901. hash_compare_scalar((p_lhs).y, (p_rhs).y))
  2902. #define hash_compare_vector3(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. #define hash_compare_vector4(p_lhs, p_rhs) \
  2907. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2908. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2909. hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
  2910. hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2911. #define hash_compare_quaternion(p_lhs, p_rhs) \
  2912. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2913. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2914. hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
  2915. hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2916. #define hash_compare_color(p_lhs, p_rhs) \
  2917. (hash_compare_scalar((p_lhs).r, (p_rhs).r) && \
  2918. hash_compare_scalar((p_lhs).g, (p_rhs).g) && \
  2919. hash_compare_scalar((p_lhs).b, (p_rhs).b) && \
  2920. hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2921. #define hash_compare_packed_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2922. const Vector<p_type> &l = PackedArrayRef<p_type>::get_array(p_lhs); \
  2923. const Vector<p_type> &r = PackedArrayRef<p_type>::get_array(p_rhs); \
  2924. \
  2925. if (l.size() != r.size()) \
  2926. return false; \
  2927. \
  2928. const p_type *lr = l.ptr(); \
  2929. const p_type *rr = r.ptr(); \
  2930. \
  2931. for (int i = 0; i < l.size(); ++i) { \
  2932. if (!p_compare_func((lr[i]), (rr[i]))) \
  2933. return false; \
  2934. } \
  2935. \
  2936. return true
  2937. bool Variant::hash_compare(const Variant &p_variant, int recursion_count, bool semantic_comparison) const {
  2938. if (type != p_variant.type) {
  2939. return false;
  2940. }
  2941. switch (type) {
  2942. case INT: {
  2943. return _data._int == p_variant._data._int;
  2944. } break;
  2945. case FLOAT: {
  2946. return hash_compare_scalar_base(_data._float, p_variant._data._float, semantic_comparison);
  2947. } break;
  2948. case STRING: {
  2949. return *reinterpret_cast<const String *>(_data._mem) == *reinterpret_cast<const String *>(p_variant._data._mem);
  2950. } break;
  2951. case STRING_NAME: {
  2952. return *reinterpret_cast<const StringName *>(_data._mem) == *reinterpret_cast<const StringName *>(p_variant._data._mem);
  2953. } break;
  2954. case VECTOR2: {
  2955. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2956. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2957. return hash_compare_vector2(*l, *r);
  2958. } break;
  2959. case VECTOR2I: {
  2960. const Vector2i *l = reinterpret_cast<const Vector2i *>(_data._mem);
  2961. const Vector2i *r = reinterpret_cast<const Vector2i *>(p_variant._data._mem);
  2962. return *l == *r;
  2963. } break;
  2964. case RECT2: {
  2965. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2966. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2967. return hash_compare_vector2(l->position, r->position) &&
  2968. hash_compare_vector2(l->size, r->size);
  2969. } break;
  2970. case RECT2I: {
  2971. const Rect2i *l = reinterpret_cast<const Rect2i *>(_data._mem);
  2972. const Rect2i *r = reinterpret_cast<const Rect2i *>(p_variant._data._mem);
  2973. return *l == *r;
  2974. } break;
  2975. case TRANSFORM2D: {
  2976. Transform2D *l = _data._transform2d;
  2977. Transform2D *r = p_variant._data._transform2d;
  2978. for (int i = 0; i < 3; i++) {
  2979. if (!hash_compare_vector2(l->columns[i], r->columns[i])) {
  2980. return false;
  2981. }
  2982. }
  2983. return true;
  2984. } break;
  2985. case VECTOR3: {
  2986. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2987. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2988. return hash_compare_vector3(*l, *r);
  2989. } break;
  2990. case VECTOR3I: {
  2991. const Vector3i *l = reinterpret_cast<const Vector3i *>(_data._mem);
  2992. const Vector3i *r = reinterpret_cast<const Vector3i *>(p_variant._data._mem);
  2993. return *l == *r;
  2994. } break;
  2995. case VECTOR4: {
  2996. const Vector4 *l = reinterpret_cast<const Vector4 *>(_data._mem);
  2997. const Vector4 *r = reinterpret_cast<const Vector4 *>(p_variant._data._mem);
  2998. return hash_compare_vector4(*l, *r);
  2999. } break;
  3000. case VECTOR4I: {
  3001. const Vector4i *l = reinterpret_cast<const Vector4i *>(_data._mem);
  3002. const Vector4i *r = reinterpret_cast<const Vector4i *>(p_variant._data._mem);
  3003. return *l == *r;
  3004. } break;
  3005. case PLANE: {
  3006. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  3007. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  3008. return hash_compare_vector3(l->normal, r->normal) &&
  3009. hash_compare_scalar(l->d, r->d);
  3010. } break;
  3011. case AABB: {
  3012. const ::AABB *l = _data._aabb;
  3013. const ::AABB *r = p_variant._data._aabb;
  3014. return hash_compare_vector3(l->position, r->position) &&
  3015. hash_compare_vector3(l->size, r->size);
  3016. } break;
  3017. case QUATERNION: {
  3018. const Quaternion *l = reinterpret_cast<const Quaternion *>(_data._mem);
  3019. const Quaternion *r = reinterpret_cast<const Quaternion *>(p_variant._data._mem);
  3020. return hash_compare_quaternion(*l, *r);
  3021. } break;
  3022. case BASIS: {
  3023. const Basis *l = _data._basis;
  3024. const Basis *r = p_variant._data._basis;
  3025. for (int i = 0; i < 3; i++) {
  3026. if (!hash_compare_vector3(l->rows[i], r->rows[i])) {
  3027. return false;
  3028. }
  3029. }
  3030. return true;
  3031. } break;
  3032. case TRANSFORM3D: {
  3033. const Transform3D *l = _data._transform3d;
  3034. const Transform3D *r = p_variant._data._transform3d;
  3035. for (int i = 0; i < 3; i++) {
  3036. if (!hash_compare_vector3(l->basis.rows[i], r->basis.rows[i])) {
  3037. return false;
  3038. }
  3039. }
  3040. return hash_compare_vector3(l->origin, r->origin);
  3041. } break;
  3042. case PROJECTION: {
  3043. const Projection *l = _data._projection;
  3044. const Projection *r = p_variant._data._projection;
  3045. for (int i = 0; i < 4; i++) {
  3046. if (!hash_compare_vector4(l->columns[i], r->columns[i])) {
  3047. return false;
  3048. }
  3049. }
  3050. return true;
  3051. } break;
  3052. case COLOR: {
  3053. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  3054. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  3055. return hash_compare_color(*l, *r);
  3056. } break;
  3057. case ARRAY: {
  3058. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  3059. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  3060. if (!l.recursive_equal(r, recursion_count + 1)) {
  3061. return false;
  3062. }
  3063. return true;
  3064. } break;
  3065. case DICTIONARY: {
  3066. const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
  3067. const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
  3068. if (!l.recursive_equal(r, recursion_count + 1)) {
  3069. return false;
  3070. }
  3071. return true;
  3072. } break;
  3073. // This is for floating point comparisons only.
  3074. case PACKED_FLOAT32_ARRAY: {
  3075. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, float, hash_compare_scalar);
  3076. } break;
  3077. case PACKED_FLOAT64_ARRAY: {
  3078. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, double, hash_compare_scalar);
  3079. } break;
  3080. case PACKED_VECTOR2_ARRAY: {
  3081. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector2, hash_compare_vector2);
  3082. } break;
  3083. case PACKED_VECTOR3_ARRAY: {
  3084. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector3, hash_compare_vector3);
  3085. } break;
  3086. case PACKED_COLOR_ARRAY: {
  3087. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Color, hash_compare_color);
  3088. } break;
  3089. case PACKED_VECTOR4_ARRAY: {
  3090. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector4, hash_compare_vector4);
  3091. } break;
  3092. default:
  3093. bool v;
  3094. Variant r;
  3095. evaluate(OP_EQUAL, *this, p_variant, r, v);
  3096. return r;
  3097. }
  3098. }
  3099. bool Variant::identity_compare(const Variant &p_variant) const {
  3100. if (type != p_variant.type) {
  3101. return false;
  3102. }
  3103. switch (type) {
  3104. case OBJECT: {
  3105. return _get_obj().id == p_variant._get_obj().id;
  3106. } break;
  3107. case DICTIONARY: {
  3108. const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
  3109. const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
  3110. return l.id() == r.id();
  3111. } break;
  3112. case ARRAY: {
  3113. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  3114. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  3115. return l.id() == r.id();
  3116. } break;
  3117. case PACKED_BYTE_ARRAY:
  3118. case PACKED_INT32_ARRAY:
  3119. case PACKED_INT64_ARRAY:
  3120. case PACKED_FLOAT32_ARRAY:
  3121. case PACKED_FLOAT64_ARRAY:
  3122. case PACKED_STRING_ARRAY:
  3123. case PACKED_VECTOR2_ARRAY:
  3124. case PACKED_VECTOR3_ARRAY:
  3125. case PACKED_COLOR_ARRAY:
  3126. case PACKED_VECTOR4_ARRAY: {
  3127. return _data.packed_array == p_variant._data.packed_array;
  3128. } break;
  3129. default: {
  3130. return hash_compare(p_variant);
  3131. }
  3132. }
  3133. }
  3134. bool StringLikeVariantComparator::compare(const Variant &p_lhs, const Variant &p_rhs) {
  3135. if (p_lhs.hash_compare(p_rhs)) {
  3136. return true;
  3137. }
  3138. if (p_lhs.get_type() == Variant::STRING && p_rhs.get_type() == Variant::STRING_NAME) {
  3139. return *VariantInternal::get_string(&p_lhs) == *VariantInternal::get_string_name(&p_rhs);
  3140. }
  3141. if (p_lhs.get_type() == Variant::STRING_NAME && p_rhs.get_type() == Variant::STRING) {
  3142. return *VariantInternal::get_string_name(&p_lhs) == *VariantInternal::get_string(&p_rhs);
  3143. }
  3144. return false;
  3145. }
  3146. bool Variant::is_ref_counted() const {
  3147. return type == OBJECT && _get_obj().id.is_ref_counted();
  3148. }
  3149. void Variant::static_assign(const Variant &p_variant) {
  3150. }
  3151. bool Variant::is_type_shared(Variant::Type p_type) {
  3152. switch (p_type) {
  3153. case OBJECT:
  3154. case ARRAY:
  3155. case DICTIONARY:
  3156. return true;
  3157. default: {
  3158. }
  3159. }
  3160. return false;
  3161. }
  3162. bool Variant::is_shared() const {
  3163. return is_type_shared(type);
  3164. }
  3165. bool Variant::is_read_only() const {
  3166. switch (type) {
  3167. case ARRAY:
  3168. return reinterpret_cast<const Array *>(_data._mem)->is_read_only();
  3169. case DICTIONARY:
  3170. return reinterpret_cast<const Dictionary *>(_data._mem)->is_read_only();
  3171. default:
  3172. return false;
  3173. }
  3174. }
  3175. void Variant::_variant_call_error(const String &p_method, Callable::CallError &error) {
  3176. switch (error.error) {
  3177. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  3178. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(Variant::Type(error.expected)) + "'.";
  3179. ERR_PRINT(err.utf8().get_data());
  3180. } break;
  3181. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  3182. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  3183. ERR_PRINT(err.utf8().get_data());
  3184. } break;
  3185. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  3186. String err = "Too many arguments for method '" + p_method + "'";
  3187. ERR_PRINT(err.utf8().get_data());
  3188. } break;
  3189. default: {
  3190. }
  3191. }
  3192. }
  3193. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  3194. r_value = Variant();
  3195. }
  3196. String Variant::get_construct_string() const {
  3197. String vars;
  3198. VariantWriter::write_to_string(*this, vars);
  3199. return vars;
  3200. }
  3201. String Variant::get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3202. return get_call_error_text(nullptr, p_method, p_argptrs, p_argcount, ce);
  3203. }
  3204. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3205. String err_text;
  3206. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  3207. int errorarg = ce.argument;
  3208. if (p_argptrs) {
  3209. 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));
  3210. } else {
  3211. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(Variant::Type(ce.expected));
  3212. }
  3213. } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  3214. err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
  3215. } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  3216. err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
  3217. } else if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  3218. err_text = "Method not found";
  3219. } else if (ce.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  3220. err_text = "Instance is null";
  3221. } else if (ce.error == Callable::CallError::CALL_ERROR_METHOD_NOT_CONST) {
  3222. err_text = "Method not const in const instance";
  3223. } else if (ce.error == Callable::CallError::CALL_OK) {
  3224. return "Call OK";
  3225. }
  3226. String base_text;
  3227. if (p_base) {
  3228. base_text = p_base->get_class();
  3229. Ref<Resource> script = p_base->get_script();
  3230. if (script.is_valid() && script->get_path().is_resource_file()) {
  3231. base_text += "(" + script->get_path().get_file() + ")";
  3232. }
  3233. base_text += "::";
  3234. }
  3235. return "'" + base_text + String(p_method) + "': " + err_text;
  3236. }
  3237. String Variant::get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3238. Vector<Variant> binds;
  3239. int args_bound;
  3240. p_callable.get_bound_arguments_ref(binds, args_bound);
  3241. if (args_bound <= 0) {
  3242. return get_call_error_text(p_callable.get_object(), p_callable.get_method(), p_argptrs, MAX(0, p_argcount + args_bound), ce);
  3243. } else {
  3244. Vector<const Variant *> argptrs;
  3245. argptrs.resize(p_argcount + binds.size());
  3246. for (int i = 0; i < p_argcount; i++) {
  3247. argptrs.write[i] = p_argptrs[i];
  3248. }
  3249. for (int i = 0; i < binds.size(); i++) {
  3250. argptrs.write[i + p_argcount] = &binds[i];
  3251. }
  3252. return get_call_error_text(p_callable.get_object(), p_callable.get_method(), (const Variant **)argptrs.ptr(), argptrs.size(), ce);
  3253. }
  3254. }
  3255. void Variant::register_types() {
  3256. _register_variant_operators();
  3257. _register_variant_methods();
  3258. _register_variant_setters_getters();
  3259. _register_variant_constructors();
  3260. _register_variant_destructors();
  3261. _register_variant_utility_functions();
  3262. }
  3263. void Variant::unregister_types() {
  3264. _unregister_variant_operators();
  3265. _unregister_variant_methods();
  3266. _unregister_variant_setters_getters();
  3267. _unregister_variant_destructors();
  3268. _unregister_variant_utility_functions();
  3269. }