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

Last change on this file since 790 was 749, checked in by alloc, 12 years ago

AEI2 0.99x:

  • Refactoring
File size: 9.9 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;
[749]16import net.oni2.aeinstaller.backend.oni.management.ToolsManager;
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 = "";
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;
[749]44 private net.oni2.moddepot.model.File file = null;
[648]45
46 private File exeFile = null;
[672]47 private EExeType exeType = EExeType.OSBINARY;
[648]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();
[749]69 for (String nType : nm.getTypes()) {
70 Type t = PackageManager.getInstance().getTypeByName(nType);
[648]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();
[670]78 if (nm.getBody() != null) {
[648]79 description = nm.getBody().getSafe_value();
[670]80 if (!description.toLowerCase().startsWith("<p>"))
81 description = "<p>" + description + "</p>";
82 }
[749]83 file = nm.getUploads().firstElement();
[648]84 zipSize = file.getFilesize();
85
86 if (isLocalAvailable())
87 updateLocalData();
88 }
89
[657]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
[648]103 /**
104 * Update information for local package existence
105 */
106 public void updateLocalData() {
[749]107 File config = CaseInsensitiveFile.getCaseInsensitiveFile(
108 getLocalPath(), "Mod_Info.cfg");
[648]109 File aeicfg = new File(getLocalPath(), "aei.cfg");
[749]110 File plain = CaseInsensitiveFile.getCaseInsensitiveFile(getLocalPath(),
111 "plain");
[648]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();
[672]129 exeType = mi.getExeType();
[648]130 workingDir = mi.getWorkingDir();
131 iconFile = mi.getIconFile();
132 } else {
[657]133 clearLocalOnlyInfo();
[648]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)
[749]232 return ToolsManager.getInstalledTools().contains(packageNumber);
[648]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() {
[749]325 return packageNumber < DepotConfig.corePackageNumberLimit;
[648]326 }
327
328 /**
329 * @return Get the depot file entry
330 */
[749]331 public net.oni2.moddepot.model.File getFile() {
[648]332 return file;
333 }
334
335 /**
[657]336 * @return Get the depot Node
337 */
338 public NodeMod getNode() {
339 return node;
340 }
341
342 /**
[648]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 /**
[672]393 * @return Executable type of this tool
394 */
395 public EExeType getExeType() {
396 return exeType;
397 }
398
399 /**
[648]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:
[720]429 return (PlatformInformation.getPlatform() == Platform.MACOS);
[648]430 case WIN:
[720]431 return (PlatformInformation.getPlatform() == Platform.WIN)
432 || (PlatformInformation.getPlatform() == Platform.LINUX);
[648]433 }
434 return false;
435 }
436
[657]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
[648]451 @Override
452 public int compareTo(Package o) {
453 return getPackageNumber() - o.getPackageNumber();
454 }
455
456}
Note: See TracBrowser for help on using the repository browser.