project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Datastructures/Schemes.java
author Medo <smaxein@googlemail.com>
Mon, 06 Aug 2012 22:39:36 +0200
changeset 7476 2fb781bbdd51
child 7485 0481bd74267c
permissions -rw-r--r--
Hedgeroid: Start using the frontlib for more operations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
package org.hedgewars.hedgeroid.Datastructures;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
import java.io.File;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
import java.io.IOException;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
import java.util.ArrayList;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
import java.util.Collections;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
import java.util.List;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
import java.util.Map;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
import java.util.TreeMap;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
import org.hedgewars.hedgeroid.Utils;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
import org.hedgewars.hedgeroid.frontlib.Flib;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
import org.hedgewars.hedgeroid.frontlib.Frontlib.MetaschemePtr;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
import org.hedgewars.hedgeroid.frontlib.Frontlib.SchemelistPtr;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
import android.content.Context;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
/**
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
 * Functions for handling the persistent list of schemes.
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
 * Schemes in that list are identified by name (case sensitive).
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
 */
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
public final class Schemes {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
	private Schemes() {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
		throw new AssertionError("This class is not meant to be instantiated");
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
	}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
	
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
	public static File getUserSchemesFile(Context c) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
		return new File(c.getFilesDir(), "schemes_user.ini");
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
	}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
	
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
	public static File getBuiltinSchemesFile(Context c) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
		return new File(c.getFilesDir(), "schemes_builtin.ini");
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
	}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
	
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
	public static Map<String, Scheme> loadUserSchemes(Context c) throws IOException {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
		return loadSchemes(c, getUserSchemesFile(c));
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
	}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
	
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
	public static Map<String, Scheme> loadBuiltinSchemes(Context c) throws IOException {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
		return loadSchemes(c, getBuiltinSchemesFile(c));
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
	}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
	public static Map<String, Scheme> loadSchemes(Context c, File schemeFile) throws IOException {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
		Map<String, Scheme> result = new TreeMap<String, Scheme>();
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
		String metaschemePath = new File(Utils.getDataPathFile(c), "metasettings.ini").getAbsolutePath();
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
		if(!schemeFile.isFile()) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
			// No schemes file == no schemes, no error
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
			return new TreeMap<String, Scheme>();
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
		}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
		MetaschemePtr meta = null;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
		SchemelistPtr schemeListPtr = null;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
		try {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
			meta = Flib.INSTANCE.flib_metascheme_from_ini(metaschemePath);
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
			if(meta==null) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
				throw new IOException("Unable to read metascheme");
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
			}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
			schemeListPtr = Flib.INSTANCE.flib_schemelist_from_ini(meta, schemeFile.getAbsolutePath());
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
			if(schemeListPtr == null) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
				throw new IOException("Unable to read schemelist");
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
			}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
			List<Scheme> schemeList = schemeListPtr.deref();
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
			for(Scheme scheme : schemeList) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
				result.put(scheme.name, scheme);
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
			}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
			return result;
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
		} finally {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
			if(schemeListPtr != null) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
				Flib.INSTANCE.flib_schemelist_destroy(schemeListPtr);
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
			}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
			if(meta != null) {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
				Flib.INSTANCE.flib_metascheme_release(meta);
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
			}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
		}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
	}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
	
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
	public static void saveUserSchemes(Context c, Map<String, Scheme> schemes) throws IOException {
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
		List<Scheme> schemeList = new ArrayList<Scheme>(schemes.values());
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
		Collections.sort(schemeList, Scheme.caseInsensitiveNameComparator);
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
		SchemelistPtr ptr = SchemelistPtr.createJavaOwned(schemeList);
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
		Flib.INSTANCE.flib_schemelist_to_ini(getUserSchemesFile(c).getAbsolutePath(), ptr);
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
	}
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
}