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; |
|
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; |
|
5463
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
16 |
import android.os.Parcel; |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
17 |
import android.os.Parcelable; |
5433 | 18 |
|
5463
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
19 |
public class Weapon implements Parcelable{ |
5433 | 20 |
|
21 |
public static final String DIRECTORY_WEAPON = "weapons"; |
|
22 |
||
23 |
private String name; |
|
24 |
private String QT; |
|
25 |
private String prob; |
|
26 |
private String delay; |
|
27 |
private String crate; |
|
5463
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
28 |
private static int maxWeapons; |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
29 |
|
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
30 |
static{ |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
31 |
//maxWeapons = PascalExports.HWgetNumberOfWeapons(); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
32 |
} |
5433 | 33 |
|
34 |
public Weapon(String _name, String _QT, String _prob, String _delay, String _crate){ |
|
35 |
name = _name; |
|
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 |
//Incase there's a newer ammoStore which is bigger we append with zeros |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
38 |
StringBuffer sb = new StringBuffer(); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
39 |
while(_QT.length() + sb.length() < maxWeapons){ |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
40 |
sb.append('0'); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
41 |
} |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
42 |
|
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
43 |
QT = String.format("e%s %s%s", "ammloadt", _QT, sb); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
44 |
prob = String.format("e%s %s%s", "ammprob", _prob, sb); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
45 |
delay = String.format("e%s %s%s", "ammdelay", _delay, sb); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
46 |
crate = String.format("e%s %s%s", "ammreinf", _crate, sb); |
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 |
|
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
49 |
public Weapon(Parcel in){ |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
50 |
readFromParcel(in); |
5433 | 51 |
} |
52 |
||
53 |
public String toString(){ |
|
54 |
return name; |
|
55 |
} |
|
56 |
||
5508 | 57 |
public void sendToEngine(EngineProtocolNetwork epn, int teamsCount) throws IOException{ |
58 |
epn.sendToEngine(QT);//command prefix is already in string |
|
59 |
epn.sendToEngine(prob); |
|
60 |
epn.sendToEngine(delay); |
|
61 |
epn.sendToEngine(crate); |
|
5463
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
62 |
|
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
63 |
for(int i = 0; i < teamsCount; i++){ |
5508 | 64 |
epn.sendToEngine("eammstore"); |
5463
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
65 |
} |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
66 |
} |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
67 |
|
5433 | 68 |
public static final int STATE_START = 0; |
69 |
public static final int STATE_ROOT = 1; |
|
70 |
public static final int STATE_NAME = 2; |
|
71 |
public static final int STATE_QT = 3; |
|
72 |
public static final int STATE_PROBABILITY = 4; |
|
73 |
public static final int STATE_DELAY = 5; |
|
74 |
public static final int STATE_CRATE = 6; |
|
75 |
||
5463
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
76 |
public static ArrayList<Weapon> getWeapons(Context c) throws IllegalArgumentException{ |
5433 | 77 |
String dir = c.getFilesDir().getAbsolutePath() + '/' + DIRECTORY_WEAPON + '/'; |
78 |
String[] files = new File(dir).list(); |
|
79 |
if(files == null) files = new String[]{}; |
|
80 |
Arrays.sort(files); |
|
81 |
||
82 |
ArrayList<Weapon> weapons = new ArrayList<Weapon>(); |
|
83 |
||
84 |
try { |
|
85 |
XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance(); |
|
86 |
XmlPullParser xmlPuller = xmlPullFactory.newPullParser(); |
|
87 |
||
88 |
for(String file : files){ |
|
89 |
BufferedReader br = new BufferedReader(new FileReader(dir + file), 1024); |
|
90 |
xmlPuller.setInput(br); |
|
91 |
String name = null; |
|
92 |
String qt = null; |
|
93 |
String prob = null; |
|
94 |
String delay = null; |
|
95 |
String crate = null; |
|
96 |
||
97 |
int eventType = xmlPuller.getEventType(); |
|
98 |
int state = STATE_START; |
|
99 |
while(eventType != XmlPullParser.END_DOCUMENT){ |
|
100 |
switch(state){ |
|
101 |
case STATE_START: |
|
102 |
if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("weapon")) state = STATE_ROOT; |
|
103 |
else if(eventType != XmlPullParser.START_DOCUMENT) throwException(file, eventType); |
|
104 |
break; |
|
105 |
case STATE_ROOT: |
|
106 |
if(eventType == XmlPullParser.START_TAG){ |
|
107 |
if(xmlPuller.getName().toLowerCase().equals("qt")) state = STATE_QT; |
|
108 |
else if(xmlPuller.getName().toLowerCase().equals("name")) state = STATE_NAME; |
|
109 |
else if(xmlPuller.getName().toLowerCase().equals("probability")) state = STATE_PROBABILITY; |
|
110 |
else if(xmlPuller.getName().toLowerCase().equals("delay")) state = STATE_DELAY; |
|
111 |
else if(xmlPuller.getName().toLowerCase().equals("crate")) state = STATE_CRATE; |
|
112 |
else throwException(file, eventType); |
|
113 |
}else if(eventType == XmlPullParser.END_TAG) state = STATE_START; |
|
114 |
else throwException(xmlPuller.getText(), eventType); |
|
115 |
break; |
|
116 |
case STATE_NAME: |
|
117 |
if(eventType == XmlPullParser.TEXT) name = xmlPuller.getText().trim(); |
|
118 |
else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT; |
|
119 |
else throwException(file, eventType); |
|
120 |
break; |
|
121 |
case STATE_QT: |
|
122 |
if(eventType == XmlPullParser.TEXT) qt = xmlPuller.getText().trim(); |
|
123 |
else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT; |
|
124 |
else throwException(file, eventType); |
|
125 |
break; |
|
126 |
case STATE_PROBABILITY: |
|
127 |
if(eventType == XmlPullParser.TEXT) prob = xmlPuller.getText().trim(); |
|
128 |
else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT; |
|
129 |
else throwException(file, eventType); |
|
130 |
break; |
|
131 |
case STATE_DELAY: |
|
132 |
if(eventType == XmlPullParser.TEXT) delay = xmlPuller.getText().trim(); |
|
133 |
else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT; |
|
134 |
else throwException(file, eventType); |
|
135 |
break; |
|
136 |
case STATE_CRATE: |
|
137 |
if(eventType == XmlPullParser.TEXT) crate = xmlPuller.getText().trim(); |
|
138 |
else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT; |
|
139 |
else throwException(file, eventType); |
|
140 |
break; |
|
141 |
} |
|
142 |
eventType = xmlPuller.next(); |
|
143 |
while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){//Skip whitespaces |
|
144 |
eventType = xmlPuller.next(); |
|
145 |
} |
|
146 |
}//end while(eventtype != END_DOCUMENT |
|
147 |
weapons.add(new Weapon(name, qt, prob, delay, crate)); |
|
148 |
}//end for(string file : files |
|
5463
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
149 |
return weapons; |
5433 | 150 |
|
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<Weapon>();//TODO handle correctly |
5433 | 159 |
} |
160 |
||
161 |
private static void throwException(String file, int eventType){ |
|
162 |
throw new IllegalArgumentException(String.format("Xml file: %s malformed with eventType: %d.", file, eventType)); |
|
163 |
} |
|
164 |
||
5463
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
165 |
public int describeContents() { |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
166 |
return 0; |
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 |
|
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
169 |
public void writeToParcel(Parcel dest, int flags) { |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
170 |
dest.writeString(name); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
171 |
dest.writeString(QT); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
172 |
dest.writeString(prob); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
173 |
dest.writeString(delay); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
174 |
dest.writeString(crate); |
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 |
|
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
177 |
private void readFromParcel(Parcel src){ |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
178 |
name = src.readString(); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
179 |
QT = src.readString(); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
180 |
prob = src.readString(); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
181 |
delay = src.readString(); |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
182 |
crate = src.readString(); |
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 |
|
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
185 |
public static final Parcelable.Creator<Weapon> CREATOR = new Parcelable.Creator<Weapon>() { |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
186 |
public Weapon createFromParcel(Parcel source) { |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
187 |
return new Weapon(source); |
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 |
public Weapon[] newArray(int size) { |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
190 |
return new Weapon[size]; |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
191 |
} |
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
192 |
|
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
5433
diff
changeset
|
193 |
}; |
5433 | 194 |
} |