variant.cpp 70 KB

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