variant_parser.cpp 60 KB

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