project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Weapon.java
author Xeli
Thu, 14 Jul 2011 15:55:28 +0200
branchhedgeroid
changeset 5433 8f82045953c1
permissions -rw-r--r--
Main class files for the start-local-game screen
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.BufferedReader;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     4
import java.io.File;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     5
import java.io.FileNotFoundException;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     6
import java.io.FileReader;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     7
import java.io.IOException;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     8
import java.util.ArrayList;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     9
import java.util.Arrays;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    10
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    11
import org.xmlpull.v1.XmlPullParser;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    12
import org.xmlpull.v1.XmlPullParserException;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    13
import org.xmlpull.v1.XmlPullParserFactory;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    14
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    15
import android.content.Context;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    16
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    17
public class Weapon {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    18
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    19
	public static final String DIRECTORY_WEAPON = "weapons";
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
	private String name;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    22
	private String QT;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    23
	private String prob;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    24
	private String delay;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    25
	private String crate;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    26
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    27
	public Weapon(String _name, String _QT, String _prob, String _delay, String _crate){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    28
		name = _name;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    29
		QT = _QT;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    30
		prob = _prob;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    31
		delay = _delay;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    32
		crate = _crate;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    33
	}
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
	public String toString(){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    36
		return name;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    37
	}
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
	public static final int STATE_START = 0;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    40
	public static final int STATE_ROOT = 1;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    41
	public static final int STATE_NAME = 2;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    42
	public static final int STATE_QT = 3;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    43
	public static final int STATE_PROBABILITY = 4;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    44
	public static final int STATE_DELAY = 5;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    45
	public static final int STATE_CRATE = 6;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    46
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    47
	public static Weapon[] getWeapons(Context c) throws IllegalArgumentException{
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    48
		String dir = c.getFilesDir().getAbsolutePath() + '/' + DIRECTORY_WEAPON + '/';
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    49
		String[] files = new File(dir).list();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    50
		if(files == null) files = new String[]{};
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    51
		Arrays.sort(files);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    52
		
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    53
		ArrayList<Weapon> weapons = new ArrayList<Weapon>();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    54
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    55
		try {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    56
			XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    57
			XmlPullParser xmlPuller = xmlPullFactory.newPullParser();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    58
			
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    59
			for(String file : files){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    60
				BufferedReader br = new BufferedReader(new FileReader(dir + file), 1024);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    61
				xmlPuller.setInput(br);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    62
				String name = null;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    63
				String qt = null;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    64
				String prob = null;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    65
				String delay = null;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    66
				String crate = null;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    67
				
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("weapon")) 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().toLowerCase().equals("qt")) state = STATE_QT;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    79
							else if(xmlPuller.getName().toLowerCase().equals("name")) state = STATE_NAME;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    80
							else if(xmlPuller.getName().toLowerCase().equals("probability")) state = STATE_PROBABILITY;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    81
							else if(xmlPuller.getName().toLowerCase().equals("delay")) state = STATE_DELAY;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    82
							else if(xmlPuller.getName().toLowerCase().equals("crate")) state = STATE_CRATE;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    83
							else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    84
						}else if(eventType == XmlPullParser.END_TAG) state = STATE_START;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    85
						else throwException(xmlPuller.getText(), eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    86
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    87
					case STATE_NAME:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    88
						if(eventType == XmlPullParser.TEXT) name = xmlPuller.getText().trim();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    89
						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    90
						else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    91
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    92
					case STATE_QT:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    93
						if(eventType == XmlPullParser.TEXT) qt = xmlPuller.getText().trim();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    94
						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    95
						else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    96
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    97
					case STATE_PROBABILITY:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    98
						if(eventType == XmlPullParser.TEXT) prob = xmlPuller.getText().trim();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    99
						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   100
						else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   101
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   102
					case STATE_DELAY:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   103
						if(eventType == XmlPullParser.TEXT) delay = xmlPuller.getText().trim();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   104
						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   105
						else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   106
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   107
					case STATE_CRATE:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   108
						if(eventType == XmlPullParser.TEXT) crate = xmlPuller.getText().trim();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   109
						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   110
						else throwException(file, eventType);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   111
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   112
					}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   113
					eventType = xmlPuller.next();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   114
					while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){//Skip whitespaces
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   115
						eventType = xmlPuller.next();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   116
					}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   117
				}//end while(eventtype != END_DOCUMENT
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   118
				weapons.add(new Weapon(name, qt, prob, delay, crate));
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   119
			}//end for(string file : files
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   120
			Weapon[] ret = new Weapon[weapons.size()];
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   121
			weapons.toArray(ret);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   122
			return ret;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   123
			
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   124
		} catch (XmlPullParserException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   125
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   126
		} catch (FileNotFoundException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   127
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   128
		} catch (IOException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   129
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   130
		}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   131
		return new Weapon[]{};//TODO handle correctly
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   132
	}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   133
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   134
	private static void throwException(String file, int eventType){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   135
		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
   136
	}
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
}