[600] | 1 | package net.oni2.aeinstaller.backend.mods;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 | import java.io.FileFilter;
|
---|
[604] | 5 | import java.io.FileInputStream;
|
---|
| 6 | import java.io.FileNotFoundException;
|
---|
| 7 | import java.io.FileOutputStream;
|
---|
| 8 | import java.io.IOException;
|
---|
[600] | 9 | import java.util.Collection;
|
---|
| 10 | import java.util.HashMap;
|
---|
[602] | 11 | import java.util.HashSet;
|
---|
[626] | 12 | import java.util.TreeMap;
|
---|
[602] | 13 | import java.util.TreeSet;
|
---|
[600] | 14 | import java.util.Vector;
|
---|
| 15 |
|
---|
[604] | 16 | import com.thoughtworks.xstream.XStream;
|
---|
| 17 | import com.thoughtworks.xstream.io.xml.StaxDriver;
|
---|
| 18 |
|
---|
[600] | 19 | import net.oni2.aeinstaller.backend.Paths;
|
---|
| 20 | import net.oni2.aeinstaller.backend.depot.DepotManager;
|
---|
| 21 | import net.oni2.aeinstaller.backend.depot.model.NodeMod;
|
---|
| 22 | import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm;
|
---|
[604] | 23 | import net.oni2.aeinstaller.backend.oni.Installer;
|
---|
[600] | 24 |
|
---|
| 25 | /**
|
---|
| 26 | * @author Christian Illy
|
---|
| 27 | */
|
---|
| 28 | public class ModManager {
|
---|
| 29 | private static ModManager instance = new ModManager();
|
---|
| 30 |
|
---|
| 31 | private HashMap<String, Type> types = new HashMap<String, Type>();
|
---|
| 32 | private HashMap<Integer, Mod> mods = new HashMap<Integer, Mod>();
|
---|
[602] | 33 | private HashMap<Integer, Mod> tools = new HashMap<Integer, Mod>();
|
---|
[600] | 34 |
|
---|
[604] | 35 | private Vector<Integer> currentlyInstalled = new Vector<Integer>();
|
---|
| 36 |
|
---|
[600] | 37 | /**
|
---|
[604] | 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 {
|
---|
[608] | 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 | }
|
---|
[604] | 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<Mod> mods) {
|
---|
| 69 | try {
|
---|
| 70 | Vector<Integer> installed = new Vector<Integer>();
|
---|
| 71 | for (Mod 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 | /**
|
---|
[600] | 86 | * First initialization of ModManager
|
---|
| 87 | */
|
---|
| 88 | public void init() {
|
---|
| 89 | types = new HashMap<String, Type>();
|
---|
| 90 | mods = new HashMap<Integer, Mod>();
|
---|
| 91 |
|
---|
[608] | 92 | Type localType = new Type("-Local-", null);
|
---|
| 93 | types.put("-Local-", localType);
|
---|
[600] | 94 |
|
---|
[635] | 95 | for (TaxonomyTerm tt : DepotManager.getInstance().getTypes()) {
|
---|
[600] | 96 | types.put(tt.getName(), new Type(tt.getName(), tt));
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | HashMap<Integer, Mod> modFolders = new HashMap<Integer, Mod>();
|
---|
| 100 | if (Paths.getModsPath().exists()) {
|
---|
| 101 | for (File f : Paths.getModsPath().listFiles(new FileFilter() {
|
---|
| 102 | @Override
|
---|
| 103 | public boolean accept(File pathname) {
|
---|
| 104 | return pathname.isDirectory();
|
---|
| 105 | }
|
---|
| 106 | })) {
|
---|
| 107 | Mod m = new Mod(f);
|
---|
| 108 | modFolders.put(m.getPackageNumber(), m);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | for (NodeMod nm : DepotManager.getInstance().getModPackageNodes()) {
|
---|
| 113 | if (nm.getUploads().size() == 1) {
|
---|
| 114 | Mod m = new Mod(nm);
|
---|
[603] | 115 | if (nm.isTool())
|
---|
[602] | 116 | tools.put(m.getPackageNumber(), m);
|
---|
| 117 | else
|
---|
| 118 | mods.put(m.getPackageNumber(), m);
|
---|
[600] | 119 | modFolders.remove(m.getPackageNumber());
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | for (Mod m : modFolders.values()) {
|
---|
[608] | 124 | if (!m.isMandatoryMod()) {
|
---|
| 125 | localType.addEntry(m);
|
---|
| 126 | m.getTypes().add(localType);
|
---|
| 127 | }
|
---|
[600] | 128 | mods.put(m.getPackageNumber(), m);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[604] | 131 | currentlyInstalled = Installer.getInstalledMods();
|
---|
| 132 | if (currentlyInstalled == null)
|
---|
| 133 | currentlyInstalled = new Vector<Integer>();
|
---|
[600] | 134 | }
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * @return Singleton instance
|
---|
| 138 | */
|
---|
| 139 | public static ModManager getInstance() {
|
---|
| 140 | return instance;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | Type getTypeByName(String name) {
|
---|
| 144 | return types.get(name);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
| 148 | * @return Collection of types which do have mods associated
|
---|
| 149 | */
|
---|
| 150 | public Collection<Type> getTypesWithContent() {
|
---|
| 151 | Vector<Type> res = new Vector<Type>();
|
---|
| 152 | for (Type t : types.values()) {
|
---|
| 153 | if (t.getEntries().size() > 0)
|
---|
| 154 | res.add(t);
|
---|
| 155 | }
|
---|
| 156 | return res;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /**
|
---|
[608] | 160 | * @return Collection of mods valid on this platform and not mandatory
|
---|
[600] | 161 | */
|
---|
[608] | 162 | public Collection<Mod> getModsValidAndNotMandatory() {
|
---|
| 163 | Vector<Mod> res = new Vector<Mod>();
|
---|
| 164 | for (Mod m : mods.values())
|
---|
| 165 | if (m.isValidOnPlatform() && !m.isMandatoryMod())
|
---|
| 166 | res.add(m);
|
---|
| 167 | return res;
|
---|
[600] | 168 | }
|
---|
| 169 |
|
---|
| 170 | /**
|
---|
[608] | 171 | * @return Mods which are always installed and valid on this platform
|
---|
[600] | 172 | */
|
---|
[604] | 173 | public TreeSet<Mod> getMandatoryMods() {
|
---|
| 174 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
[600] | 175 | for (Mod m : mods.values()) {
|
---|
[608] | 176 | if (m.isValidOnPlatform() && m.isMandatoryMod())
|
---|
[600] | 177 | res.add(m);
|
---|
| 178 | }
|
---|
| 179 | return res;
|
---|
| 180 | }
|
---|
[604] | 181 |
|
---|
[602] | 182 | /**
|
---|
[621] | 183 | * @return Mods which are already locally available
|
---|
| 184 | */
|
---|
| 185 | public TreeSet<Mod> getLocalAvailableMods() {
|
---|
| 186 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
| 187 | for (Mod m : mods.values()) {
|
---|
| 188 | if (m.isLocalAvailable())
|
---|
| 189 | res.add(m);
|
---|
| 190 | }
|
---|
| 191 | return res;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /**
|
---|
| 195 | * @return Mods which can be updated
|
---|
| 196 | */
|
---|
| 197 | public TreeSet<Mod> getUpdatableMods() {
|
---|
| 198 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
| 199 | for (Mod m : getLocalAvailableMods()) {
|
---|
| 200 | if (m.isNewerAvailable())
|
---|
| 201 | res.add(m);
|
---|
| 202 | }
|
---|
| 203 | return res;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | /**
|
---|
[608] | 207 | * @return Collection of tools valid on this platform and not mandatory
|
---|
[602] | 208 | */
|
---|
[626] | 209 | public TreeMap<String, Mod> getTools() {
|
---|
| 210 | TreeMap<String, Mod> res = new TreeMap<String, Mod>();
|
---|
[608] | 211 | for (Mod m : tools.values())
|
---|
| 212 | if (m.isValidOnPlatform() && !m.isMandatoryMod())
|
---|
[626] | 213 | res.put(m.getName(), m);
|
---|
[608] | 214 | return res;
|
---|
[602] | 215 | }
|
---|
[604] | 216 |
|
---|
[602] | 217 | /**
|
---|
[608] | 218 | * @return Tools which are always installed and valid on this platform
|
---|
[602] | 219 | */
|
---|
[604] | 220 | public TreeSet<Mod> getMandatoryTools() {
|
---|
| 221 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
[602] | 222 | for (Mod m : tools.values()) {
|
---|
[608] | 223 | if (m.isValidOnPlatform() && m.isMandatoryMod())
|
---|
[602] | 224 | res.add(m);
|
---|
| 225 | }
|
---|
| 226 | return res;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | /**
|
---|
[621] | 230 | * @return Tools which are already locally available
|
---|
| 231 | */
|
---|
| 232 | public TreeSet<Mod> getLocalAvailableTools() {
|
---|
| 233 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
| 234 | for (Mod m : tools.values()) {
|
---|
| 235 | if (m.isLocalAvailable())
|
---|
| 236 | res.add(m);
|
---|
| 237 | }
|
---|
| 238 | return res;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | /**
|
---|
| 242 | * @return Tools which can be updated
|
---|
| 243 | */
|
---|
| 244 | public TreeSet<Mod> getUpdatableTools() {
|
---|
| 245 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
| 246 | for (Mod m : getLocalAvailableTools()) {
|
---|
| 247 | if (m.isNewerAvailable())
|
---|
| 248 | res.add(m);
|
---|
| 249 | }
|
---|
| 250 | return res;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /**
|
---|
[623] | 254 | * @return Currently installed tools
|
---|
| 255 | */
|
---|
| 256 | public TreeSet<Mod> getInstalledTools() {
|
---|
| 257 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
| 258 | for (int n : Installer.getInstalledTools()) {
|
---|
| 259 | res.add(getModByNumber(n));
|
---|
| 260 | }
|
---|
| 261 | return res;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | /**
|
---|
| 265 | * Get a mod/tool by its package number
|
---|
[602] | 266 | *
|
---|
| 267 | * @param number
|
---|
| 268 | * Package number
|
---|
[623] | 269 | * @return Mod/tool or null
|
---|
[602] | 270 | */
|
---|
[636] | 271 | private Mod getModByNumber(int number) {
|
---|
[623] | 272 | if (mods.containsKey(number))
|
---|
| 273 | return mods.get(number);
|
---|
| 274 | if (tools.containsKey(number))
|
---|
| 275 | return tools.get(number);
|
---|
[602] | 276 | return null;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | /**
|
---|
| 280 | * Check for unresolved dependencies within the given mods
|
---|
| 281 | *
|
---|
| 282 | * @param mods
|
---|
| 283 | * Mods to check
|
---|
| 284 | * @return Unmet dependencies
|
---|
| 285 | */
|
---|
| 286 | public HashMap<Mod, HashSet<Mod>> checkDependencies(TreeSet<Mod> mods) {
|
---|
[624] | 287 | // TODO: Verify functionality (checkDependencies)
|
---|
[602] | 288 | HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
|
---|
| 289 |
|
---|
| 290 | for (Mod m : mods) {
|
---|
| 291 | for (int depNum : m.getDependencies()) {
|
---|
| 292 | Mod other = getModByNumber(depNum);
|
---|
| 293 | if (!mods.contains(other)) {
|
---|
| 294 | if (!res.containsKey(m))
|
---|
| 295 | res.put(m, new HashSet<Mod>());
|
---|
| 296 | res.get(m).add(other);
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | return res;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | /**
|
---|
[608] | 305 | * Check for incompabitilites between given mods
|
---|
[602] | 306 | *
|
---|
| 307 | * @param mods
|
---|
| 308 | * Mods to check
|
---|
[608] | 309 | * @return Incompatible mods
|
---|
[602] | 310 | */
|
---|
[608] | 311 | public HashMap<Mod, HashSet<Mod>> checkIncompabitilites(TreeSet<Mod> mods) {
|
---|
[624] | 312 | // TODO: Verify functionality (checkIncompatibilities)
|
---|
[602] | 313 | HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
|
---|
| 314 |
|
---|
| 315 | for (Mod m : mods) {
|
---|
[608] | 316 | for (int confNum : m.getIncompabitilities()) {
|
---|
[602] | 317 | Mod other = getModByNumber(confNum);
|
---|
| 318 | if (mods.contains(other)) {
|
---|
| 319 | if (!res.containsKey(m))
|
---|
| 320 | res.put(m, new HashSet<Mod>());
|
---|
| 321 | res.get(m).add(other);
|
---|
| 322 | }
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | return res;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[604] | 329 | /**
|
---|
| 330 | * @param m
|
---|
| 331 | * Mod to check
|
---|
| 332 | * @return Is mod installed?
|
---|
| 333 | */
|
---|
[631] | 334 | boolean isModInstalled(Mod m) {
|
---|
[604] | 335 | return currentlyInstalled.contains(m.getPackageNumber());
|
---|
| 336 | }
|
---|
[600] | 337 | }
|
---|