1 /* |
|
2 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
|
3 * Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation; version 2 of the License |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 */ |
|
18 |
|
19 package org.hedgewars.hedgeroid.EngineProtocol; |
|
20 |
|
21 import java.io.BufferedReader; |
|
22 import java.io.File; |
|
23 import java.io.FileNotFoundException; |
|
24 import java.io.FileReader; |
|
25 import java.io.IOException; |
|
26 import java.util.ArrayList; |
|
27 |
|
28 import org.xmlpull.v1.XmlPullParser; |
|
29 import org.xmlpull.v1.XmlPullParserException; |
|
30 import org.xmlpull.v1.XmlPullParserFactory; |
|
31 |
|
32 import android.content.Context; |
|
33 import android.os.Parcel; |
|
34 import android.os.Parcelable; |
|
35 import android.util.Log; |
|
36 |
|
37 public class Weapon implements Parcelable, Comparable<Weapon>{ |
|
38 |
|
39 public static final String DIRECTORY_WEAPON = "weapons"; |
|
40 |
|
41 private String name; |
|
42 private String QT; |
|
43 private String prob; |
|
44 private String delay; |
|
45 private String crate; |
|
46 private static int maxWeapons; |
|
47 |
|
48 static{ |
|
49 //maxWeapons = PascalExports.HWgetNumberOfWeapons(); |
|
50 } |
|
51 |
|
52 public Weapon(String _name, String _QT, String _prob, String _delay, String _crate){ |
|
53 name = _name; |
|
54 |
|
55 //Incase there's a newer ammoStore which is bigger we append with zeros |
|
56 StringBuffer sb = new StringBuffer(); |
|
57 while(_QT.length() + sb.length() < maxWeapons){ |
|
58 sb.append('0'); |
|
59 } |
|
60 |
|
61 QT = String.format("e%s %s%s", "ammloadt", _QT, sb); |
|
62 prob = String.format("e%s %s%s", "ammprob", _prob, sb); |
|
63 delay = String.format("e%s %s%s", "ammdelay", _delay, sb); |
|
64 crate = String.format("e%s %s%s", "ammreinf", _crate, sb); |
|
65 } |
|
66 |
|
67 public Weapon(Parcel in){ |
|
68 readFromParcel(in); |
|
69 } |
|
70 |
|
71 public String toString(){ |
|
72 return name; |
|
73 } |
|
74 |
|
75 public void sendToEngine(EngineProtocolNetwork epn, int teamsCount) throws IOException{ |
|
76 epn.sendToEngine(QT);//command prefix is already in string |
|
77 epn.sendToEngine(prob); |
|
78 epn.sendToEngine(delay); |
|
79 epn.sendToEngine(crate); |
|
80 |
|
81 for(int i = 0; i < teamsCount; i++){ |
|
82 epn.sendToEngine("eammstore"); |
|
83 } |
|
84 } |
|
85 |
|
86 public static final int STATE_START = 0; |
|
87 public static final int STATE_ROOT = 1; |
|
88 public static final int STATE_NAME = 2; |
|
89 public static final int STATE_QT = 3; |
|
90 public static final int STATE_PROBABILITY = 4; |
|
91 public static final int STATE_DELAY = 5; |
|
92 public static final int STATE_CRATE = 6; |
|
93 |
|
94 public static ArrayList<Weapon> getWeapons(Context c) throws IllegalArgumentException{ |
|
95 String dir = c.getFilesDir().getAbsolutePath() + '/' + DIRECTORY_WEAPON + '/'; |
|
96 String[] files = new File(dir).list(); |
|
97 if(files == null) files = new String[]{}; |
|
98 |
|
99 ArrayList<Weapon> weapons = new ArrayList<Weapon>(); |
|
100 |
|
101 try { |
|
102 XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance(); |
|
103 XmlPullParser xmlPuller = xmlPullFactory.newPullParser(); |
|
104 |
|
105 for(String file : files){ |
|
106 BufferedReader br = new BufferedReader(new FileReader(dir + file), 1024); |
|
107 xmlPuller.setInput(br); |
|
108 String name = null; |
|
109 String qt = null; |
|
110 String prob = null; |
|
111 String delay = null; |
|
112 String crate = null; |
|
113 |
|
114 int eventType = xmlPuller.getEventType(); |
|
115 int state = STATE_START; |
|
116 while(eventType != XmlPullParser.END_DOCUMENT){ |
|
117 switch(state){ |
|
118 case STATE_START: |
|
119 if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("weapon")) state = STATE_ROOT; |
|
120 else if(eventType != XmlPullParser.START_DOCUMENT) throwException(file, eventType); |
|
121 break; |
|
122 case STATE_ROOT: |
|
123 if(eventType == XmlPullParser.START_TAG){ |
|
124 if(xmlPuller.getName().toLowerCase().equals("qt")) state = STATE_QT; |
|
125 else if(xmlPuller.getName().toLowerCase().equals("name")) state = STATE_NAME; |
|
126 else if(xmlPuller.getName().toLowerCase().equals("probability")) state = STATE_PROBABILITY; |
|
127 else if(xmlPuller.getName().toLowerCase().equals("delay")) state = STATE_DELAY; |
|
128 else if(xmlPuller.getName().toLowerCase().equals("crate")) state = STATE_CRATE; |
|
129 else throwException(file, eventType); |
|
130 }else if(eventType == XmlPullParser.END_TAG) state = STATE_START; |
|
131 else throwException(xmlPuller.getText(), eventType); |
|
132 break; |
|
133 case STATE_NAME: |
|
134 if(eventType == XmlPullParser.TEXT) name = xmlPuller.getText().trim(); |
|
135 else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT; |
|
136 else throwException(file, eventType); |
|
137 break; |
|
138 case STATE_QT: |
|
139 if(eventType == XmlPullParser.TEXT) qt = xmlPuller.getText().trim(); |
|
140 else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT; |
|
141 else throwException(file, eventType); |
|
142 break; |
|
143 case STATE_PROBABILITY: |
|
144 if(eventType == XmlPullParser.TEXT) prob = xmlPuller.getText().trim(); |
|
145 else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT; |
|
146 else throwException(file, eventType); |
|
147 break; |
|
148 case STATE_DELAY: |
|
149 if(eventType == XmlPullParser.TEXT) delay = xmlPuller.getText().trim(); |
|
150 else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT; |
|
151 else throwException(file, eventType); |
|
152 break; |
|
153 case STATE_CRATE: |
|
154 if(eventType == XmlPullParser.TEXT) crate = xmlPuller.getText().trim(); |
|
155 else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT; |
|
156 else throwException(file, eventType); |
|
157 break; |
|
158 } |
|
159 eventType = xmlPuller.next(); |
|
160 while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){//Skip whitespaces |
|
161 eventType = xmlPuller.next(); |
|
162 } |
|
163 }//end while(eventtype != END_DOCUMENT |
|
164 weapons.add(new Weapon(name, qt, prob, delay, crate)); |
|
165 }//end for(string file : files |
|
166 return weapons; |
|
167 |
|
168 } catch (XmlPullParserException e) { |
|
169 e.printStackTrace(); |
|
170 } catch (FileNotFoundException e) { |
|
171 e.printStackTrace(); |
|
172 } catch (IOException e) { |
|
173 e.printStackTrace(); |
|
174 } |
|
175 return new ArrayList<Weapon>();//TODO handle correctly |
|
176 } |
|
177 |
|
178 private static void throwException(String file, int eventType){ |
|
179 throw new IllegalArgumentException(String.format("Xml file: %s malformed with eventType: %d.", file, eventType)); |
|
180 } |
|
181 |
|
182 public int describeContents() { |
|
183 return 0; |
|
184 } |
|
185 |
|
186 public void writeToParcel(Parcel dest, int flags) { |
|
187 dest.writeString(name); |
|
188 dest.writeString(QT); |
|
189 dest.writeString(prob); |
|
190 dest.writeString(delay); |
|
191 dest.writeString(crate); |
|
192 } |
|
193 |
|
194 private void readFromParcel(Parcel src){ |
|
195 name = src.readString(); |
|
196 QT = src.readString(); |
|
197 prob = src.readString(); |
|
198 delay = src.readString(); |
|
199 crate = src.readString(); |
|
200 } |
|
201 |
|
202 public static final Parcelable.Creator<Weapon> CREATOR = new Parcelable.Creator<Weapon>() { |
|
203 public Weapon createFromParcel(Parcel source) { |
|
204 return new Weapon(source); |
|
205 } |
|
206 public Weapon[] newArray(int size) { |
|
207 return new Weapon[size]; |
|
208 } |
|
209 |
|
210 }; |
|
211 |
|
212 public int compareTo(Weapon another) { |
|
213 return name.compareTo(another.name); |
|
214 } |
|
215 |
|
216 |
|
217 } |
|