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

Last change on this file since 1086 was 1076, checked in by alloc, 7 years ago

Fixed #112

File size: 11.1 KB
RevLine 
[648]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;
[863]12import java.util.Iterator;
[648]13import java.util.TreeSet;
14import java.util.Vector;
15
16import net.oni2.aeinstaller.backend.Paths;
[809]17import net.oni2.aeinstaller.backend.oni.management.ModInstallationList;
[804]18import net.oni2.aeinstaller.backend.oni.management.tools.ToolInstallationList;
[749]19import net.oni2.moddepot.DepotManager;
20import net.oni2.moddepot.model.NodeMod;
21import net.oni2.moddepot.model.TaxonomyTerm;
[648]22
[658]23import com.thoughtworks.xstream.XStream;
[857]24import com.thoughtworks.xstream.XStreamException;
[658]25import com.thoughtworks.xstream.io.xml.StaxDriver;
26
[648]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>();
[857]36 private Type localType = null;
[648]37
[860]38 private HashMap<Integer, Package> newToolsOnDepot = new HashMap<Integer, Package>();
39 private HashMap<Integer, Package> newModsOnDepot = new HashMap<Integer, Package>();
40
[648]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() {
[860]93 HashMap<Integer, Package> oldMods = mods;
94 HashMap<Integer, Package> oldTools = tools;
95
[648]96 types = new HashMap<String, Type>();
97 mods = new HashMap<Integer, Package>();
[860]98 tools = new HashMap<Integer, Package>();
[648]99
[860]100 newModsOnDepot = new HashMap<Integer, Package>();
101 newToolsOnDepot = new HashMap<Integer, Package>();
102
[857]103 localType = new Type("-Local-");
[648]104 types.put("-Local-", localType);
105
106 for (TaxonomyTerm tt : DepotManager.getInstance().getTypes()) {
[749]107 types.put(tt.getName(), new Type(tt.getName()));
[648]108 }
109
110 for (NodeMod nm : DepotManager.getInstance().getModPackageNodes()) {
[749]111 if (nm.getUploads().size() == 1) {
[648]112 Package m = new Package(nm);
[860]113 if (nm.isTool()) {
[648]114 tools.put(m.getPackageNumber(), m);
[860]115 if (!oldTools.containsKey(m.getPackageNumber()))
116 newToolsOnDepot.put(m.getPackageNumber(), m);
117 } else {
[648]118 mods.put(m.getPackageNumber(), m);
[860]119 if (!oldMods.containsKey(m.getPackageNumber()))
120 newModsOnDepot.put(m.getPackageNumber(), m);
121 }
[648]122 }
123 }
124
[857]125 updateLocalData();
[648]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 /**
[807]199 * @return Currently installed mods
200 */
201 public TreeSet<Package> getInstalledMods() {
202 TreeSet<Package> res = new TreeSet<Package>();
[809]203 for (int n : ModInstallationList.getInstance().getInstalledMods()) {
[807]204 res.add(getPackageByNumber(n));
205 }
206 return res;
207 }
208
209 /**
[648]210 * @return Collection of tools valid on this platform and not core
211 */
[658]212 public Collection<Package> getTools() {
213 Vector<Package> res = new Vector<Package>();
[648]214 for (Package m : tools.values())
215 if (m.isValidOnPlatform() && !m.isCorePackage())
[658]216 res.add(m);
[648]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>();
[804]261 for (int n : ToolInstallationList.getInstance().getItems().keySet()) {
[1076]262 Package p = getPackageByNumber(n);
263 if (p.isTool()) {
264 res.add(p);
265 }
[648]266 }
267 return res;
268 }
269
270 /**
[860]271 * @return the newToolsOnDepot
272 */
273 public HashMap<Integer, Package> getNewToolsOnDepot() {
274 return newToolsOnDepot;
275 }
276
277 /**
278 * @return the newModsOnDepot
279 */
280 public HashMap<Integer, Package> getNewModsOnDepot() {
281 return newModsOnDepot;
282 }
283
284 /**
[648]285 * Get a mod/tool by its package number
286 *
287 * @param number
288 * Package number
289 * @return Mod/tool or null
290 */
291 private Package getPackageByNumber(int number) {
292 if (mods.containsKey(number))
293 return mods.get(number);
294 if (tools.containsKey(number))
295 return tools.get(number);
296 return null;
297 }
298
299 /**
300 * Check for unresolved dependencies within the given mods
301 *
302 * @param mods
303 * Mods to check
304 * @return Unmet dependencies
305 */
[749]306 public HashMap<Package, HashSet<Package>> checkDependencies(
307 TreeSet<Package> mods) {
[648]308 HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
309
310 for (Package m : mods) {
311 for (int depNum : m.getDependencies()) {
312 Package other = getPackageByNumber(depNum);
[804]313 if (other != null) {
314 if (!mods.contains(other)) {
315 if (!res.containsKey(m))
316 res.put(m, new HashSet<Package>());
317 res.get(m).add(other);
318 }
[648]319 }
320 }
321 }
322
323 return res;
324 }
325
326 /**
327 * Check for incompabitilites between given mods
328 *
329 * @param mods
330 * Mods to check
331 * @return Incompatible mods
332 */
[749]333 public HashMap<Package, HashSet<Package>> checkIncompabitilites(
334 TreeSet<Package> mods) {
[648]335 HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
336
337 for (Package m : mods) {
338 for (int confNum : m.getIncompabitilities()) {
339 Package other = getPackageByNumber(confNum);
[804]340 if (other != null) {
341 if (mods.contains(other)) {
342 if (!res.containsKey(m))
343 res.put(m, new HashSet<Package>());
344 res.get(m).add(other);
345 }
[648]346 }
347 }
348 }
349
350 return res;
351 }
352
353 /**
354 * @param m
355 * Mod to check
356 * @return Is mod installed?
357 */
358 boolean isModInstalled(Package m) {
[857]359 return ModInstallationList.getInstance().isInstalled(
360 m.getPackageNumber());
[648]361 }
[857]362
[859]363 /**
364 * Rescan local packages folder for local only packages and updated
365 * Mod_Info.cfg
366 */
367 public void updateLocalData() {
[857]368 if (Paths.getModsPath().exists()) {
369 for (File f : Paths.getModsPath().listFiles(new FileFilter() {
370 @Override
371 public boolean accept(File pathname) {
372 return pathname.isDirectory();
373 }
374 })) {
375 Package m = new Package(f);
376 HashMap<Integer, Package> map = null;
377 if (m.isTool())
378 map = tools;
379 else
380 map = mods;
381
382 if (!map.containsKey(m.getPackageNumber())) {
383 map.put(m.getPackageNumber(), m);
384 if (!m.isCorePackage()) {
[1001]385 if (m.isTool()) {
386// toolType.addEntry(m);
387// m.getTypes().add(toolType);
388 } else {
389 localType.addEntry(m);
390 m.getTypes().add(localType);
391 }
[857]392 }
393 }
394 }
[863]395 Iterator<Package> it = localType.getEntries().iterator();
396 while (it.hasNext()) {
397 Package p = it.next();
398 if (!p.isLocalAvailable()){
399 it.remove();
400 mods.remove(p.getPackageNumber());
401 tools.remove(p.getPackageNumber());
402 }
403 }
[857]404 }
405
406 for (Package p : mods.values()) {
407 p.updateLocalData();
408 }
409 for (Package p : tools.values()) {
410 p.updateLocalData();
411 }
412 }
413
414 private static XStream getXStream() {
415 XStream xs = new XStream(new StaxDriver());
416 xs.alias("Packages", PackageManager.class);
417 xs.alias("Type", Type.class);
418 xs.alias("Package", Package.class);
419 return xs;
420 }
421
422 /**
423 * Save Depot cache instance to file
424 *
425 * @param cacheFile
426 * File to save to
427 */
428 public void saveToCacheFile(java.io.File cacheFile) {
429 try {
430 FileOutputStream fos = new FileOutputStream(cacheFile);
431 XStream xs = getXStream();
432 xs.toXML(this, fos);
433 fos.close();
434 } catch (FileNotFoundException e) {
435 e.printStackTrace();
436 } catch (IOException e) {
437 e.printStackTrace();
438 }
439 }
440
441 /**
442 * Load cache from file
443 *
444 * @param cacheFile
445 * File to load
446 */
447 public static void loadFromCacheFile(java.io.File cacheFile) {
448 try {
449 FileInputStream fis = new FileInputStream(cacheFile);
450 XStream xs = getXStream();
451 Object obj = xs.fromXML(fis);
452 fis.close();
453 if (obj instanceof PackageManager) {
454 instance = (PackageManager) obj;
455 instance.updateLocalData();
456 }
457 } catch (XStreamException e) {
458 } catch (FileNotFoundException e) {
459 } catch (IOException e) {
460 }
461 }
[648]462}
Note: See TracBrowser for help on using the repository browser.