variant_parser.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  1. /*************************************************************************/
  2. /* variant_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  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 "io/resource_loader.h"
  32. #include "os/input_event.h"
  33. #include "os/keyboard.h"
  34. CharType VariantParser::StreamFile::get_char() {
  35. return f->get_8();
  36. }
  37. bool VariantParser::StreamFile::is_utf8() const {
  38. return true;
  39. }
  40. bool VariantParser::StreamFile::is_eof() const {
  41. return f->eof_reached();
  42. }
  43. CharType VariantParser::StreamString::get_char() {
  44. if (pos >= s.length())
  45. return 0;
  46. else
  47. return s[pos++];
  48. }
  49. bool VariantParser::StreamString::is_utf8() const {
  50. return false;
  51. }
  52. bool VariantParser::StreamString::is_eof() const {
  53. return pos > s.length();
  54. }
  55. /////////////////////////////////////////////////////////////////////////////////////////////////
  56. const char *VariantParser::tk_name[TK_MAX] = {
  57. "'{'",
  58. "'}'",
  59. "'['",
  60. "']'",
  61. "'('",
  62. "')'",
  63. "identifier",
  64. "string",
  65. "number",
  66. "color",
  67. "':'",
  68. "','",
  69. "'.'",
  70. "'='",
  71. "EOF",
  72. "ERROR"
  73. };
  74. Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, String &r_err_str) {
  75. while (true) {
  76. CharType cchar;
  77. if (p_stream->saved) {
  78. cchar = p_stream->saved;
  79. p_stream->saved = 0;
  80. } else {
  81. cchar = p_stream->get_char();
  82. if (p_stream->is_eof()) {
  83. r_token.type = TK_EOF;
  84. return OK;
  85. }
  86. }
  87. switch (cchar) {
  88. case '\n': {
  89. line++;
  90. break;
  91. };
  92. case 0: {
  93. r_token.type = TK_EOF;
  94. return OK;
  95. } break;
  96. case '{': {
  97. r_token.type = TK_CURLY_BRACKET_OPEN;
  98. return OK;
  99. };
  100. case '}': {
  101. r_token.type = TK_CURLY_BRACKET_CLOSE;
  102. return OK;
  103. };
  104. case '[': {
  105. r_token.type = TK_BRACKET_OPEN;
  106. return OK;
  107. };
  108. case ']': {
  109. r_token.type = TK_BRACKET_CLOSE;
  110. return OK;
  111. };
  112. case '(': {
  113. r_token.type = TK_PARENTHESIS_OPEN;
  114. return OK;
  115. };
  116. case ')': {
  117. r_token.type = TK_PARENTHESIS_CLOSE;
  118. return OK;
  119. };
  120. case ':': {
  121. r_token.type = TK_COLON;
  122. return OK;
  123. };
  124. case ';': {
  125. while (true) {
  126. CharType ch = p_stream->get_char();
  127. if (p_stream->is_eof()) {
  128. r_token.type = TK_EOF;
  129. return OK;
  130. }
  131. if (ch == '\n')
  132. break;
  133. }
  134. break;
  135. };
  136. case ',': {
  137. r_token.type = TK_COMMA;
  138. return OK;
  139. };
  140. case '.': {
  141. r_token.type = TK_PERIOD;
  142. return OK;
  143. };
  144. case '=': {
  145. r_token.type = TK_EQUAL;
  146. return OK;
  147. };
  148. case '#': {
  149. String color_str = "#";
  150. while (true) {
  151. CharType ch = p_stream->get_char();
  152. if (p_stream->is_eof()) {
  153. r_token.type = TK_EOF;
  154. return OK;
  155. } else if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
  156. color_str += String::chr(ch);
  157. } else {
  158. p_stream->saved = ch;
  159. break;
  160. }
  161. }
  162. r_token.value = Color::html(color_str);
  163. r_token.type = TK_COLOR;
  164. return OK;
  165. };
  166. case '"': {
  167. String str;
  168. while (true) {
  169. CharType ch = p_stream->get_char();
  170. if (ch == 0) {
  171. r_err_str = "Unterminated String";
  172. r_token.type = TK_ERROR;
  173. return ERR_PARSE_ERROR;
  174. } else if (ch == '"') {
  175. break;
  176. } else if (ch == '\\') {
  177. //escaped characters...
  178. CharType next = p_stream->get_char();
  179. if (next == 0) {
  180. r_err_str = "Unterminated String";
  181. r_token.type = TK_ERROR;
  182. return ERR_PARSE_ERROR;
  183. }
  184. CharType res = 0;
  185. switch (next) {
  186. case 'b': res = 8; break;
  187. case 't': res = 9; break;
  188. case 'n': res = 10; break;
  189. case 'f': res = 12; break;
  190. case 'r': res = 13; break;
  191. case 'u': {
  192. //hexnumbarh - oct is deprecated
  193. for (int j = 0; j < 4; j++) {
  194. CharType c = p_stream->get_char();
  195. if (c == 0) {
  196. r_err_str = "Unterminated String";
  197. r_token.type = TK_ERROR;
  198. return ERR_PARSE_ERROR;
  199. }
  200. if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
  201. r_err_str = "Malformed hex constant in string";
  202. r_token.type = TK_ERROR;
  203. return ERR_PARSE_ERROR;
  204. }
  205. CharType v;
  206. if (c >= '0' && c <= '9') {
  207. v = c - '0';
  208. } else if (c >= 'a' && c <= 'f') {
  209. v = c - 'a';
  210. v += 10;
  211. } else if (c >= 'A' && c <= 'F') {
  212. v = c - 'A';
  213. v += 10;
  214. } else {
  215. ERR_PRINT("BUG");
  216. v = 0;
  217. }
  218. res <<= 4;
  219. res |= v;
  220. }
  221. } break;
  222. //case '\"': res='\"'; break;
  223. //case '\\': res='\\'; break;
  224. //case '/': res='/'; break;
  225. default: {
  226. res = next;
  227. //r_err_str="Invalid escape sequence";
  228. //return ERR_PARSE_ERROR;
  229. } break;
  230. }
  231. str += res;
  232. } else {
  233. if (ch == '\n')
  234. line++;
  235. str += ch;
  236. }
  237. }
  238. if (p_stream->is_utf8()) {
  239. str.parse_utf8(str.ascii(true).get_data());
  240. }
  241. r_token.type = TK_STRING;
  242. r_token.value = str;
  243. return OK;
  244. } break;
  245. default: {
  246. if (cchar <= 32) {
  247. break;
  248. }
  249. if (cchar == '-' || (cchar >= '0' && cchar <= '9')) {
  250. //a number
  251. String num;
  252. #define READING_SIGN 0
  253. #define READING_INT 1
  254. #define READING_DEC 2
  255. #define READING_EXP 3
  256. #define READING_DONE 4
  257. int reading = READING_INT;
  258. if (cchar == '-') {
  259. num += '-';
  260. cchar = p_stream->get_char();
  261. }
  262. CharType c = cchar;
  263. bool exp_sign = false;
  264. bool exp_beg = false;
  265. bool is_float = false;
  266. while (true) {
  267. switch (reading) {
  268. case READING_INT: {
  269. if (c >= '0' && c <= '9') {
  270. //pass
  271. } else if (c == '.') {
  272. reading = READING_DEC;
  273. is_float = true;
  274. } else if (c == 'e') {
  275. reading = READING_EXP;
  276. } else {
  277. reading = READING_DONE;
  278. }
  279. } break;
  280. case READING_DEC: {
  281. if (c >= '0' && c <= '9') {
  282. } else if (c == 'e') {
  283. reading = READING_EXP;
  284. } else {
  285. reading = READING_DONE;
  286. }
  287. } break;
  288. case READING_EXP: {
  289. if (c >= '0' && c <= '9') {
  290. exp_beg = true;
  291. } else if ((c == '-' || c == '+') && !exp_sign && !exp_beg) {
  292. if (c == '-')
  293. is_float = true;
  294. exp_sign = true;
  295. } else {
  296. reading = READING_DONE;
  297. }
  298. } break;
  299. }
  300. if (reading == READING_DONE)
  301. break;
  302. num += String::chr(c);
  303. c = p_stream->get_char();
  304. }
  305. p_stream->saved = c;
  306. r_token.type = TK_NUMBER;
  307. if (is_float)
  308. r_token.value = num.to_double();
  309. else
  310. r_token.value = num.to_int();
  311. return OK;
  312. } else if ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_') {
  313. String id;
  314. bool first = true;
  315. while ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_' || (!first && cchar >= '0' && cchar <= '9')) {
  316. id += String::chr(cchar);
  317. cchar = p_stream->get_char();
  318. first = false;
  319. }
  320. p_stream->saved = cchar;
  321. r_token.type = TK_IDENTIFIER;
  322. r_token.value = id;
  323. return OK;
  324. } else {
  325. r_err_str = "Unexpected character.";
  326. r_token.type = TK_ERROR;
  327. return ERR_PARSE_ERROR;
  328. }
  329. }
  330. }
  331. }
  332. r_token.type = TK_ERROR;
  333. return ERR_PARSE_ERROR;
  334. }
  335. Error VariantParser::_parse_enginecfg(Stream *p_stream, Vector<String> &strings, int &line, String &r_err_str) {
  336. Token token;
  337. get_token(p_stream, token, line, r_err_str);
  338. if (token.type != TK_PARENTHESIS_OPEN) {
  339. r_err_str = "Expected '(' in old-style project.godot construct";
  340. return ERR_PARSE_ERROR;
  341. }
  342. String accum;
  343. while (true) {
  344. CharType c = p_stream->get_char();
  345. if (p_stream->is_eof()) {
  346. r_err_str = "Unexpected EOF while parsing old-style project.godot construct";
  347. return ERR_PARSE_ERROR;
  348. }
  349. if (c == ',') {
  350. strings.push_back(accum.strip_edges());
  351. accum = String();
  352. } else if (c == ')') {
  353. strings.push_back(accum.strip_edges());
  354. return OK;
  355. } else if (c == '\n') {
  356. line++;
  357. }
  358. }
  359. return OK;
  360. }
  361. template <class T>
  362. Error VariantParser::_parse_construct(Stream *p_stream, Vector<T> &r_construct, int &line, String &r_err_str) {
  363. Token token;
  364. get_token(p_stream, token, line, r_err_str);
  365. if (token.type != TK_PARENTHESIS_OPEN) {
  366. r_err_str = "Expected '(' in constructor";
  367. return ERR_PARSE_ERROR;
  368. }
  369. bool first = true;
  370. while (true) {
  371. if (!first) {
  372. get_token(p_stream, token, line, r_err_str);
  373. if (token.type == TK_COMMA) {
  374. //do none
  375. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  376. break;
  377. } else {
  378. r_err_str = "Expected ',' or ')' in constructor";
  379. return ERR_PARSE_ERROR;
  380. }
  381. }
  382. get_token(p_stream, token, line, r_err_str);
  383. if (first && token.type == TK_PARENTHESIS_CLOSE) {
  384. break;
  385. } else if (token.type != TK_NUMBER) {
  386. r_err_str = "Expected float in constructor";
  387. return ERR_PARSE_ERROR;
  388. }
  389. r_construct.push_back(token.value);
  390. first = false;
  391. }
  392. return OK;
  393. }
  394. Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  395. /* {
  396. Error err = get_token(p_stream,token,line,r_err_str);
  397. if (err)
  398. return err;
  399. }*/
  400. if (token.type == TK_CURLY_BRACKET_OPEN) {
  401. Dictionary d;
  402. Error err = _parse_dictionary(d, p_stream, line, r_err_str, p_res_parser);
  403. if (err)
  404. return err;
  405. value = d;
  406. return OK;
  407. } else if (token.type == TK_BRACKET_OPEN) {
  408. Array a;
  409. Error err = _parse_array(a, p_stream, line, r_err_str, p_res_parser);
  410. if (err)
  411. return err;
  412. value = a;
  413. return OK;
  414. } else if (token.type == TK_IDENTIFIER) {
  415. String id = token.value;
  416. if (id == "true")
  417. value = true;
  418. else if (id == "false")
  419. value = false;
  420. else if (id == "null" || id == "nil")
  421. value = Variant();
  422. else if (id == "Vector2") {
  423. Vector<float> args;
  424. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  425. if (err)
  426. return err;
  427. if (args.size() != 2) {
  428. r_err_str = "Expected 2 arguments for constructor";
  429. }
  430. value = Vector2(args[0], args[1]);
  431. return OK;
  432. } else if (id == "Rect2") {
  433. Vector<float> args;
  434. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  435. if (err)
  436. return err;
  437. if (args.size() != 4) {
  438. r_err_str = "Expected 4 arguments for constructor";
  439. }
  440. value = Rect2(args[0], args[1], args[2], args[3]);
  441. return OK;
  442. } else if (id == "Vector3") {
  443. Vector<float> args;
  444. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  445. if (err)
  446. return err;
  447. if (args.size() != 3) {
  448. r_err_str = "Expected 3 arguments for constructor";
  449. }
  450. value = Vector3(args[0], args[1], args[2]);
  451. return OK;
  452. } else if (id == "Transform2D" || id == "Matrix32") { //compatibility
  453. Vector<float> args;
  454. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  455. if (err)
  456. return err;
  457. if (args.size() != 6) {
  458. r_err_str = "Expected 6 arguments for constructor";
  459. }
  460. Transform2D m;
  461. m[0] = Vector2(args[0], args[1]);
  462. m[1] = Vector2(args[2], args[3]);
  463. m[2] = Vector2(args[4], args[5]);
  464. value = m;
  465. return OK;
  466. } else if (id == "Plane") {
  467. Vector<float> args;
  468. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  469. if (err)
  470. return err;
  471. if (args.size() != 4) {
  472. r_err_str = "Expected 4 arguments for constructor";
  473. }
  474. value = Plane(args[0], args[1], args[2], args[3]);
  475. return OK;
  476. } else if (id == "Quat") {
  477. Vector<float> args;
  478. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  479. if (err)
  480. return err;
  481. if (args.size() != 4) {
  482. r_err_str = "Expected 4 arguments for constructor";
  483. }
  484. value = Quat(args[0], args[1], args[2], args[3]);
  485. return OK;
  486. } else if (id == "Rect3" || id == "AABB") {
  487. Vector<float> args;
  488. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  489. if (err)
  490. return err;
  491. if (args.size() != 6) {
  492. r_err_str = "Expected 6 arguments for constructor";
  493. }
  494. value = Rect3(Vector3(args[0], args[1], args[2]), Vector3(args[3], args[4], args[5]));
  495. return OK;
  496. } else if (id == "Basis" || id == "Matrix3") { //compatibility
  497. Vector<float> args;
  498. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  499. if (err)
  500. return err;
  501. if (args.size() != 9) {
  502. r_err_str = "Expected 9 arguments for constructor";
  503. }
  504. value = Basis(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
  505. return OK;
  506. } else if (id == "Transform") {
  507. Vector<float> args;
  508. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  509. if (err)
  510. return err;
  511. if (args.size() != 12) {
  512. r_err_str = "Expected 12 arguments for constructor";
  513. }
  514. value = Transform(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]));
  515. return OK;
  516. } else if (id == "Color") {
  517. Vector<float> args;
  518. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  519. if (err)
  520. return err;
  521. if (args.size() != 4) {
  522. r_err_str = "Expected 4 arguments for constructor";
  523. }
  524. value = Color(args[0], args[1], args[2], args[3]);
  525. return OK;
  526. } else if (id == "NodePath") {
  527. get_token(p_stream, token, line, r_err_str);
  528. if (token.type != TK_PARENTHESIS_OPEN) {
  529. r_err_str = "Expected '('";
  530. return ERR_PARSE_ERROR;
  531. }
  532. get_token(p_stream, token, line, r_err_str);
  533. if (token.type != TK_STRING) {
  534. r_err_str = "Expected string as argument for NodePath()";
  535. return ERR_PARSE_ERROR;
  536. }
  537. value = NodePath(String(token.value));
  538. get_token(p_stream, token, line, r_err_str);
  539. if (token.type != TK_PARENTHESIS_CLOSE) {
  540. r_err_str = "Expected ')'";
  541. return ERR_PARSE_ERROR;
  542. }
  543. } else if (id == "RID") {
  544. get_token(p_stream, token, line, r_err_str);
  545. if (token.type != TK_PARENTHESIS_OPEN) {
  546. r_err_str = "Expected '('";
  547. return ERR_PARSE_ERROR;
  548. }
  549. get_token(p_stream, token, line, r_err_str);
  550. if (token.type != TK_NUMBER) {
  551. r_err_str = "Expected number as argument";
  552. return ERR_PARSE_ERROR;
  553. }
  554. value = token.value;
  555. get_token(p_stream, token, line, r_err_str);
  556. if (token.type != TK_PARENTHESIS_CLOSE) {
  557. r_err_str = "Expected ')'";
  558. return ERR_PARSE_ERROR;
  559. }
  560. return OK;
  561. } else if (id == "Object") {
  562. get_token(p_stream, token, line, r_err_str);
  563. if (token.type != TK_PARENTHESIS_OPEN) {
  564. r_err_str = "Expected '('";
  565. return ERR_PARSE_ERROR;
  566. }
  567. get_token(p_stream, token, line, r_err_str);
  568. if (token.type != TK_IDENTIFIER) {
  569. r_err_str = "Expected identifier with type of object";
  570. return ERR_PARSE_ERROR;
  571. }
  572. String type = token.value;
  573. Object *obj = ClassDB::instance(type);
  574. if (!obj) {
  575. r_err_str = "Can't instance Object() of type: " + type;
  576. return ERR_PARSE_ERROR;
  577. }
  578. get_token(p_stream, token, line, r_err_str);
  579. if (token.type != TK_COMMA) {
  580. r_err_str = "Expected ',' after object type";
  581. return ERR_PARSE_ERROR;
  582. }
  583. bool at_key = true;
  584. String key;
  585. Token token;
  586. bool need_comma = false;
  587. while (true) {
  588. if (p_stream->is_eof()) {
  589. r_err_str = "Unexpected End of File while parsing Object()";
  590. return ERR_FILE_CORRUPT;
  591. }
  592. if (at_key) {
  593. Error err = get_token(p_stream, token, line, r_err_str);
  594. if (err != OK)
  595. return err;
  596. if (token.type == TK_PARENTHESIS_CLOSE) {
  597. Reference *reference = obj->cast_to<Reference>();
  598. if (reference) {
  599. value = REF(reference);
  600. } else {
  601. value = obj;
  602. }
  603. return OK;
  604. }
  605. if (need_comma) {
  606. if (token.type != TK_COMMA) {
  607. r_err_str = "Expected '}' or ','";
  608. return ERR_PARSE_ERROR;
  609. } else {
  610. need_comma = false;
  611. continue;
  612. }
  613. }
  614. if (token.type != TK_STRING) {
  615. r_err_str = "Expected property name as string";
  616. return ERR_PARSE_ERROR;
  617. }
  618. key = token.value;
  619. err = get_token(p_stream, token, line, r_err_str);
  620. if (err != OK)
  621. return err;
  622. if (token.type != TK_COLON) {
  623. r_err_str = "Expected ':'";
  624. return ERR_PARSE_ERROR;
  625. }
  626. at_key = false;
  627. } else {
  628. Error err = get_token(p_stream, token, line, r_err_str);
  629. if (err != OK)
  630. return err;
  631. Variant v;
  632. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  633. if (err)
  634. return err;
  635. obj->set(key, v);
  636. need_comma = true;
  637. at_key = true;
  638. }
  639. }
  640. return OK;
  641. } else if (id == "Resource" || id == "SubResource" || id == "ExtResource") {
  642. get_token(p_stream, token, line, r_err_str);
  643. if (token.type != TK_PARENTHESIS_OPEN) {
  644. r_err_str = "Expected '('";
  645. return ERR_PARSE_ERROR;
  646. }
  647. if (p_res_parser && id == "Resource" && p_res_parser->func) {
  648. RES res;
  649. Error err = p_res_parser->func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  650. if (err)
  651. return err;
  652. value = res;
  653. return OK;
  654. } else if (p_res_parser && id == "ExtResource" && p_res_parser->ext_func) {
  655. RES res;
  656. Error err = p_res_parser->ext_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  657. if (err)
  658. return err;
  659. value = res;
  660. return OK;
  661. } else if (p_res_parser && id == "SubResource" && p_res_parser->sub_func) {
  662. RES res;
  663. Error err = p_res_parser->sub_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  664. if (err)
  665. return err;
  666. value = res;
  667. return OK;
  668. } else {
  669. get_token(p_stream, token, line, r_err_str);
  670. if (token.type == TK_STRING) {
  671. String path = token.value;
  672. RES res = ResourceLoader::load(path);
  673. if (res.is_null()) {
  674. r_err_str = "Can't load resource at path: '" + path + "'.";
  675. return ERR_PARSE_ERROR;
  676. }
  677. get_token(p_stream, token, line, r_err_str);
  678. if (token.type != TK_PARENTHESIS_CLOSE) {
  679. r_err_str = "Expected ')'";
  680. return ERR_PARSE_ERROR;
  681. }
  682. value = res;
  683. return OK;
  684. } else {
  685. r_err_str = "Expected string as argument for Resource().";
  686. return ERR_PARSE_ERROR;
  687. }
  688. }
  689. return OK;
  690. #ifndef DISABLE_DEPRECATED
  691. } else if (id == "InputEvent") {
  692. get_token(p_stream, token, line, r_err_str);
  693. if (token.type != TK_PARENTHESIS_OPEN) {
  694. r_err_str = "Expected '('";
  695. return ERR_PARSE_ERROR;
  696. }
  697. get_token(p_stream, token, line, r_err_str);
  698. if (token.type != TK_IDENTIFIER) {
  699. r_err_str = "Expected identifier";
  700. return ERR_PARSE_ERROR;
  701. }
  702. String id = token.value;
  703. Ref<InputEvent> ie;
  704. if (id == "NONE") {
  705. get_token(p_stream, token, line, r_err_str);
  706. if (token.type != TK_PARENTHESIS_CLOSE) {
  707. r_err_str = "Expected ')'";
  708. return ERR_PARSE_ERROR;
  709. }
  710. } else if (id == "KEY") {
  711. Ref<InputEventKey> key;
  712. key.instance();
  713. ie = key;
  714. get_token(p_stream, token, line, r_err_str);
  715. if (token.type != TK_COMMA) {
  716. r_err_str = "Expected ','";
  717. return ERR_PARSE_ERROR;
  718. }
  719. get_token(p_stream, token, line, r_err_str);
  720. if (token.type == TK_IDENTIFIER) {
  721. String name = token.value;
  722. key->set_scancode(find_keycode(name));
  723. } else if (token.type == TK_NUMBER) {
  724. key->set_scancode(token.value);
  725. } else {
  726. r_err_str = "Expected string or integer for keycode";
  727. return ERR_PARSE_ERROR;
  728. }
  729. get_token(p_stream, token, line, r_err_str);
  730. if (token.type == TK_COMMA) {
  731. get_token(p_stream, token, line, r_err_str);
  732. if (token.type != TK_IDENTIFIER) {
  733. r_err_str = "Expected identifier with modifier flas";
  734. return ERR_PARSE_ERROR;
  735. }
  736. String mods = token.value;
  737. if (mods.findn("C") != -1)
  738. key->set_control(true);
  739. if (mods.findn("A") != -1)
  740. key->set_alt(true);
  741. if (mods.findn("S") != -1)
  742. key->set_shift(true);
  743. if (mods.findn("M") != -1)
  744. key->set_metakey(true);
  745. get_token(p_stream, token, line, r_err_str);
  746. if (token.type != TK_PARENTHESIS_CLOSE) {
  747. r_err_str = "Expected ')'";
  748. return ERR_PARSE_ERROR;
  749. }
  750. } else if (token.type != TK_PARENTHESIS_CLOSE) {
  751. r_err_str = "Expected ')' or modifier flags.";
  752. return ERR_PARSE_ERROR;
  753. }
  754. } else if (id == "MBUTTON") {
  755. Ref<InputEventMouseButton> mb;
  756. mb.instance();
  757. ie = mb;
  758. get_token(p_stream, token, line, r_err_str);
  759. if (token.type != TK_COMMA) {
  760. r_err_str = "Expected ','";
  761. return ERR_PARSE_ERROR;
  762. }
  763. get_token(p_stream, token, line, r_err_str);
  764. if (token.type != TK_NUMBER) {
  765. r_err_str = "Expected button index";
  766. return ERR_PARSE_ERROR;
  767. }
  768. mb->set_button_index(token.value);
  769. get_token(p_stream, token, line, r_err_str);
  770. if (token.type != TK_PARENTHESIS_CLOSE) {
  771. r_err_str = "Expected ')'";
  772. return ERR_PARSE_ERROR;
  773. }
  774. } else if (id == "JBUTTON") {
  775. Ref<InputEventJoypadButton> jb;
  776. jb.instance();
  777. ie = jb;
  778. get_token(p_stream, token, line, r_err_str);
  779. if (token.type != TK_COMMA) {
  780. r_err_str = "Expected ','";
  781. return ERR_PARSE_ERROR;
  782. }
  783. get_token(p_stream, token, line, r_err_str);
  784. if (token.type != TK_NUMBER) {
  785. r_err_str = "Expected button index";
  786. return ERR_PARSE_ERROR;
  787. }
  788. jb->set_button_index(token.value);
  789. get_token(p_stream, token, line, r_err_str);
  790. if (token.type != TK_PARENTHESIS_CLOSE) {
  791. r_err_str = "Expected ')'";
  792. return ERR_PARSE_ERROR;
  793. }
  794. } else if (id == "JAXIS") {
  795. Ref<InputEventJoypadMotion> jm;
  796. jm.instance();
  797. ie = jm;
  798. get_token(p_stream, token, line, r_err_str);
  799. if (token.type != TK_COMMA) {
  800. r_err_str = "Expected ','";
  801. return ERR_PARSE_ERROR;
  802. }
  803. get_token(p_stream, token, line, r_err_str);
  804. if (token.type != TK_NUMBER) {
  805. r_err_str = "Expected axis index";
  806. return ERR_PARSE_ERROR;
  807. }
  808. jm->set_axis(token.value);
  809. get_token(p_stream, token, line, r_err_str);
  810. if (token.type != TK_COMMA) {
  811. r_err_str = "Expected ',' after axis index";
  812. return ERR_PARSE_ERROR;
  813. }
  814. get_token(p_stream, token, line, r_err_str);
  815. if (token.type != TK_NUMBER) {
  816. r_err_str = "Expected axis sign";
  817. return ERR_PARSE_ERROR;
  818. }
  819. jm->set_axis_value(token.value);
  820. get_token(p_stream, token, line, r_err_str);
  821. if (token.type != TK_PARENTHESIS_CLOSE) {
  822. r_err_str = "Expected ')' for jaxis";
  823. return ERR_PARSE_ERROR;
  824. }
  825. } else {
  826. r_err_str = "Invalid input event type.";
  827. return ERR_PARSE_ERROR;
  828. }
  829. value = ie;
  830. return OK;
  831. #endif
  832. } else if (id == "PoolByteArray" || id == "ByteArray") {
  833. Vector<uint8_t> args;
  834. Error err = _parse_construct<uint8_t>(p_stream, args, line, r_err_str);
  835. if (err)
  836. return err;
  837. PoolVector<uint8_t> arr;
  838. {
  839. int len = args.size();
  840. arr.resize(len);
  841. PoolVector<uint8_t>::Write w = arr.write();
  842. for (int i = 0; i < len; i++) {
  843. w[i] = args[i];
  844. }
  845. }
  846. value = arr;
  847. return OK;
  848. } else if (id == "PoolIntArray" || id == "IntArray") {
  849. Vector<int> args;
  850. Error err = _parse_construct<int>(p_stream, args, line, r_err_str);
  851. if (err)
  852. return err;
  853. PoolVector<int> arr;
  854. {
  855. int len = args.size();
  856. arr.resize(len);
  857. PoolVector<int>::Write w = arr.write();
  858. for (int i = 0; i < len; i++) {
  859. w[i] = int(args[i]);
  860. }
  861. }
  862. value = arr;
  863. return OK;
  864. } else if (id == "PoolFloatArray" || id == "FloatArray") {
  865. Vector<float> args;
  866. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  867. if (err)
  868. return err;
  869. PoolVector<float> arr;
  870. {
  871. int len = args.size();
  872. arr.resize(len);
  873. PoolVector<float>::Write w = arr.write();
  874. for (int i = 0; i < len; i++) {
  875. w[i] = args[i];
  876. }
  877. }
  878. value = arr;
  879. return OK;
  880. } else if (id == "PoolStringArray" || id == "StringArray") {
  881. get_token(p_stream, token, line, r_err_str);
  882. if (token.type != TK_PARENTHESIS_OPEN) {
  883. r_err_str = "Expected '('";
  884. return ERR_PARSE_ERROR;
  885. }
  886. Vector<String> cs;
  887. bool first = true;
  888. while (true) {
  889. if (!first) {
  890. get_token(p_stream, token, line, r_err_str);
  891. if (token.type == TK_COMMA) {
  892. //do none
  893. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  894. break;
  895. } else {
  896. r_err_str = "Expected ',' or ')'";
  897. return ERR_PARSE_ERROR;
  898. }
  899. }
  900. get_token(p_stream, token, line, r_err_str);
  901. if (token.type == TK_PARENTHESIS_CLOSE) {
  902. break;
  903. } else if (token.type != TK_STRING) {
  904. r_err_str = "Expected string";
  905. return ERR_PARSE_ERROR;
  906. }
  907. first = false;
  908. cs.push_back(token.value);
  909. }
  910. PoolVector<String> arr;
  911. {
  912. int len = cs.size();
  913. arr.resize(len);
  914. PoolVector<String>::Write w = arr.write();
  915. for (int i = 0; i < len; i++) {
  916. w[i] = cs[i];
  917. }
  918. }
  919. value = arr;
  920. return OK;
  921. } else if (id == "PoolVector2Array" || id == "Vector2Array") {
  922. Vector<float> args;
  923. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  924. if (err)
  925. return err;
  926. PoolVector<Vector2> arr;
  927. {
  928. int len = args.size() / 2;
  929. arr.resize(len);
  930. PoolVector<Vector2>::Write w = arr.write();
  931. for (int i = 0; i < len; i++) {
  932. w[i] = Vector2(args[i * 2 + 0], args[i * 2 + 1]);
  933. }
  934. }
  935. value = arr;
  936. return OK;
  937. } else if (id == "PoolVector3Array" || id == "Vector3Array") {
  938. Vector<float> args;
  939. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  940. if (err)
  941. return err;
  942. PoolVector<Vector3> arr;
  943. {
  944. int len = args.size() / 3;
  945. arr.resize(len);
  946. PoolVector<Vector3>::Write w = arr.write();
  947. for (int i = 0; i < len; i++) {
  948. w[i] = Vector3(args[i * 3 + 0], args[i * 3 + 1], args[i * 3 + 2]);
  949. }
  950. }
  951. value = arr;
  952. return OK;
  953. } else if (id == "PoolColorArray" || id == "ColorArray") {
  954. Vector<float> args;
  955. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  956. if (err)
  957. return err;
  958. PoolVector<Color> arr;
  959. {
  960. int len = args.size() / 4;
  961. arr.resize(len);
  962. PoolVector<Color>::Write w = arr.write();
  963. for (int i = 0; i < len; i++) {
  964. w[i] = Color(args[i * 4 + 0], args[i * 4 + 1], args[i * 4 + 2], args[i * 4 + 3]);
  965. }
  966. }
  967. value = arr;
  968. return OK;
  969. } else {
  970. r_err_str = "Unexpected identifier: '" + id + "'.";
  971. return ERR_PARSE_ERROR;
  972. }
  973. return OK;
  974. } else if (token.type == TK_NUMBER) {
  975. value = token.value;
  976. return OK;
  977. } else if (token.type == TK_STRING) {
  978. value = token.value;
  979. return OK;
  980. } else if (token.type == TK_COLOR) {
  981. value = token.value;
  982. return OK;
  983. } else {
  984. r_err_str = "Expected value, got " + String(tk_name[token.type]) + ".";
  985. return ERR_PARSE_ERROR;
  986. }
  987. return ERR_PARSE_ERROR;
  988. }
  989. Error VariantParser::_parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  990. Token token;
  991. bool need_comma = false;
  992. while (true) {
  993. if (p_stream->is_eof()) {
  994. r_err_str = "Unexpected End of File while parsing array";
  995. return ERR_FILE_CORRUPT;
  996. }
  997. Error err = get_token(p_stream, token, line, r_err_str);
  998. if (err != OK)
  999. return err;
  1000. if (token.type == TK_BRACKET_CLOSE) {
  1001. return OK;
  1002. }
  1003. if (need_comma) {
  1004. if (token.type != TK_COMMA) {
  1005. r_err_str = "Expected ','";
  1006. return ERR_PARSE_ERROR;
  1007. } else {
  1008. need_comma = false;
  1009. continue;
  1010. }
  1011. }
  1012. Variant v;
  1013. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1014. if (err)
  1015. return err;
  1016. array.push_back(v);
  1017. need_comma = true;
  1018. }
  1019. return OK;
  1020. }
  1021. Error VariantParser::_parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  1022. bool at_key = true;
  1023. Variant key;
  1024. Token token;
  1025. bool need_comma = false;
  1026. while (true) {
  1027. if (p_stream->is_eof()) {
  1028. r_err_str = "Unexpected End of File while parsing dictionary";
  1029. return ERR_FILE_CORRUPT;
  1030. }
  1031. if (at_key) {
  1032. Error err = get_token(p_stream, token, line, r_err_str);
  1033. if (err != OK)
  1034. return err;
  1035. if (token.type == TK_CURLY_BRACKET_CLOSE) {
  1036. return OK;
  1037. }
  1038. if (need_comma) {
  1039. if (token.type != TK_COMMA) {
  1040. r_err_str = "Expected '}' or ','";
  1041. return ERR_PARSE_ERROR;
  1042. } else {
  1043. need_comma = false;
  1044. continue;
  1045. }
  1046. }
  1047. err = parse_value(token, key, p_stream, line, r_err_str, p_res_parser);
  1048. if (err)
  1049. return err;
  1050. err = get_token(p_stream, token, line, r_err_str);
  1051. if (err != OK)
  1052. return err;
  1053. if (token.type != TK_COLON) {
  1054. r_err_str = "Expected ':'";
  1055. return ERR_PARSE_ERROR;
  1056. }
  1057. at_key = false;
  1058. } else {
  1059. Error err = get_token(p_stream, token, line, r_err_str);
  1060. if (err != OK)
  1061. return err;
  1062. Variant v;
  1063. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1064. if (err)
  1065. return err;
  1066. object[key] = v;
  1067. need_comma = true;
  1068. at_key = true;
  1069. }
  1070. }
  1071. return OK;
  1072. }
  1073. 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) {
  1074. r_tag.fields.clear();
  1075. if (token.type != TK_BRACKET_OPEN) {
  1076. r_err_str = "Expected '['";
  1077. return ERR_PARSE_ERROR;
  1078. }
  1079. if (p_simple_tag) {
  1080. r_tag.name = "";
  1081. r_tag.fields.clear();
  1082. while (true) {
  1083. CharType c = p_stream->get_char();
  1084. if (p_stream->is_eof()) {
  1085. r_err_str = "Unexpected EOF while parsing simple tag";
  1086. return ERR_PARSE_ERROR;
  1087. }
  1088. if (c == ']')
  1089. break;
  1090. r_tag.name += String::chr(c);
  1091. }
  1092. r_tag.name = r_tag.name.strip_edges();
  1093. return OK;
  1094. }
  1095. get_token(p_stream, token, line, r_err_str);
  1096. if (token.type != TK_IDENTIFIER) {
  1097. r_err_str = "Expected identifier (tag name)";
  1098. return ERR_PARSE_ERROR;
  1099. }
  1100. r_tag.name = token.value;
  1101. bool parsing_tag = true;
  1102. while (true) {
  1103. if (p_stream->is_eof()) {
  1104. r_err_str = "Unexpected End of File while parsing tag: " + r_tag.name;
  1105. return ERR_FILE_CORRUPT;
  1106. }
  1107. get_token(p_stream, token, line, r_err_str);
  1108. if (token.type == TK_BRACKET_CLOSE)
  1109. break;
  1110. if (parsing_tag && token.type == TK_PERIOD) {
  1111. r_tag.name += "."; //support tags such as [someprop.Anroid] for specific platforms
  1112. get_token(p_stream, token, line, r_err_str);
  1113. } else if (parsing_tag && token.type == TK_COLON) {
  1114. r_tag.name += ":"; //support tags such as [someprop.Anroid] for specific platforms
  1115. get_token(p_stream, token, line, r_err_str);
  1116. } else {
  1117. parsing_tag = false;
  1118. }
  1119. if (token.type != TK_IDENTIFIER) {
  1120. r_err_str = "Expected Identifier";
  1121. return ERR_PARSE_ERROR;
  1122. }
  1123. String id = token.value;
  1124. if (parsing_tag) {
  1125. r_tag.name += id;
  1126. continue;
  1127. }
  1128. get_token(p_stream, token, line, r_err_str);
  1129. if (token.type != TK_EQUAL) {
  1130. return ERR_PARSE_ERROR;
  1131. }
  1132. get_token(p_stream, token, line, r_err_str);
  1133. Variant value;
  1134. Error err = parse_value(token, value, p_stream, line, r_err_str, p_res_parser);
  1135. if (err)
  1136. return err;
  1137. r_tag.fields[id] = value;
  1138. }
  1139. return OK;
  1140. }
  1141. Error VariantParser::parse_tag(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser, bool p_simple_tag) {
  1142. Token token;
  1143. get_token(p_stream, token, line, r_err_str);
  1144. if (token.type == TK_EOF) {
  1145. return ERR_FILE_EOF;
  1146. }
  1147. if (token.type != TK_BRACKET_OPEN) {
  1148. r_err_str = "Expected '['";
  1149. return ERR_PARSE_ERROR;
  1150. }
  1151. return _parse_tag(token, p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1152. }
  1153. 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) {
  1154. //assign..
  1155. r_assign = "";
  1156. String what;
  1157. while (true) {
  1158. CharType c;
  1159. if (p_stream->saved) {
  1160. c = p_stream->saved;
  1161. p_stream->saved = 0;
  1162. } else {
  1163. c = p_stream->get_char();
  1164. }
  1165. if (p_stream->is_eof())
  1166. return ERR_FILE_EOF;
  1167. if (c == ';') { //comment
  1168. while (true) {
  1169. CharType ch = p_stream->get_char();
  1170. if (p_stream->is_eof()) {
  1171. return ERR_FILE_EOF;
  1172. }
  1173. if (ch == '\n')
  1174. break;
  1175. }
  1176. continue;
  1177. }
  1178. if (c == '[' && what.length() == 0) {
  1179. //it's a tag!
  1180. p_stream->saved = '['; //go back one
  1181. Error err = parse_tag(p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1182. return err;
  1183. }
  1184. if (c > 32) {
  1185. if (c == '"') { //quoted
  1186. p_stream->saved = '"';
  1187. Token tk;
  1188. Error err = get_token(p_stream, tk, line, r_err_str);
  1189. if (err)
  1190. return err;
  1191. if (tk.type != TK_STRING) {
  1192. r_err_str = "Error reading quoted string";
  1193. return err;
  1194. }
  1195. what = tk.value;
  1196. } else if (c != '=') {
  1197. what += String::chr(c);
  1198. } else {
  1199. r_assign = what;
  1200. Token token;
  1201. get_token(p_stream, token, line, r_err_str);
  1202. Error err = parse_value(token, r_value, p_stream, line, r_err_str, p_res_parser);
  1203. if (err) {
  1204. }
  1205. return err;
  1206. }
  1207. } else if (c == '\n') {
  1208. line++;
  1209. }
  1210. }
  1211. return OK;
  1212. }
  1213. Error VariantParser::parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser) {
  1214. Token token;
  1215. Error err = get_token(p_stream, token, r_err_line, r_err_str);
  1216. if (err)
  1217. return err;
  1218. if (token.type == TK_EOF) {
  1219. return ERR_FILE_EOF;
  1220. }
  1221. return parse_value(token, r_ret, p_stream, r_err_line, r_err_str, p_res_parser);
  1222. }
  1223. //////////////////////////////////////////////////////////////////////////////////
  1224. //////////////////////////////////////////////////////////////////////////////////
  1225. ////////////////////////////////////////////////////////////////////////////////
  1226. static String rtosfix(double p_value) {
  1227. if (p_value == 0.0)
  1228. return "0"; //avoid negative zero (-0) being written, which may annoy git, svn, etc. for changes when they don't exist.
  1229. else
  1230. return rtoss(p_value);
  1231. }
  1232. 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) {
  1233. switch (p_variant.get_type()) {
  1234. case Variant::NIL: {
  1235. p_store_string_func(p_store_string_ud, "null");
  1236. } break;
  1237. case Variant::BOOL: {
  1238. p_store_string_func(p_store_string_ud, p_variant.operator bool() ? "true" : "false");
  1239. } break;
  1240. case Variant::INT: {
  1241. p_store_string_func(p_store_string_ud, itos(p_variant.operator int()));
  1242. } break;
  1243. case Variant::REAL: {
  1244. String s = rtosfix(p_variant.operator real_t());
  1245. if (s.find(".") == -1 && s.find("e") == -1)
  1246. s += ".0";
  1247. p_store_string_func(p_store_string_ud, s);
  1248. } break;
  1249. case Variant::STRING: {
  1250. String str = p_variant;
  1251. str = "\"" + str.c_escape_multiline() + "\"";
  1252. p_store_string_func(p_store_string_ud, str);
  1253. } break;
  1254. case Variant::VECTOR2: {
  1255. Vector2 v = p_variant;
  1256. p_store_string_func(p_store_string_ud, "Vector2( " + rtosfix(v.x) + ", " + rtosfix(v.y) + " )");
  1257. } break;
  1258. case Variant::RECT2: {
  1259. Rect2 aabb = p_variant;
  1260. p_store_string_func(p_store_string_ud, "Rect2( " + rtosfix(aabb.position.x) + ", " + rtosfix(aabb.position.y) + ", " + rtosfix(aabb.size.x) + ", " + rtosfix(aabb.size.y) + " )");
  1261. } break;
  1262. case Variant::VECTOR3: {
  1263. Vector3 v = p_variant;
  1264. p_store_string_func(p_store_string_ud, "Vector3( " + rtosfix(v.x) + ", " + rtosfix(v.y) + ", " + rtosfix(v.z) + " )");
  1265. } break;
  1266. case Variant::PLANE: {
  1267. Plane p = p_variant;
  1268. p_store_string_func(p_store_string_ud, "Plane( " + rtosfix(p.normal.x) + ", " + rtosfix(p.normal.y) + ", " + rtosfix(p.normal.z) + ", " + rtosfix(p.d) + " )");
  1269. } break;
  1270. case Variant::RECT3: {
  1271. Rect3 aabb = p_variant;
  1272. p_store_string_func(p_store_string_ud, "Rect3( " + rtosfix(aabb.position.x) + ", " + rtosfix(aabb.position.y) + ", " + rtosfix(aabb.position.z) + ", " + rtosfix(aabb.size.x) + ", " + rtosfix(aabb.size.y) + ", " + rtosfix(aabb.size.z) + " )");
  1273. } break;
  1274. case Variant::QUAT: {
  1275. Quat quat = p_variant;
  1276. p_store_string_func(p_store_string_ud, "Quat( " + rtosfix(quat.x) + ", " + rtosfix(quat.y) + ", " + rtosfix(quat.z) + ", " + rtosfix(quat.w) + " )");
  1277. } break;
  1278. case Variant::TRANSFORM2D: {
  1279. String s = "Transform2D( ";
  1280. Transform2D m3 = p_variant;
  1281. for (int i = 0; i < 3; i++) {
  1282. for (int j = 0; j < 2; j++) {
  1283. if (i != 0 || j != 0)
  1284. s += ", ";
  1285. s += rtosfix(m3.elements[i][j]);
  1286. }
  1287. }
  1288. p_store_string_func(p_store_string_ud, s + " )");
  1289. } break;
  1290. case Variant::BASIS: {
  1291. String s = "Basis( ";
  1292. Basis m3 = p_variant;
  1293. for (int i = 0; i < 3; i++) {
  1294. for (int j = 0; j < 3; j++) {
  1295. if (i != 0 || j != 0)
  1296. s += ", ";
  1297. s += rtosfix(m3.elements[i][j]);
  1298. }
  1299. }
  1300. p_store_string_func(p_store_string_ud, s + " )");
  1301. } break;
  1302. case Variant::TRANSFORM: {
  1303. String s = "Transform( ";
  1304. Transform t = p_variant;
  1305. Basis &m3 = t.basis;
  1306. for (int i = 0; i < 3; i++) {
  1307. for (int j = 0; j < 3; j++) {
  1308. if (i != 0 || j != 0)
  1309. s += ", ";
  1310. s += rtosfix(m3.elements[i][j]);
  1311. }
  1312. }
  1313. s = s + ", " + rtosfix(t.origin.x) + ", " + rtosfix(t.origin.y) + ", " + rtosfix(t.origin.z);
  1314. p_store_string_func(p_store_string_ud, s + " )");
  1315. } break;
  1316. // misc types
  1317. case Variant::COLOR: {
  1318. Color c = p_variant;
  1319. p_store_string_func(p_store_string_ud, "Color( " + rtosfix(c.r) + ", " + rtosfix(c.g) + ", " + rtosfix(c.b) + ", " + rtosfix(c.a) + " )");
  1320. } break;
  1321. case Variant::NODE_PATH: {
  1322. String str = p_variant;
  1323. str = "NodePath(\"" + str.c_escape() + "\")";
  1324. p_store_string_func(p_store_string_ud, str);
  1325. } break;
  1326. case Variant::OBJECT: {
  1327. Object *obj = p_variant;
  1328. if (!obj) {
  1329. p_store_string_func(p_store_string_ud, "null");
  1330. break; // don't save it
  1331. }
  1332. RES res = p_variant;
  1333. if (res.is_valid()) {
  1334. //is resource
  1335. String res_text;
  1336. //try external function
  1337. if (p_encode_res_func) {
  1338. res_text = p_encode_res_func(p_encode_res_ud, res);
  1339. }
  1340. //try path because it's a file
  1341. if (res_text == String() && res->get_path().is_resource_file()) {
  1342. //external resource
  1343. String path = res->get_path();
  1344. res_text = "Resource( \"" + path + "\")";
  1345. }
  1346. //could come up with some sort of text
  1347. if (res_text != String()) {
  1348. p_store_string_func(p_store_string_ud, res_text);
  1349. break;
  1350. }
  1351. }
  1352. //store as generic object
  1353. p_store_string_func(p_store_string_ud, "Object(" + obj->get_class() + ",");
  1354. List<PropertyInfo> props;
  1355. obj->get_property_list(&props);
  1356. bool first = true;
  1357. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1358. if (E->get().usage & PROPERTY_USAGE_STORAGE || E->get().usage & PROPERTY_USAGE_SCRIPT_VARIABLE) {
  1359. //must be serialized
  1360. if (first) {
  1361. first = false;
  1362. } else {
  1363. p_store_string_func(p_store_string_ud, ",");
  1364. }
  1365. p_store_string_func(p_store_string_ud, "\"" + E->get().name + "\":");
  1366. write(obj->get(E->get().name), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1367. }
  1368. }
  1369. p_store_string_func(p_store_string_ud, ")\n");
  1370. } break;
  1371. case Variant::DICTIONARY: {
  1372. Dictionary dict = p_variant;
  1373. List<Variant> keys;
  1374. dict.get_key_list(&keys);
  1375. keys.sort();
  1376. p_store_string_func(p_store_string_ud, "{\n");
  1377. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1378. /*
  1379. if (!_check_type(dict[E->get()]))
  1380. continue;
  1381. */
  1382. write(E->get(), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1383. p_store_string_func(p_store_string_ud, ": ");
  1384. write(dict[E->get()], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1385. if (E->next())
  1386. p_store_string_func(p_store_string_ud, ",\n");
  1387. }
  1388. p_store_string_func(p_store_string_ud, "\n}");
  1389. } break;
  1390. case Variant::ARRAY: {
  1391. p_store_string_func(p_store_string_ud, "[ ");
  1392. Array array = p_variant;
  1393. int len = array.size();
  1394. for (int i = 0; i < len; i++) {
  1395. if (i > 0)
  1396. p_store_string_func(p_store_string_ud, ", ");
  1397. write(array[i], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1398. }
  1399. p_store_string_func(p_store_string_ud, " ]");
  1400. } break;
  1401. case Variant::POOL_BYTE_ARRAY: {
  1402. p_store_string_func(p_store_string_ud, "PoolByteArray( ");
  1403. String s;
  1404. PoolVector<uint8_t> data = p_variant;
  1405. int len = data.size();
  1406. PoolVector<uint8_t>::Read r = data.read();
  1407. const uint8_t *ptr = r.ptr();
  1408. for (int i = 0; i < len; i++) {
  1409. if (i > 0)
  1410. p_store_string_func(p_store_string_ud, ", ");
  1411. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  1412. }
  1413. p_store_string_func(p_store_string_ud, " )");
  1414. } break;
  1415. case Variant::POOL_INT_ARRAY: {
  1416. p_store_string_func(p_store_string_ud, "PoolIntArray( ");
  1417. PoolVector<int> data = p_variant;
  1418. int len = data.size();
  1419. PoolVector<int>::Read r = data.read();
  1420. const int *ptr = r.ptr();
  1421. for (int i = 0; i < len; i++) {
  1422. if (i > 0)
  1423. p_store_string_func(p_store_string_ud, ", ");
  1424. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  1425. }
  1426. p_store_string_func(p_store_string_ud, " )");
  1427. } break;
  1428. case Variant::POOL_REAL_ARRAY: {
  1429. p_store_string_func(p_store_string_ud, "PoolFloatArray( ");
  1430. PoolVector<real_t> data = p_variant;
  1431. int len = data.size();
  1432. PoolVector<real_t>::Read r = data.read();
  1433. const real_t *ptr = r.ptr();
  1434. for (int i = 0; i < len; i++) {
  1435. if (i > 0)
  1436. p_store_string_func(p_store_string_ud, ", ");
  1437. p_store_string_func(p_store_string_ud, rtosfix(ptr[i]));
  1438. }
  1439. p_store_string_func(p_store_string_ud, " )");
  1440. } break;
  1441. case Variant::POOL_STRING_ARRAY: {
  1442. p_store_string_func(p_store_string_ud, "PoolStringArray( ");
  1443. PoolVector<String> data = p_variant;
  1444. int len = data.size();
  1445. PoolVector<String>::Read r = data.read();
  1446. const String *ptr = r.ptr();
  1447. String s;
  1448. //write_string("\n");
  1449. for (int i = 0; i < len; i++) {
  1450. if (i > 0)
  1451. p_store_string_func(p_store_string_ud, ", ");
  1452. String str = ptr[i];
  1453. p_store_string_func(p_store_string_ud, "\"" + str.c_escape() + "\"");
  1454. }
  1455. p_store_string_func(p_store_string_ud, " )");
  1456. } break;
  1457. case Variant::POOL_VECTOR2_ARRAY: {
  1458. p_store_string_func(p_store_string_ud, "PoolVector2Array( ");
  1459. PoolVector<Vector2> data = p_variant;
  1460. int len = data.size();
  1461. PoolVector<Vector2>::Read r = data.read();
  1462. const Vector2 *ptr = r.ptr();
  1463. for (int i = 0; i < len; i++) {
  1464. if (i > 0)
  1465. p_store_string_func(p_store_string_ud, ", ");
  1466. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].x) + ", " + rtosfix(ptr[i].y));
  1467. }
  1468. p_store_string_func(p_store_string_ud, " )");
  1469. } break;
  1470. case Variant::POOL_VECTOR3_ARRAY: {
  1471. p_store_string_func(p_store_string_ud, "PoolVector3Array( ");
  1472. PoolVector<Vector3> data = p_variant;
  1473. int len = data.size();
  1474. PoolVector<Vector3>::Read r = data.read();
  1475. const Vector3 *ptr = r.ptr();
  1476. for (int i = 0; i < len; i++) {
  1477. if (i > 0)
  1478. p_store_string_func(p_store_string_ud, ", ");
  1479. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].x) + ", " + rtosfix(ptr[i].y) + ", " + rtosfix(ptr[i].z));
  1480. }
  1481. p_store_string_func(p_store_string_ud, " )");
  1482. } break;
  1483. case Variant::POOL_COLOR_ARRAY: {
  1484. p_store_string_func(p_store_string_ud, "PoolColorArray( ");
  1485. PoolVector<Color> data = p_variant;
  1486. int len = data.size();
  1487. PoolVector<Color>::Read r = data.read();
  1488. const Color *ptr = r.ptr();
  1489. for (int i = 0; i < len; i++) {
  1490. if (i > 0)
  1491. p_store_string_func(p_store_string_ud, ", ");
  1492. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].r) + ", " + rtosfix(ptr[i].g) + ", " + rtosfix(ptr[i].b) + ", " + rtosfix(ptr[i].a));
  1493. }
  1494. p_store_string_func(p_store_string_ud, " )");
  1495. } break;
  1496. default: {}
  1497. }
  1498. return OK;
  1499. }
  1500. static Error _write_to_str(void *ud, const String &p_string) {
  1501. String *str = (String *)ud;
  1502. (*str) += p_string;
  1503. return OK;
  1504. }
  1505. Error VariantWriter::write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud) {
  1506. r_string = String();
  1507. return write(p_variant, _write_to_str, &r_string, p_encode_res_func, p_encode_res_ud);
  1508. }