variant.cpp 70 KB

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