project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Scheme.java
author Xeli
Thu, 04 Aug 2011 17:27:05 +0200
branchhedgeroid
changeset 5463 83c53a80f7ff
parent 5433 8f82045953c1
permissions -rw-r--r--
datastructures for the different aspects of a gameconfiguration
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     1
package org.hedgewars.mobile;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     2
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     3
import java.io.BufferedOutputStream;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     4
import java.io.BufferedReader;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     5
import java.io.File;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     6
import java.io.FileNotFoundException;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     7
import java.io.FileOutputStream;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     8
import java.io.FileReader;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     9
import java.io.IOException;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    10
import java.io.InputStream;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    11
import java.util.ArrayList;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    12
import java.util.Arrays;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    13
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    14
import org.xmlpull.v1.XmlPullParser;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    15
import org.xmlpull.v1.XmlPullParserException;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    16
import org.xmlpull.v1.XmlPullParserFactory;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    17
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    18
import android.content.Context;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    19
import android.content.res.TypedArray;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    20
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    21
public class Scheme {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    22
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    23
	public static final String DIRECTORY_SCHEME = "schemes";
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    24
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    25
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    26
	private String name;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    27
	private ArrayList<Integer> basic;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    28
	private ArrayList<Boolean> gamemod;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    29
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    30
	public Scheme(String _name, ArrayList<Integer> _basic, ArrayList<Boolean> _gamemod){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    31
		name = _name;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    32
		basic = _basic;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    33
		gamemod = _gamemod;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    34
	}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    35
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    36
	public String toString(){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    37
		return name;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    38
	}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    39
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    40
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    41
	public static final int STATE_START = 0;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    42
	public static final int STATE_ROOT = 1;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    43
	public static final int STATE_NAME = 2;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    44
	public static final int STATE_BASICFLAGS = 3;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    45
	public static final int STATE_GAMEMOD = 4;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    46
	public static final int STATE_BASICFLAG_INTEGER = 5;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    47
	public static final int STATE_GAMEMOD_TRUE = 6;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    48
	public static final int STATE_GAMEMOD_FALSE = 7;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    49
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    50
	public static Scheme[] getSchemes(Context c) throws IllegalArgumentException{
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    51
		String dir = c.getFilesDir().getAbsolutePath() + '/' + DIRECTORY_SCHEME + '/';
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    52
		String[] files = new File(dir).list();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    53
		if(files == null) files = new String[]{};
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    54
		Arrays.sort(files);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    55
		
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    56
		ArrayList<Scheme> schemes = new ArrayList<Scheme>();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    57
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    58
		try {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    59
			XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    60
			XmlPullParser xmlPuller = xmlPullFactory.newPullParser();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    61
			
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    62
			for(String file : files){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    63
				BufferedReader br = new BufferedReader(new FileReader(dir + file), 1024);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    64
				xmlPuller.setInput(br);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    65
				String name = null;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    66
				ArrayList<Integer> basic = new ArrayList<Integer>();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    67
				ArrayList<Boolean> gamemod = new ArrayList<Boolean>();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    68
				int eventType = xmlPuller.getEventType();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    69
				int state = STATE_START;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    70
				while(eventType != XmlPullParser.END_DOCUMENT){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    71
					switch(state){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    72
					case STATE_START:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    73
						if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("scheme")) state = STATE_ROOT;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    74
						else if(eventType != XmlPullParser.START_DOCUMENT) throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    75
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    76
					case STATE_ROOT:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    77
						if(eventType == XmlPullParser.START_TAG){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    78
							if(xmlPuller.getName().equals("basicflags")) state = STATE_BASICFLAGS;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    79
							else if(xmlPuller.getName().toLowerCase().equals("gamemod")) state = STATE_GAMEMOD;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    80
							else if(xmlPuller.getName().toLowerCase().equals("name")) state = STATE_NAME;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    81
							else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    82
						}else if(eventType == XmlPullParser.END_TAG) state = STATE_START;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    83
						else throwException(xmlPuller.getText(), eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    84
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    85
					case STATE_BASICFLAGS:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    86
						if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().toLowerCase().equals("integer")) state = STATE_BASICFLAG_INTEGER;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    87
						else if(eventType == XmlPullParser.END_TAG)	state = STATE_ROOT;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    88
						else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    89
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    90
					case STATE_GAMEMOD:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    91
						if(eventType == XmlPullParser.START_TAG){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    92
							if(xmlPuller.getName().toLowerCase().equals("true")) state = STATE_GAMEMOD_TRUE;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    93
							else if(xmlPuller.getName().toLowerCase().equals("false")) state = STATE_GAMEMOD_FALSE;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    94
							else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    95
						}else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    96
						else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    97
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    98
					case STATE_NAME:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    99
						if(eventType == XmlPullParser.TEXT) name = xmlPuller.getText().trim();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   100
						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   101
						else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   102
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   103
					case STATE_BASICFLAG_INTEGER:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   104
						if(eventType == XmlPullParser.TEXT) basic.add(Integer.parseInt(xmlPuller.getText().trim()));
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   105
						else if(eventType == XmlPullParser.END_TAG) state = STATE_BASICFLAGS;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   106
						else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   107
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   108
					case STATE_GAMEMOD_FALSE:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   109
						if(eventType == XmlPullParser.TEXT) gamemod.add(false);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   110
						else if(eventType == XmlPullParser.END_TAG) state = STATE_GAMEMOD;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   111
						else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   112
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   113
					case STATE_GAMEMOD_TRUE:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   114
						if(eventType == XmlPullParser.TEXT) gamemod.add(true);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   115
						else if(eventType == XmlPullParser.END_TAG) state = STATE_GAMEMOD;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   116
						else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   117
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   118
					}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   119
					eventType = xmlPuller.next();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   120
					while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){//Skip whitespaces
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   121
						eventType = xmlPuller.next();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   122
					}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   123
				}//end while(eventtype != END_DOCUMENT
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   124
				schemes.add(new Scheme(name, basic, gamemod));
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   125
			}//end for(string file : files
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   126
			Scheme[] ret = new Scheme[schemes.size()];
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   127
			schemes.toArray(ret);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   128
			return ret;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   129
		} catch (XmlPullParserException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   130
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   131
		} catch (FileNotFoundException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   132
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   133
		} catch (IOException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   134
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   135
		}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   136
		return new Scheme[]{};//TODO handle correctly
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   137
	}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   138
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   139
	private static void throwException(String file, int eventType){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   140
		throw new IllegalArgumentException(String.format("Xml file: %s malformed with eventType: %d.", file, eventType));
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   141
	}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   142
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   143
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   144
}