1 |
package net.oni2.aeinstaller.backend.mods; |
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileFilter; |
5 |
import java.util.Collection; |
6 |
import java.util.HashMap; |
7 |
import java.util.HashSet; |
8 |
import java.util.TreeSet; |
9 |
import java.util.Vector; |
10 |
|
11 |
import net.oni2.aeinstaller.backend.Paths; |
12 |
import net.oni2.aeinstaller.backend.depot.DepotConfig; |
13 |
import net.oni2.aeinstaller.backend.depot.DepotManager; |
14 |
import net.oni2.aeinstaller.backend.depot.model.NodeMod; |
15 |
import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm; |
16 |
|
17 |
/** |
18 |
* @author Christian Illy |
19 |
*/ |
20 |
public class ModManager { |
21 |
// Download mods |
22 |
// Update mods |
23 |
|
24 |
private static ModManager instance = new ModManager(); |
25 |
|
26 |
private HashMap<String, Type> types = new HashMap<String, Type>(); |
27 |
private HashMap<Integer, Mod> mods = new HashMap<Integer, Mod>(); |
28 |
private HashMap<Integer, Mod> tools = new HashMap<Integer, Mod>(); |
29 |
|
30 |
/** |
31 |
* First initialization of ModManager |
32 |
*/ |
33 |
public void init() { |
34 |
types = new HashMap<String, Type>(); |
35 |
mods = new HashMap<Integer, Mod>(); |
36 |
|
37 |
types.put("-Local-", new Type("-Local-", null)); |
38 |
|
39 |
for (TaxonomyTerm tt : DepotManager.getInstance() |
40 |
.getTaxonomyTermsByVocabulary( |
41 |
DepotManager.getInstance().getVocabIdType())) { |
42 |
types.put(tt.getName(), new Type(tt.getName(), tt)); |
43 |
} |
44 |
|
45 |
HashMap<Integer, Mod> modFolders = new HashMap<Integer, Mod>(); |
46 |
if (Paths.getModsPath().exists()) { |
47 |
for (File f : Paths.getModsPath().listFiles(new FileFilter() { |
48 |
@Override |
49 |
public boolean accept(File pathname) { |
50 |
return pathname.isDirectory(); |
51 |
} |
52 |
})) { |
53 |
Mod m = new Mod(f); |
54 |
modFolders.put(m.getPackageNumber(), m); |
55 |
} |
56 |
} |
57 |
|
58 |
TaxonomyTerm toolTerm = DepotManager.getInstance().getTaxonomyTerm( |
59 |
DepotConfig.getTaxonomyName_ModType_Tool()); |
60 |
for (NodeMod nm : DepotManager.getInstance().getModPackageNodes()) { |
61 |
if (nm.getUploads().size() == 1) { |
62 |
Mod m = new Mod(nm); |
63 |
if (nm.getTypes().contains(toolTerm)) |
64 |
tools.put(m.getPackageNumber(), m); |
65 |
else |
66 |
mods.put(m.getPackageNumber(), m); |
67 |
modFolders.remove(m.getPackageNumber()); |
68 |
} |
69 |
} |
70 |
|
71 |
for (Mod m : modFolders.values()) { |
72 |
mods.put(m.getPackageNumber(), m); |
73 |
} |
74 |
} |
75 |
|
76 |
public void refreshLocalMods() { |
77 |
// TODO: evtl nur private e.g. als listener für downloads? |
78 |
} |
79 |
|
80 |
/** |
81 |
* @return Singleton instance |
82 |
*/ |
83 |
public static ModManager getInstance() { |
84 |
return instance; |
85 |
} |
86 |
|
87 |
Type getTypeByName(String name) { |
88 |
return types.get(name); |
89 |
} |
90 |
|
91 |
/** |
92 |
* @return Collection of types which do have mods associated |
93 |
*/ |
94 |
public Collection<Type> getTypesWithContent() { |
95 |
Vector<Type> res = new Vector<Type>(); |
96 |
for (Type t : types.values()) { |
97 |
if (t.getEntries().size() > 0) |
98 |
res.add(t); |
99 |
} |
100 |
return res; |
101 |
} |
102 |
|
103 |
/** |
104 |
* @return Collection of mods |
105 |
*/ |
106 |
public Collection<Mod> getMods() { |
107 |
return mods.values(); |
108 |
} |
109 |
|
110 |
/** |
111 |
* @return Mods which are always installed |
112 |
*/ |
113 |
public Collection<Mod> getMandatoryMods() { |
114 |
Vector<Mod> res = new Vector<Mod>(); |
115 |
for (Mod m : mods.values()) { |
116 |
if (m.isMandatoryMod()) |
117 |
res.add(m); |
118 |
} |
119 |
return res; |
120 |
} |
121 |
|
122 |
/** |
123 |
* @return Collection of tools |
124 |
*/ |
125 |
public Collection<Mod> getTools() { |
126 |
return tools.values(); |
127 |
} |
128 |
|
129 |
/** |
130 |
* @return Tools which are always installed |
131 |
*/ |
132 |
public Collection<Mod> getMandatoryTools() { |
133 |
Vector<Mod> res = new Vector<Mod>(); |
134 |
for (Mod m : tools.values()) { |
135 |
if (m.isMandatoryMod()) |
136 |
res.add(m); |
137 |
} |
138 |
return res; |
139 |
} |
140 |
|
141 |
/** |
142 |
* Get a mod by its package number |
143 |
* |
144 |
* @param number |
145 |
* Package number |
146 |
* @return Mod or null |
147 |
*/ |
148 |
public Mod getModByNumber(int number) { |
149 |
for (Mod m : mods.values()) { |
150 |
if (m.getPackageNumber() == number) |
151 |
return m; |
152 |
} |
153 |
return null; |
154 |
} |
155 |
|
156 |
/** |
157 |
* Check for unresolved dependencies within the given mods |
158 |
* |
159 |
* @param mods |
160 |
* Mods to check |
161 |
* @return Unmet dependencies |
162 |
*/ |
163 |
public HashMap<Mod, HashSet<Mod>> checkDependencies(TreeSet<Mod> mods) { |
164 |
// TODO: Verify functionality |
165 |
HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>(); |
166 |
|
167 |
for (Mod m : mods) { |
168 |
for (int depNum : m.getDependencies()) { |
169 |
Mod other = getModByNumber(depNum); |
170 |
if (!mods.contains(other)) { |
171 |
if (!res.containsKey(m)) |
172 |
res.put(m, new HashSet<Mod>()); |
173 |
res.get(m).add(other); |
174 |
} |
175 |
} |
176 |
} |
177 |
|
178 |
return res; |
179 |
} |
180 |
|
181 |
/** |
182 |
* Check for conflicts between given mods |
183 |
* |
184 |
* @param mods |
185 |
* Mods to check |
186 |
* @return Conflicting mods |
187 |
*/ |
188 |
public HashMap<Mod, HashSet<Mod>> checkConflicts(TreeSet<Mod> mods) { |
189 |
// TODO: Verify functionality |
190 |
HashMap<Mod, HashSet<Mod>> res = new HashMap<Mod, HashSet<Mod>>(); |
191 |
|
192 |
for (Mod m : mods) { |
193 |
for (int confNum : m.getConflicts()) { |
194 |
Mod other = getModByNumber(confNum); |
195 |
if (mods.contains(other)) { |
196 |
if (!res.containsKey(m)) |
197 |
res.put(m, new HashSet<Mod>()); |
198 |
res.get(m).add(other); |
199 |
} |
200 |
} |
201 |
} |
202 |
|
203 |
return res; |
204 |
} |
205 |
|
206 |
} |