variant.cpp 69 KB

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