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

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

AEI2:

  • Added BSL handling to installation
  • Updated config-terms for dependencies/incompatibilities
  • Fixed mod counts for types
  • Open Edition folder through menu
  • Open folder of already downloaded mod through context menu
  • (Semi?)Fixed launching Oni on MacOS
  • Settings: Added checkbox for update notification
File size: 8.4 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.util.HashSet;
11
12import net.oni2.aeinstaller.backend.Paths;
13import net.oni2.aeinstaller.backend.Settings;
14import net.oni2.aeinstaller.backend.Settings.Platform;
15import net.oni2.aeinstaller.backend.depot.DepotConfig;
16import net.oni2.aeinstaller.backend.depot.DepotManager;
17import net.oni2.aeinstaller.backend.depot.model.NodeMod;
18import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm;
19
20/**
21 * @author Christian Illy
22 */
23public class Mod implements Comparable<Mod> {
24 private String name = "";
25 private int packageNumber = 0;
26
27 private HashSet<Type> types = new HashSet<Type>();
28 private ECompatiblePlatform platform = null;
29 private String version = "";
30 private String creator = "";
31 private EBSLInstallType bslInstallType = EBSLInstallType.NORMAL;
32 private String description = "";
33 private double aeVersion = 0;
34 private int zipSize = 0;
35 private NodeMod node = null;
36 private net.oni2.aeinstaller.backend.depot.model.File file = null;
37
38 private HashSet<Integer> incompatibilities = new HashSet<Integer>();
39 private HashSet<Integer> dependencies = new HashSet<Integer>();
40
41 private long localTimestamp = 0;
42
43 /**
44 * Create a new Mod entry from a given Mod-Node
45 *
46 * @param nm
47 * Mod-Node
48 */
49 public Mod(NodeMod nm) {
50 node = nm;
51 name = nm.getTitle();
52 packageNumber = nm.getPackageNumber();
53 platform = nm.getPlatform();
54 for (TaxonomyTerm tt : nm.getTypes()) {
55 Type t = ModManager.getInstance().getTypeByName(tt.getName());
56 types.add(t);
57 if (!nm.isTool() && isValidOnPlatform())
58 t.addEntry(this);
59 }
60 version = nm.getVersion();
61 creator = nm.getCreator();
62 if (nm.getBody() != null)
63 description = nm.getBody().getSafe_value();
64 file = DepotManager.getInstance().getFile(
65 nm.getUploads().firstElement().getFid());
66 zipSize = file.getFilesize();
67
68 if (isLocalAvailable())
69 updateLocalData();
70 }
71
72 /**
73 * Update information for local package existence
74 */
75 public void updateLocalData() {
76 File config = new File(getLocalPath(), "Mod_Info.cfg");
77 File aeicfg = new File(getLocalPath(), "aei.cfg");
78 if (config.exists()) {
79 try {
80 FileInputStream fstream = new FileInputStream(config);
81 InputStreamReader isr = new InputStreamReader(fstream);
82 BufferedReader br = new BufferedReader(isr);
83 String strLine;
84 while ((strLine = br.readLine()) != null) {
85 if (strLine.indexOf("->") < 1)
86 continue;
87 if (strLine.indexOf("//") >= 0)
88 strLine = strLine.substring(0, strLine.indexOf("//"));
89 String[] split = strLine.split("->", 2);
90 String sName = split[0].trim();
91 String sVal = split[1].trim();
92 if (sName.equalsIgnoreCase("AEInstallVersion")) {
93 aeVersion = Double.parseDouble(sVal);
94 } else if (sName.equalsIgnoreCase("NameOfMod")) {
95 if (node == null)
96 name = sVal;
97 } else if (sName.equalsIgnoreCase("Creator")) {
98 if (node == null)
99 creator = sVal;
100 } else if (sName.equalsIgnoreCase("HasBsl")) {
101 if (sVal.equalsIgnoreCase("addon"))
102 bslInstallType = EBSLInstallType.ADDON;
103 } else if (sName.equalsIgnoreCase("ModVersion")) {
104 if (node == null)
105 version = sVal;
106 } else if (sName.equalsIgnoreCase("Readme")) {
107 if (node == null)
108 description = sVal.replaceAll("\\\\n", "<br>");
109 } else if (sName.equalsIgnoreCase("DependsOn")) {
110 String[] depsS = sVal.split(",");
111 for (String s : depsS) {
112 try {
113 int dep = Integer.parseInt(s);
114 dependencies.add(dep);
115 } catch (NumberFormatException e) {
116 System.err
117 .format("Mod %05d does contain a non-number dependency: '%s'\n",
118 packageNumber, s);
119 }
120 }
121 } else if (sName.equalsIgnoreCase("IncompatibleWith")) {
122 String[] confS = sVal.split(",");
123 for (String s : confS) {
124 try {
125 int conf = Integer.parseInt(s);
126 incompatibilities.add(conf);
127 } catch (NumberFormatException e) {
128 System.err
129 .format("Mod %05d does contain a non-number incompatibility: '%s'\n",
130 packageNumber, s);
131 }
132 }
133 }
134 }
135 isr.close();
136 } catch (FileNotFoundException e) {
137 } catch (IOException e) {
138 e.printStackTrace();
139 }
140 } else {
141 System.err.println("No config found for mod folder: "
142 + getLocalPath().getPath());
143 }
144 if (aeicfg.exists()) {
145 try {
146 FileInputStream fstream = new FileInputStream(aeicfg);
147 InputStreamReader isr = new InputStreamReader(fstream);
148 BufferedReader br = new BufferedReader(isr);
149 String strLine;
150 while ((strLine = br.readLine()) != null) {
151 if (strLine.indexOf("->") < 1)
152 continue;
153 if (strLine.indexOf("//") >= 0)
154 strLine = strLine.substring(0, strLine.indexOf("//"));
155 String[] split = strLine.split("->", 2);
156 String sName = split[0].trim();
157 String sVal = split[1].trim();
158 if (sName.equalsIgnoreCase("Timestamp")) {
159 localTimestamp = Long.parseLong(sVal);
160 }
161 }
162 isr.close();
163 } catch (FileNotFoundException e) {
164 } catch (IOException e) {
165 e.printStackTrace();
166 }
167 }
168 }
169
170 /**
171 * Create a new Mod entry from the given local mod folder
172 *
173 * @param folder
174 * Mod folder with Mod_Info.cfg
175 */
176 public Mod(File folder) {
177 packageNumber = Integer.parseInt(folder.getName().substring(0, 5));
178 updateLocalData();
179
180 platform = ECompatiblePlatform.BOTH;
181 }
182
183 /**
184 * @return has separate paths for win/mac/common or not
185 */
186 public boolean hasSeparatePlatformDirs() {
187 return aeVersion >= 2;
188 }
189
190 /**
191 * @return Path to local mod folder
192 */
193 public File getLocalPath() {
194 final String folderStart = String.format("%05d", packageNumber);
195
196 if (Paths.getModsPath().exists()) {
197 for (File f : Paths.getModsPath().listFiles(new FilenameFilter() {
198 @Override
199 public boolean accept(File d, String fn) {
200 return fn.startsWith(folderStart);
201 }
202 })) {
203 return f;
204 }
205 }
206
207 return new File(Paths.getModsPath(), folderStart);
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 Name of mod
229 */
230 public String getName() {
231 return name;
232 }
233
234 /**
235 * @return the package number
236 */
237 public int getPackageNumber() {
238 return packageNumber;
239 }
240
241 /**
242 * @return the package number as 5 digit string
243 */
244 public String getPackageNumberString() {
245 return String.format("%05d", packageNumber);
246 }
247
248 /**
249 * @return Types of mod
250 */
251 public HashSet<Type> getTypes() {
252 return types;
253 }
254
255 /**
256 * @return Compatible platforms
257 */
258 public ECompatiblePlatform getPlatform() {
259 return platform;
260 }
261
262 /**
263 * @return Version of mod
264 */
265 public String getVersion() {
266 return version;
267 }
268
269 /**
270 * @return Creator of mod
271 */
272 public String getCreator() {
273 return creator;
274 }
275
276 /**
277 * @return Installation type of BSL files
278 */
279 public EBSLInstallType getBSLInstallType() {
280 return bslInstallType;
281 }
282
283 /**
284 * @return Description of mod
285 */
286 public String getDescription() {
287 return description;
288 }
289
290 /**
291 * @return Size of Zip file on Depot
292 */
293 public int getZipSize() {
294 return zipSize;
295 }
296
297 /**
298 * @return Is a mod that is always installed?
299 */
300 public boolean isMandatoryMod() {
301 return packageNumber < DepotConfig.getMandatoryLimit();
302 }
303
304 /**
305 * @return Get the depot file entry
306 */
307 public net.oni2.aeinstaller.backend.depot.model.File getFile() {
308 return file;
309 }
310
311 @Override
312 public String toString() {
313 return name;
314 }
315
316 /**
317 * @return the incompabitilities
318 */
319 public HashSet<Integer> getIncompabitilities() {
320 return incompatibilities;
321 }
322
323 /**
324 * @return the dependencies
325 */
326 public HashSet<Integer> getDependencies() {
327 return dependencies;
328 }
329
330 /**
331 * @return Is this mod valid on the running platform?
332 */
333 public boolean isValidOnPlatform() {
334 switch (platform) {
335 case BOTH:
336 return true;
337 case MACOS:
338 return (Settings.getPlatform() == Platform.MACOS);
339 case WIN:
340 return (Settings.getPlatform() == Platform.WIN)
341 || (Settings.getPlatform() == Platform.LINUX);
342 }
343 return false;
344 }
345
346 @Override
347 public int compareTo(Mod o) {
348 return getPackageNumber() - o.getPackageNumber();
349 }
350}
Note: See TracBrowser for help on using the repository browser.