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