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