[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;
|
---|
| 12 | import java.util.TreeSet;
|
---|
[600] | 13 | import java.util.Vector;
|
---|
| 14 |
|
---|
[604] | 15 | import com.thoughtworks.xstream.XStream;
|
---|
| 16 | import com.thoughtworks.xstream.io.xml.StaxDriver;
|
---|
| 17 |
|
---|
[600] | 18 | import net.oni2.aeinstaller.backend.Paths;
|
---|
| 19 | import net.oni2.aeinstaller.backend.depot.DepotManager;
|
---|
| 20 | import net.oni2.aeinstaller.backend.depot.model.NodeMod;
|
---|
| 21 | import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm;
|
---|
[604] | 22 | import net.oni2.aeinstaller.backend.oni.Installer;
|
---|
[600] | 23 |
|
---|
| 24 | /**
|
---|
| 25 | * @author Christian Illy
|
---|
| 26 | */
|
---|
| 27 | public class ModManager {
|
---|
| 28 | private static ModManager instance = new ModManager();
|
---|
| 29 |
|
---|
| 30 | private HashMap<String, Type> types = new HashMap<String, Type>();
|
---|
| 31 | private HashMap<Integer, Mod> mods = new HashMap<Integer, Mod>();
|
---|
[602] | 32 | private HashMap<Integer, Mod> tools = new HashMap<Integer, Mod>();
|
---|
[600] | 33 |
|
---|
[604] | 34 | private Vector<Integer> currentlyInstalled = new Vector<Integer>();
|
---|
| 35 |
|
---|
[600] | 36 | /**
|
---|
[604] | 37 | * @param f
|
---|
| 38 | * Mod selection file
|
---|
| 39 | * @return Mod selection
|
---|
| 40 | */
|
---|
| 41 | @SuppressWarnings("unchecked")
|
---|
| 42 | public Vector<Integer> loadModSelection(File f) {
|
---|
| 43 | Vector<Integer> res = new Vector<Integer>();
|
---|
| 44 | try {
|
---|
[608] | 45 | if (f.exists()) {
|
---|
| 46 | FileInputStream fis = new FileInputStream(f);
|
---|
| 47 | XStream xs = new XStream(new StaxDriver());
|
---|
| 48 | Object obj = xs.fromXML(fis);
|
---|
| 49 | if (obj instanceof Vector<?>)
|
---|
| 50 | res = (Vector<Integer>) obj;
|
---|
| 51 | fis.close();
|
---|
| 52 | }
|
---|
[604] | 53 | } catch (FileNotFoundException e) {
|
---|
| 54 | e.printStackTrace();
|
---|
| 55 | } catch (IOException e) {
|
---|
| 56 | e.printStackTrace();
|
---|
| 57 | }
|
---|
| 58 | return res;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * @param f
|
---|
| 63 | * Mod selection file
|
---|
| 64 | * @param mods
|
---|
| 65 | * Selected mods
|
---|
| 66 | */
|
---|
| 67 | public void saveModSelection(File f, TreeSet<Mod> mods) {
|
---|
| 68 | try {
|
---|
| 69 | Vector<Integer> installed = new Vector<Integer>();
|
---|
| 70 | for (Mod m : mods) {
|
---|
| 71 | installed.add(m.getPackageNumber());
|
---|
| 72 | }
|
---|
| 73 | FileOutputStream fos = new FileOutputStream(f);
|
---|
| 74 | XStream xs = new XStream(new StaxDriver());
|
---|
| 75 | xs.toXML(installed, fos);
|
---|
| 76 | fos.close();
|
---|
| 77 | } catch (FileNotFoundException e) {
|
---|
| 78 | e.printStackTrace();
|
---|
| 79 | } catch (IOException e) {
|
---|
| 80 | e.printStackTrace();
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
[600] | 85 | * First initialization of ModManager
|
---|
| 86 | */
|
---|
| 87 | public void init() {
|
---|
| 88 | types = new HashMap<String, Type>();
|
---|
| 89 | mods = new HashMap<Integer, Mod>();
|
---|
| 90 |
|
---|
[608] | 91 | Type localType = new Type("-Local-", null);
|
---|
| 92 | types.put("-Local-", localType);
|
---|
[600] | 93 |
|
---|
| 94 | for (TaxonomyTerm tt : DepotManager.getInstance()
|
---|
| 95 | .getTaxonomyTermsByVocabulary(
|
---|
| 96 | DepotManager.getInstance().getVocabIdType())) {
|
---|
| 97 | types.put(tt.getName(), new Type(tt.getName(), tt));
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | HashMap<Integer, Mod> modFolders = new HashMap<Integer, Mod>();
|
---|
| 101 | if (Paths.getModsPath().exists()) {
|
---|
| 102 | for (File f : Paths.getModsPath().listFiles(new FileFilter() {
|
---|
| 103 | @Override
|
---|
| 104 | public boolean accept(File pathname) {
|
---|
| 105 | return pathname.isDirectory();
|
---|
| 106 | }
|
---|
| 107 | })) {
|
---|
| 108 | Mod m = new Mod(f);
|
---|
| 109 | modFolders.put(m.getPackageNumber(), m);
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | for (NodeMod nm : DepotManager.getInstance().getModPackageNodes()) {
|
---|
| 114 | if (nm.getUploads().size() == 1) {
|
---|
| 115 | Mod m = new Mod(nm);
|
---|
[603] | 116 | if (nm.isTool())
|
---|
[602] | 117 | tools.put(m.getPackageNumber(), m);
|
---|
| 118 | else
|
---|
| 119 | mods.put(m.getPackageNumber(), m);
|
---|
[600] | 120 | modFolders.remove(m.getPackageNumber());
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | for (Mod m : modFolders.values()) {
|
---|
[608] | 125 | if (!m.isMandatoryMod()) {
|
---|
| 126 | localType.addEntry(m);
|
---|
| 127 | m.getTypes().add(localType);
|
---|
| 128 | }
|
---|
[600] | 129 | mods.put(m.getPackageNumber(), m);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[604] | 132 | currentlyInstalled = Installer.getInstalledMods();
|
---|
| 133 | if (currentlyInstalled == null)
|
---|
| 134 | currentlyInstalled = new Vector<Integer>();
|
---|
[600] | 135 | }
|
---|
| 136 |
|
---|
| 137 | /**
|
---|
| 138 | * @return Singleton instance
|
---|
| 139 | */
|
---|
| 140 | public static ModManager getInstance() {
|
---|
| 141 | return instance;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | Type getTypeByName(String name) {
|
---|
| 145 | return types.get(name);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * @return Collection of types which do have mods associated
|
---|
| 150 | */
|
---|
| 151 | public Collection<Type> getTypesWithContent() {
|
---|
| 152 | Vector<Type> res = new Vector<Type>();
|
---|
| 153 | for (Type t : types.values()) {
|
---|
| 154 | if (t.getEntries().size() > 0)
|
---|
| 155 | res.add(t);
|
---|
| 156 | }
|
---|
| 157 | return res;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /**
|
---|
[608] | 161 | * @return Collection of mods valid on this platform and not mandatory
|
---|
[600] | 162 | */
|
---|
[608] | 163 | public Collection<Mod> getModsValidAndNotMandatory() {
|
---|
| 164 | Vector<Mod> res = new Vector<Mod>();
|
---|
| 165 | for (Mod m : mods.values())
|
---|
| 166 | if (m.isValidOnPlatform() && !m.isMandatoryMod())
|
---|
| 167 | res.add(m);
|
---|
| 168 | return res;
|
---|
[600] | 169 | }
|
---|
| 170 |
|
---|
| 171 | /**
|
---|
[608] | 172 | * @return Mods which are always installed and valid on this platform
|
---|
[600] | 173 | */
|
---|
[604] | 174 | public TreeSet<Mod> getMandatoryMods() {
|
---|
| 175 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
[600] | 176 | for (Mod m : mods.values()) {
|
---|
[608] | 177 | if (m.isValidOnPlatform() && m.isMandatoryMod())
|
---|
[600] | 178 | res.add(m);
|
---|
| 179 | }
|
---|
| 180 | return res;
|
---|
| 181 | }
|
---|
[604] | 182 |
|
---|
[602] | 183 | /**
|
---|
[608] | 184 | * @return Collection of tools valid on this platform and not mandatory
|
---|
[602] | 185 | */
|
---|
| 186 | public Collection<Mod> getTools() {
|
---|
[608] | 187 | Vector<Mod> res = new Vector<Mod>();
|
---|
| 188 | for (Mod m : tools.values())
|
---|
| 189 | if (m.isValidOnPlatform() && !m.isMandatoryMod())
|
---|
| 190 | res.add(m);
|
---|
| 191 | return res;
|
---|
[602] | 192 | }
|
---|
[604] | 193 |
|
---|
[602] | 194 | /**
|
---|
[608] | 195 | * @return Tools which are always installed and valid on this platform
|
---|
[602] | 196 | */
|
---|
[604] | 197 | public TreeSet<Mod> getMandatoryTools() {
|
---|
| 198 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
[602] | 199 | for (Mod m : tools.values()) {
|
---|
[608] | 200 | if (m.isValidOnPlatform() && m.isMandatoryMod())
|
---|
[602] | 201 | res.add(m);
|
---|
| 202 | }
|
---|
| 203 | return res;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | /**
|
---|
| 207 | * Get a mod by its package number
|
---|
| 208 | *
|
---|
| 209 | * @param number
|
---|
| 210 | * Package number
|
---|
| 211 | * @return Mod or null
|
---|
| 212 | */
|
---|
| 213 | public Mod getModByNumber(int number) {
|
---|
| 214 | for (Mod m : mods.values()) {
|
---|
| 215 | if (m.getPackageNumber() == number)
|
---|
| 216 | return m;
|
---|
| 217 | }
|
---|
| 218 | return null;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /**
|
---|
| 222 | * Check for unresolved dependencies within the given mods
|
---|
| 223 | *
|
---|
| 224 | * @param mods
|
---|
| 225 | * Mods to check
|
---|
| 226 | * @return Unmet dependencies
|
---|
| 227 | */
|
---|
| 228 | public HashMap<Mod, HashSet<Mod>> checkDependencies(TreeSet<Mod> mods) {
|
---|
| 229 | // TODO: Verify functionality
|
---|
| 230 | HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
|
---|
| 231 |
|
---|
| 232 | for (Mod m : mods) {
|
---|
| 233 | for (int depNum : m.getDependencies()) {
|
---|
| 234 | Mod other = getModByNumber(depNum);
|
---|
| 235 | if (!mods.contains(other)) {
|
---|
| 236 | if (!res.containsKey(m))
|
---|
| 237 | res.put(m, new HashSet<Mod>());
|
---|
| 238 | res.get(m).add(other);
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | return res;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | /**
|
---|
[608] | 247 | * Check for incompabitilites between given mods
|
---|
[602] | 248 | *
|
---|
| 249 | * @param mods
|
---|
| 250 | * Mods to check
|
---|
[608] | 251 | * @return Incompatible mods
|
---|
[602] | 252 | */
|
---|
[608] | 253 | public HashMap<Mod, HashSet<Mod>> checkIncompabitilites(TreeSet<Mod> mods) {
|
---|
[602] | 254 | // TODO: Verify functionality
|
---|
| 255 | HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
|
---|
| 256 |
|
---|
| 257 | for (Mod m : mods) {
|
---|
[608] | 258 | for (int confNum : m.getIncompabitilities()) {
|
---|
[602] | 259 | Mod other = getModByNumber(confNum);
|
---|
| 260 | if (mods.contains(other)) {
|
---|
| 261 | if (!res.containsKey(m))
|
---|
| 262 | res.put(m, new HashSet<Mod>());
|
---|
| 263 | res.get(m).add(other);
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | return res;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[604] | 271 | /**
|
---|
| 272 | * @param m
|
---|
| 273 | * Mod to check
|
---|
| 274 | * @return Is mod installed?
|
---|
| 275 | */
|
---|
| 276 | public boolean isModInstalled(Mod m) {
|
---|
| 277 | return currentlyInstalled.contains(m.getPackageNumber());
|
---|
| 278 | }
|
---|
[600] | 279 | }
|
---|