project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Datastructures/Weaponsets.java
author Medo <smaxein@googlemail.com>
Mon, 20 Aug 2012 21:05:57 +0200
changeset 7584 7831c84cc644
parent 7508 763d3961400b
permissions -rw-r--r--
License change: With the agreement of Xeli, I changed the Hedgeroid license to GPLv2+ (from GPLv2).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     1
/*
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     4
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     5
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     6
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     8
 * of the License, or (at your option) any later version.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     9
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    13
 * GNU General Public License for more details.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    14
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    15
 * You should have received a copy of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    16
 * along with this program; if not, write to the Free Software
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    18
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    19
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid.Datastructures;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import java.io.File;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import java.io.IOException;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import java.util.ArrayList;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    25
import java.util.Iterator;
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
import java.util.List;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import org.hedgewars.hedgeroid.frontlib.Flib;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import org.hedgewars.hedgeroid.frontlib.Frontlib.WeaponsetListPtr;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.content.Context;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
public final class Weaponsets {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
	private Weaponsets() {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
		throw new AssertionError("This class is not meant to be instantiated");
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
	}
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
	
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
	public static File getUserWeaponsetsFile(Context c) {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
		return new File(c.getFilesDir(), "weapons_user.ini");
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
	}
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
	
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	public static File getBuiltinWeaponsetsFile(Context c) {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
		return new File(c.getFilesDir(), "weapons_builtin.ini");
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
	}
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
	
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
	public static List<Weaponset> loadAllWeaponsets(Context c) throws IOException {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
		List<Weaponset> result = loadBuiltinWeaponsets(c);
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
		result.addAll(loadUserWeaponsets(c));
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
		return result;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
	}
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
	
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
	public static List<Weaponset> loadUserWeaponsets(Context c) throws IOException {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
		return loadWeaponsets(c, getUserWeaponsetsFile(c));
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
	}
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
	
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
	public static List<Weaponset> loadBuiltinWeaponsets(Context c) throws IOException {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
		return loadWeaponsets(c, getBuiltinWeaponsetsFile(c));
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
	}
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
	
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
	public static List<Weaponset> loadWeaponsets(Context c, File weaponsetFile) throws IOException {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
		if(!weaponsetFile.isFile()) {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
			// No file == no weaponsets, no error
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
			return new ArrayList<Weaponset>();
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
		}
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
		WeaponsetListPtr weaponsetListPtr = null;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
		try {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
			weaponsetListPtr = Flib.INSTANCE.flib_weaponsetlist_from_ini(weaponsetFile.getAbsolutePath());
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
			if(weaponsetListPtr == null) {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
				throw new IOException("Unable to read weaponsets from "+weaponsetFile);
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
			}
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
			return weaponsetListPtr.deref();
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
		} finally {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
			if(weaponsetListPtr != null) {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
				Flib.INSTANCE.flib_weaponsetlist_destroy(weaponsetListPtr);
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
			}
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
		}
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
	}
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
	
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
	public static void saveUserWeaponsets(Context c, List<Weaponset> weaponsets) throws IOException {
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
		WeaponsetListPtr ptr = WeaponsetListPtr.createJavaOwned(weaponsets);
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
		Flib.INSTANCE.flib_weaponsetlist_to_ini(getUserWeaponsetsFile(c).getAbsolutePath(), ptr);
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
	}
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    83
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    84
	public static void deleteUserWeaponset(Context c, String setToDelete) throws IOException {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    85
		List<Weaponset> userWeaponsets = loadUserWeaponsets(c);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    86
		for(Iterator<Weaponset> iter = userWeaponsets.iterator(); iter.hasNext();) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    87
			Weaponset set = iter.next();
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    88
			if(set.name.equals(setToDelete)) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    89
				iter.remove();
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    90
				break;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    91
			}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    92
		}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    93
		saveUserWeaponsets(c, userWeaponsets);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    94
	}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    95
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    96
	public static List<String> toNameList(List<Weaponset> weaponsets) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    97
		List<String> result = new ArrayList<String>();
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    98
		for(Weaponset weaponset : weaponsets) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    99
			result.add(weaponset.name);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   100
		}
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   101
		return result;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   102
	}
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
   103
}