variant.cpp 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306
  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 == false;
  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. return 0;
  847. }
  848. Variant::operator unsigned int() const {
  849. switch (type) {
  850. case NIL: return 0;
  851. case BOOL: return _data._bool ? 1 : 0;
  852. case INT: return _data._int;
  853. case REAL: return _data._real;
  854. case STRING: return operator String().to_int();
  855. default: {
  856. return 0;
  857. }
  858. }
  859. return 0;
  860. }
  861. Variant::operator int64_t() const {
  862. switch (type) {
  863. case NIL: return 0;
  864. case BOOL: return _data._bool ? 1 : 0;
  865. case INT: return _data._int;
  866. case REAL: return _data._real;
  867. case STRING: return operator String().to_int64();
  868. default: {
  869. return 0;
  870. }
  871. }
  872. return 0;
  873. }
  874. /*
  875. Variant::operator long unsigned int() const {
  876. switch( type ) {
  877. case NIL: return 0;
  878. case BOOL: return _data._bool ? 1 : 0;
  879. case INT: return _data._int;
  880. case REAL: return _data._real;
  881. case STRING: return operator String().to_int();
  882. default: {
  883. return 0;
  884. }
  885. }
  886. return 0;
  887. };
  888. */
  889. Variant::operator uint64_t() const {
  890. switch (type) {
  891. case NIL: return 0;
  892. case BOOL: return _data._bool ? 1 : 0;
  893. case INT: return _data._int;
  894. case REAL: return _data._real;
  895. case STRING: return operator String().to_int();
  896. default: {
  897. return 0;
  898. }
  899. }
  900. return 0;
  901. }
  902. #ifdef NEED_LONG_INT
  903. Variant::operator signed long() const {
  904. switch (type) {
  905. case NIL: return 0;
  906. case BOOL: return _data._bool ? 1 : 0;
  907. case INT: return _data._int;
  908. case REAL: return _data._real;
  909. case STRING: return operator String().to_int();
  910. default: {
  911. return 0;
  912. }
  913. }
  914. return 0;
  915. };
  916. Variant::operator unsigned long() const {
  917. switch (type) {
  918. case NIL: return 0;
  919. case BOOL: return _data._bool ? 1 : 0;
  920. case INT: return _data._int;
  921. case REAL: return _data._real;
  922. case STRING: return operator String().to_int();
  923. default: {
  924. return 0;
  925. }
  926. }
  927. return 0;
  928. };
  929. #endif
  930. Variant::operator signed short() const {
  931. switch (type) {
  932. case NIL: return 0;
  933. case BOOL: return _data._bool ? 1 : 0;
  934. case INT: return _data._int;
  935. case REAL: return _data._real;
  936. case STRING: return operator String().to_int();
  937. default: {
  938. return 0;
  939. }
  940. }
  941. return 0;
  942. }
  943. Variant::operator unsigned short() const {
  944. switch (type) {
  945. case NIL: return 0;
  946. case BOOL: return _data._bool ? 1 : 0;
  947. case INT: return _data._int;
  948. case REAL: return _data._real;
  949. case STRING: return operator String().to_int();
  950. default: {
  951. return 0;
  952. }
  953. }
  954. return 0;
  955. }
  956. Variant::operator signed char() const {
  957. switch (type) {
  958. case NIL: return 0;
  959. case BOOL: return _data._bool ? 1 : 0;
  960. case INT: return _data._int;
  961. case REAL: return _data._real;
  962. case STRING: return operator String().to_int();
  963. default: {
  964. return 0;
  965. }
  966. }
  967. return 0;
  968. }
  969. Variant::operator unsigned char() const {
  970. switch (type) {
  971. case NIL: return 0;
  972. case BOOL: return _data._bool ? 1 : 0;
  973. case INT: return _data._int;
  974. case REAL: return _data._real;
  975. case STRING: return operator String().to_int();
  976. default: {
  977. return 0;
  978. }
  979. }
  980. return 0;
  981. }
  982. Variant::operator CharType() const {
  983. return operator unsigned int();
  984. }
  985. Variant::operator float() const {
  986. switch (type) {
  987. case NIL: return 0;
  988. case BOOL: return _data._bool ? 1.0 : 0.0;
  989. case INT: return (float)_data._int;
  990. case REAL: return _data._real;
  991. case STRING: return operator String().to_double();
  992. default: {
  993. return 0;
  994. }
  995. }
  996. return 0;
  997. }
  998. Variant::operator double() const {
  999. switch (type) {
  1000. case NIL: return 0;
  1001. case BOOL: return _data._bool ? 1.0 : 0.0;
  1002. case INT: return (double)_data._int;
  1003. case REAL: return _data._real;
  1004. case STRING: return operator String().to_double();
  1005. default: {
  1006. return 0;
  1007. }
  1008. }
  1009. return true;
  1010. }
  1011. Variant::operator StringName() const {
  1012. if (type == NODE_PATH) {
  1013. return reinterpret_cast<const NodePath *>(_data._mem)->get_sname();
  1014. }
  1015. return StringName(operator String());
  1016. }
  1017. struct _VariantStrPair {
  1018. String key;
  1019. String value;
  1020. bool operator<(const _VariantStrPair &p) const {
  1021. return key < p.key;
  1022. }
  1023. };
  1024. Variant::operator String() const {
  1025. switch (type) {
  1026. case NIL: return "Null";
  1027. case BOOL: return _data._bool ? "True" : "False";
  1028. case INT: return itos(_data._int);
  1029. case REAL: return rtos(_data._real);
  1030. case STRING: return *reinterpret_cast<const String *>(_data._mem);
  1031. case VECTOR2: return "(" + operator Vector2() + ")";
  1032. case RECT2: return "(" + operator Rect2() + ")";
  1033. case TRANSFORM2D: {
  1034. Transform2D mat32 = operator Transform2D();
  1035. return "(" + Variant(mat32.elements[0]).operator String() + ", " + Variant(mat32.elements[1]).operator String() + ", " + Variant(mat32.elements[2]).operator String() + ")";
  1036. } break;
  1037. case VECTOR3: return "(" + operator Vector3() + ")";
  1038. case PLANE:
  1039. return operator Plane();
  1040. //case QUAT:
  1041. case AABB: return operator ::AABB();
  1042. case QUAT: return "(" + operator Quat() + ")";
  1043. case BASIS: {
  1044. Basis mat3 = operator Basis();
  1045. String mtx("(");
  1046. for (int i = 0; i < 3; i++) {
  1047. if (i != 0)
  1048. mtx += ", ";
  1049. mtx += "(";
  1050. for (int j = 0; j < 3; j++) {
  1051. if (j != 0)
  1052. mtx += ", ";
  1053. mtx += Variant(mat3.elements[i][j]).operator String();
  1054. }
  1055. mtx += ")";
  1056. }
  1057. return mtx + ")";
  1058. } break;
  1059. case TRANSFORM: return operator Transform();
  1060. case NODE_PATH: return operator NodePath();
  1061. case COLOR: return String::num(operator Color().r) + "," + String::num(operator Color().g) + "," + String::num(operator Color().b) + "," + String::num(operator Color().a);
  1062. case DICTIONARY: {
  1063. const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
  1064. //const String *K=NULL;
  1065. String str("{");
  1066. List<Variant> keys;
  1067. d.get_key_list(&keys);
  1068. Vector<_VariantStrPair> pairs;
  1069. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1070. _VariantStrPair sp;
  1071. sp.key = String(E->get());
  1072. sp.value = d[E->get()];
  1073. pairs.push_back(sp);
  1074. }
  1075. pairs.sort();
  1076. for (int i = 0; i < pairs.size(); i++) {
  1077. if (i > 0)
  1078. str += ", ";
  1079. str += pairs[i].key + ":" + pairs[i].value;
  1080. }
  1081. str += "}";
  1082. return str;
  1083. } break;
  1084. case POOL_VECTOR2_ARRAY: {
  1085. PoolVector<Vector2> vec = operator PoolVector<Vector2>();
  1086. String str("[");
  1087. for (int i = 0; i < vec.size(); i++) {
  1088. if (i > 0)
  1089. str += ", ";
  1090. str = str + Variant(vec[i]);
  1091. }
  1092. str += "]";
  1093. return str;
  1094. } break;
  1095. case POOL_VECTOR3_ARRAY: {
  1096. PoolVector<Vector3> vec = operator PoolVector<Vector3>();
  1097. String str("[");
  1098. for (int i = 0; i < vec.size(); i++) {
  1099. if (i > 0)
  1100. str += ", ";
  1101. str = str + Variant(vec[i]);
  1102. }
  1103. str += "]";
  1104. return str;
  1105. } break;
  1106. case POOL_STRING_ARRAY: {
  1107. PoolVector<String> vec = operator PoolVector<String>();
  1108. String str("[");
  1109. for (int i = 0; i < vec.size(); i++) {
  1110. if (i > 0)
  1111. str += ", ";
  1112. str = str + vec[i];
  1113. }
  1114. str += "]";
  1115. return str;
  1116. } break;
  1117. case POOL_INT_ARRAY: {
  1118. PoolVector<int> vec = operator PoolVector<int>();
  1119. String str("[");
  1120. for (int i = 0; i < vec.size(); i++) {
  1121. if (i > 0)
  1122. str += ", ";
  1123. str = str + itos(vec[i]);
  1124. }
  1125. str += "]";
  1126. return str;
  1127. } break;
  1128. case POOL_REAL_ARRAY: {
  1129. PoolVector<real_t> vec = operator PoolVector<real_t>();
  1130. String str("[");
  1131. for (int i = 0; i < vec.size(); i++) {
  1132. if (i > 0)
  1133. str += ", ";
  1134. str = str + rtos(vec[i]);
  1135. }
  1136. str += "]";
  1137. return str;
  1138. } break;
  1139. case ARRAY: {
  1140. Array arr = operator Array();
  1141. String str("[");
  1142. for (int i = 0; i < arr.size(); i++) {
  1143. if (i)
  1144. str += ", ";
  1145. str += String(arr[i]);
  1146. };
  1147. str += "]";
  1148. return str;
  1149. } break;
  1150. case OBJECT: {
  1151. if (_get_obj().obj) {
  1152. #ifdef DEBUG_ENABLED
  1153. if (ScriptDebugger::get_singleton() && _get_obj().ref.is_null()) {
  1154. //only if debugging!
  1155. if (!ObjectDB::instance_validate(_get_obj().obj)) {
  1156. return "[Deleted Object]";
  1157. };
  1158. };
  1159. #endif
  1160. return "[" + _get_obj().obj->get_class() + ":" + itos(_get_obj().obj->get_instance_id()) + "]";
  1161. } else
  1162. return "[Object:null]";
  1163. } break;
  1164. default: {
  1165. return "[" + get_type_name(type) + "]";
  1166. }
  1167. }
  1168. return "";
  1169. }
  1170. Variant::operator Vector2() const {
  1171. if (type == VECTOR2)
  1172. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1173. else if (type == VECTOR3)
  1174. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1175. else
  1176. return Vector2();
  1177. }
  1178. Variant::operator Rect2() const {
  1179. if (type == RECT2)
  1180. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1181. else
  1182. return Rect2();
  1183. }
  1184. Variant::operator Vector3() const {
  1185. if (type == VECTOR3)
  1186. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1187. else if (type == VECTOR2)
  1188. return Vector3(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1189. else
  1190. return Vector3();
  1191. }
  1192. Variant::operator Plane() const {
  1193. if (type == PLANE)
  1194. return *reinterpret_cast<const Plane *>(_data._mem);
  1195. else
  1196. return Plane();
  1197. }
  1198. Variant::operator ::AABB() const {
  1199. if (type == AABB)
  1200. return *_data._aabb;
  1201. else
  1202. return ::AABB();
  1203. }
  1204. Variant::operator Basis() const {
  1205. if (type == BASIS)
  1206. return *_data._basis;
  1207. else if (type == QUAT)
  1208. return *reinterpret_cast<const Quat *>(_data._mem);
  1209. else if (type == VECTOR3) {
  1210. return Basis(*reinterpret_cast<const Vector3 *>(_data._mem));
  1211. } else if (type == TRANSFORM) // unexposed in Variant::can_convert?
  1212. return _data._transform->basis;
  1213. else
  1214. return Basis();
  1215. }
  1216. Variant::operator Quat() const {
  1217. if (type == QUAT)
  1218. return *reinterpret_cast<const Quat *>(_data._mem);
  1219. else if (type == BASIS)
  1220. return *_data._basis;
  1221. else if (type == TRANSFORM)
  1222. return _data._transform->basis;
  1223. else
  1224. return Quat();
  1225. }
  1226. Variant::operator Transform() const {
  1227. if (type == TRANSFORM)
  1228. return *_data._transform;
  1229. else if (type == BASIS)
  1230. return Transform(*_data._basis, Vector3());
  1231. else if (type == QUAT)
  1232. return Transform(Basis(*reinterpret_cast<const Quat *>(_data._mem)), Vector3());
  1233. else if (type == TRANSFORM2D) {
  1234. const Transform2D &t = *_data._transform2d;
  1235. Transform m;
  1236. m.basis.elements[0][0] = t.elements[0][0];
  1237. m.basis.elements[1][0] = t.elements[0][1];
  1238. m.basis.elements[0][1] = t.elements[1][0];
  1239. m.basis.elements[1][1] = t.elements[1][1];
  1240. m.origin[0] = t.elements[2][0];
  1241. m.origin[1] = t.elements[2][1];
  1242. return m;
  1243. } else
  1244. return Transform();
  1245. }
  1246. Variant::operator Transform2D() const {
  1247. if (type == TRANSFORM2D) {
  1248. return *_data._transform2d;
  1249. } else if (type == TRANSFORM) {
  1250. const Transform &t = *_data._transform;
  1251. Transform2D m;
  1252. m.elements[0][0] = t.basis.elements[0][0];
  1253. m.elements[0][1] = t.basis.elements[1][0];
  1254. m.elements[1][0] = t.basis.elements[0][1];
  1255. m.elements[1][1] = t.basis.elements[1][1];
  1256. m.elements[2][0] = t.origin[0];
  1257. m.elements[2][1] = t.origin[1];
  1258. return m;
  1259. } else
  1260. return Transform2D();
  1261. }
  1262. Variant::operator Color() const {
  1263. if (type == COLOR)
  1264. return *reinterpret_cast<const Color *>(_data._mem);
  1265. else if (type == STRING)
  1266. return Color::html(operator String());
  1267. else if (type == INT)
  1268. return Color::hex(operator int());
  1269. else
  1270. return Color();
  1271. }
  1272. Variant::operator NodePath() const {
  1273. if (type == NODE_PATH)
  1274. return *reinterpret_cast<const NodePath *>(_data._mem);
  1275. else if (type == STRING)
  1276. return NodePath(operator String());
  1277. else
  1278. return NodePath();
  1279. }
  1280. Variant::operator RefPtr() const {
  1281. if (type == OBJECT)
  1282. return _get_obj().ref;
  1283. else
  1284. return RefPtr();
  1285. }
  1286. Variant::operator RID() const {
  1287. if (type == _RID)
  1288. return *reinterpret_cast<const RID *>(_data._mem);
  1289. else if (type == OBJECT && !_get_obj().ref.is_null()) {
  1290. return _get_obj().ref.get_rid();
  1291. } else if (type == OBJECT && _get_obj().obj) {
  1292. #ifdef DEBUG_ENABLED
  1293. if (ScriptDebugger::get_singleton()) {
  1294. if (!ObjectDB::instance_validate(_get_obj().obj)) {
  1295. ERR_EXPLAIN("Invalid pointer (object was deleted)");
  1296. ERR_FAIL_V(RID());
  1297. };
  1298. };
  1299. #endif
  1300. Variant::CallError ce;
  1301. Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->get_rid, NULL, 0, ce);
  1302. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() == Variant::_RID) {
  1303. return ret;
  1304. }
  1305. return RID();
  1306. } else {
  1307. return RID();
  1308. }
  1309. }
  1310. Variant::operator Object *() const {
  1311. if (type == OBJECT)
  1312. return _get_obj().obj;
  1313. else
  1314. return NULL;
  1315. }
  1316. Variant::operator Node *() const {
  1317. if (type == OBJECT)
  1318. return Object::cast_to<Node>(_get_obj().obj);
  1319. else
  1320. return NULL;
  1321. }
  1322. Variant::operator Control *() const {
  1323. if (type == OBJECT)
  1324. return Object::cast_to<Control>(_get_obj().obj);
  1325. else
  1326. return NULL;
  1327. }
  1328. Variant::operator Dictionary() const {
  1329. if (type == DICTIONARY)
  1330. return *reinterpret_cast<const Dictionary *>(_data._mem);
  1331. else
  1332. return Dictionary();
  1333. }
  1334. template <class DA, class SA>
  1335. inline DA _convert_array(const SA &p_array) {
  1336. DA da;
  1337. da.resize(p_array.size());
  1338. for (int i = 0; i < p_array.size(); i++) {
  1339. da.set(i, Variant(p_array.get(i)));
  1340. }
  1341. return da;
  1342. }
  1343. template <class DA>
  1344. inline DA _convert_array_from_variant(const Variant &p_variant) {
  1345. switch (p_variant.get_type()) {
  1346. case Variant::ARRAY: {
  1347. return _convert_array<DA, Array>(p_variant.operator Array());
  1348. }
  1349. case Variant::POOL_BYTE_ARRAY: {
  1350. return _convert_array<DA, PoolVector<uint8_t> >(p_variant.operator PoolVector<uint8_t>());
  1351. }
  1352. case Variant::POOL_INT_ARRAY: {
  1353. return _convert_array<DA, PoolVector<int> >(p_variant.operator PoolVector<int>());
  1354. }
  1355. case Variant::POOL_REAL_ARRAY: {
  1356. return _convert_array<DA, PoolVector<real_t> >(p_variant.operator PoolVector<real_t>());
  1357. }
  1358. case Variant::POOL_STRING_ARRAY: {
  1359. return _convert_array<DA, PoolVector<String> >(p_variant.operator PoolVector<String>());
  1360. }
  1361. case Variant::POOL_VECTOR2_ARRAY: {
  1362. return _convert_array<DA, PoolVector<Vector2> >(p_variant.operator PoolVector<Vector2>());
  1363. }
  1364. case Variant::POOL_VECTOR3_ARRAY: {
  1365. return _convert_array<DA, PoolVector<Vector3> >(p_variant.operator PoolVector<Vector3>());
  1366. }
  1367. case Variant::POOL_COLOR_ARRAY: {
  1368. return _convert_array<DA, PoolVector<Color> >(p_variant.operator PoolVector<Color>());
  1369. }
  1370. default: {
  1371. return DA();
  1372. }
  1373. }
  1374. return DA();
  1375. }
  1376. Variant::operator Array() const {
  1377. if (type == ARRAY)
  1378. return *reinterpret_cast<const Array *>(_data._mem);
  1379. else
  1380. return _convert_array_from_variant<Array>(*this);
  1381. }
  1382. Variant::operator PoolVector<uint8_t>() const {
  1383. if (type == POOL_BYTE_ARRAY)
  1384. return *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  1385. else
  1386. return _convert_array_from_variant<PoolVector<uint8_t> >(*this);
  1387. }
  1388. Variant::operator PoolVector<int>() const {
  1389. if (type == POOL_INT_ARRAY)
  1390. return *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  1391. else
  1392. return _convert_array_from_variant<PoolVector<int> >(*this);
  1393. }
  1394. Variant::operator PoolVector<real_t>() const {
  1395. if (type == POOL_REAL_ARRAY)
  1396. return *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  1397. else
  1398. return _convert_array_from_variant<PoolVector<real_t> >(*this);
  1399. }
  1400. Variant::operator PoolVector<String>() const {
  1401. if (type == POOL_STRING_ARRAY)
  1402. return *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  1403. else
  1404. return _convert_array_from_variant<PoolVector<String> >(*this);
  1405. }
  1406. Variant::operator PoolVector<Vector3>() const {
  1407. if (type == POOL_VECTOR3_ARRAY)
  1408. return *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  1409. else
  1410. return _convert_array_from_variant<PoolVector<Vector3> >(*this);
  1411. }
  1412. Variant::operator PoolVector<Vector2>() const {
  1413. if (type == POOL_VECTOR2_ARRAY)
  1414. return *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  1415. else
  1416. return _convert_array_from_variant<PoolVector<Vector2> >(*this);
  1417. }
  1418. Variant::operator PoolVector<Color>() const {
  1419. if (type == POOL_COLOR_ARRAY)
  1420. return *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  1421. else
  1422. return _convert_array_from_variant<PoolVector<Color> >(*this);
  1423. }
  1424. /* helpers */
  1425. Variant::operator Vector<RID>() const {
  1426. Array va = operator Array();
  1427. Vector<RID> rids;
  1428. rids.resize(va.size());
  1429. for (int i = 0; i < rids.size(); i++)
  1430. rids.write[i] = va[i];
  1431. return rids;
  1432. }
  1433. Variant::operator Vector<Vector2>() const {
  1434. PoolVector<Vector2> from = operator PoolVector<Vector2>();
  1435. Vector<Vector2> to;
  1436. int len = from.size();
  1437. if (len == 0)
  1438. return Vector<Vector2>();
  1439. to.resize(len);
  1440. PoolVector<Vector2>::Read r = from.read();
  1441. Vector2 *w = to.ptrw();
  1442. for (int i = 0; i < len; i++) {
  1443. w[i] = r[i];
  1444. }
  1445. return to;
  1446. }
  1447. Variant::operator PoolVector<Plane>() const {
  1448. Array va = operator Array();
  1449. PoolVector<Plane> planes;
  1450. int va_size = va.size();
  1451. if (va_size == 0)
  1452. return planes;
  1453. planes.resize(va_size);
  1454. PoolVector<Plane>::Write w = planes.write();
  1455. for (int i = 0; i < va_size; i++)
  1456. w[i] = va[i];
  1457. return planes;
  1458. }
  1459. Variant::operator PoolVector<Face3>() const {
  1460. PoolVector<Vector3> va = operator PoolVector<Vector3>();
  1461. PoolVector<Face3> faces;
  1462. int va_size = va.size();
  1463. if (va_size == 0)
  1464. return faces;
  1465. faces.resize(va_size / 3);
  1466. PoolVector<Face3>::Write w = faces.write();
  1467. PoolVector<Vector3>::Read r = va.read();
  1468. for (int i = 0; i < va_size; i++)
  1469. w[i / 3].vertex[i % 3] = r[i];
  1470. return faces;
  1471. }
  1472. Variant::operator Vector<Plane>() const {
  1473. Array va = operator Array();
  1474. Vector<Plane> planes;
  1475. int va_size = va.size();
  1476. if (va_size == 0)
  1477. return planes;
  1478. planes.resize(va_size);
  1479. for (int i = 0; i < va_size; i++)
  1480. planes.write[i] = va[i];
  1481. return planes;
  1482. }
  1483. Variant::operator Vector<Variant>() const {
  1484. Array from = operator Array();
  1485. Vector<Variant> to;
  1486. int len = from.size();
  1487. to.resize(len);
  1488. for (int i = 0; i < len; i++) {
  1489. to.write[i] = from[i];
  1490. }
  1491. return to;
  1492. }
  1493. Variant::operator Vector<uint8_t>() const {
  1494. PoolVector<uint8_t> from = operator PoolVector<uint8_t>();
  1495. Vector<uint8_t> to;
  1496. int len = from.size();
  1497. to.resize(len);
  1498. for (int i = 0; i < len; i++) {
  1499. to.write[i] = from[i];
  1500. }
  1501. return to;
  1502. }
  1503. Variant::operator Vector<int>() const {
  1504. PoolVector<int> from = operator PoolVector<int>();
  1505. Vector<int> to;
  1506. int len = from.size();
  1507. to.resize(len);
  1508. for (int i = 0; i < len; i++) {
  1509. to.write[i] = from[i];
  1510. }
  1511. return to;
  1512. }
  1513. Variant::operator Vector<real_t>() const {
  1514. PoolVector<real_t> from = operator PoolVector<real_t>();
  1515. Vector<real_t> to;
  1516. int len = from.size();
  1517. to.resize(len);
  1518. for (int i = 0; i < len; i++) {
  1519. to.write[i] = from[i];
  1520. }
  1521. return to;
  1522. }
  1523. Variant::operator Vector<String>() const {
  1524. PoolVector<String> from = operator PoolVector<String>();
  1525. Vector<String> to;
  1526. int len = from.size();
  1527. to.resize(len);
  1528. for (int i = 0; i < len; i++) {
  1529. to.write[i] = from[i];
  1530. }
  1531. return to;
  1532. }
  1533. Variant::operator Vector<StringName>() const {
  1534. PoolVector<String> from = operator PoolVector<String>();
  1535. Vector<StringName> to;
  1536. int len = from.size();
  1537. to.resize(len);
  1538. for (int i = 0; i < len; i++) {
  1539. to.write[i] = from[i];
  1540. }
  1541. return to;
  1542. }
  1543. Variant::operator Vector<Vector3>() const {
  1544. PoolVector<Vector3> from = operator PoolVector<Vector3>();
  1545. Vector<Vector3> to;
  1546. int len = from.size();
  1547. if (len == 0)
  1548. return Vector<Vector3>();
  1549. to.resize(len);
  1550. PoolVector<Vector3>::Read r = from.read();
  1551. Vector3 *w = to.ptrw();
  1552. for (int i = 0; i < len; i++) {
  1553. w[i] = r[i];
  1554. }
  1555. return to;
  1556. }
  1557. Variant::operator Vector<Color>() const {
  1558. PoolVector<Color> from = operator PoolVector<Color>();
  1559. Vector<Color> to;
  1560. int len = from.size();
  1561. if (len == 0)
  1562. return Vector<Color>();
  1563. to.resize(len);
  1564. PoolVector<Color>::Read r = from.read();
  1565. Color *w = to.ptrw();
  1566. for (int i = 0; i < len; i++) {
  1567. w[i] = r[i];
  1568. }
  1569. return to;
  1570. }
  1571. Variant::operator Margin() const {
  1572. return (Margin) operator int();
  1573. }
  1574. Variant::operator Orientation() const {
  1575. return (Orientation) operator int();
  1576. }
  1577. Variant::operator IP_Address() const {
  1578. if (type == POOL_REAL_ARRAY || type == POOL_INT_ARRAY || type == POOL_BYTE_ARRAY) {
  1579. PoolVector<int> addr = operator PoolVector<int>();
  1580. if (addr.size() == 4) {
  1581. return IP_Address(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
  1582. }
  1583. }
  1584. return IP_Address(operator String());
  1585. }
  1586. Variant::Variant(bool p_bool) {
  1587. type = BOOL;
  1588. _data._bool = p_bool;
  1589. }
  1590. /*
  1591. Variant::Variant(long unsigned int p_long) {
  1592. type=INT;
  1593. _data._int=p_long;
  1594. };
  1595. */
  1596. Variant::Variant(signed int p_int) {
  1597. type = INT;
  1598. _data._int = p_int;
  1599. }
  1600. Variant::Variant(unsigned int p_int) {
  1601. type = INT;
  1602. _data._int = p_int;
  1603. }
  1604. #ifdef NEED_LONG_INT
  1605. Variant::Variant(signed long p_int) {
  1606. type = INT;
  1607. _data._int = p_int;
  1608. }
  1609. Variant::Variant(unsigned long p_int) {
  1610. type = INT;
  1611. _data._int = p_int;
  1612. }
  1613. #endif
  1614. Variant::Variant(int64_t p_int) {
  1615. type = INT;
  1616. _data._int = p_int;
  1617. }
  1618. Variant::Variant(uint64_t p_int) {
  1619. type = INT;
  1620. _data._int = p_int;
  1621. }
  1622. Variant::Variant(signed short p_short) {
  1623. type = INT;
  1624. _data._int = p_short;
  1625. }
  1626. Variant::Variant(unsigned short p_short) {
  1627. type = INT;
  1628. _data._int = p_short;
  1629. }
  1630. Variant::Variant(signed char p_char) {
  1631. type = INT;
  1632. _data._int = p_char;
  1633. }
  1634. Variant::Variant(unsigned char p_char) {
  1635. type = INT;
  1636. _data._int = p_char;
  1637. }
  1638. Variant::Variant(float p_float) {
  1639. type = REAL;
  1640. _data._real = p_float;
  1641. }
  1642. Variant::Variant(double p_double) {
  1643. type = REAL;
  1644. _data._real = p_double;
  1645. }
  1646. Variant::Variant(const StringName &p_string) {
  1647. type = STRING;
  1648. memnew_placement(_data._mem, String(p_string.operator String()));
  1649. }
  1650. Variant::Variant(const String &p_string) {
  1651. type = STRING;
  1652. memnew_placement(_data._mem, String(p_string));
  1653. }
  1654. Variant::Variant(const char *const p_cstring) {
  1655. type = STRING;
  1656. memnew_placement(_data._mem, String((const char *)p_cstring));
  1657. }
  1658. Variant::Variant(const CharType *p_wstring) {
  1659. type = STRING;
  1660. memnew_placement(_data._mem, String(p_wstring));
  1661. }
  1662. Variant::Variant(const Vector3 &p_vector3) {
  1663. type = VECTOR3;
  1664. memnew_placement(_data._mem, Vector3(p_vector3));
  1665. }
  1666. Variant::Variant(const Vector2 &p_vector2) {
  1667. type = VECTOR2;
  1668. memnew_placement(_data._mem, Vector2(p_vector2));
  1669. }
  1670. Variant::Variant(const Rect2 &p_rect2) {
  1671. type = RECT2;
  1672. memnew_placement(_data._mem, Rect2(p_rect2));
  1673. }
  1674. Variant::Variant(const Plane &p_plane) {
  1675. type = PLANE;
  1676. memnew_placement(_data._mem, Plane(p_plane));
  1677. }
  1678. Variant::Variant(const ::AABB &p_aabb) {
  1679. type = AABB;
  1680. _data._aabb = memnew(::AABB(p_aabb));
  1681. }
  1682. Variant::Variant(const Basis &p_matrix) {
  1683. type = BASIS;
  1684. _data._basis = memnew(Basis(p_matrix));
  1685. }
  1686. Variant::Variant(const Quat &p_quat) {
  1687. type = QUAT;
  1688. memnew_placement(_data._mem, Quat(p_quat));
  1689. }
  1690. Variant::Variant(const Transform &p_transform) {
  1691. type = TRANSFORM;
  1692. _data._transform = memnew(Transform(p_transform));
  1693. }
  1694. Variant::Variant(const Transform2D &p_transform) {
  1695. type = TRANSFORM2D;
  1696. _data._transform2d = memnew(Transform2D(p_transform));
  1697. }
  1698. Variant::Variant(const Color &p_color) {
  1699. type = COLOR;
  1700. memnew_placement(_data._mem, Color(p_color));
  1701. }
  1702. Variant::Variant(const NodePath &p_node_path) {
  1703. type = NODE_PATH;
  1704. memnew_placement(_data._mem, NodePath(p_node_path));
  1705. }
  1706. Variant::Variant(const RefPtr &p_resource) {
  1707. type = OBJECT;
  1708. memnew_placement(_data._mem, ObjData);
  1709. REF *ref = reinterpret_cast<REF *>(p_resource.get_data());
  1710. _get_obj().obj = ref->ptr();
  1711. _get_obj().ref = p_resource;
  1712. }
  1713. Variant::Variant(const RID &p_rid) {
  1714. type = _RID;
  1715. memnew_placement(_data._mem, RID(p_rid));
  1716. }
  1717. Variant::Variant(const Object *p_object) {
  1718. type = OBJECT;
  1719. memnew_placement(_data._mem, ObjData);
  1720. _get_obj().obj = const_cast<Object *>(p_object);
  1721. }
  1722. Variant::Variant(const Dictionary &p_dictionary) {
  1723. type = DICTIONARY;
  1724. memnew_placement(_data._mem, (Dictionary)(p_dictionary));
  1725. }
  1726. Variant::Variant(const Array &p_array) {
  1727. type = ARRAY;
  1728. memnew_placement(_data._mem, Array(p_array));
  1729. }
  1730. Variant::Variant(const PoolVector<Plane> &p_array) {
  1731. type = ARRAY;
  1732. Array *plane_array = memnew_placement(_data._mem, Array);
  1733. plane_array->resize(p_array.size());
  1734. for (int i = 0; i < p_array.size(); i++) {
  1735. plane_array->operator[](i) = Variant(p_array[i]);
  1736. }
  1737. }
  1738. Variant::Variant(const Vector<Plane> &p_array) {
  1739. type = ARRAY;
  1740. Array *plane_array = memnew_placement(_data._mem, Array);
  1741. plane_array->resize(p_array.size());
  1742. for (int i = 0; i < p_array.size(); i++) {
  1743. plane_array->operator[](i) = Variant(p_array[i]);
  1744. }
  1745. }
  1746. Variant::Variant(const Vector<RID> &p_array) {
  1747. type = ARRAY;
  1748. Array *rid_array = memnew_placement(_data._mem, Array);
  1749. rid_array->resize(p_array.size());
  1750. for (int i = 0; i < p_array.size(); i++) {
  1751. rid_array->set(i, Variant(p_array[i]));
  1752. }
  1753. }
  1754. Variant::Variant(const Vector<Vector2> &p_array) {
  1755. type = NIL;
  1756. PoolVector<Vector2> v;
  1757. int len = p_array.size();
  1758. if (len > 0) {
  1759. v.resize(len);
  1760. PoolVector<Vector2>::Write w = v.write();
  1761. const Vector2 *r = p_array.ptr();
  1762. for (int i = 0; i < len; i++)
  1763. w[i] = r[i];
  1764. }
  1765. *this = v;
  1766. }
  1767. Variant::Variant(const PoolVector<uint8_t> &p_raw_array) {
  1768. type = POOL_BYTE_ARRAY;
  1769. memnew_placement(_data._mem, PoolVector<uint8_t>(p_raw_array));
  1770. }
  1771. Variant::Variant(const PoolVector<int> &p_int_array) {
  1772. type = POOL_INT_ARRAY;
  1773. memnew_placement(_data._mem, PoolVector<int>(p_int_array));
  1774. }
  1775. Variant::Variant(const PoolVector<real_t> &p_real_array) {
  1776. type = POOL_REAL_ARRAY;
  1777. memnew_placement(_data._mem, PoolVector<real_t>(p_real_array));
  1778. }
  1779. Variant::Variant(const PoolVector<String> &p_string_array) {
  1780. type = POOL_STRING_ARRAY;
  1781. memnew_placement(_data._mem, PoolVector<String>(p_string_array));
  1782. }
  1783. Variant::Variant(const PoolVector<Vector3> &p_vector3_array) {
  1784. type = POOL_VECTOR3_ARRAY;
  1785. memnew_placement(_data._mem, PoolVector<Vector3>(p_vector3_array));
  1786. }
  1787. Variant::Variant(const PoolVector<Vector2> &p_vector2_array) {
  1788. type = POOL_VECTOR2_ARRAY;
  1789. memnew_placement(_data._mem, PoolVector<Vector2>(p_vector2_array));
  1790. }
  1791. Variant::Variant(const PoolVector<Color> &p_color_array) {
  1792. type = POOL_COLOR_ARRAY;
  1793. memnew_placement(_data._mem, PoolVector<Color>(p_color_array));
  1794. }
  1795. Variant::Variant(const PoolVector<Face3> &p_face_array) {
  1796. PoolVector<Vector3> vertices;
  1797. int face_count = p_face_array.size();
  1798. vertices.resize(face_count * 3);
  1799. if (face_count) {
  1800. PoolVector<Face3>::Read r = p_face_array.read();
  1801. PoolVector<Vector3>::Write w = vertices.write();
  1802. for (int i = 0; i < face_count; i++) {
  1803. for (int j = 0; j < 3; j++)
  1804. w[i * 3 + j] = r[i].vertex[j];
  1805. }
  1806. r = PoolVector<Face3>::Read();
  1807. w = PoolVector<Vector3>::Write();
  1808. }
  1809. type = NIL;
  1810. *this = vertices;
  1811. }
  1812. /* helpers */
  1813. Variant::Variant(const Vector<Variant> &p_array) {
  1814. type = NIL;
  1815. Array v;
  1816. int len = p_array.size();
  1817. v.resize(len);
  1818. for (int i = 0; i < len; i++)
  1819. v.set(i, p_array[i]);
  1820. *this = v;
  1821. }
  1822. Variant::Variant(const Vector<uint8_t> &p_array) {
  1823. type = NIL;
  1824. PoolVector<uint8_t> v;
  1825. int len = p_array.size();
  1826. v.resize(len);
  1827. for (int i = 0; i < len; i++)
  1828. v.set(i, p_array[i]);
  1829. *this = v;
  1830. }
  1831. Variant::Variant(const Vector<int> &p_array) {
  1832. type = NIL;
  1833. PoolVector<int> v;
  1834. int len = p_array.size();
  1835. v.resize(len);
  1836. for (int i = 0; i < len; i++)
  1837. v.set(i, p_array[i]);
  1838. *this = v;
  1839. }
  1840. Variant::Variant(const Vector<real_t> &p_array) {
  1841. type = NIL;
  1842. PoolVector<real_t> v;
  1843. int len = p_array.size();
  1844. v.resize(len);
  1845. for (int i = 0; i < len; i++)
  1846. v.set(i, p_array[i]);
  1847. *this = v;
  1848. }
  1849. Variant::Variant(const Vector<String> &p_array) {
  1850. type = NIL;
  1851. PoolVector<String> v;
  1852. int len = p_array.size();
  1853. v.resize(len);
  1854. for (int i = 0; i < len; i++)
  1855. v.set(i, p_array[i]);
  1856. *this = v;
  1857. }
  1858. Variant::Variant(const Vector<StringName> &p_array) {
  1859. type = NIL;
  1860. PoolVector<String> v;
  1861. int len = p_array.size();
  1862. v.resize(len);
  1863. for (int i = 0; i < len; i++)
  1864. v.set(i, p_array[i]);
  1865. *this = v;
  1866. }
  1867. Variant::Variant(const Vector<Vector3> &p_array) {
  1868. type = NIL;
  1869. PoolVector<Vector3> v;
  1870. int len = p_array.size();
  1871. if (len > 0) {
  1872. v.resize(len);
  1873. PoolVector<Vector3>::Write w = v.write();
  1874. const Vector3 *r = p_array.ptr();
  1875. for (int i = 0; i < len; i++)
  1876. w[i] = r[i];
  1877. }
  1878. *this = v;
  1879. }
  1880. Variant::Variant(const Vector<Color> &p_array) {
  1881. type = NIL;
  1882. PoolVector<Color> v;
  1883. int len = p_array.size();
  1884. v.resize(len);
  1885. for (int i = 0; i < len; i++)
  1886. v.set(i, p_array[i]);
  1887. *this = v;
  1888. }
  1889. void Variant::operator=(const Variant &p_variant) {
  1890. if (unlikely(this == &p_variant))
  1891. return;
  1892. if (unlikely(type != p_variant.type)) {
  1893. reference(p_variant);
  1894. return;
  1895. }
  1896. switch (p_variant.type) {
  1897. case NIL: {
  1898. // none
  1899. } break;
  1900. // atomic types
  1901. case BOOL: {
  1902. _data._bool = p_variant._data._bool;
  1903. } break;
  1904. case INT: {
  1905. _data._int = p_variant._data._int;
  1906. } break;
  1907. case REAL: {
  1908. _data._real = p_variant._data._real;
  1909. } break;
  1910. case STRING: {
  1911. *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
  1912. } break;
  1913. // math types
  1914. case VECTOR2: {
  1915. *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  1916. } break;
  1917. case RECT2: {
  1918. *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  1919. } break;
  1920. case TRANSFORM2D: {
  1921. *_data._transform2d = *(p_variant._data._transform2d);
  1922. } break;
  1923. case VECTOR3: {
  1924. *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  1925. } break;
  1926. case PLANE: {
  1927. *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
  1928. } break;
  1929. case AABB: {
  1930. *_data._aabb = *(p_variant._data._aabb);
  1931. } break;
  1932. case QUAT: {
  1933. *reinterpret_cast<Quat *>(_data._mem) = *reinterpret_cast<const Quat *>(p_variant._data._mem);
  1934. } break;
  1935. case BASIS: {
  1936. *_data._basis = *(p_variant._data._basis);
  1937. } break;
  1938. case TRANSFORM: {
  1939. *_data._transform = *(p_variant._data._transform);
  1940. } break;
  1941. // misc types
  1942. case COLOR: {
  1943. *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
  1944. } break;
  1945. case _RID: {
  1946. *reinterpret_cast<RID *>(_data._mem) = *reinterpret_cast<const RID *>(p_variant._data._mem);
  1947. } break;
  1948. case OBJECT: {
  1949. *reinterpret_cast<ObjData *>(_data._mem) = p_variant._get_obj();
  1950. } break;
  1951. case NODE_PATH: {
  1952. *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
  1953. } break;
  1954. case DICTIONARY: {
  1955. *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
  1956. } break;
  1957. case ARRAY: {
  1958. *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
  1959. } break;
  1960. // arrays
  1961. case POOL_BYTE_ARRAY: {
  1962. *reinterpret_cast<PoolVector<uint8_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem);
  1963. } break;
  1964. case POOL_INT_ARRAY: {
  1965. *reinterpret_cast<PoolVector<int> *>(_data._mem) = *reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem);
  1966. } break;
  1967. case POOL_REAL_ARRAY: {
  1968. *reinterpret_cast<PoolVector<real_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem);
  1969. } break;
  1970. case POOL_STRING_ARRAY: {
  1971. *reinterpret_cast<PoolVector<String> *>(_data._mem) = *reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem);
  1972. } break;
  1973. case POOL_VECTOR2_ARRAY: {
  1974. *reinterpret_cast<PoolVector<Vector2> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem);
  1975. } break;
  1976. case POOL_VECTOR3_ARRAY: {
  1977. *reinterpret_cast<PoolVector<Vector3> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem);
  1978. } break;
  1979. case POOL_COLOR_ARRAY: {
  1980. *reinterpret_cast<PoolVector<Color> *>(_data._mem) = *reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem);
  1981. } break;
  1982. default: {
  1983. }
  1984. }
  1985. }
  1986. Variant::Variant(const IP_Address &p_address) {
  1987. type = STRING;
  1988. memnew_placement(_data._mem, String(p_address));
  1989. }
  1990. Variant::Variant(const Variant &p_variant) {
  1991. type = NIL;
  1992. reference(p_variant);
  1993. }
  1994. /*
  1995. Variant::~Variant() {
  1996. clear();
  1997. }*/
  1998. uint32_t Variant::hash() const {
  1999. switch (type) {
  2000. case NIL: {
  2001. return 0;
  2002. } break;
  2003. case BOOL: {
  2004. return _data._bool ? 1 : 0;
  2005. } break;
  2006. case INT: {
  2007. return _data._int;
  2008. } break;
  2009. case REAL: {
  2010. return hash_djb2_one_float(_data._real);
  2011. } break;
  2012. case STRING: {
  2013. return reinterpret_cast<const String *>(_data._mem)->hash();
  2014. } break;
  2015. // math types
  2016. case VECTOR2: {
  2017. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->x);
  2018. return hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->y, hash);
  2019. } break;
  2020. case RECT2: {
  2021. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.x);
  2022. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.y, hash);
  2023. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.x, hash);
  2024. return hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.y, hash);
  2025. } break;
  2026. case TRANSFORM2D: {
  2027. uint32_t hash = 5831;
  2028. for (int i = 0; i < 3; i++) {
  2029. for (int j = 0; j < 2; j++) {
  2030. hash = hash_djb2_one_float(_data._transform2d->elements[i][j], hash);
  2031. }
  2032. }
  2033. return hash;
  2034. } break;
  2035. case VECTOR3: {
  2036. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->x);
  2037. hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->y, hash);
  2038. return hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->z, hash);
  2039. } break;
  2040. case PLANE: {
  2041. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.x);
  2042. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.y, hash);
  2043. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.z, hash);
  2044. return hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->d, hash);
  2045. } break;
  2046. /*
  2047. case QUAT: {
  2048. } break;*/
  2049. case AABB: {
  2050. uint32_t hash = 5831;
  2051. for (int i = 0; i < 3; i++) {
  2052. hash = hash_djb2_one_float(_data._aabb->position[i], hash);
  2053. hash = hash_djb2_one_float(_data._aabb->size[i], hash);
  2054. }
  2055. return hash;
  2056. } break;
  2057. case QUAT: {
  2058. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->x);
  2059. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->y, hash);
  2060. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->z, hash);
  2061. return hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->w, hash);
  2062. } break;
  2063. case BASIS: {
  2064. uint32_t hash = 5831;
  2065. for (int i = 0; i < 3; i++) {
  2066. for (int j = 0; j < 3; j++) {
  2067. hash = hash_djb2_one_float(_data._basis->elements[i][j], hash);
  2068. }
  2069. }
  2070. return hash;
  2071. } break;
  2072. case TRANSFORM: {
  2073. uint32_t hash = 5831;
  2074. for (int i = 0; i < 3; i++) {
  2075. for (int j = 0; j < 3; j++) {
  2076. hash = hash_djb2_one_float(_data._transform->basis.elements[i][j], hash);
  2077. }
  2078. hash = hash_djb2_one_float(_data._transform->origin[i], hash);
  2079. }
  2080. return hash;
  2081. } break;
  2082. // misc types
  2083. case COLOR: {
  2084. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->r);
  2085. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->g, hash);
  2086. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->b, hash);
  2087. return hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->a, hash);
  2088. } break;
  2089. case _RID: {
  2090. return hash_djb2_one_64(reinterpret_cast<const RID *>(_data._mem)->get_id());
  2091. } break;
  2092. case OBJECT: {
  2093. return hash_djb2_one_64(make_uint64_t(_get_obj().obj));
  2094. } break;
  2095. case NODE_PATH: {
  2096. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2097. } break;
  2098. case DICTIONARY: {
  2099. return reinterpret_cast<const Dictionary *>(_data._mem)->hash();
  2100. } break;
  2101. case ARRAY: {
  2102. const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
  2103. return arr.hash();
  2104. } break;
  2105. case POOL_BYTE_ARRAY: {
  2106. const PoolVector<uint8_t> &arr = *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  2107. int len = arr.size();
  2108. if (likely(len)) {
  2109. PoolVector<uint8_t>::Read r = arr.read();
  2110. return hash_djb2_buffer((uint8_t *)&r[0], len);
  2111. } else {
  2112. return hash_djb2_one_64(0);
  2113. }
  2114. } break;
  2115. case POOL_INT_ARRAY: {
  2116. const PoolVector<int> &arr = *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  2117. int len = arr.size();
  2118. if (likely(len)) {
  2119. PoolVector<int>::Read r = arr.read();
  2120. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(int));
  2121. } else {
  2122. return hash_djb2_one_64(0);
  2123. }
  2124. } break;
  2125. case POOL_REAL_ARRAY: {
  2126. const PoolVector<real_t> &arr = *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  2127. int len = arr.size();
  2128. if (likely(len)) {
  2129. PoolVector<real_t>::Read r = arr.read();
  2130. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(real_t));
  2131. } else {
  2132. return hash_djb2_one_float(0.0);
  2133. }
  2134. } break;
  2135. case POOL_STRING_ARRAY: {
  2136. uint32_t hash = 5831;
  2137. const PoolVector<String> &arr = *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  2138. int len = arr.size();
  2139. if (likely(len)) {
  2140. PoolVector<String>::Read r = arr.read();
  2141. for (int i = 0; i < len; i++) {
  2142. hash = hash_djb2_one_32(r[i].hash(), hash);
  2143. }
  2144. }
  2145. return hash;
  2146. } break;
  2147. case POOL_VECTOR2_ARRAY: {
  2148. uint32_t hash = 5831;
  2149. const PoolVector<Vector2> &arr = *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  2150. int len = arr.size();
  2151. if (likely(len)) {
  2152. PoolVector<Vector2>::Read r = arr.read();
  2153. for (int i = 0; i < len; i++) {
  2154. hash = hash_djb2_one_float(r[i].x, hash);
  2155. hash = hash_djb2_one_float(r[i].y, hash);
  2156. }
  2157. }
  2158. return hash;
  2159. } break;
  2160. case POOL_VECTOR3_ARRAY: {
  2161. uint32_t hash = 5831;
  2162. const PoolVector<Vector3> &arr = *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  2163. int len = arr.size();
  2164. if (likely(len)) {
  2165. PoolVector<Vector3>::Read r = arr.read();
  2166. for (int i = 0; i < len; i++) {
  2167. hash = hash_djb2_one_float(r[i].x, hash);
  2168. hash = hash_djb2_one_float(r[i].y, hash);
  2169. hash = hash_djb2_one_float(r[i].z, hash);
  2170. }
  2171. }
  2172. return hash;
  2173. } break;
  2174. case POOL_COLOR_ARRAY: {
  2175. uint32_t hash = 5831;
  2176. const PoolVector<Color> &arr = *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  2177. int len = arr.size();
  2178. if (likely(len)) {
  2179. PoolVector<Color>::Read r = arr.read();
  2180. for (int i = 0; i < len; i++) {
  2181. hash = hash_djb2_one_float(r[i].r, hash);
  2182. hash = hash_djb2_one_float(r[i].g, hash);
  2183. hash = hash_djb2_one_float(r[i].b, hash);
  2184. hash = hash_djb2_one_float(r[i].a, hash);
  2185. }
  2186. }
  2187. return hash;
  2188. } break;
  2189. default: {
  2190. }
  2191. }
  2192. return 0;
  2193. }
  2194. #define hash_compare_scalar(p_lhs, p_rhs) \
  2195. ((p_lhs) == (p_rhs)) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs))
  2196. #define hash_compare_vector2(p_lhs, p_rhs) \
  2197. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2198. (hash_compare_scalar((p_lhs).y, (p_rhs).y))
  2199. #define hash_compare_vector3(p_lhs, p_rhs) \
  2200. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2201. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2202. (hash_compare_scalar((p_lhs).z, (p_rhs).z))
  2203. #define hash_compare_quat(p_lhs, p_rhs) \
  2204. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2205. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2206. (hash_compare_scalar((p_lhs).z, (p_rhs).z)) && \
  2207. (hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2208. #define hash_compare_color(p_lhs, p_rhs) \
  2209. (hash_compare_scalar((p_lhs).r, (p_rhs).r)) && \
  2210. (hash_compare_scalar((p_lhs).g, (p_rhs).g)) && \
  2211. (hash_compare_scalar((p_lhs).b, (p_rhs).b)) && \
  2212. (hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2213. #define hash_compare_pool_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2214. const PoolVector<p_type> &l = *reinterpret_cast<const PoolVector<p_type> *>(p_lhs); \
  2215. const PoolVector<p_type> &r = *reinterpret_cast<const PoolVector<p_type> *>(p_rhs); \
  2216. \
  2217. if (l.size() != r.size()) \
  2218. return false; \
  2219. \
  2220. PoolVector<p_type>::Read lr = l.read(); \
  2221. PoolVector<p_type>::Read rr = r.read(); \
  2222. \
  2223. for (int i = 0; i < l.size(); ++i) { \
  2224. if (!p_compare_func((lr[i]), (rr[i]))) \
  2225. return false; \
  2226. } \
  2227. \
  2228. return true
  2229. bool Variant::hash_compare(const Variant &p_variant) const {
  2230. if (type != p_variant.type)
  2231. return false;
  2232. switch (type) {
  2233. case REAL: {
  2234. return hash_compare_scalar(_data._real, p_variant._data._real);
  2235. } break;
  2236. case VECTOR2: {
  2237. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2238. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2239. return hash_compare_vector2(*l, *r);
  2240. } break;
  2241. case RECT2: {
  2242. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2243. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2244. return (hash_compare_vector2(l->position, r->position)) &&
  2245. (hash_compare_vector2(l->size, r->size));
  2246. } break;
  2247. case TRANSFORM2D: {
  2248. Transform2D *l = _data._transform2d;
  2249. Transform2D *r = p_variant._data._transform2d;
  2250. for (int i = 0; i < 3; i++) {
  2251. if (!(hash_compare_vector2(l->elements[i], r->elements[i])))
  2252. return false;
  2253. }
  2254. return true;
  2255. } break;
  2256. case VECTOR3: {
  2257. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2258. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2259. return hash_compare_vector3(*l, *r);
  2260. } break;
  2261. case PLANE: {
  2262. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  2263. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  2264. return (hash_compare_vector3(l->normal, r->normal)) &&
  2265. (hash_compare_scalar(l->d, r->d));
  2266. } break;
  2267. case AABB: {
  2268. const ::AABB *l = _data._aabb;
  2269. const ::AABB *r = p_variant._data._aabb;
  2270. return (hash_compare_vector3(l->position, r->position) &&
  2271. (hash_compare_vector3(l->size, r->size)));
  2272. } break;
  2273. case QUAT: {
  2274. const Quat *l = reinterpret_cast<const Quat *>(_data._mem);
  2275. const Quat *r = reinterpret_cast<const Quat *>(p_variant._data._mem);
  2276. return hash_compare_quat(*l, *r);
  2277. } break;
  2278. case BASIS: {
  2279. const Basis *l = _data._basis;
  2280. const Basis *r = p_variant._data._basis;
  2281. for (int i = 0; i < 3; i++) {
  2282. if (!(hash_compare_vector3(l->elements[i], r->elements[i])))
  2283. return false;
  2284. }
  2285. return true;
  2286. } break;
  2287. case TRANSFORM: {
  2288. const Transform *l = _data._transform;
  2289. const Transform *r = p_variant._data._transform;
  2290. for (int i = 0; i < 3; i++) {
  2291. if (!(hash_compare_vector3(l->basis.elements[i], r->basis.elements[i])))
  2292. return false;
  2293. }
  2294. return hash_compare_vector3(l->origin, r->origin);
  2295. } break;
  2296. case COLOR: {
  2297. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  2298. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  2299. return hash_compare_color(*l, *r);
  2300. } break;
  2301. case ARRAY: {
  2302. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  2303. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  2304. if (l.size() != r.size())
  2305. return false;
  2306. for (int i = 0; i < l.size(); ++i) {
  2307. if (!l[i].hash_compare(r[i]))
  2308. return false;
  2309. }
  2310. return true;
  2311. } break;
  2312. case POOL_REAL_ARRAY: {
  2313. hash_compare_pool_array(_data._mem, p_variant._data._mem, real_t, hash_compare_scalar);
  2314. } break;
  2315. case POOL_VECTOR2_ARRAY: {
  2316. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector2, hash_compare_vector2);
  2317. } break;
  2318. case POOL_VECTOR3_ARRAY: {
  2319. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector3, hash_compare_vector3);
  2320. } break;
  2321. case POOL_COLOR_ARRAY: {
  2322. hash_compare_pool_array(_data._mem, p_variant._data._mem, Color, hash_compare_color);
  2323. } break;
  2324. default:
  2325. bool v;
  2326. Variant r;
  2327. evaluate(OP_EQUAL, *this, p_variant, r, v);
  2328. return r;
  2329. }
  2330. return false;
  2331. }
  2332. bool Variant::is_ref() const {
  2333. return type == OBJECT && !_get_obj().ref.is_null();
  2334. }
  2335. Vector<Variant> varray() {
  2336. return Vector<Variant>();
  2337. }
  2338. Vector<Variant> varray(const Variant &p_arg1) {
  2339. Vector<Variant> v;
  2340. v.push_back(p_arg1);
  2341. return v;
  2342. }
  2343. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2) {
  2344. Vector<Variant> v;
  2345. v.push_back(p_arg1);
  2346. v.push_back(p_arg2);
  2347. return v;
  2348. }
  2349. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3) {
  2350. Vector<Variant> v;
  2351. v.push_back(p_arg1);
  2352. v.push_back(p_arg2);
  2353. v.push_back(p_arg3);
  2354. return v;
  2355. }
  2356. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4) {
  2357. Vector<Variant> v;
  2358. v.push_back(p_arg1);
  2359. v.push_back(p_arg2);
  2360. v.push_back(p_arg3);
  2361. v.push_back(p_arg4);
  2362. return v;
  2363. }
  2364. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5) {
  2365. Vector<Variant> v;
  2366. v.push_back(p_arg1);
  2367. v.push_back(p_arg2);
  2368. v.push_back(p_arg3);
  2369. v.push_back(p_arg4);
  2370. v.push_back(p_arg5);
  2371. return v;
  2372. }
  2373. void Variant::static_assign(const Variant &p_variant) {
  2374. }
  2375. bool Variant::is_shared() const {
  2376. switch (type) {
  2377. case OBJECT: return true;
  2378. case ARRAY: return true;
  2379. case DICTIONARY: return true;
  2380. default: {
  2381. }
  2382. }
  2383. return false;
  2384. }
  2385. Variant Variant::call(const StringName &p_method, VARIANT_ARG_DECLARE) {
  2386. VARIANT_ARGPTRS;
  2387. int argc = 0;
  2388. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  2389. if (argptr[i]->get_type() == Variant::NIL)
  2390. break;
  2391. argc++;
  2392. }
  2393. CallError error;
  2394. Variant ret = call(p_method, argptr, argc, error);
  2395. switch (error.error) {
  2396. case CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2397. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(error.expected) + "'.";
  2398. ERR_PRINT(err.utf8().get_data());
  2399. } break;
  2400. case CallError::CALL_ERROR_INVALID_METHOD: {
  2401. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  2402. ERR_PRINT(err.utf8().get_data());
  2403. } break;
  2404. case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  2405. String err = "Too many arguments for method '" + p_method + "'";
  2406. ERR_PRINT(err.utf8().get_data());
  2407. } break;
  2408. default: {
  2409. }
  2410. }
  2411. return ret;
  2412. }
  2413. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  2414. r_value = Variant();
  2415. }
  2416. String Variant::get_construct_string() const {
  2417. String vars;
  2418. VariantWriter::write_to_string(*this, vars);
  2419. return vars;
  2420. }
  2421. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Variant::CallError &ce) {
  2422. String err_text;
  2423. if (ce.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  2424. int errorarg = ce.argument;
  2425. if (p_argptrs) {
  2426. 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) + ".";
  2427. } else {
  2428. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(ce.expected) + ".";
  2429. }
  2430. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  2431. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2432. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  2433. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2434. } else if (ce.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  2435. err_text = "Method not found.";
  2436. } else if (ce.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  2437. err_text = "Instance is null";
  2438. } else if (ce.error == Variant::CallError::CALL_OK) {
  2439. return "Call OK";
  2440. }
  2441. String class_name = p_base->get_class();
  2442. Ref<Script> script = p_base->get_script();
  2443. if (script.is_valid() && script->get_path().is_resource_file()) {
  2444. class_name += "(" + script->get_path().get_file() + ")";
  2445. }
  2446. return "'" + class_name + "::" + String(p_method) + "': " + err_text;
  2447. }
  2448. String vformat(const String &p_text, const Variant &p1, const Variant &p2, const Variant &p3, const Variant &p4, const Variant &p5) {
  2449. Array args;
  2450. if (p1.get_type() != Variant::NIL) {
  2451. args.push_back(p1);
  2452. if (p2.get_type() != Variant::NIL) {
  2453. args.push_back(p2);
  2454. if (p3.get_type() != Variant::NIL) {
  2455. args.push_back(p3);
  2456. if (p4.get_type() != Variant::NIL) {
  2457. args.push_back(p4);
  2458. if (p5.get_type() != Variant::NIL) {
  2459. args.push_back(p5);
  2460. }
  2461. }
  2462. }
  2463. }
  2464. }
  2465. bool error = false;
  2466. String fmt = p_text.sprintf(args, &error);
  2467. ERR_FAIL_COND_V(error, String());
  2468. return fmt;
  2469. }