variant_parser.cpp 45 KB

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