author | Wuzzy <almikes@aol.com> |
Thu, 12 May 2016 16:20:17 +0200 | |
changeset 12196 | d7ccd18ae7bf |
parent 7584 | 7831c84cc644 |
permissions | -rw-r--r-- |
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 | 20 |
package org.hedgewars.hedgeroid.Datastructures; |
21 |
||
22 |
import java.io.File; |
|
23 |
import java.io.IOException; |
|
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 | 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 |
} |
|
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 | 103 |
} |