project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Datastructures/Weapon.java
changeset 7476 2fb781bbdd51
parent 7083 5339aba29571
equal deleted inserted replaced
7473:45b9f25ff611 7476:2fb781bbdd51
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  */
    17  */
    18 
    18 
    19 package org.hedgewars.hedgeroid.Datastructures;
    19 package org.hedgewars.hedgeroid.Datastructures;
    20 
    20 
    21 import java.io.BufferedReader;
    21 import org.hedgewars.hedgeroid.EngineProtocol.PascalExports;
    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 
    22 
    28 import org.hedgewars.hedgeroid.EngineProtocol.EngineProtocolNetwork;
    23 public class Weapon implements Comparable<Weapon>{
    29 import org.hedgewars.hedgeroid.EngineProtocol.PascalExports;
       
    30 import org.xmlpull.v1.XmlPullParser;
       
    31 import org.xmlpull.v1.XmlPullParserException;
       
    32 import org.xmlpull.v1.XmlPullParserFactory;
       
    33 
       
    34 import android.content.Context;
       
    35 import android.os.Parcel;
       
    36 import android.os.Parcelable;
       
    37 
       
    38 public class Weapon implements Parcelable, Comparable<Weapon>{
       
    39 
       
    40 	public static final String DIRECTORY_WEAPON = "weapons";
    24 	public static final String DIRECTORY_WEAPON = "weapons";
       
    25 	public static final int maxWeapons = PascalExports.HWgetNumberOfWeapons();
    41 	
    26 	
    42 	private String name;
    27 	private String name;
    43 	private String QT;
    28 	private String QT;
    44 	private String prob;
    29 	private String prob;
    45 	private String delay;
    30 	private String delay;
    46 	private String crate;
    31 	private String crate;
    47 	private static int maxWeapons;
       
    48 	
       
    49 	static{
       
    50 		maxWeapons = PascalExports.HWgetNumberOfWeapons();
       
    51 	}
       
    52 	
    32 	
    53 	public Weapon(String _name, String _QT, String _prob, String _delay, String _crate){
    33 	public Weapon(String _name, String _QT, String _prob, String _delay, String _crate){
    54 		name = _name;
    34 		name = _name;
    55 		
    35 		
    56 		//Incase there's a newer ammoStore which is bigger we append with zeros
    36 		//Incase there's a newer ammoStore which is bigger we append with zeros
    63 		prob = String.format("e%s %s%s", "ammprob", _prob, sb);
    43 		prob = String.format("e%s %s%s", "ammprob", _prob, sb);
    64 		delay = String.format("e%s %s%s", "ammdelay", _delay, sb);
    44 		delay = String.format("e%s %s%s", "ammdelay", _delay, sb);
    65 		crate = String.format("e%s %s%s", "ammreinf", _crate, sb);
    45 		crate = String.format("e%s %s%s", "ammreinf", _crate, sb);
    66 	}
    46 	}
    67 	
    47 	
    68 	public Weapon(Parcel in){
       
    69 		readFromParcel(in);
       
    70 	}
       
    71 	
       
    72 	public String toString(){
    48 	public String toString(){
    73 		return name;
    49 		return name;
    74 	}
    50 	}
    75 	
       
    76 	public void sendToEngine(EngineProtocolNetwork epn, int teamsCount) throws IOException{
       
    77 		epn.sendToEngine(QT);//command prefix is already in string 
       
    78 		epn.sendToEngine(prob);
       
    79 		epn.sendToEngine(delay);
       
    80 		epn.sendToEngine(crate);
       
    81 		
    51 		
    82 		for(int i = 0; i < teamsCount; i++){
       
    83 			epn.sendToEngine("eammstore");
       
    84 		}
       
    85 	}
       
    86 	
       
    87 	public static final int STATE_START = 0;
       
    88 	public static final int STATE_ROOT = 1;
       
    89 	public static final int STATE_NAME = 2;
       
    90 	public static final int STATE_QT = 3;
       
    91 	public static final int STATE_PROBABILITY = 4;
       
    92 	public static final int STATE_DELAY = 5;
       
    93 	public static final int STATE_CRATE = 6;
       
    94 	
       
    95 	public static ArrayList<Weapon> getWeapons(Context c) throws IllegalArgumentException{
       
    96 		String dir = c.getFilesDir().getAbsolutePath() + '/' + DIRECTORY_WEAPON + '/';
       
    97 		String[] files = new File(dir).list();
       
    98 		if(files == null) files = new String[]{};
       
    99 		
       
   100 		ArrayList<Weapon> weapons = new ArrayList<Weapon>();
       
   101 
       
   102 		try {
       
   103 			XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance();
       
   104 			XmlPullParser xmlPuller = xmlPullFactory.newPullParser();
       
   105 			
       
   106 			for(String file : files){
       
   107 				BufferedReader br = new BufferedReader(new FileReader(dir + file), 1024);
       
   108 				xmlPuller.setInput(br);
       
   109 				String name = null;
       
   110 				String qt = null;
       
   111 				String prob = null;
       
   112 				String delay = null;
       
   113 				String crate = null;
       
   114 				
       
   115 				int eventType = xmlPuller.getEventType();
       
   116 				int state = STATE_START;
       
   117 				while(eventType != XmlPullParser.END_DOCUMENT){
       
   118 					switch(state){
       
   119 					case STATE_START:
       
   120 						if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("weapon")) state = STATE_ROOT;
       
   121 						else if(eventType != XmlPullParser.START_DOCUMENT) throwException(file, eventType);
       
   122 						break;
       
   123 					case STATE_ROOT:
       
   124 						if(eventType == XmlPullParser.START_TAG){
       
   125 							if(xmlPuller.getName().toLowerCase().equals("qt")) state = STATE_QT;
       
   126 							else if(xmlPuller.getName().toLowerCase().equals("name")) state = STATE_NAME;
       
   127 							else if(xmlPuller.getName().toLowerCase().equals("probability")) state = STATE_PROBABILITY;
       
   128 							else if(xmlPuller.getName().toLowerCase().equals("delay")) state = STATE_DELAY;
       
   129 							else if(xmlPuller.getName().toLowerCase().equals("crate")) state = STATE_CRATE;
       
   130 							else throwException(file, eventType);
       
   131 						}else if(eventType == XmlPullParser.END_TAG) state = STATE_START;
       
   132 						else throwException(xmlPuller.getText(), eventType);
       
   133 						break;
       
   134 					case STATE_NAME:
       
   135 						if(eventType == XmlPullParser.TEXT) name = xmlPuller.getText().trim();
       
   136 						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
   137 						else throwException(file, eventType);
       
   138 						break;
       
   139 					case STATE_QT:
       
   140 						if(eventType == XmlPullParser.TEXT) qt = xmlPuller.getText().trim();
       
   141 						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
   142 						else throwException(file, eventType);
       
   143 						break;
       
   144 					case STATE_PROBABILITY:
       
   145 						if(eventType == XmlPullParser.TEXT) prob = xmlPuller.getText().trim();
       
   146 						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
   147 						else throwException(file, eventType);
       
   148 						break;
       
   149 					case STATE_DELAY:
       
   150 						if(eventType == XmlPullParser.TEXT) delay = xmlPuller.getText().trim();
       
   151 						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
   152 						else throwException(file, eventType);
       
   153 						break;
       
   154 					case STATE_CRATE:
       
   155 						if(eventType == XmlPullParser.TEXT) crate = xmlPuller.getText().trim();
       
   156 						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
   157 						else throwException(file, eventType);
       
   158 						break;
       
   159 					}
       
   160 					eventType = xmlPuller.next();
       
   161 					while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){//Skip whitespaces
       
   162 						eventType = xmlPuller.next();
       
   163 					}
       
   164 				}//end while(eventtype != END_DOCUMENT
       
   165 				weapons.add(new Weapon(name, qt, prob, delay, crate));
       
   166 			}//end for(string file : files
       
   167 			return weapons;
       
   168 			
       
   169 		} catch (XmlPullParserException e) {
       
   170 			e.printStackTrace();
       
   171 		} catch (FileNotFoundException e) {
       
   172 			e.printStackTrace();
       
   173 		} catch (IOException e) {
       
   174 			e.printStackTrace();
       
   175 		}
       
   176 		return new ArrayList<Weapon>();//TODO handle correctly
       
   177 	}
       
   178 	
       
   179 	private static void throwException(String file, int eventType){
       
   180 		throw new IllegalArgumentException(String.format("Xml file: %s malformed with eventType: %d.", file, eventType));
       
   181 	}
       
   182 
       
   183 	public int describeContents() {
       
   184 		return 0;
       
   185 	}
       
   186 
       
   187 	public void writeToParcel(Parcel dest, int flags) {
       
   188 		dest.writeString(name);
       
   189 		dest.writeString(QT);
       
   190 		dest.writeString(prob);
       
   191 		dest.writeString(delay);
       
   192 		dest.writeString(crate);
       
   193 	}
       
   194 	
       
   195 	private void readFromParcel(Parcel src){
       
   196 		name = src.readString();
       
   197 		QT = src.readString();
       
   198 		prob = src.readString();
       
   199 		delay = src.readString();
       
   200 		crate = src.readString();
       
   201 	}
       
   202 	
       
   203 	public static final Parcelable.Creator<Weapon> CREATOR = new Parcelable.Creator<Weapon>() {
       
   204 		public Weapon createFromParcel(Parcel source) {
       
   205 			return new Weapon(source);
       
   206 		}
       
   207 		public Weapon[] newArray(int size) {
       
   208 			return new Weapon[size];
       
   209 		}
       
   210 		
       
   211 	};
       
   212 
       
   213 	public int compareTo(Weapon another) {
    52 	public int compareTo(Weapon another) {
   214 		return name.compareTo(another.name);
    53 		return name.compareTo(another.name);
   215 	}
    54 	}
   216 	
       
   217 	
       
   218 }
    55 }