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