variant.cpp 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711
  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. bool Variant::initialize_ref(Object *p_object) {
  939. RefCounted *ref_counted = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_object));
  940. if (!ref_counted->init_ref()) {
  941. return false;
  942. }
  943. return true;
  944. }
  945. void Variant::reference(const Variant &p_variant) {
  946. switch (type) {
  947. case NIL:
  948. case BOOL:
  949. case INT:
  950. case FLOAT:
  951. break;
  952. default:
  953. clear();
  954. }
  955. type = p_variant.type;
  956. switch (p_variant.type) {
  957. case NIL: {
  958. // None.
  959. } break;
  960. // Atomic types.
  961. case BOOL: {
  962. _data._bool = p_variant._data._bool;
  963. } break;
  964. case INT: {
  965. _data._int = p_variant._data._int;
  966. } break;
  967. case FLOAT: {
  968. _data._float = p_variant._data._float;
  969. } break;
  970. case STRING: {
  971. memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem)));
  972. } break;
  973. // Math types.
  974. case VECTOR2: {
  975. memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem)));
  976. } break;
  977. case VECTOR2I: {
  978. memnew_placement(_data._mem, Vector2i(*reinterpret_cast<const Vector2i *>(p_variant._data._mem)));
  979. } break;
  980. case RECT2: {
  981. memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem)));
  982. } break;
  983. case RECT2I: {
  984. memnew_placement(_data._mem, Rect2i(*reinterpret_cast<const Rect2i *>(p_variant._data._mem)));
  985. } break;
  986. case TRANSFORM2D: {
  987. _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
  988. memnew_placement(_data._transform2d, Transform2D(*p_variant._data._transform2d));
  989. } break;
  990. case VECTOR3: {
  991. memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(p_variant._data._mem)));
  992. } break;
  993. case VECTOR3I: {
  994. memnew_placement(_data._mem, Vector3i(*reinterpret_cast<const Vector3i *>(p_variant._data._mem)));
  995. } break;
  996. case VECTOR4: {
  997. memnew_placement(_data._mem, Vector4(*reinterpret_cast<const Vector4 *>(p_variant._data._mem)));
  998. } break;
  999. case VECTOR4I: {
  1000. memnew_placement(_data._mem, Vector4i(*reinterpret_cast<const Vector4i *>(p_variant._data._mem)));
  1001. } break;
  1002. case PLANE: {
  1003. memnew_placement(_data._mem, Plane(*reinterpret_cast<const Plane *>(p_variant._data._mem)));
  1004. } break;
  1005. case AABB: {
  1006. _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
  1007. memnew_placement(_data._aabb, ::AABB(*p_variant._data._aabb));
  1008. } break;
  1009. case QUATERNION: {
  1010. memnew_placement(_data._mem, Quaternion(*reinterpret_cast<const Quaternion *>(p_variant._data._mem)));
  1011. } break;
  1012. case BASIS: {
  1013. _data._basis = (Basis *)Pools::_bucket_medium.alloc();
  1014. memnew_placement(_data._basis, Basis(*p_variant._data._basis));
  1015. } break;
  1016. case TRANSFORM3D: {
  1017. _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
  1018. memnew_placement(_data._transform3d, Transform3D(*p_variant._data._transform3d));
  1019. } break;
  1020. case PROJECTION: {
  1021. _data._projection = (Projection *)Pools::_bucket_large.alloc();
  1022. memnew_placement(_data._projection, Projection(*p_variant._data._projection));
  1023. } break;
  1024. // Miscellaneous types.
  1025. case COLOR: {
  1026. memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem)));
  1027. } break;
  1028. case RID: {
  1029. memnew_placement(_data._mem, ::RID(*reinterpret_cast<const ::RID *>(p_variant._data._mem)));
  1030. } break;
  1031. case OBJECT: {
  1032. memnew_placement(_data._mem, ObjData);
  1033. if (p_variant._get_obj().obj && p_variant._get_obj().id.is_ref_counted()) {
  1034. RefCounted *ref_counted = static_cast<RefCounted *>(p_variant._get_obj().obj);
  1035. if (!ref_counted->reference()) {
  1036. _get_obj().obj = nullptr;
  1037. _get_obj().id = ObjectID();
  1038. break;
  1039. }
  1040. }
  1041. _get_obj().obj = const_cast<Object *>(p_variant._get_obj().obj);
  1042. _get_obj().id = p_variant._get_obj().id;
  1043. } break;
  1044. case CALLABLE: {
  1045. memnew_placement(_data._mem, Callable(*reinterpret_cast<const Callable *>(p_variant._data._mem)));
  1046. } break;
  1047. case SIGNAL: {
  1048. memnew_placement(_data._mem, Signal(*reinterpret_cast<const Signal *>(p_variant._data._mem)));
  1049. } break;
  1050. case STRING_NAME: {
  1051. memnew_placement(_data._mem, StringName(*reinterpret_cast<const StringName *>(p_variant._data._mem)));
  1052. } break;
  1053. case NODE_PATH: {
  1054. memnew_placement(_data._mem, NodePath(*reinterpret_cast<const NodePath *>(p_variant._data._mem)));
  1055. } break;
  1056. case DICTIONARY: {
  1057. memnew_placement(_data._mem, Dictionary(*reinterpret_cast<const Dictionary *>(p_variant._data._mem)));
  1058. } break;
  1059. case ARRAY: {
  1060. memnew_placement(_data._mem, Array(*reinterpret_cast<const Array *>(p_variant._data._mem)));
  1061. } break;
  1062. // Arrays.
  1063. case PACKED_BYTE_ARRAY: {
  1064. _data.packed_array = static_cast<PackedArrayRef<uint8_t> *>(p_variant._data.packed_array)->reference();
  1065. if (!_data.packed_array) {
  1066. _data.packed_array = PackedArrayRef<uint8_t>::create();
  1067. }
  1068. } break;
  1069. case PACKED_INT32_ARRAY: {
  1070. _data.packed_array = static_cast<PackedArrayRef<int32_t> *>(p_variant._data.packed_array)->reference();
  1071. if (!_data.packed_array) {
  1072. _data.packed_array = PackedArrayRef<int32_t>::create();
  1073. }
  1074. } break;
  1075. case PACKED_INT64_ARRAY: {
  1076. _data.packed_array = static_cast<PackedArrayRef<int64_t> *>(p_variant._data.packed_array)->reference();
  1077. if (!_data.packed_array) {
  1078. _data.packed_array = PackedArrayRef<int64_t>::create();
  1079. }
  1080. } break;
  1081. case PACKED_FLOAT32_ARRAY: {
  1082. _data.packed_array = static_cast<PackedArrayRef<float> *>(p_variant._data.packed_array)->reference();
  1083. if (!_data.packed_array) {
  1084. _data.packed_array = PackedArrayRef<float>::create();
  1085. }
  1086. } break;
  1087. case PACKED_FLOAT64_ARRAY: {
  1088. _data.packed_array = static_cast<PackedArrayRef<double> *>(p_variant._data.packed_array)->reference();
  1089. if (!_data.packed_array) {
  1090. _data.packed_array = PackedArrayRef<double>::create();
  1091. }
  1092. } break;
  1093. case PACKED_STRING_ARRAY: {
  1094. _data.packed_array = static_cast<PackedArrayRef<String> *>(p_variant._data.packed_array)->reference();
  1095. if (!_data.packed_array) {
  1096. _data.packed_array = PackedArrayRef<String>::create();
  1097. }
  1098. } break;
  1099. case PACKED_VECTOR2_ARRAY: {
  1100. _data.packed_array = static_cast<PackedArrayRef<Vector2> *>(p_variant._data.packed_array)->reference();
  1101. if (!_data.packed_array) {
  1102. _data.packed_array = PackedArrayRef<Vector2>::create();
  1103. }
  1104. } break;
  1105. case PACKED_VECTOR3_ARRAY: {
  1106. _data.packed_array = static_cast<PackedArrayRef<Vector3> *>(p_variant._data.packed_array)->reference();
  1107. if (!_data.packed_array) {
  1108. _data.packed_array = PackedArrayRef<Vector3>::create();
  1109. }
  1110. } break;
  1111. case PACKED_COLOR_ARRAY: {
  1112. _data.packed_array = static_cast<PackedArrayRef<Color> *>(p_variant._data.packed_array)->reference();
  1113. if (!_data.packed_array) {
  1114. _data.packed_array = PackedArrayRef<Color>::create();
  1115. }
  1116. } break;
  1117. case PACKED_VECTOR4_ARRAY: {
  1118. _data.packed_array = static_cast<PackedArrayRef<Vector4> *>(p_variant._data.packed_array)->reference();
  1119. if (!_data.packed_array) {
  1120. _data.packed_array = PackedArrayRef<Vector4>::create();
  1121. }
  1122. } break;
  1123. default: {
  1124. }
  1125. }
  1126. }
  1127. void Variant::zero() {
  1128. switch (type) {
  1129. case NIL:
  1130. break;
  1131. case BOOL:
  1132. _data._bool = false;
  1133. break;
  1134. case INT:
  1135. _data._int = 0;
  1136. break;
  1137. case FLOAT:
  1138. _data._float = 0;
  1139. break;
  1140. case VECTOR2:
  1141. *reinterpret_cast<Vector2 *>(_data._mem) = Vector2();
  1142. break;
  1143. case VECTOR2I:
  1144. *reinterpret_cast<Vector2i *>(_data._mem) = Vector2i();
  1145. break;
  1146. case RECT2:
  1147. *reinterpret_cast<Rect2 *>(_data._mem) = Rect2();
  1148. break;
  1149. case RECT2I:
  1150. *reinterpret_cast<Rect2i *>(_data._mem) = Rect2i();
  1151. break;
  1152. case VECTOR3:
  1153. *reinterpret_cast<Vector3 *>(_data._mem) = Vector3();
  1154. break;
  1155. case VECTOR3I:
  1156. *reinterpret_cast<Vector3i *>(_data._mem) = Vector3i();
  1157. break;
  1158. case VECTOR4:
  1159. *reinterpret_cast<Vector4 *>(_data._mem) = Vector4();
  1160. break;
  1161. case VECTOR4I:
  1162. *reinterpret_cast<Vector4i *>(_data._mem) = Vector4i();
  1163. break;
  1164. case PLANE:
  1165. *reinterpret_cast<Plane *>(_data._mem) = Plane();
  1166. break;
  1167. case QUATERNION:
  1168. *reinterpret_cast<Quaternion *>(_data._mem) = Quaternion();
  1169. break;
  1170. case COLOR:
  1171. *reinterpret_cast<Color *>(_data._mem) = Color();
  1172. break;
  1173. default:
  1174. Type prev_type = type;
  1175. clear();
  1176. if (type != prev_type) {
  1177. // clear() changes type to NIL, so it needs to be restored.
  1178. Callable::CallError ce;
  1179. Variant::construct(prev_type, *this, nullptr, 0, ce);
  1180. }
  1181. break;
  1182. }
  1183. }
  1184. void Variant::_clear_internal() {
  1185. switch (type) {
  1186. case STRING: {
  1187. reinterpret_cast<String *>(_data._mem)->~String();
  1188. } break;
  1189. // Math types.
  1190. case TRANSFORM2D: {
  1191. if (_data._transform2d) {
  1192. _data._transform2d->~Transform2D();
  1193. Pools::_bucket_small.free((Pools::BucketSmall *)_data._transform2d);
  1194. _data._transform2d = nullptr;
  1195. }
  1196. } break;
  1197. case AABB: {
  1198. if (_data._aabb) {
  1199. _data._aabb->~AABB();
  1200. Pools::_bucket_small.free((Pools::BucketSmall *)_data._aabb);
  1201. _data._aabb = nullptr;
  1202. }
  1203. } break;
  1204. case BASIS: {
  1205. if (_data._basis) {
  1206. _data._basis->~Basis();
  1207. Pools::_bucket_medium.free((Pools::BucketMedium *)_data._basis);
  1208. _data._basis = nullptr;
  1209. }
  1210. } break;
  1211. case TRANSFORM3D: {
  1212. if (_data._transform3d) {
  1213. _data._transform3d->~Transform3D();
  1214. Pools::_bucket_medium.free((Pools::BucketMedium *)_data._transform3d);
  1215. _data._transform3d = nullptr;
  1216. }
  1217. } break;
  1218. case PROJECTION: {
  1219. if (_data._projection) {
  1220. _data._projection->~Projection();
  1221. Pools::_bucket_large.free((Pools::BucketLarge *)_data._projection);
  1222. _data._projection = nullptr;
  1223. }
  1224. } break;
  1225. // Miscellaneous types.
  1226. case STRING_NAME: {
  1227. reinterpret_cast<StringName *>(_data._mem)->~StringName();
  1228. } break;
  1229. case NODE_PATH: {
  1230. reinterpret_cast<NodePath *>(_data._mem)->~NodePath();
  1231. } break;
  1232. case OBJECT: {
  1233. if (_get_obj().id.is_ref_counted()) {
  1234. // We are safe that there is a reference here.
  1235. RefCounted *ref_counted = static_cast<RefCounted *>(_get_obj().obj);
  1236. if (ref_counted->unreference()) {
  1237. memdelete(ref_counted);
  1238. }
  1239. }
  1240. _get_obj().obj = nullptr;
  1241. _get_obj().id = ObjectID();
  1242. } break;
  1243. case RID: {
  1244. // Not much need probably.
  1245. // HACK: Can't seem to use destructor + scoping operator, so hack.
  1246. typedef ::RID RID_Class;
  1247. reinterpret_cast<RID_Class *>(_data._mem)->~RID_Class();
  1248. } break;
  1249. case CALLABLE: {
  1250. reinterpret_cast<Callable *>(_data._mem)->~Callable();
  1251. } break;
  1252. case SIGNAL: {
  1253. reinterpret_cast<Signal *>(_data._mem)->~Signal();
  1254. } break;
  1255. case DICTIONARY: {
  1256. reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary();
  1257. } break;
  1258. case ARRAY: {
  1259. reinterpret_cast<Array *>(_data._mem)->~Array();
  1260. } break;
  1261. // Arrays.
  1262. case PACKED_BYTE_ARRAY: {
  1263. PackedArrayRefBase::destroy(_data.packed_array);
  1264. } break;
  1265. case PACKED_INT32_ARRAY: {
  1266. PackedArrayRefBase::destroy(_data.packed_array);
  1267. } break;
  1268. case PACKED_INT64_ARRAY: {
  1269. PackedArrayRefBase::destroy(_data.packed_array);
  1270. } break;
  1271. case PACKED_FLOAT32_ARRAY: {
  1272. PackedArrayRefBase::destroy(_data.packed_array);
  1273. } break;
  1274. case PACKED_FLOAT64_ARRAY: {
  1275. PackedArrayRefBase::destroy(_data.packed_array);
  1276. } break;
  1277. case PACKED_STRING_ARRAY: {
  1278. PackedArrayRefBase::destroy(_data.packed_array);
  1279. } break;
  1280. case PACKED_VECTOR2_ARRAY: {
  1281. PackedArrayRefBase::destroy(_data.packed_array);
  1282. } break;
  1283. case PACKED_VECTOR3_ARRAY: {
  1284. PackedArrayRefBase::destroy(_data.packed_array);
  1285. } break;
  1286. case PACKED_COLOR_ARRAY: {
  1287. PackedArrayRefBase::destroy(_data.packed_array);
  1288. } break;
  1289. case PACKED_VECTOR4_ARRAY: {
  1290. PackedArrayRefBase::destroy(_data.packed_array);
  1291. } break;
  1292. default: {
  1293. // Not needed, there is no point. The following do not allocate memory:
  1294. // VECTOR2, VECTOR3, VECTOR4, RECT2, PLANE, QUATERNION, COLOR.
  1295. }
  1296. }
  1297. }
  1298. Variant::operator int64_t() const {
  1299. switch (type) {
  1300. case NIL:
  1301. return 0;
  1302. case BOOL:
  1303. return _data._bool ? 1 : 0;
  1304. case INT:
  1305. return _data._int;
  1306. case FLOAT:
  1307. return _data._float;
  1308. case STRING:
  1309. return operator String().to_int();
  1310. default: {
  1311. return 0;
  1312. }
  1313. }
  1314. }
  1315. Variant::operator int32_t() const {
  1316. switch (type) {
  1317. case NIL:
  1318. return 0;
  1319. case BOOL:
  1320. return _data._bool ? 1 : 0;
  1321. case INT:
  1322. return _data._int;
  1323. case FLOAT:
  1324. return _data._float;
  1325. case STRING:
  1326. return operator String().to_int();
  1327. default: {
  1328. return 0;
  1329. }
  1330. }
  1331. }
  1332. Variant::operator int16_t() const {
  1333. switch (type) {
  1334. case NIL:
  1335. return 0;
  1336. case BOOL:
  1337. return _data._bool ? 1 : 0;
  1338. case INT:
  1339. return _data._int;
  1340. case FLOAT:
  1341. return _data._float;
  1342. case STRING:
  1343. return operator String().to_int();
  1344. default: {
  1345. return 0;
  1346. }
  1347. }
  1348. }
  1349. Variant::operator int8_t() const {
  1350. switch (type) {
  1351. case NIL:
  1352. return 0;
  1353. case BOOL:
  1354. return _data._bool ? 1 : 0;
  1355. case INT:
  1356. return _data._int;
  1357. case FLOAT:
  1358. return _data._float;
  1359. case STRING:
  1360. return operator String().to_int();
  1361. default: {
  1362. return 0;
  1363. }
  1364. }
  1365. }
  1366. Variant::operator uint64_t() const {
  1367. switch (type) {
  1368. case NIL:
  1369. return 0;
  1370. case BOOL:
  1371. return _data._bool ? 1 : 0;
  1372. case INT:
  1373. return _data._int;
  1374. case FLOAT:
  1375. return _data._float;
  1376. case STRING:
  1377. return operator String().to_int();
  1378. default: {
  1379. return 0;
  1380. }
  1381. }
  1382. }
  1383. Variant::operator uint32_t() const {
  1384. switch (type) {
  1385. case NIL:
  1386. return 0;
  1387. case BOOL:
  1388. return _data._bool ? 1 : 0;
  1389. case INT:
  1390. return _data._int;
  1391. case FLOAT:
  1392. return _data._float;
  1393. case STRING:
  1394. return operator String().to_int();
  1395. default: {
  1396. return 0;
  1397. }
  1398. }
  1399. }
  1400. Variant::operator uint16_t() const {
  1401. switch (type) {
  1402. case NIL:
  1403. return 0;
  1404. case BOOL:
  1405. return _data._bool ? 1 : 0;
  1406. case INT:
  1407. return _data._int;
  1408. case FLOAT:
  1409. return _data._float;
  1410. case STRING:
  1411. return operator String().to_int();
  1412. default: {
  1413. return 0;
  1414. }
  1415. }
  1416. }
  1417. Variant::operator uint8_t() const {
  1418. switch (type) {
  1419. case NIL:
  1420. return 0;
  1421. case BOOL:
  1422. return _data._bool ? 1 : 0;
  1423. case INT:
  1424. return _data._int;
  1425. case FLOAT:
  1426. return _data._float;
  1427. case STRING:
  1428. return operator String().to_int();
  1429. default: {
  1430. return 0;
  1431. }
  1432. }
  1433. }
  1434. Variant::operator ObjectID() const {
  1435. if (type == INT) {
  1436. return ObjectID(_data._int);
  1437. } else if (type == OBJECT) {
  1438. return _get_obj().id;
  1439. } else {
  1440. return ObjectID();
  1441. }
  1442. }
  1443. Variant::operator char32_t() const {
  1444. return operator uint32_t();
  1445. }
  1446. Variant::operator float() const {
  1447. switch (type) {
  1448. case NIL:
  1449. return 0;
  1450. case BOOL:
  1451. return _data._bool ? 1.0 : 0.0;
  1452. case INT:
  1453. return (float)_data._int;
  1454. case FLOAT:
  1455. return _data._float;
  1456. case STRING:
  1457. return operator String().to_float();
  1458. default: {
  1459. return 0;
  1460. }
  1461. }
  1462. }
  1463. Variant::operator double() const {
  1464. switch (type) {
  1465. case NIL:
  1466. return 0;
  1467. case BOOL:
  1468. return _data._bool ? 1.0 : 0.0;
  1469. case INT:
  1470. return (double)_data._int;
  1471. case FLOAT:
  1472. return _data._float;
  1473. case STRING:
  1474. return operator String().to_float();
  1475. default: {
  1476. return 0;
  1477. }
  1478. }
  1479. }
  1480. Variant::operator StringName() const {
  1481. if (type == STRING_NAME) {
  1482. return *reinterpret_cast<const StringName *>(_data._mem);
  1483. } else if (type == STRING) {
  1484. return *reinterpret_cast<const String *>(_data._mem);
  1485. }
  1486. return StringName();
  1487. }
  1488. struct _VariantStrPair {
  1489. String key;
  1490. String value;
  1491. bool operator<(const _VariantStrPair &p) const {
  1492. return key < p.key;
  1493. }
  1494. };
  1495. Variant::operator String() const {
  1496. return stringify(0);
  1497. }
  1498. String stringify_variant_clean(const Variant &p_variant, int recursion_count) {
  1499. String s = p_variant.stringify(recursion_count);
  1500. // Wrap strings in quotes to avoid ambiguity.
  1501. switch (p_variant.get_type()) {
  1502. case Variant::STRING: {
  1503. s = s.c_escape().quote();
  1504. } break;
  1505. case Variant::STRING_NAME: {
  1506. s = "&" + s.c_escape().quote();
  1507. } break;
  1508. case Variant::NODE_PATH: {
  1509. s = "^" + s.c_escape().quote();
  1510. } break;
  1511. default: {
  1512. } break;
  1513. }
  1514. return s;
  1515. }
  1516. template <typename T>
  1517. String stringify_vector(const T &vec, int recursion_count) {
  1518. String str("[");
  1519. for (int i = 0; i < vec.size(); i++) {
  1520. if (i > 0) {
  1521. str += ", ";
  1522. }
  1523. str += stringify_variant_clean(vec[i], recursion_count);
  1524. }
  1525. str += "]";
  1526. return str;
  1527. }
  1528. String Variant::stringify(int recursion_count) const {
  1529. switch (type) {
  1530. case NIL:
  1531. return "<null>";
  1532. case BOOL:
  1533. return _data._bool ? "true" : "false";
  1534. case INT:
  1535. return itos(_data._int);
  1536. case FLOAT:
  1537. return rtos(_data._float);
  1538. case STRING:
  1539. return *reinterpret_cast<const String *>(_data._mem);
  1540. case VECTOR2:
  1541. return operator Vector2();
  1542. case VECTOR2I:
  1543. return operator Vector2i();
  1544. case RECT2:
  1545. return operator Rect2();
  1546. case RECT2I:
  1547. return operator Rect2i();
  1548. case TRANSFORM2D:
  1549. return operator Transform2D();
  1550. case VECTOR3:
  1551. return operator Vector3();
  1552. case VECTOR3I:
  1553. return operator Vector3i();
  1554. case VECTOR4:
  1555. return operator Vector4();
  1556. case VECTOR4I:
  1557. return operator Vector4i();
  1558. case PLANE:
  1559. return operator Plane();
  1560. case AABB:
  1561. return operator ::AABB();
  1562. case QUATERNION:
  1563. return operator Quaternion();
  1564. case BASIS:
  1565. return operator Basis();
  1566. case TRANSFORM3D:
  1567. return operator Transform3D();
  1568. case PROJECTION:
  1569. return operator Projection();
  1570. case STRING_NAME:
  1571. return operator StringName();
  1572. case NODE_PATH:
  1573. return operator NodePath();
  1574. case COLOR:
  1575. return operator Color();
  1576. case DICTIONARY: {
  1577. ERR_FAIL_COND_V_MSG(recursion_count > MAX_RECURSION, "{ ... }", "Maximum dictionary recursion reached!");
  1578. recursion_count++;
  1579. const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
  1580. // Add leading and trailing space to Dictionary printing. This distinguishes it
  1581. // from array printing on fonts that have similar-looking {} and [] characters.
  1582. String str("{ ");
  1583. List<Variant> keys;
  1584. d.get_key_list(&keys);
  1585. Vector<_VariantStrPair> pairs;
  1586. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1587. _VariantStrPair sp;
  1588. sp.key = stringify_variant_clean(E->get(), recursion_count);
  1589. sp.value = stringify_variant_clean(d[E->get()], recursion_count);
  1590. pairs.push_back(sp);
  1591. }
  1592. for (int i = 0; i < pairs.size(); i++) {
  1593. if (i > 0) {
  1594. str += ", ";
  1595. }
  1596. str += pairs[i].key + ": " + pairs[i].value;
  1597. }
  1598. str += " }";
  1599. return str;
  1600. }
  1601. // Packed arrays cannot contain recursive structures, the recursion_count increment is not needed.
  1602. case PACKED_VECTOR2_ARRAY: {
  1603. return stringify_vector(operator PackedVector2Array(), recursion_count);
  1604. }
  1605. case PACKED_VECTOR3_ARRAY: {
  1606. return stringify_vector(operator PackedVector3Array(), recursion_count);
  1607. }
  1608. case PACKED_COLOR_ARRAY: {
  1609. return stringify_vector(operator PackedColorArray(), recursion_count);
  1610. }
  1611. case PACKED_VECTOR4_ARRAY: {
  1612. return stringify_vector(operator PackedVector4Array(), recursion_count);
  1613. }
  1614. case PACKED_STRING_ARRAY: {
  1615. return stringify_vector(operator PackedStringArray(), recursion_count);
  1616. }
  1617. case PACKED_BYTE_ARRAY: {
  1618. return stringify_vector(operator PackedByteArray(), recursion_count);
  1619. }
  1620. case PACKED_INT32_ARRAY: {
  1621. return stringify_vector(operator PackedInt32Array(), recursion_count);
  1622. }
  1623. case PACKED_INT64_ARRAY: {
  1624. return stringify_vector(operator PackedInt64Array(), recursion_count);
  1625. }
  1626. case PACKED_FLOAT32_ARRAY: {
  1627. return stringify_vector(operator PackedFloat32Array(), recursion_count);
  1628. }
  1629. case PACKED_FLOAT64_ARRAY: {
  1630. return stringify_vector(operator PackedFloat64Array(), recursion_count);
  1631. }
  1632. case ARRAY: {
  1633. ERR_FAIL_COND_V_MSG(recursion_count > MAX_RECURSION, "[...]", "Maximum array recursion reached!");
  1634. recursion_count++;
  1635. return stringify_vector(operator Array(), recursion_count);
  1636. }
  1637. case OBJECT: {
  1638. if (_get_obj().obj) {
  1639. if (!_get_obj().id.is_ref_counted() && ObjectDB::get_instance(_get_obj().id) == nullptr) {
  1640. return "<Freed Object>";
  1641. }
  1642. return _get_obj().obj->to_string();
  1643. } else {
  1644. return "<Object#null>";
  1645. }
  1646. }
  1647. case CALLABLE: {
  1648. const Callable &c = *reinterpret_cast<const Callable *>(_data._mem);
  1649. return c;
  1650. }
  1651. case SIGNAL: {
  1652. const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
  1653. return s;
  1654. }
  1655. case RID: {
  1656. const ::RID &s = *reinterpret_cast<const ::RID *>(_data._mem);
  1657. return "RID(" + itos(s.get_id()) + ")";
  1658. }
  1659. default: {
  1660. return "<" + get_type_name(type) + ">";
  1661. }
  1662. }
  1663. }
  1664. String Variant::to_json_string() const {
  1665. return JSON::stringify(*this);
  1666. }
  1667. Variant::operator Vector2() const {
  1668. if (type == VECTOR2) {
  1669. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1670. } else if (type == VECTOR2I) {
  1671. return *reinterpret_cast<const Vector2i *>(_data._mem);
  1672. } else if (type == VECTOR3) {
  1673. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1674. } else if (type == VECTOR3I) {
  1675. return Vector2(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y);
  1676. } else if (type == VECTOR4) {
  1677. return Vector2(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y);
  1678. } else if (type == VECTOR4I) {
  1679. return Vector2(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y);
  1680. } else {
  1681. return Vector2();
  1682. }
  1683. }
  1684. Variant::operator Vector2i() const {
  1685. if (type == VECTOR2I) {
  1686. return *reinterpret_cast<const Vector2i *>(_data._mem);
  1687. } else if (type == VECTOR2) {
  1688. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1689. } else if (type == VECTOR3) {
  1690. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1691. } else if (type == VECTOR3I) {
  1692. return Vector2(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y);
  1693. } else if (type == VECTOR4) {
  1694. return Vector2(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y);
  1695. } else if (type == VECTOR4I) {
  1696. return Vector2(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y);
  1697. } else {
  1698. return Vector2i();
  1699. }
  1700. }
  1701. Variant::operator Rect2() const {
  1702. if (type == RECT2) {
  1703. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1704. } else if (type == RECT2I) {
  1705. return *reinterpret_cast<const Rect2i *>(_data._mem);
  1706. } else {
  1707. return Rect2();
  1708. }
  1709. }
  1710. Variant::operator Rect2i() const {
  1711. if (type == RECT2I) {
  1712. return *reinterpret_cast<const Rect2i *>(_data._mem);
  1713. } else if (type == RECT2) {
  1714. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1715. } else {
  1716. return Rect2i();
  1717. }
  1718. }
  1719. Variant::operator Vector3() const {
  1720. if (type == VECTOR3) {
  1721. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1722. } else if (type == VECTOR3I) {
  1723. return *reinterpret_cast<const Vector3i *>(_data._mem);
  1724. } else if (type == VECTOR2) {
  1725. return Vector3(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1726. } else if (type == VECTOR2I) {
  1727. return Vector3(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0);
  1728. } else if (type == VECTOR4) {
  1729. return Vector3(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y, reinterpret_cast<const Vector4 *>(_data._mem)->z);
  1730. } else if (type == VECTOR4I) {
  1731. return Vector3(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y, reinterpret_cast<const Vector4i *>(_data._mem)->z);
  1732. } else {
  1733. return Vector3();
  1734. }
  1735. }
  1736. Variant::operator Vector3i() const {
  1737. if (type == VECTOR3I) {
  1738. return *reinterpret_cast<const Vector3i *>(_data._mem);
  1739. } else if (type == VECTOR3) {
  1740. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1741. } else if (type == VECTOR2) {
  1742. return Vector3i(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1743. } else if (type == VECTOR2I) {
  1744. return Vector3i(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0);
  1745. } else if (type == VECTOR4) {
  1746. return Vector3i(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y, reinterpret_cast<const Vector4 *>(_data._mem)->z);
  1747. } else if (type == VECTOR4I) {
  1748. return Vector3i(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y, reinterpret_cast<const Vector4i *>(_data._mem)->z);
  1749. } else {
  1750. return Vector3i();
  1751. }
  1752. }
  1753. Variant::operator Vector4() const {
  1754. if (type == VECTOR4) {
  1755. return *reinterpret_cast<const Vector4 *>(_data._mem);
  1756. } else if (type == VECTOR4I) {
  1757. return *reinterpret_cast<const Vector4i *>(_data._mem);
  1758. } else if (type == VECTOR2) {
  1759. return Vector4(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0, 0.0);
  1760. } else if (type == VECTOR2I) {
  1761. return Vector4(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0, 0.0);
  1762. } else if (type == VECTOR3) {
  1763. 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);
  1764. } else if (type == VECTOR3I) {
  1765. 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);
  1766. } else {
  1767. return Vector4();
  1768. }
  1769. }
  1770. Variant::operator Vector4i() const {
  1771. if (type == VECTOR4I) {
  1772. return *reinterpret_cast<const Vector4i *>(_data._mem);
  1773. } else if (type == VECTOR4) {
  1774. const Vector4 &v4 = *reinterpret_cast<const Vector4 *>(_data._mem);
  1775. return Vector4i(v4.x, v4.y, v4.z, v4.w);
  1776. } else if (type == VECTOR2) {
  1777. return Vector4i(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 Vector4i(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 Vector4i(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 Vector4i(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 Vector4i();
  1786. }
  1787. }
  1788. Variant::operator Plane() const {
  1789. if (type == PLANE) {
  1790. return *reinterpret_cast<const Plane *>(_data._mem);
  1791. } else {
  1792. return Plane();
  1793. }
  1794. }
  1795. Variant::operator ::AABB() const {
  1796. if (type == AABB) {
  1797. return *_data._aabb;
  1798. } else {
  1799. return ::AABB();
  1800. }
  1801. }
  1802. Variant::operator Basis() const {
  1803. if (type == BASIS) {
  1804. return *_data._basis;
  1805. } else if (type == QUATERNION) {
  1806. return *reinterpret_cast<const Quaternion *>(_data._mem);
  1807. } else if (type == TRANSFORM3D) { // unexposed in Variant::can_convert?
  1808. return _data._transform3d->basis;
  1809. } else {
  1810. return Basis();
  1811. }
  1812. }
  1813. Variant::operator Quaternion() const {
  1814. if (type == QUATERNION) {
  1815. return *reinterpret_cast<const Quaternion *>(_data._mem);
  1816. } else if (type == BASIS) {
  1817. return *_data._basis;
  1818. } else if (type == TRANSFORM3D) {
  1819. return _data._transform3d->basis;
  1820. } else {
  1821. return Quaternion();
  1822. }
  1823. }
  1824. Variant::operator Transform3D() const {
  1825. if (type == TRANSFORM3D) {
  1826. return *_data._transform3d;
  1827. } else if (type == BASIS) {
  1828. return Transform3D(*_data._basis, Vector3());
  1829. } else if (type == QUATERNION) {
  1830. return Transform3D(Basis(*reinterpret_cast<const Quaternion *>(_data._mem)), Vector3());
  1831. } else if (type == TRANSFORM2D) {
  1832. const Transform2D &t = *_data._transform2d;
  1833. Transform3D m;
  1834. m.basis.rows[0][0] = t.columns[0][0];
  1835. m.basis.rows[1][0] = t.columns[0][1];
  1836. m.basis.rows[0][1] = t.columns[1][0];
  1837. m.basis.rows[1][1] = t.columns[1][1];
  1838. m.origin[0] = t.columns[2][0];
  1839. m.origin[1] = t.columns[2][1];
  1840. return m;
  1841. } else if (type == PROJECTION) {
  1842. return *_data._projection;
  1843. } else {
  1844. return Transform3D();
  1845. }
  1846. }
  1847. Variant::operator Projection() const {
  1848. if (type == TRANSFORM3D) {
  1849. return *_data._transform3d;
  1850. } else if (type == BASIS) {
  1851. return Transform3D(*_data._basis, Vector3());
  1852. } else if (type == QUATERNION) {
  1853. return Transform3D(Basis(*reinterpret_cast<const Quaternion *>(_data._mem)), Vector3());
  1854. } else if (type == TRANSFORM2D) {
  1855. const Transform2D &t = *_data._transform2d;
  1856. Transform3D m;
  1857. m.basis.rows[0][0] = t.columns[0][0];
  1858. m.basis.rows[1][0] = t.columns[0][1];
  1859. m.basis.rows[0][1] = t.columns[1][0];
  1860. m.basis.rows[1][1] = t.columns[1][1];
  1861. m.origin[0] = t.columns[2][0];
  1862. m.origin[1] = t.columns[2][1];
  1863. return m;
  1864. } else if (type == PROJECTION) {
  1865. return *_data._projection;
  1866. } else {
  1867. return Projection();
  1868. }
  1869. }
  1870. Variant::operator Transform2D() const {
  1871. if (type == TRANSFORM2D) {
  1872. return *_data._transform2d;
  1873. } else if (type == TRANSFORM3D) {
  1874. const Transform3D &t = *_data._transform3d;
  1875. Transform2D m;
  1876. m.columns[0][0] = t.basis.rows[0][0];
  1877. m.columns[0][1] = t.basis.rows[1][0];
  1878. m.columns[1][0] = t.basis.rows[0][1];
  1879. m.columns[1][1] = t.basis.rows[1][1];
  1880. m.columns[2][0] = t.origin[0];
  1881. m.columns[2][1] = t.origin[1];
  1882. return m;
  1883. } else {
  1884. return Transform2D();
  1885. }
  1886. }
  1887. Variant::operator Color() const {
  1888. if (type == COLOR) {
  1889. return *reinterpret_cast<const Color *>(_data._mem);
  1890. } else if (type == STRING) {
  1891. return Color(operator String());
  1892. } else if (type == INT) {
  1893. return Color::hex(operator int());
  1894. } else {
  1895. return Color();
  1896. }
  1897. }
  1898. Variant::operator NodePath() const {
  1899. if (type == NODE_PATH) {
  1900. return *reinterpret_cast<const NodePath *>(_data._mem);
  1901. } else if (type == STRING) {
  1902. return NodePath(operator String());
  1903. } else {
  1904. return NodePath();
  1905. }
  1906. }
  1907. Variant::operator ::RID() const {
  1908. if (type == RID) {
  1909. return *reinterpret_cast<const ::RID *>(_data._mem);
  1910. } else if (type == OBJECT && _get_obj().obj == nullptr) {
  1911. return ::RID();
  1912. } else if (type == OBJECT && _get_obj().obj) {
  1913. #ifdef DEBUG_ENABLED
  1914. if (EngineDebugger::is_active()) {
  1915. ERR_FAIL_NULL_V_MSG(ObjectDB::get_instance(_get_obj().id), ::RID(), "Invalid pointer (object was freed).");
  1916. }
  1917. #endif
  1918. Callable::CallError ce;
  1919. Variant ret = _get_obj().obj->callp(CoreStringName(get_rid), nullptr, 0, ce);
  1920. if (ce.error == Callable::CallError::CALL_OK && ret.get_type() == Variant::RID) {
  1921. return ret;
  1922. }
  1923. return ::RID();
  1924. } else {
  1925. return ::RID();
  1926. }
  1927. }
  1928. Variant::operator Object *() const {
  1929. if (type == OBJECT) {
  1930. return _get_obj().obj;
  1931. } else {
  1932. return nullptr;
  1933. }
  1934. }
  1935. Object *Variant::get_validated_object_with_check(bool &r_previously_freed) const {
  1936. if (type == OBJECT) {
  1937. Object *instance = ObjectDB::get_instance(_get_obj().id);
  1938. r_previously_freed = !instance && _get_obj().id != ObjectID();
  1939. return instance;
  1940. } else {
  1941. r_previously_freed = false;
  1942. return nullptr;
  1943. }
  1944. }
  1945. Object *Variant::get_validated_object() const {
  1946. if (type == OBJECT) {
  1947. return ObjectDB::get_instance(_get_obj().id);
  1948. } else {
  1949. return nullptr;
  1950. }
  1951. }
  1952. Variant::operator Dictionary() const {
  1953. if (type == DICTIONARY) {
  1954. return *reinterpret_cast<const Dictionary *>(_data._mem);
  1955. } else {
  1956. return Dictionary();
  1957. }
  1958. }
  1959. Variant::operator Callable() const {
  1960. if (type == CALLABLE) {
  1961. return *reinterpret_cast<const Callable *>(_data._mem);
  1962. } else {
  1963. return Callable();
  1964. }
  1965. }
  1966. Variant::operator Signal() const {
  1967. if (type == SIGNAL) {
  1968. return *reinterpret_cast<const Signal *>(_data._mem);
  1969. } else {
  1970. return Signal();
  1971. }
  1972. }
  1973. template <typename DA, typename SA>
  1974. inline DA _convert_array(const SA &p_array) {
  1975. DA da;
  1976. da.resize(p_array.size());
  1977. for (int i = 0; i < p_array.size(); i++) {
  1978. da.set(i, Variant(p_array.get(i)));
  1979. }
  1980. return da;
  1981. }
  1982. template <typename DA>
  1983. inline DA _convert_array_from_variant(const Variant &p_variant) {
  1984. switch (p_variant.get_type()) {
  1985. case Variant::ARRAY: {
  1986. return _convert_array<DA, Array>(p_variant.operator Array());
  1987. }
  1988. case Variant::PACKED_BYTE_ARRAY: {
  1989. return _convert_array<DA, PackedByteArray>(p_variant.operator PackedByteArray());
  1990. }
  1991. case Variant::PACKED_INT32_ARRAY: {
  1992. return _convert_array<DA, PackedInt32Array>(p_variant.operator PackedInt32Array());
  1993. }
  1994. case Variant::PACKED_INT64_ARRAY: {
  1995. return _convert_array<DA, PackedInt64Array>(p_variant.operator PackedInt64Array());
  1996. }
  1997. case Variant::PACKED_FLOAT32_ARRAY: {
  1998. return _convert_array<DA, PackedFloat32Array>(p_variant.operator PackedFloat32Array());
  1999. }
  2000. case Variant::PACKED_FLOAT64_ARRAY: {
  2001. return _convert_array<DA, PackedFloat64Array>(p_variant.operator PackedFloat64Array());
  2002. }
  2003. case Variant::PACKED_STRING_ARRAY: {
  2004. return _convert_array<DA, PackedStringArray>(p_variant.operator PackedStringArray());
  2005. }
  2006. case Variant::PACKED_VECTOR2_ARRAY: {
  2007. return _convert_array<DA, PackedVector2Array>(p_variant.operator PackedVector2Array());
  2008. }
  2009. case Variant::PACKED_VECTOR3_ARRAY: {
  2010. return _convert_array<DA, PackedVector3Array>(p_variant.operator PackedVector3Array());
  2011. }
  2012. case Variant::PACKED_COLOR_ARRAY: {
  2013. return _convert_array<DA, PackedColorArray>(p_variant.operator PackedColorArray());
  2014. }
  2015. case Variant::PACKED_VECTOR4_ARRAY: {
  2016. return _convert_array<DA, PackedVector4Array>(p_variant.operator PackedVector4Array());
  2017. }
  2018. default: {
  2019. return DA();
  2020. }
  2021. }
  2022. }
  2023. Variant::operator Array() const {
  2024. if (type == ARRAY) {
  2025. return *reinterpret_cast<const Array *>(_data._mem);
  2026. } else {
  2027. return _convert_array_from_variant<Array>(*this);
  2028. }
  2029. }
  2030. Variant::operator PackedByteArray() const {
  2031. if (type == PACKED_BYTE_ARRAY) {
  2032. return static_cast<PackedArrayRef<uint8_t> *>(_data.packed_array)->array;
  2033. } else {
  2034. return _convert_array_from_variant<PackedByteArray>(*this);
  2035. }
  2036. }
  2037. Variant::operator PackedInt32Array() const {
  2038. if (type == PACKED_INT32_ARRAY) {
  2039. return static_cast<PackedArrayRef<int32_t> *>(_data.packed_array)->array;
  2040. } else {
  2041. return _convert_array_from_variant<PackedInt32Array>(*this);
  2042. }
  2043. }
  2044. Variant::operator PackedInt64Array() const {
  2045. if (type == PACKED_INT64_ARRAY) {
  2046. return static_cast<PackedArrayRef<int64_t> *>(_data.packed_array)->array;
  2047. } else {
  2048. return _convert_array_from_variant<PackedInt64Array>(*this);
  2049. }
  2050. }
  2051. Variant::operator PackedFloat32Array() const {
  2052. if (type == PACKED_FLOAT32_ARRAY) {
  2053. return static_cast<PackedArrayRef<float> *>(_data.packed_array)->array;
  2054. } else {
  2055. return _convert_array_from_variant<PackedFloat32Array>(*this);
  2056. }
  2057. }
  2058. Variant::operator PackedFloat64Array() const {
  2059. if (type == PACKED_FLOAT64_ARRAY) {
  2060. return static_cast<PackedArrayRef<double> *>(_data.packed_array)->array;
  2061. } else {
  2062. return _convert_array_from_variant<PackedFloat64Array>(*this);
  2063. }
  2064. }
  2065. Variant::operator PackedStringArray() const {
  2066. if (type == PACKED_STRING_ARRAY) {
  2067. return static_cast<PackedArrayRef<String> *>(_data.packed_array)->array;
  2068. } else {
  2069. return _convert_array_from_variant<PackedStringArray>(*this);
  2070. }
  2071. }
  2072. Variant::operator PackedVector2Array() const {
  2073. if (type == PACKED_VECTOR2_ARRAY) {
  2074. return static_cast<PackedArrayRef<Vector2> *>(_data.packed_array)->array;
  2075. } else {
  2076. return _convert_array_from_variant<PackedVector2Array>(*this);
  2077. }
  2078. }
  2079. Variant::operator PackedVector3Array() const {
  2080. if (type == PACKED_VECTOR3_ARRAY) {
  2081. return static_cast<PackedArrayRef<Vector3> *>(_data.packed_array)->array;
  2082. } else {
  2083. return _convert_array_from_variant<PackedVector3Array>(*this);
  2084. }
  2085. }
  2086. Variant::operator PackedColorArray() const {
  2087. if (type == PACKED_COLOR_ARRAY) {
  2088. return static_cast<PackedArrayRef<Color> *>(_data.packed_array)->array;
  2089. } else {
  2090. return _convert_array_from_variant<PackedColorArray>(*this);
  2091. }
  2092. }
  2093. Variant::operator PackedVector4Array() const {
  2094. if (type == PACKED_VECTOR4_ARRAY) {
  2095. return static_cast<PackedArrayRef<Vector4> *>(_data.packed_array)->array;
  2096. } else {
  2097. return _convert_array_from_variant<PackedVector4Array>(*this);
  2098. }
  2099. }
  2100. /* helpers */
  2101. Variant::operator Vector<::RID>() const {
  2102. Array va = operator Array();
  2103. Vector<::RID> rids;
  2104. rids.resize(va.size());
  2105. for (int i = 0; i < rids.size(); i++) {
  2106. rids.write[i] = va[i];
  2107. }
  2108. return rids;
  2109. }
  2110. Variant::operator Vector<Plane>() const {
  2111. Array va = operator Array();
  2112. Vector<Plane> planes;
  2113. int va_size = va.size();
  2114. if (va_size == 0) {
  2115. return planes;
  2116. }
  2117. planes.resize(va_size);
  2118. Plane *w = planes.ptrw();
  2119. for (int i = 0; i < va_size; i++) {
  2120. w[i] = va[i];
  2121. }
  2122. return planes;
  2123. }
  2124. Variant::operator Vector<Face3>() const {
  2125. PackedVector3Array va = operator PackedVector3Array();
  2126. Vector<Face3> faces;
  2127. int va_size = va.size();
  2128. if (va_size == 0) {
  2129. return faces;
  2130. }
  2131. faces.resize(va_size / 3);
  2132. Face3 *w = faces.ptrw();
  2133. const Vector3 *r = va.ptr();
  2134. for (int i = 0; i < va_size; i++) {
  2135. w[i / 3].vertex[i % 3] = r[i];
  2136. }
  2137. return faces;
  2138. }
  2139. Variant::operator Vector<Variant>() const {
  2140. Array va = operator Array();
  2141. Vector<Variant> variants;
  2142. int va_size = va.size();
  2143. if (va_size == 0) {
  2144. return variants;
  2145. }
  2146. variants.resize(va_size);
  2147. Variant *w = variants.ptrw();
  2148. for (int i = 0; i < va_size; i++) {
  2149. w[i] = va[i];
  2150. }
  2151. return variants;
  2152. }
  2153. Variant::operator Vector<StringName>() const {
  2154. PackedStringArray from = operator PackedStringArray();
  2155. Vector<StringName> to;
  2156. int len = from.size();
  2157. to.resize(len);
  2158. for (int i = 0; i < len; i++) {
  2159. to.write[i] = from[i];
  2160. }
  2161. return to;
  2162. }
  2163. Variant::operator Side() const {
  2164. return (Side) operator int();
  2165. }
  2166. Variant::operator Orientation() const {
  2167. return (Orientation) operator int();
  2168. }
  2169. Variant::operator IPAddress() const {
  2170. if (type == PACKED_FLOAT32_ARRAY || type == PACKED_INT32_ARRAY || type == PACKED_FLOAT64_ARRAY || type == PACKED_INT64_ARRAY || type == PACKED_BYTE_ARRAY) {
  2171. Vector<int> addr = operator Vector<int>();
  2172. if (addr.size() == 4) {
  2173. return IPAddress(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
  2174. }
  2175. }
  2176. return IPAddress(operator String());
  2177. }
  2178. Variant::Variant(bool p_bool) :
  2179. type(BOOL) {
  2180. _data._bool = p_bool;
  2181. }
  2182. Variant::Variant(int64_t p_int64) :
  2183. type(INT) {
  2184. _data._int = p_int64;
  2185. }
  2186. Variant::Variant(int32_t p_int32) :
  2187. type(INT) {
  2188. _data._int = p_int32;
  2189. }
  2190. Variant::Variant(int16_t p_int16) :
  2191. type(INT) {
  2192. _data._int = p_int16;
  2193. }
  2194. Variant::Variant(int8_t p_int8) :
  2195. type(INT) {
  2196. _data._int = p_int8;
  2197. }
  2198. Variant::Variant(uint64_t p_uint64) :
  2199. type(INT) {
  2200. _data._int = p_uint64;
  2201. }
  2202. Variant::Variant(uint32_t p_uint32) :
  2203. type(INT) {
  2204. _data._int = p_uint32;
  2205. }
  2206. Variant::Variant(uint16_t p_uint16) :
  2207. type(INT) {
  2208. _data._int = p_uint16;
  2209. }
  2210. Variant::Variant(uint8_t p_uint8) :
  2211. type(INT) {
  2212. _data._int = p_uint8;
  2213. }
  2214. Variant::Variant(float p_float) :
  2215. type(FLOAT) {
  2216. _data._float = p_float;
  2217. }
  2218. Variant::Variant(double p_double) :
  2219. type(FLOAT) {
  2220. _data._float = p_double;
  2221. }
  2222. Variant::Variant(const ObjectID &p_id) :
  2223. type(INT) {
  2224. _data._int = p_id;
  2225. }
  2226. Variant::Variant(const StringName &p_string) :
  2227. type(STRING_NAME) {
  2228. memnew_placement(_data._mem, StringName(p_string));
  2229. }
  2230. Variant::Variant(const String &p_string) :
  2231. type(STRING) {
  2232. memnew_placement(_data._mem, String(p_string));
  2233. }
  2234. Variant::Variant(const char *const p_cstring) :
  2235. type(STRING) {
  2236. memnew_placement(_data._mem, String((const char *)p_cstring));
  2237. }
  2238. Variant::Variant(const char32_t *p_wstring) :
  2239. type(STRING) {
  2240. memnew_placement(_data._mem, String(p_wstring));
  2241. }
  2242. Variant::Variant(const Vector3 &p_vector3) :
  2243. type(VECTOR3) {
  2244. memnew_placement(_data._mem, Vector3(p_vector3));
  2245. }
  2246. Variant::Variant(const Vector3i &p_vector3i) :
  2247. type(VECTOR3I) {
  2248. memnew_placement(_data._mem, Vector3i(p_vector3i));
  2249. }
  2250. Variant::Variant(const Vector4 &p_vector4) :
  2251. type(VECTOR4) {
  2252. memnew_placement(_data._mem, Vector4(p_vector4));
  2253. }
  2254. Variant::Variant(const Vector4i &p_vector4i) :
  2255. type(VECTOR4I) {
  2256. memnew_placement(_data._mem, Vector4i(p_vector4i));
  2257. }
  2258. Variant::Variant(const Vector2 &p_vector2) :
  2259. type(VECTOR2) {
  2260. memnew_placement(_data._mem, Vector2(p_vector2));
  2261. }
  2262. Variant::Variant(const Vector2i &p_vector2i) :
  2263. type(VECTOR2I) {
  2264. memnew_placement(_data._mem, Vector2i(p_vector2i));
  2265. }
  2266. Variant::Variant(const Rect2 &p_rect2) :
  2267. type(RECT2) {
  2268. memnew_placement(_data._mem, Rect2(p_rect2));
  2269. }
  2270. Variant::Variant(const Rect2i &p_rect2i) :
  2271. type(RECT2I) {
  2272. memnew_placement(_data._mem, Rect2i(p_rect2i));
  2273. }
  2274. Variant::Variant(const Plane &p_plane) :
  2275. type(PLANE) {
  2276. memnew_placement(_data._mem, Plane(p_plane));
  2277. }
  2278. Variant::Variant(const ::AABB &p_aabb) :
  2279. type(AABB) {
  2280. _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
  2281. memnew_placement(_data._aabb, ::AABB(p_aabb));
  2282. }
  2283. Variant::Variant(const Basis &p_matrix) :
  2284. type(BASIS) {
  2285. _data._basis = (Basis *)Pools::_bucket_medium.alloc();
  2286. memnew_placement(_data._basis, Basis(p_matrix));
  2287. }
  2288. Variant::Variant(const Quaternion &p_quaternion) :
  2289. type(QUATERNION) {
  2290. memnew_placement(_data._mem, Quaternion(p_quaternion));
  2291. }
  2292. Variant::Variant(const Transform3D &p_transform) :
  2293. type(TRANSFORM3D) {
  2294. _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
  2295. memnew_placement(_data._transform3d, Transform3D(p_transform));
  2296. }
  2297. Variant::Variant(const Projection &pp_projection) :
  2298. type(PROJECTION) {
  2299. _data._projection = (Projection *)Pools::_bucket_large.alloc();
  2300. memnew_placement(_data._projection, Projection(pp_projection));
  2301. }
  2302. Variant::Variant(const Transform2D &p_transform) :
  2303. type(TRANSFORM2D) {
  2304. _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
  2305. memnew_placement(_data._transform2d, Transform2D(p_transform));
  2306. }
  2307. Variant::Variant(const Color &p_color) :
  2308. type(COLOR) {
  2309. memnew_placement(_data._mem, Color(p_color));
  2310. }
  2311. Variant::Variant(const NodePath &p_node_path) :
  2312. type(NODE_PATH) {
  2313. memnew_placement(_data._mem, NodePath(p_node_path));
  2314. }
  2315. Variant::Variant(const ::RID &p_rid) :
  2316. type(RID) {
  2317. memnew_placement(_data._mem, ::RID(p_rid));
  2318. }
  2319. Variant::Variant(const Object *p_object) :
  2320. type(OBJECT) {
  2321. memnew_placement(_data._mem, ObjData);
  2322. if (p_object) {
  2323. if (p_object->is_ref_counted()) {
  2324. RefCounted *ref_counted = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_object));
  2325. if (!ref_counted->init_ref()) {
  2326. _get_obj().obj = nullptr;
  2327. _get_obj().id = ObjectID();
  2328. return;
  2329. }
  2330. }
  2331. _get_obj().obj = const_cast<Object *>(p_object);
  2332. _get_obj().id = p_object->get_instance_id();
  2333. } else {
  2334. _get_obj().obj = nullptr;
  2335. _get_obj().id = ObjectID();
  2336. }
  2337. }
  2338. Variant::Variant(const Callable &p_callable) :
  2339. type(CALLABLE) {
  2340. memnew_placement(_data._mem, Callable(p_callable));
  2341. }
  2342. Variant::Variant(const Signal &p_callable) :
  2343. type(SIGNAL) {
  2344. memnew_placement(_data._mem, Signal(p_callable));
  2345. }
  2346. Variant::Variant(const Dictionary &p_dictionary) :
  2347. type(DICTIONARY) {
  2348. memnew_placement(_data._mem, Dictionary(p_dictionary));
  2349. }
  2350. Variant::Variant(const Array &p_array) :
  2351. type(ARRAY) {
  2352. memnew_placement(_data._mem, Array(p_array));
  2353. }
  2354. Variant::Variant(const PackedByteArray &p_byte_array) :
  2355. type(PACKED_BYTE_ARRAY) {
  2356. _data.packed_array = PackedArrayRef<uint8_t>::create(p_byte_array);
  2357. }
  2358. Variant::Variant(const PackedInt32Array &p_int32_array) :
  2359. type(PACKED_INT32_ARRAY) {
  2360. _data.packed_array = PackedArrayRef<int32_t>::create(p_int32_array);
  2361. }
  2362. Variant::Variant(const PackedInt64Array &p_int64_array) :
  2363. type(PACKED_INT64_ARRAY) {
  2364. _data.packed_array = PackedArrayRef<int64_t>::create(p_int64_array);
  2365. }
  2366. Variant::Variant(const PackedFloat32Array &p_float32_array) :
  2367. type(PACKED_FLOAT32_ARRAY) {
  2368. _data.packed_array = PackedArrayRef<float>::create(p_float32_array);
  2369. }
  2370. Variant::Variant(const PackedFloat64Array &p_float64_array) :
  2371. type(PACKED_FLOAT64_ARRAY) {
  2372. _data.packed_array = PackedArrayRef<double>::create(p_float64_array);
  2373. }
  2374. Variant::Variant(const PackedStringArray &p_string_array) :
  2375. type(PACKED_STRING_ARRAY) {
  2376. _data.packed_array = PackedArrayRef<String>::create(p_string_array);
  2377. }
  2378. Variant::Variant(const PackedVector2Array &p_vector2_array) :
  2379. type(PACKED_VECTOR2_ARRAY) {
  2380. _data.packed_array = PackedArrayRef<Vector2>::create(p_vector2_array);
  2381. }
  2382. Variant::Variant(const PackedVector3Array &p_vector3_array) :
  2383. type(PACKED_VECTOR3_ARRAY) {
  2384. _data.packed_array = PackedArrayRef<Vector3>::create(p_vector3_array);
  2385. }
  2386. Variant::Variant(const PackedColorArray &p_color_array) :
  2387. type(PACKED_COLOR_ARRAY) {
  2388. _data.packed_array = PackedArrayRef<Color>::create(p_color_array);
  2389. }
  2390. Variant::Variant(const PackedVector4Array &p_vector4_array) :
  2391. type(PACKED_VECTOR4_ARRAY) {
  2392. _data.packed_array = PackedArrayRef<Vector4>::create(p_vector4_array);
  2393. }
  2394. /* helpers */
  2395. Variant::Variant(const Vector<::RID> &p_array) :
  2396. type(ARRAY) {
  2397. Array *rid_array = memnew_placement(_data._mem, Array);
  2398. rid_array->resize(p_array.size());
  2399. for (int i = 0; i < p_array.size(); i++) {
  2400. rid_array->set(i, Variant(p_array[i]));
  2401. }
  2402. }
  2403. Variant::Variant(const Vector<Plane> &p_array) :
  2404. type(ARRAY) {
  2405. Array *plane_array = memnew_placement(_data._mem, Array);
  2406. plane_array->resize(p_array.size());
  2407. for (int i = 0; i < p_array.size(); i++) {
  2408. plane_array->operator[](i) = Variant(p_array[i]);
  2409. }
  2410. }
  2411. Variant::Variant(const Vector<Face3> &p_face_array) :
  2412. type(NIL) {
  2413. PackedVector3Array vertices;
  2414. int face_count = p_face_array.size();
  2415. vertices.resize(face_count * 3);
  2416. if (face_count) {
  2417. const Face3 *r = p_face_array.ptr();
  2418. Vector3 *w = vertices.ptrw();
  2419. for (int i = 0; i < face_count; i++) {
  2420. for (int j = 0; j < 3; j++) {
  2421. w[i * 3 + j] = r[i].vertex[j];
  2422. }
  2423. }
  2424. }
  2425. *this = vertices;
  2426. }
  2427. Variant::Variant(const Vector<Variant> &p_array) :
  2428. type(NIL) {
  2429. Array arr;
  2430. arr.resize(p_array.size());
  2431. for (int i = 0; i < p_array.size(); i++) {
  2432. arr[i] = p_array[i];
  2433. }
  2434. *this = arr;
  2435. }
  2436. Variant::Variant(const Vector<StringName> &p_array) :
  2437. type(NIL) {
  2438. PackedStringArray v;
  2439. int len = p_array.size();
  2440. v.resize(len);
  2441. for (int i = 0; i < len; i++) {
  2442. v.set(i, p_array[i]);
  2443. }
  2444. *this = v;
  2445. }
  2446. void Variant::operator=(const Variant &p_variant) {
  2447. if (unlikely(this == &p_variant)) {
  2448. return;
  2449. }
  2450. if (unlikely(type != p_variant.type)) {
  2451. reference(p_variant);
  2452. return;
  2453. }
  2454. switch (p_variant.type) {
  2455. case NIL: {
  2456. // none
  2457. } break;
  2458. // atomic types
  2459. case BOOL: {
  2460. _data._bool = p_variant._data._bool;
  2461. } break;
  2462. case INT: {
  2463. _data._int = p_variant._data._int;
  2464. } break;
  2465. case FLOAT: {
  2466. _data._float = p_variant._data._float;
  2467. } break;
  2468. case STRING: {
  2469. *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
  2470. } break;
  2471. // math types
  2472. case VECTOR2: {
  2473. *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2474. } break;
  2475. case VECTOR2I: {
  2476. *reinterpret_cast<Vector2i *>(_data._mem) = *reinterpret_cast<const Vector2i *>(p_variant._data._mem);
  2477. } break;
  2478. case RECT2: {
  2479. *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2480. } break;
  2481. case RECT2I: {
  2482. *reinterpret_cast<Rect2i *>(_data._mem) = *reinterpret_cast<const Rect2i *>(p_variant._data._mem);
  2483. } break;
  2484. case TRANSFORM2D: {
  2485. *_data._transform2d = *(p_variant._data._transform2d);
  2486. } break;
  2487. case VECTOR3: {
  2488. *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2489. } break;
  2490. case VECTOR3I: {
  2491. *reinterpret_cast<Vector3i *>(_data._mem) = *reinterpret_cast<const Vector3i *>(p_variant._data._mem);
  2492. } break;
  2493. case VECTOR4: {
  2494. *reinterpret_cast<Vector4 *>(_data._mem) = *reinterpret_cast<const Vector4 *>(p_variant._data._mem);
  2495. } break;
  2496. case VECTOR4I: {
  2497. *reinterpret_cast<Vector4i *>(_data._mem) = *reinterpret_cast<const Vector4i *>(p_variant._data._mem);
  2498. } break;
  2499. case PLANE: {
  2500. *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
  2501. } break;
  2502. case AABB: {
  2503. *_data._aabb = *(p_variant._data._aabb);
  2504. } break;
  2505. case QUATERNION: {
  2506. *reinterpret_cast<Quaternion *>(_data._mem) = *reinterpret_cast<const Quaternion *>(p_variant._data._mem);
  2507. } break;
  2508. case BASIS: {
  2509. *_data._basis = *(p_variant._data._basis);
  2510. } break;
  2511. case TRANSFORM3D: {
  2512. *_data._transform3d = *(p_variant._data._transform3d);
  2513. } break;
  2514. case PROJECTION: {
  2515. *_data._projection = *(p_variant._data._projection);
  2516. } break;
  2517. // misc types
  2518. case COLOR: {
  2519. *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
  2520. } break;
  2521. case RID: {
  2522. *reinterpret_cast<::RID *>(_data._mem) = *reinterpret_cast<const ::RID *>(p_variant._data._mem);
  2523. } break;
  2524. case OBJECT: {
  2525. if (_get_obj().id.is_ref_counted()) {
  2526. //we are safe that there is a reference here
  2527. RefCounted *ref_counted = static_cast<RefCounted *>(_get_obj().obj);
  2528. if (ref_counted->unreference()) {
  2529. memdelete(ref_counted);
  2530. }
  2531. }
  2532. if (p_variant._get_obj().obj && p_variant._get_obj().id.is_ref_counted()) {
  2533. RefCounted *ref_counted = static_cast<RefCounted *>(p_variant._get_obj().obj);
  2534. if (!ref_counted->reference()) {
  2535. _get_obj().obj = nullptr;
  2536. _get_obj().id = ObjectID();
  2537. break;
  2538. }
  2539. }
  2540. _get_obj().obj = const_cast<Object *>(p_variant._get_obj().obj);
  2541. _get_obj().id = p_variant._get_obj().id;
  2542. } break;
  2543. case CALLABLE: {
  2544. *reinterpret_cast<Callable *>(_data._mem) = *reinterpret_cast<const Callable *>(p_variant._data._mem);
  2545. } break;
  2546. case SIGNAL: {
  2547. *reinterpret_cast<Signal *>(_data._mem) = *reinterpret_cast<const Signal *>(p_variant._data._mem);
  2548. } break;
  2549. case STRING_NAME: {
  2550. *reinterpret_cast<StringName *>(_data._mem) = *reinterpret_cast<const StringName *>(p_variant._data._mem);
  2551. } break;
  2552. case NODE_PATH: {
  2553. *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
  2554. } break;
  2555. case DICTIONARY: {
  2556. *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
  2557. } break;
  2558. case ARRAY: {
  2559. *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
  2560. } break;
  2561. // arrays
  2562. case PACKED_BYTE_ARRAY: {
  2563. _data.packed_array = PackedArrayRef<uint8_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2564. } break;
  2565. case PACKED_INT32_ARRAY: {
  2566. _data.packed_array = PackedArrayRef<int32_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2567. } break;
  2568. case PACKED_INT64_ARRAY: {
  2569. _data.packed_array = PackedArrayRef<int64_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2570. } break;
  2571. case PACKED_FLOAT32_ARRAY: {
  2572. _data.packed_array = PackedArrayRef<float>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2573. } break;
  2574. case PACKED_FLOAT64_ARRAY: {
  2575. _data.packed_array = PackedArrayRef<double>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2576. } break;
  2577. case PACKED_STRING_ARRAY: {
  2578. _data.packed_array = PackedArrayRef<String>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2579. } break;
  2580. case PACKED_VECTOR2_ARRAY: {
  2581. _data.packed_array = PackedArrayRef<Vector2>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2582. } break;
  2583. case PACKED_VECTOR3_ARRAY: {
  2584. _data.packed_array = PackedArrayRef<Vector3>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2585. } break;
  2586. case PACKED_COLOR_ARRAY: {
  2587. _data.packed_array = PackedArrayRef<Color>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2588. } break;
  2589. case PACKED_VECTOR4_ARRAY: {
  2590. _data.packed_array = PackedArrayRef<Vector4>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2591. } break;
  2592. default: {
  2593. }
  2594. }
  2595. }
  2596. Variant::Variant(const IPAddress &p_address) :
  2597. type(STRING) {
  2598. memnew_placement(_data._mem, String(p_address));
  2599. }
  2600. Variant::Variant(const Variant &p_variant) :
  2601. type(NIL) {
  2602. reference(p_variant);
  2603. }
  2604. uint32_t Variant::hash() const {
  2605. return recursive_hash(0);
  2606. }
  2607. uint32_t Variant::recursive_hash(int recursion_count) const {
  2608. switch (type) {
  2609. case NIL: {
  2610. return 0;
  2611. } break;
  2612. case BOOL: {
  2613. return _data._bool ? 1 : 0;
  2614. } break;
  2615. case INT: {
  2616. return hash_one_uint64((uint64_t)_data._int);
  2617. } break;
  2618. case FLOAT: {
  2619. return hash_murmur3_one_double(_data._float);
  2620. } break;
  2621. case STRING: {
  2622. return reinterpret_cast<const String *>(_data._mem)->hash();
  2623. } break;
  2624. // math types
  2625. case VECTOR2: {
  2626. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2 *>(_data._mem));
  2627. } break;
  2628. case VECTOR2I: {
  2629. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2i *>(_data._mem));
  2630. } break;
  2631. case RECT2: {
  2632. return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2 *>(_data._mem));
  2633. } break;
  2634. case RECT2I: {
  2635. return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2i *>(_data._mem));
  2636. } break;
  2637. case TRANSFORM2D: {
  2638. uint32_t h = HASH_MURMUR3_SEED;
  2639. const Transform2D &t = *_data._transform2d;
  2640. h = hash_murmur3_one_real(t[0].x, h);
  2641. h = hash_murmur3_one_real(t[0].y, h);
  2642. h = hash_murmur3_one_real(t[1].x, h);
  2643. h = hash_murmur3_one_real(t[1].y, h);
  2644. h = hash_murmur3_one_real(t[2].x, h);
  2645. h = hash_murmur3_one_real(t[2].y, h);
  2646. return hash_fmix32(h);
  2647. } break;
  2648. case VECTOR3: {
  2649. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3 *>(_data._mem));
  2650. } break;
  2651. case VECTOR3I: {
  2652. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3i *>(_data._mem));
  2653. } break;
  2654. case VECTOR4: {
  2655. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4 *>(_data._mem));
  2656. } break;
  2657. case VECTOR4I: {
  2658. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4i *>(_data._mem));
  2659. } break;
  2660. case PLANE: {
  2661. uint32_t h = HASH_MURMUR3_SEED;
  2662. const Plane &p = *reinterpret_cast<const Plane *>(_data._mem);
  2663. h = hash_murmur3_one_real(p.normal.x, h);
  2664. h = hash_murmur3_one_real(p.normal.y, h);
  2665. h = hash_murmur3_one_real(p.normal.z, h);
  2666. h = hash_murmur3_one_real(p.d, h);
  2667. return hash_fmix32(h);
  2668. } break;
  2669. case AABB: {
  2670. return HashMapHasherDefault::hash(*_data._aabb);
  2671. } break;
  2672. case QUATERNION: {
  2673. uint32_t h = HASH_MURMUR3_SEED;
  2674. const Quaternion &q = *reinterpret_cast<const Quaternion *>(_data._mem);
  2675. h = hash_murmur3_one_real(q.x, h);
  2676. h = hash_murmur3_one_real(q.y, h);
  2677. h = hash_murmur3_one_real(q.z, h);
  2678. h = hash_murmur3_one_real(q.w, h);
  2679. return hash_fmix32(h);
  2680. } break;
  2681. case BASIS: {
  2682. uint32_t h = HASH_MURMUR3_SEED;
  2683. const Basis &b = *_data._basis;
  2684. h = hash_murmur3_one_real(b[0].x, h);
  2685. h = hash_murmur3_one_real(b[0].y, h);
  2686. h = hash_murmur3_one_real(b[0].z, h);
  2687. h = hash_murmur3_one_real(b[1].x, h);
  2688. h = hash_murmur3_one_real(b[1].y, h);
  2689. h = hash_murmur3_one_real(b[1].z, h);
  2690. h = hash_murmur3_one_real(b[2].x, h);
  2691. h = hash_murmur3_one_real(b[2].y, h);
  2692. h = hash_murmur3_one_real(b[2].z, h);
  2693. return hash_fmix32(h);
  2694. } break;
  2695. case TRANSFORM3D: {
  2696. uint32_t h = HASH_MURMUR3_SEED;
  2697. const Transform3D &t = *_data._transform3d;
  2698. h = hash_murmur3_one_real(t.basis[0].x, h);
  2699. h = hash_murmur3_one_real(t.basis[0].y, h);
  2700. h = hash_murmur3_one_real(t.basis[0].z, h);
  2701. h = hash_murmur3_one_real(t.basis[1].x, h);
  2702. h = hash_murmur3_one_real(t.basis[1].y, h);
  2703. h = hash_murmur3_one_real(t.basis[1].z, h);
  2704. h = hash_murmur3_one_real(t.basis[2].x, h);
  2705. h = hash_murmur3_one_real(t.basis[2].y, h);
  2706. h = hash_murmur3_one_real(t.basis[2].z, h);
  2707. h = hash_murmur3_one_real(t.origin.x, h);
  2708. h = hash_murmur3_one_real(t.origin.y, h);
  2709. h = hash_murmur3_one_real(t.origin.z, h);
  2710. return hash_fmix32(h);
  2711. } break;
  2712. case PROJECTION: {
  2713. uint32_t h = HASH_MURMUR3_SEED;
  2714. const Projection &t = *_data._projection;
  2715. h = hash_murmur3_one_real(t.columns[0].x, h);
  2716. h = hash_murmur3_one_real(t.columns[0].y, h);
  2717. h = hash_murmur3_one_real(t.columns[0].z, h);
  2718. h = hash_murmur3_one_real(t.columns[0].w, h);
  2719. h = hash_murmur3_one_real(t.columns[1].x, h);
  2720. h = hash_murmur3_one_real(t.columns[1].y, h);
  2721. h = hash_murmur3_one_real(t.columns[1].z, h);
  2722. h = hash_murmur3_one_real(t.columns[1].w, h);
  2723. h = hash_murmur3_one_real(t.columns[2].x, h);
  2724. h = hash_murmur3_one_real(t.columns[2].y, h);
  2725. h = hash_murmur3_one_real(t.columns[2].z, h);
  2726. h = hash_murmur3_one_real(t.columns[2].w, h);
  2727. h = hash_murmur3_one_real(t.columns[3].x, h);
  2728. h = hash_murmur3_one_real(t.columns[3].y, h);
  2729. h = hash_murmur3_one_real(t.columns[3].z, h);
  2730. h = hash_murmur3_one_real(t.columns[3].w, h);
  2731. return hash_fmix32(h);
  2732. } break;
  2733. // misc types
  2734. case COLOR: {
  2735. uint32_t h = HASH_MURMUR3_SEED;
  2736. const Color &c = *reinterpret_cast<const Color *>(_data._mem);
  2737. h = hash_murmur3_one_float(c.r, h);
  2738. h = hash_murmur3_one_float(c.g, h);
  2739. h = hash_murmur3_one_float(c.b, h);
  2740. h = hash_murmur3_one_float(c.a, h);
  2741. return hash_fmix32(h);
  2742. } break;
  2743. case RID: {
  2744. return hash_one_uint64(reinterpret_cast<const ::RID *>(_data._mem)->get_id());
  2745. } break;
  2746. case OBJECT: {
  2747. return hash_one_uint64(hash_make_uint64_t(_get_obj().obj));
  2748. } break;
  2749. case STRING_NAME: {
  2750. return reinterpret_cast<const StringName *>(_data._mem)->hash();
  2751. } break;
  2752. case NODE_PATH: {
  2753. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2754. } break;
  2755. case DICTIONARY: {
  2756. return reinterpret_cast<const Dictionary *>(_data._mem)->recursive_hash(recursion_count);
  2757. } break;
  2758. case CALLABLE: {
  2759. return reinterpret_cast<const Callable *>(_data._mem)->hash();
  2760. } break;
  2761. case SIGNAL: {
  2762. const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
  2763. uint32_t hash = s.get_name().hash();
  2764. return hash_murmur3_one_64(s.get_object_id(), hash);
  2765. } break;
  2766. case ARRAY: {
  2767. const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
  2768. return arr.recursive_hash(recursion_count);
  2769. } break;
  2770. case PACKED_BYTE_ARRAY: {
  2771. const PackedByteArray &arr = PackedArrayRef<uint8_t>::get_array(_data.packed_array);
  2772. int len = arr.size();
  2773. if (likely(len)) {
  2774. const uint8_t *r = arr.ptr();
  2775. return hash_murmur3_buffer((uint8_t *)&r[0], len);
  2776. } else {
  2777. return hash_murmur3_one_64(0);
  2778. }
  2779. } break;
  2780. case PACKED_INT32_ARRAY: {
  2781. const PackedInt32Array &arr = PackedArrayRef<int32_t>::get_array(_data.packed_array);
  2782. int len = arr.size();
  2783. if (likely(len)) {
  2784. const int32_t *r = arr.ptr();
  2785. return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int32_t));
  2786. } else {
  2787. return hash_murmur3_one_64(0);
  2788. }
  2789. } break;
  2790. case PACKED_INT64_ARRAY: {
  2791. const PackedInt64Array &arr = PackedArrayRef<int64_t>::get_array(_data.packed_array);
  2792. int len = arr.size();
  2793. if (likely(len)) {
  2794. const int64_t *r = arr.ptr();
  2795. return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int64_t));
  2796. } else {
  2797. return hash_murmur3_one_64(0);
  2798. }
  2799. } break;
  2800. case PACKED_FLOAT32_ARRAY: {
  2801. const PackedFloat32Array &arr = PackedArrayRef<float>::get_array(_data.packed_array);
  2802. int len = arr.size();
  2803. if (likely(len)) {
  2804. const float *r = arr.ptr();
  2805. uint32_t h = HASH_MURMUR3_SEED;
  2806. for (int32_t i = 0; i < len; i++) {
  2807. h = hash_murmur3_one_float(r[i], h);
  2808. }
  2809. return hash_fmix32(h);
  2810. } else {
  2811. return hash_murmur3_one_float(0.0);
  2812. }
  2813. } break;
  2814. case PACKED_FLOAT64_ARRAY: {
  2815. const PackedFloat64Array &arr = PackedArrayRef<double>::get_array(_data.packed_array);
  2816. int len = arr.size();
  2817. if (likely(len)) {
  2818. const double *r = arr.ptr();
  2819. uint32_t h = HASH_MURMUR3_SEED;
  2820. for (int32_t i = 0; i < len; i++) {
  2821. h = hash_murmur3_one_double(r[i], h);
  2822. }
  2823. return hash_fmix32(h);
  2824. } else {
  2825. return hash_murmur3_one_double(0.0);
  2826. }
  2827. } break;
  2828. case PACKED_STRING_ARRAY: {
  2829. uint32_t hash = HASH_MURMUR3_SEED;
  2830. const PackedStringArray &arr = PackedArrayRef<String>::get_array(_data.packed_array);
  2831. int len = arr.size();
  2832. if (likely(len)) {
  2833. const String *r = arr.ptr();
  2834. for (int i = 0; i < len; i++) {
  2835. hash = hash_murmur3_one_32(r[i].hash(), hash);
  2836. }
  2837. hash = hash_fmix32(hash);
  2838. }
  2839. return hash;
  2840. } break;
  2841. case PACKED_VECTOR2_ARRAY: {
  2842. uint32_t hash = HASH_MURMUR3_SEED;
  2843. const PackedVector2Array &arr = PackedArrayRef<Vector2>::get_array(_data.packed_array);
  2844. int len = arr.size();
  2845. if (likely(len)) {
  2846. const Vector2 *r = arr.ptr();
  2847. for (int i = 0; i < len; i++) {
  2848. hash = hash_murmur3_one_real(r[i].x, hash);
  2849. hash = hash_murmur3_one_real(r[i].y, hash);
  2850. }
  2851. hash = hash_fmix32(hash);
  2852. }
  2853. return hash;
  2854. } break;
  2855. case PACKED_VECTOR3_ARRAY: {
  2856. uint32_t hash = HASH_MURMUR3_SEED;
  2857. const PackedVector3Array &arr = PackedArrayRef<Vector3>::get_array(_data.packed_array);
  2858. int len = arr.size();
  2859. if (likely(len)) {
  2860. const Vector3 *r = arr.ptr();
  2861. for (int i = 0; i < len; i++) {
  2862. hash = hash_murmur3_one_real(r[i].x, hash);
  2863. hash = hash_murmur3_one_real(r[i].y, hash);
  2864. hash = hash_murmur3_one_real(r[i].z, hash);
  2865. }
  2866. hash = hash_fmix32(hash);
  2867. }
  2868. return hash;
  2869. } break;
  2870. case PACKED_COLOR_ARRAY: {
  2871. uint32_t hash = HASH_MURMUR3_SEED;
  2872. const PackedColorArray &arr = PackedArrayRef<Color>::get_array(_data.packed_array);
  2873. int len = arr.size();
  2874. if (likely(len)) {
  2875. const Color *r = arr.ptr();
  2876. for (int i = 0; i < len; i++) {
  2877. hash = hash_murmur3_one_float(r[i].r, hash);
  2878. hash = hash_murmur3_one_float(r[i].g, hash);
  2879. hash = hash_murmur3_one_float(r[i].b, hash);
  2880. hash = hash_murmur3_one_float(r[i].a, hash);
  2881. }
  2882. hash = hash_fmix32(hash);
  2883. }
  2884. return hash;
  2885. } break;
  2886. case PACKED_VECTOR4_ARRAY: {
  2887. uint32_t hash = HASH_MURMUR3_SEED;
  2888. const PackedVector4Array &arr = PackedArrayRef<Vector4>::get_array(_data.packed_array);
  2889. int len = arr.size();
  2890. if (likely(len)) {
  2891. const Vector4 *r = arr.ptr();
  2892. for (int i = 0; i < len; i++) {
  2893. hash = hash_murmur3_one_real(r[i].x, hash);
  2894. hash = hash_murmur3_one_real(r[i].y, hash);
  2895. hash = hash_murmur3_one_real(r[i].z, hash);
  2896. hash = hash_murmur3_one_real(r[i].w, hash);
  2897. }
  2898. hash = hash_fmix32(hash);
  2899. }
  2900. return hash;
  2901. } break;
  2902. default: {
  2903. }
  2904. }
  2905. return 0;
  2906. }
  2907. #define hash_compare_scalar_base(p_lhs, p_rhs, semantic_comparison) \
  2908. (((p_lhs) == (p_rhs)) || (semantic_comparison && Math::is_nan(p_lhs) && Math::is_nan(p_rhs)))
  2909. #define hash_compare_scalar(p_lhs, p_rhs) \
  2910. (hash_compare_scalar_base(p_lhs, p_rhs, true))
  2911. #define hash_compare_vector2(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. #define hash_compare_vector3(p_lhs, p_rhs) \
  2915. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2916. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2917. hash_compare_scalar((p_lhs).z, (p_rhs).z))
  2918. #define hash_compare_vector4(p_lhs, p_rhs) \
  2919. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2920. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2921. hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
  2922. hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2923. #define hash_compare_quaternion(p_lhs, p_rhs) \
  2924. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2925. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2926. hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
  2927. hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2928. #define hash_compare_color(p_lhs, p_rhs) \
  2929. (hash_compare_scalar((p_lhs).r, (p_rhs).r) && \
  2930. hash_compare_scalar((p_lhs).g, (p_rhs).g) && \
  2931. hash_compare_scalar((p_lhs).b, (p_rhs).b) && \
  2932. hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2933. #define hash_compare_packed_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2934. const Vector<p_type> &l = PackedArrayRef<p_type>::get_array(p_lhs); \
  2935. const Vector<p_type> &r = PackedArrayRef<p_type>::get_array(p_rhs); \
  2936. \
  2937. if (l.size() != r.size()) \
  2938. return false; \
  2939. \
  2940. const p_type *lr = l.ptr(); \
  2941. const p_type *rr = r.ptr(); \
  2942. \
  2943. for (int i = 0; i < l.size(); ++i) { \
  2944. if (!p_compare_func((lr[i]), (rr[i]))) \
  2945. return false; \
  2946. } \
  2947. \
  2948. return true
  2949. bool Variant::hash_compare(const Variant &p_variant, int recursion_count, bool semantic_comparison) const {
  2950. if (type != p_variant.type) {
  2951. return false;
  2952. }
  2953. switch (type) {
  2954. case INT: {
  2955. return _data._int == p_variant._data._int;
  2956. } break;
  2957. case FLOAT: {
  2958. return hash_compare_scalar_base(_data._float, p_variant._data._float, semantic_comparison);
  2959. } break;
  2960. case STRING: {
  2961. return *reinterpret_cast<const String *>(_data._mem) == *reinterpret_cast<const String *>(p_variant._data._mem);
  2962. } break;
  2963. case STRING_NAME: {
  2964. return *reinterpret_cast<const StringName *>(_data._mem) == *reinterpret_cast<const StringName *>(p_variant._data._mem);
  2965. } break;
  2966. case VECTOR2: {
  2967. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2968. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2969. return hash_compare_vector2(*l, *r);
  2970. } break;
  2971. case VECTOR2I: {
  2972. const Vector2i *l = reinterpret_cast<const Vector2i *>(_data._mem);
  2973. const Vector2i *r = reinterpret_cast<const Vector2i *>(p_variant._data._mem);
  2974. return *l == *r;
  2975. } break;
  2976. case RECT2: {
  2977. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2978. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2979. return hash_compare_vector2(l->position, r->position) &&
  2980. hash_compare_vector2(l->size, r->size);
  2981. } break;
  2982. case RECT2I: {
  2983. const Rect2i *l = reinterpret_cast<const Rect2i *>(_data._mem);
  2984. const Rect2i *r = reinterpret_cast<const Rect2i *>(p_variant._data._mem);
  2985. return *l == *r;
  2986. } break;
  2987. case TRANSFORM2D: {
  2988. Transform2D *l = _data._transform2d;
  2989. Transform2D *r = p_variant._data._transform2d;
  2990. for (int i = 0; i < 3; i++) {
  2991. if (!hash_compare_vector2(l->columns[i], r->columns[i])) {
  2992. return false;
  2993. }
  2994. }
  2995. return true;
  2996. } break;
  2997. case VECTOR3: {
  2998. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2999. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  3000. return hash_compare_vector3(*l, *r);
  3001. } break;
  3002. case VECTOR3I: {
  3003. const Vector3i *l = reinterpret_cast<const Vector3i *>(_data._mem);
  3004. const Vector3i *r = reinterpret_cast<const Vector3i *>(p_variant._data._mem);
  3005. return *l == *r;
  3006. } break;
  3007. case VECTOR4: {
  3008. const Vector4 *l = reinterpret_cast<const Vector4 *>(_data._mem);
  3009. const Vector4 *r = reinterpret_cast<const Vector4 *>(p_variant._data._mem);
  3010. return hash_compare_vector4(*l, *r);
  3011. } break;
  3012. case VECTOR4I: {
  3013. const Vector4i *l = reinterpret_cast<const Vector4i *>(_data._mem);
  3014. const Vector4i *r = reinterpret_cast<const Vector4i *>(p_variant._data._mem);
  3015. return *l == *r;
  3016. } break;
  3017. case PLANE: {
  3018. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  3019. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  3020. return hash_compare_vector3(l->normal, r->normal) &&
  3021. hash_compare_scalar(l->d, r->d);
  3022. } break;
  3023. case AABB: {
  3024. const ::AABB *l = _data._aabb;
  3025. const ::AABB *r = p_variant._data._aabb;
  3026. return hash_compare_vector3(l->position, r->position) &&
  3027. hash_compare_vector3(l->size, r->size);
  3028. } break;
  3029. case QUATERNION: {
  3030. const Quaternion *l = reinterpret_cast<const Quaternion *>(_data._mem);
  3031. const Quaternion *r = reinterpret_cast<const Quaternion *>(p_variant._data._mem);
  3032. return hash_compare_quaternion(*l, *r);
  3033. } break;
  3034. case BASIS: {
  3035. const Basis *l = _data._basis;
  3036. const Basis *r = p_variant._data._basis;
  3037. for (int i = 0; i < 3; i++) {
  3038. if (!hash_compare_vector3(l->rows[i], r->rows[i])) {
  3039. return false;
  3040. }
  3041. }
  3042. return true;
  3043. } break;
  3044. case TRANSFORM3D: {
  3045. const Transform3D *l = _data._transform3d;
  3046. const Transform3D *r = p_variant._data._transform3d;
  3047. for (int i = 0; i < 3; i++) {
  3048. if (!hash_compare_vector3(l->basis.rows[i], r->basis.rows[i])) {
  3049. return false;
  3050. }
  3051. }
  3052. return hash_compare_vector3(l->origin, r->origin);
  3053. } break;
  3054. case PROJECTION: {
  3055. const Projection *l = _data._projection;
  3056. const Projection *r = p_variant._data._projection;
  3057. for (int i = 0; i < 4; i++) {
  3058. if (!hash_compare_vector4(l->columns[i], r->columns[i])) {
  3059. return false;
  3060. }
  3061. }
  3062. return true;
  3063. } break;
  3064. case COLOR: {
  3065. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  3066. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  3067. return hash_compare_color(*l, *r);
  3068. } break;
  3069. case ARRAY: {
  3070. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  3071. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  3072. if (!l.recursive_equal(r, recursion_count + 1)) {
  3073. return false;
  3074. }
  3075. return true;
  3076. } break;
  3077. case DICTIONARY: {
  3078. const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
  3079. const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
  3080. if (!l.recursive_equal(r, recursion_count + 1)) {
  3081. return false;
  3082. }
  3083. return true;
  3084. } break;
  3085. // This is for floating point comparisons only.
  3086. case PACKED_FLOAT32_ARRAY: {
  3087. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, float, hash_compare_scalar);
  3088. } break;
  3089. case PACKED_FLOAT64_ARRAY: {
  3090. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, double, hash_compare_scalar);
  3091. } break;
  3092. case PACKED_VECTOR2_ARRAY: {
  3093. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector2, hash_compare_vector2);
  3094. } break;
  3095. case PACKED_VECTOR3_ARRAY: {
  3096. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector3, hash_compare_vector3);
  3097. } break;
  3098. case PACKED_COLOR_ARRAY: {
  3099. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Color, hash_compare_color);
  3100. } break;
  3101. case PACKED_VECTOR4_ARRAY: {
  3102. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector4, hash_compare_vector4);
  3103. } break;
  3104. default:
  3105. bool v;
  3106. Variant r;
  3107. evaluate(OP_EQUAL, *this, p_variant, r, v);
  3108. return r;
  3109. }
  3110. }
  3111. bool Variant::identity_compare(const Variant &p_variant) const {
  3112. if (type != p_variant.type) {
  3113. return false;
  3114. }
  3115. switch (type) {
  3116. case OBJECT: {
  3117. return _get_obj().id == p_variant._get_obj().id;
  3118. } break;
  3119. case DICTIONARY: {
  3120. const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
  3121. const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
  3122. return l.id() == r.id();
  3123. } break;
  3124. case ARRAY: {
  3125. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  3126. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  3127. return l.id() == r.id();
  3128. } break;
  3129. case PACKED_BYTE_ARRAY:
  3130. case PACKED_INT32_ARRAY:
  3131. case PACKED_INT64_ARRAY:
  3132. case PACKED_FLOAT32_ARRAY:
  3133. case PACKED_FLOAT64_ARRAY:
  3134. case PACKED_STRING_ARRAY:
  3135. case PACKED_VECTOR2_ARRAY:
  3136. case PACKED_VECTOR3_ARRAY:
  3137. case PACKED_COLOR_ARRAY:
  3138. case PACKED_VECTOR4_ARRAY: {
  3139. return _data.packed_array == p_variant._data.packed_array;
  3140. } break;
  3141. default: {
  3142. return hash_compare(p_variant);
  3143. }
  3144. }
  3145. }
  3146. bool StringLikeVariantComparator::compare(const Variant &p_lhs, const Variant &p_rhs) {
  3147. if (p_lhs.hash_compare(p_rhs)) {
  3148. return true;
  3149. }
  3150. if (p_lhs.get_type() == Variant::STRING && p_rhs.get_type() == Variant::STRING_NAME) {
  3151. return *VariantInternal::get_string(&p_lhs) == *VariantInternal::get_string_name(&p_rhs);
  3152. }
  3153. if (p_lhs.get_type() == Variant::STRING_NAME && p_rhs.get_type() == Variant::STRING) {
  3154. return *VariantInternal::get_string_name(&p_lhs) == *VariantInternal::get_string(&p_rhs);
  3155. }
  3156. return false;
  3157. }
  3158. bool Variant::is_ref_counted() const {
  3159. return type == OBJECT && _get_obj().id.is_ref_counted();
  3160. }
  3161. void Variant::static_assign(const Variant &p_variant) {
  3162. }
  3163. bool Variant::is_type_shared(Variant::Type p_type) {
  3164. switch (p_type) {
  3165. case OBJECT:
  3166. case ARRAY:
  3167. case DICTIONARY:
  3168. return true;
  3169. default: {
  3170. }
  3171. }
  3172. return false;
  3173. }
  3174. bool Variant::is_shared() const {
  3175. return is_type_shared(type);
  3176. }
  3177. bool Variant::is_read_only() const {
  3178. switch (type) {
  3179. case ARRAY:
  3180. return reinterpret_cast<const Array *>(_data._mem)->is_read_only();
  3181. case DICTIONARY:
  3182. return reinterpret_cast<const Dictionary *>(_data._mem)->is_read_only();
  3183. default:
  3184. return false;
  3185. }
  3186. }
  3187. void Variant::_variant_call_error(const String &p_method, Callable::CallError &error) {
  3188. switch (error.error) {
  3189. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  3190. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(Variant::Type(error.expected)) + "'.";
  3191. ERR_PRINT(err.utf8().get_data());
  3192. } break;
  3193. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  3194. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  3195. ERR_PRINT(err.utf8().get_data());
  3196. } break;
  3197. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  3198. String err = "Too many arguments for method '" + p_method + "'";
  3199. ERR_PRINT(err.utf8().get_data());
  3200. } break;
  3201. default: {
  3202. }
  3203. }
  3204. }
  3205. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  3206. r_value = Variant();
  3207. }
  3208. String Variant::get_construct_string() const {
  3209. String vars;
  3210. VariantWriter::write_to_string(*this, vars);
  3211. return vars;
  3212. }
  3213. String Variant::get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3214. return get_call_error_text(nullptr, p_method, p_argptrs, p_argcount, ce);
  3215. }
  3216. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3217. String err_text;
  3218. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  3219. int errorarg = ce.argument;
  3220. if (p_argptrs) {
  3221. 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));
  3222. } else {
  3223. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(Variant::Type(ce.expected));
  3224. }
  3225. } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  3226. err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
  3227. } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  3228. err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
  3229. } else if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  3230. err_text = "Method not found";
  3231. } else if (ce.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  3232. err_text = "Instance is null";
  3233. } else if (ce.error == Callable::CallError::CALL_ERROR_METHOD_NOT_CONST) {
  3234. err_text = "Method not const in const instance";
  3235. } else if (ce.error == Callable::CallError::CALL_OK) {
  3236. return "Call OK";
  3237. }
  3238. String base_text;
  3239. if (p_base) {
  3240. base_text = p_base->get_class();
  3241. Ref<Resource> script = p_base->get_script();
  3242. if (script.is_valid() && script->get_path().is_resource_file()) {
  3243. base_text += "(" + script->get_path().get_file() + ")";
  3244. }
  3245. base_text += "::";
  3246. }
  3247. return "'" + base_text + String(p_method) + "': " + err_text;
  3248. }
  3249. String Variant::get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3250. Vector<Variant> binds;
  3251. int args_bound;
  3252. p_callable.get_bound_arguments_ref(binds, args_bound);
  3253. if (args_bound <= 0) {
  3254. return get_call_error_text(p_callable.get_object(), p_callable.get_method(), p_argptrs, MAX(0, p_argcount + args_bound), ce);
  3255. } else {
  3256. Vector<const Variant *> argptrs;
  3257. argptrs.resize(p_argcount + binds.size());
  3258. for (int i = 0; i < p_argcount; i++) {
  3259. argptrs.write[i] = p_argptrs[i];
  3260. }
  3261. for (int i = 0; i < binds.size(); i++) {
  3262. argptrs.write[i + p_argcount] = &binds[i];
  3263. }
  3264. return get_call_error_text(p_callable.get_object(), p_callable.get_method(), (const Variant **)argptrs.ptr(), argptrs.size(), ce);
  3265. }
  3266. }
  3267. void Variant::register_types() {
  3268. _register_variant_operators();
  3269. _register_variant_methods();
  3270. _register_variant_setters_getters();
  3271. _register_variant_constructors();
  3272. _register_variant_destructors();
  3273. _register_variant_utility_functions();
  3274. }
  3275. void Variant::unregister_types() {
  3276. _unregister_variant_operators();
  3277. _unregister_variant_methods();
  3278. _unregister_variant_setters_getters();
  3279. _unregister_variant_destructors();
  3280. _unregister_variant_utility_functions();
  3281. }