variant_parser.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970
  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. return OK;
  361. }
  362. template <class T>
  363. Error VariantParser::_parse_construct(Stream *p_stream, Vector<T> &r_construct, int &line, String &r_err_str) {
  364. Token token;
  365. get_token(p_stream, token, line, r_err_str);
  366. if (token.type != TK_PARENTHESIS_OPEN) {
  367. r_err_str = "Expected '(' in constructor";
  368. return ERR_PARSE_ERROR;
  369. }
  370. bool first = true;
  371. while (true) {
  372. if (!first) {
  373. get_token(p_stream, token, line, r_err_str);
  374. if (token.type == TK_COMMA) {
  375. //do none
  376. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  377. break;
  378. } else {
  379. r_err_str = "Expected ',' or ')' in constructor";
  380. return ERR_PARSE_ERROR;
  381. }
  382. }
  383. get_token(p_stream, token, line, r_err_str);
  384. if (first && token.type == TK_PARENTHESIS_CLOSE) {
  385. break;
  386. } else if (token.type != TK_NUMBER) {
  387. r_err_str = "Expected float in constructor";
  388. return ERR_PARSE_ERROR;
  389. }
  390. r_construct.push_back(token.value);
  391. first = false;
  392. }
  393. return OK;
  394. }
  395. Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  396. /* {
  397. Error err = get_token(p_stream,token,line,r_err_str);
  398. if (err)
  399. return err;
  400. }*/
  401. if (token.type == TK_CURLY_BRACKET_OPEN) {
  402. Dictionary d;
  403. Error err = _parse_dictionary(d, p_stream, line, r_err_str, p_res_parser);
  404. if (err)
  405. return err;
  406. value = d;
  407. return OK;
  408. } else if (token.type == TK_BRACKET_OPEN) {
  409. Array a;
  410. Error err = _parse_array(a, p_stream, line, r_err_str, p_res_parser);
  411. if (err)
  412. return err;
  413. value = a;
  414. return OK;
  415. } else if (token.type == TK_IDENTIFIER) {
  416. String id = token.value;
  417. if (id == "true")
  418. value = true;
  419. else if (id == "false")
  420. value = false;
  421. else if (id == "null" || id == "nil")
  422. value = Variant();
  423. else if (id == "Vector2") {
  424. Vector<float> args;
  425. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  426. if (err)
  427. return err;
  428. if (args.size() != 2) {
  429. r_err_str = "Expected 2 arguments for constructor";
  430. }
  431. value = Vector2(args[0], args[1]);
  432. return OK;
  433. } else if (id == "Rect2") {
  434. Vector<float> args;
  435. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  436. if (err)
  437. return err;
  438. if (args.size() != 4) {
  439. r_err_str = "Expected 4 arguments for constructor";
  440. }
  441. value = Rect2(args[0], args[1], args[2], args[3]);
  442. return OK;
  443. } else if (id == "Vector3") {
  444. Vector<float> args;
  445. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  446. if (err)
  447. return err;
  448. if (args.size() != 3) {
  449. r_err_str = "Expected 3 arguments for constructor";
  450. }
  451. value = Vector3(args[0], args[1], args[2]);
  452. return OK;
  453. } else if (id == "Transform2D" || id == "Matrix32") { //compatibility
  454. Vector<float> args;
  455. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  456. if (err)
  457. return err;
  458. if (args.size() != 6) {
  459. r_err_str = "Expected 6 arguments for constructor";
  460. }
  461. Transform2D m;
  462. m[0] = Vector2(args[0], args[1]);
  463. m[1] = Vector2(args[2], args[3]);
  464. m[2] = Vector2(args[4], args[5]);
  465. value = m;
  466. return OK;
  467. } else if (id == "Plane") {
  468. Vector<float> args;
  469. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  470. if (err)
  471. return err;
  472. if (args.size() != 4) {
  473. r_err_str = "Expected 4 arguments for constructor";
  474. }
  475. value = Plane(args[0], args[1], args[2], args[3]);
  476. return OK;
  477. } else if (id == "Quat") {
  478. Vector<float> args;
  479. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  480. if (err)
  481. return err;
  482. if (args.size() != 4) {
  483. r_err_str = "Expected 4 arguments for constructor";
  484. }
  485. value = Quat(args[0], args[1], args[2], args[3]);
  486. return OK;
  487. } else if (id == "AABB" || id == "Rect3") {
  488. Vector<float> args;
  489. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  490. if (err)
  491. return err;
  492. if (args.size() != 6) {
  493. r_err_str = "Expected 6 arguments for constructor";
  494. }
  495. value = AABB(Vector3(args[0], args[1], args[2]), Vector3(args[3], args[4], args[5]));
  496. return OK;
  497. } else if (id == "Basis" || id == "Matrix3") { //compatibility
  498. Vector<float> args;
  499. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  500. if (err)
  501. return err;
  502. if (args.size() != 9) {
  503. r_err_str = "Expected 9 arguments for constructor";
  504. }
  505. value = Basis(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
  506. return OK;
  507. } else if (id == "Transform") {
  508. Vector<float> args;
  509. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  510. if (err)
  511. return err;
  512. if (args.size() != 12) {
  513. r_err_str = "Expected 12 arguments for constructor";
  514. }
  515. 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]));
  516. return OK;
  517. } else if (id == "Color") {
  518. Vector<float> args;
  519. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  520. if (err)
  521. return err;
  522. if (args.size() != 4) {
  523. r_err_str = "Expected 4 arguments for constructor";
  524. }
  525. value = Color(args[0], args[1], args[2], args[3]);
  526. return OK;
  527. } else if (id == "NodePath") {
  528. get_token(p_stream, token, line, r_err_str);
  529. if (token.type != TK_PARENTHESIS_OPEN) {
  530. r_err_str = "Expected '('";
  531. return ERR_PARSE_ERROR;
  532. }
  533. get_token(p_stream, token, line, r_err_str);
  534. if (token.type != TK_STRING) {
  535. r_err_str = "Expected string as argument for NodePath()";
  536. return ERR_PARSE_ERROR;
  537. }
  538. value = NodePath(String(token.value));
  539. get_token(p_stream, token, line, r_err_str);
  540. if (token.type != TK_PARENTHESIS_CLOSE) {
  541. r_err_str = "Expected ')'";
  542. return ERR_PARSE_ERROR;
  543. }
  544. } else if (id == "RID") {
  545. get_token(p_stream, token, line, r_err_str);
  546. if (token.type != TK_PARENTHESIS_OPEN) {
  547. r_err_str = "Expected '('";
  548. return ERR_PARSE_ERROR;
  549. }
  550. get_token(p_stream, token, line, r_err_str);
  551. if (token.type != TK_NUMBER) {
  552. r_err_str = "Expected number as argument";
  553. return ERR_PARSE_ERROR;
  554. }
  555. value = token.value;
  556. get_token(p_stream, token, line, r_err_str);
  557. if (token.type != TK_PARENTHESIS_CLOSE) {
  558. r_err_str = "Expected ')'";
  559. return ERR_PARSE_ERROR;
  560. }
  561. return OK;
  562. } else if (id == "Object") {
  563. get_token(p_stream, token, line, r_err_str);
  564. if (token.type != TK_PARENTHESIS_OPEN) {
  565. r_err_str = "Expected '('";
  566. return ERR_PARSE_ERROR;
  567. }
  568. get_token(p_stream, token, line, r_err_str);
  569. if (token.type != TK_IDENTIFIER) {
  570. r_err_str = "Expected identifier with type of object";
  571. return ERR_PARSE_ERROR;
  572. }
  573. String type = token.value;
  574. Object *obj = ClassDB::instance(type);
  575. if (!obj) {
  576. r_err_str = "Can't instance Object() of type: " + type;
  577. return ERR_PARSE_ERROR;
  578. }
  579. get_token(p_stream, token, line, r_err_str);
  580. if (token.type != TK_COMMA) {
  581. r_err_str = "Expected ',' after object type";
  582. return ERR_PARSE_ERROR;
  583. }
  584. bool at_key = true;
  585. String key;
  586. Token token;
  587. bool need_comma = false;
  588. while (true) {
  589. if (p_stream->is_eof()) {
  590. r_err_str = "Unexpected End of File while parsing Object()";
  591. return ERR_FILE_CORRUPT;
  592. }
  593. if (at_key) {
  594. Error err = get_token(p_stream, token, line, r_err_str);
  595. if (err != OK)
  596. return err;
  597. if (token.type == TK_PARENTHESIS_CLOSE) {
  598. Reference *reference = Object::cast_to<Reference>(obj);
  599. if (reference) {
  600. value = REF(reference);
  601. } else {
  602. value = obj;
  603. }
  604. return OK;
  605. }
  606. if (need_comma) {
  607. if (token.type != TK_COMMA) {
  608. r_err_str = "Expected '}' or ','";
  609. return ERR_PARSE_ERROR;
  610. } else {
  611. need_comma = false;
  612. continue;
  613. }
  614. }
  615. if (token.type != TK_STRING) {
  616. r_err_str = "Expected property name as string";
  617. return ERR_PARSE_ERROR;
  618. }
  619. key = token.value;
  620. err = get_token(p_stream, token, line, r_err_str);
  621. if (err != OK)
  622. return err;
  623. if (token.type != TK_COLON) {
  624. r_err_str = "Expected ':'";
  625. return ERR_PARSE_ERROR;
  626. }
  627. at_key = false;
  628. } else {
  629. Error err = get_token(p_stream, token, line, r_err_str);
  630. if (err != OK)
  631. return err;
  632. Variant v;
  633. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  634. if (err)
  635. return err;
  636. obj->set(key, v);
  637. need_comma = true;
  638. at_key = true;
  639. }
  640. }
  641. return OK;
  642. } else if (id == "Resource" || id == "SubResource" || id == "ExtResource") {
  643. get_token(p_stream, token, line, r_err_str);
  644. if (token.type != TK_PARENTHESIS_OPEN) {
  645. r_err_str = "Expected '('";
  646. return ERR_PARSE_ERROR;
  647. }
  648. if (p_res_parser && id == "Resource" && p_res_parser->func) {
  649. RES res;
  650. Error err = p_res_parser->func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  651. if (err)
  652. return err;
  653. value = res;
  654. return OK;
  655. } else if (p_res_parser && id == "ExtResource" && p_res_parser->ext_func) {
  656. RES res;
  657. Error err = p_res_parser->ext_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  658. if (err)
  659. return err;
  660. value = res;
  661. return OK;
  662. } else if (p_res_parser && id == "SubResource" && p_res_parser->sub_func) {
  663. RES res;
  664. Error err = p_res_parser->sub_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  665. if (err)
  666. return err;
  667. value = res;
  668. return OK;
  669. } else {
  670. get_token(p_stream, token, line, r_err_str);
  671. if (token.type == TK_STRING) {
  672. String path = token.value;
  673. RES res = ResourceLoader::load(path);
  674. if (res.is_null()) {
  675. r_err_str = "Can't load resource at path: '" + path + "'.";
  676. return ERR_PARSE_ERROR;
  677. }
  678. get_token(p_stream, token, line, r_err_str);
  679. if (token.type != TK_PARENTHESIS_CLOSE) {
  680. r_err_str = "Expected ')'";
  681. return ERR_PARSE_ERROR;
  682. }
  683. value = res;
  684. return OK;
  685. } else {
  686. r_err_str = "Expected string as argument for Resource().";
  687. return ERR_PARSE_ERROR;
  688. }
  689. }
  690. return OK;
  691. #ifndef DISABLE_DEPRECATED
  692. } else if (id == "InputEvent") {
  693. get_token(p_stream, token, line, r_err_str);
  694. if (token.type != TK_PARENTHESIS_OPEN) {
  695. r_err_str = "Expected '('";
  696. return ERR_PARSE_ERROR;
  697. }
  698. get_token(p_stream, token, line, r_err_str);
  699. if (token.type != TK_IDENTIFIER) {
  700. r_err_str = "Expected identifier";
  701. return ERR_PARSE_ERROR;
  702. }
  703. String id = token.value;
  704. Ref<InputEvent> ie;
  705. if (id == "NONE") {
  706. get_token(p_stream, token, line, r_err_str);
  707. if (token.type != TK_PARENTHESIS_CLOSE) {
  708. r_err_str = "Expected ')'";
  709. return ERR_PARSE_ERROR;
  710. }
  711. } else if (id == "KEY") {
  712. Ref<InputEventKey> key;
  713. key.instance();
  714. ie = key;
  715. get_token(p_stream, token, line, r_err_str);
  716. if (token.type != TK_COMMA) {
  717. r_err_str = "Expected ','";
  718. return ERR_PARSE_ERROR;
  719. }
  720. get_token(p_stream, token, line, r_err_str);
  721. if (token.type == TK_IDENTIFIER) {
  722. String name = token.value;
  723. key->set_scancode(find_keycode(name));
  724. } else if (token.type == TK_NUMBER) {
  725. key->set_scancode(token.value);
  726. } else {
  727. r_err_str = "Expected string or integer for keycode";
  728. return ERR_PARSE_ERROR;
  729. }
  730. get_token(p_stream, token, line, r_err_str);
  731. if (token.type == TK_COMMA) {
  732. get_token(p_stream, token, line, r_err_str);
  733. if (token.type != TK_IDENTIFIER) {
  734. r_err_str = "Expected identifier with modifier flas";
  735. return ERR_PARSE_ERROR;
  736. }
  737. String mods = token.value;
  738. if (mods.findn("C") != -1)
  739. key->set_control(true);
  740. if (mods.findn("A") != -1)
  741. key->set_alt(true);
  742. if (mods.findn("S") != -1)
  743. key->set_shift(true);
  744. if (mods.findn("M") != -1)
  745. key->set_metakey(true);
  746. get_token(p_stream, token, line, r_err_str);
  747. if (token.type != TK_PARENTHESIS_CLOSE) {
  748. r_err_str = "Expected ')'";
  749. return ERR_PARSE_ERROR;
  750. }
  751. } else if (token.type != TK_PARENTHESIS_CLOSE) {
  752. r_err_str = "Expected ')' or modifier flags.";
  753. return ERR_PARSE_ERROR;
  754. }
  755. } else if (id == "MBUTTON") {
  756. Ref<InputEventMouseButton> mb;
  757. mb.instance();
  758. ie = mb;
  759. get_token(p_stream, token, line, r_err_str);
  760. if (token.type != TK_COMMA) {
  761. r_err_str = "Expected ','";
  762. return ERR_PARSE_ERROR;
  763. }
  764. get_token(p_stream, token, line, r_err_str);
  765. if (token.type != TK_NUMBER) {
  766. r_err_str = "Expected button index";
  767. return ERR_PARSE_ERROR;
  768. }
  769. mb->set_button_index(token.value);
  770. get_token(p_stream, token, line, r_err_str);
  771. if (token.type != TK_PARENTHESIS_CLOSE) {
  772. r_err_str = "Expected ')'";
  773. return ERR_PARSE_ERROR;
  774. }
  775. } else if (id == "JBUTTON") {
  776. Ref<InputEventJoypadButton> jb;
  777. jb.instance();
  778. ie = jb;
  779. get_token(p_stream, token, line, r_err_str);
  780. if (token.type != TK_COMMA) {
  781. r_err_str = "Expected ','";
  782. return ERR_PARSE_ERROR;
  783. }
  784. get_token(p_stream, token, line, r_err_str);
  785. if (token.type != TK_NUMBER) {
  786. r_err_str = "Expected button index";
  787. return ERR_PARSE_ERROR;
  788. }
  789. jb->set_button_index(token.value);
  790. get_token(p_stream, token, line, r_err_str);
  791. if (token.type != TK_PARENTHESIS_CLOSE) {
  792. r_err_str = "Expected ')'";
  793. return ERR_PARSE_ERROR;
  794. }
  795. } else if (id == "JAXIS") {
  796. Ref<InputEventJoypadMotion> jm;
  797. jm.instance();
  798. ie = jm;
  799. get_token(p_stream, token, line, r_err_str);
  800. if (token.type != TK_COMMA) {
  801. r_err_str = "Expected ','";
  802. return ERR_PARSE_ERROR;
  803. }
  804. get_token(p_stream, token, line, r_err_str);
  805. if (token.type != TK_NUMBER) {
  806. r_err_str = "Expected axis index";
  807. return ERR_PARSE_ERROR;
  808. }
  809. jm->set_axis(token.value);
  810. get_token(p_stream, token, line, r_err_str);
  811. if (token.type != TK_COMMA) {
  812. r_err_str = "Expected ',' after axis index";
  813. return ERR_PARSE_ERROR;
  814. }
  815. get_token(p_stream, token, line, r_err_str);
  816. if (token.type != TK_NUMBER) {
  817. r_err_str = "Expected axis sign";
  818. return ERR_PARSE_ERROR;
  819. }
  820. jm->set_axis_value(token.value);
  821. get_token(p_stream, token, line, r_err_str);
  822. if (token.type != TK_PARENTHESIS_CLOSE) {
  823. r_err_str = "Expected ')' for jaxis";
  824. return ERR_PARSE_ERROR;
  825. }
  826. } else {
  827. r_err_str = "Invalid input event type.";
  828. return ERR_PARSE_ERROR;
  829. }
  830. value = ie;
  831. return OK;
  832. #endif
  833. } else if (id == "PoolByteArray" || id == "ByteArray") {
  834. Vector<uint8_t> args;
  835. Error err = _parse_construct<uint8_t>(p_stream, args, line, r_err_str);
  836. if (err)
  837. return err;
  838. PoolVector<uint8_t> arr;
  839. {
  840. int len = args.size();
  841. arr.resize(len);
  842. PoolVector<uint8_t>::Write w = arr.write();
  843. for (int i = 0; i < len; i++) {
  844. w[i] = args[i];
  845. }
  846. }
  847. value = arr;
  848. return OK;
  849. } else if (id == "PoolIntArray" || id == "IntArray") {
  850. Vector<int> args;
  851. Error err = _parse_construct<int>(p_stream, args, line, r_err_str);
  852. if (err)
  853. return err;
  854. PoolVector<int> arr;
  855. {
  856. int len = args.size();
  857. arr.resize(len);
  858. PoolVector<int>::Write w = arr.write();
  859. for (int i = 0; i < len; i++) {
  860. w[i] = int(args[i]);
  861. }
  862. }
  863. value = arr;
  864. return OK;
  865. } else if (id == "PoolRealArray" || id == "FloatArray") {
  866. Vector<float> args;
  867. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  868. if (err)
  869. return err;
  870. PoolVector<float> arr;
  871. {
  872. int len = args.size();
  873. arr.resize(len);
  874. PoolVector<float>::Write w = arr.write();
  875. for (int i = 0; i < len; i++) {
  876. w[i] = args[i];
  877. }
  878. }
  879. value = arr;
  880. return OK;
  881. } else if (id == "PoolStringArray" || id == "StringArray") {
  882. get_token(p_stream, token, line, r_err_str);
  883. if (token.type != TK_PARENTHESIS_OPEN) {
  884. r_err_str = "Expected '('";
  885. return ERR_PARSE_ERROR;
  886. }
  887. Vector<String> cs;
  888. bool first = true;
  889. while (true) {
  890. if (!first) {
  891. get_token(p_stream, token, line, r_err_str);
  892. if (token.type == TK_COMMA) {
  893. //do none
  894. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  895. break;
  896. } else {
  897. r_err_str = "Expected ',' or ')'";
  898. return ERR_PARSE_ERROR;
  899. }
  900. }
  901. get_token(p_stream, token, line, r_err_str);
  902. if (token.type == TK_PARENTHESIS_CLOSE) {
  903. break;
  904. } else if (token.type != TK_STRING) {
  905. r_err_str = "Expected string";
  906. return ERR_PARSE_ERROR;
  907. }
  908. first = false;
  909. cs.push_back(token.value);
  910. }
  911. PoolVector<String> arr;
  912. {
  913. int len = cs.size();
  914. arr.resize(len);
  915. PoolVector<String>::Write w = arr.write();
  916. for (int i = 0; i < len; i++) {
  917. w[i] = cs[i];
  918. }
  919. }
  920. value = arr;
  921. return OK;
  922. } else if (id == "PoolVector2Array" || id == "Vector2Array") {
  923. Vector<float> args;
  924. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  925. if (err)
  926. return err;
  927. PoolVector<Vector2> arr;
  928. {
  929. int len = args.size() / 2;
  930. arr.resize(len);
  931. PoolVector<Vector2>::Write w = arr.write();
  932. for (int i = 0; i < len; i++) {
  933. w[i] = Vector2(args[i * 2 + 0], args[i * 2 + 1]);
  934. }
  935. }
  936. value = arr;
  937. return OK;
  938. } else if (id == "PoolVector3Array" || id == "Vector3Array") {
  939. Vector<float> args;
  940. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  941. if (err)
  942. return err;
  943. PoolVector<Vector3> arr;
  944. {
  945. int len = args.size() / 3;
  946. arr.resize(len);
  947. PoolVector<Vector3>::Write w = arr.write();
  948. for (int i = 0; i < len; i++) {
  949. w[i] = Vector3(args[i * 3 + 0], args[i * 3 + 1], args[i * 3 + 2]);
  950. }
  951. }
  952. value = arr;
  953. return OK;
  954. } else if (id == "PoolColorArray" || id == "ColorArray") {
  955. Vector<float> args;
  956. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  957. if (err)
  958. return err;
  959. PoolVector<Color> arr;
  960. {
  961. int len = args.size() / 4;
  962. arr.resize(len);
  963. PoolVector<Color>::Write w = arr.write();
  964. for (int i = 0; i < len; i++) {
  965. w[i] = Color(args[i * 4 + 0], args[i * 4 + 1], args[i * 4 + 2], args[i * 4 + 3]);
  966. }
  967. }
  968. value = arr;
  969. return OK;
  970. } else {
  971. r_err_str = "Unexpected identifier: '" + id + "'.";
  972. return ERR_PARSE_ERROR;
  973. }
  974. return OK;
  975. } else if (token.type == TK_NUMBER) {
  976. value = token.value;
  977. return OK;
  978. } else if (token.type == TK_STRING) {
  979. value = token.value;
  980. return OK;
  981. } else if (token.type == TK_COLOR) {
  982. value = token.value;
  983. return OK;
  984. } else {
  985. r_err_str = "Expected value, got " + String(tk_name[token.type]) + ".";
  986. return ERR_PARSE_ERROR;
  987. }
  988. return ERR_PARSE_ERROR;
  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. return OK;
  1021. }
  1022. Error VariantParser::_parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  1023. bool at_key = true;
  1024. Variant key;
  1025. Token token;
  1026. bool need_comma = false;
  1027. while (true) {
  1028. if (p_stream->is_eof()) {
  1029. r_err_str = "Unexpected End of File while parsing dictionary";
  1030. return ERR_FILE_CORRUPT;
  1031. }
  1032. if (at_key) {
  1033. Error err = get_token(p_stream, token, line, r_err_str);
  1034. if (err != OK)
  1035. return err;
  1036. if (token.type == TK_CURLY_BRACKET_CLOSE) {
  1037. return OK;
  1038. }
  1039. if (need_comma) {
  1040. if (token.type != TK_COMMA) {
  1041. r_err_str = "Expected '}' or ','";
  1042. return ERR_PARSE_ERROR;
  1043. } else {
  1044. need_comma = false;
  1045. continue;
  1046. }
  1047. }
  1048. err = parse_value(token, key, p_stream, line, r_err_str, p_res_parser);
  1049. if (err)
  1050. return err;
  1051. err = get_token(p_stream, token, line, r_err_str);
  1052. if (err != OK)
  1053. return err;
  1054. if (token.type != TK_COLON) {
  1055. r_err_str = "Expected ':'";
  1056. return ERR_PARSE_ERROR;
  1057. }
  1058. at_key = false;
  1059. } else {
  1060. Error err = get_token(p_stream, token, line, r_err_str);
  1061. if (err != OK)
  1062. return err;
  1063. Variant v;
  1064. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1065. if (err)
  1066. return err;
  1067. object[key] = v;
  1068. need_comma = true;
  1069. at_key = true;
  1070. }
  1071. }
  1072. return OK;
  1073. }
  1074. 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) {
  1075. r_tag.fields.clear();
  1076. if (token.type != TK_BRACKET_OPEN) {
  1077. r_err_str = "Expected '['";
  1078. return ERR_PARSE_ERROR;
  1079. }
  1080. if (p_simple_tag) {
  1081. r_tag.name = "";
  1082. r_tag.fields.clear();
  1083. while (true) {
  1084. CharType c = p_stream->get_char();
  1085. if (p_stream->is_eof()) {
  1086. r_err_str = "Unexpected EOF while parsing simple tag";
  1087. return ERR_PARSE_ERROR;
  1088. }
  1089. if (c == ']')
  1090. break;
  1091. r_tag.name += String::chr(c);
  1092. }
  1093. r_tag.name = r_tag.name.strip_edges();
  1094. return OK;
  1095. }
  1096. get_token(p_stream, token, line, r_err_str);
  1097. if (token.type != TK_IDENTIFIER) {
  1098. r_err_str = "Expected identifier (tag name)";
  1099. return ERR_PARSE_ERROR;
  1100. }
  1101. r_tag.name = token.value;
  1102. bool parsing_tag = true;
  1103. while (true) {
  1104. if (p_stream->is_eof()) {
  1105. r_err_str = "Unexpected End of File while parsing tag: " + r_tag.name;
  1106. return ERR_FILE_CORRUPT;
  1107. }
  1108. get_token(p_stream, token, line, r_err_str);
  1109. if (token.type == TK_BRACKET_CLOSE)
  1110. break;
  1111. if (parsing_tag && token.type == TK_PERIOD) {
  1112. r_tag.name += "."; //support tags such as [someprop.Android] for specific platforms
  1113. get_token(p_stream, token, line, r_err_str);
  1114. } else if (parsing_tag && token.type == TK_COLON) {
  1115. r_tag.name += ":"; //support tags such as [someprop.Android] for specific platforms
  1116. get_token(p_stream, token, line, r_err_str);
  1117. } else {
  1118. parsing_tag = false;
  1119. }
  1120. if (token.type != TK_IDENTIFIER) {
  1121. r_err_str = "Expected Identifier";
  1122. return ERR_PARSE_ERROR;
  1123. }
  1124. String id = token.value;
  1125. if (parsing_tag) {
  1126. r_tag.name += id;
  1127. continue;
  1128. }
  1129. get_token(p_stream, token, line, r_err_str);
  1130. if (token.type != TK_EQUAL) {
  1131. return ERR_PARSE_ERROR;
  1132. }
  1133. get_token(p_stream, token, line, r_err_str);
  1134. Variant value;
  1135. Error err = parse_value(token, value, p_stream, line, r_err_str, p_res_parser);
  1136. if (err)
  1137. return err;
  1138. r_tag.fields[id] = value;
  1139. }
  1140. return OK;
  1141. }
  1142. Error VariantParser::parse_tag(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser, bool p_simple_tag) {
  1143. Token token;
  1144. get_token(p_stream, token, line, r_err_str);
  1145. if (token.type == TK_EOF) {
  1146. return ERR_FILE_EOF;
  1147. }
  1148. if (token.type != TK_BRACKET_OPEN) {
  1149. r_err_str = "Expected '['";
  1150. return ERR_PARSE_ERROR;
  1151. }
  1152. return _parse_tag(token, p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1153. }
  1154. 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) {
  1155. //assign..
  1156. r_assign = "";
  1157. String what;
  1158. while (true) {
  1159. CharType c;
  1160. if (p_stream->saved) {
  1161. c = p_stream->saved;
  1162. p_stream->saved = 0;
  1163. } else {
  1164. c = p_stream->get_char();
  1165. }
  1166. if (p_stream->is_eof())
  1167. return ERR_FILE_EOF;
  1168. if (c == ';') { //comment
  1169. while (true) {
  1170. CharType ch = p_stream->get_char();
  1171. if (p_stream->is_eof()) {
  1172. return ERR_FILE_EOF;
  1173. }
  1174. if (ch == '\n')
  1175. break;
  1176. }
  1177. continue;
  1178. }
  1179. if (c == '[' && what.length() == 0) {
  1180. //it's a tag!
  1181. p_stream->saved = '['; //go back one
  1182. Error err = parse_tag(p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1183. return err;
  1184. }
  1185. if (c > 32) {
  1186. if (c == '"') { //quoted
  1187. p_stream->saved = '"';
  1188. Token tk;
  1189. Error err = get_token(p_stream, tk, line, r_err_str);
  1190. if (err)
  1191. return err;
  1192. if (tk.type != TK_STRING) {
  1193. r_err_str = "Error reading quoted string";
  1194. return err;
  1195. }
  1196. what = tk.value;
  1197. } else if (c != '=') {
  1198. what += String::chr(c);
  1199. } else {
  1200. r_assign = what;
  1201. Token token;
  1202. get_token(p_stream, token, line, r_err_str);
  1203. Error err = parse_value(token, r_value, p_stream, line, r_err_str, p_res_parser);
  1204. if (err) {
  1205. }
  1206. return err;
  1207. }
  1208. } else if (c == '\n') {
  1209. line++;
  1210. }
  1211. }
  1212. return OK;
  1213. }
  1214. Error VariantParser::parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser) {
  1215. Token token;
  1216. Error err = get_token(p_stream, token, r_err_line, r_err_str);
  1217. if (err)
  1218. return err;
  1219. if (token.type == TK_EOF) {
  1220. return ERR_FILE_EOF;
  1221. }
  1222. return parse_value(token, r_ret, p_stream, r_err_line, r_err_str, p_res_parser);
  1223. }
  1224. //////////////////////////////////////////////////////////////////////////////////
  1225. //////////////////////////////////////////////////////////////////////////////////
  1226. ////////////////////////////////////////////////////////////////////////////////
  1227. static String rtosfix(double p_value) {
  1228. if (p_value == 0.0)
  1229. return "0"; //avoid negative zero (-0) being written, which may annoy git, svn, etc. for changes when they don't exist.
  1230. else
  1231. return rtoss(p_value);
  1232. }
  1233. 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) {
  1234. switch (p_variant.get_type()) {
  1235. case Variant::NIL: {
  1236. p_store_string_func(p_store_string_ud, "null");
  1237. } break;
  1238. case Variant::BOOL: {
  1239. p_store_string_func(p_store_string_ud, p_variant.operator bool() ? "true" : "false");
  1240. } break;
  1241. case Variant::INT: {
  1242. p_store_string_func(p_store_string_ud, itos(p_variant.operator int()));
  1243. } break;
  1244. case Variant::REAL: {
  1245. String s = rtosfix(p_variant.operator real_t());
  1246. if (s.find(".") == -1 && s.find("e") == -1)
  1247. s += ".0";
  1248. p_store_string_func(p_store_string_ud, s);
  1249. } break;
  1250. case Variant::STRING: {
  1251. String str = p_variant;
  1252. str = "\"" + str.c_escape_multiline() + "\"";
  1253. p_store_string_func(p_store_string_ud, str);
  1254. } break;
  1255. case Variant::VECTOR2: {
  1256. Vector2 v = p_variant;
  1257. p_store_string_func(p_store_string_ud, "Vector2( " + rtosfix(v.x) + ", " + rtosfix(v.y) + " )");
  1258. } break;
  1259. case Variant::RECT2: {
  1260. Rect2 aabb = p_variant;
  1261. 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) + " )");
  1262. } break;
  1263. case Variant::VECTOR3: {
  1264. Vector3 v = p_variant;
  1265. p_store_string_func(p_store_string_ud, "Vector3( " + rtosfix(v.x) + ", " + rtosfix(v.y) + ", " + rtosfix(v.z) + " )");
  1266. } break;
  1267. case Variant::PLANE: {
  1268. Plane p = p_variant;
  1269. p_store_string_func(p_store_string_ud, "Plane( " + rtosfix(p.normal.x) + ", " + rtosfix(p.normal.y) + ", " + rtosfix(p.normal.z) + ", " + rtosfix(p.d) + " )");
  1270. } break;
  1271. case Variant::AABB: {
  1272. AABB aabb = p_variant;
  1273. 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) + " )");
  1274. } break;
  1275. case Variant::QUAT: {
  1276. Quat quat = p_variant;
  1277. p_store_string_func(p_store_string_ud, "Quat( " + rtosfix(quat.x) + ", " + rtosfix(quat.y) + ", " + rtosfix(quat.z) + ", " + rtosfix(quat.w) + " )");
  1278. } break;
  1279. case Variant::TRANSFORM2D: {
  1280. String s = "Transform2D( ";
  1281. Transform2D m3 = p_variant;
  1282. for (int i = 0; i < 3; i++) {
  1283. for (int j = 0; j < 2; j++) {
  1284. if (i != 0 || j != 0)
  1285. s += ", ";
  1286. s += rtosfix(m3.elements[i][j]);
  1287. }
  1288. }
  1289. p_store_string_func(p_store_string_ud, s + " )");
  1290. } break;
  1291. case Variant::BASIS: {
  1292. String s = "Basis( ";
  1293. Basis m3 = p_variant;
  1294. for (int i = 0; i < 3; i++) {
  1295. for (int j = 0; j < 3; j++) {
  1296. if (i != 0 || j != 0)
  1297. s += ", ";
  1298. s += rtosfix(m3.elements[i][j]);
  1299. }
  1300. }
  1301. p_store_string_func(p_store_string_ud, s + " )");
  1302. } break;
  1303. case Variant::TRANSFORM: {
  1304. String s = "Transform( ";
  1305. Transform t = p_variant;
  1306. Basis &m3 = t.basis;
  1307. for (int i = 0; i < 3; i++) {
  1308. for (int j = 0; j < 3; j++) {
  1309. if (i != 0 || j != 0)
  1310. s += ", ";
  1311. s += rtosfix(m3.elements[i][j]);
  1312. }
  1313. }
  1314. s = s + ", " + rtosfix(t.origin.x) + ", " + rtosfix(t.origin.y) + ", " + rtosfix(t.origin.z);
  1315. p_store_string_func(p_store_string_ud, s + " )");
  1316. } break;
  1317. // misc types
  1318. case Variant::COLOR: {
  1319. Color c = p_variant;
  1320. p_store_string_func(p_store_string_ud, "Color( " + rtosfix(c.r) + ", " + rtosfix(c.g) + ", " + rtosfix(c.b) + ", " + rtosfix(c.a) + " )");
  1321. } break;
  1322. case Variant::NODE_PATH: {
  1323. String str = p_variant;
  1324. str = "NodePath(\"" + str.c_escape() + "\")";
  1325. p_store_string_func(p_store_string_ud, str);
  1326. } break;
  1327. case Variant::OBJECT: {
  1328. Object *obj = p_variant;
  1329. if (!obj) {
  1330. p_store_string_func(p_store_string_ud, "null");
  1331. break; // don't save it
  1332. }
  1333. RES res = p_variant;
  1334. if (res.is_valid()) {
  1335. //is resource
  1336. String res_text;
  1337. //try external function
  1338. if (p_encode_res_func) {
  1339. res_text = p_encode_res_func(p_encode_res_ud, res);
  1340. }
  1341. //try path because it's a file
  1342. if (res_text == String() && res->get_path().is_resource_file()) {
  1343. //external resource
  1344. String path = res->get_path();
  1345. res_text = "Resource( \"" + path + "\")";
  1346. }
  1347. //could come up with some sort of text
  1348. if (res_text != String()) {
  1349. p_store_string_func(p_store_string_ud, res_text);
  1350. break;
  1351. }
  1352. }
  1353. //store as generic object
  1354. p_store_string_func(p_store_string_ud, "Object(" + obj->get_class() + ",");
  1355. List<PropertyInfo> props;
  1356. obj->get_property_list(&props);
  1357. bool first = true;
  1358. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1359. if (E->get().usage & PROPERTY_USAGE_STORAGE || E->get().usage & PROPERTY_USAGE_SCRIPT_VARIABLE) {
  1360. //must be serialized
  1361. if (first) {
  1362. first = false;
  1363. } else {
  1364. p_store_string_func(p_store_string_ud, ",");
  1365. }
  1366. p_store_string_func(p_store_string_ud, "\"" + E->get().name + "\":");
  1367. write(obj->get(E->get().name), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1368. }
  1369. }
  1370. p_store_string_func(p_store_string_ud, ")\n");
  1371. } break;
  1372. case Variant::DICTIONARY: {
  1373. Dictionary dict = p_variant;
  1374. List<Variant> keys;
  1375. dict.get_key_list(&keys);
  1376. keys.sort();
  1377. p_store_string_func(p_store_string_ud, "{\n");
  1378. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1379. /*
  1380. if (!_check_type(dict[E->get()]))
  1381. continue;
  1382. */
  1383. write(E->get(), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1384. p_store_string_func(p_store_string_ud, ": ");
  1385. write(dict[E->get()], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1386. if (E->next())
  1387. p_store_string_func(p_store_string_ud, ",\n");
  1388. }
  1389. p_store_string_func(p_store_string_ud, "\n}");
  1390. } break;
  1391. case Variant::ARRAY: {
  1392. p_store_string_func(p_store_string_ud, "[ ");
  1393. Array array = p_variant;
  1394. int len = array.size();
  1395. for (int i = 0; i < len; i++) {
  1396. if (i > 0)
  1397. p_store_string_func(p_store_string_ud, ", ");
  1398. write(array[i], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1399. }
  1400. p_store_string_func(p_store_string_ud, " ]");
  1401. } break;
  1402. case Variant::POOL_BYTE_ARRAY: {
  1403. p_store_string_func(p_store_string_ud, "PoolByteArray( ");
  1404. String s;
  1405. PoolVector<uint8_t> data = p_variant;
  1406. int len = data.size();
  1407. PoolVector<uint8_t>::Read r = data.read();
  1408. const uint8_t *ptr = r.ptr();
  1409. for (int i = 0; i < len; i++) {
  1410. if (i > 0)
  1411. p_store_string_func(p_store_string_ud, ", ");
  1412. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  1413. }
  1414. p_store_string_func(p_store_string_ud, " )");
  1415. } break;
  1416. case Variant::POOL_INT_ARRAY: {
  1417. p_store_string_func(p_store_string_ud, "PoolIntArray( ");
  1418. PoolVector<int> data = p_variant;
  1419. int len = data.size();
  1420. PoolVector<int>::Read r = data.read();
  1421. const int *ptr = r.ptr();
  1422. for (int i = 0; i < len; i++) {
  1423. if (i > 0)
  1424. p_store_string_func(p_store_string_ud, ", ");
  1425. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  1426. }
  1427. p_store_string_func(p_store_string_ud, " )");
  1428. } break;
  1429. case Variant::POOL_REAL_ARRAY: {
  1430. p_store_string_func(p_store_string_ud, "PoolRealArray( ");
  1431. PoolVector<real_t> data = p_variant;
  1432. int len = data.size();
  1433. PoolVector<real_t>::Read r = data.read();
  1434. const real_t *ptr = r.ptr();
  1435. for (int i = 0; i < len; i++) {
  1436. if (i > 0)
  1437. p_store_string_func(p_store_string_ud, ", ");
  1438. p_store_string_func(p_store_string_ud, rtosfix(ptr[i]));
  1439. }
  1440. p_store_string_func(p_store_string_ud, " )");
  1441. } break;
  1442. case Variant::POOL_STRING_ARRAY: {
  1443. p_store_string_func(p_store_string_ud, "PoolStringArray( ");
  1444. PoolVector<String> data = p_variant;
  1445. int len = data.size();
  1446. PoolVector<String>::Read r = data.read();
  1447. const String *ptr = r.ptr();
  1448. String s;
  1449. //write_string("\n");
  1450. for (int i = 0; i < len; i++) {
  1451. if (i > 0)
  1452. p_store_string_func(p_store_string_ud, ", ");
  1453. String str = ptr[i];
  1454. p_store_string_func(p_store_string_ud, "\"" + str.c_escape() + "\"");
  1455. }
  1456. p_store_string_func(p_store_string_ud, " )");
  1457. } break;
  1458. case Variant::POOL_VECTOR2_ARRAY: {
  1459. p_store_string_func(p_store_string_ud, "PoolVector2Array( ");
  1460. PoolVector<Vector2> data = p_variant;
  1461. int len = data.size();
  1462. PoolVector<Vector2>::Read r = data.read();
  1463. const Vector2 *ptr = r.ptr();
  1464. for (int i = 0; i < len; i++) {
  1465. if (i > 0)
  1466. p_store_string_func(p_store_string_ud, ", ");
  1467. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].x) + ", " + rtosfix(ptr[i].y));
  1468. }
  1469. p_store_string_func(p_store_string_ud, " )");
  1470. } break;
  1471. case Variant::POOL_VECTOR3_ARRAY: {
  1472. p_store_string_func(p_store_string_ud, "PoolVector3Array( ");
  1473. PoolVector<Vector3> data = p_variant;
  1474. int len = data.size();
  1475. PoolVector<Vector3>::Read r = data.read();
  1476. const Vector3 *ptr = r.ptr();
  1477. for (int i = 0; i < len; i++) {
  1478. if (i > 0)
  1479. p_store_string_func(p_store_string_ud, ", ");
  1480. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].x) + ", " + rtosfix(ptr[i].y) + ", " + rtosfix(ptr[i].z));
  1481. }
  1482. p_store_string_func(p_store_string_ud, " )");
  1483. } break;
  1484. case Variant::POOL_COLOR_ARRAY: {
  1485. p_store_string_func(p_store_string_ud, "PoolColorArray( ");
  1486. PoolVector<Color> data = p_variant;
  1487. int len = data.size();
  1488. PoolVector<Color>::Read r = data.read();
  1489. const Color *ptr = r.ptr();
  1490. for (int i = 0; i < len; i++) {
  1491. if (i > 0)
  1492. p_store_string_func(p_store_string_ud, ", ");
  1493. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].r) + ", " + rtosfix(ptr[i].g) + ", " + rtosfix(ptr[i].b) + ", " + rtosfix(ptr[i].a));
  1494. }
  1495. p_store_string_func(p_store_string_ud, " )");
  1496. } break;
  1497. default: {}
  1498. }
  1499. return OK;
  1500. }
  1501. static Error _write_to_str(void *ud, const String &p_string) {
  1502. String *str = (String *)ud;
  1503. (*str) += p_string;
  1504. return OK;
  1505. }
  1506. Error VariantWriter::write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud) {
  1507. r_string = String();
  1508. return write(p_variant, _write_to_str, &r_string, p_encode_res_func, p_encode_res_ud);
  1509. }