| 1 |
package net.oni2.aeinstaller.backend.mods; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.FileFilter; |
| 5 |
import java.util.Collection; |
| 6 |
import java.util.HashMap; |
| 7 |
import java.util.HashSet; |
| 8 |
import java.util.TreeSet; |
| 9 |
import java.util.Vector; |
| 10 |
|
| 11 |
import net.oni2.aeinstaller.backend.Paths; |
| 12 |
import net.oni2.aeinstaller.backend.depot.DepotManager; |
| 13 |
import net.oni2.aeinstaller.backend.depot.model.NodeMod; |
| 14 |
import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm; |
| 15 |
|
| 16 |
/** |
| 17 |
* @author Christian Illy |
| 18 |
*/ |
| 19 |
public class ModManager { |
| 20 |
// Download mods |
| 21 |
// Update mods |
| 22 |
|
| 23 |
private static ModManager instance = new ModManager(); |
| 24 |
|
| 25 |
private HashMap<String, Type> types = new HashMap<String, Type>(); |
| 26 |
private HashMap<Integer, Mod> mods = new HashMap<Integer, Mod>(); |
| 27 |
private HashMap<Integer, Mod> tools = new HashMap<Integer, Mod>(); |
| 28 |
|
| 29 |
/** |
| 30 |
* First initialization of ModManager |
| 31 |
*/ |
| 32 |
public void init() { |
| 33 |
types = new HashMap<String, Type>(); |
| 34 |
mods = new HashMap<Integer, Mod>(); |
| 35 |
|
| 36 |
types.put("-Local-", new Type("-Local-", null)); |
| 37 |
|
| 38 |
for (TaxonomyTerm tt : DepotManager.getInstance() |
| 39 |
.getTaxonomyTermsByVocabulary( |
| 40 |
DepotManager.getInstance().getVocabIdType())) { |
| 41 |
types.put(tt.getName(), new Type(tt.getName(), tt)); |
| 42 |
} |
| 43 |
|
| 44 |
HashMap<Integer, Mod> modFolders = new HashMap<Integer, Mod>(); |
| 45 |
if (Paths.getModsPath().exists()) { |
| 46 |
for (File f : Paths.getModsPath().listFiles(new FileFilter() { |
| 47 |
@Override |
| 48 |
public boolean accept(File pathname) { |
| 49 |
return pathname.isDirectory(); |
| 50 |
} |
| 51 |
})) { |
| 52 |
Mod m = new Mod(f); |
| 53 |
modFolders.put(m.getPackageNumber(), m); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
for (NodeMod nm : DepotManager.getInstance().getModPackageNodes()) { |
| 58 |
if (nm.getUploads().size() == 1) { |
| 59 |
Mod m = new Mod(nm); |
| 60 |
if (nm.isTool()) |
| 61 |
tools.put(m.getPackageNumber(), m); |
| 62 |
else |
| 63 |
mods.put(m.getPackageNumber(), m); |
| 64 |
modFolders.remove(m.getPackageNumber()); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
for (Mod m : modFolders.values()) { |
| 69 |
mods.put(m.getPackageNumber(), m); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public void refreshLocalMods() { |
| 74 |
// TODO: evtl nur private e.g. als listener für downloads? |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @return Singleton instance |
| 79 |
*/ |
| 80 |
public static ModManager getInstance() { |
| 81 |
return instance; |
| 82 |
} |
| 83 |
|
| 84 |
Type getTypeByName(String name) { |
| 85 |
return types.get(name); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @return Collection of types which do have mods associated |
| 90 |
*/ |
| 91 |
public Collection<Type> getTypesWithContent() { |
| 92 |
Vector<Type> res = new Vector<Type>(); |
| 93 |
for (Type t : types.values()) { |
| 94 |
if (t.getEntries().size() > 0) |
| 95 |
res.add(t); |
| 96 |
} |
| 97 |
return res; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @return Collection of mods |
| 102 |
*/ |
| 103 |
public Collection<Mod> getMods() { |
| 104 |
return mods.values(); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @return Mods which are always installed |
| 109 |
*/ |
| 110 |
public Collection<Mod> getMandatoryMods() { |
| 111 |
Vector<Mod> res = new Vector<Mod>(); |
| 112 |
for (Mod m : mods.values()) { |
| 113 |
if (m.isMandatoryMod()) |
| 114 |
res.add(m); |
| 115 |
} |
| 116 |
return res; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @return Collection of tools |
| 121 |
*/ |
| 122 |
public Collection<Mod> getTools() { |
| 123 |
return tools.values(); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @return Tools which are always installed |
| 128 |
*/ |
| 129 |
public Collection<Mod> getMandatoryTools() { |
| 130 |
Vector<Mod> res = new Vector<Mod>(); |
| 131 |
for (Mod m : tools.values()) { |
| 132 |
if (m.isMandatoryMod()) |
| 133 |
res.add(m); |
| 134 |
} |
| 135 |
return res; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get a mod by its package number |
| 140 |
* |
| 141 |
* @param number |
| 142 |
* Package number |
| 143 |
* @return Mod or null |
| 144 |
*/ |
| 145 |
public Mod getModByNumber(int number) { |
| 146 |
for (Mod m : mods.values()) { |
| 147 |
if (m.getPackageNumber() == number) |
| 148 |
return m; |
| 149 |
} |
| 150 |
return null; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Check for unresolved dependencies within the given mods |
| 155 |
* |
| 156 |
* @param mods |
| 157 |
* Mods to check |
| 158 |
* @return Unmet dependencies |
| 159 |
*/ |
| 160 |
public HashMap<Mod, HashSet<Mod>> checkDependencies(TreeSet<Mod> mods) { |
| 161 |
// TODO: Verify functionality |
| 162 |
HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>(); |
| 163 |
|
| 164 |
for (Mod m : mods) { |
| 165 |
for (int depNum : m.getDependencies()) { |
| 166 |
Mod other = getModByNumber(depNum); |
| 167 |
if (!mods.contains(other)) { |
| 168 |
if (!res.containsKey(m)) |
| 169 |
res.put(m, new HashSet<Mod>()); |
| 170 |
res.get(m).add(other); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return res; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Check for conflicts between given mods |
| 180 |
* |
| 181 |
* @param mods |
| 182 |
* Mods to check |
| 183 |
* @return Conflicting mods |
| 184 |
*/ |
| 185 |
public HashMap<Mod, HashSet<Mod>> checkConflicts(TreeSet<Mod> mods) { |
| 186 |
// TODO: Verify functionality |
| 187 |
HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>(); |
| 188 |
|
| 189 |
for (Mod m : mods) { |
| 190 |
for (int confNum : m.getConflicts()) { |
| 191 |
Mod other = getModByNumber(confNum); |
| 192 |
if (mods.contains(other)) { |
| 193 |
if (!res.containsKey(m)) |
| 194 |
res.put(m, new HashSet<Mod>()); |
| 195 |
res.get(m).add(other); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return res; |
| 201 |
} |
| 202 |
|
| 203 |
} |