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

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

AEI2.11:

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