variant.cpp 70 KB

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