source: AE/installer2/src/net/oni2/aeinstaller/backend/packages/PackageManager.java@ 693

Last change on this file since 693 was 658, checked in by alloc, 12 years ago

AEI2 0.99i:

  • Lots of refactorings for the GUI:
    • Global type for the mod info box (Main, Tools, Core)
    • Same table for Main, Tools, Core
File size: 8.3 KB
Line 
1package net.oni2.aeinstaller.backend.packages;
2
3import java.io.File;
4import java.io.FileFilter;
5import java.io.FileInputStream;
6import java.io.FileNotFoundException;
7import java.io.FileOutputStream;
8import java.io.IOException;
9import java.util.Collection;
10import java.util.HashMap;
11import java.util.HashSet;
12import java.util.TreeSet;
13import java.util.Vector;
14
15import net.oni2.aeinstaller.backend.Paths;
16import net.oni2.aeinstaller.backend.depot.DepotManager;
17import net.oni2.aeinstaller.backend.depot.model.NodeMod;
18import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm;
19import net.oni2.aeinstaller.backend.oni.Installer;
20
21import com.thoughtworks.xstream.XStream;
22import com.thoughtworks.xstream.io.xml.StaxDriver;
23
24/**
25 * @author Christian Illy
26 */
27public class PackageManager {
28 private static PackageManager instance = new PackageManager();
29
30 private HashMap<String, Type> types = new HashMap<String, Type>();
31 private HashMap<Integer, Package> mods = new HashMap<Integer, Package>();
32 private HashMap<Integer, Package> tools = new HashMap<Integer, Package>();
33
34 private Vector<Integer> currentlyInstalled = new Vector<Integer>();
35
36 /**
37 * @param f
38 * Mod selection file
39 * @return Mod selection
40 */
41 @SuppressWarnings("unchecked")
42 public Vector<Integer> loadModSelection(File f) {
43 Vector<Integer> res = new Vector<Integer>();
44 try {
45 if (f.exists()) {
46 FileInputStream fis = new FileInputStream(f);
47 XStream xs = new XStream(new StaxDriver());
48 Object obj = xs.fromXML(fis);
49 if (obj instanceof Vector<?>)
50 res = (Vector<Integer>) obj;
51 fis.close();
52 }
53 } catch (FileNotFoundException e) {
54 e.printStackTrace();
55 } catch (IOException e) {
56 e.printStackTrace();
57 }
58 return res;
59 }
60
61 /**
62 * @param f
63 * Mod selection file
64 * @param mods
65 * Selected mods
66 */
67 public void saveModSelection(File f, TreeSet<Package> mods) {
68 try {
69 Vector<Integer> installed = new Vector<Integer>();
70 for (Package m : mods) {
71 installed.add(m.getPackageNumber());
72 }
73 FileOutputStream fos = new FileOutputStream(f);
74 XStream xs = new XStream(new StaxDriver());
75 xs.toXML(installed, fos);
76 fos.close();
77 } catch (FileNotFoundException e) {
78 e.printStackTrace();
79 } catch (IOException e) {
80 e.printStackTrace();
81 }
82 }
83
84 /**
85 * First initialization of ModManager
86 */
87 public void init() {
88 types = new HashMap<String, Type>();
89 mods = new HashMap<Integer, Package>();
90
91 Type localType = new Type("-Local-", null);
92 types.put("-Local-", localType);
93
94 for (TaxonomyTerm tt : DepotManager.getInstance().getTypes()) {
95 types.put(tt.getName(), new Type(tt.getName(), tt));
96 }
97
98 HashMap<Integer, Package> modFolders = new HashMap<Integer, Package>();
99 if (Paths.getModsPath().exists()) {
100 for (File f : Paths.getModsPath().listFiles(new FileFilter() {
101 @Override
102 public boolean accept(File pathname) {
103 return pathname.isDirectory();
104 }
105 })) {
106 Package m = new Package(f);
107 modFolders.put(m.getPackageNumber(), m);
108 }
109 }
110
111 for (NodeMod nm : DepotManager.getInstance().getModPackageNodes()) {
112 if (nm.getUploads().size() == 1 && nm.getStatus() == 1) {
113 Package m = new Package(nm);
114 if (nm.isTool())
115 tools.put(m.getPackageNumber(), m);
116 else
117 mods.put(m.getPackageNumber(), m);
118 modFolders.remove(m.getPackageNumber());
119 }
120 }
121
122 for (Package m : modFolders.values()) {
123 if (!m.isCorePackage()) {
124 localType.addEntry(m);
125 m.getTypes().add(localType);
126 }
127 if (m.isTool())
128 tools.put(m.getPackageNumber(), m);
129 else
130 mods.put(m.getPackageNumber(), m);
131 }
132
133 updateInstalledMods();
134 }
135
136 /**
137 * Update the list of currently installed mods
138 */
139 public void updateInstalledMods() {
140 currentlyInstalled = Installer.getInstalledMods();
141 if (currentlyInstalled == null)
142 currentlyInstalled = new Vector<Integer>();
143 }
144
145 /**
146 * @return Singleton instance
147 */
148 public static PackageManager getInstance() {
149 return instance;
150 }
151
152 Type getTypeByName(String name) {
153 return types.get(name);
154 }
155
156 /**
157 * @return Collection of types which do have mods associated
158 */
159 public Collection<Type> getTypesWithContent() {
160 Vector<Type> res = new Vector<Type>();
161 for (Type t : types.values()) {
162 if (t.getEntries().size() > 0)
163 res.add(t);
164 }
165 return res;
166 }
167
168 /**
169 * @return Collection of mods valid on this platform and not core package
170 */
171 public Collection<Package> getModsValidAndNotCore() {
172 Vector<Package> res = new Vector<Package>();
173 for (Package m : mods.values())
174 if (m.isValidOnPlatform() && !m.isCorePackage())
175 res.add(m);
176 return res;
177 }
178
179 /**
180 * @return Mods which are always installed and valid on this platform
181 */
182 public TreeSet<Package> getCoreMods() {
183 TreeSet<Package> res = new TreeSet<Package>();
184 for (Package m : mods.values()) {
185 if (m.isValidOnPlatform() && m.isCorePackage())
186 res.add(m);
187 }
188 return res;
189 }
190
191 /**
192 * @return Mods which are already locally available
193 */
194 public TreeSet<Package> getLocalAvailableMods() {
195 TreeSet<Package> res = new TreeSet<Package>();
196 for (Package m : mods.values()) {
197 if (m.isLocalAvailable())
198 res.add(m);
199 }
200 return res;
201 }
202
203 /**
204 * @return Mods which can be updated
205 */
206 public TreeSet<Package> getUpdatableMods() {
207 TreeSet<Package> res = new TreeSet<Package>();
208 for (Package m : getLocalAvailableMods()) {
209 if (m.isNewerAvailable())
210 res.add(m);
211 }
212 return res;
213 }
214
215 /**
216 * @return Collection of tools valid on this platform and not core
217 */
218 public Collection<Package> getTools() {
219 Vector<Package> res = new Vector<Package>();
220 for (Package m : tools.values())
221 if (m.isValidOnPlatform() && !m.isCorePackage())
222 res.add(m);
223 return res;
224 }
225
226 /**
227 * @return Tools which are always installed and valid on this platform
228 */
229 public TreeSet<Package> getCoreTools() {
230 TreeSet<Package> res = new TreeSet<Package>();
231 for (Package m : tools.values()) {
232 if (m.isValidOnPlatform() && m.isCorePackage())
233 res.add(m);
234 }
235 return res;
236 }
237
238 /**
239 * @return Tools which are already locally available
240 */
241 public TreeSet<Package> getLocalAvailableTools() {
242 TreeSet<Package> res = new TreeSet<Package>();
243 for (Package m : tools.values()) {
244 if (m.isLocalAvailable())
245 res.add(m);
246 }
247 return res;
248 }
249
250 /**
251 * @return Tools which can be updated
252 */
253 public TreeSet<Package> getUpdatableTools() {
254 TreeSet<Package> res = new TreeSet<Package>();
255 for (Package m : getLocalAvailableTools()) {
256 if (m.isNewerAvailable())
257 res.add(m);
258 }
259 return res;
260 }
261
262 /**
263 * @return Currently installed tools
264 */
265 public TreeSet<Package> getInstalledTools() {
266 TreeSet<Package> res = new TreeSet<Package>();
267 for (int n : Installer.getInstalledTools()) {
268 res.add(getPackageByNumber(n));
269 }
270 return res;
271 }
272
273 /**
274 * Get a mod/tool by its package number
275 *
276 * @param number
277 * Package number
278 * @return Mod/tool or null
279 */
280 private Package getPackageByNumber(int number) {
281 if (mods.containsKey(number))
282 return mods.get(number);
283 if (tools.containsKey(number))
284 return tools.get(number);
285 return null;
286 }
287
288 /**
289 * Check for unresolved dependencies within the given mods
290 *
291 * @param mods
292 * Mods to check
293 * @return Unmet dependencies
294 */
295 public HashMap<Package, HashSet<Package>> checkDependencies(TreeSet<Package> mods) {
296 HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
297
298 for (Package m : mods) {
299 for (int depNum : m.getDependencies()) {
300 Package other = getPackageByNumber(depNum);
301 if (!mods.contains(other)) {
302 if (!res.containsKey(m))
303 res.put(m, new HashSet<Package>());
304 res.get(m).add(other);
305 }
306 }
307 }
308
309 return res;
310 }
311
312 /**
313 * Check for incompabitilites between given mods
314 *
315 * @param mods
316 * Mods to check
317 * @return Incompatible mods
318 */
319 public HashMap<Package, HashSet<Package>> checkIncompabitilites(TreeSet<Package> mods) {
320 HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
321
322 for (Package m : mods) {
323 for (int confNum : m.getIncompabitilities()) {
324 Package other = getPackageByNumber(confNum);
325 if (mods.contains(other)) {
326 if (!res.containsKey(m))
327 res.put(m, new HashSet<Package>());
328 res.get(m).add(other);
329 }
330 }
331 }
332
333 return res;
334 }
335
336 /**
337 * @param m
338 * Mod to check
339 * @return Is mod installed?
340 */
341 boolean isModInstalled(Package m) {
342 return currentlyInstalled.contains(m.getPackageNumber());
343 }
344}
Note: See TracBrowser for help on using the repository browser.