1 package org.hedgewars.hedgeroid.Datastructures; |
1 package org.hedgewars.hedgeroid.Datastructures; |
2 |
2 |
3 import java.io.File; |
3 import java.io.File; |
4 import java.io.IOException; |
4 import java.io.IOException; |
5 import java.util.ArrayList; |
5 import java.util.ArrayList; |
6 import java.util.Collections; |
|
7 import java.util.List; |
6 import java.util.List; |
8 import java.util.Map; |
|
9 import java.util.TreeMap; |
|
10 |
7 |
11 import org.hedgewars.hedgeroid.Utils; |
|
12 import org.hedgewars.hedgeroid.frontlib.Flib; |
8 import org.hedgewars.hedgeroid.frontlib.Flib; |
13 import org.hedgewars.hedgeroid.frontlib.Frontlib.MetaschemePtr; |
|
14 import org.hedgewars.hedgeroid.frontlib.Frontlib.SchemelistPtr; |
9 import org.hedgewars.hedgeroid.frontlib.Frontlib.SchemelistPtr; |
15 |
10 |
16 import android.content.Context; |
11 import android.content.Context; |
17 |
12 |
18 /** |
13 /** |
30 |
25 |
31 public static File getBuiltinSchemesFile(Context c) { |
26 public static File getBuiltinSchemesFile(Context c) { |
32 return new File(c.getFilesDir(), "schemes_builtin.ini"); |
27 return new File(c.getFilesDir(), "schemes_builtin.ini"); |
33 } |
28 } |
34 |
29 |
35 public static Map<String, Scheme> loadAllSchemes(Context c) throws IOException { |
30 public static List<Scheme> loadAllSchemes(Context c) throws IOException { |
36 Map<String, Scheme> result = loadUserSchemes(c); |
31 List<Scheme> result = loadBuiltinSchemes(c); |
37 result.putAll(loadBuiltinSchemes(c)); |
32 result.addAll(loadUserSchemes(c)); |
38 return result; |
33 return result; |
39 } |
34 } |
40 |
35 |
41 public static Map<String, Scheme> loadUserSchemes(Context c) throws IOException { |
36 public static List<Scheme> loadUserSchemes(Context c) throws IOException { |
42 return loadSchemes(c, getUserSchemesFile(c)); |
37 return loadSchemes(c, getUserSchemesFile(c)); |
43 } |
38 } |
44 |
39 |
45 public static Map<String, Scheme> loadBuiltinSchemes(Context c) throws IOException { |
40 public static List<Scheme> loadBuiltinSchemes(Context c) throws IOException { |
46 return loadSchemes(c, getBuiltinSchemesFile(c)); |
41 return loadSchemes(c, getBuiltinSchemesFile(c)); |
47 } |
42 } |
48 |
43 |
49 public static Map<String, Scheme> loadSchemes(Context c, File schemeFile) throws IOException { |
44 public static List<Scheme> loadSchemes(Context c, File schemeFile) throws IOException { |
50 Map<String, Scheme> result = new TreeMap<String, Scheme>(); |
|
51 String metaschemePath = new File(Utils.getDataPathFile(c), "metasettings.ini").getAbsolutePath(); |
|
52 if(!schemeFile.isFile()) { |
45 if(!schemeFile.isFile()) { |
53 // No schemes file == no schemes, no error |
46 // No schemes file == no schemes, no error |
54 return new TreeMap<String, Scheme>(); |
47 return new ArrayList<Scheme>(); |
55 } |
48 } |
56 MetaschemePtr meta = null; |
|
57 SchemelistPtr schemeListPtr = null; |
49 SchemelistPtr schemeListPtr = null; |
58 try { |
50 try { |
59 meta = Flib.INSTANCE.flib_metascheme_from_ini(metaschemePath); |
51 schemeListPtr = Flib.INSTANCE.flib_schemelist_from_ini(schemeFile.getAbsolutePath()); |
60 if(meta==null) { |
|
61 throw new IOException("Unable to read metascheme"); |
|
62 } |
|
63 schemeListPtr = Flib.INSTANCE.flib_schemelist_from_ini(meta, schemeFile.getAbsolutePath()); |
|
64 if(schemeListPtr == null) { |
52 if(schemeListPtr == null) { |
65 throw new IOException("Unable to read schemelist"); |
53 throw new IOException("Unable to read schemelist"); |
66 } |
54 } |
67 List<Scheme> schemeList = schemeListPtr.deref(); |
55 return schemeListPtr.deref(); |
68 for(Scheme scheme : schemeList) { |
|
69 result.put(scheme.name, scheme); |
|
70 } |
|
71 return result; |
|
72 } finally { |
56 } finally { |
73 if(schemeListPtr != null) { |
57 if(schemeListPtr != null) { |
74 Flib.INSTANCE.flib_schemelist_destroy(schemeListPtr); |
58 Flib.INSTANCE.flib_schemelist_destroy(schemeListPtr); |
75 } |
59 } |
76 if(meta != null) { |
|
77 Flib.INSTANCE.flib_metascheme_release(meta); |
|
78 } |
|
79 } |
60 } |
80 } |
61 } |
81 |
62 |
82 public static void saveUserSchemes(Context c, Map<String, Scheme> schemes) throws IOException { |
63 public static void saveUserSchemes(Context c, List<Scheme> schemes) throws IOException { |
83 List<Scheme> schemeList = new ArrayList<Scheme>(schemes.values()); |
64 SchemelistPtr ptr = SchemelistPtr.createJavaOwned(schemes); |
84 Collections.sort(schemeList, Scheme.caseInsensitiveNameComparator); |
|
85 SchemelistPtr ptr = SchemelistPtr.createJavaOwned(schemeList); |
|
86 Flib.INSTANCE.flib_schemelist_to_ini(getUserSchemesFile(c).getAbsolutePath(), ptr); |
65 Flib.INSTANCE.flib_schemelist_to_ini(getUserSchemesFile(c).getAbsolutePath(), ptr); |
87 } |
66 } |
|
67 |
|
68 public static List<String> toNameList(List<Scheme> schemes) { |
|
69 List<String> result = new ArrayList<String>(); |
|
70 for(Scheme scheme : schemes) { |
|
71 result.add(scheme.name); |
|
72 } |
|
73 return result; |
|
74 } |
88 } |
75 } |