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.TreeSet;
|
---|
13 | import java.util.Vector;
|
---|
14 |
|
---|
15 | import net.oni2.aeinstaller.backend.Paths;
|
---|
16 | import net.oni2.aeinstaller.backend.oni.management.Installer;
|
---|
17 | import net.oni2.aeinstaller.backend.oni.management.tools.ToolInstallationList;
|
---|
18 | import net.oni2.moddepot.DepotManager;
|
---|
19 | import net.oni2.moddepot.model.NodeMod;
|
---|
20 | import net.oni2.moddepot.model.TaxonomyTerm;
|
---|
21 |
|
---|
22 | import com.thoughtworks.xstream.XStream;
|
---|
23 | import com.thoughtworks.xstream.io.xml.StaxDriver;
|
---|
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-");
|
---|
93 | types.put("-Local-", localType);
|
---|
94 |
|
---|
95 | for (TaxonomyTerm tt : DepotManager.getInstance().getTypes()) {
|
---|
96 | types.put(tt.getName(), new Type(tt.getName()));
|
---|
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) {
|
---|
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 Collection<Package> getTools() {
|
---|
220 | Vector<Package> res = new Vector<Package>();
|
---|
221 | for (Package m : tools.values())
|
---|
222 | if (m.isValidOnPlatform() && !m.isCorePackage())
|
---|
223 | res.add(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 : ToolInstallationList.getInstance().getItems().keySet()) {
|
---|
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(
|
---|
297 | TreeSet<Package> mods) {
|
---|
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);
|
---|
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 | }
|
---|
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 | */
|
---|
323 | public HashMap<Package, HashSet<Package>> checkIncompabitilites(
|
---|
324 | TreeSet<Package> mods) {
|
---|
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);
|
---|
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 | }
|
---|
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 | }
|
---|