variant.cpp 69 KB

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