project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/Weapon.java
branchhedgeroid
changeset 6047 10011f051f9c
parent 6045 9a7cc0f29430
child 6049 7bc38086d771
equal deleted inserted replaced
6045:9a7cc0f29430 6047:10011f051f9c
     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.mobile.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 import java.util.Arrays;
       
    28 
       
    29 import org.xmlpull.v1.XmlPullParser;
       
    30 import org.xmlpull.v1.XmlPullParserException;
       
    31 import org.xmlpull.v1.XmlPullParserFactory;
       
    32 
       
    33 import android.content.Context;
       
    34 import android.os.Parcel;
       
    35 import android.os.Parcelable;
       
    36 
       
    37 public class Weapon implements Parcelable{
       
    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 		Arrays.sort(files);
       
    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 }