variant_parser.cpp 67 KB

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