| [648] | 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;
|
|---|
| [863] | 12 | import java.util.Iterator;
|
|---|
| [648] | 13 | import java.util.TreeSet;
|
|---|
| 14 | import java.util.Vector;
|
|---|
| 15 |
|
|---|
| 16 | import net.oni2.aeinstaller.backend.Paths;
|
|---|
| [809] | 17 | import net.oni2.aeinstaller.backend.oni.management.ModInstallationList;
|
|---|
| [804] | 18 | import net.oni2.aeinstaller.backend.oni.management.tools.ToolInstallationList;
|
|---|
| [749] | 19 | import net.oni2.moddepot.DepotManager;
|
|---|
| 20 | import net.oni2.moddepot.model.NodeMod;
|
|---|
| 21 | import net.oni2.moddepot.model.TaxonomyTerm;
|
|---|
| [648] | 22 |
|
|---|
| [658] | 23 | import com.thoughtworks.xstream.XStream;
|
|---|
| [857] | 24 | import com.thoughtworks.xstream.XStreamException;
|
|---|
| [658] | 25 | import com.thoughtworks.xstream.io.xml.StaxDriver;
|
|---|
| 26 |
|
|---|
| [648] | 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>();
|
|---|
| [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()) {
|
|---|
| [648] | 262 | res.add(getPackageByNumber(n));
|
|---|
| 263 | }
|
|---|
| 264 | return res;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /**
|
|---|
| [860] | 268 | * @return the newToolsOnDepot
|
|---|
| 269 | */
|
|---|
| 270 | public HashMap<Integer, Package> getNewToolsOnDepot() {
|
|---|
| 271 | return newToolsOnDepot;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | /**
|
|---|
| 275 | * @return the newModsOnDepot
|
|---|
| 276 | */
|
|---|
| 277 | public HashMap<Integer, Package> getNewModsOnDepot() {
|
|---|
| 278 | return newModsOnDepot;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | /**
|
|---|
| [648] | 282 | * Get a mod/tool by its package number
|
|---|
| 283 | *
|
|---|
| 284 | * @param number
|
|---|
| 285 | * Package number
|
|---|
| 286 | * @return Mod/tool or null
|
|---|
| 287 | */
|
|---|
| 288 | private Package getPackageByNumber(int number) {
|
|---|
| 289 | if (mods.containsKey(number))
|
|---|
| 290 | return mods.get(number);
|
|---|
| 291 | if (tools.containsKey(number))
|
|---|
| 292 | return tools.get(number);
|
|---|
| 293 | return null;
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | /**
|
|---|
| 297 | * Check for unresolved dependencies within the given mods
|
|---|
| 298 | *
|
|---|
| 299 | * @param mods
|
|---|
| 300 | * Mods to check
|
|---|
| 301 | * @return Unmet dependencies
|
|---|
| 302 | */
|
|---|
| [749] | 303 | public HashMap<Package, HashSet<Package>> checkDependencies(
|
|---|
| 304 | TreeSet<Package> mods) {
|
|---|
| [648] | 305 | HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
|
|---|
| 306 |
|
|---|
| 307 | for (Package m : mods) {
|
|---|
| 308 | for (int depNum : m.getDependencies()) {
|
|---|
| 309 | Package other = getPackageByNumber(depNum);
|
|---|
| [804] | 310 | if (other != null) {
|
|---|
| 311 | if (!mods.contains(other)) {
|
|---|
| 312 | if (!res.containsKey(m))
|
|---|
| 313 | res.put(m, new HashSet<Package>());
|
|---|
| 314 | res.get(m).add(other);
|
|---|
| 315 | }
|
|---|
| [648] | 316 | }
|
|---|
| 317 | }
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | return res;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | /**
|
|---|
| 324 | * Check for incompabitilites between given mods
|
|---|
| 325 | *
|
|---|
| 326 | * @param mods
|
|---|
| 327 | * Mods to check
|
|---|
| 328 | * @return Incompatible mods
|
|---|
| 329 | */
|
|---|
| [749] | 330 | public HashMap<Package, HashSet<Package>> checkIncompabitilites(
|
|---|
| 331 | TreeSet<Package> mods) {
|
|---|
| [648] | 332 | HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
|
|---|
| 333 |
|
|---|
| 334 | for (Package m : mods) {
|
|---|
| 335 | for (int confNum : m.getIncompabitilities()) {
|
|---|
| 336 | Package other = getPackageByNumber(confNum);
|
|---|
| [804] | 337 | if (other != null) {
|
|---|
| 338 | if (mods.contains(other)) {
|
|---|
| 339 | if (!res.containsKey(m))
|
|---|
| 340 | res.put(m, new HashSet<Package>());
|
|---|
| 341 | res.get(m).add(other);
|
|---|
| 342 | }
|
|---|
| [648] | 343 | }
|
|---|
| 344 | }
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | return res;
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | /**
|
|---|
| 351 | * @param m
|
|---|
| 352 | * Mod to check
|
|---|
| 353 | * @return Is mod installed?
|
|---|
| 354 | */
|
|---|
| 355 | boolean isModInstalled(Package m) {
|
|---|
| [857] | 356 | return ModInstallationList.getInstance().isInstalled(
|
|---|
| 357 | m.getPackageNumber());
|
|---|
| [648] | 358 | }
|
|---|
| [857] | 359 |
|
|---|
| [859] | 360 | /**
|
|---|
| 361 | * Rescan local packages folder for local only packages and updated
|
|---|
| 362 | * Mod_Info.cfg
|
|---|
| 363 | */
|
|---|
| 364 | public void updateLocalData() {
|
|---|
| [857] | 365 | if (Paths.getModsPath().exists()) {
|
|---|
| 366 | for (File f : Paths.getModsPath().listFiles(new FileFilter() {
|
|---|
| 367 | @Override
|
|---|
| 368 | public boolean accept(File pathname) {
|
|---|
| 369 | return pathname.isDirectory();
|
|---|
| 370 | }
|
|---|
| 371 | })) {
|
|---|
| 372 | Package m = new Package(f);
|
|---|
| 373 | HashMap<Integer, Package> map = null;
|
|---|
| 374 | if (m.isTool())
|
|---|
| 375 | map = tools;
|
|---|
| 376 | else
|
|---|
| 377 | map = mods;
|
|---|
| 378 |
|
|---|
| 379 | if (!map.containsKey(m.getPackageNumber())) {
|
|---|
| 380 | map.put(m.getPackageNumber(), m);
|
|---|
| 381 | if (!m.isCorePackage()) {
|
|---|
| 382 | localType.addEntry(m);
|
|---|
| 383 | m.getTypes().add(localType);
|
|---|
| 384 | }
|
|---|
| 385 | }
|
|---|
| 386 | }
|
|---|
| [863] | 387 | Iterator<Package> it = localType.getEntries().iterator();
|
|---|
| 388 | while (it.hasNext()) {
|
|---|
| 389 | Package p = it.next();
|
|---|
| 390 | if (!p.isLocalAvailable()){
|
|---|
| 391 | it.remove();
|
|---|
| 392 | mods.remove(p.getPackageNumber());
|
|---|
| 393 | tools.remove(p.getPackageNumber());
|
|---|
| 394 | }
|
|---|
| 395 | }
|
|---|
| [857] | 396 | }
|
|---|
| 397 |
|
|---|
| 398 | for (Package p : mods.values()) {
|
|---|
| 399 | p.updateLocalData();
|
|---|
| 400 | }
|
|---|
| 401 | for (Package p : tools.values()) {
|
|---|
| 402 | p.updateLocalData();
|
|---|
| 403 | }
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | private static XStream getXStream() {
|
|---|
| 407 | XStream xs = new XStream(new StaxDriver());
|
|---|
| 408 | xs.alias("Packages", PackageManager.class);
|
|---|
| 409 | xs.alias("Type", Type.class);
|
|---|
| 410 | xs.alias("Package", Package.class);
|
|---|
| 411 | return xs;
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | /**
|
|---|
| 415 | * Save Depot cache instance to file
|
|---|
| 416 | *
|
|---|
| 417 | * @param cacheFile
|
|---|
| 418 | * File to save to
|
|---|
| 419 | */
|
|---|
| 420 | public void saveToCacheFile(java.io.File cacheFile) {
|
|---|
| 421 | try {
|
|---|
| 422 | FileOutputStream fos = new FileOutputStream(cacheFile);
|
|---|
| 423 | XStream xs = getXStream();
|
|---|
| 424 | xs.toXML(this, fos);
|
|---|
| 425 | fos.close();
|
|---|
| 426 | } catch (FileNotFoundException e) {
|
|---|
| 427 | e.printStackTrace();
|
|---|
| 428 | } catch (IOException e) {
|
|---|
| 429 | e.printStackTrace();
|
|---|
| 430 | }
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 | /**
|
|---|
| 434 | * Load cache from file
|
|---|
| 435 | *
|
|---|
| 436 | * @param cacheFile
|
|---|
| 437 | * File to load
|
|---|
| 438 | */
|
|---|
| 439 | public static void loadFromCacheFile(java.io.File cacheFile) {
|
|---|
| 440 | try {
|
|---|
| 441 | FileInputStream fis = new FileInputStream(cacheFile);
|
|---|
| 442 | XStream xs = getXStream();
|
|---|
| 443 | Object obj = xs.fromXML(fis);
|
|---|
| 444 | fis.close();
|
|---|
| 445 | if (obj instanceof PackageManager) {
|
|---|
| 446 | instance = (PackageManager) obj;
|
|---|
| 447 | instance.updateLocalData();
|
|---|
| 448 | }
|
|---|
| 449 | } catch (XStreamException e) {
|
|---|
| 450 | } catch (FileNotFoundException e) {
|
|---|
| 451 | } catch (IOException e) {
|
|---|
| 452 | }
|
|---|
| 453 | }
|
|---|
| [648] | 454 | }
|
|---|