variant_parser.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513
  1. /**************************************************************************/
  2. /* variant_parser.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_parser.h"
  31. #include "core/crypto/crypto_core.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/object/script_language.h"
  34. #include "core/string/string_buffer.h"
  35. char32_t VariantParser::Stream::get_char() {
  36. // is within buffer?
  37. if (readahead_pointer < readahead_filled) {
  38. return readahead_buffer[readahead_pointer++];
  39. }
  40. // attempt to readahead
  41. readahead_filled = _read_buffer(readahead_buffer, readahead_enabled ? READAHEAD_SIZE : 1);
  42. if (readahead_filled) {
  43. readahead_pointer = 0;
  44. } else {
  45. // EOF
  46. readahead_pointer = 1;
  47. eof = true;
  48. return 0;
  49. }
  50. return get_char();
  51. }
  52. bool VariantParser::Stream::is_eof() const {
  53. if (readahead_enabled) {
  54. return eof;
  55. }
  56. return _is_eof();
  57. }
  58. bool VariantParser::StreamFile::is_utf8() const {
  59. return true;
  60. }
  61. bool VariantParser::StreamFile::_is_eof() const {
  62. return f->eof_reached();
  63. }
  64. uint32_t VariantParser::StreamFile::_read_buffer(char32_t *p_buffer, uint32_t p_num_chars) {
  65. // The buffer is assumed to include at least one character (for null terminator)
  66. ERR_FAIL_COND_V(!p_num_chars, 0);
  67. uint8_t *temp = (uint8_t *)alloca(p_num_chars);
  68. uint64_t num_read = f->get_buffer(temp, p_num_chars);
  69. ERR_FAIL_COND_V(num_read == UINT64_MAX, 0);
  70. // translate to wchar
  71. for (uint32_t n = 0; n < num_read; n++) {
  72. p_buffer[n] = temp[n];
  73. }
  74. // could be less than p_num_chars, or zero
  75. return num_read;
  76. }
  77. bool VariantParser::StreamString::is_utf8() const {
  78. return false;
  79. }
  80. bool VariantParser::StreamString::_is_eof() const {
  81. return pos > s.length();
  82. }
  83. uint32_t VariantParser::StreamString::_read_buffer(char32_t *p_buffer, uint32_t p_num_chars) {
  84. // The buffer is assumed to include at least one character (for null terminator)
  85. ERR_FAIL_COND_V(!p_num_chars, 0);
  86. ERR_FAIL_NULL_V(p_buffer, 0);
  87. int available = MAX(s.length() - pos, 0);
  88. if (available >= (int)p_num_chars) {
  89. const char32_t *src = s.ptr();
  90. src += pos;
  91. memcpy(p_buffer, src, p_num_chars * sizeof(char32_t));
  92. pos += p_num_chars;
  93. return p_num_chars;
  94. }
  95. // going to reach EOF
  96. if (available) {
  97. const char32_t *src = s.ptr();
  98. src += pos;
  99. memcpy(p_buffer, src, available * sizeof(char32_t));
  100. pos += available;
  101. }
  102. // add a zero
  103. p_buffer[available] = 0;
  104. return available;
  105. }
  106. /////////////////////////////////////////////////////////////////////////////////////////////////
  107. const char *VariantParser::tk_name[TK_MAX] = {
  108. "'{'",
  109. "'}'",
  110. "'['",
  111. "']'",
  112. "'('",
  113. "')'",
  114. "identifier",
  115. "string",
  116. "string_name",
  117. "number",
  118. "color",
  119. "':'",
  120. "','",
  121. "'.'",
  122. "'='",
  123. "EOF",
  124. "ERROR"
  125. };
  126. static double stor_fix(const String &p_str) {
  127. if (p_str == "inf") {
  128. return INFINITY;
  129. } else if (p_str == "-inf" || p_str == "inf_neg") {
  130. // inf_neg kept for compatibility.
  131. return -INFINITY;
  132. } else if (p_str == "nan") {
  133. return NAN;
  134. }
  135. return -1;
  136. }
  137. Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, String &r_err_str) {
  138. bool string_name = false;
  139. while (true) {
  140. char32_t cchar;
  141. if (p_stream->saved) {
  142. cchar = p_stream->saved;
  143. p_stream->saved = 0;
  144. } else {
  145. cchar = p_stream->get_char();
  146. if (p_stream->is_eof()) {
  147. r_token.type = TK_EOF;
  148. return OK;
  149. }
  150. }
  151. switch (cchar) {
  152. case '\n': {
  153. line++;
  154. break;
  155. }
  156. case 0: {
  157. r_token.type = TK_EOF;
  158. return OK;
  159. } break;
  160. case '{': {
  161. r_token.type = TK_CURLY_BRACKET_OPEN;
  162. return OK;
  163. }
  164. case '}': {
  165. r_token.type = TK_CURLY_BRACKET_CLOSE;
  166. return OK;
  167. }
  168. case '[': {
  169. r_token.type = TK_BRACKET_OPEN;
  170. return OK;
  171. }
  172. case ']': {
  173. r_token.type = TK_BRACKET_CLOSE;
  174. return OK;
  175. }
  176. case '(': {
  177. r_token.type = TK_PARENTHESIS_OPEN;
  178. return OK;
  179. }
  180. case ')': {
  181. r_token.type = TK_PARENTHESIS_CLOSE;
  182. return OK;
  183. }
  184. case ':': {
  185. r_token.type = TK_COLON;
  186. return OK;
  187. }
  188. case ';': {
  189. while (true) {
  190. char32_t ch = p_stream->get_char();
  191. if (p_stream->is_eof()) {
  192. r_token.type = TK_EOF;
  193. return OK;
  194. }
  195. if (ch == '\n') {
  196. line++;
  197. break;
  198. }
  199. }
  200. break;
  201. }
  202. case ',': {
  203. r_token.type = TK_COMMA;
  204. return OK;
  205. }
  206. case '.': {
  207. r_token.type = TK_PERIOD;
  208. return OK;
  209. }
  210. case '=': {
  211. r_token.type = TK_EQUAL;
  212. return OK;
  213. }
  214. case '#': {
  215. StringBuffer<> color_str;
  216. color_str += '#';
  217. while (true) {
  218. char32_t ch = p_stream->get_char();
  219. if (p_stream->is_eof()) {
  220. r_token.type = TK_EOF;
  221. return OK;
  222. } else if (is_hex_digit(ch)) {
  223. color_str += ch;
  224. } else {
  225. p_stream->saved = ch;
  226. break;
  227. }
  228. }
  229. r_token.value = Color::html(color_str.as_string());
  230. r_token.type = TK_COLOR;
  231. return OK;
  232. }
  233. #ifndef DISABLE_DEPRECATED
  234. case '@': // Compatibility with 3.x StringNames.
  235. #endif
  236. case '&': { // StringName.
  237. cchar = p_stream->get_char();
  238. if (cchar != '"') {
  239. r_err_str = "Expected '\"' after '&'";
  240. r_token.type = TK_ERROR;
  241. return ERR_PARSE_ERROR;
  242. }
  243. string_name = true;
  244. [[fallthrough]];
  245. }
  246. case '"': {
  247. String str;
  248. char32_t prev = 0;
  249. while (true) {
  250. char32_t ch = p_stream->get_char();
  251. if (ch == 0) {
  252. r_err_str = "Unterminated string";
  253. r_token.type = TK_ERROR;
  254. return ERR_PARSE_ERROR;
  255. } else if (ch == '"') {
  256. break;
  257. } else if (ch == '\\') {
  258. //escaped characters...
  259. char32_t next = p_stream->get_char();
  260. if (next == 0) {
  261. r_err_str = "Unterminated string";
  262. r_token.type = TK_ERROR;
  263. return ERR_PARSE_ERROR;
  264. }
  265. char32_t res = 0;
  266. switch (next) {
  267. case 'b':
  268. res = 8;
  269. break;
  270. case 't':
  271. res = 9;
  272. break;
  273. case 'n':
  274. res = 10;
  275. break;
  276. case 'f':
  277. res = 12;
  278. break;
  279. case 'r':
  280. res = 13;
  281. break;
  282. case 'U':
  283. case 'u': {
  284. // Hexadecimal sequence.
  285. int hex_len = (next == 'U') ? 6 : 4;
  286. for (int j = 0; j < hex_len; j++) {
  287. char32_t c = p_stream->get_char();
  288. if (c == 0) {
  289. r_err_str = "Unterminated string";
  290. r_token.type = TK_ERROR;
  291. return ERR_PARSE_ERROR;
  292. }
  293. if (!is_hex_digit(c)) {
  294. r_err_str = "Malformed hex constant in string";
  295. r_token.type = TK_ERROR;
  296. return ERR_PARSE_ERROR;
  297. }
  298. char32_t v;
  299. if (is_digit(c)) {
  300. v = c - '0';
  301. } else if (c >= 'a' && c <= 'f') {
  302. v = c - 'a';
  303. v += 10;
  304. } else if (c >= 'A' && c <= 'F') {
  305. v = c - 'A';
  306. v += 10;
  307. } else {
  308. ERR_PRINT("Bug parsing hex constant.");
  309. v = 0;
  310. }
  311. res <<= 4;
  312. res |= v;
  313. }
  314. } break;
  315. default: {
  316. res = next;
  317. } break;
  318. }
  319. // Parse UTF-16 pair.
  320. if ((res & 0xfffffc00) == 0xd800) {
  321. if (prev == 0) {
  322. prev = res;
  323. continue;
  324. } else {
  325. r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
  326. r_token.type = TK_ERROR;
  327. return ERR_PARSE_ERROR;
  328. }
  329. } else if ((res & 0xfffffc00) == 0xdc00) {
  330. if (prev == 0) {
  331. r_err_str = "Invalid UTF-16 sequence in string, unpaired trail surrogate";
  332. r_token.type = TK_ERROR;
  333. return ERR_PARSE_ERROR;
  334. } else {
  335. res = (prev << 10UL) + res - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
  336. prev = 0;
  337. }
  338. }
  339. if (prev != 0) {
  340. r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
  341. r_token.type = TK_ERROR;
  342. return ERR_PARSE_ERROR;
  343. }
  344. str += res;
  345. } else {
  346. if (prev != 0) {
  347. r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
  348. r_token.type = TK_ERROR;
  349. return ERR_PARSE_ERROR;
  350. }
  351. if (ch == '\n') {
  352. line++;
  353. }
  354. str += ch;
  355. }
  356. }
  357. if (prev != 0) {
  358. r_err_str = "Invalid UTF-16 sequence in string, unpaired lead surrogate";
  359. r_token.type = TK_ERROR;
  360. return ERR_PARSE_ERROR;
  361. }
  362. if (p_stream->is_utf8()) {
  363. str.parse_utf8(str.ascii(true).get_data());
  364. }
  365. if (string_name) {
  366. r_token.type = TK_STRING_NAME;
  367. r_token.value = StringName(str);
  368. } else {
  369. r_token.type = TK_STRING;
  370. r_token.value = str;
  371. }
  372. return OK;
  373. } break;
  374. default: {
  375. if (cchar <= 32) {
  376. break;
  377. }
  378. StringBuffer<> token_text;
  379. if (cchar == '-') {
  380. token_text += '-';
  381. cchar = p_stream->get_char();
  382. }
  383. if (cchar >= '0' && cchar <= '9') {
  384. //a number
  385. #define READING_SIGN 0
  386. #define READING_INT 1
  387. #define READING_DEC 2
  388. #define READING_EXP 3
  389. #define READING_DONE 4
  390. int reading = READING_INT;
  391. char32_t c = cchar;
  392. bool exp_sign = false;
  393. bool exp_beg = false;
  394. bool is_float = false;
  395. while (true) {
  396. switch (reading) {
  397. case READING_INT: {
  398. if (is_digit(c)) {
  399. //pass
  400. } else if (c == '.') {
  401. reading = READING_DEC;
  402. is_float = true;
  403. } else if (c == 'e' || c == 'E') {
  404. reading = READING_EXP;
  405. is_float = true;
  406. } else {
  407. reading = READING_DONE;
  408. }
  409. } break;
  410. case READING_DEC: {
  411. if (is_digit(c)) {
  412. } else if (c == 'e' || c == 'E') {
  413. reading = READING_EXP;
  414. } else {
  415. reading = READING_DONE;
  416. }
  417. } break;
  418. case READING_EXP: {
  419. if (is_digit(c)) {
  420. exp_beg = true;
  421. } else if ((c == '-' || c == '+') && !exp_sign && !exp_beg) {
  422. exp_sign = true;
  423. } else {
  424. reading = READING_DONE;
  425. }
  426. } break;
  427. }
  428. if (reading == READING_DONE) {
  429. break;
  430. }
  431. token_text += c;
  432. c = p_stream->get_char();
  433. }
  434. p_stream->saved = c;
  435. r_token.type = TK_NUMBER;
  436. if (is_float) {
  437. r_token.value = token_text.as_double();
  438. } else {
  439. r_token.value = token_text.as_int();
  440. }
  441. return OK;
  442. } else if (is_ascii_alphabet_char(cchar) || is_underscore(cchar)) {
  443. bool first = true;
  444. while (is_ascii_alphabet_char(cchar) || is_underscore(cchar) || (!first && is_digit(cchar))) {
  445. token_text += cchar;
  446. cchar = p_stream->get_char();
  447. first = false;
  448. }
  449. p_stream->saved = cchar;
  450. r_token.type = TK_IDENTIFIER;
  451. r_token.value = token_text.as_string();
  452. return OK;
  453. } else {
  454. r_err_str = "Unexpected character";
  455. r_token.type = TK_ERROR;
  456. return ERR_PARSE_ERROR;
  457. }
  458. }
  459. }
  460. }
  461. r_err_str = "Unknown error getting token";
  462. r_token.type = TK_ERROR;
  463. return ERR_PARSE_ERROR;
  464. }
  465. Error VariantParser::_parse_enginecfg(Stream *p_stream, Vector<String> &strings, int &line, String &r_err_str) {
  466. Token token;
  467. get_token(p_stream, token, line, r_err_str);
  468. if (token.type != TK_PARENTHESIS_OPEN) {
  469. r_err_str = "Expected '(' in old-style project.godot construct";
  470. return ERR_PARSE_ERROR;
  471. }
  472. String accum;
  473. while (true) {
  474. char32_t c = p_stream->get_char();
  475. if (p_stream->is_eof()) {
  476. r_err_str = "Unexpected EOF while parsing old-style project.godot construct";
  477. return ERR_PARSE_ERROR;
  478. }
  479. if (c == ',') {
  480. strings.push_back(accum.strip_edges());
  481. accum = String();
  482. } else if (c == ')') {
  483. strings.push_back(accum.strip_edges());
  484. return OK;
  485. } else if (c == '\n') {
  486. line++;
  487. }
  488. }
  489. }
  490. template <typename T>
  491. Error VariantParser::_parse_construct(Stream *p_stream, Vector<T> &r_construct, int &line, String &r_err_str) {
  492. Token token;
  493. get_token(p_stream, token, line, r_err_str);
  494. if (token.type != TK_PARENTHESIS_OPEN) {
  495. r_err_str = "Expected '(' in constructor";
  496. return ERR_PARSE_ERROR;
  497. }
  498. bool first = true;
  499. while (true) {
  500. if (!first) {
  501. get_token(p_stream, token, line, r_err_str);
  502. if (token.type == TK_COMMA) {
  503. //do none
  504. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  505. break;
  506. } else {
  507. r_err_str = "Expected ',' or ')' in constructor";
  508. return ERR_PARSE_ERROR;
  509. }
  510. }
  511. get_token(p_stream, token, line, r_err_str);
  512. if (first && token.type == TK_PARENTHESIS_CLOSE) {
  513. break;
  514. } else if (token.type != TK_NUMBER) {
  515. bool valid = false;
  516. if (token.type == TK_IDENTIFIER) {
  517. double real = stor_fix(token.value);
  518. if (real != -1) {
  519. token.type = TK_NUMBER;
  520. token.value = real;
  521. valid = true;
  522. }
  523. }
  524. if (!valid) {
  525. r_err_str = "Expected float in constructor";
  526. return ERR_PARSE_ERROR;
  527. }
  528. }
  529. r_construct.push_back(token.value);
  530. first = false;
  531. }
  532. return OK;
  533. }
  534. Error VariantParser::_parse_byte_array(Stream *p_stream, Vector<uint8_t> &r_construct, int &line, String &r_err_str) {
  535. Token token;
  536. get_token(p_stream, token, line, r_err_str);
  537. if (token.type != TK_PARENTHESIS_OPEN) {
  538. r_err_str = "Expected '(' in constructor";
  539. return ERR_PARSE_ERROR;
  540. }
  541. get_token(p_stream, token, line, r_err_str);
  542. if (token.type == TK_STRING) {
  543. // Base64 encoded array.
  544. String base64_encoded_string = token.value;
  545. int strlen = base64_encoded_string.length();
  546. CharString cstr = base64_encoded_string.ascii();
  547. size_t arr_len = 0;
  548. r_construct.resize(strlen / 4 * 3 + 1);
  549. uint8_t *w = r_construct.ptrw();
  550. Error err = CryptoCore::b64_decode(&w[0], r_construct.size(), &arr_len, (unsigned char *)cstr.get_data(), strlen);
  551. if (err) {
  552. r_err_str = "Invalid base64-encoded string";
  553. return ERR_PARSE_ERROR;
  554. }
  555. r_construct.resize(arr_len);
  556. get_token(p_stream, token, line, r_err_str);
  557. if (token.type != TK_PARENTHESIS_CLOSE) {
  558. r_err_str = "Expected ')' in constructor";
  559. return ERR_PARSE_ERROR;
  560. }
  561. } else if (token.type == TK_NUMBER || token.type == TK_IDENTIFIER) {
  562. // Individual elements.
  563. while (true) {
  564. if (token.type != TK_NUMBER) {
  565. bool valid = false;
  566. if (token.type == TK_IDENTIFIER) {
  567. double real = stor_fix(token.value);
  568. if (real != -1) {
  569. token.type = TK_NUMBER;
  570. token.value = real;
  571. valid = true;
  572. }
  573. }
  574. if (!valid) {
  575. r_err_str = "Expected number in constructor";
  576. return ERR_PARSE_ERROR;
  577. }
  578. }
  579. r_construct.push_back(token.value);
  580. get_token(p_stream, token, line, r_err_str);
  581. if (token.type == TK_COMMA) {
  582. //do none
  583. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  584. break;
  585. } else {
  586. r_err_str = "Expected ',' or ')' in constructor";
  587. return ERR_PARSE_ERROR;
  588. }
  589. get_token(p_stream, token, line, r_err_str);
  590. }
  591. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  592. // Empty array.
  593. return OK;
  594. } else {
  595. r_err_str = "Expected base64 string, or list of numbers in constructor";
  596. return ERR_PARSE_ERROR;
  597. }
  598. return OK;
  599. }
  600. Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  601. if (token.type == TK_CURLY_BRACKET_OPEN) {
  602. Dictionary d;
  603. Error err = _parse_dictionary(d, p_stream, line, r_err_str, p_res_parser);
  604. if (err) {
  605. return err;
  606. }
  607. value = d;
  608. return OK;
  609. } else if (token.type == TK_BRACKET_OPEN) {
  610. Array a;
  611. Error err = _parse_array(a, p_stream, line, r_err_str, p_res_parser);
  612. if (err) {
  613. return err;
  614. }
  615. value = a;
  616. return OK;
  617. } else if (token.type == TK_IDENTIFIER) {
  618. String id = token.value;
  619. if (id == "true") {
  620. value = true;
  621. } else if (id == "false") {
  622. value = false;
  623. } else if (id == "null" || id == "nil") {
  624. value = Variant();
  625. } else if (id == "inf") {
  626. value = INFINITY;
  627. } else if (id == "-inf" || id == "inf_neg") {
  628. // inf_neg kept for compatibility.
  629. value = -INFINITY;
  630. } else if (id == "nan") {
  631. value = NAN;
  632. } else if (id == "Vector2") {
  633. Vector<real_t> args;
  634. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  635. if (err) {
  636. return err;
  637. }
  638. if (args.size() != 2) {
  639. r_err_str = "Expected 2 arguments for constructor";
  640. return ERR_PARSE_ERROR;
  641. }
  642. value = Vector2(args[0], args[1]);
  643. } else if (id == "Vector2i") {
  644. Vector<int32_t> args;
  645. Error err = _parse_construct<int32_t>(p_stream, args, line, r_err_str);
  646. if (err) {
  647. return err;
  648. }
  649. if (args.size() != 2) {
  650. r_err_str = "Expected 2 arguments for constructor";
  651. return ERR_PARSE_ERROR;
  652. }
  653. value = Vector2i(args[0], args[1]);
  654. } else if (id == "Rect2") {
  655. Vector<real_t> args;
  656. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  657. if (err) {
  658. return err;
  659. }
  660. if (args.size() != 4) {
  661. r_err_str = "Expected 4 arguments for constructor";
  662. return ERR_PARSE_ERROR;
  663. }
  664. value = Rect2(args[0], args[1], args[2], args[3]);
  665. } else if (id == "Rect2i") {
  666. Vector<int32_t> args;
  667. Error err = _parse_construct<int32_t>(p_stream, args, line, r_err_str);
  668. if (err) {
  669. return err;
  670. }
  671. if (args.size() != 4) {
  672. r_err_str = "Expected 4 arguments for constructor";
  673. return ERR_PARSE_ERROR;
  674. }
  675. value = Rect2i(args[0], args[1], args[2], args[3]);
  676. } else if (id == "Vector3") {
  677. Vector<real_t> args;
  678. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  679. if (err) {
  680. return err;
  681. }
  682. if (args.size() != 3) {
  683. r_err_str = "Expected 3 arguments for constructor";
  684. return ERR_PARSE_ERROR;
  685. }
  686. value = Vector3(args[0], args[1], args[2]);
  687. } else if (id == "Vector3i") {
  688. Vector<int32_t> args;
  689. Error err = _parse_construct<int32_t>(p_stream, args, line, r_err_str);
  690. if (err) {
  691. return err;
  692. }
  693. if (args.size() != 3) {
  694. r_err_str = "Expected 3 arguments for constructor";
  695. return ERR_PARSE_ERROR;
  696. }
  697. value = Vector3i(args[0], args[1], args[2]);
  698. } else if (id == "Vector4") {
  699. Vector<real_t> args;
  700. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  701. if (err) {
  702. return err;
  703. }
  704. if (args.size() != 4) {
  705. r_err_str = "Expected 4 arguments for constructor";
  706. return ERR_PARSE_ERROR;
  707. }
  708. value = Vector4(args[0], args[1], args[2], args[3]);
  709. } else if (id == "Vector4i") {
  710. Vector<int32_t> args;
  711. Error err = _parse_construct<int32_t>(p_stream, args, line, r_err_str);
  712. if (err) {
  713. return err;
  714. }
  715. if (args.size() != 4) {
  716. r_err_str = "Expected 4 arguments for constructor";
  717. return ERR_PARSE_ERROR;
  718. }
  719. value = Vector4i(args[0], args[1], args[2], args[3]);
  720. } else if (id == "Transform2D" || id == "Matrix32") { //compatibility
  721. Vector<real_t> args;
  722. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  723. if (err) {
  724. return err;
  725. }
  726. if (args.size() != 6) {
  727. r_err_str = "Expected 6 arguments for constructor";
  728. return ERR_PARSE_ERROR;
  729. }
  730. Transform2D m;
  731. m[0] = Vector2(args[0], args[1]);
  732. m[1] = Vector2(args[2], args[3]);
  733. m[2] = Vector2(args[4], args[5]);
  734. value = m;
  735. } else if (id == "Plane") {
  736. Vector<real_t> args;
  737. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  738. if (err) {
  739. return err;
  740. }
  741. if (args.size() != 4) {
  742. r_err_str = "Expected 4 arguments for constructor";
  743. return ERR_PARSE_ERROR;
  744. }
  745. value = Plane(args[0], args[1], args[2], args[3]);
  746. } else if (id == "Quaternion" || id == "Quat") { // "Quat" kept for compatibility
  747. Vector<real_t> args;
  748. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  749. if (err) {
  750. return err;
  751. }
  752. if (args.size() != 4) {
  753. r_err_str = "Expected 4 arguments for constructor";
  754. return ERR_PARSE_ERROR;
  755. }
  756. value = Quaternion(args[0], args[1], args[2], args[3]);
  757. } else if (id == "AABB" || id == "Rect3") {
  758. Vector<real_t> args;
  759. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  760. if (err) {
  761. return err;
  762. }
  763. if (args.size() != 6) {
  764. r_err_str = "Expected 6 arguments for constructor";
  765. return ERR_PARSE_ERROR;
  766. }
  767. value = AABB(Vector3(args[0], args[1], args[2]), Vector3(args[3], args[4], args[5]));
  768. } else if (id == "Basis" || id == "Matrix3") { //compatibility
  769. Vector<real_t> args;
  770. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  771. if (err) {
  772. return err;
  773. }
  774. if (args.size() != 9) {
  775. r_err_str = "Expected 9 arguments for constructor";
  776. return ERR_PARSE_ERROR;
  777. }
  778. value = Basis(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
  779. } else if (id == "Transform3D" || id == "Transform") { // "Transform" kept for compatibility with Godot <4.
  780. Vector<real_t> args;
  781. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  782. if (err) {
  783. return err;
  784. }
  785. if (args.size() != 12) {
  786. r_err_str = "Expected 12 arguments for constructor";
  787. return ERR_PARSE_ERROR;
  788. }
  789. value = Transform3D(Basis(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]), Vector3(args[9], args[10], args[11]));
  790. } else if (id == "Projection") { // "Transform" kept for compatibility with Godot <4.
  791. Vector<real_t> args;
  792. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  793. if (err) {
  794. return err;
  795. }
  796. if (args.size() != 16) {
  797. r_err_str = "Expected 16 arguments for constructor";
  798. return ERR_PARSE_ERROR;
  799. }
  800. value = Projection(Vector4(args[0], args[1], args[2], args[3]), Vector4(args[4], args[5], args[6], args[7]), Vector4(args[8], args[9], args[10], args[11]), Vector4(args[12], args[13], args[14], args[15]));
  801. } else if (id == "Color") {
  802. Vector<float> args;
  803. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  804. if (err) {
  805. return err;
  806. }
  807. if (args.size() != 4) {
  808. r_err_str = "Expected 4 arguments for constructor";
  809. return ERR_PARSE_ERROR;
  810. }
  811. value = Color(args[0], args[1], args[2], args[3]);
  812. } else if (id == "NodePath") {
  813. get_token(p_stream, token, line, r_err_str);
  814. if (token.type != TK_PARENTHESIS_OPEN) {
  815. r_err_str = "Expected '('";
  816. return ERR_PARSE_ERROR;
  817. }
  818. get_token(p_stream, token, line, r_err_str);
  819. if (token.type != TK_STRING) {
  820. r_err_str = "Expected string as argument for NodePath()";
  821. return ERR_PARSE_ERROR;
  822. }
  823. value = NodePath(String(token.value));
  824. get_token(p_stream, token, line, r_err_str);
  825. if (token.type != TK_PARENTHESIS_CLOSE) {
  826. r_err_str = "Expected ')'";
  827. return ERR_PARSE_ERROR;
  828. }
  829. } else if (id == "RID") {
  830. get_token(p_stream, token, line, r_err_str);
  831. if (token.type != TK_PARENTHESIS_OPEN) {
  832. r_err_str = "Expected '('";
  833. return ERR_PARSE_ERROR;
  834. }
  835. get_token(p_stream, token, line, r_err_str);
  836. // Permit empty RID.
  837. if (token.type == TK_PARENTHESIS_CLOSE) {
  838. value = RID();
  839. return OK;
  840. } else if (token.type != TK_NUMBER) {
  841. r_err_str = "Expected number as argument or ')'";
  842. return ERR_PARSE_ERROR;
  843. }
  844. value = RID::from_uint64(token.value);
  845. get_token(p_stream, token, line, r_err_str);
  846. if (token.type != TK_PARENTHESIS_CLOSE) {
  847. r_err_str = "Expected ')'";
  848. return ERR_PARSE_ERROR;
  849. }
  850. } else if (id == "Signal") {
  851. get_token(p_stream, token, line, r_err_str);
  852. if (token.type != TK_PARENTHESIS_OPEN) {
  853. r_err_str = "Expected '('";
  854. return ERR_PARSE_ERROR;
  855. }
  856. // Load as empty.
  857. value = Signal();
  858. get_token(p_stream, token, line, r_err_str);
  859. if (token.type != TK_PARENTHESIS_CLOSE) {
  860. r_err_str = "Expected ')'";
  861. return ERR_PARSE_ERROR;
  862. }
  863. } else if (id == "Callable") {
  864. get_token(p_stream, token, line, r_err_str);
  865. if (token.type != TK_PARENTHESIS_OPEN) {
  866. r_err_str = "Expected '('";
  867. return ERR_PARSE_ERROR;
  868. }
  869. // Load as empty.
  870. value = Callable();
  871. get_token(p_stream, token, line, r_err_str);
  872. if (token.type != TK_PARENTHESIS_CLOSE) {
  873. r_err_str = "Expected ')'";
  874. return ERR_PARSE_ERROR;
  875. }
  876. } else if (id == "Object") {
  877. get_token(p_stream, token, line, r_err_str);
  878. if (token.type != TK_PARENTHESIS_OPEN) {
  879. r_err_str = "Expected '('";
  880. return ERR_PARSE_ERROR;
  881. }
  882. get_token(p_stream, token, line, r_err_str);
  883. if (token.type != TK_IDENTIFIER) {
  884. r_err_str = "Expected identifier with type of object";
  885. return ERR_PARSE_ERROR;
  886. }
  887. String type = token.value;
  888. Object *obj = ClassDB::instantiate(type);
  889. if (!obj) {
  890. r_err_str = vformat("Can't instantiate Object() of type '%s'", type);
  891. return ERR_PARSE_ERROR;
  892. }
  893. Ref<RefCounted> ref = Ref<RefCounted>(Object::cast_to<RefCounted>(obj));
  894. get_token(p_stream, token, line, r_err_str);
  895. if (token.type != TK_COMMA) {
  896. r_err_str = "Expected ',' after object type";
  897. return ERR_PARSE_ERROR;
  898. }
  899. bool at_key = true;
  900. String key;
  901. bool need_comma = false;
  902. while (true) {
  903. if (p_stream->is_eof()) {
  904. r_err_str = "Unexpected EOF while parsing Object()";
  905. return ERR_FILE_CORRUPT;
  906. }
  907. if (at_key) {
  908. Error err = get_token(p_stream, token, line, r_err_str);
  909. if (err != OK) {
  910. return err;
  911. }
  912. if (token.type == TK_PARENTHESIS_CLOSE) {
  913. value = ref.is_valid() ? Variant(ref) : Variant(obj);
  914. return OK;
  915. }
  916. if (need_comma) {
  917. if (token.type != TK_COMMA) {
  918. r_err_str = "Expected '}' or ','";
  919. return ERR_PARSE_ERROR;
  920. } else {
  921. need_comma = false;
  922. continue;
  923. }
  924. }
  925. if (token.type != TK_STRING) {
  926. r_err_str = "Expected property name as string";
  927. return ERR_PARSE_ERROR;
  928. }
  929. key = token.value;
  930. err = get_token(p_stream, token, line, r_err_str);
  931. if (err != OK) {
  932. return err;
  933. }
  934. if (token.type != TK_COLON) {
  935. r_err_str = "Expected ':'";
  936. return ERR_PARSE_ERROR;
  937. }
  938. at_key = false;
  939. } else {
  940. Error err = get_token(p_stream, token, line, r_err_str);
  941. if (err != OK) {
  942. return err;
  943. }
  944. Variant v;
  945. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  946. if (err) {
  947. return err;
  948. }
  949. obj->set(key, v);
  950. need_comma = true;
  951. at_key = true;
  952. }
  953. }
  954. } else if (id == "Resource" || id == "SubResource" || id == "ExtResource") {
  955. get_token(p_stream, token, line, r_err_str);
  956. if (token.type != TK_PARENTHESIS_OPEN) {
  957. r_err_str = "Expected '('";
  958. return ERR_PARSE_ERROR;
  959. }
  960. if (p_res_parser && id == "Resource" && p_res_parser->func) {
  961. Ref<Resource> res;
  962. Error err = p_res_parser->func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  963. if (err) {
  964. return err;
  965. }
  966. value = res;
  967. } else if (p_res_parser && id == "ExtResource" && p_res_parser->ext_func) {
  968. Ref<Resource> res;
  969. Error err = p_res_parser->ext_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  970. if (err) {
  971. // If the file is missing, the error can be ignored.
  972. if (err != ERR_FILE_NOT_FOUND && err != ERR_CANT_OPEN && err != ERR_FILE_CANT_OPEN) {
  973. return err;
  974. }
  975. }
  976. value = res;
  977. } else if (p_res_parser && id == "SubResource" && p_res_parser->sub_func) {
  978. Ref<Resource> res;
  979. Error err = p_res_parser->sub_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  980. if (err) {
  981. return err;
  982. }
  983. value = res;
  984. } else {
  985. get_token(p_stream, token, line, r_err_str);
  986. if (token.type == TK_STRING) {
  987. String path = token.value;
  988. Ref<Resource> res = ResourceLoader::load(path);
  989. if (res.is_null()) {
  990. r_err_str = "Can't load resource at path: " + path;
  991. return ERR_PARSE_ERROR;
  992. }
  993. get_token(p_stream, token, line, r_err_str);
  994. if (token.type != TK_PARENTHESIS_CLOSE) {
  995. r_err_str = "Expected ')'";
  996. return ERR_PARSE_ERROR;
  997. }
  998. value = res;
  999. } else {
  1000. r_err_str = "Expected string as argument for Resource()";
  1001. return ERR_PARSE_ERROR;
  1002. }
  1003. }
  1004. } else if (id == "Dictionary") {
  1005. Error err = OK;
  1006. get_token(p_stream, token, line, r_err_str);
  1007. if (token.type != TK_BRACKET_OPEN) {
  1008. r_err_str = "Expected '['";
  1009. return ERR_PARSE_ERROR;
  1010. }
  1011. get_token(p_stream, token, line, r_err_str);
  1012. if (token.type != TK_IDENTIFIER) {
  1013. r_err_str = "Expected type identifier for key";
  1014. return ERR_PARSE_ERROR;
  1015. }
  1016. static HashMap<StringName, Variant::Type> builtin_types;
  1017. if (builtin_types.is_empty()) {
  1018. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  1019. builtin_types[Variant::get_type_name((Variant::Type)i)] = (Variant::Type)i;
  1020. }
  1021. }
  1022. Dictionary dict;
  1023. Variant::Type key_type = Variant::NIL;
  1024. StringName key_class_name;
  1025. Variant key_script;
  1026. bool got_comma_token = false;
  1027. if (builtin_types.has(token.value)) {
  1028. key_type = builtin_types.get(token.value);
  1029. } else if (token.value == "Resource" || token.value == "SubResource" || token.value == "ExtResource") {
  1030. Variant resource;
  1031. err = parse_value(token, resource, p_stream, line, r_err_str, p_res_parser);
  1032. if (err) {
  1033. if (token.value == "Resource" && err == ERR_PARSE_ERROR && r_err_str == "Expected '('" && token.type == TK_COMMA) {
  1034. err = OK;
  1035. r_err_str = String();
  1036. key_type = Variant::OBJECT;
  1037. key_class_name = token.value;
  1038. got_comma_token = true;
  1039. } else {
  1040. return err;
  1041. }
  1042. } else {
  1043. Ref<Script> script = resource;
  1044. if (script.is_valid() && script->is_valid()) {
  1045. key_type = Variant::OBJECT;
  1046. key_class_name = script->get_instance_base_type();
  1047. key_script = script;
  1048. }
  1049. }
  1050. } else if (ClassDB::class_exists(token.value)) {
  1051. key_type = Variant::OBJECT;
  1052. key_class_name = token.value;
  1053. }
  1054. if (!got_comma_token) {
  1055. get_token(p_stream, token, line, r_err_str);
  1056. if (token.type != TK_COMMA) {
  1057. r_err_str = "Expected ',' after key type";
  1058. return ERR_PARSE_ERROR;
  1059. }
  1060. }
  1061. get_token(p_stream, token, line, r_err_str);
  1062. if (token.type != TK_IDENTIFIER) {
  1063. r_err_str = "Expected type identifier for value";
  1064. return ERR_PARSE_ERROR;
  1065. }
  1066. Variant::Type value_type = Variant::NIL;
  1067. StringName value_class_name;
  1068. Variant value_script;
  1069. bool got_bracket_token = false;
  1070. if (builtin_types.has(token.value)) {
  1071. value_type = builtin_types.get(token.value);
  1072. } else if (token.value == "Resource" || token.value == "SubResource" || token.value == "ExtResource") {
  1073. Variant resource;
  1074. err = parse_value(token, resource, p_stream, line, r_err_str, p_res_parser);
  1075. if (err) {
  1076. if (token.value == "Resource" && err == ERR_PARSE_ERROR && r_err_str == "Expected '('" && token.type == TK_BRACKET_CLOSE) {
  1077. err = OK;
  1078. r_err_str = String();
  1079. value_type = Variant::OBJECT;
  1080. value_class_name = token.value;
  1081. got_bracket_token = true;
  1082. } else {
  1083. return err;
  1084. }
  1085. } else {
  1086. Ref<Script> script = resource;
  1087. if (script.is_valid() && script->is_valid()) {
  1088. value_type = Variant::OBJECT;
  1089. value_class_name = script->get_instance_base_type();
  1090. value_script = script;
  1091. }
  1092. }
  1093. } else if (ClassDB::class_exists(token.value)) {
  1094. value_type = Variant::OBJECT;
  1095. value_class_name = token.value;
  1096. }
  1097. if (key_type != Variant::NIL || value_type != Variant::NIL) {
  1098. dict.set_typed(key_type, key_class_name, key_script, value_type, value_class_name, value_script);
  1099. }
  1100. if (!got_bracket_token) {
  1101. get_token(p_stream, token, line, r_err_str);
  1102. if (token.type != TK_BRACKET_CLOSE) {
  1103. r_err_str = "Expected ']'";
  1104. return ERR_PARSE_ERROR;
  1105. }
  1106. }
  1107. get_token(p_stream, token, line, r_err_str);
  1108. if (token.type != TK_PARENTHESIS_OPEN) {
  1109. r_err_str = "Expected '('";
  1110. return ERR_PARSE_ERROR;
  1111. }
  1112. get_token(p_stream, token, line, r_err_str);
  1113. if (token.type != TK_CURLY_BRACKET_OPEN) {
  1114. r_err_str = "Expected '{'";
  1115. return ERR_PARSE_ERROR;
  1116. }
  1117. Dictionary values;
  1118. err = _parse_dictionary(values, p_stream, line, r_err_str, p_res_parser);
  1119. if (err) {
  1120. return err;
  1121. }
  1122. get_token(p_stream, token, line, r_err_str);
  1123. if (token.type != TK_PARENTHESIS_CLOSE) {
  1124. r_err_str = "Expected ')'";
  1125. return ERR_PARSE_ERROR;
  1126. }
  1127. dict.assign(values);
  1128. value = dict;
  1129. } else if (id == "Array") {
  1130. Error err = OK;
  1131. get_token(p_stream, token, line, r_err_str);
  1132. if (token.type != TK_BRACKET_OPEN) {
  1133. r_err_str = "Expected '['";
  1134. return ERR_PARSE_ERROR;
  1135. }
  1136. get_token(p_stream, token, line, r_err_str);
  1137. if (token.type != TK_IDENTIFIER) {
  1138. r_err_str = "Expected type identifier";
  1139. return ERR_PARSE_ERROR;
  1140. }
  1141. static HashMap<String, Variant::Type> builtin_types;
  1142. if (builtin_types.is_empty()) {
  1143. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  1144. builtin_types[Variant::get_type_name((Variant::Type)i)] = (Variant::Type)i;
  1145. }
  1146. }
  1147. Array array = Array();
  1148. bool got_bracket_token = false;
  1149. if (builtin_types.has(token.value)) {
  1150. array.set_typed(builtin_types.get(token.value), StringName(), Variant());
  1151. } else if (token.value == "Resource" || token.value == "SubResource" || token.value == "ExtResource") {
  1152. Variant resource;
  1153. err = parse_value(token, resource, p_stream, line, r_err_str, p_res_parser);
  1154. if (err) {
  1155. if (token.value == "Resource" && err == ERR_PARSE_ERROR && r_err_str == "Expected '('" && token.type == TK_BRACKET_CLOSE) {
  1156. err = OK;
  1157. r_err_str = String();
  1158. array.set_typed(Variant::OBJECT, token.value, Variant());
  1159. got_bracket_token = true;
  1160. } else {
  1161. return err;
  1162. }
  1163. } else {
  1164. Ref<Script> script = resource;
  1165. if (script.is_valid() && script->is_valid()) {
  1166. array.set_typed(Variant::OBJECT, script->get_instance_base_type(), script);
  1167. }
  1168. }
  1169. } else if (ClassDB::class_exists(token.value)) {
  1170. array.set_typed(Variant::OBJECT, token.value, Variant());
  1171. }
  1172. if (!got_bracket_token) {
  1173. get_token(p_stream, token, line, r_err_str);
  1174. if (token.type != TK_BRACKET_CLOSE) {
  1175. r_err_str = "Expected ']'";
  1176. return ERR_PARSE_ERROR;
  1177. }
  1178. }
  1179. get_token(p_stream, token, line, r_err_str);
  1180. if (token.type != TK_PARENTHESIS_OPEN) {
  1181. r_err_str = "Expected '('";
  1182. return ERR_PARSE_ERROR;
  1183. }
  1184. get_token(p_stream, token, line, r_err_str);
  1185. if (token.type != TK_BRACKET_OPEN) {
  1186. r_err_str = "Expected '['";
  1187. return ERR_PARSE_ERROR;
  1188. }
  1189. Array values;
  1190. err = _parse_array(values, p_stream, line, r_err_str, p_res_parser);
  1191. if (err) {
  1192. return err;
  1193. }
  1194. get_token(p_stream, token, line, r_err_str);
  1195. if (token.type != TK_PARENTHESIS_CLOSE) {
  1196. r_err_str = "Expected ')'";
  1197. return ERR_PARSE_ERROR;
  1198. }
  1199. array.assign(values);
  1200. value = array;
  1201. } else if (id == "PackedByteArray" || id == "PoolByteArray" || id == "ByteArray") {
  1202. Vector<uint8_t> args;
  1203. Error err = _parse_byte_array(p_stream, args, line, r_err_str);
  1204. if (err) {
  1205. return err;
  1206. }
  1207. Vector<uint8_t> arr;
  1208. {
  1209. int len = args.size();
  1210. arr.resize(len);
  1211. uint8_t *w = arr.ptrw();
  1212. for (int i = 0; i < len; i++) {
  1213. w[i] = args[i];
  1214. }
  1215. }
  1216. value = arr;
  1217. } else if (id == "PackedInt32Array" || id == "PackedIntArray" || id == "PoolIntArray" || id == "IntArray") {
  1218. Vector<int32_t> args;
  1219. Error err = _parse_construct<int32_t>(p_stream, args, line, r_err_str);
  1220. if (err) {
  1221. return err;
  1222. }
  1223. Vector<int32_t> arr;
  1224. {
  1225. int32_t len = args.size();
  1226. arr.resize(len);
  1227. int32_t *w = arr.ptrw();
  1228. for (int32_t i = 0; i < len; i++) {
  1229. w[i] = int32_t(args[i]);
  1230. }
  1231. }
  1232. value = arr;
  1233. } else if (id == "PackedInt64Array") {
  1234. Vector<int64_t> args;
  1235. Error err = _parse_construct<int64_t>(p_stream, args, line, r_err_str);
  1236. if (err) {
  1237. return err;
  1238. }
  1239. Vector<int64_t> arr;
  1240. {
  1241. int64_t len = args.size();
  1242. arr.resize(len);
  1243. int64_t *w = arr.ptrw();
  1244. for (int64_t i = 0; i < len; i++) {
  1245. w[i] = int64_t(args[i]);
  1246. }
  1247. }
  1248. value = arr;
  1249. } else if (id == "PackedFloat32Array" || id == "PackedRealArray" || id == "PoolRealArray" || id == "FloatArray") {
  1250. Vector<float> args;
  1251. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  1252. if (err) {
  1253. return err;
  1254. }
  1255. Vector<float> arr;
  1256. {
  1257. int len = args.size();
  1258. arr.resize(len);
  1259. float *w = arr.ptrw();
  1260. for (int i = 0; i < len; i++) {
  1261. w[i] = args[i];
  1262. }
  1263. }
  1264. value = arr;
  1265. } else if (id == "PackedFloat64Array") {
  1266. Vector<double> args;
  1267. Error err = _parse_construct<double>(p_stream, args, line, r_err_str);
  1268. if (err) {
  1269. return err;
  1270. }
  1271. Vector<double> arr;
  1272. {
  1273. int len = args.size();
  1274. arr.resize(len);
  1275. double *w = arr.ptrw();
  1276. for (int i = 0; i < len; i++) {
  1277. w[i] = args[i];
  1278. }
  1279. }
  1280. value = arr;
  1281. } else if (id == "PackedStringArray" || id == "PoolStringArray" || id == "StringArray") {
  1282. get_token(p_stream, token, line, r_err_str);
  1283. if (token.type != TK_PARENTHESIS_OPEN) {
  1284. r_err_str = "Expected '('";
  1285. return ERR_PARSE_ERROR;
  1286. }
  1287. Vector<String> cs;
  1288. bool first = true;
  1289. while (true) {
  1290. if (!first) {
  1291. get_token(p_stream, token, line, r_err_str);
  1292. if (token.type == TK_COMMA) {
  1293. //do none
  1294. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  1295. break;
  1296. } else {
  1297. r_err_str = "Expected ',' or ')'";
  1298. return ERR_PARSE_ERROR;
  1299. }
  1300. }
  1301. get_token(p_stream, token, line, r_err_str);
  1302. if (token.type == TK_PARENTHESIS_CLOSE) {
  1303. break;
  1304. } else if (token.type != TK_STRING) {
  1305. r_err_str = "Expected string";
  1306. return ERR_PARSE_ERROR;
  1307. }
  1308. first = false;
  1309. cs.push_back(token.value);
  1310. }
  1311. Vector<String> arr;
  1312. {
  1313. int len = cs.size();
  1314. arr.resize(len);
  1315. String *w = arr.ptrw();
  1316. for (int i = 0; i < len; i++) {
  1317. w[i] = cs[i];
  1318. }
  1319. }
  1320. value = arr;
  1321. } else if (id == "PackedVector2Array" || id == "PoolVector2Array" || id == "Vector2Array") {
  1322. Vector<real_t> args;
  1323. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  1324. if (err) {
  1325. return err;
  1326. }
  1327. Vector<Vector2> arr;
  1328. {
  1329. int len = args.size() / 2;
  1330. arr.resize(len);
  1331. Vector2 *w = arr.ptrw();
  1332. for (int i = 0; i < len; i++) {
  1333. w[i] = Vector2(args[i * 2 + 0], args[i * 2 + 1]);
  1334. }
  1335. }
  1336. value = arr;
  1337. } else if (id == "PackedVector3Array" || id == "PoolVector3Array" || id == "Vector3Array") {
  1338. Vector<real_t> args;
  1339. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  1340. if (err) {
  1341. return err;
  1342. }
  1343. Vector<Vector3> arr;
  1344. {
  1345. int len = args.size() / 3;
  1346. arr.resize(len);
  1347. Vector3 *w = arr.ptrw();
  1348. for (int i = 0; i < len; i++) {
  1349. w[i] = Vector3(args[i * 3 + 0], args[i * 3 + 1], args[i * 3 + 2]);
  1350. }
  1351. }
  1352. value = arr;
  1353. } else if (id == "PackedVector4Array" || id == "PoolVector4Array" || id == "Vector4Array") {
  1354. Vector<real_t> args;
  1355. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  1356. if (err) {
  1357. return err;
  1358. }
  1359. Vector<Vector4> arr;
  1360. {
  1361. int len = args.size() / 4;
  1362. arr.resize(len);
  1363. Vector4 *w = arr.ptrw();
  1364. for (int i = 0; i < len; i++) {
  1365. w[i] = Vector4(args[i * 4 + 0], args[i * 4 + 1], args[i * 4 + 2], args[i * 4 + 3]);
  1366. }
  1367. }
  1368. value = arr;
  1369. } else if (id == "PackedColorArray" || id == "PoolColorArray" || id == "ColorArray") {
  1370. Vector<float> args;
  1371. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  1372. if (err) {
  1373. return err;
  1374. }
  1375. Vector<Color> arr;
  1376. {
  1377. int len = args.size() / 4;
  1378. arr.resize(len);
  1379. Color *w = arr.ptrw();
  1380. for (int i = 0; i < len; i++) {
  1381. w[i] = Color(args[i * 4 + 0], args[i * 4 + 1], args[i * 4 + 2], args[i * 4 + 3]);
  1382. }
  1383. }
  1384. value = arr;
  1385. } else {
  1386. r_err_str = vformat("Unexpected identifier '%s'", id);
  1387. return ERR_PARSE_ERROR;
  1388. }
  1389. // All above branches end up here unless they had an early return.
  1390. return OK;
  1391. } else if (token.type == TK_NUMBER) {
  1392. value = token.value;
  1393. return OK;
  1394. } else if (token.type == TK_STRING) {
  1395. value = token.value;
  1396. return OK;
  1397. } else if (token.type == TK_STRING_NAME) {
  1398. value = token.value;
  1399. return OK;
  1400. } else if (token.type == TK_COLOR) {
  1401. value = token.value;
  1402. return OK;
  1403. } else {
  1404. r_err_str = vformat("Expected value, got '%s'", String(tk_name[token.type]));
  1405. return ERR_PARSE_ERROR;
  1406. }
  1407. }
  1408. Error VariantParser::_parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  1409. Token token;
  1410. bool need_comma = false;
  1411. while (true) {
  1412. if (p_stream->is_eof()) {
  1413. r_err_str = "Unexpected EOF while parsing array";
  1414. return ERR_FILE_CORRUPT;
  1415. }
  1416. Error err = get_token(p_stream, token, line, r_err_str);
  1417. if (err != OK) {
  1418. return err;
  1419. }
  1420. if (token.type == TK_BRACKET_CLOSE) {
  1421. return OK;
  1422. }
  1423. if (need_comma) {
  1424. if (token.type != TK_COMMA) {
  1425. r_err_str = "Expected ','";
  1426. return ERR_PARSE_ERROR;
  1427. } else {
  1428. need_comma = false;
  1429. continue;
  1430. }
  1431. }
  1432. Variant v;
  1433. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1434. if (err) {
  1435. return err;
  1436. }
  1437. array.push_back(v);
  1438. need_comma = true;
  1439. }
  1440. }
  1441. Error VariantParser::_parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  1442. bool at_key = true;
  1443. Variant key;
  1444. Token token;
  1445. bool need_comma = false;
  1446. while (true) {
  1447. if (p_stream->is_eof()) {
  1448. r_err_str = "Unexpected EOF while parsing dictionary";
  1449. return ERR_FILE_CORRUPT;
  1450. }
  1451. if (at_key) {
  1452. Error err = get_token(p_stream, token, line, r_err_str);
  1453. if (err != OK) {
  1454. return err;
  1455. }
  1456. if (token.type == TK_CURLY_BRACKET_CLOSE) {
  1457. return OK;
  1458. }
  1459. if (need_comma) {
  1460. if (token.type != TK_COMMA) {
  1461. r_err_str = "Expected '}' or ','";
  1462. return ERR_PARSE_ERROR;
  1463. } else {
  1464. need_comma = false;
  1465. continue;
  1466. }
  1467. }
  1468. err = parse_value(token, key, p_stream, line, r_err_str, p_res_parser);
  1469. if (err) {
  1470. return err;
  1471. }
  1472. err = get_token(p_stream, token, line, r_err_str);
  1473. if (err != OK) {
  1474. return err;
  1475. }
  1476. if (token.type != TK_COLON) {
  1477. r_err_str = "Expected ':'";
  1478. return ERR_PARSE_ERROR;
  1479. }
  1480. at_key = false;
  1481. } else {
  1482. Error err = get_token(p_stream, token, line, r_err_str);
  1483. if (err != OK) {
  1484. return err;
  1485. }
  1486. Variant v;
  1487. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1488. if (err && err != ERR_FILE_MISSING_DEPENDENCIES) {
  1489. return err;
  1490. }
  1491. object[key] = v;
  1492. need_comma = true;
  1493. at_key = true;
  1494. }
  1495. }
  1496. }
  1497. Error VariantParser::_parse_tag(Token &token, Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser, bool p_simple_tag) {
  1498. r_tag.fields.clear();
  1499. if (token.type != TK_BRACKET_OPEN) {
  1500. r_err_str = "Expected '['";
  1501. return ERR_PARSE_ERROR;
  1502. }
  1503. if (p_simple_tag) {
  1504. r_tag.name = "";
  1505. r_tag.fields.clear();
  1506. bool escaping = false;
  1507. if (p_stream->is_utf8()) {
  1508. CharString cs;
  1509. while (true) {
  1510. char c = p_stream->get_char();
  1511. if (p_stream->is_eof()) {
  1512. r_err_str = "Unexpected EOF while parsing simple tag";
  1513. return ERR_PARSE_ERROR;
  1514. }
  1515. if (c == ']') {
  1516. if (escaping) {
  1517. escaping = false;
  1518. } else {
  1519. break;
  1520. }
  1521. } else if (c == '\\') {
  1522. escaping = true;
  1523. } else {
  1524. escaping = false;
  1525. }
  1526. cs += c;
  1527. }
  1528. r_tag.name.parse_utf8(cs.get_data(), cs.length());
  1529. } else {
  1530. while (true) {
  1531. char32_t c = p_stream->get_char();
  1532. if (p_stream->is_eof()) {
  1533. r_err_str = "Unexpected EOF while parsing simple tag";
  1534. return ERR_PARSE_ERROR;
  1535. }
  1536. if (c == ']') {
  1537. if (escaping) {
  1538. escaping = false;
  1539. } else {
  1540. break;
  1541. }
  1542. } else if (c == '\\') {
  1543. escaping = true;
  1544. } else {
  1545. escaping = false;
  1546. }
  1547. r_tag.name += String::chr(c);
  1548. }
  1549. }
  1550. r_tag.name = r_tag.name.strip_edges();
  1551. return OK;
  1552. }
  1553. get_token(p_stream, token, line, r_err_str);
  1554. if (token.type != TK_IDENTIFIER) {
  1555. r_err_str = "Expected identifier (tag name)";
  1556. return ERR_PARSE_ERROR;
  1557. }
  1558. r_tag.name = token.value;
  1559. bool parsing_tag = true;
  1560. while (true) {
  1561. if (p_stream->is_eof()) {
  1562. r_err_str = vformat("Unexpected EOF while parsing tag '%s'", r_tag.name);
  1563. return ERR_FILE_CORRUPT;
  1564. }
  1565. get_token(p_stream, token, line, r_err_str);
  1566. if (token.type == TK_BRACKET_CLOSE) {
  1567. break;
  1568. }
  1569. if (parsing_tag && token.type == TK_PERIOD) {
  1570. r_tag.name += "."; //support tags such as [someprop.Android] for specific platforms
  1571. get_token(p_stream, token, line, r_err_str);
  1572. } else if (parsing_tag && token.type == TK_COLON) {
  1573. r_tag.name += ":"; //support tags such as [someprop.Android] for specific platforms
  1574. get_token(p_stream, token, line, r_err_str);
  1575. } else {
  1576. parsing_tag = false;
  1577. }
  1578. if (token.type != TK_IDENTIFIER) {
  1579. r_err_str = "Expected identifier";
  1580. return ERR_PARSE_ERROR;
  1581. }
  1582. String id = token.value;
  1583. if (parsing_tag) {
  1584. r_tag.name += id;
  1585. continue;
  1586. }
  1587. get_token(p_stream, token, line, r_err_str);
  1588. if (token.type != TK_EQUAL) {
  1589. r_err_str = "Expected '=' after identifier";
  1590. return ERR_PARSE_ERROR;
  1591. }
  1592. get_token(p_stream, token, line, r_err_str);
  1593. Variant value;
  1594. Error err = parse_value(token, value, p_stream, line, r_err_str, p_res_parser);
  1595. if (err) {
  1596. return err;
  1597. }
  1598. r_tag.fields[id] = value;
  1599. }
  1600. return OK;
  1601. }
  1602. Error VariantParser::parse_tag(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser, bool p_simple_tag) {
  1603. Token token;
  1604. get_token(p_stream, token, line, r_err_str);
  1605. if (token.type == TK_EOF) {
  1606. return ERR_FILE_EOF;
  1607. }
  1608. if (token.type != TK_BRACKET_OPEN) {
  1609. r_err_str = "Expected '['";
  1610. return ERR_PARSE_ERROR;
  1611. }
  1612. return _parse_tag(token, p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1613. }
  1614. Error VariantParser::parse_tag_assign_eof(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, String &r_assign, Variant &r_value, ResourceParser *p_res_parser, bool p_simple_tag) {
  1615. //assign..
  1616. r_assign = "";
  1617. String what;
  1618. while (true) {
  1619. char32_t c;
  1620. if (p_stream->saved) {
  1621. c = p_stream->saved;
  1622. p_stream->saved = 0;
  1623. } else {
  1624. c = p_stream->get_char();
  1625. }
  1626. if (p_stream->is_eof()) {
  1627. return ERR_FILE_EOF;
  1628. }
  1629. if (c == ';') { //comment
  1630. while (true) {
  1631. char32_t ch = p_stream->get_char();
  1632. if (p_stream->is_eof()) {
  1633. return ERR_FILE_EOF;
  1634. }
  1635. if (ch == '\n') {
  1636. line++;
  1637. break;
  1638. }
  1639. }
  1640. continue;
  1641. }
  1642. if (c == '[' && what.length() == 0) {
  1643. //it's a tag!
  1644. p_stream->saved = '['; //go back one
  1645. Error err = parse_tag(p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1646. return err;
  1647. }
  1648. if (c > 32) {
  1649. if (c == '"') { //quoted
  1650. p_stream->saved = '"';
  1651. Token tk;
  1652. Error err = get_token(p_stream, tk, line, r_err_str);
  1653. if (err) {
  1654. return err;
  1655. }
  1656. if (tk.type != TK_STRING) {
  1657. r_err_str = "Error reading quoted string";
  1658. return ERR_INVALID_DATA;
  1659. }
  1660. what = tk.value;
  1661. } else if (c != '=') {
  1662. what += String::chr(c);
  1663. } else {
  1664. r_assign = what;
  1665. Token token;
  1666. get_token(p_stream, token, line, r_err_str);
  1667. Error err = parse_value(token, r_value, p_stream, line, r_err_str, p_res_parser);
  1668. return err;
  1669. }
  1670. } else if (c == '\n') {
  1671. line++;
  1672. }
  1673. }
  1674. }
  1675. Error VariantParser::parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser) {
  1676. Token token;
  1677. Error err = get_token(p_stream, token, r_err_line, r_err_str);
  1678. if (err) {
  1679. return err;
  1680. }
  1681. if (token.type == TK_EOF) {
  1682. return ERR_FILE_EOF;
  1683. }
  1684. return parse_value(token, r_ret, p_stream, r_err_line, r_err_str, p_res_parser);
  1685. }
  1686. //////////////////////////////////////////////////////////////////////////////////
  1687. //////////////////////////////////////////////////////////////////////////////////
  1688. //////////////////////////////////////////////////////////////////////////////////
  1689. static String rtos_fix(double p_value) {
  1690. if (p_value == 0.0) {
  1691. return "0"; //avoid negative zero (-0) being written, which may annoy git, svn, etc. for changes when they don't exist.
  1692. } else if (isnan(p_value)) {
  1693. return "nan";
  1694. } else if (isinf(p_value)) {
  1695. if (p_value > 0) {
  1696. return "inf";
  1697. } else {
  1698. return "-inf";
  1699. }
  1700. } else {
  1701. return rtoss(p_value);
  1702. }
  1703. }
  1704. Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_string_func, void *p_store_string_ud, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud, int p_recursion_count, bool p_compat) {
  1705. switch (p_variant.get_type()) {
  1706. case Variant::NIL: {
  1707. p_store_string_func(p_store_string_ud, "null");
  1708. } break;
  1709. case Variant::BOOL: {
  1710. p_store_string_func(p_store_string_ud, p_variant.operator bool() ? "true" : "false");
  1711. } break;
  1712. case Variant::INT: {
  1713. p_store_string_func(p_store_string_ud, itos(p_variant.operator int64_t()));
  1714. } break;
  1715. case Variant::FLOAT: {
  1716. String s = rtos_fix(p_variant.operator double());
  1717. if (s != "inf" && s != "-inf" && s != "nan") {
  1718. if (!s.contains_char('.') && !s.contains_char('e') && !s.contains_char('E')) {
  1719. s += ".0";
  1720. }
  1721. }
  1722. p_store_string_func(p_store_string_ud, s);
  1723. } break;
  1724. case Variant::STRING: {
  1725. String str = p_variant;
  1726. str = "\"" + str.c_escape_multiline() + "\"";
  1727. p_store_string_func(p_store_string_ud, str);
  1728. } break;
  1729. // Math types.
  1730. case Variant::VECTOR2: {
  1731. Vector2 v = p_variant;
  1732. p_store_string_func(p_store_string_ud, "Vector2(" + rtos_fix(v.x) + ", " + rtos_fix(v.y) + ")");
  1733. } break;
  1734. case Variant::VECTOR2I: {
  1735. Vector2i v = p_variant;
  1736. p_store_string_func(p_store_string_ud, "Vector2i(" + itos(v.x) + ", " + itos(v.y) + ")");
  1737. } break;
  1738. case Variant::RECT2: {
  1739. Rect2 aabb = p_variant;
  1740. p_store_string_func(p_store_string_ud, "Rect2(" + rtos_fix(aabb.position.x) + ", " + rtos_fix(aabb.position.y) + ", " + rtos_fix(aabb.size.x) + ", " + rtos_fix(aabb.size.y) + ")");
  1741. } break;
  1742. case Variant::RECT2I: {
  1743. Rect2i aabb = p_variant;
  1744. p_store_string_func(p_store_string_ud, "Rect2i(" + itos(aabb.position.x) + ", " + itos(aabb.position.y) + ", " + itos(aabb.size.x) + ", " + itos(aabb.size.y) + ")");
  1745. } break;
  1746. case Variant::VECTOR3: {
  1747. Vector3 v = p_variant;
  1748. p_store_string_func(p_store_string_ud, "Vector3(" + rtos_fix(v.x) + ", " + rtos_fix(v.y) + ", " + rtos_fix(v.z) + ")");
  1749. } break;
  1750. case Variant::VECTOR3I: {
  1751. Vector3i v = p_variant;
  1752. p_store_string_func(p_store_string_ud, "Vector3i(" + itos(v.x) + ", " + itos(v.y) + ", " + itos(v.z) + ")");
  1753. } break;
  1754. case Variant::VECTOR4: {
  1755. Vector4 v = p_variant;
  1756. p_store_string_func(p_store_string_ud, "Vector4(" + rtos_fix(v.x) + ", " + rtos_fix(v.y) + ", " + rtos_fix(v.z) + ", " + rtos_fix(v.w) + ")");
  1757. } break;
  1758. case Variant::VECTOR4I: {
  1759. Vector4i v = p_variant;
  1760. p_store_string_func(p_store_string_ud, "Vector4i(" + itos(v.x) + ", " + itos(v.y) + ", " + itos(v.z) + ", " + itos(v.w) + ")");
  1761. } break;
  1762. case Variant::PLANE: {
  1763. Plane p = p_variant;
  1764. p_store_string_func(p_store_string_ud, "Plane(" + rtos_fix(p.normal.x) + ", " + rtos_fix(p.normal.y) + ", " + rtos_fix(p.normal.z) + ", " + rtos_fix(p.d) + ")");
  1765. } break;
  1766. case Variant::AABB: {
  1767. AABB aabb = p_variant;
  1768. p_store_string_func(p_store_string_ud, "AABB(" + rtos_fix(aabb.position.x) + ", " + rtos_fix(aabb.position.y) + ", " + rtos_fix(aabb.position.z) + ", " + rtos_fix(aabb.size.x) + ", " + rtos_fix(aabb.size.y) + ", " + rtos_fix(aabb.size.z) + ")");
  1769. } break;
  1770. case Variant::QUATERNION: {
  1771. Quaternion quaternion = p_variant;
  1772. p_store_string_func(p_store_string_ud, "Quaternion(" + rtos_fix(quaternion.x) + ", " + rtos_fix(quaternion.y) + ", " + rtos_fix(quaternion.z) + ", " + rtos_fix(quaternion.w) + ")");
  1773. } break;
  1774. case Variant::TRANSFORM2D: {
  1775. String s = "Transform2D(";
  1776. Transform2D m3 = p_variant;
  1777. for (int i = 0; i < 3; i++) {
  1778. for (int j = 0; j < 2; j++) {
  1779. if (i != 0 || j != 0) {
  1780. s += ", ";
  1781. }
  1782. s += rtos_fix(m3.columns[i][j]);
  1783. }
  1784. }
  1785. p_store_string_func(p_store_string_ud, s + ")");
  1786. } break;
  1787. case Variant::BASIS: {
  1788. String s = "Basis(";
  1789. Basis m3 = p_variant;
  1790. for (int i = 0; i < 3; i++) {
  1791. for (int j = 0; j < 3; j++) {
  1792. if (i != 0 || j != 0) {
  1793. s += ", ";
  1794. }
  1795. s += rtos_fix(m3.rows[i][j]);
  1796. }
  1797. }
  1798. p_store_string_func(p_store_string_ud, s + ")");
  1799. } break;
  1800. case Variant::TRANSFORM3D: {
  1801. String s = "Transform3D(";
  1802. Transform3D t = p_variant;
  1803. Basis &m3 = t.basis;
  1804. for (int i = 0; i < 3; i++) {
  1805. for (int j = 0; j < 3; j++) {
  1806. if (i != 0 || j != 0) {
  1807. s += ", ";
  1808. }
  1809. s += rtos_fix(m3.rows[i][j]);
  1810. }
  1811. }
  1812. s = s + ", " + rtos_fix(t.origin.x) + ", " + rtos_fix(t.origin.y) + ", " + rtos_fix(t.origin.z);
  1813. p_store_string_func(p_store_string_ud, s + ")");
  1814. } break;
  1815. case Variant::PROJECTION: {
  1816. String s = "Projection(";
  1817. Projection t = p_variant;
  1818. for (int i = 0; i < 4; i++) {
  1819. for (int j = 0; j < 4; j++) {
  1820. if (i != 0 || j != 0) {
  1821. s += ", ";
  1822. }
  1823. s += rtos_fix(t.columns[i][j]);
  1824. }
  1825. }
  1826. p_store_string_func(p_store_string_ud, s + ")");
  1827. } break;
  1828. // Misc types.
  1829. case Variant::COLOR: {
  1830. Color c = p_variant;
  1831. p_store_string_func(p_store_string_ud, "Color(" + rtos_fix(c.r) + ", " + rtos_fix(c.g) + ", " + rtos_fix(c.b) + ", " + rtos_fix(c.a) + ")");
  1832. } break;
  1833. case Variant::STRING_NAME: {
  1834. String str = p_variant;
  1835. str = "&\"" + str.c_escape() + "\"";
  1836. p_store_string_func(p_store_string_ud, str);
  1837. } break;
  1838. case Variant::NODE_PATH: {
  1839. String str = p_variant;
  1840. str = "NodePath(\"" + str.c_escape() + "\")";
  1841. p_store_string_func(p_store_string_ud, str);
  1842. } break;
  1843. case Variant::RID: {
  1844. RID rid = p_variant;
  1845. if (rid == RID()) {
  1846. p_store_string_func(p_store_string_ud, "RID()");
  1847. } else {
  1848. p_store_string_func(p_store_string_ud, "RID(" + itos(rid.get_id()) + ")");
  1849. }
  1850. } break;
  1851. // Do not really store these, but ensure that assignments are not empty.
  1852. case Variant::SIGNAL: {
  1853. p_store_string_func(p_store_string_ud, "Signal()");
  1854. } break;
  1855. case Variant::CALLABLE: {
  1856. p_store_string_func(p_store_string_ud, "Callable()");
  1857. } break;
  1858. case Variant::OBJECT: {
  1859. if (unlikely(p_recursion_count > MAX_RECURSION)) {
  1860. ERR_PRINT("Max recursion reached");
  1861. p_store_string_func(p_store_string_ud, "null");
  1862. return OK;
  1863. }
  1864. p_recursion_count++;
  1865. Object *obj = p_variant.get_validated_object();
  1866. if (!obj) {
  1867. p_store_string_func(p_store_string_ud, "null");
  1868. break; // don't save it
  1869. }
  1870. Ref<Resource> res = p_variant;
  1871. if (res.is_valid()) {
  1872. //is resource
  1873. String res_text;
  1874. //try external function
  1875. if (p_encode_res_func) {
  1876. res_text = p_encode_res_func(p_encode_res_ud, res);
  1877. }
  1878. //try path because it's a file
  1879. if (res_text.is_empty() && res->get_path().is_resource_file()) {
  1880. //external resource
  1881. String path = res->get_path();
  1882. res_text = "Resource(\"" + path + "\")";
  1883. }
  1884. //could come up with some sort of text
  1885. if (!res_text.is_empty()) {
  1886. p_store_string_func(p_store_string_ud, res_text);
  1887. break;
  1888. }
  1889. }
  1890. //store as generic object
  1891. p_store_string_func(p_store_string_ud, "Object(" + obj->get_class() + ",");
  1892. List<PropertyInfo> props;
  1893. obj->get_property_list(&props);
  1894. bool first = true;
  1895. for (const PropertyInfo &E : props) {
  1896. if (E.usage & PROPERTY_USAGE_STORAGE || E.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) {
  1897. //must be serialized
  1898. if (first) {
  1899. first = false;
  1900. } else {
  1901. p_store_string_func(p_store_string_ud, ",");
  1902. }
  1903. p_store_string_func(p_store_string_ud, "\"" + E.name + "\":");
  1904. write(obj->get(E.name), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat);
  1905. }
  1906. }
  1907. p_store_string_func(p_store_string_ud, ")\n");
  1908. } break;
  1909. case Variant::DICTIONARY: {
  1910. Dictionary dict = p_variant;
  1911. if (dict.is_typed()) {
  1912. p_store_string_func(p_store_string_ud, "Dictionary[");
  1913. Variant::Type key_builtin_type = (Variant::Type)dict.get_typed_key_builtin();
  1914. StringName key_class_name = dict.get_typed_key_class_name();
  1915. Ref<Script> key_script = dict.get_typed_key_script();
  1916. if (key_script.is_valid()) {
  1917. String resource_text;
  1918. if (p_encode_res_func) {
  1919. resource_text = p_encode_res_func(p_encode_res_ud, key_script);
  1920. }
  1921. if (resource_text.is_empty() && key_script->get_path().is_resource_file()) {
  1922. resource_text = "Resource(\"" + key_script->get_path() + "\")";
  1923. }
  1924. if (!resource_text.is_empty()) {
  1925. p_store_string_func(p_store_string_ud, resource_text);
  1926. } else {
  1927. ERR_PRINT("Failed to encode a path to a custom script for a dictionary key type.");
  1928. p_store_string_func(p_store_string_ud, key_class_name);
  1929. }
  1930. } else if (key_class_name != StringName()) {
  1931. p_store_string_func(p_store_string_ud, key_class_name);
  1932. } else if (key_builtin_type == Variant::NIL) {
  1933. p_store_string_func(p_store_string_ud, "Variant");
  1934. } else {
  1935. p_store_string_func(p_store_string_ud, Variant::get_type_name(key_builtin_type));
  1936. }
  1937. p_store_string_func(p_store_string_ud, ", ");
  1938. Variant::Type value_builtin_type = (Variant::Type)dict.get_typed_value_builtin();
  1939. StringName value_class_name = dict.get_typed_value_class_name();
  1940. Ref<Script> value_script = dict.get_typed_value_script();
  1941. if (value_script.is_valid()) {
  1942. String resource_text;
  1943. if (p_encode_res_func) {
  1944. resource_text = p_encode_res_func(p_encode_res_ud, value_script);
  1945. }
  1946. if (resource_text.is_empty() && value_script->get_path().is_resource_file()) {
  1947. resource_text = "Resource(\"" + value_script->get_path() + "\")";
  1948. }
  1949. if (!resource_text.is_empty()) {
  1950. p_store_string_func(p_store_string_ud, resource_text);
  1951. } else {
  1952. ERR_PRINT("Failed to encode a path to a custom script for a dictionary value type.");
  1953. p_store_string_func(p_store_string_ud, value_class_name);
  1954. }
  1955. } else if (value_class_name != StringName()) {
  1956. p_store_string_func(p_store_string_ud, value_class_name);
  1957. } else if (value_builtin_type == Variant::NIL) {
  1958. p_store_string_func(p_store_string_ud, "Variant");
  1959. } else {
  1960. p_store_string_func(p_store_string_ud, Variant::get_type_name(value_builtin_type));
  1961. }
  1962. p_store_string_func(p_store_string_ud, "](");
  1963. }
  1964. if (unlikely(p_recursion_count > MAX_RECURSION)) {
  1965. ERR_PRINT("Max recursion reached");
  1966. p_store_string_func(p_store_string_ud, "{}");
  1967. } else {
  1968. List<Variant> keys;
  1969. dict.get_key_list(&keys);
  1970. keys.sort_custom<StringLikeVariantOrder>();
  1971. if (keys.is_empty()) {
  1972. // Avoid unnecessary line break.
  1973. p_store_string_func(p_store_string_ud, "{}");
  1974. } else {
  1975. p_recursion_count++;
  1976. p_store_string_func(p_store_string_ud, "{\n");
  1977. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1978. write(E->get(), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat);
  1979. p_store_string_func(p_store_string_ud, ": ");
  1980. write(dict[E->get()], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat);
  1981. if (E->next()) {
  1982. p_store_string_func(p_store_string_ud, ",\n");
  1983. } else {
  1984. p_store_string_func(p_store_string_ud, "\n");
  1985. }
  1986. }
  1987. p_store_string_func(p_store_string_ud, "}");
  1988. }
  1989. }
  1990. if (dict.is_typed()) {
  1991. p_store_string_func(p_store_string_ud, ")");
  1992. }
  1993. } break;
  1994. case Variant::ARRAY: {
  1995. Array array = p_variant;
  1996. if (array.is_typed()) {
  1997. p_store_string_func(p_store_string_ud, "Array[");
  1998. Variant::Type builtin_type = (Variant::Type)array.get_typed_builtin();
  1999. StringName class_name = array.get_typed_class_name();
  2000. Ref<Script> script = array.get_typed_script();
  2001. if (script.is_valid()) {
  2002. String resource_text = String();
  2003. if (p_encode_res_func) {
  2004. resource_text = p_encode_res_func(p_encode_res_ud, script);
  2005. }
  2006. if (resource_text.is_empty() && script->get_path().is_resource_file()) {
  2007. resource_text = "Resource(\"" + script->get_path() + "\")";
  2008. }
  2009. if (!resource_text.is_empty()) {
  2010. p_store_string_func(p_store_string_ud, resource_text);
  2011. } else {
  2012. ERR_PRINT("Failed to encode a path to a custom script for an array type.");
  2013. p_store_string_func(p_store_string_ud, class_name);
  2014. }
  2015. } else if (class_name != StringName()) {
  2016. p_store_string_func(p_store_string_ud, class_name);
  2017. } else {
  2018. p_store_string_func(p_store_string_ud, Variant::get_type_name(builtin_type));
  2019. }
  2020. p_store_string_func(p_store_string_ud, "](");
  2021. }
  2022. if (unlikely(p_recursion_count > MAX_RECURSION)) {
  2023. ERR_PRINT("Max recursion reached");
  2024. p_store_string_func(p_store_string_ud, "[]");
  2025. } else {
  2026. p_recursion_count++;
  2027. p_store_string_func(p_store_string_ud, "[");
  2028. bool first = true;
  2029. for (const Variant &var : array) {
  2030. if (first) {
  2031. first = false;
  2032. } else {
  2033. p_store_string_func(p_store_string_ud, ", ");
  2034. }
  2035. write(var, p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat);
  2036. }
  2037. p_store_string_func(p_store_string_ud, "]");
  2038. }
  2039. if (array.is_typed()) {
  2040. p_store_string_func(p_store_string_ud, ")");
  2041. }
  2042. } break;
  2043. case Variant::PACKED_BYTE_ARRAY: {
  2044. p_store_string_func(p_store_string_ud, "PackedByteArray(");
  2045. Vector<uint8_t> data = p_variant;
  2046. if (p_compat) {
  2047. int len = data.size();
  2048. const uint8_t *ptr = data.ptr();
  2049. for (int i = 0; i < len; i++) {
  2050. if (i > 0) {
  2051. p_store_string_func(p_store_string_ud, ", ");
  2052. }
  2053. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  2054. }
  2055. } else if (data.size() > 0) {
  2056. p_store_string_func(p_store_string_ud, "\"");
  2057. p_store_string_func(p_store_string_ud, CryptoCore::b64_encode_str(data.ptr(), data.size()));
  2058. p_store_string_func(p_store_string_ud, "\"");
  2059. }
  2060. p_store_string_func(p_store_string_ud, ")");
  2061. } break;
  2062. case Variant::PACKED_INT32_ARRAY: {
  2063. p_store_string_func(p_store_string_ud, "PackedInt32Array(");
  2064. Vector<int32_t> data = p_variant;
  2065. int32_t len = data.size();
  2066. const int32_t *ptr = data.ptr();
  2067. for (int32_t i = 0; i < len; i++) {
  2068. if (i > 0) {
  2069. p_store_string_func(p_store_string_ud, ", ");
  2070. }
  2071. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  2072. }
  2073. p_store_string_func(p_store_string_ud, ")");
  2074. } break;
  2075. case Variant::PACKED_INT64_ARRAY: {
  2076. p_store_string_func(p_store_string_ud, "PackedInt64Array(");
  2077. Vector<int64_t> data = p_variant;
  2078. int64_t len = data.size();
  2079. const int64_t *ptr = data.ptr();
  2080. for (int64_t i = 0; i < len; i++) {
  2081. if (i > 0) {
  2082. p_store_string_func(p_store_string_ud, ", ");
  2083. }
  2084. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  2085. }
  2086. p_store_string_func(p_store_string_ud, ")");
  2087. } break;
  2088. case Variant::PACKED_FLOAT32_ARRAY: {
  2089. p_store_string_func(p_store_string_ud, "PackedFloat32Array(");
  2090. Vector<float> data = p_variant;
  2091. int len = data.size();
  2092. const float *ptr = data.ptr();
  2093. for (int i = 0; i < len; i++) {
  2094. if (i > 0) {
  2095. p_store_string_func(p_store_string_ud, ", ");
  2096. }
  2097. p_store_string_func(p_store_string_ud, rtos_fix(ptr[i]));
  2098. }
  2099. p_store_string_func(p_store_string_ud, ")");
  2100. } break;
  2101. case Variant::PACKED_FLOAT64_ARRAY: {
  2102. p_store_string_func(p_store_string_ud, "PackedFloat64Array(");
  2103. Vector<double> data = p_variant;
  2104. int len = data.size();
  2105. const double *ptr = data.ptr();
  2106. for (int i = 0; i < len; i++) {
  2107. if (i > 0) {
  2108. p_store_string_func(p_store_string_ud, ", ");
  2109. }
  2110. p_store_string_func(p_store_string_ud, rtos_fix(ptr[i]));
  2111. }
  2112. p_store_string_func(p_store_string_ud, ")");
  2113. } break;
  2114. case Variant::PACKED_STRING_ARRAY: {
  2115. p_store_string_func(p_store_string_ud, "PackedStringArray(");
  2116. Vector<String> data = p_variant;
  2117. int len = data.size();
  2118. const String *ptr = data.ptr();
  2119. for (int i = 0; i < len; i++) {
  2120. if (i > 0) {
  2121. p_store_string_func(p_store_string_ud, ", ");
  2122. }
  2123. p_store_string_func(p_store_string_ud, "\"" + ptr[i].c_escape() + "\"");
  2124. }
  2125. p_store_string_func(p_store_string_ud, ")");
  2126. } break;
  2127. case Variant::PACKED_VECTOR2_ARRAY: {
  2128. p_store_string_func(p_store_string_ud, "PackedVector2Array(");
  2129. Vector<Vector2> data = p_variant;
  2130. int len = data.size();
  2131. const Vector2 *ptr = data.ptr();
  2132. for (int i = 0; i < len; i++) {
  2133. if (i > 0) {
  2134. p_store_string_func(p_store_string_ud, ", ");
  2135. }
  2136. p_store_string_func(p_store_string_ud, rtos_fix(ptr[i].x) + ", " + rtos_fix(ptr[i].y));
  2137. }
  2138. p_store_string_func(p_store_string_ud, ")");
  2139. } break;
  2140. case Variant::PACKED_VECTOR3_ARRAY: {
  2141. p_store_string_func(p_store_string_ud, "PackedVector3Array(");
  2142. Vector<Vector3> data = p_variant;
  2143. int len = data.size();
  2144. const Vector3 *ptr = data.ptr();
  2145. for (int i = 0; i < len; i++) {
  2146. if (i > 0) {
  2147. p_store_string_func(p_store_string_ud, ", ");
  2148. }
  2149. p_store_string_func(p_store_string_ud, rtos_fix(ptr[i].x) + ", " + rtos_fix(ptr[i].y) + ", " + rtos_fix(ptr[i].z));
  2150. }
  2151. p_store_string_func(p_store_string_ud, ")");
  2152. } break;
  2153. case Variant::PACKED_COLOR_ARRAY: {
  2154. p_store_string_func(p_store_string_ud, "PackedColorArray(");
  2155. Vector<Color> data = p_variant;
  2156. int len = data.size();
  2157. const Color *ptr = data.ptr();
  2158. for (int i = 0; i < len; i++) {
  2159. if (i > 0) {
  2160. p_store_string_func(p_store_string_ud, ", ");
  2161. }
  2162. p_store_string_func(p_store_string_ud, rtos_fix(ptr[i].r) + ", " + rtos_fix(ptr[i].g) + ", " + rtos_fix(ptr[i].b) + ", " + rtos_fix(ptr[i].a));
  2163. }
  2164. p_store_string_func(p_store_string_ud, ")");
  2165. } break;
  2166. case Variant::PACKED_VECTOR4_ARRAY: {
  2167. p_store_string_func(p_store_string_ud, "PackedVector4Array(");
  2168. Vector<Vector4> data = p_variant;
  2169. int len = data.size();
  2170. const Vector4 *ptr = data.ptr();
  2171. for (int i = 0; i < len; i++) {
  2172. if (i > 0) {
  2173. p_store_string_func(p_store_string_ud, ", ");
  2174. }
  2175. p_store_string_func(p_store_string_ud, rtos_fix(ptr[i].x) + ", " + rtos_fix(ptr[i].y) + ", " + rtos_fix(ptr[i].z) + ", " + rtos_fix(ptr[i].w));
  2176. }
  2177. p_store_string_func(p_store_string_ud, ")");
  2178. } break;
  2179. default: {
  2180. ERR_PRINT("Unknown variant type");
  2181. return ERR_BUG;
  2182. }
  2183. }
  2184. return OK;
  2185. }
  2186. static Error _write_to_str(void *ud, const String &p_string) {
  2187. String *str = (String *)ud;
  2188. (*str) += p_string;
  2189. return OK;
  2190. }
  2191. Error VariantWriter::write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud, bool p_compat) {
  2192. r_string = String();
  2193. return write(p_variant, _write_to_str, &r_string, p_encode_res_func, p_encode_res_ud, 0, p_compat);
  2194. }