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

Last change on this file since 924 was 863, checked in by alloc, 12 years ago

AEI2.12:

File size: 10.9 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.Iterator;
13import java.util.TreeSet;
14import java.util.Vector;
15
16import net.oni2.aeinstaller.backend.Paths;
17import net.oni2.aeinstaller.backend.oni.management.ModInstallationList;
18import net.oni2.aeinstaller.backend.oni.management.tools.ToolInstallationList;
19import net.oni2.moddepot.DepotManager;
20import net.oni2.moddepot.model.NodeMod;
21import net.oni2.moddepot.model.TaxonomyTerm;
22
23import com.thoughtworks.xstream.XStream;
24import com.thoughtworks.xstream.XStreamException;
25import com.thoughtworks.xstream.io.xml.StaxDriver;
26
27/**
28 * @author Christian Illy
29 */
30public class PackageManager {
31 private static PackageManager instance = new PackageManager();
32
33 private HashMap<String, Type> types = new HashMap<String, Type>();
34 private HashMap<Integer, Package> mods = new HashMap<Integer, Package>();
35 private HashMap<Integer, Package> tools = new HashMap<Integer, Package>();
36 private Type localType = null;
37
38 private HashMap<Integer, Package> newToolsOnDepot = new HashMap<Integer, Package>();
39 private HashMap<Integer, Package> newModsOnDepot = new HashMap<Integer, Package>();
40
41 /**
42 * @param f
43 * Mod selection file
44 * @return Mod selection
45 */
46 @SuppressWarnings("unchecked")
47 public Vector<Integer> loadModSelection(File f) {
48 Vector<Integer> res = new Vector<Integer>();
49 try {
50 if (f.exists()) {
51 FileInputStream fis = new FileInputStream(f);
52 XStream xs = new XStream(new StaxDriver());
53 Object obj = xs.fromXML(fis);
54 if (obj instanceof Vector<?>)
55 res = (Vector<Integer>) obj;
56 fis.close();
57 }
58 } catch (FileNotFoundException e) {
59 e.printStackTrace();
60 } catch (IOException e) {
61 e.printStackTrace();
62 }
63 return res;
64 }
65
66 /**
67 * @param f
68 * Mod selection file
69 * @param mods
70 * Selected mods
71 */
72 public void saveModSelection(File f, TreeSet<Package> mods) {
73 try {
74 Vector<Integer> installed = new Vector<Integer>();
75 for (Package m : mods) {
76 installed.add(m.getPackageNumber());
77 }
78 FileOutputStream fos = new FileOutputStream(f);
79 XStream xs = new XStream(new StaxDriver());
80 xs.toXML(installed, fos);
81 fos.close();
82 } catch (FileNotFoundException e) {
83 e.printStackTrace();
84 } catch (IOException e) {
85 e.printStackTrace();
86 }
87 }
88
89 /**
90 * First initialization of ModManager
91 */
92 public void init() {
93 HashMap<Integer, Package> oldMods = mods;
94 HashMap<Integer, Package> oldTools = tools;
95
96 types = new HashMap<String, Type>();
97 mods = new HashMap<Integer, Package>();
98 tools = new HashMap<Integer, Package>();
99
100 newModsOnDepot = new HashMap<Integer, Package>();
101 newToolsOnDepot = new HashMap<Integer, Package>();
102
103 localType = new Type("-Local-");
104 types.put("-Local-", localType);
105
106 for (TaxonomyTerm tt : DepotManager.getInstance().getTypes()) {
107 types.put(tt.getName(), new Type(tt.getName()));
108 }
109
110 for (NodeMod nm : DepotManager.getInstance().getModPackageNodes()) {
111 if (nm.getUploads().size() == 1) {
112 Package m = new Package(nm);
113 if (nm.isTool()) {
114 tools.put(m.getPackageNumber(), m);
115 if (!oldTools.containsKey(m.getPackageNumber()))
116 newToolsOnDepot.put(m.getPackageNumber(), m);
117 } else {
118 mods.put(m.getPackageNumber(), m);
119 if (!oldMods.containsKey(m.getPackageNumber()))
120 newModsOnDepot.put(m.getPackageNumber(), m);
121 }
122 }
123 }
124
125 updateLocalData();
126 }
127
128 /**
129 * @return Singleton instance
130 */
131 public static PackageManager getInstance() {
132 return instance;
133 }
134
135 Type getTypeByName(String name) {
136 return types.get(name);
137 }
138
139 /**
140 * @return Collection of types which do have mods associated
141 */
142 public Collection<Type> getTypesWithContent() {
143 Vector<Type> res = new Vector<Type>();
144 for (Type t : types.values()) {
145 if (t.getEntries().size() > 0)
146 res.add(t);
147 }
148 return res;
149 }
150
151 /**
152 * @return Collection of mods valid on this platform and not core package
153 */
154 public Collection<Package> getModsValidAndNotCore() {
155 Vector<Package> res = new Vector<Package>();
156 for (Package m : mods.values())
157 if (m.isValidOnPlatform() && !m.isCorePackage())
158 res.add(m);
159 return res;
160 }
161
162 /**
163 * @return Mods which are always installed and valid on this platform
164 */
165 public TreeSet<Package> getCoreMods() {
166 TreeSet<Package> res = new TreeSet<Package>();
167 for (Package m : mods.values()) {
168 if (m.isValidOnPlatform() && m.isCorePackage())
169 res.add(m);
170 }
171 return res;
172 }
173
174 /**
175 * @return Mods which are already locally available
176 */
177 public TreeSet<Package> getLocalAvailableMods() {
178 TreeSet<Package> res = new TreeSet<Package>();
179 for (Package m : mods.values()) {
180 if (m.isLocalAvailable())
181 res.add(m);
182 }
183 return res;
184 }
185
186 /**
187 * @return Mods which can be updated
188 */
189 public TreeSet<Package> getUpdatableMods() {
190 TreeSet<Package> res = new TreeSet<Package>();
191 for (Package m : getLocalAvailableMods()) {
192 if (m.isNewerAvailable())
193 res.add(m);
194 }
195 return res;
196 }
197
198 /**
199 * @return Currently installed mods
200 */
201 public TreeSet<Package> getInstalledMods() {
202 TreeSet<Package> res = new TreeSet<Package>();
203 for (int n : ModInstallationList.getInstance().getInstalledMods()) {
204 res.add(getPackageByNumber(n));
205 }
206 return res;
207 }
208
209 /**
210 * @return Collection of tools valid on this platform and not core
211 */
212 public Collection<Package> getTools() {
213 Vector<Package> res = new Vector<Package>();
214 for (Package m : tools.values())
215 if (m.isValidOnPlatform() && !m.isCorePackage())
216 res.add(m);
217 return res;
218 }
219
220 /**
221 * @return Tools which are always installed and valid on this platform
222 */
223 public TreeSet<Package> getCoreTools() {
224 TreeSet<Package> res = new TreeSet<Package>();
225 for (Package m : tools.values()) {
226 if (m.isValidOnPlatform() && m.isCorePackage())
227 res.add(m);
228 }
229 return res;
230 }
231
232 /**
233 * @return Tools which are already locally available
234 */
235 public TreeSet<Package> getLocalAvailableTools() {
236 TreeSet<Package> res = new TreeSet<Package>();
237 for (Package m : tools.values()) {
238 if (m.isLocalAvailable())
239 res.add(m);
240 }
241 return res;
242 }
243
244 /**
245 * @return Tools which can be updated
246 */
247 public TreeSet<Package> getUpdatableTools() {
248 TreeSet<Package> res = new TreeSet<Package>();
249 for (Package m : getLocalAvailableTools()) {
250 if (m.isNewerAvailable())
251 res.add(m);
252 }
253 return res;
254 }
255
256 /**
257 * @return Currently installed tools
258 */
259 public TreeSet<Package> getInstalledTools() {
260 TreeSet<Package> res = new TreeSet<Package>();
261 for (int n : ToolInstallationList.getInstance().getItems().keySet()) {
262 res.add(getPackageByNumber(n));
263 }
264 return res;
265 }
266
267 /**
268 * @return the newToolsOnDepot
269 */
270 public HashMap<Integer, Package> getNewToolsOnDepot() {
271 return newToolsOnDepot;
272 }
273
274 /**
275 * @return the newModsOnDepot
276 */
277 public HashMap<Integer, Package> getNewModsOnDepot() {
278 return newModsOnDepot;
279 }
280
281 /**
282 * Get a mod/tool by its package number
283 *
284 * @param number
285 * Package number
286 * @return Mod/tool or null
287 */
288 private Package getPackageByNumber(int number) {
289 if (mods.containsKey(number))
290 return mods.get(number);
291 if (tools.containsKey(number))
292 return tools.get(number);
293 return null;
294 }
295
296 /**
297 * Check for unresolved dependencies within the given mods
298 *
299 * @param mods
300 * Mods to check
301 * @return Unmet dependencies
302 */
303 public HashMap<Package, HashSet<Package>> checkDependencies(
304 TreeSet<Package> mods) {
305 HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
306
307 for (Package m : mods) {
308 for (int depNum : m.getDependencies()) {
309 Package other = getPackageByNumber(depNum);
310 if (other != null) {
311 if (!mods.contains(other)) {
312 if (!res.containsKey(m))
313 res.put(m, new HashSet<Package>());
314 res.get(m).add(other);
315 }
316 }
317 }
318 }
319
320 return res;
321 }
322
323 /**
324 * Check for incompabitilites between given mods
325 *
326 * @param mods
327 * Mods to check
328 * @return Incompatible mods
329 */
330 public HashMap<Package, HashSet<Package>> checkIncompabitilites(
331 TreeSet<Package> mods) {
332 HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
333
334 for (Package m : mods) {
335 for (int confNum : m.getIncompabitilities()) {
336 Package other = getPackageByNumber(confNum);
337 if (other != null) {
338 if (mods.contains(other)) {
339 if (!res.containsKey(m))
340 res.put(m, new HashSet<Package>());
341 res.get(m).add(other);
342 }
343 }
344 }
345 }
346
347 return res;
348 }
349
350 /**
351 * @param m
352 * Mod to check
353 * @return Is mod installed?
354 */
355 boolean isModInstalled(Package m) {
356 return ModInstallationList.getInstance().isInstalled(
357 m.getPackageNumber());
358 }
359
360 /**
361 * Rescan local packages folder for local only packages and updated
362 * Mod_Info.cfg
363 */
364 public void updateLocalData() {
365 if (Paths.getModsPath().exists()) {
366 for (File f : Paths.getModsPath().listFiles(new FileFilter() {
367 @Override
368 public boolean accept(File pathname) {
369 return pathname.isDirectory();
370 }
371 })) {
372 Package m = new Package(f);
373 HashMap<Integer, Package> map = null;
374 if (m.isTool())
375 map = tools;
376 else
377 map = mods;
378
379 if (!map.containsKey(m.getPackageNumber())) {
380 map.put(m.getPackageNumber(), m);
381 if (!m.isCorePackage()) {
382 localType.addEntry(m);
383 m.getTypes().add(localType);
384 }
385 }
386 }
387 Iterator<Package> it = localType.getEntries().iterator();
388 while (it.hasNext()) {
389 Package p = it.next();
390 if (!p.isLocalAvailable()){
391 it.remove();
392 mods.remove(p.getPackageNumber());
393 tools.remove(p.getPackageNumber());
394 }
395 }
396 }
397
398 for (Package p : mods.values()) {
399 p.updateLocalData();
400 }
401 for (Package p : tools.values()) {
402 p.updateLocalData();
403 }
404 }
405
406 private static XStream getXStream() {
407 XStream xs = new XStream(new StaxDriver());
408 xs.alias("Packages", PackageManager.class);
409 xs.alias("Type", Type.class);
410 xs.alias("Package", Package.class);
411 return xs;
412 }
413
414 /**
415 * Save Depot cache instance to file
416 *
417 * @param cacheFile
418 * File to save to
419 */
420 public void saveToCacheFile(java.io.File cacheFile) {
421 try {
422 FileOutputStream fos = new FileOutputStream(cacheFile);
423 XStream xs = getXStream();
424 xs.toXML(this, fos);
425 fos.close();
426 } catch (FileNotFoundException e) {
427 e.printStackTrace();
428 } catch (IOException e) {
429 e.printStackTrace();
430 }
431 }
432
433 /**
434 * Load cache from file
435 *
436 * @param cacheFile
437 * File to load
438 */
439 public static void loadFromCacheFile(java.io.File cacheFile) {
440 try {
441 FileInputStream fis = new FileInputStream(cacheFile);
442 XStream xs = getXStream();
443 Object obj = xs.fromXML(fis);
444 fis.close();
445 if (obj instanceof PackageManager) {
446 instance = (PackageManager) obj;
447 instance.updateLocalData();
448 }
449 } catch (XStreamException e) {
450 } catch (FileNotFoundException e) {
451 } catch (IOException e) {
452 }
453 }
454}
Note: See TracBrowser for help on using the repository browser.