variant_parser.cpp 57 KB

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