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