| 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.Iterator; |
| 13 |
import java.util.TreeSet; |
| 14 |
import java.util.Vector; |
| 15 |
|
| 16 |
import net.oni2.aeinstaller.backend.Paths; |
| 17 |
import net.oni2.aeinstaller.backend.oni.management.ModInstallationList; |
| 18 |
import net.oni2.aeinstaller.backend.oni.management.tools.ToolInstallationList; |
| 19 |
import net.oni2.moddepot.DepotManager; |
| 20 |
import net.oni2.moddepot.model.NodeMod; |
| 21 |
import net.oni2.moddepot.model.TaxonomyTerm; |
| 22 |
|
| 23 |
import com.thoughtworks.xstream.XStream; |
| 24 |
import com.thoughtworks.xstream.XStreamException; |
| 25 |
import com.thoughtworks.xstream.io.xml.StaxDriver; |
| 26 |
|
| 27 |
/** |
| 28 |
* @author Christian Illy |
| 29 |
*/ |
| 30 |
public 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 |
Package p = getPackageByNumber(n); |
| 263 |
if (p.isTool()) { |
| 264 |
res.add(p); |
| 265 |
} |
| 266 |
} |
| 267 |
return res; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 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 |
/** |
| 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 |
*/ |
| 306 |
public HashMap<Package, HashSet<Package>> checkDependencies( |
| 307 |
TreeSet<Package> mods) { |
| 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); |
| 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 |
} |
| 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 |
*/ |
| 333 |
public HashMap<Package, HashSet<Package>> checkIncompabitilites( |
| 334 |
TreeSet<Package> mods) { |
| 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); |
| 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 |
} |
| 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) { |
| 359 |
return ModInstallationList.getInstance().isInstalled( |
| 360 |
m.getPackageNumber()); |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Rescan local packages folder for local only packages and updated |
| 365 |
* Mod_Info.cfg |
| 366 |
*/ |
| 367 |
public void updateLocalData() { |
| 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()) { |
| 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 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 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 |
} |
| 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 |
} |
| 462 |
} |