project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Datastructures/FrontendDataUtils.java
author Medo <smaxein@googlemail.com>
Sun, 12 Aug 2012 22:46:23 +0200
changeset 7485 0481bd74267c
parent 7476 2fb781bbdd51
child 7508 763d3961400b
permissions -rw-r--r--
Hedgeroid: - (hopefully) completed the frontlib JNA mappings - added documentation - Changed more code to use frontlib for ini reading/writing - tried to make everything work again that was working before - Teamlist can now be used to add and remove teams - Netplay now correctly handles team additions when being room chief - Fixed TeamCreatorActivity so that editing teams works

/*
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */


package org.hedgewars.hedgeroid.Datastructures;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.hedgewars.hedgeroid.R;
import org.hedgewars.hedgeroid.Utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class FrontendDataUtils {

	/**
	 * @throws FileNotFoundException if the sdcard isn't available or the Maps directory doesn't exist
	 */
	public static ArrayList<MapFile> getMaps(Context c) throws FileNotFoundException {
		File[] files = Utils.getFilesFromRelativeDir(c,"Maps");
		ArrayList<MapFile> ret = new ArrayList<MapFile>();

		for(File f : files) {
			boolean isMission = Utils.hasFileWithSuffix(f, ".lua");
			ret.add(new MapFile(f.getName(), isMission));
		}
		Collections.sort(ret, MapFile.MISSIONS_FIRST_NAME_ORDER);

		return ret;
	}

	/**
	 * Returns a list of all multiplayer scripts (game styles)
	 * @throws FileNotFoundException if the sdcard isn't available or the Scripts/Multiplayer directory doesn't exist
	 */
	public static List<String> getGameStyles(Context c) throws FileNotFoundException {
		File[] files = Utils.getFilesFromRelativeDir(c, "Scripts/Multiplayer");
		ArrayList<String> ret = new ArrayList<String>();
		/*
		 * Caution: It is important that the "empty" style has this exact name, because
		 * it will be interpreted as "don't load a script" by the frontlib, and also by
		 * the QtFrontend in a netgame. This should probably be improved some time
		 * (maybe TODO add a dummy script called "Normal" to the MP scripts?) 
		 */
		ret.add("Normal");
		for(int i = 0; i < files.length; i++) {
			String name = files[i].getName();
			if(name.endsWith(".lua")){
				//replace _ by a space and removed the last four characters (.lua)
				ret.add(name.replace('_', ' ').substring(0, name.length()-4));
			}
		}
		Collections.sort(ret, String.CASE_INSENSITIVE_ORDER);
		return ret;
	}

	/**
	 * @throws FileNotFoundException if the sdcard isn't available or the Themes directory doesn't exist
	 */
	public static List<String> getThemes(Context c) throws FileNotFoundException {
		List<String> list = Utils.getDirsWithFileSuffix(c, "Themes", "icon.png");
		Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
		return list;
	}

	public static List<Weaponset> getWeaponsets(Context c) {
		// TODO stub, re-implement
		/*List<Weapon> list = Weapon.getWeapons(c);
		Collections.sort(list);*/
		return Collections.emptyList();
	}

	/**
	 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Graves directory doesn't exist
	 */
	public static ArrayList<HashMap<String, ?>> getGraves(Context c) throws FileNotFoundException {
		File gravePath = new File(new File(Utils.getDataPathFile(c), "Graphics"), "Graves");
		ArrayList<String> names = Utils.getFileNamesFromDirWithSuffix(c,"Graphics/Graves", ".png", true);
		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size());

		for(String s : names){
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("txt", s);
			Bitmap b = BitmapFactory.decodeFile(new File(gravePath, s + ".png").getAbsolutePath());
			int width = b.getWidth();
			if(b.getHeight() > width){//some pictures contain more 'frames' underneath each other, if so we only use the first frame
                                Bitmap tmp = Bitmap.createBitmap(width, width, b.getConfig());
                                int[] pixels = new int[width * width];
                                b.getPixels(pixels, 0,width,0,0,width,width);
				tmp.setPixels(pixels,0,width,0,0,width,width);
                                b.recycle();
				b = tmp;
			}
			map.put("img", b);
			data.add(map);
		}
		return data;
	}

	/**
	 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Graves directory doesn't exist
	 */
	public static ArrayList<HashMap<String, ?>> getFlags(Context c) throws FileNotFoundException {
		File flagsPath = new File(new File(Utils.getDataPathFile(c), "Graphics"), "Flags");
		ArrayList<String> names = Utils.getFileNamesFromDirWithSuffix(c, "Graphics/Flags", ".png", true);
		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size());

		for(String s : names){
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("txt", s);
			Bitmap b = BitmapFactory.decodeFile(new File(flagsPath, s + ".png").getAbsolutePath());
			map.put("img", b);
			data.add(map);
		}
		return data;
	}

	/**
	 * @throws FileNotFoundException if the sdcard isn't available or the Sounds/voices directory doesn't exist
	 */
	public static ArrayList<String> getVoices(Context c) throws FileNotFoundException {
		File[] files = Utils.getFilesFromRelativeDir(c, "Sounds/voices");
		ArrayList<String> ret = new ArrayList<String>();

		for(File f : files){
			if(f.isDirectory()) ret.add(f.getName());
		}
		return ret;
	}

	/**
	 * @throws FileNotFoundException if the sdcard isn't available or the Forts directory doesn't exist
	 */
	public static ArrayList<String> getForts(Context c) throws FileNotFoundException {
		return Utils.getFileNamesFromDirWithSuffix(c,"Forts", "L.png", true);
	}
	
	// TODO wat
	public static ArrayList<HashMap<String, ?>> getTypes(Context c){
		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(6);
		String[] levels = {c.getString(R.string.human), c.getString(R.string.bot5), c.getString(R.string.bot4), c.getString(R.string.bot3), c.getString(R.string.bot2), c.getString(R.string.bot1)};
		int[] images = {R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1};

		for(int i = 0; i < levels.length; i++){
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("txt", levels[i]);
			map.put("img", images[i]);
			data.add(map);
		}

		return data;
	}

	/**
	 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Hats directory doesn't exist
	 */
	public static ArrayList<HashMap<String, ?>> getHats(Context c) throws FileNotFoundException {
		ArrayList<String> files = Utils.getFileNamesFromDirWithSuffix(c,"Graphics/Hats", ".png", true);
		File hatsPath = new File(new File(Utils.getDataPathFile(c), "Graphics"), "Hats");
		int size = files.size();
		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(size);

		HashMap<String, Object> hashmap; 
		for(String s : files){
			hashmap = new HashMap<String, Object>();
			hashmap.put("txt", s);
			Bitmap b = BitmapFactory.decodeFile(new File(hatsPath, s + ".png").getAbsolutePath());
			b = Bitmap.createBitmap(b, 0,0,b.getWidth()/2, b.getWidth()/2);
			hashmap.put("img", b);
			data.add(hashmap);
		}

		return data;
	}

	public static List<Team> getTeams(Context c) {
		List<Team> ret = new ArrayList<Team>();
		
		File teamsDir = new File(c.getFilesDir(), Team.DIRECTORY_TEAMS);
		File[] teamFileNames = teamsDir.listFiles();
		if(teamFileNames != null){
			for(File file : teamFileNames){
				Team team = Team.load(file);
				if(team != null){
					ret.add(team);
				}
			}
		}
		return ret;
	}
}