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 | FileInputStream fis = new FileInputStream(f);
|
---|
46 | XStream xs = new XStream(new StaxDriver());
|
---|
47 | Object obj = xs.fromXML(fis);
|
---|
48 | if (obj instanceof Vector<?>)
|
---|
49 | res = (Vector<Integer>) obj;
|
---|
50 | fis.close();
|
---|
51 | } catch (FileNotFoundException e) {
|
---|
52 | e.printStackTrace();
|
---|
53 | } catch (IOException e) {
|
---|
54 | e.printStackTrace();
|
---|
55 | }
|
---|
56 | return res;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * @param f
|
---|
61 | * Mod selection file
|
---|
62 | * @param mods
|
---|
63 | * Selected mods
|
---|
64 | */
|
---|
65 | public void saveModSelection(File f, TreeSet<Mod> mods) {
|
---|
66 | try {
|
---|
67 | Vector<Integer> installed = new Vector<Integer>();
|
---|
68 | for (Mod m : mods) {
|
---|
69 | installed.add(m.getPackageNumber());
|
---|
70 | }
|
---|
71 | FileOutputStream fos = new FileOutputStream(f);
|
---|
72 | XStream xs = new XStream(new StaxDriver());
|
---|
73 | xs.toXML(installed, fos);
|
---|
74 | fos.close();
|
---|
75 | } catch (FileNotFoundException e) {
|
---|
76 | e.printStackTrace();
|
---|
77 | } catch (IOException e) {
|
---|
78 | e.printStackTrace();
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * First initialization of ModManager
|
---|
84 | */
|
---|
85 | public void init() {
|
---|
86 | types = new HashMap<String, Type>();
|
---|
87 | mods = new HashMap<Integer, Mod>();
|
---|
88 |
|
---|
89 | types.put("-Local-", new Type("-Local-", null));
|
---|
90 |
|
---|
91 | for (TaxonomyTerm tt : DepotManager.getInstance()
|
---|
92 | .getTaxonomyTermsByVocabulary(
|
---|
93 | DepotManager.getInstance().getVocabIdType())) {
|
---|
94 | types.put(tt.getName(), new Type(tt.getName(), tt));
|
---|
95 | }
|
---|
96 |
|
---|
97 | HashMap<Integer, Mod> modFolders = new HashMap<Integer, Mod>();
|
---|
98 | if (Paths.getModsPath().exists()) {
|
---|
99 | for (File f : Paths.getModsPath().listFiles(new FileFilter() {
|
---|
100 | @Override
|
---|
101 | public boolean accept(File pathname) {
|
---|
102 | return pathname.isDirectory();
|
---|
103 | }
|
---|
104 | })) {
|
---|
105 | Mod m = new Mod(f);
|
---|
106 | modFolders.put(m.getPackageNumber(), m);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | for (NodeMod nm : DepotManager.getInstance().getModPackageNodes()) {
|
---|
111 | if (nm.getUploads().size() == 1) {
|
---|
112 | Mod m = new Mod(nm);
|
---|
113 | if (nm.isTool())
|
---|
114 | tools.put(m.getPackageNumber(), m);
|
---|
115 | else
|
---|
116 | mods.put(m.getPackageNumber(), m);
|
---|
117 | modFolders.remove(m.getPackageNumber());
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | for (Mod m : modFolders.values()) {
|
---|
122 | mods.put(m.getPackageNumber(), m);
|
---|
123 | }
|
---|
124 |
|
---|
125 | currentlyInstalled = Installer.getInstalledMods();
|
---|
126 | if (currentlyInstalled == null)
|
---|
127 | currentlyInstalled = new Vector<Integer>();
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * @return Singleton instance
|
---|
132 | */
|
---|
133 | public static ModManager getInstance() {
|
---|
134 | return instance;
|
---|
135 | }
|
---|
136 |
|
---|
137 | Type getTypeByName(String name) {
|
---|
138 | return types.get(name);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * @return Collection of types which do have mods associated
|
---|
143 | */
|
---|
144 | public Collection<Type> getTypesWithContent() {
|
---|
145 | Vector<Type> res = new Vector<Type>();
|
---|
146 | for (Type t : types.values()) {
|
---|
147 | if (t.getEntries().size() > 0)
|
---|
148 | res.add(t);
|
---|
149 | }
|
---|
150 | return res;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * @return Collection of mods
|
---|
155 | */
|
---|
156 | public Collection<Mod> getMods() {
|
---|
157 | return mods.values();
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * @return Mods which are always installed
|
---|
162 | */
|
---|
163 | public TreeSet<Mod> getMandatoryMods() {
|
---|
164 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
165 | for (Mod m : mods.values()) {
|
---|
166 | if (m.isMandatoryMod())
|
---|
167 | res.add(m);
|
---|
168 | }
|
---|
169 | return res;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * @return Collection of tools
|
---|
174 | */
|
---|
175 | public Collection<Mod> getTools() {
|
---|
176 | return tools.values();
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * @return Tools which are always installed
|
---|
181 | */
|
---|
182 | public TreeSet<Mod> getMandatoryTools() {
|
---|
183 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
184 | for (Mod m : tools.values()) {
|
---|
185 | if (m.isMandatoryMod())
|
---|
186 | res.add(m);
|
---|
187 | }
|
---|
188 | return res;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Get a mod by its package number
|
---|
193 | *
|
---|
194 | * @param number
|
---|
195 | * Package number
|
---|
196 | * @return Mod or null
|
---|
197 | */
|
---|
198 | public Mod getModByNumber(int number) {
|
---|
199 | for (Mod m : mods.values()) {
|
---|
200 | if (m.getPackageNumber() == number)
|
---|
201 | return m;
|
---|
202 | }
|
---|
203 | return null;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Check for unresolved dependencies within the given mods
|
---|
208 | *
|
---|
209 | * @param mods
|
---|
210 | * Mods to check
|
---|
211 | * @return Unmet dependencies
|
---|
212 | */
|
---|
213 | public HashMap<Mod, HashSet<Mod>> checkDependencies(TreeSet<Mod> mods) {
|
---|
214 | // TODO: Verify functionality
|
---|
215 | HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
|
---|
216 |
|
---|
217 | for (Mod m : mods) {
|
---|
218 | for (int depNum : m.getDependencies()) {
|
---|
219 | Mod other = getModByNumber(depNum);
|
---|
220 | if (!mods.contains(other)) {
|
---|
221 | if (!res.containsKey(m))
|
---|
222 | res.put(m, new HashSet<Mod>());
|
---|
223 | res.get(m).add(other);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | return res;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Check for conflicts between given mods
|
---|
233 | *
|
---|
234 | * @param mods
|
---|
235 | * Mods to check
|
---|
236 | * @return Conflicting mods
|
---|
237 | */
|
---|
238 | public HashMap<Mod, HashSet<Mod>> checkConflicts(TreeSet<Mod> mods) {
|
---|
239 | // TODO: Verify functionality
|
---|
240 | HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
|
---|
241 |
|
---|
242 | for (Mod m : mods) {
|
---|
243 | for (int confNum : m.getConflicts()) {
|
---|
244 | Mod other = getModByNumber(confNum);
|
---|
245 | if (mods.contains(other)) {
|
---|
246 | if (!res.containsKey(m))
|
---|
247 | res.put(m, new HashSet<Mod>());
|
---|
248 | res.get(m).add(other);
|
---|
249 | }
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | return res;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * @param m
|
---|
258 | * Mod to check
|
---|
259 | * @return Is mod installed?
|
---|
260 | */
|
---|
261 | public boolean isModInstalled(Mod m) {
|
---|
262 | return currentlyInstalled.contains(m.getPackageNumber());
|
---|
263 | }
|
---|
264 | }
|
---|