1 |
package net.oni2.aeinstaller.backend.mods; |
2 |
|
3 |
import java.io.BufferedReader; |
4 |
import java.io.File; |
5 |
import java.io.FileInputStream; |
6 |
import java.io.FileNotFoundException; |
7 |
import java.io.FilenameFilter; |
8 |
import java.io.IOException; |
9 |
import java.io.InputStreamReader; |
10 |
import java.net.URI; |
11 |
import java.net.URISyntaxException; |
12 |
import java.util.HashSet; |
13 |
|
14 |
import net.oni2.aeinstaller.backend.Paths; |
15 |
import net.oni2.aeinstaller.backend.Settings; |
16 |
import net.oni2.aeinstaller.backend.Settings.Platform; |
17 |
import net.oni2.aeinstaller.backend.depot.DepotConfig; |
18 |
import net.oni2.aeinstaller.backend.depot.DepotManager; |
19 |
import net.oni2.aeinstaller.backend.depot.model.NodeMod; |
20 |
import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm; |
21 |
|
22 |
/** |
23 |
* @author Christian Illy |
24 |
*/ |
25 |
public class Mod implements Comparable<Mod> { |
26 |
private String name = ""; |
27 |
private int packageNumber = 0; |
28 |
|
29 |
private HashSet<Type> types = new HashSet<Type>(); |
30 |
private boolean tool = false; |
31 |
private ECompatiblePlatform platform = null; |
32 |
private String version = ""; |
33 |
private String creator = ""; |
34 |
private EBSLInstallType bslInstallType = EBSLInstallType.NORMAL; |
35 |
private String description = ""; |
36 |
private double aeVersion = 0; |
37 |
private int zipSize = 0; |
38 |
private NodeMod node = null; |
39 |
private net.oni2.aeinstaller.backend.depot.model.File file = null; |
40 |
|
41 |
private File exeFile = null; |
42 |
private File iconFile = null; |
43 |
private String workingDir = "Base"; |
44 |
|
45 |
private HashSet<Integer> incompatibilities = new HashSet<Integer>(); |
46 |
private HashSet<Integer> dependencies = new HashSet<Integer>(); |
47 |
private HashSet<Integer> unlockLevel = new HashSet<Integer>(); |
48 |
|
49 |
private long localTimestamp = 0; |
50 |
|
51 |
/** |
52 |
* Create a new Mod entry from a given Mod-Node |
53 |
* |
54 |
* @param nm |
55 |
* Mod-Node |
56 |
*/ |
57 |
public Mod(NodeMod nm) { |
58 |
node = nm; |
59 |
name = nm.getTitle(); |
60 |
packageNumber = nm.getPackageNumber(); |
61 |
platform = nm.getPlatform(); |
62 |
tool = nm.isTool(); |
63 |
for (TaxonomyTerm tt : nm.getTypes()) { |
64 |
Type t = ModManager.getInstance().getTypeByName(tt.getName()); |
65 |
types.add(t); |
66 |
if (!tool && !isMandatoryMod() && isValidOnPlatform()) |
67 |
t.addEntry(this); |
68 |
} |
69 |
version = nm.getVersion(); |
70 |
creator = nm.getCreator(); |
71 |
if (nm.getBody() != null) |
72 |
description = nm.getBody().getSafe_value(); |
73 |
file = DepotManager.getInstance().getFile( |
74 |
nm.getUploads().firstElement().getFid()); |
75 |
zipSize = file.getFilesize(); |
76 |
|
77 |
if (isLocalAvailable()) |
78 |
updateLocalData(); |
79 |
} |
80 |
|
81 |
/** |
82 |
* Update information for local package existence |
83 |
*/ |
84 |
public void updateLocalData() { |
85 |
File config = new File(getLocalPath(), "Mod_Info.cfg"); |
86 |
File aeicfg = new File(getLocalPath(), "aei.cfg"); |
87 |
File plain = new File(getLocalPath(), "plain"); |
88 |
if (config.exists()) { |
89 |
Mod_Info mi = new Mod_Info(config, packageNumber); |
90 |
|
91 |
aeVersion = mi.getAeVersion(); |
92 |
bslInstallType = mi.getBslInstallType(); |
93 |
if (node == null) { |
94 |
name = mi.getName(); |
95 |
creator = mi.getCreator(); |
96 |
version = mi.getVersion(); |
97 |
description = mi.getDescription(); |
98 |
} |
99 |
|
100 |
dependencies = mi.getDependencies(); |
101 |
incompatibilities = mi.getIncompatibilities(); |
102 |
unlockLevel = mi.getUnlockLevel(); |
103 |
|
104 |
exeFile = mi.getExeFile(); |
105 |
workingDir = mi.getWorkingDir(); |
106 |
iconFile = mi.getIconFile(); |
107 |
} else { |
108 |
System.err.println("No config found for mod folder: " |
109 |
+ getLocalPath().getPath()); |
110 |
} |
111 |
if (aeicfg.exists()) { |
112 |
try { |
113 |
FileInputStream fstream = new FileInputStream(aeicfg); |
114 |
InputStreamReader isr = new InputStreamReader(fstream); |
115 |
BufferedReader br = new BufferedReader(isr); |
116 |
String strLine; |
117 |
while ((strLine = br.readLine()) != null) { |
118 |
if (strLine.indexOf("->") < 1) |
119 |
continue; |
120 |
if (strLine.indexOf("//") >= 0) |
121 |
strLine = strLine.substring(0, strLine.indexOf("//")); |
122 |
String[] split = strLine.split("->", 2); |
123 |
String sName = split[0].trim(); |
124 |
String sVal = split[1].trim(); |
125 |
if (sName.equalsIgnoreCase("Timestamp")) { |
126 |
localTimestamp = Long.parseLong(sVal); |
127 |
} |
128 |
} |
129 |
isr.close(); |
130 |
} catch (FileNotFoundException e) { |
131 |
} catch (IOException e) { |
132 |
e.printStackTrace(); |
133 |
} |
134 |
} |
135 |
if (node == null) |
136 |
tool = plain.exists(); |
137 |
} |
138 |
|
139 |
/** |
140 |
* Create a new Mod entry from the given local mod folder |
141 |
* |
142 |
* @param folder |
143 |
* Mod folder with Mod_Info.cfg |
144 |
*/ |
145 |
public Mod(File folder) { |
146 |
packageNumber = Integer.parseInt(folder.getName().substring(0, 5)); |
147 |
updateLocalData(); |
148 |
|
149 |
platform = ECompatiblePlatform.BOTH; |
150 |
} |
151 |
|
152 |
/** |
153 |
* @return has separate paths for win/mac/common or not |
154 |
*/ |
155 |
public boolean hasSeparatePlatformDirs() { |
156 |
return aeVersion >= 2; |
157 |
} |
158 |
|
159 |
private String getSanitizedPathName() { |
160 |
return name.replaceAll("[^a-zA-Z0-9_.-]", "_"); |
161 |
} |
162 |
|
163 |
/** |
164 |
* @return Path to local mod folder |
165 |
*/ |
166 |
public File getLocalPath() { |
167 |
final String folderStart = String.format("%05d", packageNumber); |
168 |
|
169 |
if (Paths.getModsPath().exists()) { |
170 |
for (File f : Paths.getModsPath().listFiles(new FilenameFilter() { |
171 |
@Override |
172 |
public boolean accept(File d, String fn) { |
173 |
return fn.startsWith(folderStart); |
174 |
} |
175 |
})) { |
176 |
return f; |
177 |
} |
178 |
} |
179 |
|
180 |
return new File(Paths.getModsPath(), folderStart |
181 |
+ getSanitizedPathName()); |
182 |
} |
183 |
|
184 |
/** |
185 |
* @return Is there a newer version on the depot? |
186 |
*/ |
187 |
public boolean isNewerAvailable() { |
188 |
if (file != null) |
189 |
return file.getTimestamp() > localTimestamp; |
190 |
else |
191 |
return false; |
192 |
} |
193 |
|
194 |
/** |
195 |
* @return Mod exists within mods folder |
196 |
*/ |
197 |
public boolean isLocalAvailable() { |
198 |
return getLocalPath().exists(); |
199 |
} |
200 |
|
201 |
/** |
202 |
* @return Is mod installed? |
203 |
*/ |
204 |
public boolean isInstalled() { |
205 |
return ModManager.getInstance().isModInstalled(this); |
206 |
} |
207 |
|
208 |
/** |
209 |
* @return Name of mod |
210 |
*/ |
211 |
public String getName() { |
212 |
return name; |
213 |
} |
214 |
|
215 |
/** |
216 |
* @return the package number |
217 |
*/ |
218 |
public int getPackageNumber() { |
219 |
return packageNumber; |
220 |
} |
221 |
|
222 |
/** |
223 |
* @return the package number as 5 digit string |
224 |
*/ |
225 |
public String getPackageNumberString() { |
226 |
return String.format("%05d", packageNumber); |
227 |
} |
228 |
|
229 |
/** |
230 |
* @return Types of mod |
231 |
*/ |
232 |
public HashSet<Type> getTypes() { |
233 |
return types; |
234 |
} |
235 |
|
236 |
/** |
237 |
* @return Is this mod actually a tool? |
238 |
*/ |
239 |
public boolean isTool() { |
240 |
return tool; |
241 |
} |
242 |
|
243 |
/** |
244 |
* @return Compatible platforms |
245 |
*/ |
246 |
public ECompatiblePlatform getPlatform() { |
247 |
return platform; |
248 |
} |
249 |
|
250 |
/** |
251 |
* @return Version of mod |
252 |
*/ |
253 |
public String getVersion() { |
254 |
return version; |
255 |
} |
256 |
|
257 |
/** |
258 |
* @return Creator of mod |
259 |
*/ |
260 |
public String getCreator() { |
261 |
return creator; |
262 |
} |
263 |
|
264 |
/** |
265 |
* @return Installation type of BSL files |
266 |
*/ |
267 |
public EBSLInstallType getBSLInstallType() { |
268 |
return bslInstallType; |
269 |
} |
270 |
|
271 |
/** |
272 |
* @return Description of mod |
273 |
*/ |
274 |
public String getDescription() { |
275 |
return description; |
276 |
} |
277 |
|
278 |
/** |
279 |
* @return Size of Zip file on Depot |
280 |
*/ |
281 |
public int getZipSize() { |
282 |
return zipSize; |
283 |
} |
284 |
|
285 |
/** |
286 |
* @return Is a mod that is always installed? |
287 |
*/ |
288 |
public boolean isMandatoryMod() { |
289 |
return packageNumber < DepotConfig.getMandatoryLimit(); |
290 |
} |
291 |
|
292 |
/** |
293 |
* @return Get the depot file entry |
294 |
*/ |
295 |
public net.oni2.aeinstaller.backend.depot.model.File getFile() { |
296 |
return file; |
297 |
} |
298 |
|
299 |
/** |
300 |
* @return Depot page URI |
301 |
*/ |
302 |
public URI getUrl() { |
303 |
if (node == null) |
304 |
return null; |
305 |
if (node.getPath() == null) |
306 |
return null; |
307 |
URI res = null; |
308 |
try { |
309 |
res = new URI(node.getPath()); |
310 |
} catch (URISyntaxException e) { |
311 |
e.printStackTrace(); |
312 |
} |
313 |
return res; |
314 |
} |
315 |
|
316 |
@Override |
317 |
public String toString() { |
318 |
return name; |
319 |
} |
320 |
|
321 |
/** |
322 |
* @return the incompabitilities |
323 |
*/ |
324 |
public HashSet<Integer> getIncompabitilities() { |
325 |
return incompatibilities; |
326 |
} |
327 |
|
328 |
/** |
329 |
* @return the dependencies |
330 |
*/ |
331 |
public HashSet<Integer> getDependencies() { |
332 |
return dependencies; |
333 |
} |
334 |
|
335 |
/** |
336 |
* @return the levels this mod will unlock |
337 |
*/ |
338 |
public HashSet<Integer> getUnlockLevels() { |
339 |
return unlockLevel; |
340 |
} |
341 |
|
342 |
/** |
343 |
* @return Executable name of this tool |
344 |
*/ |
345 |
public File getExeFile() { |
346 |
return exeFile; |
347 |
} |
348 |
|
349 |
/** |
350 |
* @return Icon file of this tool |
351 |
*/ |
352 |
public File getIconFile() { |
353 |
return iconFile; |
354 |
} |
355 |
|
356 |
/** |
357 |
* @return Working directory of this tool |
358 |
*/ |
359 |
public File getWorkingDir() { |
360 |
if (workingDir.equalsIgnoreCase("Exe")) { |
361 |
if (exeFile != null) |
362 |
return exeFile.getParentFile(); |
363 |
else |
364 |
return Paths.getEditionGDF(); |
365 |
} else if (workingDir.equalsIgnoreCase("GDF")) |
366 |
return Paths.getEditionGDF(); |
367 |
else |
368 |
return Paths.getEditionBasePath(); |
369 |
} |
370 |
|
371 |
/** |
372 |
* @return Is this mod valid on the running platform? |
373 |
*/ |
374 |
public boolean isValidOnPlatform() { |
375 |
switch (platform) { |
376 |
case BOTH: |
377 |
return true; |
378 |
case MACOS: |
379 |
return (Settings.getPlatform() == Platform.MACOS); |
380 |
case WIN: |
381 |
return (Settings.getPlatform() == Platform.WIN) |
382 |
|| (Settings.getPlatform() == Platform.LINUX); |
383 |
} |
384 |
return false; |
385 |
} |
386 |
|
387 |
@Override |
388 |
public int compareTo(Mod o) { |
389 |
return getPackageNumber() - o.getPackageNumber(); |
390 |
} |
391 |
} |