2
0

variant_parser.cpp 46 KB

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