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

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

AEI2 0.84:

  • Actually *do* update packages
  • Uninstall backend for tools
File size: 8.0 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;
12import java.util.TreeSet;
[600]13import java.util.Vector;
14
[604]15import com.thoughtworks.xstream.XStream;
16import com.thoughtworks.xstream.io.xml.StaxDriver;
17
[600]18import net.oni2.aeinstaller.backend.Paths;
19import net.oni2.aeinstaller.backend.depot.DepotManager;
20import net.oni2.aeinstaller.backend.depot.model.NodeMod;
21import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm;
[604]22import net.oni2.aeinstaller.backend.oni.Installer;
[600]23
24/**
25 * @author Christian Illy
26 */
27public 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 /**
[621]184 * @return Mods which are already locally available
185 */
186 public TreeSet<Mod> getLocalAvailableMods() {
187 TreeSet<Mod> res = new TreeSet<Mod>();
188 for (Mod m : mods.values()) {
189 if (m.isLocalAvailable())
190 res.add(m);
191 }
192 return res;
193 }
194
195 /**
196 * @return Mods which can be updated
197 */
198 public TreeSet<Mod> getUpdatableMods() {
199 TreeSet<Mod> res = new TreeSet<Mod>();
200 for (Mod m : getLocalAvailableMods()) {
201 if (m.isNewerAvailable())
202 res.add(m);
203 }
204 return res;
205 }
206
207 /**
[608]208 * @return Collection of tools valid on this platform and not mandatory
[602]209 */
210 public Collection<Mod> getTools() {
[608]211 Vector<Mod> res = new Vector<Mod>();
212 for (Mod m : tools.values())
213 if (m.isValidOnPlatform() && !m.isMandatoryMod())
214 res.add(m);
215 return res;
[602]216 }
[604]217
[602]218 /**
[608]219 * @return Tools which are always installed and valid on this platform
[602]220 */
[604]221 public TreeSet<Mod> getMandatoryTools() {
222 TreeSet<Mod> res = new TreeSet<Mod>();
[602]223 for (Mod m : tools.values()) {
[608]224 if (m.isValidOnPlatform() && m.isMandatoryMod())
[602]225 res.add(m);
226 }
227 return res;
228 }
229
230 /**
[621]231 * @return Tools which are already locally available
232 */
233 public TreeSet<Mod> getLocalAvailableTools() {
234 TreeSet<Mod> res = new TreeSet<Mod>();
235 for (Mod m : tools.values()) {
236 if (m.isLocalAvailable())
237 res.add(m);
238 }
239 return res;
240 }
241
242 /**
243 * @return Tools which can be updated
244 */
245 public TreeSet<Mod> getUpdatableTools() {
246 TreeSet<Mod> res = new TreeSet<Mod>();
247 for (Mod m : getLocalAvailableTools()) {
248 if (m.isNewerAvailable())
249 res.add(m);
250 }
251 return res;
252 }
253
254 /**
[623]255 * @return Currently installed tools
256 */
257 public TreeSet<Mod> getInstalledTools() {
258 TreeSet<Mod> res = new TreeSet<Mod>();
259 for (int n : Installer.getInstalledTools()) {
260 res.add(getModByNumber(n));
261 }
262 return res;
263 }
264
265 /**
266 * Get a mod/tool by its package number
[602]267 *
268 * @param number
269 * Package number
[623]270 * @return Mod/tool or null
[602]271 */
272 public Mod getModByNumber(int number) {
[623]273 if (mods.containsKey(number))
274 return mods.get(number);
275 if (tools.containsKey(number))
276 return tools.get(number);
[602]277 return null;
278 }
279
280 /**
281 * Check for unresolved dependencies within the given mods
282 *
283 * @param mods
284 * Mods to check
285 * @return Unmet dependencies
286 */
287 public HashMap<Mod, HashSet<Mod>> checkDependencies(TreeSet<Mod> mods) {
[624]288 // TODO: Verify functionality (checkDependencies)
[602]289 HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
290
291 for (Mod m : mods) {
292 for (int depNum : m.getDependencies()) {
293 Mod other = getModByNumber(depNum);
294 if (!mods.contains(other)) {
295 if (!res.containsKey(m))
296 res.put(m, new HashSet<Mod>());
297 res.get(m).add(other);
298 }
299 }
300 }
301
302 return res;
303 }
304
305 /**
[608]306 * Check for incompabitilites between given mods
[602]307 *
308 * @param mods
309 * Mods to check
[608]310 * @return Incompatible mods
[602]311 */
[608]312 public HashMap<Mod, HashSet<Mod>> checkIncompabitilites(TreeSet<Mod> mods) {
[624]313 // TODO: Verify functionality (checkIncompatibilities)
[602]314 HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
315
316 for (Mod m : mods) {
[608]317 for (int confNum : m.getIncompabitilities()) {
[602]318 Mod other = getModByNumber(confNum);
319 if (mods.contains(other)) {
320 if (!res.containsKey(m))
321 res.put(m, new HashSet<Mod>());
322 res.get(m).add(other);
323 }
324 }
325 }
326
327 return res;
328 }
329
[604]330 /**
331 * @param m
332 * Mod to check
333 * @return Is mod installed?
334 */
335 public boolean isModInstalled(Mod m) {
336 return currentlyInstalled.contains(m.getPackageNumber());
337 }
[600]338}
Note: See TracBrowser for help on using the repository browser.