variant_parser.cpp 48 KB

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