variant_parser.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  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. if (token.type != TK_NUMBER) {
  768. r_err_str = "Expected number as argument";
  769. return ERR_PARSE_ERROR;
  770. }
  771. value = token.value;
  772. get_token(p_stream, token, line, r_err_str);
  773. if (token.type != TK_PARENTHESIS_CLOSE) {
  774. r_err_str = "Expected ')'";
  775. return ERR_PARSE_ERROR;
  776. }
  777. } else if (id == "Object") {
  778. get_token(p_stream, token, line, r_err_str);
  779. if (token.type != TK_PARENTHESIS_OPEN) {
  780. r_err_str = "Expected '('";
  781. return ERR_PARSE_ERROR;
  782. }
  783. get_token(p_stream, token, line, r_err_str);
  784. if (token.type != TK_IDENTIFIER) {
  785. r_err_str = "Expected identifier with type of object";
  786. return ERR_PARSE_ERROR;
  787. }
  788. String type = token.value;
  789. Object *obj = ClassDB::instantiate(type);
  790. if (!obj) {
  791. r_err_str = "Can't instantiate Object() of type: " + type;
  792. return ERR_PARSE_ERROR;
  793. }
  794. Ref<RefCounted> ref = Ref<RefCounted>(Object::cast_to<RefCounted>(obj));
  795. get_token(p_stream, token, line, r_err_str);
  796. if (token.type != TK_COMMA) {
  797. r_err_str = "Expected ',' after object type";
  798. return ERR_PARSE_ERROR;
  799. }
  800. bool at_key = true;
  801. String key;
  802. bool need_comma = false;
  803. while (true) {
  804. if (p_stream->is_eof()) {
  805. r_err_str = "Unexpected End of File while parsing Object()";
  806. return ERR_FILE_CORRUPT;
  807. }
  808. if (at_key) {
  809. Error err = get_token(p_stream, token, line, r_err_str);
  810. if (err != OK) {
  811. return err;
  812. }
  813. if (token.type == TK_PARENTHESIS_CLOSE) {
  814. value = ref.is_valid() ? Variant(ref) : Variant(obj);
  815. return OK;
  816. }
  817. if (need_comma) {
  818. if (token.type != TK_COMMA) {
  819. r_err_str = "Expected '}' or ','";
  820. return ERR_PARSE_ERROR;
  821. } else {
  822. need_comma = false;
  823. continue;
  824. }
  825. }
  826. if (token.type != TK_STRING) {
  827. r_err_str = "Expected property name as string";
  828. return ERR_PARSE_ERROR;
  829. }
  830. key = token.value;
  831. err = get_token(p_stream, token, line, r_err_str);
  832. if (err != OK) {
  833. return err;
  834. }
  835. if (token.type != TK_COLON) {
  836. r_err_str = "Expected ':'";
  837. return ERR_PARSE_ERROR;
  838. }
  839. at_key = false;
  840. } else {
  841. Error err = get_token(p_stream, token, line, r_err_str);
  842. if (err != OK) {
  843. return err;
  844. }
  845. Variant v;
  846. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  847. if (err) {
  848. return err;
  849. }
  850. obj->set(key, v);
  851. need_comma = true;
  852. at_key = true;
  853. }
  854. }
  855. } else if (id == "Resource" || id == "SubResource" || id == "ExtResource") {
  856. get_token(p_stream, token, line, r_err_str);
  857. if (token.type != TK_PARENTHESIS_OPEN) {
  858. r_err_str = "Expected '('";
  859. return ERR_PARSE_ERROR;
  860. }
  861. if (p_res_parser && id == "Resource" && p_res_parser->func) {
  862. Ref<Resource> res;
  863. Error err = p_res_parser->func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  864. if (err) {
  865. return err;
  866. }
  867. value = res;
  868. } else if (p_res_parser && id == "ExtResource" && p_res_parser->ext_func) {
  869. Ref<Resource> res;
  870. Error err = p_res_parser->ext_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  871. if (err) {
  872. return err;
  873. }
  874. value = res;
  875. } else if (p_res_parser && id == "SubResource" && p_res_parser->sub_func) {
  876. Ref<Resource> res;
  877. Error err = p_res_parser->sub_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  878. if (err) {
  879. return err;
  880. }
  881. value = res;
  882. } else {
  883. get_token(p_stream, token, line, r_err_str);
  884. if (token.type == TK_STRING) {
  885. String path = token.value;
  886. Ref<Resource> res = ResourceLoader::load(path);
  887. if (res.is_null()) {
  888. r_err_str = "Can't load resource at path: '" + path + "'.";
  889. return ERR_PARSE_ERROR;
  890. }
  891. get_token(p_stream, token, line, r_err_str);
  892. if (token.type != TK_PARENTHESIS_CLOSE) {
  893. r_err_str = "Expected ')'";
  894. return ERR_PARSE_ERROR;
  895. }
  896. value = res;
  897. } else {
  898. r_err_str = "Expected string as argument for Resource().";
  899. return ERR_PARSE_ERROR;
  900. }
  901. }
  902. } else if (id == "Array") {
  903. Error err = OK;
  904. get_token(p_stream, token, line, r_err_str);
  905. if (token.type != TK_BRACKET_OPEN) {
  906. r_err_str = "Expected '['";
  907. return ERR_PARSE_ERROR;
  908. }
  909. get_token(p_stream, token, line, r_err_str);
  910. if (token.type != TK_IDENTIFIER) {
  911. r_err_str = "Expected type identifier";
  912. return ERR_PARSE_ERROR;
  913. }
  914. static HashMap<StringName, Variant::Type> builtin_types;
  915. if (builtin_types.is_empty()) {
  916. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  917. builtin_types[Variant::get_type_name((Variant::Type)i)] = (Variant::Type)i;
  918. }
  919. }
  920. Array array = Array();
  921. bool got_bracket_token = false;
  922. if (builtin_types.has(token.value)) {
  923. array.set_typed(builtin_types.get(token.value), StringName(), Variant());
  924. } else if (token.value == "Resource" || token.value == "SubResource" || token.value == "ExtResource") {
  925. Variant resource;
  926. err = parse_value(token, resource, p_stream, line, r_err_str, p_res_parser);
  927. if (err) {
  928. if (token.value == "Resource" && err == ERR_PARSE_ERROR && r_err_str == "Expected '('" && token.type == TK_BRACKET_CLOSE) {
  929. err = OK;
  930. r_err_str = String();
  931. array.set_typed(Variant::OBJECT, token.value, Variant());
  932. got_bracket_token = true;
  933. } else {
  934. return err;
  935. }
  936. } else {
  937. Ref<Script> script = resource;
  938. if (script.is_valid() && script->is_valid()) {
  939. array.set_typed(Variant::OBJECT, script->get_instance_base_type(), script);
  940. }
  941. }
  942. } else if (ClassDB::class_exists(token.value)) {
  943. array.set_typed(Variant::OBJECT, token.value, Variant());
  944. }
  945. if (!got_bracket_token) {
  946. get_token(p_stream, token, line, r_err_str);
  947. if (token.type != TK_BRACKET_CLOSE) {
  948. r_err_str = "Expected ']'";
  949. return ERR_PARSE_ERROR;
  950. }
  951. }
  952. get_token(p_stream, token, line, r_err_str);
  953. if (token.type != TK_PARENTHESIS_OPEN) {
  954. r_err_str = "Expected '('";
  955. return ERR_PARSE_ERROR;
  956. }
  957. get_token(p_stream, token, line, r_err_str);
  958. if (token.type != TK_BRACKET_OPEN) {
  959. r_err_str = "Expected '['";
  960. return ERR_PARSE_ERROR;
  961. }
  962. Array values;
  963. err = _parse_array(values, p_stream, line, r_err_str, p_res_parser);
  964. if (err) {
  965. return err;
  966. }
  967. get_token(p_stream, token, line, r_err_str);
  968. if (token.type != TK_PARENTHESIS_CLOSE) {
  969. r_err_str = "Expected ')'";
  970. return ERR_PARSE_ERROR;
  971. }
  972. array.assign(values);
  973. value = array;
  974. } else if (id == "PackedByteArray" || id == "PoolByteArray" || id == "ByteArray") {
  975. Vector<uint8_t> args;
  976. Error err = _parse_construct<uint8_t>(p_stream, args, line, r_err_str);
  977. if (err) {
  978. return err;
  979. }
  980. Vector<uint8_t> arr;
  981. {
  982. int len = args.size();
  983. arr.resize(len);
  984. uint8_t *w = arr.ptrw();
  985. for (int i = 0; i < len; i++) {
  986. w[i] = args[i];
  987. }
  988. }
  989. value = arr;
  990. } else if (id == "PackedInt32Array" || id == "PackedIntArray" || id == "PoolIntArray" || id == "IntArray") {
  991. Vector<int32_t> args;
  992. Error err = _parse_construct<int32_t>(p_stream, args, line, r_err_str);
  993. if (err) {
  994. return err;
  995. }
  996. Vector<int32_t> arr;
  997. {
  998. int32_t len = args.size();
  999. arr.resize(len);
  1000. int32_t *w = arr.ptrw();
  1001. for (int32_t i = 0; i < len; i++) {
  1002. w[i] = int32_t(args[i]);
  1003. }
  1004. }
  1005. value = arr;
  1006. } else if (id == "PackedInt64Array") {
  1007. Vector<int64_t> args;
  1008. Error err = _parse_construct<int64_t>(p_stream, args, line, r_err_str);
  1009. if (err) {
  1010. return err;
  1011. }
  1012. Vector<int64_t> arr;
  1013. {
  1014. int64_t len = args.size();
  1015. arr.resize(len);
  1016. int64_t *w = arr.ptrw();
  1017. for (int64_t i = 0; i < len; i++) {
  1018. w[i] = int64_t(args[i]);
  1019. }
  1020. }
  1021. value = arr;
  1022. } else if (id == "PackedFloat32Array" || id == "PackedRealArray" || id == "PoolRealArray" || id == "FloatArray") {
  1023. Vector<float> args;
  1024. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  1025. if (err) {
  1026. return err;
  1027. }
  1028. Vector<float> arr;
  1029. {
  1030. int len = args.size();
  1031. arr.resize(len);
  1032. float *w = arr.ptrw();
  1033. for (int i = 0; i < len; i++) {
  1034. w[i] = args[i];
  1035. }
  1036. }
  1037. value = arr;
  1038. } else if (id == "PackedFloat64Array") {
  1039. Vector<double> args;
  1040. Error err = _parse_construct<double>(p_stream, args, line, r_err_str);
  1041. if (err) {
  1042. return err;
  1043. }
  1044. Vector<double> arr;
  1045. {
  1046. int len = args.size();
  1047. arr.resize(len);
  1048. double *w = arr.ptrw();
  1049. for (int i = 0; i < len; i++) {
  1050. w[i] = args[i];
  1051. }
  1052. }
  1053. value = arr;
  1054. } else if (id == "PackedStringArray" || id == "PoolStringArray" || id == "StringArray") {
  1055. get_token(p_stream, token, line, r_err_str);
  1056. if (token.type != TK_PARENTHESIS_OPEN) {
  1057. r_err_str = "Expected '('";
  1058. return ERR_PARSE_ERROR;
  1059. }
  1060. Vector<String> cs;
  1061. bool first = true;
  1062. while (true) {
  1063. if (!first) {
  1064. get_token(p_stream, token, line, r_err_str);
  1065. if (token.type == TK_COMMA) {
  1066. //do none
  1067. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  1068. break;
  1069. } else {
  1070. r_err_str = "Expected ',' or ')'";
  1071. return ERR_PARSE_ERROR;
  1072. }
  1073. }
  1074. get_token(p_stream, token, line, r_err_str);
  1075. if (token.type == TK_PARENTHESIS_CLOSE) {
  1076. break;
  1077. } else if (token.type != TK_STRING) {
  1078. r_err_str = "Expected string";
  1079. return ERR_PARSE_ERROR;
  1080. }
  1081. first = false;
  1082. cs.push_back(token.value);
  1083. }
  1084. Vector<String> arr;
  1085. {
  1086. int len = cs.size();
  1087. arr.resize(len);
  1088. String *w = arr.ptrw();
  1089. for (int i = 0; i < len; i++) {
  1090. w[i] = cs[i];
  1091. }
  1092. }
  1093. value = arr;
  1094. } else if (id == "PackedVector2Array" || id == "PoolVector2Array" || id == "Vector2Array") {
  1095. Vector<real_t> args;
  1096. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  1097. if (err) {
  1098. return err;
  1099. }
  1100. Vector<Vector2> arr;
  1101. {
  1102. int len = args.size() / 2;
  1103. arr.resize(len);
  1104. Vector2 *w = arr.ptrw();
  1105. for (int i = 0; i < len; i++) {
  1106. w[i] = Vector2(args[i * 2 + 0], args[i * 2 + 1]);
  1107. }
  1108. }
  1109. value = arr;
  1110. } else if (id == "PackedVector3Array" || id == "PoolVector3Array" || id == "Vector3Array") {
  1111. Vector<real_t> args;
  1112. Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
  1113. if (err) {
  1114. return err;
  1115. }
  1116. Vector<Vector3> arr;
  1117. {
  1118. int len = args.size() / 3;
  1119. arr.resize(len);
  1120. Vector3 *w = arr.ptrw();
  1121. for (int i = 0; i < len; i++) {
  1122. w[i] = Vector3(args[i * 3 + 0], args[i * 3 + 1], args[i * 3 + 2]);
  1123. }
  1124. }
  1125. value = arr;
  1126. } else if (id == "PackedColorArray" || id == "PoolColorArray" || id == "ColorArray") {
  1127. Vector<float> args;
  1128. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  1129. if (err) {
  1130. return err;
  1131. }
  1132. Vector<Color> arr;
  1133. {
  1134. int len = args.size() / 4;
  1135. arr.resize(len);
  1136. Color *w = arr.ptrw();
  1137. for (int i = 0; i < len; i++) {
  1138. w[i] = Color(args[i * 4 + 0], args[i * 4 + 1], args[i * 4 + 2], args[i * 4 + 3]);
  1139. }
  1140. }
  1141. value = arr;
  1142. } else {
  1143. r_err_str = "Unexpected identifier: '" + id + "'.";
  1144. return ERR_PARSE_ERROR;
  1145. }
  1146. // All above branches end up here unless they had an early return.
  1147. return OK;
  1148. } else if (token.type == TK_NUMBER) {
  1149. value = token.value;
  1150. return OK;
  1151. } else if (token.type == TK_STRING) {
  1152. value = token.value;
  1153. return OK;
  1154. } else if (token.type == TK_STRING_NAME) {
  1155. value = token.value;
  1156. return OK;
  1157. } else if (token.type == TK_COLOR) {
  1158. value = token.value;
  1159. return OK;
  1160. } else {
  1161. r_err_str = "Expected value, got " + String(tk_name[token.type]) + ".";
  1162. return ERR_PARSE_ERROR;
  1163. }
  1164. }
  1165. Error VariantParser::_parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  1166. Token token;
  1167. bool need_comma = false;
  1168. while (true) {
  1169. if (p_stream->is_eof()) {
  1170. r_err_str = "Unexpected End of File while parsing array";
  1171. return ERR_FILE_CORRUPT;
  1172. }
  1173. Error err = get_token(p_stream, token, line, r_err_str);
  1174. if (err != OK) {
  1175. return err;
  1176. }
  1177. if (token.type == TK_BRACKET_CLOSE) {
  1178. return OK;
  1179. }
  1180. if (need_comma) {
  1181. if (token.type != TK_COMMA) {
  1182. r_err_str = "Expected ','";
  1183. return ERR_PARSE_ERROR;
  1184. } else {
  1185. need_comma = false;
  1186. continue;
  1187. }
  1188. }
  1189. Variant v;
  1190. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1191. if (err) {
  1192. return err;
  1193. }
  1194. array.push_back(v);
  1195. need_comma = true;
  1196. }
  1197. }
  1198. Error VariantParser::_parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  1199. bool at_key = true;
  1200. Variant key;
  1201. Token token;
  1202. bool need_comma = false;
  1203. while (true) {
  1204. if (p_stream->is_eof()) {
  1205. r_err_str = "Unexpected End of File while parsing dictionary";
  1206. return ERR_FILE_CORRUPT;
  1207. }
  1208. if (at_key) {
  1209. Error err = get_token(p_stream, token, line, r_err_str);
  1210. if (err != OK) {
  1211. return err;
  1212. }
  1213. if (token.type == TK_CURLY_BRACKET_CLOSE) {
  1214. return OK;
  1215. }
  1216. if (need_comma) {
  1217. if (token.type != TK_COMMA) {
  1218. r_err_str = "Expected '}' or ','";
  1219. return ERR_PARSE_ERROR;
  1220. } else {
  1221. need_comma = false;
  1222. continue;
  1223. }
  1224. }
  1225. err = parse_value(token, key, p_stream, line, r_err_str, p_res_parser);
  1226. if (err) {
  1227. return err;
  1228. }
  1229. err = get_token(p_stream, token, line, r_err_str);
  1230. if (err != OK) {
  1231. return err;
  1232. }
  1233. if (token.type != TK_COLON) {
  1234. r_err_str = "Expected ':'";
  1235. return ERR_PARSE_ERROR;
  1236. }
  1237. at_key = false;
  1238. } else {
  1239. Error err = get_token(p_stream, token, line, r_err_str);
  1240. if (err != OK) {
  1241. return err;
  1242. }
  1243. Variant v;
  1244. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1245. if (err && err != ERR_FILE_MISSING_DEPENDENCIES) {
  1246. return err;
  1247. }
  1248. object[key] = v;
  1249. need_comma = true;
  1250. at_key = true;
  1251. }
  1252. }
  1253. }
  1254. 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) {
  1255. r_tag.fields.clear();
  1256. if (token.type != TK_BRACKET_OPEN) {
  1257. r_err_str = "Expected '['";
  1258. return ERR_PARSE_ERROR;
  1259. }
  1260. if (p_simple_tag) {
  1261. r_tag.name = "";
  1262. r_tag.fields.clear();
  1263. bool escaping = false;
  1264. if (p_stream->is_utf8()) {
  1265. CharString cs;
  1266. while (true) {
  1267. char c = p_stream->get_char();
  1268. if (p_stream->is_eof()) {
  1269. r_err_str = "Unexpected EOF while parsing simple tag";
  1270. return ERR_PARSE_ERROR;
  1271. }
  1272. if (c == ']') {
  1273. if (escaping) {
  1274. escaping = false;
  1275. } else {
  1276. break;
  1277. }
  1278. } else if (c == '\\') {
  1279. escaping = true;
  1280. } else {
  1281. escaping = false;
  1282. }
  1283. cs += c;
  1284. }
  1285. r_tag.name.parse_utf8(cs.get_data(), cs.length());
  1286. } else {
  1287. while (true) {
  1288. char32_t c = p_stream->get_char();
  1289. if (p_stream->is_eof()) {
  1290. r_err_str = "Unexpected EOF while parsing simple tag";
  1291. return ERR_PARSE_ERROR;
  1292. }
  1293. if (c == ']') {
  1294. if (escaping) {
  1295. escaping = false;
  1296. } else {
  1297. break;
  1298. }
  1299. } else if (c == '\\') {
  1300. escaping = true;
  1301. } else {
  1302. escaping = false;
  1303. }
  1304. r_tag.name += String::chr(c);
  1305. }
  1306. }
  1307. r_tag.name = r_tag.name.strip_edges();
  1308. return OK;
  1309. }
  1310. get_token(p_stream, token, line, r_err_str);
  1311. if (token.type != TK_IDENTIFIER) {
  1312. r_err_str = "Expected identifier (tag name)";
  1313. return ERR_PARSE_ERROR;
  1314. }
  1315. r_tag.name = token.value;
  1316. bool parsing_tag = true;
  1317. while (true) {
  1318. if (p_stream->is_eof()) {
  1319. r_err_str = "Unexpected End of File while parsing tag: " + r_tag.name;
  1320. return ERR_FILE_CORRUPT;
  1321. }
  1322. get_token(p_stream, token, line, r_err_str);
  1323. if (token.type == TK_BRACKET_CLOSE) {
  1324. break;
  1325. }
  1326. if (parsing_tag && token.type == TK_PERIOD) {
  1327. r_tag.name += "."; //support tags such as [someprop.Android] for specific platforms
  1328. get_token(p_stream, token, line, r_err_str);
  1329. } else if (parsing_tag && token.type == TK_COLON) {
  1330. r_tag.name += ":"; //support tags such as [someprop.Android] for specific platforms
  1331. get_token(p_stream, token, line, r_err_str);
  1332. } else {
  1333. parsing_tag = false;
  1334. }
  1335. if (token.type != TK_IDENTIFIER) {
  1336. r_err_str = "Expected Identifier";
  1337. return ERR_PARSE_ERROR;
  1338. }
  1339. String id = token.value;
  1340. if (parsing_tag) {
  1341. r_tag.name += id;
  1342. continue;
  1343. }
  1344. get_token(p_stream, token, line, r_err_str);
  1345. if (token.type != TK_EQUAL) {
  1346. return ERR_PARSE_ERROR;
  1347. }
  1348. get_token(p_stream, token, line, r_err_str);
  1349. Variant value;
  1350. Error err = parse_value(token, value, p_stream, line, r_err_str, p_res_parser);
  1351. if (err) {
  1352. return err;
  1353. }
  1354. r_tag.fields[id] = value;
  1355. }
  1356. return OK;
  1357. }
  1358. Error VariantParser::parse_tag(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser, bool p_simple_tag) {
  1359. Token token;
  1360. get_token(p_stream, token, line, r_err_str);
  1361. if (token.type == TK_EOF) {
  1362. return ERR_FILE_EOF;
  1363. }
  1364. if (token.type != TK_BRACKET_OPEN) {
  1365. r_err_str = "Expected '['";
  1366. return ERR_PARSE_ERROR;
  1367. }
  1368. return _parse_tag(token, p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1369. }
  1370. 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) {
  1371. //assign..
  1372. r_assign = "";
  1373. String what;
  1374. while (true) {
  1375. char32_t c;
  1376. if (p_stream->saved) {
  1377. c = p_stream->saved;
  1378. p_stream->saved = 0;
  1379. } else {
  1380. c = p_stream->get_char();
  1381. }
  1382. if (p_stream->is_eof()) {
  1383. return ERR_FILE_EOF;
  1384. }
  1385. if (c == ';') { //comment
  1386. while (true) {
  1387. char32_t ch = p_stream->get_char();
  1388. if (p_stream->is_eof()) {
  1389. return ERR_FILE_EOF;
  1390. }
  1391. if (ch == '\n') {
  1392. line++;
  1393. break;
  1394. }
  1395. }
  1396. continue;
  1397. }
  1398. if (c == '[' && what.length() == 0) {
  1399. //it's a tag!
  1400. p_stream->saved = '['; //go back one
  1401. Error err = parse_tag(p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1402. return err;
  1403. }
  1404. if (c > 32) {
  1405. if (c == '"') { //quoted
  1406. p_stream->saved = '"';
  1407. Token tk;
  1408. Error err = get_token(p_stream, tk, line, r_err_str);
  1409. if (err) {
  1410. return err;
  1411. }
  1412. if (tk.type != TK_STRING) {
  1413. r_err_str = "Error reading quoted string";
  1414. return ERR_INVALID_DATA;
  1415. }
  1416. what = tk.value;
  1417. } else if (c != '=') {
  1418. what += String::chr(c);
  1419. } else {
  1420. r_assign = what;
  1421. Token token;
  1422. get_token(p_stream, token, line, r_err_str);
  1423. Error err = parse_value(token, r_value, p_stream, line, r_err_str, p_res_parser);
  1424. return err;
  1425. }
  1426. } else if (c == '\n') {
  1427. line++;
  1428. }
  1429. }
  1430. }
  1431. Error VariantParser::parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser) {
  1432. Token token;
  1433. Error err = get_token(p_stream, token, r_err_line, r_err_str);
  1434. if (err) {
  1435. return err;
  1436. }
  1437. if (token.type == TK_EOF) {
  1438. return ERR_FILE_EOF;
  1439. }
  1440. return parse_value(token, r_ret, p_stream, r_err_line, r_err_str, p_res_parser);
  1441. }
  1442. //////////////////////////////////////////////////////////////////////////////////
  1443. //////////////////////////////////////////////////////////////////////////////////
  1444. //////////////////////////////////////////////////////////////////////////////////
  1445. static String rtos_fix(double p_value) {
  1446. if (p_value == 0.0) {
  1447. return "0"; //avoid negative zero (-0) being written, which may annoy git, svn, etc. for changes when they don't exist.
  1448. } else if (isnan(p_value)) {
  1449. return "nan";
  1450. } else if (isinf(p_value)) {
  1451. if (p_value > 0) {
  1452. return "inf";
  1453. } else {
  1454. return "inf_neg";
  1455. }
  1456. } else {
  1457. return rtoss(p_value);
  1458. }
  1459. }
  1460. 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 recursion_count) {
  1461. switch (p_variant.get_type()) {
  1462. case Variant::NIL: {
  1463. p_store_string_func(p_store_string_ud, "null");
  1464. } break;
  1465. case Variant::BOOL: {
  1466. p_store_string_func(p_store_string_ud, p_variant.operator bool() ? "true" : "false");
  1467. } break;
  1468. case Variant::INT: {
  1469. p_store_string_func(p_store_string_ud, itos(p_variant.operator int64_t()));
  1470. } break;
  1471. case Variant::FLOAT: {
  1472. String s = rtos_fix(p_variant.operator double());
  1473. if (s != "inf" && s != "inf_neg" && s != "nan") {
  1474. if (!s.contains(".") && !s.contains("e")) {
  1475. s += ".0";
  1476. }
  1477. }
  1478. p_store_string_func(p_store_string_ud, s);
  1479. } break;
  1480. case Variant::STRING: {
  1481. String str = p_variant;
  1482. str = "\"" + str.c_escape_multiline() + "\"";
  1483. p_store_string_func(p_store_string_ud, str);
  1484. } break;
  1485. case Variant::VECTOR2: {
  1486. Vector2 v = p_variant;
  1487. p_store_string_func(p_store_string_ud, "Vector2(" + rtos_fix(v.x) + ", " + rtos_fix(v.y) + ")");
  1488. } break;
  1489. case Variant::VECTOR2I: {
  1490. Vector2i v = p_variant;
  1491. p_store_string_func(p_store_string_ud, "Vector2i(" + itos(v.x) + ", " + itos(v.y) + ")");
  1492. } break;
  1493. case Variant::RECT2: {
  1494. Rect2 aabb = p_variant;
  1495. 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) + ")");
  1496. } break;
  1497. case Variant::RECT2I: {
  1498. Rect2i aabb = p_variant;
  1499. 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) + ")");
  1500. } break;
  1501. case Variant::VECTOR3: {
  1502. Vector3 v = p_variant;
  1503. p_store_string_func(p_store_string_ud, "Vector3(" + rtos_fix(v.x) + ", " + rtos_fix(v.y) + ", " + rtos_fix(v.z) + ")");
  1504. } break;
  1505. case Variant::VECTOR3I: {
  1506. Vector3i v = p_variant;
  1507. p_store_string_func(p_store_string_ud, "Vector3i(" + itos(v.x) + ", " + itos(v.y) + ", " + itos(v.z) + ")");
  1508. } break;
  1509. case Variant::VECTOR4: {
  1510. Vector4 v = p_variant;
  1511. 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) + ")");
  1512. } break;
  1513. case Variant::VECTOR4I: {
  1514. Vector4i v = p_variant;
  1515. p_store_string_func(p_store_string_ud, "Vector4i(" + itos(v.x) + ", " + itos(v.y) + ", " + itos(v.z) + ", " + itos(v.w) + ")");
  1516. } break;
  1517. case Variant::PLANE: {
  1518. Plane p = p_variant;
  1519. 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) + ")");
  1520. } break;
  1521. case Variant::AABB: {
  1522. AABB aabb = p_variant;
  1523. 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) + ")");
  1524. } break;
  1525. case Variant::QUATERNION: {
  1526. Quaternion quaternion = p_variant;
  1527. 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) + ")");
  1528. } break;
  1529. case Variant::TRANSFORM2D: {
  1530. String s = "Transform2D(";
  1531. Transform2D m3 = p_variant;
  1532. for (int i = 0; i < 3; i++) {
  1533. for (int j = 0; j < 2; j++) {
  1534. if (i != 0 || j != 0) {
  1535. s += ", ";
  1536. }
  1537. s += rtos_fix(m3.columns[i][j]);
  1538. }
  1539. }
  1540. p_store_string_func(p_store_string_ud, s + ")");
  1541. } break;
  1542. case Variant::BASIS: {
  1543. String s = "Basis(";
  1544. Basis m3 = p_variant;
  1545. for (int i = 0; i < 3; i++) {
  1546. for (int j = 0; j < 3; j++) {
  1547. if (i != 0 || j != 0) {
  1548. s += ", ";
  1549. }
  1550. s += rtos_fix(m3.rows[i][j]);
  1551. }
  1552. }
  1553. p_store_string_func(p_store_string_ud, s + ")");
  1554. } break;
  1555. case Variant::TRANSFORM3D: {
  1556. String s = "Transform3D(";
  1557. Transform3D t = p_variant;
  1558. Basis &m3 = t.basis;
  1559. for (int i = 0; i < 3; i++) {
  1560. for (int j = 0; j < 3; j++) {
  1561. if (i != 0 || j != 0) {
  1562. s += ", ";
  1563. }
  1564. s += rtos_fix(m3.rows[i][j]);
  1565. }
  1566. }
  1567. s = s + ", " + rtos_fix(t.origin.x) + ", " + rtos_fix(t.origin.y) + ", " + rtos_fix(t.origin.z);
  1568. p_store_string_func(p_store_string_ud, s + ")");
  1569. } break;
  1570. case Variant::PROJECTION: {
  1571. String s = "Projection(";
  1572. Projection t = p_variant;
  1573. for (int i = 0; i < 4; i++) {
  1574. for (int j = 0; j < 4; j++) {
  1575. if (i != 0 || j != 0) {
  1576. s += ", ";
  1577. }
  1578. s += rtos_fix(t.columns[i][j]);
  1579. }
  1580. }
  1581. p_store_string_func(p_store_string_ud, s + ")");
  1582. } break;
  1583. // misc types
  1584. case Variant::COLOR: {
  1585. Color c = p_variant;
  1586. 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) + ")");
  1587. } break;
  1588. case Variant::STRING_NAME: {
  1589. String str = p_variant;
  1590. str = "&\"" + str.c_escape() + "\"";
  1591. p_store_string_func(p_store_string_ud, str);
  1592. } break;
  1593. case Variant::NODE_PATH: {
  1594. String str = p_variant;
  1595. str = "NodePath(\"" + str.c_escape() + "\")";
  1596. p_store_string_func(p_store_string_ud, str);
  1597. } break;
  1598. case Variant::OBJECT: {
  1599. Object *obj = p_variant.get_validated_object();
  1600. if (!obj) {
  1601. p_store_string_func(p_store_string_ud, "null");
  1602. break; // don't save it
  1603. }
  1604. Ref<Resource> res = p_variant;
  1605. if (res.is_valid()) {
  1606. //is resource
  1607. String res_text;
  1608. //try external function
  1609. if (p_encode_res_func) {
  1610. res_text = p_encode_res_func(p_encode_res_ud, res);
  1611. }
  1612. //try path because it's a file
  1613. if (res_text.is_empty() && res->get_path().is_resource_file()) {
  1614. //external resource
  1615. String path = res->get_path();
  1616. res_text = "Resource(\"" + path + "\")";
  1617. }
  1618. //could come up with some sort of text
  1619. if (!res_text.is_empty()) {
  1620. p_store_string_func(p_store_string_ud, res_text);
  1621. break;
  1622. }
  1623. }
  1624. //store as generic object
  1625. p_store_string_func(p_store_string_ud, "Object(" + obj->get_class() + ",");
  1626. List<PropertyInfo> props;
  1627. obj->get_property_list(&props);
  1628. bool first = true;
  1629. for (const PropertyInfo &E : props) {
  1630. if (E.usage & PROPERTY_USAGE_STORAGE || E.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) {
  1631. //must be serialized
  1632. if (first) {
  1633. first = false;
  1634. } else {
  1635. p_store_string_func(p_store_string_ud, ",");
  1636. }
  1637. p_store_string_func(p_store_string_ud, "\"" + E.name + "\":");
  1638. write(obj->get(E.name), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1639. }
  1640. }
  1641. p_store_string_func(p_store_string_ud, ")\n");
  1642. } break;
  1643. case Variant::DICTIONARY: {
  1644. Dictionary dict = p_variant;
  1645. if (recursion_count > MAX_RECURSION) {
  1646. ERR_PRINT("Max recursion reached");
  1647. p_store_string_func(p_store_string_ud, "{}");
  1648. } else {
  1649. recursion_count++;
  1650. List<Variant> keys;
  1651. dict.get_key_list(&keys);
  1652. keys.sort();
  1653. if (keys.is_empty()) { // Avoid unnecessary line break.
  1654. p_store_string_func(p_store_string_ud, "{}");
  1655. break;
  1656. }
  1657. p_store_string_func(p_store_string_ud, "{\n");
  1658. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1659. write(E->get(), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, recursion_count);
  1660. p_store_string_func(p_store_string_ud, ": ");
  1661. write(dict[E->get()], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, recursion_count);
  1662. if (E->next()) {
  1663. p_store_string_func(p_store_string_ud, ",\n");
  1664. } else {
  1665. p_store_string_func(p_store_string_ud, "\n");
  1666. }
  1667. }
  1668. p_store_string_func(p_store_string_ud, "}");
  1669. }
  1670. } break;
  1671. case Variant::ARRAY: {
  1672. Array array = p_variant;
  1673. if (array.get_typed_builtin() != Variant::NIL) {
  1674. p_store_string_func(p_store_string_ud, "Array[");
  1675. Variant::Type builtin_type = (Variant::Type)array.get_typed_builtin();
  1676. StringName class_name = array.get_typed_class_name();
  1677. Ref<Script> script = array.get_typed_script();
  1678. if (script.is_valid()) {
  1679. String resource_text = String();
  1680. if (p_encode_res_func) {
  1681. resource_text = p_encode_res_func(p_encode_res_ud, script);
  1682. }
  1683. if (resource_text.is_empty() && script->get_path().is_resource_file()) {
  1684. resource_text = "Resource(\"" + script->get_path() + "\")";
  1685. }
  1686. if (!resource_text.is_empty()) {
  1687. p_store_string_func(p_store_string_ud, resource_text);
  1688. } else {
  1689. ERR_PRINT("Failed to encode a path to a custom script for an array type.");
  1690. p_store_string_func(p_store_string_ud, class_name);
  1691. }
  1692. } else if (class_name != StringName()) {
  1693. p_store_string_func(p_store_string_ud, class_name);
  1694. } else {
  1695. p_store_string_func(p_store_string_ud, Variant::get_type_name(builtin_type));
  1696. }
  1697. p_store_string_func(p_store_string_ud, "](");
  1698. }
  1699. if (recursion_count > MAX_RECURSION) {
  1700. ERR_PRINT("Max recursion reached");
  1701. p_store_string_func(p_store_string_ud, "[]");
  1702. } else {
  1703. recursion_count++;
  1704. p_store_string_func(p_store_string_ud, "[");
  1705. int len = array.size();
  1706. for (int i = 0; i < len; i++) {
  1707. if (i > 0) {
  1708. p_store_string_func(p_store_string_ud, ", ");
  1709. }
  1710. write(array[i], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, recursion_count);
  1711. }
  1712. p_store_string_func(p_store_string_ud, "]");
  1713. }
  1714. if (array.get_typed_builtin() != Variant::NIL) {
  1715. p_store_string_func(p_store_string_ud, ")");
  1716. }
  1717. } break;
  1718. case Variant::PACKED_BYTE_ARRAY: {
  1719. p_store_string_func(p_store_string_ud, "PackedByteArray(");
  1720. Vector<uint8_t> data = p_variant;
  1721. int len = data.size();
  1722. const uint8_t *ptr = data.ptr();
  1723. for (int i = 0; i < len; i++) {
  1724. if (i > 0) {
  1725. p_store_string_func(p_store_string_ud, ", ");
  1726. }
  1727. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  1728. }
  1729. p_store_string_func(p_store_string_ud, ")");
  1730. } break;
  1731. case Variant::PACKED_INT32_ARRAY: {
  1732. p_store_string_func(p_store_string_ud, "PackedInt32Array(");
  1733. Vector<int32_t> data = p_variant;
  1734. int32_t len = data.size();
  1735. const int32_t *ptr = data.ptr();
  1736. for (int32_t i = 0; i < len; i++) {
  1737. if (i > 0) {
  1738. p_store_string_func(p_store_string_ud, ", ");
  1739. }
  1740. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  1741. }
  1742. p_store_string_func(p_store_string_ud, ")");
  1743. } break;
  1744. case Variant::PACKED_INT64_ARRAY: {
  1745. p_store_string_func(p_store_string_ud, "PackedInt64Array(");
  1746. Vector<int64_t> data = p_variant;
  1747. int64_t len = data.size();
  1748. const int64_t *ptr = data.ptr();
  1749. for (int64_t i = 0; i < len; i++) {
  1750. if (i > 0) {
  1751. p_store_string_func(p_store_string_ud, ", ");
  1752. }
  1753. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  1754. }
  1755. p_store_string_func(p_store_string_ud, ")");
  1756. } break;
  1757. case Variant::PACKED_FLOAT32_ARRAY: {
  1758. p_store_string_func(p_store_string_ud, "PackedFloat32Array(");
  1759. Vector<float> data = p_variant;
  1760. int len = data.size();
  1761. const float *ptr = data.ptr();
  1762. for (int i = 0; i < len; i++) {
  1763. if (i > 0) {
  1764. p_store_string_func(p_store_string_ud, ", ");
  1765. }
  1766. p_store_string_func(p_store_string_ud, rtos_fix(ptr[i]));
  1767. }
  1768. p_store_string_func(p_store_string_ud, ")");
  1769. } break;
  1770. case Variant::PACKED_FLOAT64_ARRAY: {
  1771. p_store_string_func(p_store_string_ud, "PackedFloat64Array(");
  1772. Vector<double> data = p_variant;
  1773. int len = data.size();
  1774. const double *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, rtos_fix(ptr[i]));
  1780. }
  1781. p_store_string_func(p_store_string_ud, ")");
  1782. } break;
  1783. case Variant::PACKED_STRING_ARRAY: {
  1784. p_store_string_func(p_store_string_ud, "PackedStringArray(");
  1785. Vector<String> data = p_variant;
  1786. int len = data.size();
  1787. const String *ptr = data.ptr();
  1788. for (int 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, "\"" + ptr[i].c_escape() + "\"");
  1793. }
  1794. p_store_string_func(p_store_string_ud, ")");
  1795. } break;
  1796. case Variant::PACKED_VECTOR2_ARRAY: {
  1797. p_store_string_func(p_store_string_ud, "PackedVector2Array(");
  1798. Vector<Vector2> data = p_variant;
  1799. int len = data.size();
  1800. const Vector2 *ptr = data.ptr();
  1801. for (int 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, rtos_fix(ptr[i].x) + ", " + rtos_fix(ptr[i].y));
  1806. }
  1807. p_store_string_func(p_store_string_ud, ")");
  1808. } break;
  1809. case Variant::PACKED_VECTOR3_ARRAY: {
  1810. p_store_string_func(p_store_string_ud, "PackedVector3Array(");
  1811. Vector<Vector3> data = p_variant;
  1812. int len = data.size();
  1813. const Vector3 *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].x) + ", " + rtos_fix(ptr[i].y) + ", " + rtos_fix(ptr[i].z));
  1819. }
  1820. p_store_string_func(p_store_string_ud, ")");
  1821. } break;
  1822. case Variant::PACKED_COLOR_ARRAY: {
  1823. p_store_string_func(p_store_string_ud, "PackedColorArray(");
  1824. Vector<Color> data = p_variant;
  1825. int len = data.size();
  1826. const Color *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].r) + ", " + rtos_fix(ptr[i].g) + ", " + rtos_fix(ptr[i].b) + ", " + rtos_fix(ptr[i].a));
  1832. }
  1833. p_store_string_func(p_store_string_ud, ")");
  1834. } break;
  1835. default: {
  1836. }
  1837. }
  1838. return OK;
  1839. }
  1840. static Error _write_to_str(void *ud, const String &p_string) {
  1841. String *str = (String *)ud;
  1842. (*str) += p_string;
  1843. return OK;
  1844. }
  1845. Error VariantWriter::write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud) {
  1846. r_string = String();
  1847. return write(p_variant, _write_to_str, &r_string, p_encode_res_func, p_encode_res_ud);
  1848. }