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

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

AEI2: Added load/save config

File size: 8.6 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 = null;
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> conflicts = 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 for (TaxonomyTerm tt : nm.getTypes()) {
54 Type t = ModManager.getInstance().getTypeByName(tt.getName());
55 types.add(t);
56 if (!nm.isTool())
57 t.addEntry(this);
58 }
59 platform = nm.getPlatform();
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 (sVal.equalsIgnoreCase("yes"))
104 bslInstallType = EBSLInstallType.NORMAL;
105 else
106 bslInstallType = EBSLInstallType.NONE;
107 } else if (sName.equalsIgnoreCase("ModVersion")) {
108 if (node == null)
109 version = sVal;
110 } else if (sName.equalsIgnoreCase("Readme")) {
111 if (node == null)
112 description = sVal.replaceAll("\\\\n", "<br>");
113 } else if (sName.equalsIgnoreCase("Depends")) {
114 String[] depsS = sVal.split(",");
115 for (String s : depsS) {
116 try {
117 int dep = Integer.parseInt(s);
118 dependencies.add(dep);
119 } catch (NumberFormatException e) {
120 System.err
121 .format("Mod %05d does contain a non-number dependency: '%s'\n",
122 packageNumber, s);
123 }
124 }
125 } else if (sName.equalsIgnoreCase("Conflicts")) {
126 String[] confS = sVal.split(",");
127 for (String s : confS) {
128 try {
129 int conf = Integer.parseInt(s);
130 conflicts.add(conf);
131 } catch (NumberFormatException e) {
132 System.err
133 .format("Mod %05d does contain a non-number dependency: '%s'\n",
134 packageNumber, s);
135 }
136 }
137 }
138 }
139 isr.close();
140 } catch (FileNotFoundException e) {
141 } catch (IOException e) {
142 e.printStackTrace();
143 }
144 } else {
145 System.err.println("No config found for mod folder: "
146 + getLocalPath().getPath());
147 }
148 if (aeicfg.exists()) {
149 try {
150 FileInputStream fstream = new FileInputStream(aeicfg);
151 InputStreamReader isr = new InputStreamReader(fstream);
152 BufferedReader br = new BufferedReader(isr);
153 String strLine;
154 while ((strLine = br.readLine()) != null) {
155 if (strLine.indexOf("->") < 1)
156 continue;
157 if (strLine.indexOf("//") >= 0)
158 strLine = strLine.substring(0, strLine.indexOf("//"));
159 String[] split = strLine.split("->", 2);
160 String sName = split[0].trim();
161 String sVal = split[1].trim();
162 if (sName.equalsIgnoreCase("Timestamp")) {
163 localTimestamp = Long.parseLong(sVal);
164 }
165 }
166 isr.close();
167 } catch (FileNotFoundException e) {
168 } catch (IOException e) {
169 e.printStackTrace();
170 }
171 }
172 }
173
174 /**
175 * Create a new Mod entry from the given local mod folder
176 *
177 * @param folder
178 * Mod folder with Mod_Info.cfg
179 */
180 public Mod(File folder) {
181 packageNumber = Integer.parseInt(folder.getName().substring(0, 5));
182 updateLocalData();
183
184 Type t = ModManager.getInstance().getTypeByName("-Local-");
185 types.add(t);
186 t.addEntry(this);
187
188 platform = ECompatiblePlatform.BOTH;
189 }
190
191 /**
192 * @return has separate paths for win/mac/common or not
193 */
194 public boolean hasSeparatePlatformDirs() {
195 return aeVersion >= 2;
196 }
197
198 /**
199 * @return Path to local mod folder
200 */
201 public File getLocalPath() {
202 final String folderStart = String.format("%05d", packageNumber);
203
204 if (Paths.getModsPath().exists()) {
205 for (File f : Paths.getModsPath().listFiles(new FilenameFilter() {
206 @Override
207 public boolean accept(File d, String fn) {
208 return fn.startsWith(folderStart);
209 }
210 })) {
211 return f;
212 }
213 }
214
215 return new File(Paths.getModsPath(), folderStart);
216 }
217
218 /**
219 * @return Is there a newer version on the depot?
220 */
221 public boolean isNewerAvailable() {
222 if (file != null)
223 return file.getTimestamp() > localTimestamp;
224 else
225 return false;
226 }
227
228 /**
229 * @return Mod exists within mods folder
230 */
231 public boolean isLocalAvailable() {
232 return getLocalPath().exists();
233 }
234
235 /**
236 * @return Name of mod
237 */
238 public String getName() {
239 return name;
240 }
241
242 /**
243 * @return the package number
244 */
245 public int getPackageNumber() {
246 return packageNumber;
247 }
248
249 /**
250 * @return the package number as 5 digit string
251 */
252 public String getPackageNumberString() {
253 return String.format("%05d", packageNumber);
254 }
255
256 /**
257 * @return Types of mod
258 */
259 public HashSet<Type> getTypes() {
260 return types;
261 }
262
263 /**
264 * @return Compatible platforms
265 */
266 public ECompatiblePlatform getPlatform() {
267 return platform;
268 }
269
270 /**
271 * @return Version of mod
272 */
273 public String getVersion() {
274 return version;
275 }
276
277 /**
278 * @return Creator of mod
279 */
280 public String getCreator() {
281 return creator;
282 }
283
284 /**
285 * @return Installation type of BSL files
286 */
287 public EBSLInstallType getBSLInstallType() {
288 return bslInstallType;
289 }
290
291 /**
292 * @return Description of mod
293 */
294 public String getDescription() {
295 return description;
296 }
297
298 /**
299 * @return Size of Zip file on Depot
300 */
301 public int getZipSize() {
302 return zipSize;
303 }
304
305 /**
306 * @return Is a mod that is always installed?
307 */
308 public boolean isMandatoryMod() {
309 return packageNumber < DepotConfig.getMandatoryLimit();
310 }
311
312 /**
313 * @return Get the depot file entry
314 */
315 public net.oni2.aeinstaller.backend.depot.model.File getFile() {
316 return file;
317 }
318
319 @Override
320 public String toString() {
321 return name;
322 }
323
324 /**
325 * @return the conflicts
326 */
327 public HashSet<Integer> getConflicts() {
328 return conflicts;
329 }
330
331 /**
332 * @return the dependencies
333 */
334 public HashSet<Integer> getDependencies() {
335 return dependencies;
336 }
337
338 /**
339 * @return Is this mod valid on the running platform?
340 */
341 public boolean validOnPlatform() {
342 ECompatiblePlatform plat = platform;
343 switch (plat) {
344 case BOTH:
345 return true;
346 case MACOS:
347 return (Settings.getPlatform() == Platform.MACOS);
348 case WIN:
349 return (Settings.getPlatform() == Platform.WIN)
350 || (Settings.getPlatform() == Platform.LINUX);
351 }
352 return false;
353 }
354
355 @Override
356 public int compareTo(Mod o) {
357 return getPackageNumber() - o.getPackageNumber();
358 }
359}
Note: See TracBrowser for help on using the repository browser.