1 /* 2 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game 3 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com> 4 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 */ 20 21 package org.hedgewars.hedgeroid.Datastructures; 22 23 import java.io.File; 24 import java.io.FileNotFoundException; 25 import java.util.ArrayList; 26 import java.util.HashMap; 27 import java.util.List; 28 import java.util.Map; 29 30 import org.hedgewars.hedgeroid.R; 31 import org.hedgewars.hedgeroid.util.FileUtils; 32 33 import android.content.Context; 34 import android.graphics.Bitmap; 35 import android.graphics.BitmapFactory; 36 37 public class FrontendDataUtils { 38 39 /** 40 * @throws FileNotFoundException if the sdcard isn't available or the Maps directory doesn't exist 41 */ getMaps(Context c)42 public static List<MapFile> getMaps(Context c) throws FileNotFoundException { 43 File[] files = FileUtils.getFilesFromRelativeDir(c,"Maps"); 44 List<MapFile> ret = new ArrayList<MapFile>(); 45 46 for(File f : files) { 47 boolean isMission = FileUtils.hasFileWithSuffix(f, ".lua"); 48 ret.add(new MapFile(f.getName(), isMission)); 49 } 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 */ getGameStyles(Context c)58 public static List<String> getGameStyles(Context c) throws FileNotFoundException { 59 File[] files = FileUtils.getFilesFromRelativeDir(c, "Scripts/Multiplayer"); 60 List<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)); 73 } 74 } 75 return ret; 76 } 77 78 /** 79 * @throws FileNotFoundException if the sdcard isn't available or the Themes directory doesn't exist 80 */ getThemes(Context c)81 public static List<String> getThemes(Context c) throws FileNotFoundException { 82 return FileUtils.getDirsWithFileSuffix(c, "Themes", "icon.png"); 83 } 84 85 /** 86 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Graves directory doesn't exist 87 */ getGraves(Context c)88 public static List<Map<String, ?>> getGraves(Context c) throws FileNotFoundException { 89 File gravePath = FileUtils.getDataPathFile(c, "Graphics", "Graves"); 90 List<String> names = FileUtils.getFileNamesFromDirWithSuffix(c,"Graphics/Graves", ".png", true); 91 List<Map<String, ?>> data = new ArrayList<Map<String, ?>>(names.size()); 92 93 for(String s : names){ 94 HashMap<String, Object> map = new HashMap<String, Object>(); 95 map.put("txt", s); 96 Bitmap b = BitmapFactory.decodeFile(new File(gravePath, s + ".png").getAbsolutePath()); 97 int width = b.getWidth(); 98 if(b.getHeight() > width){ 99 // some pictures contain more 'frames' underneath each other, if so we only use the first frame 100 b = Bitmap.createBitmap(b, 0, 0, width, width); 101 } 102 map.put("img", b); 103 data.add(map); 104 } 105 return data; 106 } 107 108 /** 109 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Graves directory doesn't exist 110 */ getFlags(Context c)111 public static List<Map<String, ?>> getFlags(Context c) throws FileNotFoundException { 112 File flagsPath = FileUtils.getDataPathFile(c, "Graphics", "Flags"); 113 List<String> names = FileUtils.getFileNamesFromDirWithSuffix(c, "Graphics/Flags", ".png", true); 114 List<Map<String, ?>> data = new ArrayList<Map<String, ?>>(names.size()); 115 116 for(String s : names){ 117 Map<String, Object> map = new HashMap<String, Object>(); 118 map.put("txt", s); 119 Bitmap b = BitmapFactory.decodeFile(new File(flagsPath, s + ".png").getAbsolutePath()); 120 map.put("img", b); 121 data.add(map); 122 } 123 return data; 124 } 125 126 /** 127 * @throws FileNotFoundException if the sdcard isn't available or the Sounds/voices directory doesn't exist 128 */ getVoices(Context c)129 public static List<String> getVoices(Context c) throws FileNotFoundException { 130 File[] files = FileUtils.getFilesFromRelativeDir(c, "Sounds/voices"); 131 List<String> ret = new ArrayList<String>(); 132 133 for(File f : files){ 134 if(f.isDirectory()) ret.add(f.getName()); 135 } 136 return ret; 137 } 138 139 /** 140 * @throws FileNotFoundException if the sdcard isn't available or the Forts directory doesn't exist 141 */ getForts(Context c)142 public static List<String> getForts(Context c) throws FileNotFoundException { 143 return FileUtils.getFileNamesFromDirWithSuffix(c,"Forts", "L.png", true); 144 } 145 getTypes(Context c)146 public static List<Map<String, ?>> getTypes(Context c){ 147 List<Map<String, ?>> data = new ArrayList<Map<String, ?>>(6); 148 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)}; 149 int[] images = {R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1}; 150 151 for(int i = 0; i < levels.length; i++){ 152 Map<String, Object> map = new HashMap<String, Object>(); 153 map.put("txt", levels[i]); 154 map.put("img", images[i]); 155 map.put("level", i); 156 157 data.add(map); 158 } 159 160 return data; 161 } 162 163 /** 164 * @throws FileNotFoundException if the sdcard isn't available or the Graphics/Hats directory doesn't exist 165 */ getHats(Context c)166 public static List<Map<String, ?>> getHats(Context c) throws FileNotFoundException { 167 List<String> files = FileUtils.getFileNamesFromDirWithSuffix(c,"Graphics/Hats", ".png", true); 168 File hatsPath = FileUtils.getDataPathFile(c, "Graphics", "Hats"); 169 int size = files.size(); 170 List<Map<String, ?>> data = new ArrayList<Map<String, ?>>(size); 171 172 for(String s : files){ 173 Map<String, Object> hashmap = new HashMap<String, Object>(); 174 hashmap.put("txt", s); 175 Bitmap b = BitmapFactory.decodeFile(new File(hatsPath, s + ".png").getAbsolutePath()); 176 b = Bitmap.createBitmap(b, 0,0,b.getWidth()/2, b.getWidth()/2); 177 hashmap.put("img", b); 178 data.add(hashmap); 179 } 180 181 return data; 182 } 183 getTeams(Context c)184 public static List<Team> getTeams(Context c) { 185 List<Team> ret = new ArrayList<Team>(); 186 187 File teamsDir = new File(c.getFilesDir(), Team.DIRECTORY_TEAMS); 188 File[] teamFileNames = teamsDir.listFiles(); 189 if(teamFileNames != null){ 190 for(File file : teamFileNames){ 191 if(file.getName().endsWith(".hwt")) { 192 Team team = Team.load(file); 193 if(team != null){ 194 ret.add(team); 195 } 196 } 197 } 198 } 199 return ret; 200 } 201 } 202