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 | * Get a mod by its package number
|
---|
256 | *
|
---|
257 | * @param number
|
---|
258 | * Package number
|
---|
259 | * @return Mod or null
|
---|
260 | */
|
---|
261 | public Mod getModByNumber(int number) {
|
---|
262 | for (Mod m : mods.values()) {
|
---|
263 | if (m.getPackageNumber() == number)
|
---|
264 | return m;
|
---|
265 | }
|
---|
266 | return null;
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Check for unresolved dependencies within the given mods
|
---|
271 | *
|
---|
272 | * @param mods
|
---|
273 | * Mods to check
|
---|
274 | * @return Unmet dependencies
|
---|
275 | */
|
---|
276 | public HashMap<Mod, HashSet<Mod>> checkDependencies(TreeSet<Mod> mods) {
|
---|
277 | // TODO: Verify functionality
|
---|
278 | HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
|
---|
279 |
|
---|
280 | for (Mod m : mods) {
|
---|
281 | for (int depNum : m.getDependencies()) {
|
---|
282 | Mod other = getModByNumber(depNum);
|
---|
283 | if (!mods.contains(other)) {
|
---|
284 | if (!res.containsKey(m))
|
---|
285 | res.put(m, new HashSet<Mod>());
|
---|
286 | res.get(m).add(other);
|
---|
287 | }
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | return res;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Check for incompabitilites between given mods
|
---|
296 | *
|
---|
297 | * @param mods
|
---|
298 | * Mods to check
|
---|
299 | * @return Incompatible mods
|
---|
300 | */
|
---|
301 | public HashMap<Mod, HashSet<Mod>> checkIncompabitilites(TreeSet<Mod> mods) {
|
---|
302 | // TODO: Verify functionality
|
---|
303 | HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
|
---|
304 |
|
---|
305 | for (Mod m : mods) {
|
---|
306 | for (int confNum : m.getIncompabitilities()) {
|
---|
307 | Mod other = getModByNumber(confNum);
|
---|
308 | if (mods.contains(other)) {
|
---|
309 | if (!res.containsKey(m))
|
---|
310 | res.put(m, new HashSet<Mod>());
|
---|
311 | res.get(m).add(other);
|
---|
312 | }
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | return res;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * @param m
|
---|
321 | * Mod to check
|
---|
322 | * @return Is mod installed?
|
---|
323 | */
|
---|
324 | public boolean isModInstalled(Mod m) {
|
---|
325 | return currentlyInstalled.contains(m.getPackageNumber());
|
---|
326 | }
|
---|
327 | }
|
---|