1 | package net.oni2.aeinstaller.backend.packages;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileFilter;
|
---|
5 | import java.io.FileInputStream;
|
---|
6 | import java.io.FileNotFoundException;
|
---|
7 | import java.io.FileOutputStream;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.TreeMap;
|
---|
13 | import java.util.TreeSet;
|
---|
14 | import java.util.Vector;
|
---|
15 |
|
---|
16 | import com.thoughtworks.xstream.XStream;
|
---|
17 | import com.thoughtworks.xstream.io.xml.StaxDriver;
|
---|
18 |
|
---|
19 | import net.oni2.aeinstaller.backend.Paths;
|
---|
20 | import net.oni2.aeinstaller.backend.depot.DepotManager;
|
---|
21 | import net.oni2.aeinstaller.backend.depot.model.NodeMod;
|
---|
22 | import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm;
|
---|
23 | import net.oni2.aeinstaller.backend.oni.Installer;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @author Christian Illy
|
---|
27 | */
|
---|
28 | public 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 |
|
---|
92 | Type localType = new Type("-Local-", null);
|
---|
93 | types.put("-Local-", localType);
|
---|
94 |
|
---|
95 | for (TaxonomyTerm tt : DepotManager.getInstance().getTypes()) {
|
---|
96 | types.put(tt.getName(), new Type(tt.getName(), tt));
|
---|
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()) {
|
---|
113 | if (nm.getUploads().size() == 1 && nm.getStatus() == 1) {
|
---|
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 | */
|
---|
219 | public TreeMap<String, Package> getTools() {
|
---|
220 | TreeMap<String, Package> res = new TreeMap<String, Package>();
|
---|
221 | for (Package m : tools.values())
|
---|
222 | if (m.isValidOnPlatform() && !m.isCorePackage())
|
---|
223 | res.put(m.getName(), m);
|
---|
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>();
|
---|
268 | for (int n : Installer.getInstalledTools()) {
|
---|
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 | */
|
---|
296 | public HashMap<Package, HashSet<Package>> checkDependencies(TreeSet<Package> mods) {
|
---|
297 | HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
|
---|
298 |
|
---|
299 | for (Package m : mods) {
|
---|
300 | for (int depNum : m.getDependencies()) {
|
---|
301 | Package other = getPackageByNumber(depNum);
|
---|
302 | if (!mods.contains(other)) {
|
---|
303 | if (!res.containsKey(m))
|
---|
304 | res.put(m, new HashSet<Package>());
|
---|
305 | res.get(m).add(other);
|
---|
306 | }
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | return res;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Check for incompabitilites between given mods
|
---|
315 | *
|
---|
316 | * @param mods
|
---|
317 | * Mods to check
|
---|
318 | * @return Incompatible mods
|
---|
319 | */
|
---|
320 | public HashMap<Package, HashSet<Package>> checkIncompabitilites(TreeSet<Package> mods) {
|
---|
321 | HashMap<Package, HashSet<Package>> res = new HashMap<Package, HashSet<Package>>();
|
---|
322 |
|
---|
323 | for (Package m : mods) {
|
---|
324 | for (int confNum : m.getIncompabitilities()) {
|
---|
325 | Package other = getPackageByNumber(confNum);
|
---|
326 | if (mods.contains(other)) {
|
---|
327 | if (!res.containsKey(m))
|
---|
328 | res.put(m, new HashSet<Package>());
|
---|
329 | res.get(m).add(other);
|
---|
330 | }
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | return res;
|
---|
335 | }
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * @param m
|
---|
339 | * Mod to check
|
---|
340 | * @return Is mod installed?
|
---|
341 | */
|
---|
342 | boolean isModInstalled(Package m) {
|
---|
343 | return currentlyInstalled.contains(m.getPackageNumber());
|
---|
344 | }
|
---|
345 | }
|
---|