source: AE/installer2/src/net/oni2/aeinstaller/backend/mods/Mod.java@ 639

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

AEI2 0.96:

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