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()
|
---|
96 | .getTaxonomyTermsByVocabulary(
|
---|
97 | DepotManager.getInstance().getVocabIdType())) {
|
---|
98 | types.put(tt.getName(), new Type(tt.getName(), tt));
|
---|
99 | }
|
---|
100 |
|
---|
101 | HashMap<Integer, Mod> modFolders = new HashMap<Integer, Mod>();
|
---|
102 | if (Paths.getModsPath().exists()) {
|
---|
103 | for (File f : Paths.getModsPath().listFiles(new FileFilter() {
|
---|
104 | @Override
|
---|
105 | public boolean accept(File pathname) {
|
---|
106 | return pathname.isDirectory();
|
---|
107 | }
|
---|
108 | })) {
|
---|
109 | Mod m = new Mod(f);
|
---|
110 | modFolders.put(m.getPackageNumber(), m);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | for (NodeMod nm : DepotManager.getInstance().getModPackageNodes()) {
|
---|
115 | if (nm.getUploads().size() == 1) {
|
---|
116 | Mod m = new Mod(nm);
|
---|
117 | if (nm.isTool())
|
---|
118 | tools.put(m.getPackageNumber(), m);
|
---|
119 | else
|
---|
120 | mods.put(m.getPackageNumber(), m);
|
---|
121 | modFolders.remove(m.getPackageNumber());
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | for (Mod m : modFolders.values()) {
|
---|
126 | if (!m.isMandatoryMod()) {
|
---|
127 | localType.addEntry(m);
|
---|
128 | m.getTypes().add(localType);
|
---|
129 | }
|
---|
130 | mods.put(m.getPackageNumber(), m);
|
---|
131 | }
|
---|
132 |
|
---|
133 | currentlyInstalled = Installer.getInstalledMods();
|
---|
134 | if (currentlyInstalled == null)
|
---|
135 | currentlyInstalled = new Vector<Integer>();
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * @return Singleton instance
|
---|
140 | */
|
---|
141 | public static ModManager getInstance() {
|
---|
142 | return instance;
|
---|
143 | }
|
---|
144 |
|
---|
145 | Type getTypeByName(String name) {
|
---|
146 | return types.get(name);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * @return Collection of types which do have mods associated
|
---|
151 | */
|
---|
152 | public Collection<Type> getTypesWithContent() {
|
---|
153 | Vector<Type> res = new Vector<Type>();
|
---|
154 | for (Type t : types.values()) {
|
---|
155 | if (t.getEntries().size() > 0)
|
---|
156 | res.add(t);
|
---|
157 | }
|
---|
158 | return res;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * @return Collection of mods valid on this platform and not mandatory
|
---|
163 | */
|
---|
164 | public Collection<Mod> getModsValidAndNotMandatory() {
|
---|
165 | Vector<Mod> res = new Vector<Mod>();
|
---|
166 | for (Mod m : mods.values())
|
---|
167 | if (m.isValidOnPlatform() && !m.isMandatoryMod())
|
---|
168 | res.add(m);
|
---|
169 | return res;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * @return Mods which are always installed and valid on this platform
|
---|
174 | */
|
---|
175 | public TreeSet<Mod> getMandatoryMods() {
|
---|
176 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
177 | for (Mod m : mods.values()) {
|
---|
178 | if (m.isValidOnPlatform() && m.isMandatoryMod())
|
---|
179 | res.add(m);
|
---|
180 | }
|
---|
181 | return res;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * @return Mods which are already locally available
|
---|
186 | */
|
---|
187 | public TreeSet<Mod> getLocalAvailableMods() {
|
---|
188 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
189 | for (Mod m : mods.values()) {
|
---|
190 | if (m.isLocalAvailable())
|
---|
191 | res.add(m);
|
---|
192 | }
|
---|
193 | return res;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * @return Mods which can be updated
|
---|
198 | */
|
---|
199 | public TreeSet<Mod> getUpdatableMods() {
|
---|
200 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
201 | for (Mod m : getLocalAvailableMods()) {
|
---|
202 | if (m.isNewerAvailable())
|
---|
203 | res.add(m);
|
---|
204 | }
|
---|
205 | return res;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * @return Collection of tools valid on this platform and not mandatory
|
---|
210 | */
|
---|
211 | public TreeMap<String, Mod> getTools() {
|
---|
212 | TreeMap<String, Mod> res = new TreeMap<String, Mod>();
|
---|
213 | for (Mod m : tools.values())
|
---|
214 | if (m.isValidOnPlatform() && !m.isMandatoryMod())
|
---|
215 | res.put(m.getName(), m);
|
---|
216 | return res;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * @return Tools which are always installed and valid on this platform
|
---|
221 | */
|
---|
222 | public TreeSet<Mod> getMandatoryTools() {
|
---|
223 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
224 | for (Mod m : tools.values()) {
|
---|
225 | if (m.isValidOnPlatform() && m.isMandatoryMod())
|
---|
226 | res.add(m);
|
---|
227 | }
|
---|
228 | return res;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * @return Tools which are already locally available
|
---|
233 | */
|
---|
234 | public TreeSet<Mod> getLocalAvailableTools() {
|
---|
235 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
236 | for (Mod m : tools.values()) {
|
---|
237 | if (m.isLocalAvailable())
|
---|
238 | res.add(m);
|
---|
239 | }
|
---|
240 | return res;
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * @return Tools which can be updated
|
---|
245 | */
|
---|
246 | public TreeSet<Mod> getUpdatableTools() {
|
---|
247 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
248 | for (Mod m : getLocalAvailableTools()) {
|
---|
249 | if (m.isNewerAvailable())
|
---|
250 | res.add(m);
|
---|
251 | }
|
---|
252 | return res;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * @return Currently installed tools
|
---|
257 | */
|
---|
258 | public TreeSet<Mod> getInstalledTools() {
|
---|
259 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
260 | for (int n : Installer.getInstalledTools()) {
|
---|
261 | res.add(getModByNumber(n));
|
---|
262 | }
|
---|
263 | return res;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Get a mod/tool by its package number
|
---|
268 | *
|
---|
269 | * @param number
|
---|
270 | * Package number
|
---|
271 | * @return Mod/tool or null
|
---|
272 | */
|
---|
273 | public Mod getModByNumber(int number) {
|
---|
274 | if (mods.containsKey(number))
|
---|
275 | return mods.get(number);
|
---|
276 | if (tools.containsKey(number))
|
---|
277 | return tools.get(number);
|
---|
278 | return null;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Check for unresolved dependencies within the given mods
|
---|
283 | *
|
---|
284 | * @param mods
|
---|
285 | * Mods to check
|
---|
286 | * @return Unmet dependencies
|
---|
287 | */
|
---|
288 | public HashMap<Mod, HashSet<Mod>> checkDependencies(TreeSet<Mod> mods) {
|
---|
289 | // TODO: Verify functionality (checkDependencies)
|
---|
290 | HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
|
---|
291 |
|
---|
292 | for (Mod m : mods) {
|
---|
293 | for (int depNum : m.getDependencies()) {
|
---|
294 | Mod other = getModByNumber(depNum);
|
---|
295 | if (!mods.contains(other)) {
|
---|
296 | if (!res.containsKey(m))
|
---|
297 | res.put(m, new HashSet<Mod>());
|
---|
298 | res.get(m).add(other);
|
---|
299 | }
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | return res;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Check for incompabitilites between given mods
|
---|
308 | *
|
---|
309 | * @param mods
|
---|
310 | * Mods to check
|
---|
311 | * @return Incompatible mods
|
---|
312 | */
|
---|
313 | public HashMap<Mod, HashSet<Mod>> checkIncompabitilites(TreeSet<Mod> mods) {
|
---|
314 | // TODO: Verify functionality (checkIncompatibilities)
|
---|
315 | HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
|
---|
316 |
|
---|
317 | for (Mod m : mods) {
|
---|
318 | for (int confNum : m.getIncompabitilities()) {
|
---|
319 | Mod other = getModByNumber(confNum);
|
---|
320 | if (mods.contains(other)) {
|
---|
321 | if (!res.containsKey(m))
|
---|
322 | res.put(m, new HashSet<Mod>());
|
---|
323 | res.get(m).add(other);
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | return res;
|
---|
329 | }
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * @param m
|
---|
333 | * Mod to check
|
---|
334 | * @return Is mod installed?
|
---|
335 | */
|
---|
336 | public boolean isModInstalled(Mod m) {
|
---|
337 | return currentlyInstalled.contains(m.getPackageNumber());
|
---|
338 | }
|
---|
339 | }
|
---|