source: java/installer2/src/net/oni2/aeinstaller/backend/packages/PackageManager.java@ 806

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

AEI2.02:

File size: 8.4 KB
RevLine 
[648]1package net.oni2.aeinstaller.backend.packages;
2
3import java.io.File;
4import java.io.FileFilter;
5import java.io.FileInputStream;
6import java.io.FileNotFoundException;
7import java.io.FileOutputStream;
8import java.io.IOException;
9import java.util.Collection;
10import java.util.HashMap;
11import java.util.HashSet;
12import java.util.TreeSet;
13import java.util.Vector;
14
15import net.oni2.aeinstaller.backend.Paths;
[804]16import net.oni2.aeinstaller.backend.oni.management.Installer;
17import net.oni2.aeinstaller.backend.oni.management.tools.ToolInstallationList;
[749]18import net.oni2.moddepot.DepotManager;
19import net.oni2.moddepot.model.NodeMod;
20import net.oni2.moddepot.model.TaxonomyTerm;
[648]21
[658]22import com.thoughtworks.xstream.XStream;
23import com.thoughtworks.xstream.io.xml.StaxDriver;
24
[648]25/**
26 * @author Christian Illy
27 */
28public class PackageManager {
29 private static PackageManager instance = new PackageManager();
30
31 private HashMap<String, Type> types = new HashMap<String, Type>();
32 private HashMap<Integer, Package> mods = new HashMap<Integer, Package>();
33 private HashMap<Integer, Package> tools = new HashMap<Integer, Package>();
34
35 private Vector<Integer> currentlyInstalled = new Vector<Integer>();
36
37 /**
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 {
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 }
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<Package> mods) {
69 try {
70 Vector<Integer> installed = new Vector<Integer>();
71 for (Package 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 /**
86 * First initialization of ModManager
87 */
88 public void init() {
89 types = new HashMap<String, Type>();
90 mods = new HashMap<Integer, Package>();
91
[749]92 Type localType = new Type("-Local-");
[648]93 types.put("-Local-", localType);
94
95 for (TaxonomyTerm tt : DepotManager.getInstance().getTypes()) {
[749]96 types.put(tt.getName(), new Type(tt.getName()));
[648]97 }
98
99 HashMap<Integer, Package> modFolders = new HashMap<Integer, Package>();
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 Package m = new Package(f);
108 modFolders.put(m.getPackageNumber(), m);
109 }
110 }
111
112 for (NodeMod nm : DepotManager.getInstance().getModPackageNodes()) {
[749]113 if (nm.getUploads().size() == 1) {
[648]114 Package m = new Package(nm);
115 if (nm.isTool())
116 tools.put(m.getPackageNumber(), m);
117 else
118 mods.put(m.getPackageNumber(), m);
119 modFolders.remove(m.getPackageNumber());
120 }
121 }
122
123 for (Package m : modFolders.values()) {
124 if (!m.isCorePackage()) {
125 localType.addEntry(m);
126 m.getTypes().add(localType);
127 }
128 if (m.isTool())
129 tools.put(m.getPackageNumber(), m);
130 else
131 mods.put(m.getPackageNumber(), m);
132 }
133
134 updateInstalledMods();
135 }
136
137 /**
138 * Update the list of currently installed mods
139 */
140 public void updateInstalledMods() {
141 currentlyInstalled = Installer.getInstalledMods();
142 if (currentlyInstalled == null)
143 currentlyInstalled = new Vector<Integer>();
144 }
145
146 /**
147 * @return Singleton instance
148 */
149 public static PackageManager getInstance() {
150 return instance;
151 }
152
153 Type getTypeByName(String name) {
154 return types.get(name);
155 }
156
157 /**
158 * @return Collection of types which do have mods associated
159 */
160 public Collection<Type> getTypesWithContent() {
161 Vector<Type> res = new Vector<Type>();
162 for (Type t : types.values()) {
163 if (t.getEntries().size() > 0)
164 res.add(t);
165 }
166 return res;
167 }
168
169 /**
170 * @return Collection of mods valid on this platform and not core package
171 */
172 public Collection<Package> getModsValidAndNotCore() {
173 Vector<Package> res = new Vector<Package>();
174 for (Package m : mods.values())
175 if (m.isValidOnPlatform() && !m.isCorePackage())
176 res.add(m);
177 return res;
178 }
179
180 /**
181 * @return Mods which are always installed and valid on this platform
182 */
183 public TreeSet<Package> getCoreMods() {
184 TreeSet<Package> res = new TreeSet<Package>();
185 for (Package m : mods.values()) {
186 if (m.isValidOnPlatform() && m.isCorePackage())
187 res.add(m);
188 }
189 return res;
190 }
191
192 /**
193 * @return Mods which are already locally available
194 */
195 public TreeSet<Package> getLocalAvailableMods() {
196 TreeSet<Package> res = new TreeSet<Package>();
197 for (Package m : mods.values()) {
198 if (m.isLocalAvailable())
199 res.add(m);
200 }
201 return res;
202 }
203
204 /**
205 * @return Mods which can be updated
206 */
207 public TreeSet<Package> getUpdatableMods() {
208 TreeSet<Package> res = new TreeSet<Package>();
209 for (Package m : getLocalAvailableMods()) {
210 if (m.isNewerAvailable())
211 res.add(m);
212 }
213 return res;
214 }
215
216 /**
217 * @return Collection of tools valid on this platform and not core
218 */
[658]219 public Collection<Package> getTools() {
220 Vector<Package> res = new Vector<Package>();
[648]221 for (Package m : tools.values())
222 if (m.isValidOnPlatform() && !m.isCorePackage())
[658]223 res.add(m);
[648]224 return res;
225 }
226
227 /**
228 * @return Tools which are always installed and valid on this platform
229 */
230 public TreeSet<Package> getCoreTools() {
231 TreeSet<Package> res = new TreeSet<Package>();
232 for (Package m : tools.values()) {
233 if (m.isValidOnPlatform() && m.isCorePackage())
234 res.add(m);
235 }
236 return res;
237 }
238
239 /**
240 * @return Tools which are already locally available
241 */
242 public TreeSet<Package> getLocalAvailableTools() {
243 TreeSet<Package> res = new TreeSet<Package>();
244 for (Package m : tools.values()) {
245 if (m.isLocalAvailable())
246 res.add(m);
247 }
248 return res;
249 }
250
251 /**
252 * @return Tools which can be updated
253 */
254 public TreeSet<Package> getUpdatableTools() {
255 TreeSet<Package> res = new TreeSet<Package>();
256 for (Package m : getLocalAvailableTools()) {
257 if (m.isNewerAvailable())
258 res.add(m);
259 }
260 return res;
261 }
262
263 /**
264 * @return Currently installed tools
265 */
266 public TreeSet<Package> getInstalledTools() {
267 TreeSet<Package> res = new TreeSet<Package>();
[804]268 for (int n : ToolInstallationList.getInstance().getItems().keySet()) {
[648]269 res.add(getPackageByNumber(n));
270 }
271 return res;
272 }
273
274 /**
275 * Get a mod/tool by its package number
276 *
277 * @param number
278 * Package number
279 * @return Mod/tool or null
280 */
281 private Package getPackageByNumber(int number) {
282 if (mods.containsKey(number))
283 return mods.get(number);
284 if (tools.containsKey(number))
285 return tools.get(number);
286 return null;
287 }
288
289 /**
290 * Check for unresolved dependencies within the given mods
291 *
292 * @param mods
293 * Mods to check
294 * @return Unmet dependencies
295 */
[749]296 public HashMap<Package, HashSet<Package>> checkDependencies(
297 TreeSet<Package> mods) {
[648]298 HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
299
300 for (Package m : mods) {
301 for (int depNum : m.getDependencies()) {
302 Package other = getPackageByNumber(depNum);
[804]303 if (other != null) {
304 if (!mods.contains(other)) {
305 if (!res.containsKey(m))
306 res.put(m, new HashSet<Package>());
307 res.get(m).add(other);
308 }
[648]309 }
310 }
311 }
312
313 return res;
314 }
315
316 /**
317 * Check for incompabitilites between given mods
318 *
319 * @param mods
320 * Mods to check
321 * @return Incompatible mods
322 */
[749]323 public HashMap<Package, HashSet<Package>> checkIncompabitilites(
324 TreeSet<Package> mods) {
[648]325 HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
326
327 for (Package m : mods) {
328 for (int confNum : m.getIncompabitilities()) {
329 Package other = getPackageByNumber(confNum);
[804]330 if (other != null) {
331 if (mods.contains(other)) {
332 if (!res.containsKey(m))
333 res.put(m, new HashSet<Package>());
334 res.get(m).add(other);
335 }
[648]336 }
337 }
338 }
339
340 return res;
341 }
342
343 /**
344 * @param m
345 * Mod to check
346 * @return Is mod installed?
347 */
348 boolean isModInstalled(Package m) {
349 return currentlyInstalled.contains(m.getPackageNumber());
350 }
351}
Note: See TracBrowser for help on using the repository browser.