variant_parser.cpp 46 KB

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