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