source: AE/installer2/src/net/oni2/aeinstaller/backend/packages/Package.java@ 693

Last change on this file since 693 was 672, checked in by alloc, 12 years ago

AEI2 0.99q:

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