5433
|
1 |
package org.hedgewars.mobile;
|
|
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.IOException;
|
|
8 |
import java.util.ArrayList;
|
|
9 |
import java.util.Arrays;
|
|
10 |
|
|
11 |
import org.xmlpull.v1.XmlPullParser;
|
|
12 |
import org.xmlpull.v1.XmlPullParserException;
|
|
13 |
import org.xmlpull.v1.XmlPullParserFactory;
|
|
14 |
|
|
15 |
import android.content.Context;
|
|
16 |
|
|
17 |
public class Weapon {
|
|
18 |
|
|
19 |
public static final String DIRECTORY_WEAPON = "weapons";
|
|
20 |
|
|
21 |
private String name;
|
|
22 |
private String QT;
|
|
23 |
private String prob;
|
|
24 |
private String delay;
|
|
25 |
private String crate;
|
|
26 |
|
|
27 |
public Weapon(String _name, String _QT, String _prob, String _delay, String _crate){
|
|
28 |
name = _name;
|
|
29 |
QT = _QT;
|
|
30 |
prob = _prob;
|
|
31 |
delay = _delay;
|
|
32 |
crate = _crate;
|
|
33 |
}
|
|
34 |
|
|
35 |
public String toString(){
|
|
36 |
return name;
|
|
37 |
}
|
|
38 |
|
|
39 |
public static final int STATE_START = 0;
|
|
40 |
public static final int STATE_ROOT = 1;
|
|
41 |
public static final int STATE_NAME = 2;
|
|
42 |
public static final int STATE_QT = 3;
|
|
43 |
public static final int STATE_PROBABILITY = 4;
|
|
44 |
public static final int STATE_DELAY = 5;
|
|
45 |
public static final int STATE_CRATE = 6;
|
|
46 |
|
|
47 |
public static Weapon[] getWeapons(Context c) throws IllegalArgumentException{
|
|
48 |
String dir = c.getFilesDir().getAbsolutePath() + '/' + DIRECTORY_WEAPON + '/';
|
|
49 |
String[] files = new File(dir).list();
|
|
50 |
if(files == null) files = new String[]{};
|
|
51 |
Arrays.sort(files);
|
|
52 |
|
|
53 |
ArrayList<Weapon> weapons = new ArrayList<Weapon>();
|
|
54 |
|
|
55 |
try {
|
|
56 |
XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance();
|
|
57 |
XmlPullParser xmlPuller = xmlPullFactory.newPullParser();
|
|
58 |
|
|
59 |
for(String file : files){
|
|
60 |
BufferedReader br = new BufferedReader(new FileReader(dir + file), 1024);
|
|
61 |
xmlPuller.setInput(br);
|
|
62 |
String name = null;
|
|
63 |
String qt = null;
|
|
64 |
String prob = null;
|
|
65 |
String delay = null;
|
|
66 |
String crate = null;
|
|
67 |
|
|
68 |
int eventType = xmlPuller.getEventType();
|
|
69 |
int state = STATE_START;
|
|
70 |
while(eventType != XmlPullParser.END_DOCUMENT){
|
|
71 |
switch(state){
|
|
72 |
case STATE_START:
|
|
73 |
if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("weapon")) state = STATE_ROOT;
|
|
74 |
else if(eventType != XmlPullParser.START_DOCUMENT) throwException(file, eventType);
|
|
75 |
break;
|
|
76 |
case STATE_ROOT:
|
|
77 |
if(eventType == XmlPullParser.START_TAG){
|
|
78 |
if(xmlPuller.getName().toLowerCase().equals("qt")) state = STATE_QT;
|
|
79 |
else if(xmlPuller.getName().toLowerCase().equals("name")) state = STATE_NAME;
|
|
80 |
else if(xmlPuller.getName().toLowerCase().equals("probability")) state = STATE_PROBABILITY;
|
|
81 |
else if(xmlPuller.getName().toLowerCase().equals("delay")) state = STATE_DELAY;
|
|
82 |
else if(xmlPuller.getName().toLowerCase().equals("crate")) state = STATE_CRATE;
|
|
83 |
else throwException(file, eventType);
|
|
84 |
}else if(eventType == XmlPullParser.END_TAG) state = STATE_START;
|
|
85 |
else throwException(xmlPuller.getText(), eventType);
|
|
86 |
break;
|
|
87 |
case STATE_NAME:
|
|
88 |
if(eventType == XmlPullParser.TEXT) name = xmlPuller.getText().trim();
|
|
89 |
else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
|
|
90 |
else throwException(file, eventType);
|
|
91 |
break;
|
|
92 |
case STATE_QT:
|
|
93 |
if(eventType == XmlPullParser.TEXT) qt = xmlPuller.getText().trim();
|
|
94 |
else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
|
|
95 |
else throwException(file, eventType);
|
|
96 |
break;
|
|
97 |
case STATE_PROBABILITY:
|
|
98 |
if(eventType == XmlPullParser.TEXT) prob = xmlPuller.getText().trim();
|
|
99 |
else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
|
|
100 |
else throwException(file, eventType);
|
|
101 |
break;
|
|
102 |
case STATE_DELAY:
|
|
103 |
if(eventType == XmlPullParser.TEXT) delay = xmlPuller.getText().trim();
|
|
104 |
else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
|
|
105 |
else throwException(file, eventType);
|
|
106 |
break;
|
|
107 |
case STATE_CRATE:
|
|
108 |
if(eventType == XmlPullParser.TEXT) crate = xmlPuller.getText().trim();
|
|
109 |
else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
|
|
110 |
else throwException(file, eventType);
|
|
111 |
break;
|
|
112 |
}
|
|
113 |
eventType = xmlPuller.next();
|
|
114 |
while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){//Skip whitespaces
|
|
115 |
eventType = xmlPuller.next();
|
|
116 |
}
|
|
117 |
}//end while(eventtype != END_DOCUMENT
|
|
118 |
weapons.add(new Weapon(name, qt, prob, delay, crate));
|
|
119 |
}//end for(string file : files
|
|
120 |
Weapon[] ret = new Weapon[weapons.size()];
|
|
121 |
weapons.toArray(ret);
|
|
122 |
return ret;
|
|
123 |
|
|
124 |
} catch (XmlPullParserException e) {
|
|
125 |
e.printStackTrace();
|
|
126 |
} catch (FileNotFoundException e) {
|
|
127 |
e.printStackTrace();
|
|
128 |
} catch (IOException e) {
|
|
129 |
e.printStackTrace();
|
|
130 |
}
|
|
131 |
return new Weapon[]{};//TODO handle correctly
|
|
132 |
}
|
|
133 |
|
|
134 |
private static void throwException(String file, int eventType){
|
|
135 |
throw new IllegalArgumentException(String.format("Xml file: %s malformed with eventType: %d.", file, eventType));
|
|
136 |
}
|
|
137 |
|
|
138 |
}
|