project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Datastructures/FrontendDataUtils.java
changeset 7485 0481bd74267c
parent 7476 2fb781bbdd51
child 7508 763d3961400b
equal deleted inserted replaced
7482:d70a5b0d1190 7485:0481bd74267c
    18 
    18 
    19 
    19 
    20 package org.hedgewars.hedgeroid.Datastructures;
    20 package org.hedgewars.hedgeroid.Datastructures;
    21 
    21 
    22 import java.io.File;
    22 import java.io.File;
       
    23 import java.io.FileNotFoundException;
    23 import java.util.ArrayList;
    24 import java.util.ArrayList;
    24 import java.util.Collections;
    25 import java.util.Collections;
    25 import java.util.HashMap;
    26 import java.util.HashMap;
    26 import java.util.List;
    27 import java.util.List;
    27 
    28 
    28 import org.hedgewars.hedgeroid.R;
    29 import org.hedgewars.hedgeroid.R;
    29 import org.hedgewars.hedgeroid.StartGameActivity;
       
    30 import org.hedgewars.hedgeroid.Utils;
    30 import org.hedgewars.hedgeroid.Utils;
    31 import org.hedgewars.hedgeroid.Datastructures.Map.MapType;
       
    32 
    31 
    33 import android.content.Context;
    32 import android.content.Context;
    34 import android.graphics.Bitmap;
    33 import android.graphics.Bitmap;
    35 import android.graphics.BitmapFactory;
    34 import android.graphics.BitmapFactory;
    36 
    35 
    37 public class FrontendDataUtils {
    36 public class FrontendDataUtils {
    38 
    37 
    39 
    38 	/**
    40 	public static ArrayList<Map> getMaps(Context c){
    39 	 * @throws FileNotFoundException if the sdcard isn't available or the Maps directory doesn't exist
       
    40 	 */
       
    41 	public static ArrayList<MapFile> getMaps(Context c) throws FileNotFoundException {
    41 		File[] files = Utils.getFilesFromRelativeDir(c,"Maps");
    42 		File[] files = Utils.getFilesFromRelativeDir(c,"Maps");
    42 		ArrayList<Map> ret = new ArrayList<Map>();
    43 		ArrayList<MapFile> ret = new ArrayList<MapFile>();
    43 
    44 
    44 		for(File f : files){
    45 		for(File f : files) {
    45 			if(Utils.hasFileWithSuffix(f, ".lua")){
    46 			boolean isMission = Utils.hasFileWithSuffix(f, ".lua");
    46 				ret.add(new Map(f,MapType.TYPE_MISSION, c));
    47 			ret.add(new MapFile(f.getName(), isMission));
    47 			}else{
    48 		}
    48 				ret.add(new Map(f, MapType.TYPE_DEFAULT,c));
    49 		Collections.sort(ret, MapFile.MISSIONS_FIRST_NAME_ORDER);
       
    50 
       
    51 		return ret;
       
    52 	}
       
    53 
       
    54 	/**
       
    55 	 * Returns a list of all multiplayer scripts (game styles)
       
    56 	 * @throws FileNotFoundException if the sdcard isn't available or the Scripts/Multiplayer directory doesn't exist
       
    57 	 */
       
    58 	public static List<String> getGameStyles(Context c) throws FileNotFoundException {
       
    59 		File[] files = Utils.getFilesFromRelativeDir(c, "Scripts/Multiplayer");
       
    60 		ArrayList<String> ret = new ArrayList<String>();
       
    61 		/*
       
    62 		 * Caution: It is important that the "empty" style has this exact name, because
       
    63 		 * it will be interpreted as "don't load a script" by the frontlib, and also by
       
    64 		 * the QtFrontend in a netgame. This should probably be improved some time
       
    65 		 * (maybe TODO add a dummy script called "Normal" to the MP scripts?) 
       
    66 		 */
       
    67 		ret.add("Normal");
       
    68 		for(int i = 0; i < files.length; i++) {
       
    69 			String name = files[i].getName();
       
    70 			if(name.endsWith(".lua")){
       
    71 				//replace _ by a space and removed the last four characters (.lua)
       
    72 				ret.add(name.replace('_', ' ').substring(0, name.length()-4));
    49 			}
    73 			}
    50 		}
    74 		}
    51 		Collections.sort(ret);
    75 		Collections.sort(ret, String.CASE_INSENSITIVE_ORDER);
    52 
    76 		return ret;
    53 		return ret;
    77 	}
    54 	}
    78 
    55 
    79 	/**
    56 	public static List<String> getGameplay(Context c){
    80 	 * @throws FileNotFoundException if the sdcard isn't available or the Themes directory doesn't exist
    57 		String[] files = Utils.getFileNamesFromRelativeDir(c, "Scripts/Multiplayer");
    81 	 */
    58 		ArrayList<String> ret = new ArrayList<String>();
    82 	public static List<String> getThemes(Context c) throws FileNotFoundException {
    59 		
       
    60 		for(int i = 0; i < files.length; i++){
       
    61 			if(files[i].endsWith(".lua")){
       
    62 				ret.add(files[i].replace('_', ' ').substring(0, files[i].length()-4)); //replace _ by a space and removed the last four characters (.lua)
       
    63 			}
       
    64 		}
       
    65 		ret.add(0,"None");
       
    66 		Collections.sort(ret);
       
    67 		return ret;	
       
    68 	}
       
    69 
       
    70 	public static List<String> getThemes(Context c){
       
    71 		List<String> list = Utils.getDirsWithFileSuffix(c, "Themes", "icon.png");
    83 		List<String> list = Utils.getDirsWithFileSuffix(c, "Themes", "icon.png");
    72 		Collections.sort(list);
    84 		Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
    73 		return list;
    85 		return list;
    74 	}
    86 	}
    75 
    87 
    76 	public static List<Weapon> getWeapons(Context c){
    88 	public static List<Weaponset> getWeaponsets(Context c) {
    77 		// TODO stub, re-implement
    89 		// TODO stub, re-implement
    78 		/*List<Weapon> list = Weapon.getWeapons(c);
    90 		/*List<Weapon> list = Weapon.getWeapons(c);
    79 		Collections.sort(list);*/
    91 		Collections.sort(list);*/
    80 		return Collections.emptyList();
    92 		return Collections.emptyList();
    81 	}
    93 	}
    82 
    94 
    83 	public static ArrayList<HashMap<String, ?>> getGraves(Context c){
    95 	/**
    84 		String pathPrefix = Utils.getDataPath(c) + "Graphics/Graves/";
    96 	 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Graves directory doesn't exist
    85 		ArrayList<String> names = Utils.getFilesFromDirWithSuffix(c,"Graphics/Graves", ".png", true);
    97 	 */
       
    98 	public static ArrayList<HashMap<String, ?>> getGraves(Context c) throws FileNotFoundException {
       
    99 		File gravePath = new File(new File(Utils.getDataPathFile(c), "Graphics"), "Graves");
       
   100 		ArrayList<String> names = Utils.getFileNamesFromDirWithSuffix(c,"Graphics/Graves", ".png", true);
    86 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size());
   101 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size());
    87 
   102 
    88 		for(String s : names){
   103 		for(String s : names){
    89 			HashMap<String, Object> map = new HashMap<String, Object>();
   104 			HashMap<String, Object> map = new HashMap<String, Object>();
    90 			map.put("txt", s);
   105 			map.put("txt", s);
    91 			Bitmap b = BitmapFactory.decodeFile(pathPrefix + s + ".png");//create a full path - decode to to a bitmap
   106 			Bitmap b = BitmapFactory.decodeFile(new File(gravePath, s + ".png").getAbsolutePath());
    92 			int width = b.getWidth();
   107 			int width = b.getWidth();
    93 			if(b.getHeight() > width){//some pictures contain more 'frames' underneath each other, if so we only use the first frame
   108 			if(b.getHeight() > width){//some pictures contain more 'frames' underneath each other, if so we only use the first frame
    94                                 Bitmap tmp = Bitmap.createBitmap(width, width, b.getConfig());
   109                                 Bitmap tmp = Bitmap.createBitmap(width, width, b.getConfig());
    95                                 int[] pixels = new int[width * width];
   110                                 int[] pixels = new int[width * width];
    96                                 b.getPixels(pixels, 0,width,0,0,width,width);
   111                                 b.getPixels(pixels, 0,width,0,0,width,width);
   102 			data.add(map);
   117 			data.add(map);
   103 		}
   118 		}
   104 		return data;
   119 		return data;
   105 	}
   120 	}
   106 
   121 
   107 	public static ArrayList<HashMap<String, ?>> getFlags(Context c){
   122 	/**
   108 		String pathPrefix = Utils.getDataPath(c) + "Graphics/Flags/";
   123 	 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Graves directory doesn't exist
   109 		ArrayList<String> names = Utils.getFilesFromDirWithSuffix(c, "Graphics/Flags", ".png", true);
   124 	 */
       
   125 	public static ArrayList<HashMap<String, ?>> getFlags(Context c) throws FileNotFoundException {
       
   126 		File flagsPath = new File(new File(Utils.getDataPathFile(c), "Graphics"), "Flags");
       
   127 		ArrayList<String> names = Utils.getFileNamesFromDirWithSuffix(c, "Graphics/Flags", ".png", true);
   110 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size());
   128 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size());
   111 
   129 
   112 		for(String s : names){
   130 		for(String s : names){
   113 			HashMap<String, Object> map = new HashMap<String, Object>();
   131 			HashMap<String, Object> map = new HashMap<String, Object>();
   114 			map.put("txt", s);
   132 			map.put("txt", s);
   115 			Bitmap b = BitmapFactory.decodeFile(pathPrefix + s + ".png");//create a full path - decode to to a bitmap
   133 			Bitmap b = BitmapFactory.decodeFile(new File(flagsPath, s + ".png").getAbsolutePath());
   116 			map.put("img", b);
   134 			map.put("img", b);
   117 			data.add(map);
   135 			data.add(map);
   118 		}
   136 		}
   119 		return data;
   137 		return data;
   120 	}
   138 	}
   121 
   139 
   122 	public static ArrayList<String> getVoices(Context c){
   140 	/**
       
   141 	 * @throws FileNotFoundException if the sdcard isn't available or the Sounds/voices directory doesn't exist
       
   142 	 */
       
   143 	public static ArrayList<String> getVoices(Context c) throws FileNotFoundException {
   123 		File[] files = Utils.getFilesFromRelativeDir(c, "Sounds/voices");
   144 		File[] files = Utils.getFilesFromRelativeDir(c, "Sounds/voices");
   124 		ArrayList<String> ret = new ArrayList<String>();
   145 		ArrayList<String> ret = new ArrayList<String>();
   125 
   146 
   126 		for(File f : files){
   147 		for(File f : files){
   127 			if(f.isDirectory()) ret.add(f.getName());
   148 			if(f.isDirectory()) ret.add(f.getName());
   128 		}
   149 		}
   129 		return ret;
   150 		return ret;
   130 	}
   151 	}
   131 
   152 
   132 	public static ArrayList<String> getForts(Context c){
   153 	/**
   133 		return Utils.getFilesFromDirWithSuffix(c,"Forts", "L.png", true);
   154 	 * @throws FileNotFoundException if the sdcard isn't available or the Forts directory doesn't exist
   134 	}
   155 	 */
       
   156 	public static ArrayList<String> getForts(Context c) throws FileNotFoundException {
       
   157 		return Utils.getFileNamesFromDirWithSuffix(c,"Forts", "L.png", true);
       
   158 	}
       
   159 	
       
   160 	// TODO wat
   135 	public static ArrayList<HashMap<String, ?>> getTypes(Context c){
   161 	public static ArrayList<HashMap<String, ?>> getTypes(Context c){
   136 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(6);
   162 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(6);
   137 		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)};
   163 		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)};
   138 		int[] images = {R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1};
   164 		int[] images = {R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1};
   139 
   165 
   145 		}
   171 		}
   146 
   172 
   147 		return data;
   173 		return data;
   148 	}
   174 	}
   149 
   175 
   150 	public static ArrayList<HashMap<String, ?>> getHats(Context c){
   176 	/**
   151 		ArrayList<String> files = Utils.getFilesFromDirWithSuffix(c,"Graphics/Hats", ".png", true);
   177 	 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Hats directory doesn't exist
   152 		String pathPrefix = Utils.getDataPath(c) + "Graphics/Hats/";
   178 	 */
       
   179 	public static ArrayList<HashMap<String, ?>> getHats(Context c) throws FileNotFoundException {
       
   180 		ArrayList<String> files = Utils.getFileNamesFromDirWithSuffix(c,"Graphics/Hats", ".png", true);
       
   181 		File hatsPath = new File(new File(Utils.getDataPathFile(c), "Graphics"), "Hats");
   153 		int size = files.size();
   182 		int size = files.size();
   154 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(size);
   183 		ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(size);
   155 
   184 
   156 		HashMap<String, Object> hashmap; 
   185 		HashMap<String, Object> hashmap; 
   157 		for(String s : files){
   186 		for(String s : files){
   158 			hashmap = new HashMap<String, Object>();
   187 			hashmap = new HashMap<String, Object>();
   159 			hashmap.put("txt", s);
   188 			hashmap.put("txt", s);
   160 			Bitmap b = BitmapFactory.decodeFile(pathPrefix + s + ".png");//create a full path - decode to to a bitmap
   189 			Bitmap b = BitmapFactory.decodeFile(new File(hatsPath, s + ".png").getAbsolutePath());
   161 			b = Bitmap.createBitmap(b, 0,0,b.getWidth()/2, b.getWidth()/2);
   190 			b = Bitmap.createBitmap(b, 0,0,b.getWidth()/2, b.getWidth()/2);
   162 			hashmap.put("img", b);
   191 			hashmap.put("img", b);
   163 			data.add(hashmap);
   192 			data.add(hashmap);
   164 		}
   193 		}
   165 
   194 
   166 		return data;
   195 		return data;
   167 	}
   196 	}
   168 
   197 
   169 	public static List<TeamFile> getTeamFiles(Context c) {
   198 	public static List<Team> getTeams(Context c) {
   170 		List<TeamFile> ret = new ArrayList<TeamFile>();
   199 		List<Team> ret = new ArrayList<Team>();
   171 		
   200 		
   172 		File teamsDir = new File(c.getFilesDir(), Team.DIRECTORY_TEAMS);
   201 		File teamsDir = new File(c.getFilesDir(), Team.DIRECTORY_TEAMS);
   173 		File[] teamFileNames = teamsDir.listFiles();
   202 		File[] teamFileNames = teamsDir.listFiles();
   174 		if(teamFileNames != null){
   203 		if(teamFileNames != null){
   175 			for(File file : teamFileNames){
   204 			for(File file : teamFileNames){
   176 				Team team = Team.load(file);
   205 				Team team = Team.load(file);
   177 				if(team != null){
   206 				if(team != null){
   178 					ret.add(new TeamFile(team, file));
   207 					ret.add(team);
   179 				}
   208 				}
   180 			}
   209 			}
   181 		}
   210 		}
   182 		return ret;
   211 		return ret;
   183 	}
   212 	}
   184 
       
   185 	public static Scheme[] getSchemes(StartGameActivity startGameActivity) {
       
   186 		// TODO Auto-generated method stub
       
   187 		return null;
       
   188 	}
       
   189 }
   213 }