|
1 package org.hedgewars.mobile.EngineProtocol; |
|
2 |
|
3 import java.io.BufferedReader; |
|
4 import java.io.File; |
|
5 import java.io.FileNotFoundException; |
|
6 import java.io.FileReader; |
|
7 import java.io.FilenameFilter; |
|
8 import java.io.IOException; |
|
9 import java.io.OutputStream; |
|
10 import java.util.ArrayList; |
|
11 import java.util.Arrays; |
|
12 import java.util.LinkedHashMap; |
|
13 |
|
14 import org.xmlpull.v1.XmlPullParser; |
|
15 import org.xmlpull.v1.XmlPullParserException; |
|
16 import org.xmlpull.v1.XmlPullParserFactory; |
|
17 |
|
18 import android.content.Context; |
|
19 import android.os.Parcel; |
|
20 import android.os.Parcelable; |
|
21 |
|
22 public class Scheme implements Parcelable{ |
|
23 |
|
24 public static final String DIRECTORY_SCHEME = "schemes"; |
|
25 |
|
26 private String name; |
|
27 //private ArrayList<Integer> basic; |
|
28 private Integer gamemod; |
|
29 private ArrayList<Integer> basic;; |
|
30 private static ArrayList<LinkedHashMap<String, ?>> basicflags = new ArrayList<LinkedHashMap<String, ?>>(); |
|
31 |
|
32 public Scheme(String _name, ArrayList<Integer> _basic, int _gamemod){ |
|
33 name = _name; |
|
34 gamemod = _gamemod; |
|
35 basic = _basic; |
|
36 |
|
37 } |
|
38 |
|
39 public Scheme(Parcel in){ |
|
40 readFromParcel(in); |
|
41 } |
|
42 |
|
43 public void sendToEngine(OutputStream os)throws IOException{ |
|
44 os.write(String.format("e$gmflags %d", gamemod).getBytes()); |
|
45 |
|
46 for(int pos = 0; pos < basic.size(); pos++){ |
|
47 LinkedHashMap<String, ?> basicflag = basicflags.get(pos); |
|
48 |
|
49 String command = (String)basicflag.get("command"); |
|
50 Integer value = basic.get(pos); |
|
51 Boolean checkOverMax = (Boolean) basicflag.get("checkOverMax"); |
|
52 Boolean times1000 = (Boolean) basicflag.get("times1000"); |
|
53 Integer max = (Integer) basicflag.get("max"); |
|
54 |
|
55 if(checkOverMax && value >= max) value = max; |
|
56 if(times1000) value *= 1000; |
|
57 |
|
58 os.write(String.format("%s %d", command, value).getBytes()); |
|
59 } |
|
60 os.flush(); |
|
61 } |
|
62 public String toString(){ |
|
63 return name; |
|
64 } |
|
65 |
|
66 |
|
67 public static final int STATE_START = 0; |
|
68 public static final int STATE_ROOT = 1; |
|
69 public static final int STATE_NAME = 2; |
|
70 public static final int STATE_BASICFLAGS = 3; |
|
71 public static final int STATE_GAMEMOD = 4; |
|
72 public static final int STATE_BASICFLAG_INTEGER = 5; |
|
73 public static final int STATE_GAMEMOD_TRUE = 6; |
|
74 public static final int STATE_GAMEMOD_FALSE = 7; |
|
75 |
|
76 public static ArrayList<Scheme> getSchemes(Context c) throws IllegalArgumentException{ |
|
77 String dir = c.getFilesDir().getAbsolutePath() + '/' + DIRECTORY_SCHEME + '/'; |
|
78 String[] files = new File(dir).list(fnf); |
|
79 if(files == null) files = new String[]{}; |
|
80 Arrays.sort(files); |
|
81 ArrayList<Scheme> schemes = new ArrayList<Scheme>(); |
|
82 |
|
83 try { |
|
84 XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance(); |
|
85 XmlPullParser xmlPuller = xmlPullFactory.newPullParser(); |
|
86 |
|
87 for(String file : files){ |
|
88 BufferedReader br = new BufferedReader(new FileReader(dir + file), 1024); |
|
89 xmlPuller.setInput(br); |
|
90 String name = null; |
|
91 ArrayList<Integer> basic = new ArrayList<Integer>(); |
|
92 Integer gamemod = 0; |
|
93 int mask = 0x000000004; |
|
94 |
|
95 int eventType = xmlPuller.getEventType(); |
|
96 int state = STATE_START; |
|
97 while(eventType != XmlPullParser.END_DOCUMENT){ |
|
98 switch(state){ |
|
99 case STATE_START: |
|
100 if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("scheme")) state = STATE_ROOT; |
|
101 else if(eventType != XmlPullParser.START_DOCUMENT) throwException(file, eventType); |
|
102 break; |
|
103 case STATE_ROOT: |
|
104 if(eventType == XmlPullParser.START_TAG){ |
|
105 if(xmlPuller.getName().equals("basicflags")) state = STATE_BASICFLAGS; |
|
106 else if(xmlPuller.getName().toLowerCase().equals("gamemod")) state = STATE_GAMEMOD; |
|
107 else if(xmlPuller.getName().toLowerCase().equals("name")) state = STATE_NAME; |
|
108 else throwException(file, eventType); |
|
109 }else if(eventType == XmlPullParser.END_TAG) state = STATE_START; |
|
110 else throwException(xmlPuller.getText(), eventType); |
|
111 break; |
|
112 case STATE_BASICFLAGS: |
|
113 if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().toLowerCase().equals("integer")) state = STATE_BASICFLAG_INTEGER; |
|
114 else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT; |
|
115 else throwException(file, eventType); |
|
116 break; |
|
117 case STATE_GAMEMOD: |
|
118 if(eventType == XmlPullParser.START_TAG){ |
|
119 if(xmlPuller.getName().toLowerCase().equals("true")) state = STATE_GAMEMOD_TRUE; |
|
120 else if(xmlPuller.getName().toLowerCase().equals("false")) state = STATE_GAMEMOD_FALSE; |
|
121 else throwException(file, eventType); |
|
122 }else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT; |
|
123 else throwException(file, eventType); |
|
124 break; |
|
125 case STATE_NAME: |
|
126 if(eventType == XmlPullParser.TEXT) name = xmlPuller.getText().trim(); |
|
127 else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT; |
|
128 else throwException(file, eventType); |
|
129 break; |
|
130 case STATE_BASICFLAG_INTEGER: |
|
131 if(eventType == XmlPullParser.TEXT) basic.add(Integer.parseInt(xmlPuller.getText().trim())); |
|
132 else if(eventType == XmlPullParser.END_TAG) state = STATE_BASICFLAGS; |
|
133 else throwException(file, eventType); |
|
134 break; |
|
135 case STATE_GAMEMOD_FALSE: |
|
136 if(eventType == XmlPullParser.TEXT) gamemod <<= 1; |
|
137 else if(eventType == XmlPullParser.END_TAG) state = STATE_GAMEMOD; |
|
138 else throwException(file, eventType); |
|
139 break; |
|
140 case STATE_GAMEMOD_TRUE: |
|
141 if(eventType == XmlPullParser.TEXT){ |
|
142 gamemod |= mask; |
|
143 gamemod <<= 1; |
|
144 }else if(eventType == XmlPullParser.END_TAG) state = STATE_GAMEMOD; |
|
145 else throwException(file, eventType); |
|
146 break; |
|
147 } |
|
148 eventType = getEventType(xmlPuller); |
|
149 }//end while(eventtype != END_DOCUMENT |
|
150 schemes.add(new Scheme(name, basic, gamemod)); |
|
151 }//end for(string file : files |
|
152 return schemes; |
|
153 } catch (XmlPullParserException e) { |
|
154 e.printStackTrace(); |
|
155 } catch (FileNotFoundException e) { |
|
156 e.printStackTrace(); |
|
157 } catch (IOException e) { |
|
158 e.printStackTrace(); |
|
159 } |
|
160 return new ArrayList<Scheme>();//TODO handle correctly |
|
161 } |
|
162 |
|
163 private static FilenameFilter fnf = new FilenameFilter(){ |
|
164 public boolean accept(File dir, String filename) { |
|
165 return filename.toLowerCase().startsWith("scheme_"); |
|
166 } |
|
167 }; |
|
168 |
|
169 /** |
|
170 * This method will parse the basic flags from a prespecified xml file. |
|
171 * I use a raw xml file rather than one parsed by aatp at compile time |
|
172 * to keep it generic with other frontends, ie in the future we could |
|
173 * use one provided by the Data folder. |
|
174 */ |
|
175 public static void parseBasicFlags(Context c){ |
|
176 String filename = String.format("%s/%s/basicflags", c.getFilesDir().getAbsolutePath(), DIRECTORY_SCHEME); |
|
177 |
|
178 XmlPullParser xmlPuller = null; |
|
179 BufferedReader br = null; |
|
180 try { |
|
181 XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance(); |
|
182 xmlPuller = xmlPullFactory.newPullParser(); |
|
183 br = new BufferedReader(new FileReader(filename), 1024); |
|
184 xmlPuller.setInput(br); |
|
185 |
|
186 int eventType = getEventType(xmlPuller); |
|
187 boolean continueParsing = true; |
|
188 do{ |
|
189 switch(eventType){ |
|
190 |
|
191 case XmlPullParser.START_TAG: |
|
192 if(xmlPuller.getName().toLowerCase().equals("flag")){ |
|
193 basicflags.add(parseFlag(xmlPuller)); |
|
194 }else if(xmlPuller.getName().toLowerCase().equals("basicflags")){ |
|
195 eventType = getEventType(xmlPuller); |
|
196 }else{ |
|
197 skipCurrentTag(xmlPuller); |
|
198 eventType = getEventType(xmlPuller); |
|
199 } |
|
200 break; |
|
201 case XmlPullParser.START_DOCUMENT://ignore all tags not being "flag" |
|
202 case XmlPullParser.END_TAG: |
|
203 case XmlPullParser.TEXT: |
|
204 default: |
|
205 continueParsing = true; |
|
206 case XmlPullParser.END_DOCUMENT: |
|
207 continueParsing = false; |
|
208 } |
|
209 }while(continueParsing); |
|
210 |
|
211 }catch(IOException e){ |
|
212 e.printStackTrace(); |
|
213 }catch (XmlPullParserException e) { |
|
214 e.printStackTrace(); |
|
215 }finally{ |
|
216 if(br != null) |
|
217 try { |
|
218 br.close(); |
|
219 } catch (IOException e) {} |
|
220 } |
|
221 |
|
222 } |
|
223 |
|
224 /* |
|
225 * * Parses a Tag structure from xml as example we use |
|
226 *<flag> |
|
227 * <checkOverMax> |
|
228 * <boolean>false</boolean> |
|
229 * </checkOverMax> |
|
230 *</flag> |
|
231 * |
|
232 * It returns a LinkedHashMap with key/value pairs |
|
233 */ |
|
234 private static LinkedHashMap<String, Object> parseFlag(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{ |
|
235 LinkedHashMap<String, Object> hash = new LinkedHashMap<String, Object>(); |
|
236 |
|
237 int eventType = xmlPuller.getEventType();//Get the event type which triggered this method |
|
238 if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().toLowerCase().equals("flag")){//valid start of flag tag |
|
239 String lcKey = null; |
|
240 String lcType = null; |
|
241 String value = null; |
|
242 |
|
243 eventType = getEventType(xmlPuller);//<checkOverMax> |
|
244 while(eventType == XmlPullParser.START_TAG){ |
|
245 lcKey = xmlPuller.getName();//checkOverMax |
|
246 if(getEventType(xmlPuller) == XmlPullParser.START_TAG){//<boolean> |
|
247 lcType = xmlPuller.getName().toLowerCase(); |
|
248 if(getEventType(xmlPuller) == XmlPullParser.TEXT){ |
|
249 value = xmlPuller.getText(); |
|
250 if(getEventType(xmlPuller) == XmlPullParser.END_TAG && //</boolean> |
|
251 getEventType(xmlPuller) == XmlPullParser.END_TAG){//</checkOverMax> |
|
252 if(lcType.equals("boolean")) hash.put(lcKey, new Boolean(value)); |
|
253 else if(lcType.equals("string"))hash.put(lcKey, value); |
|
254 else if(lcType.equals("integer")){ |
|
255 try{ |
|
256 hash.put(lcKey, new Integer(value)); |
|
257 }catch (NumberFormatException e){ |
|
258 throw new XmlPullParserException("Wrong integer value in xml file"); |
|
259 } |
|
260 }else{ |
|
261 throwException("basicflags", eventType); |
|
262 } |
|
263 }//</boolean> / </checkOverMax> |
|
264 }//if TEXT |
|
265 }//if boolean |
|
266 eventType = getEventType(xmlPuller);//start new loop |
|
267 } |
|
268 eventType = getEventType(xmlPuller);//</flag> |
|
269 } |
|
270 |
|
271 return hash; |
|
272 } |
|
273 |
|
274 private static void skipCurrentTag(XmlPullParser xmlPuller) throws XmlPullParserException, IOException{ |
|
275 int eventType = xmlPuller.getEventType(); |
|
276 if(eventType != XmlPullParser.START_TAG)return; |
|
277 String tag = xmlPuller.getName().toLowerCase(); |
|
278 |
|
279 while(true){ |
|
280 eventType = getEventType(xmlPuller);//getNext() |
|
281 switch(eventType){ |
|
282 case XmlPullParser.START_DOCUMENT://we're inside of a start tag so START_ or END_DOCUMENT is just wrong |
|
283 case XmlPullParser.END_DOCUMENT: |
|
284 throw new XmlPullParserException("invalid xml file"); |
|
285 case XmlPullParser.START_TAG://if we get a new tag recursively handle it |
|
286 skipCurrentTag(xmlPuller); |
|
287 break; |
|
288 case XmlPullParser.TEXT: |
|
289 break; |
|
290 case XmlPullParser.END_TAG: |
|
291 if(!xmlPuller.getName().toLowerCase().equals(tag)){//if the end tag doesn't match the start tag |
|
292 throw new XmlPullParserException("invalid xml file"); |
|
293 }else{ |
|
294 return;//skip completed |
|
295 } |
|
296 |
|
297 } |
|
298 } |
|
299 } |
|
300 |
|
301 /** |
|
302 * Skips whitespaces.. |
|
303 */ |
|
304 private static int getEventType(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{ |
|
305 int eventType = xmlPuller.next(); |
|
306 while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){ |
|
307 eventType = xmlPuller.next(); |
|
308 } |
|
309 return eventType; |
|
310 } |
|
311 private static void throwException(String file, int eventType){ |
|
312 throw new IllegalArgumentException(String.format("Xml file: %s malformed with error: %d.", file, eventType)); |
|
313 } |
|
314 |
|
315 @Override |
|
316 public int describeContents() { |
|
317 // TODO Auto-generated method stub |
|
318 return 0; |
|
319 } |
|
320 |
|
321 public void writeToParcel(Parcel dest, int flags) { |
|
322 dest.writeString(name); |
|
323 dest.writeInt(gamemod); |
|
324 dest.writeList(basic); |
|
325 |
|
326 } |
|
327 |
|
328 public void readFromParcel(Parcel src){ |
|
329 name = src.readString(); |
|
330 gamemod = src.readInt(); |
|
331 basic = src.readArrayList(ArrayList.class.getClassLoader()); |
|
332 } |
|
333 |
|
334 public static final Parcelable.Creator<Scheme> CREATOR = new Parcelable.Creator<Scheme>() { |
|
335 public Scheme createFromParcel(Parcel source) { |
|
336 return new Scheme(source); |
|
337 } |
|
338 public Scheme[] newArray(int size) { |
|
339 return new Scheme[size]; |
|
340 } |
|
341 |
|
342 }; |
|
343 } |