1 | package net.oni2.aeinstaller.backend.mods;
|
---|
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 ModManager {
|
---|
29 | private static ModManager instance = new ModManager();
|
---|
30 |
|
---|
31 | private HashMap<String, Type> types = new HashMap<String, Type>();
|
---|
32 | private HashMap<Integer, Mod> mods = new HashMap<Integer, Mod>();
|
---|
33 | private HashMap<Integer, Mod> tools = new HashMap<Integer, Mod>();
|
---|
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<Mod> mods) {
|
---|
69 | try {
|
---|
70 | Vector<Integer> installed = new Vector<Integer>();
|
---|
71 | for (Mod 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, Mod>();
|
---|
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, Mod> modFolders = new HashMap<Integer, Mod>();
|
---|
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 | Mod m = new Mod(f);
|
---|
108 | modFolders.put(m.getPackageNumber(), m);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | for (NodeMod nm : DepotManager.getInstance().getModPackageNodes()) {
|
---|
113 | if (nm.getUploads().size() == 1) {
|
---|
114 | Mod m = new Mod(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 (Mod m : modFolders.values()) {
|
---|
124 | if (!m.isMandatoryMod()) {
|
---|
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 ModManager 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 mandatory
|
---|
171 | */
|
---|
172 | public Collection<Mod> getModsValidAndNotMandatory() {
|
---|
173 | Vector<Mod> res = new Vector<Mod>();
|
---|
174 | for (Mod m : mods.values())
|
---|
175 | if (m.isValidOnPlatform() && !m.isMandatoryMod())
|
---|
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<Mod> getMandatoryMods() {
|
---|
184 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
185 | for (Mod m : mods.values()) {
|
---|
186 | if (m.isValidOnPlatform() && m.isMandatoryMod())
|
---|
187 | res.add(m);
|
---|
188 | }
|
---|
189 | return res;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * @return Mods which are already locally available
|
---|
194 | */
|
---|
195 | public TreeSet<Mod> getLocalAvailableMods() {
|
---|
196 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
197 | for (Mod 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<Mod> getUpdatableMods() {
|
---|
208 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
209 | for (Mod 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 mandatory
|
---|
218 | */
|
---|
219 | public TreeMap<String, Mod> getTools() {
|
---|
220 | TreeMap<String, Mod> res = new TreeMap<String, Mod>();
|
---|
221 | for (Mod m : tools.values())
|
---|
222 | if (m.isValidOnPlatform() && !m.isMandatoryMod())
|
---|
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<Mod> getMandatoryTools() {
|
---|
231 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
232 | for (Mod m : tools.values()) {
|
---|
233 | if (m.isValidOnPlatform() && m.isMandatoryMod())
|
---|
234 | res.add(m);
|
---|
235 | }
|
---|
236 | return res;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * @return Tools which are already locally available
|
---|
241 | */
|
---|
242 | public TreeSet<Mod> getLocalAvailableTools() {
|
---|
243 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
244 | for (Mod 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<Mod> getUpdatableTools() {
|
---|
255 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
256 | for (Mod 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<Mod> getInstalledTools() {
|
---|
267 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
268 | for (int n : Installer.getInstalledTools()) {
|
---|
269 | res.add(getModByNumber(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 Mod getModByNumber(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<Mod, HashSet<Mod>> checkDependencies(TreeSet<Mod> mods) {
|
---|
297 | // TODO: Verify functionality (checkDependencies)
|
---|
298 | HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
|
---|
299 |
|
---|
300 | for (Mod m : mods) {
|
---|
301 | for (int depNum : m.getDependencies()) {
|
---|
302 | Mod other = getModByNumber(depNum);
|
---|
303 | if (!mods.contains(other)) {
|
---|
304 | if (!res.containsKey(m))
|
---|
305 | res.put(m, new HashSet<Mod>());
|
---|
306 | res.get(m).add(other);
|
---|
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 | */
|
---|
321 | public HashMap<Mod, HashSet<Mod>> checkIncompabitilites(TreeSet<Mod> mods) {
|
---|
322 | // TODO: Verify functionality (checkIncompatibilities)
|
---|
323 | HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
|
---|
324 |
|
---|
325 | for (Mod m : mods) {
|
---|
326 | for (int confNum : m.getIncompabitilities()) {
|
---|
327 | Mod other = getModByNumber(confNum);
|
---|
328 | if (mods.contains(other)) {
|
---|
329 | if (!res.containsKey(m))
|
---|
330 | res.put(m, new HashSet<Mod>());
|
---|
331 | res.get(m).add(other);
|
---|
332 | }
|
---|
333 | }
|
---|
334 | }
|
---|
335 |
|
---|
336 | return res;
|
---|
337 | }
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * @param m
|
---|
341 | * Mod to check
|
---|
342 | * @return Is mod installed?
|
---|
343 | */
|
---|
344 | boolean isModInstalled(Mod m) {
|
---|
345 | return currentlyInstalled.contains(m.getPackageNumber());
|
---|
346 | }
|
---|
347 | }
|
---|