variant_parser.cpp 49 KB

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