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

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

AEI2.03:

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;
[809]16import net.oni2.aeinstaller.backend.oni.management.ModInstallationList;
[804]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 /**
36 * @param f
37 * Mod selection file
38 * @return Mod selection
39 */
40 @SuppressWarnings("unchecked")
41 public Vector<Integer> loadModSelection(File f) {
42 Vector<Integer> res = new Vector<Integer>();
43 try {
44 if (f.exists()) {
45 FileInputStream fis = new FileInputStream(f);
46 XStream xs = new XStream(new StaxDriver());
47 Object obj = xs.fromXML(fis);
48 if (obj instanceof Vector<?>)
49 res = (Vector<Integer>) obj;
50 fis.close();
51 }
52 } catch (FileNotFoundException e) {
53 e.printStackTrace();
54 } catch (IOException e) {
55 e.printStackTrace();
56 }
57 return res;
58 }
59
60 /**
61 * @param f
62 * Mod selection file
63 * @param mods
64 * Selected mods
65 */
66 public void saveModSelection(File f, TreeSet<Package> mods) {
67 try {
68 Vector<Integer> installed = new Vector<Integer>();
69 for (Package m : mods) {
70 installed.add(m.getPackageNumber());
71 }
72 FileOutputStream fos = new FileOutputStream(f);
73 XStream xs = new XStream(new StaxDriver());
74 xs.toXML(installed, fos);
75 fos.close();
76 } catch (FileNotFoundException e) {
77 e.printStackTrace();
78 } catch (IOException e) {
79 e.printStackTrace();
80 }
81 }
82
83 /**
84 * First initialization of ModManager
85 */
86 public void init() {
87 types = new HashMap<String, Type>();
88 mods = new HashMap<Integer, Package>();
89
[749]90 Type localType = new Type("-Local-");
[648]91 types.put("-Local-", localType);
92
93 for (TaxonomyTerm tt : DepotManager.getInstance().getTypes()) {
[749]94 types.put(tt.getName(), new Type(tt.getName()));
[648]95 }
96
97 HashMap<Integer, Package> modFolders = new HashMap<Integer, Package>();
98 if (Paths.getModsPath().exists()) {
99 for (File f : Paths.getModsPath().listFiles(new FileFilter() {
100 @Override
101 public boolean accept(File pathname) {
102 return pathname.isDirectory();
103 }
104 })) {
105 Package m = new Package(f);
106 modFolders.put(m.getPackageNumber(), m);
107 }
108 }
109
110 for (NodeMod nm : DepotManager.getInstance().getModPackageNodes()) {
[749]111 if (nm.getUploads().size() == 1) {
[648]112 Package m = new Package(nm);
113 if (nm.isTool())
114 tools.put(m.getPackageNumber(), m);
115 else
116 mods.put(m.getPackageNumber(), m);
117 modFolders.remove(m.getPackageNumber());
118 }
119 }
120
121 for (Package m : modFolders.values()) {
122 if (!m.isCorePackage()) {
123 localType.addEntry(m);
124 m.getTypes().add(localType);
125 }
126 if (m.isTool())
127 tools.put(m.getPackageNumber(), m);
128 else
129 mods.put(m.getPackageNumber(), m);
130 }
131 }
132
133 /**
134 * @return Singleton instance
135 */
136 public static PackageManager getInstance() {
137 return instance;
138 }
139
140 Type getTypeByName(String name) {
141 return types.get(name);
142 }
143
144 /**
145 * @return Collection of types which do have mods associated
146 */
147 public Collection<Type> getTypesWithContent() {
148 Vector<Type> res = new Vector<Type>();
149 for (Type t : types.values()) {
150 if (t.getEntries().size() > 0)
151 res.add(t);
152 }
153 return res;
154 }
155
156 /**
157 * @return Collection of mods valid on this platform and not core package
158 */
159 public Collection<Package> getModsValidAndNotCore() {
160 Vector<Package> res = new Vector<Package>();
161 for (Package m : mods.values())
162 if (m.isValidOnPlatform() && !m.isCorePackage())
163 res.add(m);
164 return res;
165 }
166
167 /**
168 * @return Mods which are always installed and valid on this platform
169 */
170 public TreeSet<Package> getCoreMods() {
171 TreeSet<Package> res = new TreeSet<Package>();
172 for (Package m : mods.values()) {
173 if (m.isValidOnPlatform() && m.isCorePackage())
174 res.add(m);
175 }
176 return res;
177 }
178
179 /**
180 * @return Mods which are already locally available
181 */
182 public TreeSet<Package> getLocalAvailableMods() {
183 TreeSet<Package> res = new TreeSet<Package>();
184 for (Package m : mods.values()) {
185 if (m.isLocalAvailable())
186 res.add(m);
187 }
188 return res;
189 }
190
191 /**
192 * @return Mods which can be updated
193 */
194 public TreeSet<Package> getUpdatableMods() {
195 TreeSet<Package> res = new TreeSet<Package>();
196 for (Package m : getLocalAvailableMods()) {
197 if (m.isNewerAvailable())
198 res.add(m);
199 }
200 return res;
201 }
202
203 /**
[807]204 * @return Currently installed mods
205 */
206 public TreeSet<Package> getInstalledMods() {
207 TreeSet<Package> res = new TreeSet<Package>();
[809]208 for (int n : ModInstallationList.getInstance().getInstalledMods()) {
[807]209 res.add(getPackageByNumber(n));
210 }
211 return res;
212 }
213
214 /**
[648]215 * @return Collection of tools valid on this platform and not core
216 */
[658]217 public Collection<Package> getTools() {
218 Vector<Package> res = new Vector<Package>();
[648]219 for (Package m : tools.values())
220 if (m.isValidOnPlatform() && !m.isCorePackage())
[658]221 res.add(m);
[648]222 return res;
223 }
224
225 /**
226 * @return Tools which are always installed and valid on this platform
227 */
228 public TreeSet<Package> getCoreTools() {
229 TreeSet<Package> res = new TreeSet<Package>();
230 for (Package m : tools.values()) {
231 if (m.isValidOnPlatform() && m.isCorePackage())
232 res.add(m);
233 }
234 return res;
235 }
236
237 /**
238 * @return Tools which are already locally available
239 */
240 public TreeSet<Package> getLocalAvailableTools() {
241 TreeSet<Package> res = new TreeSet<Package>();
242 for (Package m : tools.values()) {
243 if (m.isLocalAvailable())
244 res.add(m);
245 }
246 return res;
247 }
248
249 /**
250 * @return Tools which can be updated
251 */
252 public TreeSet<Package> getUpdatableTools() {
253 TreeSet<Package> res = new TreeSet<Package>();
254 for (Package m : getLocalAvailableTools()) {
255 if (m.isNewerAvailable())
256 res.add(m);
257 }
258 return res;
259 }
260
261 /**
262 * @return Currently installed tools
263 */
264 public TreeSet<Package> getInstalledTools() {
265 TreeSet<Package> res = new TreeSet<Package>();
[804]266 for (int n : ToolInstallationList.getInstance().getItems().keySet()) {
[648]267 res.add(getPackageByNumber(n));
268 }
269 return res;
270 }
271
272 /**
273 * Get a mod/tool by its package number
274 *
275 * @param number
276 * Package number
277 * @return Mod/tool or null
278 */
279 private Package getPackageByNumber(int number) {
280 if (mods.containsKey(number))
281 return mods.get(number);
282 if (tools.containsKey(number))
283 return tools.get(number);
284 return null;
285 }
286
287 /**
288 * Check for unresolved dependencies within the given mods
289 *
290 * @param mods
291 * Mods to check
292 * @return Unmet dependencies
293 */
[749]294 public HashMap<Package, HashSet<Package>> checkDependencies(
295 TreeSet<Package> mods) {
[648]296 HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
297
298 for (Package m : mods) {
299 for (int depNum : m.getDependencies()) {
300 Package other = getPackageByNumber(depNum);
[804]301 if (other != null) {
302 if (!mods.contains(other)) {
303 if (!res.containsKey(m))
304 res.put(m, new HashSet<Package>());
305 res.get(m).add(other);
306 }
[648]307 }
308 }
309 }
310
311 return res;
312 }
313
314 /**
315 * Check for incompabitilites between given mods
316 *
317 * @param mods
318 * Mods to check
319 * @return Incompatible mods
320 */
[749]321 public HashMap<Package, HashSet<Package>> checkIncompabitilites(
322 TreeSet<Package> mods) {
[648]323 HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
324
325 for (Package m : mods) {
326 for (int confNum : m.getIncompabitilities()) {
327 Package other = getPackageByNumber(confNum);
[804]328 if (other != null) {
329 if (mods.contains(other)) {
330 if (!res.containsKey(m))
331 res.put(m, new HashSet<Package>());
332 res.get(m).add(other);
333 }
[648]334 }
335 }
336 }
337
338 return res;
339 }
340
341 /**
342 * @param m
343 * Mod to check
344 * @return Is mod installed?
345 */
346 boolean isModInstalled(Package m) {
[809]347 return ModInstallationList.getInstance().isInstalled(m.getPackageNumber());
[648]348 }
349}
Note: See TracBrowser for help on using the repository browser.