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