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