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

Last change on this file since 1202 was 1202, checked in by alloc, 10 hours ago

AEI 2.31: Fixes for reading offline package cache

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