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