variant.cpp 77 KB

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