project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/EngineProtocol/Scheme.java
changeset 6488 c356ddebab84
parent 6487 bd3c736c1eac
child 6489 e1f0058cfedd
equal deleted inserted replaced
6487:bd3c736c1eac 6488:c356ddebab84
     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.FilenameFilter;
       
    26 import java.io.IOException;
       
    27 import java.util.ArrayList;
       
    28 import java.util.Arrays;
       
    29 import java.util.LinkedHashMap;
       
    30 
       
    31 import org.xmlpull.v1.XmlPullParser;
       
    32 import org.xmlpull.v1.XmlPullParserException;
       
    33 import org.xmlpull.v1.XmlPullParserFactory;
       
    34 
       
    35 import android.content.Context;
       
    36 import android.os.Parcel;
       
    37 import android.os.Parcelable;
       
    38 
       
    39 public class Scheme implements Parcelable, Comparable<Scheme>{
       
    40 
       
    41 	public static final String DIRECTORY_SCHEME = "schemes";
       
    42 
       
    43 	private String name;
       
    44 	//private ArrayList<Integer> basic;
       
    45 	private Integer gamemod;
       
    46 	private ArrayList<Integer> basic;;
       
    47 	private static ArrayList<LinkedHashMap<String, ?>> basicflags = new ArrayList<LinkedHashMap<String, ?>>();//TODO why is it static?
       
    48 	
       
    49 	public Scheme(String _name, ArrayList<Integer> _basic, int _gamemod){
       
    50 		name = _name;
       
    51 		gamemod = _gamemod;
       
    52 		basic = _basic;
       
    53 	}
       
    54 	
       
    55 	public Scheme(Parcel in){
       
    56 		readFromParcel(in);
       
    57 	}
       
    58 
       
    59 	public void sendToEngine(EngineProtocolNetwork epn)throws IOException{ 
       
    60 		epn.sendToEngine(String.format("e$gmflags %d", gamemod));
       
    61 
       
    62 		for(int pos = 0; pos < basic.size(); pos++){
       
    63 			LinkedHashMap<String, ?> basicflag = basicflags.get(pos);
       
    64 			
       
    65 			String command = (String)basicflag.get("command");
       
    66 			Integer value = basic.get(pos);
       
    67 			Boolean checkOverMax = (Boolean) basicflag.get("checkOverMax");
       
    68 			Boolean times1000 = (Boolean) basicflag.get("times1000");
       
    69 			Integer max = (Integer) basicflag.get("max");
       
    70 			
       
    71 			if(checkOverMax && value >= max) value = max;
       
    72 			if(times1000) value *= 1000;
       
    73 			
       
    74 			epn.sendToEngine(String.format("%s %d", command, value));
       
    75 		}
       
    76 	}
       
    77 	public String toString(){
       
    78 		return name;
       
    79 	}
       
    80 
       
    81 
       
    82 	public static final int STATE_START = 0;
       
    83 	public static final int STATE_ROOT = 1;
       
    84 	public static final int STATE_NAME = 2;
       
    85 	public static final int STATE_BASICFLAGS = 3;
       
    86 	public static final int STATE_GAMEMOD = 4;
       
    87 	public static final int STATE_BASICFLAG_INTEGER = 5;
       
    88 	public static final int STATE_GAMEMOD_TRUE = 6;
       
    89 	public static final int STATE_GAMEMOD_FALSE = 7;
       
    90 
       
    91 	public static ArrayList<Scheme> getSchemes(Context c) throws IllegalArgumentException{
       
    92 		String dir = c.getFilesDir().getAbsolutePath() + '/' + DIRECTORY_SCHEME + '/';
       
    93 		String[] files = new File(dir).list(fnf);
       
    94 		if(files == null) files = new String[]{};
       
    95 		Arrays.sort(files);
       
    96 		ArrayList<Scheme> schemes = new ArrayList<Scheme>();
       
    97 
       
    98 		try {
       
    99 			XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance();
       
   100 			XmlPullParser xmlPuller = xmlPullFactory.newPullParser();
       
   101 
       
   102 			for(String file : files){
       
   103 				BufferedReader br = new BufferedReader(new FileReader(dir + file), 1024);
       
   104 				xmlPuller.setInput(br);
       
   105 				String name = null;
       
   106 				ArrayList<Integer> basic = new ArrayList<Integer>();
       
   107 				Integer gamemod = 0;
       
   108 				int mask = 0x000000004;
       
   109 
       
   110 				int eventType = xmlPuller.getEventType();
       
   111 				int state = STATE_START;
       
   112 				while(eventType != XmlPullParser.END_DOCUMENT){
       
   113 					switch(state){
       
   114 					case STATE_START:
       
   115 						if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("scheme")) state = STATE_ROOT;
       
   116 						else if(eventType != XmlPullParser.START_DOCUMENT) throwException(file, eventType);
       
   117 						break;
       
   118 					case STATE_ROOT:
       
   119 						if(eventType == XmlPullParser.START_TAG){
       
   120 							if(xmlPuller.getName().equals("basicflags")) state = STATE_BASICFLAGS;
       
   121 							else if(xmlPuller.getName().toLowerCase().equals("gamemod")) state = STATE_GAMEMOD;
       
   122 							else if(xmlPuller.getName().toLowerCase().equals("name")) state = STATE_NAME;
       
   123 							else throwException(file, eventType);
       
   124 						}else if(eventType == XmlPullParser.END_TAG) state = STATE_START;
       
   125 						else throwException(xmlPuller.getText(), eventType);
       
   126 						break;
       
   127 					case STATE_BASICFLAGS:
       
   128 						if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().toLowerCase().equals("integer")) state = STATE_BASICFLAG_INTEGER;
       
   129 						else if(eventType == XmlPullParser.END_TAG)	state = STATE_ROOT;
       
   130 						else throwException(file, eventType);
       
   131 						break;
       
   132 					case STATE_GAMEMOD:
       
   133 						if(eventType == XmlPullParser.START_TAG){
       
   134 							if(xmlPuller.getName().toLowerCase().equals("true")) state = STATE_GAMEMOD_TRUE;
       
   135 							else if(xmlPuller.getName().toLowerCase().equals("false")) state = STATE_GAMEMOD_FALSE;
       
   136 							else throwException(file, eventType);
       
   137 						}else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
   138 						else throwException(file, eventType);
       
   139 						break;
       
   140 					case STATE_NAME:
       
   141 						if(eventType == XmlPullParser.TEXT) name = xmlPuller.getText().trim();
       
   142 						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
       
   143 						else throwException(file, eventType);
       
   144 						break;
       
   145 					case STATE_BASICFLAG_INTEGER:
       
   146 						if(eventType == XmlPullParser.TEXT) basic.add(Integer.parseInt(xmlPuller.getText().trim()));
       
   147 						else if(eventType == XmlPullParser.END_TAG) state = STATE_BASICFLAGS;
       
   148 						else throwException(file, eventType);
       
   149 						break;
       
   150 					case STATE_GAMEMOD_FALSE:
       
   151 						if(eventType == XmlPullParser.TEXT) gamemod <<= 1;
       
   152 						else if(eventType == XmlPullParser.END_TAG) state = STATE_GAMEMOD;
       
   153 						else throwException(file, eventType);
       
   154 						break;
       
   155 					case STATE_GAMEMOD_TRUE:
       
   156 						if(eventType == XmlPullParser.TEXT){
       
   157 							gamemod |= mask;
       
   158 							gamemod <<= 1;
       
   159 						}else if(eventType == XmlPullParser.END_TAG) state = STATE_GAMEMOD;
       
   160 						else throwException(file, eventType);
       
   161 						break;
       
   162 					}
       
   163 					eventType = getEventType(xmlPuller);
       
   164 				}//end while(eventtype != END_DOCUMENT
       
   165 				schemes.add(new Scheme(name, basic, gamemod));
       
   166 			}//end for(string file : files
       
   167 			return schemes;
       
   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<Scheme>();//TODO handle correctly
       
   176 	}
       
   177 	
       
   178 	private static FilenameFilter fnf = new FilenameFilter(){
       
   179 		public boolean accept(File dir, String filename) {
       
   180 			return filename.toLowerCase().startsWith("scheme_");
       
   181 		}
       
   182 	};
       
   183 
       
   184 	/**
       
   185 	 * This method will parse the basic flags from a prespecified xml file.
       
   186 	 * I use a raw xml file rather than one parsed by aatp at compile time
       
   187 	 * to keep it generic with other frontends, ie in the future we could 
       
   188 	 * use one provided by the Data folder.
       
   189 	 */
       
   190 	public static void parseBasicFlags(Context c){
       
   191 		String filename = String.format("%s/%s/basicflags", c.getFilesDir().getAbsolutePath(), DIRECTORY_SCHEME);
       
   192 
       
   193 		XmlPullParser xmlPuller = null;
       
   194 		BufferedReader br = null;
       
   195 		try {
       
   196 			XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance();
       
   197 			xmlPuller = xmlPullFactory.newPullParser();
       
   198 			br = new BufferedReader(new FileReader(filename), 1024);
       
   199 			xmlPuller.setInput(br);
       
   200 
       
   201 			int eventType = getEventType(xmlPuller);
       
   202 			boolean continueParsing = true;
       
   203 			do{
       
   204 				switch(eventType){
       
   205 				
       
   206 				case XmlPullParser.START_TAG:
       
   207 					if(xmlPuller.getName().toLowerCase().equals("flag")){
       
   208 						basicflags.add(parseFlag(xmlPuller));
       
   209 					}else if(xmlPuller.getName().toLowerCase().equals("basicflags")){
       
   210 						eventType = getEventType(xmlPuller);
       
   211 					}else{
       
   212 						skipCurrentTag(xmlPuller);
       
   213 						eventType = getEventType(xmlPuller);
       
   214 					}
       
   215 					break;
       
   216 				case XmlPullParser.START_DOCUMENT://ignore all tags not being "flag"
       
   217 				case XmlPullParser.END_TAG:
       
   218 				case XmlPullParser.TEXT:
       
   219 				default:
       
   220 					continueParsing = true;
       
   221 				case XmlPullParser.END_DOCUMENT:
       
   222 					continueParsing = false;
       
   223 				}
       
   224 			}while(continueParsing);
       
   225 
       
   226 		}catch(IOException e){
       
   227 			e.printStackTrace();
       
   228 		}catch (XmlPullParserException e) {
       
   229 			e.printStackTrace();
       
   230 		}finally{
       
   231 			if(br != null)
       
   232 				try {
       
   233 					br.close();
       
   234 				} catch (IOException e) {}
       
   235 		}
       
   236 
       
   237 	}
       
   238 
       
   239 	/*
       
   240 	 * * Parses a Tag structure from xml as example we use
       
   241 	 *<flag>
       
   242 	 *   <checkOverMax>
       
   243 	 *       <boolean>false</boolean>
       
   244 	 *   </checkOverMax>
       
   245 	 *</flag>
       
   246 	 *
       
   247 	 * It returns a LinkedHashMap with key/value pairs
       
   248 	 */
       
   249 	private static LinkedHashMap<String, Object> parseFlag(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{
       
   250 		LinkedHashMap<String, Object> hash = new LinkedHashMap<String, Object>();
       
   251 
       
   252 		int eventType = xmlPuller.getEventType();//Get the event type which triggered this method
       
   253 		if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().toLowerCase().equals("flag")){//valid start of flag tag
       
   254 			String lcKey = null;
       
   255 			String lcType = null;
       
   256 			String value = null;
       
   257 
       
   258 			eventType = getEventType(xmlPuller);//<checkOverMax>
       
   259 			while(eventType == XmlPullParser.START_TAG){
       
   260 				lcKey = xmlPuller.getName();//checkOverMax
       
   261 				if(getEventType(xmlPuller) == XmlPullParser.START_TAG){//<boolean>
       
   262 					lcType = xmlPuller.getName().toLowerCase();
       
   263 					if(getEventType(xmlPuller) == XmlPullParser.TEXT){
       
   264 						value = xmlPuller.getText();
       
   265 						if(getEventType(xmlPuller) == XmlPullParser.END_TAG && //</boolean> 
       
   266 								getEventType(xmlPuller) == XmlPullParser.END_TAG){//</checkOverMax>
       
   267 							if(lcType.equals("boolean")) hash.put(lcKey, new Boolean(value));
       
   268 							else if(lcType.equals("string"))hash.put(lcKey, value);							
       
   269 							else if(lcType.equals("integer")){
       
   270 								try{
       
   271 									hash.put(lcKey, new Integer(value));
       
   272 								}catch (NumberFormatException e){
       
   273 									throw new XmlPullParserException("Wrong integer value in xml file");
       
   274 								}
       
   275 							}else{
       
   276 								throwException("basicflags", eventType);
       
   277 							}
       
   278 						}//</boolean> / </checkOverMax>
       
   279 					}//if TEXT
       
   280 				}//if boolean
       
   281 				eventType = getEventType(xmlPuller);//start new loop
       
   282 			}
       
   283 			eventType = getEventType(xmlPuller);//</flag>
       
   284 		}
       
   285 
       
   286 		return hash;
       
   287 	}
       
   288 
       
   289 	private static void skipCurrentTag(XmlPullParser xmlPuller) throws XmlPullParserException, IOException{
       
   290 		int eventType = xmlPuller.getEventType();
       
   291 		if(eventType != XmlPullParser.START_TAG)return;
       
   292 		String tag = xmlPuller.getName().toLowerCase();
       
   293 
       
   294 		while(true){
       
   295 			eventType = getEventType(xmlPuller);//getNext()
       
   296 			switch(eventType){
       
   297 			case XmlPullParser.START_DOCUMENT://we're inside of a start tag so START_ or END_DOCUMENT is just wrong
       
   298 			case XmlPullParser.END_DOCUMENT:
       
   299 				throw new XmlPullParserException("invalid xml file");
       
   300 			case XmlPullParser.START_TAG://if we get a new tag recursively handle it
       
   301 				skipCurrentTag(xmlPuller);
       
   302 				break;
       
   303 			case XmlPullParser.TEXT:
       
   304 				break;
       
   305 			case XmlPullParser.END_TAG:
       
   306 				if(!xmlPuller.getName().toLowerCase().equals(tag)){//if the end tag doesn't match the start tag
       
   307 					throw new XmlPullParserException("invalid xml file");
       
   308 				}else{
       
   309 					return;//skip completed	
       
   310 				}
       
   311 
       
   312 			}
       
   313 		}
       
   314 	}
       
   315 
       
   316 	/**
       
   317 	 * Skips whitespaces..
       
   318 	 */
       
   319 	private static int getEventType(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{
       
   320 		int eventType = xmlPuller.next();
       
   321 		while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){
       
   322 			eventType = xmlPuller.next();
       
   323 		}
       
   324 		return eventType;
       
   325 	}
       
   326 	private static void throwException(String file, int eventType){
       
   327 		throw new IllegalArgumentException(String.format("Xml file: %s malformed with error: %d.", file, eventType));
       
   328 	}
       
   329 
       
   330 	public int describeContents() {
       
   331 		return 0;
       
   332 	}
       
   333 
       
   334 	public void writeToParcel(Parcel dest, int flags) {
       
   335 		dest.writeString(name);
       
   336 		dest.writeInt(gamemod);
       
   337 		dest.writeList(basic);
       
   338 	}
       
   339 	
       
   340 	public void readFromParcel(Parcel src){
       
   341 		name = src.readString();
       
   342 		gamemod = src.readInt();
       
   343 		basic = src.readArrayList(ArrayList.class.getClassLoader());
       
   344 	}
       
   345 
       
   346 	public static final Parcelable.Creator<Scheme> CREATOR = new Parcelable.Creator<Scheme>() {
       
   347 		public Scheme createFromParcel(Parcel source) {
       
   348 			return new Scheme(source);
       
   349 		}
       
   350 		public Scheme[] newArray(int size) {
       
   351 			return new Scheme[size];
       
   352 		}
       
   353 		
       
   354 	};
       
   355 
       
   356 	public int compareTo(Scheme another) {
       
   357 		return name.compareTo(another.name);
       
   358 	}
       
   359 }