variant_parser.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  1. /*************************************************************************/
  2. /* variant_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 == "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 == "AABB" || id == "Rect3") {
  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 = AABB(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 token2;
  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, token2, line, r_err_str);
  594. if (err != OK)
  595. return err;
  596. if (token2.type == TK_PARENTHESIS_CLOSE) {
  597. Reference *reference = Object::cast_to<Reference>(obj);
  598. if (reference) {
  599. value = REF(reference);
  600. } else {
  601. value = obj;
  602. }
  603. return OK;
  604. }
  605. if (need_comma) {
  606. if (token2.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 (token2.type != TK_STRING) {
  615. r_err_str = "Expected property name as string";
  616. return ERR_PARSE_ERROR;
  617. }
  618. key = token2.value;
  619. err = get_token(p_stream, token2, line, r_err_str);
  620. if (err != OK)
  621. return err;
  622. if (token2.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, token2, line, r_err_str);
  629. if (err != OK)
  630. return err;
  631. Variant v;
  632. err = parse_value(token2, 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. } else if (id == "Resource" || id == "SubResource" || id == "ExtResource") {
  641. get_token(p_stream, token, line, r_err_str);
  642. if (token.type != TK_PARENTHESIS_OPEN) {
  643. r_err_str = "Expected '('";
  644. return ERR_PARSE_ERROR;
  645. }
  646. if (p_res_parser && id == "Resource" && p_res_parser->func) {
  647. RES res;
  648. Error err = p_res_parser->func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  649. if (err)
  650. return err;
  651. value = res;
  652. return OK;
  653. } else if (p_res_parser && id == "ExtResource" && p_res_parser->ext_func) {
  654. RES res;
  655. Error err = p_res_parser->ext_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  656. if (err)
  657. return err;
  658. value = res;
  659. return OK;
  660. } else if (p_res_parser && id == "SubResource" && p_res_parser->sub_func) {
  661. RES res;
  662. Error err = p_res_parser->sub_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  663. if (err)
  664. return err;
  665. value = res;
  666. return OK;
  667. } else {
  668. get_token(p_stream, token, line, r_err_str);
  669. if (token.type == TK_STRING) {
  670. String path = token.value;
  671. RES res = ResourceLoader::load(path);
  672. if (res.is_null()) {
  673. r_err_str = "Can't load resource at path: '" + path + "'.";
  674. return ERR_PARSE_ERROR;
  675. }
  676. get_token(p_stream, token, line, r_err_str);
  677. if (token.type != TK_PARENTHESIS_CLOSE) {
  678. r_err_str = "Expected ')'";
  679. return ERR_PARSE_ERROR;
  680. }
  681. value = res;
  682. return OK;
  683. } else {
  684. r_err_str = "Expected string as argument for Resource().";
  685. return ERR_PARSE_ERROR;
  686. }
  687. }
  688. #ifndef DISABLE_DEPRECATED
  689. } else if (id == "InputEvent") {
  690. get_token(p_stream, token, line, r_err_str);
  691. if (token.type != TK_PARENTHESIS_OPEN) {
  692. r_err_str = "Expected '('";
  693. return ERR_PARSE_ERROR;
  694. }
  695. get_token(p_stream, token, line, r_err_str);
  696. if (token.type != TK_IDENTIFIER) {
  697. r_err_str = "Expected identifier";
  698. return ERR_PARSE_ERROR;
  699. }
  700. String id2 = token.value;
  701. Ref<InputEvent> ie;
  702. if (id2 == "NONE") {
  703. get_token(p_stream, token, line, r_err_str);
  704. if (token.type != TK_PARENTHESIS_CLOSE) {
  705. r_err_str = "Expected ')'";
  706. return ERR_PARSE_ERROR;
  707. }
  708. } else if (id2 == "KEY") {
  709. Ref<InputEventKey> key;
  710. key.instance();
  711. ie = key;
  712. get_token(p_stream, token, line, r_err_str);
  713. if (token.type != TK_COMMA) {
  714. r_err_str = "Expected ','";
  715. return ERR_PARSE_ERROR;
  716. }
  717. get_token(p_stream, token, line, r_err_str);
  718. if (token.type == TK_IDENTIFIER) {
  719. String name = token.value;
  720. key->set_scancode(find_keycode(name));
  721. } else if (token.type == TK_NUMBER) {
  722. key->set_scancode(token.value);
  723. } else {
  724. r_err_str = "Expected string or integer for keycode";
  725. return ERR_PARSE_ERROR;
  726. }
  727. get_token(p_stream, token, line, r_err_str);
  728. if (token.type == TK_COMMA) {
  729. get_token(p_stream, token, line, r_err_str);
  730. if (token.type != TK_IDENTIFIER) {
  731. r_err_str = "Expected identifier with modifier flas";
  732. return ERR_PARSE_ERROR;
  733. }
  734. String mods = token.value;
  735. if (mods.findn("C") != -1)
  736. key->set_control(true);
  737. if (mods.findn("A") != -1)
  738. key->set_alt(true);
  739. if (mods.findn("S") != -1)
  740. key->set_shift(true);
  741. if (mods.findn("M") != -1)
  742. key->set_metakey(true);
  743. get_token(p_stream, token, line, r_err_str);
  744. if (token.type != TK_PARENTHESIS_CLOSE) {
  745. r_err_str = "Expected ')'";
  746. return ERR_PARSE_ERROR;
  747. }
  748. } else if (token.type != TK_PARENTHESIS_CLOSE) {
  749. r_err_str = "Expected ')' or modifier flags.";
  750. return ERR_PARSE_ERROR;
  751. }
  752. } else if (id2 == "MBUTTON") {
  753. Ref<InputEventMouseButton> mb;
  754. mb.instance();
  755. ie = mb;
  756. get_token(p_stream, token, line, r_err_str);
  757. if (token.type != TK_COMMA) {
  758. r_err_str = "Expected ','";
  759. return ERR_PARSE_ERROR;
  760. }
  761. get_token(p_stream, token, line, r_err_str);
  762. if (token.type != TK_NUMBER) {
  763. r_err_str = "Expected button index";
  764. return ERR_PARSE_ERROR;
  765. }
  766. mb->set_button_index(token.value);
  767. get_token(p_stream, token, line, r_err_str);
  768. if (token.type != TK_PARENTHESIS_CLOSE) {
  769. r_err_str = "Expected ')'";
  770. return ERR_PARSE_ERROR;
  771. }
  772. } else if (id2 == "JBUTTON") {
  773. Ref<InputEventJoypadButton> jb;
  774. jb.instance();
  775. ie = jb;
  776. get_token(p_stream, token, line, r_err_str);
  777. if (token.type != TK_COMMA) {
  778. r_err_str = "Expected ','";
  779. return ERR_PARSE_ERROR;
  780. }
  781. get_token(p_stream, token, line, r_err_str);
  782. if (token.type != TK_NUMBER) {
  783. r_err_str = "Expected button index";
  784. return ERR_PARSE_ERROR;
  785. }
  786. jb->set_button_index(token.value);
  787. get_token(p_stream, token, line, r_err_str);
  788. if (token.type != TK_PARENTHESIS_CLOSE) {
  789. r_err_str = "Expected ')'";
  790. return ERR_PARSE_ERROR;
  791. }
  792. } else if (id2 == "JAXIS") {
  793. Ref<InputEventJoypadMotion> jm;
  794. jm.instance();
  795. ie = jm;
  796. get_token(p_stream, token, line, r_err_str);
  797. if (token.type != TK_COMMA) {
  798. r_err_str = "Expected ','";
  799. return ERR_PARSE_ERROR;
  800. }
  801. get_token(p_stream, token, line, r_err_str);
  802. if (token.type != TK_NUMBER) {
  803. r_err_str = "Expected axis index";
  804. return ERR_PARSE_ERROR;
  805. }
  806. jm->set_axis(token.value);
  807. get_token(p_stream, token, line, r_err_str);
  808. if (token.type != TK_COMMA) {
  809. r_err_str = "Expected ',' after axis index";
  810. return ERR_PARSE_ERROR;
  811. }
  812. get_token(p_stream, token, line, r_err_str);
  813. if (token.type != TK_NUMBER) {
  814. r_err_str = "Expected axis sign";
  815. return ERR_PARSE_ERROR;
  816. }
  817. jm->set_axis_value(token.value);
  818. get_token(p_stream, token, line, r_err_str);
  819. if (token.type != TK_PARENTHESIS_CLOSE) {
  820. r_err_str = "Expected ')' for jaxis";
  821. return ERR_PARSE_ERROR;
  822. }
  823. } else {
  824. r_err_str = "Invalid input event type.";
  825. return ERR_PARSE_ERROR;
  826. }
  827. value = ie;
  828. return OK;
  829. #endif
  830. } else if (id == "PoolByteArray" || id == "ByteArray") {
  831. Vector<uint8_t> args;
  832. Error err = _parse_construct<uint8_t>(p_stream, args, line, r_err_str);
  833. if (err)
  834. return err;
  835. PoolVector<uint8_t> arr;
  836. {
  837. int len = args.size();
  838. arr.resize(len);
  839. PoolVector<uint8_t>::Write w = arr.write();
  840. for (int i = 0; i < len; i++) {
  841. w[i] = args[i];
  842. }
  843. }
  844. value = arr;
  845. return OK;
  846. } else if (id == "PoolIntArray" || id == "IntArray") {
  847. Vector<int> args;
  848. Error err = _parse_construct<int>(p_stream, args, line, r_err_str);
  849. if (err)
  850. return err;
  851. PoolVector<int> arr;
  852. {
  853. int len = args.size();
  854. arr.resize(len);
  855. PoolVector<int>::Write w = arr.write();
  856. for (int i = 0; i < len; i++) {
  857. w[i] = int(args[i]);
  858. }
  859. }
  860. value = arr;
  861. return OK;
  862. } else if (id == "PoolRealArray" || id == "FloatArray") {
  863. Vector<float> args;
  864. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  865. if (err)
  866. return err;
  867. PoolVector<float> arr;
  868. {
  869. int len = args.size();
  870. arr.resize(len);
  871. PoolVector<float>::Write w = arr.write();
  872. for (int i = 0; i < len; i++) {
  873. w[i] = args[i];
  874. }
  875. }
  876. value = arr;
  877. return OK;
  878. } else if (id == "PoolStringArray" || id == "StringArray") {
  879. get_token(p_stream, token, line, r_err_str);
  880. if (token.type != TK_PARENTHESIS_OPEN) {
  881. r_err_str = "Expected '('";
  882. return ERR_PARSE_ERROR;
  883. }
  884. Vector<String> cs;
  885. bool first = true;
  886. while (true) {
  887. if (!first) {
  888. get_token(p_stream, token, line, r_err_str);
  889. if (token.type == TK_COMMA) {
  890. //do none
  891. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  892. break;
  893. } else {
  894. r_err_str = "Expected ',' or ')'";
  895. return ERR_PARSE_ERROR;
  896. }
  897. }
  898. get_token(p_stream, token, line, r_err_str);
  899. if (token.type == TK_PARENTHESIS_CLOSE) {
  900. break;
  901. } else if (token.type != TK_STRING) {
  902. r_err_str = "Expected string";
  903. return ERR_PARSE_ERROR;
  904. }
  905. first = false;
  906. cs.push_back(token.value);
  907. }
  908. PoolVector<String> arr;
  909. {
  910. int len = cs.size();
  911. arr.resize(len);
  912. PoolVector<String>::Write w = arr.write();
  913. for (int i = 0; i < len; i++) {
  914. w[i] = cs[i];
  915. }
  916. }
  917. value = arr;
  918. return OK;
  919. } else if (id == "PoolVector2Array" || id == "Vector2Array") {
  920. Vector<float> args;
  921. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  922. if (err)
  923. return err;
  924. PoolVector<Vector2> arr;
  925. {
  926. int len = args.size() / 2;
  927. arr.resize(len);
  928. PoolVector<Vector2>::Write w = arr.write();
  929. for (int i = 0; i < len; i++) {
  930. w[i] = Vector2(args[i * 2 + 0], args[i * 2 + 1]);
  931. }
  932. }
  933. value = arr;
  934. return OK;
  935. } else if (id == "PoolVector3Array" || id == "Vector3Array") {
  936. Vector<float> args;
  937. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  938. if (err)
  939. return err;
  940. PoolVector<Vector3> arr;
  941. {
  942. int len = args.size() / 3;
  943. arr.resize(len);
  944. PoolVector<Vector3>::Write w = arr.write();
  945. for (int i = 0; i < len; i++) {
  946. w[i] = Vector3(args[i * 3 + 0], args[i * 3 + 1], args[i * 3 + 2]);
  947. }
  948. }
  949. value = arr;
  950. return OK;
  951. } else if (id == "PoolColorArray" || id == "ColorArray") {
  952. Vector<float> args;
  953. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  954. if (err)
  955. return err;
  956. PoolVector<Color> arr;
  957. {
  958. int len = args.size() / 4;
  959. arr.resize(len);
  960. PoolVector<Color>::Write w = arr.write();
  961. for (int i = 0; i < len; i++) {
  962. w[i] = Color(args[i * 4 + 0], args[i * 4 + 1], args[i * 4 + 2], args[i * 4 + 3]);
  963. }
  964. }
  965. value = arr;
  966. return OK;
  967. } else {
  968. r_err_str = "Unexpected identifier: '" + id + "'.";
  969. return ERR_PARSE_ERROR;
  970. }
  971. return OK;
  972. } else if (token.type == TK_NUMBER) {
  973. value = token.value;
  974. return OK;
  975. } else if (token.type == TK_STRING) {
  976. value = token.value;
  977. return OK;
  978. } else if (token.type == TK_COLOR) {
  979. value = token.value;
  980. return OK;
  981. } else {
  982. r_err_str = "Expected value, got " + String(tk_name[token.type]) + ".";
  983. return ERR_PARSE_ERROR;
  984. }
  985. }
  986. Error VariantParser::_parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  987. Token token;
  988. bool need_comma = false;
  989. while (true) {
  990. if (p_stream->is_eof()) {
  991. r_err_str = "Unexpected End of File while parsing array";
  992. return ERR_FILE_CORRUPT;
  993. }
  994. Error err = get_token(p_stream, token, line, r_err_str);
  995. if (err != OK)
  996. return err;
  997. if (token.type == TK_BRACKET_CLOSE) {
  998. return OK;
  999. }
  1000. if (need_comma) {
  1001. if (token.type != TK_COMMA) {
  1002. r_err_str = "Expected ','";
  1003. return ERR_PARSE_ERROR;
  1004. } else {
  1005. need_comma = false;
  1006. continue;
  1007. }
  1008. }
  1009. Variant v;
  1010. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1011. if (err)
  1012. return err;
  1013. array.push_back(v);
  1014. need_comma = true;
  1015. }
  1016. }
  1017. Error VariantParser::_parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  1018. bool at_key = true;
  1019. Variant key;
  1020. Token token;
  1021. bool need_comma = false;
  1022. while (true) {
  1023. if (p_stream->is_eof()) {
  1024. r_err_str = "Unexpected End of File while parsing dictionary";
  1025. return ERR_FILE_CORRUPT;
  1026. }
  1027. if (at_key) {
  1028. Error err = get_token(p_stream, token, line, r_err_str);
  1029. if (err != OK)
  1030. return err;
  1031. if (token.type == TK_CURLY_BRACKET_CLOSE) {
  1032. return OK;
  1033. }
  1034. if (need_comma) {
  1035. if (token.type != TK_COMMA) {
  1036. r_err_str = "Expected '}' or ','";
  1037. return ERR_PARSE_ERROR;
  1038. } else {
  1039. need_comma = false;
  1040. continue;
  1041. }
  1042. }
  1043. err = parse_value(token, key, p_stream, line, r_err_str, p_res_parser);
  1044. if (err)
  1045. return err;
  1046. err = get_token(p_stream, token, line, r_err_str);
  1047. if (err != OK)
  1048. return err;
  1049. if (token.type != TK_COLON) {
  1050. r_err_str = "Expected ':'";
  1051. return ERR_PARSE_ERROR;
  1052. }
  1053. at_key = false;
  1054. } else {
  1055. Error err = get_token(p_stream, token, line, r_err_str);
  1056. if (err != OK)
  1057. return err;
  1058. Variant v;
  1059. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1060. if (err)
  1061. return err;
  1062. object[key] = v;
  1063. need_comma = true;
  1064. at_key = true;
  1065. }
  1066. }
  1067. }
  1068. 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) {
  1069. r_tag.fields.clear();
  1070. if (token.type != TK_BRACKET_OPEN) {
  1071. r_err_str = "Expected '['";
  1072. return ERR_PARSE_ERROR;
  1073. }
  1074. if (p_simple_tag) {
  1075. r_tag.name = "";
  1076. r_tag.fields.clear();
  1077. while (true) {
  1078. CharType c = p_stream->get_char();
  1079. if (p_stream->is_eof()) {
  1080. r_err_str = "Unexpected EOF while parsing simple tag";
  1081. return ERR_PARSE_ERROR;
  1082. }
  1083. if (c == ']')
  1084. break;
  1085. r_tag.name += String::chr(c);
  1086. }
  1087. r_tag.name = r_tag.name.strip_edges();
  1088. return OK;
  1089. }
  1090. get_token(p_stream, token, line, r_err_str);
  1091. if (token.type != TK_IDENTIFIER) {
  1092. r_err_str = "Expected identifier (tag name)";
  1093. return ERR_PARSE_ERROR;
  1094. }
  1095. r_tag.name = token.value;
  1096. bool parsing_tag = true;
  1097. while (true) {
  1098. if (p_stream->is_eof()) {
  1099. r_err_str = "Unexpected End of File while parsing tag: " + r_tag.name;
  1100. return ERR_FILE_CORRUPT;
  1101. }
  1102. get_token(p_stream, token, line, r_err_str);
  1103. if (token.type == TK_BRACKET_CLOSE)
  1104. break;
  1105. if (parsing_tag && token.type == TK_PERIOD) {
  1106. r_tag.name += "."; //support tags such as [someprop.Android] for specific platforms
  1107. get_token(p_stream, token, line, r_err_str);
  1108. } else if (parsing_tag && token.type == TK_COLON) {
  1109. r_tag.name += ":"; //support tags such as [someprop.Android] for specific platforms
  1110. get_token(p_stream, token, line, r_err_str);
  1111. } else {
  1112. parsing_tag = false;
  1113. }
  1114. if (token.type != TK_IDENTIFIER) {
  1115. r_err_str = "Expected Identifier";
  1116. return ERR_PARSE_ERROR;
  1117. }
  1118. String id = token.value;
  1119. if (parsing_tag) {
  1120. r_tag.name += id;
  1121. continue;
  1122. }
  1123. get_token(p_stream, token, line, r_err_str);
  1124. if (token.type != TK_EQUAL) {
  1125. return ERR_PARSE_ERROR;
  1126. }
  1127. get_token(p_stream, token, line, r_err_str);
  1128. Variant value;
  1129. Error err = parse_value(token, value, p_stream, line, r_err_str, p_res_parser);
  1130. if (err)
  1131. return err;
  1132. r_tag.fields[id] = value;
  1133. }
  1134. return OK;
  1135. }
  1136. Error VariantParser::parse_tag(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser, bool p_simple_tag) {
  1137. Token token;
  1138. get_token(p_stream, token, line, r_err_str);
  1139. if (token.type == TK_EOF) {
  1140. return ERR_FILE_EOF;
  1141. }
  1142. if (token.type != TK_BRACKET_OPEN) {
  1143. r_err_str = "Expected '['";
  1144. return ERR_PARSE_ERROR;
  1145. }
  1146. return _parse_tag(token, p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1147. }
  1148. 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) {
  1149. //assign..
  1150. r_assign = "";
  1151. String what;
  1152. while (true) {
  1153. CharType c;
  1154. if (p_stream->saved) {
  1155. c = p_stream->saved;
  1156. p_stream->saved = 0;
  1157. } else {
  1158. c = p_stream->get_char();
  1159. }
  1160. if (p_stream->is_eof())
  1161. return ERR_FILE_EOF;
  1162. if (c == ';') { //comment
  1163. while (true) {
  1164. CharType ch = p_stream->get_char();
  1165. if (p_stream->is_eof()) {
  1166. return ERR_FILE_EOF;
  1167. }
  1168. if (ch == '\n')
  1169. break;
  1170. }
  1171. continue;
  1172. }
  1173. if (c == '[' && what.length() == 0) {
  1174. //it's a tag!
  1175. p_stream->saved = '['; //go back one
  1176. Error err = parse_tag(p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1177. return err;
  1178. }
  1179. if (c > 32) {
  1180. if (c == '"') { //quoted
  1181. p_stream->saved = '"';
  1182. Token tk;
  1183. Error err = get_token(p_stream, tk, line, r_err_str);
  1184. if (err)
  1185. return err;
  1186. if (tk.type != TK_STRING) {
  1187. r_err_str = "Error reading quoted string";
  1188. return err;
  1189. }
  1190. what = tk.value;
  1191. } else if (c != '=') {
  1192. what += String::chr(c);
  1193. } else {
  1194. if (p_stream->is_utf8()) {
  1195. what.parse_utf8(what.ascii(true).get_data());
  1196. }
  1197. r_assign = what;
  1198. Token token;
  1199. get_token(p_stream, token, line, r_err_str);
  1200. Error err = parse_value(token, r_value, p_stream, line, r_err_str, p_res_parser);
  1201. return err;
  1202. }
  1203. } else if (c == '\n') {
  1204. line++;
  1205. }
  1206. }
  1207. }
  1208. Error VariantParser::parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser) {
  1209. Token token;
  1210. Error err = get_token(p_stream, token, r_err_line, r_err_str);
  1211. if (err)
  1212. return err;
  1213. if (token.type == TK_EOF) {
  1214. return ERR_FILE_EOF;
  1215. }
  1216. return parse_value(token, r_ret, p_stream, r_err_line, r_err_str, p_res_parser);
  1217. }
  1218. //////////////////////////////////////////////////////////////////////////////////
  1219. //////////////////////////////////////////////////////////////////////////////////
  1220. ////////////////////////////////////////////////////////////////////////////////
  1221. static String rtosfix(double p_value) {
  1222. if (p_value == 0.0)
  1223. return "0"; //avoid negative zero (-0) being written, which may annoy git, svn, etc. for changes when they don't exist.
  1224. else
  1225. return rtoss(p_value);
  1226. }
  1227. 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) {
  1228. switch (p_variant.get_type()) {
  1229. case Variant::NIL: {
  1230. p_store_string_func(p_store_string_ud, "null");
  1231. } break;
  1232. case Variant::BOOL: {
  1233. p_store_string_func(p_store_string_ud, p_variant.operator bool() ? "true" : "false");
  1234. } break;
  1235. case Variant::INT: {
  1236. p_store_string_func(p_store_string_ud, itos(p_variant.operator int64_t()));
  1237. } break;
  1238. case Variant::REAL: {
  1239. String s = rtosfix(p_variant.operator real_t());
  1240. if (s.find(".") == -1 && s.find("e") == -1)
  1241. s += ".0";
  1242. p_store_string_func(p_store_string_ud, s);
  1243. } break;
  1244. case Variant::STRING: {
  1245. String str = p_variant;
  1246. str = "\"" + str.c_escape_multiline() + "\"";
  1247. p_store_string_func(p_store_string_ud, str);
  1248. } break;
  1249. case Variant::VECTOR2: {
  1250. Vector2 v = p_variant;
  1251. p_store_string_func(p_store_string_ud, "Vector2( " + rtosfix(v.x) + ", " + rtosfix(v.y) + " )");
  1252. } break;
  1253. case Variant::RECT2: {
  1254. Rect2 aabb = p_variant;
  1255. 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) + " )");
  1256. } break;
  1257. case Variant::VECTOR3: {
  1258. Vector3 v = p_variant;
  1259. p_store_string_func(p_store_string_ud, "Vector3( " + rtosfix(v.x) + ", " + rtosfix(v.y) + ", " + rtosfix(v.z) + " )");
  1260. } break;
  1261. case Variant::PLANE: {
  1262. Plane p = p_variant;
  1263. p_store_string_func(p_store_string_ud, "Plane( " + rtosfix(p.normal.x) + ", " + rtosfix(p.normal.y) + ", " + rtosfix(p.normal.z) + ", " + rtosfix(p.d) + " )");
  1264. } break;
  1265. case Variant::AABB: {
  1266. AABB aabb = p_variant;
  1267. 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) + " )");
  1268. } break;
  1269. case Variant::QUAT: {
  1270. Quat quat = p_variant;
  1271. p_store_string_func(p_store_string_ud, "Quat( " + rtosfix(quat.x) + ", " + rtosfix(quat.y) + ", " + rtosfix(quat.z) + ", " + rtosfix(quat.w) + " )");
  1272. } break;
  1273. case Variant::TRANSFORM2D: {
  1274. String s = "Transform2D( ";
  1275. Transform2D m3 = p_variant;
  1276. for (int i = 0; i < 3; i++) {
  1277. for (int j = 0; j < 2; j++) {
  1278. if (i != 0 || j != 0)
  1279. s += ", ";
  1280. s += rtosfix(m3.elements[i][j]);
  1281. }
  1282. }
  1283. p_store_string_func(p_store_string_ud, s + " )");
  1284. } break;
  1285. case Variant::BASIS: {
  1286. String s = "Basis( ";
  1287. Basis m3 = p_variant;
  1288. for (int i = 0; i < 3; i++) {
  1289. for (int j = 0; j < 3; j++) {
  1290. if (i != 0 || j != 0)
  1291. s += ", ";
  1292. s += rtosfix(m3.elements[i][j]);
  1293. }
  1294. }
  1295. p_store_string_func(p_store_string_ud, s + " )");
  1296. } break;
  1297. case Variant::TRANSFORM: {
  1298. String s = "Transform( ";
  1299. Transform t = p_variant;
  1300. Basis &m3 = t.basis;
  1301. for (int i = 0; i < 3; i++) {
  1302. for (int j = 0; j < 3; j++) {
  1303. if (i != 0 || j != 0)
  1304. s += ", ";
  1305. s += rtosfix(m3.elements[i][j]);
  1306. }
  1307. }
  1308. s = s + ", " + rtosfix(t.origin.x) + ", " + rtosfix(t.origin.y) + ", " + rtosfix(t.origin.z);
  1309. p_store_string_func(p_store_string_ud, s + " )");
  1310. } break;
  1311. // misc types
  1312. case Variant::COLOR: {
  1313. Color c = p_variant;
  1314. p_store_string_func(p_store_string_ud, "Color( " + rtosfix(c.r) + ", " + rtosfix(c.g) + ", " + rtosfix(c.b) + ", " + rtosfix(c.a) + " )");
  1315. } break;
  1316. case Variant::NODE_PATH: {
  1317. String str = p_variant;
  1318. str = "NodePath(\"" + str.c_escape() + "\")";
  1319. p_store_string_func(p_store_string_ud, str);
  1320. } break;
  1321. case Variant::OBJECT: {
  1322. Object *obj = p_variant;
  1323. if (!obj) {
  1324. p_store_string_func(p_store_string_ud, "null");
  1325. break; // don't save it
  1326. }
  1327. RES res = p_variant;
  1328. if (res.is_valid()) {
  1329. //is resource
  1330. String res_text;
  1331. //try external function
  1332. if (p_encode_res_func) {
  1333. res_text = p_encode_res_func(p_encode_res_ud, res);
  1334. }
  1335. //try path because it's a file
  1336. if (res_text == String() && res->get_path().is_resource_file()) {
  1337. //external resource
  1338. String path = res->get_path();
  1339. res_text = "Resource( \"" + path + "\")";
  1340. }
  1341. //could come up with some sort of text
  1342. if (res_text != String()) {
  1343. p_store_string_func(p_store_string_ud, res_text);
  1344. break;
  1345. }
  1346. }
  1347. //store as generic object
  1348. p_store_string_func(p_store_string_ud, "Object(" + obj->get_class() + ",");
  1349. List<PropertyInfo> props;
  1350. obj->get_property_list(&props);
  1351. bool first = true;
  1352. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1353. if (E->get().usage & PROPERTY_USAGE_STORAGE || E->get().usage & PROPERTY_USAGE_SCRIPT_VARIABLE) {
  1354. //must be serialized
  1355. if (first) {
  1356. first = false;
  1357. } else {
  1358. p_store_string_func(p_store_string_ud, ",");
  1359. }
  1360. p_store_string_func(p_store_string_ud, "\"" + E->get().name + "\":");
  1361. write(obj->get(E->get().name), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1362. }
  1363. }
  1364. p_store_string_func(p_store_string_ud, ")\n");
  1365. } break;
  1366. case Variant::DICTIONARY: {
  1367. Dictionary dict = p_variant;
  1368. List<Variant> keys;
  1369. dict.get_key_list(&keys);
  1370. keys.sort();
  1371. p_store_string_func(p_store_string_ud, "{\n");
  1372. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1373. /*
  1374. if (!_check_type(dict[E->get()]))
  1375. continue;
  1376. */
  1377. write(E->get(), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1378. p_store_string_func(p_store_string_ud, ": ");
  1379. write(dict[E->get()], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1380. if (E->next())
  1381. p_store_string_func(p_store_string_ud, ",\n");
  1382. }
  1383. p_store_string_func(p_store_string_ud, "\n}");
  1384. } break;
  1385. case Variant::ARRAY: {
  1386. p_store_string_func(p_store_string_ud, "[ ");
  1387. Array array = p_variant;
  1388. int len = array.size();
  1389. for (int i = 0; i < len; i++) {
  1390. if (i > 0)
  1391. p_store_string_func(p_store_string_ud, ", ");
  1392. write(array[i], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1393. }
  1394. p_store_string_func(p_store_string_ud, " ]");
  1395. } break;
  1396. case Variant::POOL_BYTE_ARRAY: {
  1397. p_store_string_func(p_store_string_ud, "PoolByteArray( ");
  1398. String s;
  1399. PoolVector<uint8_t> data = p_variant;
  1400. int len = data.size();
  1401. PoolVector<uint8_t>::Read r = data.read();
  1402. const uint8_t *ptr = r.ptr();
  1403. for (int i = 0; i < len; i++) {
  1404. if (i > 0)
  1405. p_store_string_func(p_store_string_ud, ", ");
  1406. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  1407. }
  1408. p_store_string_func(p_store_string_ud, " )");
  1409. } break;
  1410. case Variant::POOL_INT_ARRAY: {
  1411. p_store_string_func(p_store_string_ud, "PoolIntArray( ");
  1412. PoolVector<int> data = p_variant;
  1413. int len = data.size();
  1414. PoolVector<int>::Read r = data.read();
  1415. const int *ptr = r.ptr();
  1416. for (int i = 0; i < len; i++) {
  1417. if (i > 0)
  1418. p_store_string_func(p_store_string_ud, ", ");
  1419. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  1420. }
  1421. p_store_string_func(p_store_string_ud, " )");
  1422. } break;
  1423. case Variant::POOL_REAL_ARRAY: {
  1424. p_store_string_func(p_store_string_ud, "PoolRealArray( ");
  1425. PoolVector<real_t> data = p_variant;
  1426. int len = data.size();
  1427. PoolVector<real_t>::Read r = data.read();
  1428. const real_t *ptr = r.ptr();
  1429. for (int i = 0; i < len; i++) {
  1430. if (i > 0)
  1431. p_store_string_func(p_store_string_ud, ", ");
  1432. p_store_string_func(p_store_string_ud, rtosfix(ptr[i]));
  1433. }
  1434. p_store_string_func(p_store_string_ud, " )");
  1435. } break;
  1436. case Variant::POOL_STRING_ARRAY: {
  1437. p_store_string_func(p_store_string_ud, "PoolStringArray( ");
  1438. PoolVector<String> data = p_variant;
  1439. int len = data.size();
  1440. PoolVector<String>::Read r = data.read();
  1441. const String *ptr = r.ptr();
  1442. String s;
  1443. //write_string("\n");
  1444. for (int i = 0; i < len; i++) {
  1445. if (i > 0)
  1446. p_store_string_func(p_store_string_ud, ", ");
  1447. String str = ptr[i];
  1448. p_store_string_func(p_store_string_ud, "\"" + str.c_escape() + "\"");
  1449. }
  1450. p_store_string_func(p_store_string_ud, " )");
  1451. } break;
  1452. case Variant::POOL_VECTOR2_ARRAY: {
  1453. p_store_string_func(p_store_string_ud, "PoolVector2Array( ");
  1454. PoolVector<Vector2> data = p_variant;
  1455. int len = data.size();
  1456. PoolVector<Vector2>::Read r = data.read();
  1457. const Vector2 *ptr = r.ptr();
  1458. for (int i = 0; i < len; i++) {
  1459. if (i > 0)
  1460. p_store_string_func(p_store_string_ud, ", ");
  1461. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].x) + ", " + rtosfix(ptr[i].y));
  1462. }
  1463. p_store_string_func(p_store_string_ud, " )");
  1464. } break;
  1465. case Variant::POOL_VECTOR3_ARRAY: {
  1466. p_store_string_func(p_store_string_ud, "PoolVector3Array( ");
  1467. PoolVector<Vector3> data = p_variant;
  1468. int len = data.size();
  1469. PoolVector<Vector3>::Read r = data.read();
  1470. const Vector3 *ptr = r.ptr();
  1471. for (int i = 0; i < len; i++) {
  1472. if (i > 0)
  1473. p_store_string_func(p_store_string_ud, ", ");
  1474. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].x) + ", " + rtosfix(ptr[i].y) + ", " + rtosfix(ptr[i].z));
  1475. }
  1476. p_store_string_func(p_store_string_ud, " )");
  1477. } break;
  1478. case Variant::POOL_COLOR_ARRAY: {
  1479. p_store_string_func(p_store_string_ud, "PoolColorArray( ");
  1480. PoolVector<Color> data = p_variant;
  1481. int len = data.size();
  1482. PoolVector<Color>::Read r = data.read();
  1483. const Color *ptr = r.ptr();
  1484. for (int i = 0; i < len; i++) {
  1485. if (i > 0)
  1486. p_store_string_func(p_store_string_ud, ", ");
  1487. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].r) + ", " + rtosfix(ptr[i].g) + ", " + rtosfix(ptr[i].b) + ", " + rtosfix(ptr[i].a));
  1488. }
  1489. p_store_string_func(p_store_string_ud, " )");
  1490. } break;
  1491. default: {
  1492. }
  1493. }
  1494. return OK;
  1495. }
  1496. static Error _write_to_str(void *ud, const String &p_string) {
  1497. String *str = (String *)ud;
  1498. (*str) += p_string;
  1499. return OK;
  1500. }
  1501. Error VariantWriter::write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud) {
  1502. r_string = String();
  1503. return write(p_variant, _write_to_str, &r_string, p_encode_res_func, p_encode_res_ud);
  1504. }