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

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

AEI2 0.97:

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