|
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.IOException; |
|
8 import java.io.OutputStream; |
|
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; |
|
17 import android.os.Parcel; |
|
18 import android.os.Parcelable; |
|
19 |
|
20 public class Weapon implements Parcelable{ |
|
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; |
|
29 private static int maxWeapons; |
|
30 |
|
31 static{ |
|
32 //maxWeapons = PascalExports.HWgetNumberOfWeapons(); |
|
33 } |
|
34 |
|
35 public Weapon(String _name, String _QT, String _prob, String _delay, String _crate){ |
|
36 name = _name; |
|
37 |
|
38 //Incase there's a newer ammoStore which is bigger we append with zeros |
|
39 StringBuffer sb = new StringBuffer(); |
|
40 while(_QT.length() + sb.length() < maxWeapons){ |
|
41 sb.append('0'); |
|
42 } |
|
43 |
|
44 QT = String.format("e%s %s%s", "ammloadt", _QT, sb); |
|
45 prob = String.format("e%s %s%s", "ammprob", _prob, sb); |
|
46 delay = String.format("e%s %s%s", "ammdelay", _delay, sb); |
|
47 crate = String.format("e%s %s%s", "ammreinf", _crate, sb); |
|
48 } |
|
49 |
|
50 public Weapon(Parcel in){ |
|
51 readFromParcel(in); |
|
52 } |
|
53 |
|
54 public String toString(){ |
|
55 return name; |
|
56 } |
|
57 |
|
58 public void sendToEngine(OutputStream os, int teamsCount) throws IOException{ |
|
59 os.write(QT.getBytes());//command prefix is already in string |
|
60 os.write(prob.getBytes()); |
|
61 os.write(delay.getBytes()); |
|
62 os.write(crate.getBytes()); |
|
63 |
|
64 byte[] ammstore = "eammstore".getBytes(); |
|
65 for(int i = 0; i < teamsCount; i++){ |
|
66 os.write(ammstore); |
|
67 } |
|
68 os.flush(); |
|
69 } |
|
70 |
|
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 |
|
79 public static ArrayList<Weapon> getWeapons(Context c) throws IllegalArgumentException{ |
|
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 |
|
152 return weapons; |
|
153 |
|
154 } catch (XmlPullParserException e) { |
|
155 e.printStackTrace(); |
|
156 } catch (FileNotFoundException e) { |
|
157 e.printStackTrace(); |
|
158 } catch (IOException e) { |
|
159 e.printStackTrace(); |
|
160 } |
|
161 return new ArrayList<Weapon>();//TODO handle correctly |
|
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 |
|
168 public int describeContents() { |
|
169 return 0; |
|
170 } |
|
171 |
|
172 public void writeToParcel(Parcel dest, int flags) { |
|
173 dest.writeString(name); |
|
174 dest.writeString(QT); |
|
175 dest.writeString(prob); |
|
176 dest.writeString(delay); |
|
177 dest.writeString(crate); |
|
178 } |
|
179 |
|
180 private void readFromParcel(Parcel src){ |
|
181 name = src.readString(); |
|
182 QT = src.readString(); |
|
183 prob = src.readString(); |
|
184 delay = src.readString(); |
|
185 crate = src.readString(); |
|
186 } |
|
187 |
|
188 public static final Parcelable.Creator<Weapon> CREATOR = new Parcelable.Creator<Weapon>() { |
|
189 public Weapon createFromParcel(Parcel source) { |
|
190 return new Weapon(source); |
|
191 } |
|
192 public Weapon[] newArray(int size) { |
|
193 return new Weapon[size]; |
|
194 } |
|
195 |
|
196 }; |
|
197 } |