variant.cpp 70 KB

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