| 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 |
/** |
| 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 |
|
| 92 |
localType = new Type("-Local-"); |
| 93 |
types.put("-Local-", localType); |
| 94 |
|
| 95 |
for (TaxonomyTerm tt : DepotManager.getInstance().getTypes()) { |
| 96 |
types.put(tt.getName(), new Type(tt.getName())); |
| 97 |
} |
| 98 |
|
| 99 |
for (NodeMod nm : DepotManager.getInstance().getModPackageNodes()) { |
| 100 |
if (nm.getUploads().size() == 1) { |
| 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 |
|
| 109 |
updateLocalData(); |
| 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 |
/** |
| 183 |
* @return Currently installed mods |
| 184 |
*/ |
| 185 |
public TreeSet<Package> getInstalledMods() { |
| 186 |
TreeSet<Package> res = new TreeSet<Package>(); |
| 187 |
for (int n : ModInstallationList.getInstance().getInstalledMods()) { |
| 188 |
res.add(getPackageByNumber(n)); |
| 189 |
} |
| 190 |
return res; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* @return Collection of tools valid on this platform and not core |
| 195 |
*/ |
| 196 |
public Collection<Package> getTools() { |
| 197 |
Vector<Package> res = new Vector<Package>(); |
| 198 |
for (Package m : tools.values()) |
| 199 |
if (m.isValidOnPlatform() && !m.isCorePackage()) |
| 200 |
res.add(m); |
| 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>(); |
| 245 |
for (int n : ToolInstallationList.getInstance().getItems().keySet()) { |
| 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 |
*/ |
| 273 |
public HashMap<Package, HashSet<Package>> checkDependencies( |
| 274 |
TreeSet<Package> mods) { |
| 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); |
| 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 |
} |
| 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 |
*/ |
| 300 |
public HashMap<Package, HashSet<Package>> checkIncompabitilites( |
| 301 |
TreeSet<Package> mods) { |
| 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); |
| 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 |
} |
| 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) { |
| 326 |
return ModInstallationList.getInstance().isInstalled( |
| 327 |
m.getPackageNumber()); |
| 328 |
} |
| 329 |
|
| 330 |
private void updateLocalData() { |
| 331 |
if (Paths.getModsPath().exists()) { |
| 332 |
for (File f : Paths.getModsPath().listFiles(new FileFilter() { |
| 333 |
@Override |
| 334 |
public boolean accept(File pathname) { |
| 335 |
return pathname.isDirectory(); |
| 336 |
} |
| 337 |
})) { |
| 338 |
Package m = new Package(f); |
| 339 |
HashMap<Integer, Package> map = null; |
| 340 |
if (m.isTool()) |
| 341 |
map = tools; |
| 342 |
else |
| 343 |
map = mods; |
| 344 |
|
| 345 |
if (!map.containsKey(m.getPackageNumber())) { |
| 346 |
map.put(m.getPackageNumber(), m); |
| 347 |
if (!m.isCorePackage()) { |
| 348 |
localType.addEntry(m); |
| 349 |
m.getTypes().add(localType); |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
for (Package p : mods.values()) { |
| 356 |
p.updateLocalData(); |
| 357 |
} |
| 358 |
for (Package p : tools.values()) { |
| 359 |
p.updateLocalData(); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
private static XStream getXStream() { |
| 364 |
XStream xs = new XStream(new StaxDriver()); |
| 365 |
xs.alias("Packages", PackageManager.class); |
| 366 |
xs.alias("Type", Type.class); |
| 367 |
xs.alias("Package", Package.class); |
| 368 |
return xs; |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Save Depot cache instance to file |
| 373 |
* |
| 374 |
* @param cacheFile |
| 375 |
* File to save to |
| 376 |
*/ |
| 377 |
public void saveToCacheFile(java.io.File cacheFile) { |
| 378 |
try { |
| 379 |
FileOutputStream fos = new FileOutputStream(cacheFile); |
| 380 |
XStream xs = getXStream(); |
| 381 |
xs.toXML(this, fos); |
| 382 |
fos.close(); |
| 383 |
} catch (FileNotFoundException e) { |
| 384 |
e.printStackTrace(); |
| 385 |
} catch (IOException e) { |
| 386 |
e.printStackTrace(); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* Load cache from file |
| 392 |
* |
| 393 |
* @param cacheFile |
| 394 |
* File to load |
| 395 |
*/ |
| 396 |
public static void loadFromCacheFile(java.io.File cacheFile) { |
| 397 |
try { |
| 398 |
FileInputStream fis = new FileInputStream(cacheFile); |
| 399 |
XStream xs = getXStream(); |
| 400 |
Object obj = xs.fromXML(fis); |
| 401 |
fis.close(); |
| 402 |
if (obj instanceof PackageManager) { |
| 403 |
instance = (PackageManager) obj; |
| 404 |
instance.updateLocalData(); |
| 405 |
} |
| 406 |
} catch (XStreamException e) { |
| 407 |
} catch (FileNotFoundException e) { |
| 408 |
} catch (IOException e) { |
| 409 |
} |
| 410 |
} |
| 411 |
} |