variant_parser.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  1. /*************************************************************************/
  2. /* variant_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "variant_parser.h"
  31. #include "io/resource_loader.h"
  32. #include "os/input_event.h"
  33. #include "os/keyboard.h"
  34. CharType VariantParser::StreamFile::get_char() {
  35. return f->get_8();
  36. }
  37. bool VariantParser::StreamFile::is_utf8() const {
  38. return true;
  39. }
  40. bool VariantParser::StreamFile::is_eof() const {
  41. return f->eof_reached();
  42. }
  43. CharType VariantParser::StreamString::get_char() {
  44. if (pos >= s.length())
  45. return 0;
  46. else
  47. return s[pos++];
  48. }
  49. bool VariantParser::StreamString::is_utf8() const {
  50. return false;
  51. }
  52. bool VariantParser::StreamString::is_eof() const {
  53. return pos > s.length();
  54. }
  55. /////////////////////////////////////////////////////////////////////////////////////////////////
  56. const char *VariantParser::tk_name[TK_MAX] = {
  57. "'{'",
  58. "'}'",
  59. "'['",
  60. "']'",
  61. "'('",
  62. "')'",
  63. "identifier",
  64. "string",
  65. "number",
  66. "color",
  67. "':'",
  68. "','",
  69. "'.'",
  70. "'='",
  71. "EOF",
  72. "ERROR"
  73. };
  74. Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, String &r_err_str) {
  75. while (true) {
  76. CharType cchar;
  77. if (p_stream->saved) {
  78. cchar = p_stream->saved;
  79. p_stream->saved = 0;
  80. } else {
  81. cchar = p_stream->get_char();
  82. if (p_stream->is_eof()) {
  83. r_token.type = TK_EOF;
  84. return OK;
  85. }
  86. }
  87. switch (cchar) {
  88. case '\n': {
  89. line++;
  90. break;
  91. };
  92. case 0: {
  93. r_token.type = TK_EOF;
  94. return OK;
  95. } break;
  96. case '{': {
  97. r_token.type = TK_CURLY_BRACKET_OPEN;
  98. return OK;
  99. };
  100. case '}': {
  101. r_token.type = TK_CURLY_BRACKET_CLOSE;
  102. return OK;
  103. };
  104. case '[': {
  105. r_token.type = TK_BRACKET_OPEN;
  106. return OK;
  107. };
  108. case ']': {
  109. r_token.type = TK_BRACKET_CLOSE;
  110. return OK;
  111. };
  112. case '(': {
  113. r_token.type = TK_PARENTHESIS_OPEN;
  114. return OK;
  115. };
  116. case ')': {
  117. r_token.type = TK_PARENTHESIS_CLOSE;
  118. return OK;
  119. };
  120. case ':': {
  121. r_token.type = TK_COLON;
  122. return OK;
  123. };
  124. case ';': {
  125. while (true) {
  126. CharType ch = p_stream->get_char();
  127. if (p_stream->is_eof()) {
  128. r_token.type = TK_EOF;
  129. return OK;
  130. }
  131. if (ch == '\n')
  132. break;
  133. }
  134. break;
  135. };
  136. case ',': {
  137. r_token.type = TK_COMMA;
  138. return OK;
  139. };
  140. case '.': {
  141. r_token.type = TK_PERIOD;
  142. return OK;
  143. };
  144. case '=': {
  145. r_token.type = TK_EQUAL;
  146. return OK;
  147. };
  148. case '#': {
  149. String color_str = "#";
  150. while (true) {
  151. CharType ch = p_stream->get_char();
  152. if (p_stream->is_eof()) {
  153. r_token.type = TK_EOF;
  154. return OK;
  155. } else if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
  156. color_str += String::chr(ch);
  157. } else {
  158. p_stream->saved = ch;
  159. break;
  160. }
  161. }
  162. r_token.value = Color::html(color_str);
  163. r_token.type = TK_COLOR;
  164. return OK;
  165. };
  166. case '"': {
  167. String str;
  168. while (true) {
  169. CharType ch = p_stream->get_char();
  170. if (ch == 0) {
  171. r_err_str = "Unterminated String";
  172. r_token.type = TK_ERROR;
  173. return ERR_PARSE_ERROR;
  174. } else if (ch == '"') {
  175. break;
  176. } else if (ch == '\\') {
  177. //escaped characters...
  178. CharType next = p_stream->get_char();
  179. if (next == 0) {
  180. r_err_str = "Unterminated String";
  181. r_token.type = TK_ERROR;
  182. return ERR_PARSE_ERROR;
  183. }
  184. CharType res = 0;
  185. switch (next) {
  186. case 'b': res = 8; break;
  187. case 't': res = 9; break;
  188. case 'n': res = 10; break;
  189. case 'f': res = 12; break;
  190. case 'r': res = 13; break;
  191. case 'u': {
  192. //hexnumbarh - oct is deprecated
  193. for (int j = 0; j < 4; j++) {
  194. CharType c = p_stream->get_char();
  195. if (c == 0) {
  196. r_err_str = "Unterminated String";
  197. r_token.type = TK_ERROR;
  198. return ERR_PARSE_ERROR;
  199. }
  200. if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
  201. r_err_str = "Malformed hex constant in string";
  202. r_token.type = TK_ERROR;
  203. return ERR_PARSE_ERROR;
  204. }
  205. CharType v;
  206. if (c >= '0' && c <= '9') {
  207. v = c - '0';
  208. } else if (c >= 'a' && c <= 'f') {
  209. v = c - 'a';
  210. v += 10;
  211. } else if (c >= 'A' && c <= 'F') {
  212. v = c - 'A';
  213. v += 10;
  214. } else {
  215. ERR_PRINT("BUG");
  216. v = 0;
  217. }
  218. res <<= 4;
  219. res |= v;
  220. }
  221. } break;
  222. //case '\"': res='\"'; break;
  223. //case '\\': res='\\'; break;
  224. //case '/': res='/'; break;
  225. default: {
  226. res = next;
  227. //r_err_str="Invalid escape sequence";
  228. //return ERR_PARSE_ERROR;
  229. } break;
  230. }
  231. str += res;
  232. } else {
  233. if (ch == '\n')
  234. line++;
  235. str += ch;
  236. }
  237. }
  238. if (p_stream->is_utf8()) {
  239. str.parse_utf8(str.ascii(true).get_data());
  240. }
  241. r_token.type = TK_STRING;
  242. r_token.value = str;
  243. return OK;
  244. } break;
  245. default: {
  246. if (cchar <= 32) {
  247. break;
  248. }
  249. if (cchar == '-' || (cchar >= '0' && cchar <= '9')) {
  250. //a number
  251. String num;
  252. #define READING_SIGN 0
  253. #define READING_INT 1
  254. #define READING_DEC 2
  255. #define READING_EXP 3
  256. #define READING_DONE 4
  257. int reading = READING_INT;
  258. if (cchar == '-') {
  259. num += '-';
  260. cchar = p_stream->get_char();
  261. }
  262. CharType c = cchar;
  263. bool exp_sign = false;
  264. bool exp_beg = false;
  265. bool is_float = false;
  266. while (true) {
  267. switch (reading) {
  268. case READING_INT: {
  269. if (c >= '0' && c <= '9') {
  270. //pass
  271. } else if (c == '.') {
  272. reading = READING_DEC;
  273. is_float = true;
  274. } else if (c == 'e') {
  275. reading = READING_EXP;
  276. } else {
  277. reading = READING_DONE;
  278. }
  279. } break;
  280. case READING_DEC: {
  281. if (c >= '0' && c <= '9') {
  282. } else if (c == 'e') {
  283. reading = READING_EXP;
  284. } else {
  285. reading = READING_DONE;
  286. }
  287. } break;
  288. case READING_EXP: {
  289. if (c >= '0' && c <= '9') {
  290. exp_beg = true;
  291. } else if ((c == '-' || c == '+') && !exp_sign && !exp_beg) {
  292. if (c == '-')
  293. is_float = true;
  294. exp_sign = true;
  295. } else {
  296. reading = READING_DONE;
  297. }
  298. } break;
  299. }
  300. if (reading == READING_DONE)
  301. break;
  302. num += String::chr(c);
  303. c = p_stream->get_char();
  304. }
  305. p_stream->saved = c;
  306. r_token.type = TK_NUMBER;
  307. if (is_float)
  308. r_token.value = num.to_double();
  309. else
  310. r_token.value = num.to_int();
  311. return OK;
  312. } else if ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_') {
  313. String id;
  314. bool first = true;
  315. while ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_' || (!first && cchar >= '0' && cchar <= '9')) {
  316. id += String::chr(cchar);
  317. cchar = p_stream->get_char();
  318. first = false;
  319. }
  320. p_stream->saved = cchar;
  321. r_token.type = TK_IDENTIFIER;
  322. r_token.value = id;
  323. return OK;
  324. } else {
  325. r_err_str = "Unexpected character.";
  326. r_token.type = TK_ERROR;
  327. return ERR_PARSE_ERROR;
  328. }
  329. }
  330. }
  331. }
  332. r_token.type = TK_ERROR;
  333. return ERR_PARSE_ERROR;
  334. }
  335. Error VariantParser::_parse_enginecfg(Stream *p_stream, Vector<String> &strings, int &line, String &r_err_str) {
  336. Token token;
  337. get_token(p_stream, token, line, r_err_str);
  338. if (token.type != TK_PARENTHESIS_OPEN) {
  339. r_err_str = "Expected '(' in old-style project.godot construct";
  340. return ERR_PARSE_ERROR;
  341. }
  342. String accum;
  343. while (true) {
  344. CharType c = p_stream->get_char();
  345. if (p_stream->is_eof()) {
  346. r_err_str = "Unexpected EOF while parsing old-style project.godot construct";
  347. return ERR_PARSE_ERROR;
  348. }
  349. if (c == ',') {
  350. strings.push_back(accum.strip_edges());
  351. accum = String();
  352. } else if (c == ')') {
  353. strings.push_back(accum.strip_edges());
  354. return OK;
  355. } else if (c == '\n') {
  356. line++;
  357. }
  358. }
  359. return OK;
  360. }
  361. template <class T>
  362. Error VariantParser::_parse_construct(Stream *p_stream, Vector<T> &r_construct, int &line, String &r_err_str) {
  363. Token token;
  364. get_token(p_stream, token, line, r_err_str);
  365. if (token.type != TK_PARENTHESIS_OPEN) {
  366. r_err_str = "Expected '(' in constructor";
  367. return ERR_PARSE_ERROR;
  368. }
  369. bool first = true;
  370. while (true) {
  371. if (!first) {
  372. get_token(p_stream, token, line, r_err_str);
  373. if (token.type == TK_COMMA) {
  374. //do none
  375. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  376. break;
  377. } else {
  378. r_err_str = "Expected ',' or ')' in constructor";
  379. return ERR_PARSE_ERROR;
  380. }
  381. }
  382. get_token(p_stream, token, line, r_err_str);
  383. if (first && token.type == TK_PARENTHESIS_CLOSE) {
  384. break;
  385. } else if (token.type != TK_NUMBER) {
  386. r_err_str = "Expected float in constructor";
  387. return ERR_PARSE_ERROR;
  388. }
  389. r_construct.push_back(token.value);
  390. first = false;
  391. }
  392. return OK;
  393. }
  394. Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  395. /* {
  396. Error err = get_token(p_stream,token,line,r_err_str);
  397. if (err)
  398. return err;
  399. }*/
  400. if (token.type == TK_CURLY_BRACKET_OPEN) {
  401. Dictionary d;
  402. Error err = _parse_dictionary(d, p_stream, line, r_err_str, p_res_parser);
  403. if (err)
  404. return err;
  405. value = d;
  406. return OK;
  407. } else if (token.type == TK_BRACKET_OPEN) {
  408. Array a;
  409. Error err = _parse_array(a, p_stream, line, r_err_str, p_res_parser);
  410. if (err)
  411. return err;
  412. value = a;
  413. return OK;
  414. } else if (token.type == TK_IDENTIFIER) {
  415. String id = token.value;
  416. if (id == "true")
  417. value = true;
  418. else if (id == "false")
  419. value = false;
  420. else if (id == "null" || id == "nil")
  421. value = Variant();
  422. else if (id == "Vector2") {
  423. Vector<float> args;
  424. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  425. if (err)
  426. return err;
  427. if (args.size() != 2) {
  428. r_err_str = "Expected 2 arguments for constructor";
  429. }
  430. value = Vector2(args[0], args[1]);
  431. return OK;
  432. } else if (id == "Rect2") {
  433. Vector<float> args;
  434. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  435. if (err)
  436. return err;
  437. if (args.size() != 4) {
  438. r_err_str = "Expected 4 arguments for constructor";
  439. }
  440. value = Rect2(args[0], args[1], args[2], args[3]);
  441. return OK;
  442. } else if (id == "Vector3") {
  443. Vector<float> args;
  444. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  445. if (err)
  446. return err;
  447. if (args.size() != 3) {
  448. r_err_str = "Expected 3 arguments for constructor";
  449. }
  450. value = Vector3(args[0], args[1], args[2]);
  451. return OK;
  452. } else if (id == "Transform2D" || id == "Matrix32") { //compatibility
  453. Vector<float> args;
  454. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  455. if (err)
  456. return err;
  457. if (args.size() != 6) {
  458. r_err_str = "Expected 6 arguments for constructor";
  459. }
  460. Transform2D m;
  461. m[0] = Vector2(args[0], args[1]);
  462. m[1] = Vector2(args[2], args[3]);
  463. m[2] = Vector2(args[4], args[5]);
  464. value = m;
  465. return OK;
  466. } else if (id == "Plane") {
  467. Vector<float> args;
  468. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  469. if (err)
  470. return err;
  471. if (args.size() != 4) {
  472. r_err_str = "Expected 4 arguments for constructor";
  473. }
  474. value = Plane(args[0], args[1], args[2], args[3]);
  475. return OK;
  476. } else if (id == "Quat") {
  477. Vector<float> args;
  478. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  479. if (err)
  480. return err;
  481. if (args.size() != 4) {
  482. r_err_str = "Expected 4 arguments for constructor";
  483. }
  484. value = Quat(args[0], args[1], args[2], args[3]);
  485. return OK;
  486. } else if (id == "Rect3" || id == "AABB") {
  487. Vector<float> args;
  488. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  489. if (err)
  490. return err;
  491. if (args.size() != 6) {
  492. r_err_str = "Expected 6 arguments for constructor";
  493. }
  494. value = Rect3(Vector3(args[0], args[1], args[2]), Vector3(args[3], args[4], args[5]));
  495. return OK;
  496. } else if (id == "Basis" || id == "Matrix3") { //compatibility
  497. Vector<float> args;
  498. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  499. if (err)
  500. return err;
  501. if (args.size() != 9) {
  502. r_err_str = "Expected 9 arguments for constructor";
  503. }
  504. value = Basis(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
  505. return OK;
  506. } else if (id == "Transform") {
  507. Vector<float> args;
  508. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  509. if (err)
  510. return err;
  511. if (args.size() != 12) {
  512. r_err_str = "Expected 12 arguments for constructor";
  513. }
  514. value = Transform(Basis(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]), Vector3(args[9], args[10], args[11]));
  515. return OK;
  516. } else if (id == "Color") {
  517. Vector<float> args;
  518. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  519. if (err)
  520. return err;
  521. if (args.size() != 4) {
  522. r_err_str = "Expected 4 arguments for constructor";
  523. }
  524. value = Color(args[0], args[1], args[2], args[3]);
  525. return OK;
  526. } else if (id == "NodePath") {
  527. get_token(p_stream, token, line, r_err_str);
  528. if (token.type != TK_PARENTHESIS_OPEN) {
  529. r_err_str = "Expected '('";
  530. return ERR_PARSE_ERROR;
  531. }
  532. get_token(p_stream, token, line, r_err_str);
  533. if (token.type != TK_STRING) {
  534. r_err_str = "Expected string as argument for NodePath()";
  535. return ERR_PARSE_ERROR;
  536. }
  537. value = NodePath(String(token.value));
  538. get_token(p_stream, token, line, r_err_str);
  539. if (token.type != TK_PARENTHESIS_CLOSE) {
  540. r_err_str = "Expected ')'";
  541. return ERR_PARSE_ERROR;
  542. }
  543. } else if (id == "RID") {
  544. get_token(p_stream, token, line, r_err_str);
  545. if (token.type != TK_PARENTHESIS_OPEN) {
  546. r_err_str = "Expected '('";
  547. return ERR_PARSE_ERROR;
  548. }
  549. get_token(p_stream, token, line, r_err_str);
  550. if (token.type != TK_NUMBER) {
  551. r_err_str = "Expected number as argument";
  552. return ERR_PARSE_ERROR;
  553. }
  554. value = token.value;
  555. get_token(p_stream, token, line, r_err_str);
  556. if (token.type != TK_PARENTHESIS_CLOSE) {
  557. r_err_str = "Expected ')'";
  558. return ERR_PARSE_ERROR;
  559. }
  560. return OK;
  561. } else if (id == "Object") {
  562. get_token(p_stream, token, line, r_err_str);
  563. if (token.type != TK_PARENTHESIS_OPEN) {
  564. r_err_str = "Expected '('";
  565. return ERR_PARSE_ERROR;
  566. }
  567. get_token(p_stream, token, line, r_err_str);
  568. if (token.type != TK_IDENTIFIER) {
  569. r_err_str = "Expected identifier with type of object";
  570. return ERR_PARSE_ERROR;
  571. }
  572. String type = token.value;
  573. Object *obj = ClassDB::instance(type);
  574. if (!obj) {
  575. r_err_str = "Can't instance Object() of type: " + type;
  576. return ERR_PARSE_ERROR;
  577. }
  578. get_token(p_stream, token, line, r_err_str);
  579. if (token.type != TK_COMMA) {
  580. r_err_str = "Expected ',' after object type";
  581. return ERR_PARSE_ERROR;
  582. }
  583. bool at_key = true;
  584. String key;
  585. Token token;
  586. bool need_comma = false;
  587. while (true) {
  588. if (p_stream->is_eof()) {
  589. r_err_str = "Unexpected End of File while parsing Object()";
  590. return ERR_FILE_CORRUPT;
  591. }
  592. if (at_key) {
  593. Error err = get_token(p_stream, token, line, r_err_str);
  594. if (err != OK)
  595. return err;
  596. if (token.type == TK_PARENTHESIS_CLOSE) {
  597. return OK;
  598. }
  599. if (need_comma) {
  600. if (token.type != TK_COMMA) {
  601. r_err_str = "Expected '}' or ','";
  602. return ERR_PARSE_ERROR;
  603. } else {
  604. need_comma = false;
  605. continue;
  606. }
  607. }
  608. get_token(p_stream, token, line, r_err_str);
  609. if (token.type != TK_STRING) {
  610. r_err_str = "Expected property name as string";
  611. return ERR_PARSE_ERROR;
  612. }
  613. key = token.value;
  614. err = get_token(p_stream, token, line, r_err_str);
  615. if (err != OK)
  616. return err;
  617. if (token.type != TK_COLON) {
  618. r_err_str = "Expected ':'";
  619. return ERR_PARSE_ERROR;
  620. }
  621. at_key = false;
  622. } else {
  623. Error err = get_token(p_stream, token, line, r_err_str);
  624. if (err != OK)
  625. return err;
  626. Variant v;
  627. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  628. if (err)
  629. return err;
  630. obj->set(key, v);
  631. need_comma = true;
  632. at_key = true;
  633. }
  634. }
  635. return OK;
  636. } else if (id == "Resource" || id == "SubResource" || id == "ExtResource") {
  637. get_token(p_stream, token, line, r_err_str);
  638. if (token.type != TK_PARENTHESIS_OPEN) {
  639. r_err_str = "Expected '('";
  640. return ERR_PARSE_ERROR;
  641. }
  642. if (p_res_parser && id == "Resource" && p_res_parser->func) {
  643. RES res;
  644. Error err = p_res_parser->func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  645. if (err)
  646. return err;
  647. value = res;
  648. return OK;
  649. } else if (p_res_parser && id == "ExtResource" && p_res_parser->ext_func) {
  650. RES res;
  651. Error err = p_res_parser->ext_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  652. if (err)
  653. return err;
  654. value = res;
  655. return OK;
  656. } else if (p_res_parser && id == "SubResource" && p_res_parser->sub_func) {
  657. RES res;
  658. Error err = p_res_parser->sub_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  659. if (err)
  660. return err;
  661. value = res;
  662. return OK;
  663. } else {
  664. get_token(p_stream, token, line, r_err_str);
  665. if (token.type == TK_STRING) {
  666. String path = token.value;
  667. RES res = ResourceLoader::load(path);
  668. if (res.is_null()) {
  669. r_err_str = "Can't load resource at path: '" + path + "'.";
  670. return ERR_PARSE_ERROR;
  671. }
  672. get_token(p_stream, token, line, r_err_str);
  673. if (token.type != TK_PARENTHESIS_CLOSE) {
  674. r_err_str = "Expected ')'";
  675. return ERR_PARSE_ERROR;
  676. }
  677. value = res;
  678. return OK;
  679. } else {
  680. r_err_str = "Expected string as argument for Resource().";
  681. return ERR_PARSE_ERROR;
  682. }
  683. }
  684. return OK;
  685. #ifndef DISABLE_DEPRECATED
  686. } else if (id == "InputEvent") {
  687. get_token(p_stream, token, line, r_err_str);
  688. if (token.type != TK_PARENTHESIS_OPEN) {
  689. r_err_str = "Expected '('";
  690. return ERR_PARSE_ERROR;
  691. }
  692. get_token(p_stream, token, line, r_err_str);
  693. if (token.type != TK_IDENTIFIER) {
  694. r_err_str = "Expected identifier";
  695. return ERR_PARSE_ERROR;
  696. }
  697. String id = token.value;
  698. Ref<InputEvent> ie;
  699. if (id == "NONE") {
  700. get_token(p_stream, token, line, r_err_str);
  701. if (token.type != TK_PARENTHESIS_CLOSE) {
  702. r_err_str = "Expected ')'";
  703. return ERR_PARSE_ERROR;
  704. }
  705. } else if (id == "KEY") {
  706. Ref<InputEventKey> key;
  707. key.instance();
  708. ie = key;
  709. get_token(p_stream, token, line, r_err_str);
  710. if (token.type != TK_COMMA) {
  711. r_err_str = "Expected ','";
  712. return ERR_PARSE_ERROR;
  713. }
  714. get_token(p_stream, token, line, r_err_str);
  715. if (token.type == TK_IDENTIFIER) {
  716. String name = token.value;
  717. key->set_scancode(find_keycode(name));
  718. } else if (token.type == TK_NUMBER) {
  719. key->set_scancode(token.value);
  720. } else {
  721. r_err_str = "Expected string or integer for keycode";
  722. return ERR_PARSE_ERROR;
  723. }
  724. get_token(p_stream, token, line, r_err_str);
  725. if (token.type == TK_COMMA) {
  726. get_token(p_stream, token, line, r_err_str);
  727. if (token.type != TK_IDENTIFIER) {
  728. r_err_str = "Expected identifier with modifier flas";
  729. return ERR_PARSE_ERROR;
  730. }
  731. String mods = token.value;
  732. if (mods.findn("C") != -1)
  733. key->set_control(true);
  734. if (mods.findn("A") != -1)
  735. key->set_alt(true);
  736. if (mods.findn("S") != -1)
  737. key->set_shift(true);
  738. if (mods.findn("M") != -1)
  739. key->set_metakey(true);
  740. get_token(p_stream, token, line, r_err_str);
  741. if (token.type != TK_PARENTHESIS_CLOSE) {
  742. r_err_str = "Expected ')'";
  743. return ERR_PARSE_ERROR;
  744. }
  745. } else if (token.type != TK_PARENTHESIS_CLOSE) {
  746. r_err_str = "Expected ')' or modifier flags.";
  747. return ERR_PARSE_ERROR;
  748. }
  749. } else if (id == "MBUTTON") {
  750. Ref<InputEventMouseButton> mb;
  751. mb.instance();
  752. ie = mb;
  753. get_token(p_stream, token, line, r_err_str);
  754. if (token.type != TK_COMMA) {
  755. r_err_str = "Expected ','";
  756. return ERR_PARSE_ERROR;
  757. }
  758. get_token(p_stream, token, line, r_err_str);
  759. if (token.type != TK_NUMBER) {
  760. r_err_str = "Expected button index";
  761. return ERR_PARSE_ERROR;
  762. }
  763. mb->set_button_index(token.value);
  764. get_token(p_stream, token, line, r_err_str);
  765. if (token.type != TK_PARENTHESIS_CLOSE) {
  766. r_err_str = "Expected ')'";
  767. return ERR_PARSE_ERROR;
  768. }
  769. } else if (id == "JBUTTON") {
  770. Ref<InputEventJoypadButton> jb;
  771. jb.instance();
  772. ie = jb;
  773. get_token(p_stream, token, line, r_err_str);
  774. if (token.type != TK_COMMA) {
  775. r_err_str = "Expected ','";
  776. return ERR_PARSE_ERROR;
  777. }
  778. get_token(p_stream, token, line, r_err_str);
  779. if (token.type != TK_NUMBER) {
  780. r_err_str = "Expected button index";
  781. return ERR_PARSE_ERROR;
  782. }
  783. jb->set_button_index(token.value);
  784. get_token(p_stream, token, line, r_err_str);
  785. if (token.type != TK_PARENTHESIS_CLOSE) {
  786. r_err_str = "Expected ')'";
  787. return ERR_PARSE_ERROR;
  788. }
  789. } else if (id == "JAXIS") {
  790. Ref<InputEventJoypadMotion> jm;
  791. jm.instance();
  792. ie = jm;
  793. get_token(p_stream, token, line, r_err_str);
  794. if (token.type != TK_COMMA) {
  795. r_err_str = "Expected ','";
  796. return ERR_PARSE_ERROR;
  797. }
  798. get_token(p_stream, token, line, r_err_str);
  799. if (token.type != TK_NUMBER) {
  800. r_err_str = "Expected axis index";
  801. return ERR_PARSE_ERROR;
  802. }
  803. jm->set_axis(token.value);
  804. get_token(p_stream, token, line, r_err_str);
  805. if (token.type != TK_COMMA) {
  806. r_err_str = "Expected ',' after axis index";
  807. return ERR_PARSE_ERROR;
  808. }
  809. get_token(p_stream, token, line, r_err_str);
  810. if (token.type != TK_NUMBER) {
  811. r_err_str = "Expected axis sign";
  812. return ERR_PARSE_ERROR;
  813. }
  814. jm->set_axis_value(token.value);
  815. get_token(p_stream, token, line, r_err_str);
  816. if (token.type != TK_PARENTHESIS_CLOSE) {
  817. r_err_str = "Expected ')' for jaxis";
  818. return ERR_PARSE_ERROR;
  819. }
  820. } else {
  821. r_err_str = "Invalid input event type.";
  822. return ERR_PARSE_ERROR;
  823. }
  824. value = ie;
  825. return OK;
  826. #endif
  827. } else if (id == "PoolByteArray" || id == "ByteArray") {
  828. Vector<uint8_t> args;
  829. Error err = _parse_construct<uint8_t>(p_stream, args, line, r_err_str);
  830. if (err)
  831. return err;
  832. PoolVector<uint8_t> arr;
  833. {
  834. int len = args.size();
  835. arr.resize(len);
  836. PoolVector<uint8_t>::Write w = arr.write();
  837. for (int i = 0; i < len; i++) {
  838. w[i] = args[i];
  839. }
  840. }
  841. value = arr;
  842. return OK;
  843. } else if (id == "PoolIntArray" || id == "IntArray") {
  844. Vector<int> args;
  845. Error err = _parse_construct<int>(p_stream, args, line, r_err_str);
  846. if (err)
  847. return err;
  848. PoolVector<int> arr;
  849. {
  850. int len = args.size();
  851. arr.resize(len);
  852. PoolVector<int>::Write w = arr.write();
  853. for (int i = 0; i < len; i++) {
  854. w[i] = int(args[i]);
  855. }
  856. }
  857. value = arr;
  858. return OK;
  859. } else if (id == "PoolFloatArray" || id == "FloatArray") {
  860. Vector<float> args;
  861. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  862. if (err)
  863. return err;
  864. PoolVector<float> arr;
  865. {
  866. int len = args.size();
  867. arr.resize(len);
  868. PoolVector<float>::Write w = arr.write();
  869. for (int i = 0; i < len; i++) {
  870. w[i] = args[i];
  871. }
  872. }
  873. value = arr;
  874. return OK;
  875. } else if (id == "PoolStringArray" || id == "StringArray") {
  876. get_token(p_stream, token, line, r_err_str);
  877. if (token.type != TK_PARENTHESIS_OPEN) {
  878. r_err_str = "Expected '('";
  879. return ERR_PARSE_ERROR;
  880. }
  881. Vector<String> cs;
  882. bool first = true;
  883. while (true) {
  884. if (!first) {
  885. get_token(p_stream, token, line, r_err_str);
  886. if (token.type == TK_COMMA) {
  887. //do none
  888. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  889. break;
  890. } else {
  891. r_err_str = "Expected ',' or ')'";
  892. return ERR_PARSE_ERROR;
  893. }
  894. }
  895. get_token(p_stream, token, line, r_err_str);
  896. if (token.type == TK_PARENTHESIS_CLOSE) {
  897. break;
  898. } else if (token.type != TK_STRING) {
  899. r_err_str = "Expected string";
  900. return ERR_PARSE_ERROR;
  901. }
  902. first = false;
  903. cs.push_back(token.value);
  904. }
  905. PoolVector<String> arr;
  906. {
  907. int len = cs.size();
  908. arr.resize(len);
  909. PoolVector<String>::Write w = arr.write();
  910. for (int i = 0; i < len; i++) {
  911. w[i] = cs[i];
  912. }
  913. }
  914. value = arr;
  915. return OK;
  916. } else if (id == "PoolVector2Array" || id == "Vector2Array") {
  917. Vector<float> args;
  918. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  919. if (err)
  920. return err;
  921. PoolVector<Vector2> arr;
  922. {
  923. int len = args.size() / 2;
  924. arr.resize(len);
  925. PoolVector<Vector2>::Write w = arr.write();
  926. for (int i = 0; i < len; i++) {
  927. w[i] = Vector2(args[i * 2 + 0], args[i * 2 + 1]);
  928. }
  929. }
  930. value = arr;
  931. return OK;
  932. } else if (id == "PoolVector3Array" || id == "Vector3Array") {
  933. Vector<float> args;
  934. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  935. if (err)
  936. return err;
  937. PoolVector<Vector3> arr;
  938. {
  939. int len = args.size() / 3;
  940. arr.resize(len);
  941. PoolVector<Vector3>::Write w = arr.write();
  942. for (int i = 0; i < len; i++) {
  943. w[i] = Vector3(args[i * 3 + 0], args[i * 3 + 1], args[i * 3 + 2]);
  944. }
  945. }
  946. value = arr;
  947. return OK;
  948. } else if (id == "PoolColorArray" || id == "ColorArray") {
  949. Vector<float> args;
  950. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  951. if (err)
  952. return err;
  953. PoolVector<Color> arr;
  954. {
  955. int len = args.size() / 4;
  956. arr.resize(len);
  957. PoolVector<Color>::Write w = arr.write();
  958. for (int i = 0; i < len; i++) {
  959. w[i] = Color(args[i * 4 + 0], args[i * 4 + 1], args[i * 4 + 2], args[i * 4 + 3]);
  960. }
  961. }
  962. value = arr;
  963. return OK;
  964. } else {
  965. r_err_str = "Unexpected identifier: '" + id + "'.";
  966. return ERR_PARSE_ERROR;
  967. }
  968. return OK;
  969. } else if (token.type == TK_NUMBER) {
  970. value = token.value;
  971. return OK;
  972. } else if (token.type == TK_STRING) {
  973. value = token.value;
  974. return OK;
  975. } else if (token.type == TK_COLOR) {
  976. value = token.value;
  977. return OK;
  978. } else {
  979. r_err_str = "Expected value, got " + String(tk_name[token.type]) + ".";
  980. return ERR_PARSE_ERROR;
  981. }
  982. return ERR_PARSE_ERROR;
  983. }
  984. Error VariantParser::_parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  985. Token token;
  986. bool need_comma = false;
  987. while (true) {
  988. if (p_stream->is_eof()) {
  989. r_err_str = "Unexpected End of File while parsing array";
  990. return ERR_FILE_CORRUPT;
  991. }
  992. Error err = get_token(p_stream, token, line, r_err_str);
  993. if (err != OK)
  994. return err;
  995. if (token.type == TK_BRACKET_CLOSE) {
  996. return OK;
  997. }
  998. if (need_comma) {
  999. if (token.type != TK_COMMA) {
  1000. r_err_str = "Expected ','";
  1001. return ERR_PARSE_ERROR;
  1002. } else {
  1003. need_comma = false;
  1004. continue;
  1005. }
  1006. }
  1007. Variant v;
  1008. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1009. if (err)
  1010. return err;
  1011. array.push_back(v);
  1012. need_comma = true;
  1013. }
  1014. return OK;
  1015. }
  1016. Error VariantParser::_parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  1017. bool at_key = true;
  1018. Variant key;
  1019. Token token;
  1020. bool need_comma = false;
  1021. while (true) {
  1022. if (p_stream->is_eof()) {
  1023. r_err_str = "Unexpected End of File while parsing dictionary";
  1024. return ERR_FILE_CORRUPT;
  1025. }
  1026. if (at_key) {
  1027. Error err = get_token(p_stream, token, line, r_err_str);
  1028. if (err != OK)
  1029. return err;
  1030. if (token.type == TK_CURLY_BRACKET_CLOSE) {
  1031. return OK;
  1032. }
  1033. if (need_comma) {
  1034. if (token.type != TK_COMMA) {
  1035. r_err_str = "Expected '}' or ','";
  1036. return ERR_PARSE_ERROR;
  1037. } else {
  1038. need_comma = false;
  1039. continue;
  1040. }
  1041. }
  1042. err = parse_value(token, key, p_stream, line, r_err_str, p_res_parser);
  1043. if (err)
  1044. return err;
  1045. err = get_token(p_stream, token, line, r_err_str);
  1046. if (err != OK)
  1047. return err;
  1048. if (token.type != TK_COLON) {
  1049. r_err_str = "Expected ':'";
  1050. return ERR_PARSE_ERROR;
  1051. }
  1052. at_key = false;
  1053. } else {
  1054. Error err = get_token(p_stream, token, line, r_err_str);
  1055. if (err != OK)
  1056. return err;
  1057. Variant v;
  1058. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1059. if (err)
  1060. return err;
  1061. object[key] = v;
  1062. need_comma = true;
  1063. at_key = true;
  1064. }
  1065. }
  1066. return OK;
  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.Anroid] 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.Anroid] 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. r_assign = what;
  1195. Token token;
  1196. get_token(p_stream, token, line, r_err_str);
  1197. Error err = parse_value(token, r_value, p_stream, line, r_err_str, p_res_parser);
  1198. if (err) {
  1199. }
  1200. return err;
  1201. }
  1202. } else if (c == '\n') {
  1203. line++;
  1204. }
  1205. }
  1206. return OK;
  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 int()));
  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::RECT3: {
  1266. Rect3 aabb = p_variant;
  1267. p_store_string_func(p_store_string_ud, "Rect3( " + rtosfix(aabb.pos.x) + ", " + rtosfix(aabb.pos.y) + ", " + rtosfix(aabb.pos.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, "PoolFloatArray( ");
  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. return OK;
  1494. }
  1495. static Error _write_to_str(void *ud, const String &p_string) {
  1496. String *str = (String *)ud;
  1497. (*str) += p_string;
  1498. return OK;
  1499. }
  1500. Error VariantWriter::write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud) {
  1501. r_string = String();
  1502. return write(p_variant, _write_to_str, &r_string, p_encode_res_func, p_encode_res_ud);
  1503. }