source: AE/installer2/src/net/oni2/aeinstaller/backend/mods/ModManager.java@ 608

Last change on this file since 608 was 608, checked in by alloc, 12 years ago

AEI2:

  • Added BSL handling to installation
  • Updated config-terms for dependencies/incompatibilities
  • Fixed mod counts for types
  • Open Edition folder through menu
  • Open folder of already downloaded mod through context menu
  • (Semi?)Fixed launching Oni on MacOS
  • Settings: Added checkbox for update notification
File size: 6.7 KB
Line 
1package net.oni2.aeinstaller.backend.mods;
2
3import java.io.File;
4import java.io.FileFilter;
5import java.io.FileInputStream;
6import java.io.FileNotFoundException;
7import java.io.FileOutputStream;
8import java.io.IOException;
9import java.util.Collection;
10import java.util.HashMap;
11import java.util.HashSet;
12import java.util.TreeSet;
13import java.util.Vector;
14
15import com.thoughtworks.xstream.XStream;
16import com.thoughtworks.xstream.io.xml.StaxDriver;
17
18import net.oni2.aeinstaller.backend.Paths;
19import net.oni2.aeinstaller.backend.depot.DepotManager;
20import net.oni2.aeinstaller.backend.depot.model.NodeMod;
21import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm;
22import net.oni2.aeinstaller.backend.oni.Installer;
23
24/**
25 * @author Christian Illy
26 */
27public 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 Collection of tools valid on this platform and not mandatory
185 */
186 public Collection<Mod> getTools() {
187 Vector<Mod> res = new Vector<Mod>();
188 for (Mod m : tools.values())
189 if (m.isValidOnPlatform() && !m.isMandatoryMod())
190 res.add(m);
191 return res;
192 }
193
194 /**
195 * @return Tools which are always installed and valid on this platform
196 */
197 public TreeSet<Mod> getMandatoryTools() {
198 TreeSet<Mod> res = new TreeSet<Mod>();
199 for (Mod m : tools.values()) {
200 if (m.isValidOnPlatform() && m.isMandatoryMod())
201 res.add(m);
202 }
203 return res;
204 }
205
206 /**
207 * Get a mod by its package number
208 *
209 * @param number
210 * Package number
211 * @return Mod or null
212 */
213 public Mod getModByNumber(int number) {
214 for (Mod m : mods.values()) {
215 if (m.getPackageNumber() == number)
216 return m;
217 }
218 return null;
219 }
220
221 /**
222 * Check for unresolved dependencies within the given mods
223 *
224 * @param mods
225 * Mods to check
226 * @return Unmet dependencies
227 */
228 public HashMap<Mod, HashSet<Mod>> checkDependencies(TreeSet<Mod> mods) {
229 // TODO: Verify functionality
230 HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
231
232 for (Mod m : mods) {
233 for (int depNum : m.getDependencies()) {
234 Mod other = getModByNumber(depNum);
235 if (!mods.contains(other)) {
236 if (!res.containsKey(m))
237 res.put(m, new HashSet<Mod>());
238 res.get(m).add(other);
239 }
240 }
241 }
242
243 return res;
244 }
245
246 /**
247 * Check for incompabitilites between given mods
248 *
249 * @param mods
250 * Mods to check
251 * @return Incompatible mods
252 */
253 public HashMap<Mod, HashSet<Mod>> checkIncompabitilites(TreeSet<Mod> mods) {
254 // TODO: Verify functionality
255 HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>();
256
257 for (Mod m : mods) {
258 for (int confNum : m.getIncompabitilities()) {
259 Mod other = getModByNumber(confNum);
260 if (mods.contains(other)) {
261 if (!res.containsKey(m))
262 res.put(m, new HashSet<Mod>());
263 res.get(m).add(other);
264 }
265 }
266 }
267
268 return res;
269 }
270
271 /**
272 * @param m
273 * Mod to check
274 * @return Is mod installed?
275 */
276 public boolean isModInstalled(Mod m) {
277 return currentlyInstalled.contains(m.getPackageNumber());
278 }
279}
Note: See TracBrowser for help on using the repository browser.