source: AE/installer2/src/net/oni2/aeinstaller/backend/mods/ModManager.java@ 634

Last change on this file since 634 was 631, checked in by alloc, 12 years ago

AEI2 0.90:

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