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

Last change on this file since 638 was 637, checked in by alloc, 12 years ago

AEI2 0.94:

  • Added context menu item to open Depot page of mod in table
  • Added last-change column to 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
[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
[637]131 updateInstalledMods();
132 }
133
134 /**
135 * Update the list of currently installed mods
136 */
137 public void updateInstalledMods() {
[604]138 currentlyInstalled = Installer.getInstalledMods();
139 if (currentlyInstalled == null)
140 currentlyInstalled = new Vector<Integer>();
[600]141 }
142
143 /**
144 * @return Singleton instance
145 */
146 public static ModManager getInstance() {
147 return instance;
148 }
149
150 Type getTypeByName(String name) {
151 return types.get(name);
152 }
153
154 /**
155 * @return Collection of types which do have mods associated
156 */
157 public Collection<Type> getTypesWithContent() {
158 Vector<Type> res = new Vector<Type>();
159 for (Type t : types.values()) {
160 if (t.getEntries().size() > 0)
161 res.add(t);
162 }
163 return res;
164 }
165
166 /**
[608]167 * @return Collection of mods valid on this platform and not mandatory
[600]168 */
[608]169 public Collection<Mod> getModsValidAndNotMandatory() {
170 Vector<Mod> res = new Vector<Mod>();
171 for (Mod m : mods.values())
172 if (m.isValidOnPlatform() && !m.isMandatoryMod())
173 res.add(m);
174 return res;
[600]175 }
176
177 /**
[608]178 * @return Mods which are always installed and valid on this platform
[600]179 */
[604]180 public TreeSet<Mod> getMandatoryMods() {
181 TreeSet<Mod> res = new TreeSet<Mod>();
[600]182 for (Mod m : mods.values()) {
[608]183 if (m.isValidOnPlatform() && m.isMandatoryMod())
[600]184 res.add(m);
185 }
186 return res;
187 }
[604]188
[602]189 /**
[621]190 * @return Mods which are already locally available
191 */
192 public TreeSet<Mod> getLocalAvailableMods() {
193 TreeSet<Mod> res = new TreeSet<Mod>();
194 for (Mod m : mods.values()) {
195 if (m.isLocalAvailable())
196 res.add(m);
197 }
198 return res;
199 }
200
201 /**
202 * @return Mods which can be updated
203 */
204 public TreeSet<Mod> getUpdatableMods() {
205 TreeSet<Mod> res = new TreeSet<Mod>();
206 for (Mod m : getLocalAvailableMods()) {
207 if (m.isNewerAvailable())
208 res.add(m);
209 }
210 return res;
211 }
212
213 /**
[608]214 * @return Collection of tools valid on this platform and not mandatory
[602]215 */
[626]216 public TreeMap<String, Mod> getTools() {
217 TreeMap<String, Mod> res = new TreeMap<String, Mod>();
[608]218 for (Mod m : tools.values())
219 if (m.isValidOnPlatform() && !m.isMandatoryMod())
[626]220 res.put(m.getName(), m);
[608]221 return res;
[602]222 }
[604]223
[602]224 /**
[608]225 * @return Tools which are always installed and valid on this platform
[602]226 */
[604]227 public TreeSet<Mod> getMandatoryTools() {
228 TreeSet<Mod> res = new TreeSet<Mod>();
[602]229 for (Mod m : tools.values()) {
[608]230 if (m.isValidOnPlatform() && m.isMandatoryMod())
[602]231 res.add(m);
232 }
233 return res;
234 }
235
236 /**
[621]237 * @return Tools which are already locally available
238 */
239 public TreeSet<Mod> getLocalAvailableTools() {
240 TreeSet<Mod> res = new TreeSet<Mod>();
241 for (Mod m : tools.values()) {
242 if (m.isLocalAvailable())
243 res.add(m);
244 }
245 return res;
246 }
247
248 /**
249 * @return Tools which can be updated
250 */
251 public TreeSet<Mod> getUpdatableTools() {
252 TreeSet<Mod> res = new TreeSet<Mod>();
253 for (Mod m : getLocalAvailableTools()) {
254 if (m.isNewerAvailable())
255 res.add(m);
256 }
257 return res;
258 }
259
260 /**
[623]261 * @return Currently installed tools
262 */
263 public TreeSet<Mod> getInstalledTools() {
264 TreeSet<Mod> res = new TreeSet<Mod>();
265 for (int n : Installer.getInstalledTools()) {
266 res.add(getModByNumber(n));
267 }
268 return res;
269 }
270
271 /**
272 * Get a mod/tool by its package number
[602]273 *
274 * @param number
275 * Package number
[623]276 * @return Mod/tool or null
[602]277 */
[636]278 private Mod getModByNumber(int number) {
[623]279 if (mods.containsKey(number))
280 return mods.get(number);
281 if (tools.containsKey(number))
282 return tools.get(number);
[602]283 return null;
284 }
285
286 /**
287 * Check for unresolved dependencies within the given mods
288 *
289 * @param mods
290 * Mods to check
291 * @return Unmet dependencies
292 */
293 public HashMap<Mod, HashSet<Mod>> checkDependencies(TreeSet<Mod> mods) {
[624]294 // TODO: Verify functionality (checkDependencies)
[602]295 HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
296
297 for (Mod m : mods) {
298 for (int depNum : m.getDependencies()) {
299 Mod other = getModByNumber(depNum);
300 if (!mods.contains(other)) {
301 if (!res.containsKey(m))
302 res.put(m, new HashSet<Mod>());
303 res.get(m).add(other);
304 }
305 }
306 }
307
308 return res;
309 }
310
311 /**
[608]312 * Check for incompabitilites between given mods
[602]313 *
314 * @param mods
315 * Mods to check
[608]316 * @return Incompatible mods
[602]317 */
[608]318 public HashMap<Mod, HashSet<Mod>> checkIncompabitilites(TreeSet<Mod> mods) {
[624]319 // TODO: Verify functionality (checkIncompatibilities)
[602]320 HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
321
322 for (Mod m : mods) {
[608]323 for (int confNum : m.getIncompabitilities()) {
[602]324 Mod other = getModByNumber(confNum);
325 if (mods.contains(other)) {
326 if (!res.containsKey(m))
327 res.put(m, new HashSet<Mod>());
328 res.get(m).add(other);
329 }
330 }
331 }
332
333 return res;
334 }
335
[604]336 /**
337 * @param m
338 * Mod to check
339 * @return Is mod installed?
340 */
[631]341 boolean isModInstalled(Mod m) {
[604]342 return currentlyInstalled.contains(m.getPackageNumber());
343 }
[600]344}
Note: See TracBrowser for help on using the repository browser.