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