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