|
1 package org.hedgewars.mobile.EngineProtocol; |
|
2 |
|
3 import java.io.File; |
|
4 import java.util.ArrayList; |
|
5 import java.util.Arrays; |
|
6 import java.util.Collections; |
|
7 import java.util.HashMap; |
|
8 |
|
9 import org.hedgewars.mobile.R; |
|
10 import org.hedgewars.mobile.Utils; |
|
11 import org.hedgewars.mobile.EngineProtocol.Map.MapType; |
|
12 |
|
13 import android.content.Context; |
|
14 import android.graphics.Bitmap; |
|
15 import android.graphics.BitmapFactory; |
|
16 |
|
17 public class FrontendDataUtils { |
|
18 |
|
19 |
|
20 public static ArrayList<Map> getMaps(Context c){ |
|
21 File[] files = Utils.getFilesFromRelativeDir(c,"Maps"); |
|
22 ArrayList<Map> ret = new ArrayList<Map>(); |
|
23 |
|
24 for(File f : files){ |
|
25 if(Utils.hasFileWithSuffix(f, ".lua")){ |
|
26 ret.add(new Map(f,MapType.TYPE_MISSION, c)); |
|
27 }else{ |
|
28 ret.add(new Map(f, MapType.TYPE_DEFAULT,c)); |
|
29 } |
|
30 } |
|
31 Collections.sort(ret); |
|
32 |
|
33 return ret; |
|
34 } |
|
35 |
|
36 public static String[] getGameplay(Context c){ |
|
37 String[] files = Utils.getFileNamesFromRelativeDir(c, "Scripts/Multiplayer"); |
|
38 int retCounter = 0; |
|
39 |
|
40 for(int i = 0; i < files.length; i++){ |
|
41 if(files[i].endsWith(".lua")){ |
|
42 files[i] = files[i].replace('_', ' ').substring(0, files[i].length()-4); //replace _ by a space and removed the last four characters (.lua) |
|
43 retCounter++; |
|
44 }else files[i] = null; |
|
45 } |
|
46 String[] ret = new String[retCounter]; |
|
47 retCounter = 0; |
|
48 for(String s : files){ |
|
49 if(s != null) ret[retCounter++] = s; |
|
50 } |
|
51 Arrays.sort(ret); |
|
52 |
|
53 return ret; |
|
54 } |
|
55 |
|
56 public static String[] getThemes(Context c){ |
|
57 return Utils.getDirsWithFileSuffix(c, "Themes", "icon.png"); |
|
58 } |
|
59 |
|
60 public static ArrayList<Scheme> getSchemes(Context c){ |
|
61 return Scheme.getSchemes(c); |
|
62 } |
|
63 |
|
64 public static ArrayList<Weapon> getWeapons(Context c){ |
|
65 return Weapon.getWeapons(c); |
|
66 } |
|
67 |
|
68 public static ArrayList<HashMap<String, ?>> getGraves(Context c){ |
|
69 String pathPrefix = Utils.getDownloadPath(c) + "Graphics/Graves/"; |
|
70 ArrayList<String> names = Utils.getFilesFromDirWithSuffix(c, "Graphics/Graves", ".png", true); |
|
71 ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size()); |
|
72 |
|
73 for(String s : names){ |
|
74 HashMap<String, Object> map = new HashMap<String, Object>(); |
|
75 map.put("txt", s); |
|
76 Bitmap b = BitmapFactory.decodeFile(pathPrefix + s + ".png");//create a full path - decode to to a bitmap |
|
77 int width = b.getWidth(); |
|
78 if(b.getHeight() > width){//some pictures contain more 'frames' underneath each other, if so we only use the first frame |
|
79 Bitmap tmp = Bitmap.createBitmap(b, 0, 0, width, width); |
|
80 b.recycle(); |
|
81 b = tmp; |
|
82 } |
|
83 map.put("img", b); |
|
84 data.add(map); |
|
85 } |
|
86 return data; |
|
87 } |
|
88 |
|
89 public static ArrayList<HashMap<String, ?>> getFlags(Context c){ |
|
90 String pathPrefix = Utils.getDownloadPath(c) + "Graphics/Flags/"; |
|
91 ArrayList<String> names = Utils.getFilesFromDirWithSuffix(c, "Graphics/Flags", ".png", true); |
|
92 ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(names.size()); |
|
93 |
|
94 for(String s : names){ |
|
95 HashMap<String, Object> map = new HashMap<String, Object>(); |
|
96 map.put("txt", s); |
|
97 Bitmap b = BitmapFactory.decodeFile(pathPrefix + s + ".png");//create a full path - decode to to a bitmap |
|
98 map.put("img", b); |
|
99 data.add(map); |
|
100 } |
|
101 return data; |
|
102 } |
|
103 |
|
104 public static ArrayList<String> getVoices(Context c){ |
|
105 File[] files = Utils.getFilesFromRelativeDir(c, "Sounds/voices"); |
|
106 ArrayList<String> ret = new ArrayList<String>(); |
|
107 |
|
108 for(File f : files){ |
|
109 if(f.isDirectory()) ret.add(f.getName()); |
|
110 } |
|
111 return ret; |
|
112 } |
|
113 |
|
114 public static ArrayList<String> getForts(Context c){ |
|
115 return Utils.getFilesFromDirWithSuffix(c, "Forts", "L.png", true); |
|
116 } |
|
117 public static ArrayList<HashMap<String, ?>> getTypes(Context c){ |
|
118 ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(6); |
|
119 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)}; |
|
120 int[] images = {R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1}; |
|
121 |
|
122 for(int i = 0; i < levels.length; i++){ |
|
123 HashMap<String, Object> map = new HashMap<String, Object>(); |
|
124 map.put("txt", levels[i]); |
|
125 map.put("img", images[i]); |
|
126 data.add(map); |
|
127 } |
|
128 |
|
129 return data; |
|
130 } |
|
131 |
|
132 public static ArrayList<HashMap<String, ?>> getHats(Context c){ |
|
133 ArrayList<String> files = Utils.getFilesFromDirWithSuffix(c, "Graphics/Hats", ".png", true); |
|
134 String pathPrefix = Utils.getDownloadPath(c) + "Graphics/Hats/"; |
|
135 int size = files.size(); |
|
136 ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(size); |
|
137 |
|
138 HashMap<String, Object> hashmap; |
|
139 for(String s : files){ |
|
140 hashmap = new HashMap<String, Object>(); |
|
141 hashmap.put("txt", s); |
|
142 Bitmap b = BitmapFactory.decodeFile(pathPrefix + s + ".png");//create a full path - decode to to a bitmap |
|
143 b = Bitmap.createBitmap(b, 0,0,b.getWidth()/2, b.getWidth()/2); |
|
144 hashmap.put("img", b); |
|
145 data.add(hashmap); |
|
146 } |
|
147 |
|
148 return data; |
|
149 } |
|
150 |
|
151 public static ArrayList<HashMap<String, ?>> getTeams(Context c){ |
|
152 ArrayList<HashMap<String, ?>> ret = new ArrayList<HashMap<String, ?>>(); |
|
153 |
|
154 File teamsDir = new File(c.getFilesDir().getAbsolutePath() + '/' + Team.DIRECTORY_TEAMS); |
|
155 File[] teamFileNames = teamsDir.listFiles(); |
|
156 |
|
157 for(File s : teamFileNames){ |
|
158 Team t = Team.getTeamFromXml(s.getAbsolutePath()); |
|
159 if(t != null){ |
|
160 ret.add(teamToHashMap(t)); |
|
161 } |
|
162 } |
|
163 return ret; |
|
164 } |
|
165 |
|
166 public static HashMap<String, Object> teamToHashMap(Team t){ |
|
167 HashMap<String, Object> hashmap = new HashMap<String, Object>(); |
|
168 hashmap.put("team", t); |
|
169 hashmap.put("txt", t.name); |
|
170 switch(t.levels[0]){ |
|
171 case 0: |
|
172 hashmap.put("img", R.drawable.human); |
|
173 break; |
|
174 case 1: |
|
175 hashmap.put("img", R.drawable.bot5); |
|
176 break; |
|
177 case 2: |
|
178 hashmap.put("img", R.drawable.bot4); |
|
179 break; |
|
180 case 3: |
|
181 hashmap.put("img", R.drawable.bot3); |
|
182 break; |
|
183 case 4: |
|
184 hashmap.put("img", R.drawable.bot2); |
|
185 break; |
|
186 default: |
|
187 case 5: |
|
188 hashmap.put("img", R.drawable.bot1); |
|
189 break; |
|
190 } |
|
191 return hashmap; |
|
192 } |
|
193 } |