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

Last change on this file since 924 was 857, checked in by alloc, 12 years ago

AEI2.10:

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