1 |
package net.oni2.aeinstaller.backend.oni; |
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileFilter; |
5 |
import java.io.FileInputStream; |
6 |
import java.io.FileNotFoundException; |
7 |
import java.io.FileOutputStream; |
8 |
import java.io.FilenameFilter; |
9 |
import java.io.IOException; |
10 |
import java.io.InputStream; |
11 |
import java.io.PrintWriter; |
12 |
import java.text.SimpleDateFormat; |
13 |
import java.util.Date; |
14 |
import java.util.HashMap; |
15 |
import java.util.HashSet; |
16 |
import java.util.List; |
17 |
import java.util.Scanner; |
18 |
import java.util.TreeMap; |
19 |
import java.util.TreeSet; |
20 |
import java.util.Vector; |
21 |
import java.util.regex.Pattern; |
22 |
|
23 |
import net.oni2.aeinstaller.AEInstaller2; |
24 |
import net.oni2.aeinstaller.backend.Paths; |
25 |
import net.oni2.aeinstaller.backend.Settings; |
26 |
import net.oni2.aeinstaller.backend.Settings.Platform; |
27 |
import net.oni2.aeinstaller.backend.packages.EBSLInstallType; |
28 |
import net.oni2.aeinstaller.backend.packages.Package; |
29 |
import net.oni2.aeinstaller.backend.packages.PackageManager; |
30 |
|
31 |
import org.apache.commons.io.FileUtils; |
32 |
import org.apache.commons.io.filefilter.RegexFileFilter; |
33 |
import org.apache.commons.io.filefilter.TrueFileFilter; |
34 |
import org.javabuilders.swing.SwingJavaBuilder; |
35 |
|
36 |
import com.thoughtworks.xstream.XStream; |
37 |
import com.thoughtworks.xstream.io.xml.StaxDriver; |
38 |
|
39 |
/** |
40 |
* @author Christian Illy |
41 |
*/ |
42 |
public class Installer { |
43 |
private static FileFilter dirFileFilter = new FileFilter() { |
44 |
@Override |
45 |
public boolean accept(File pathname) { |
46 |
return pathname.isDirectory(); |
47 |
} |
48 |
}; |
49 |
|
50 |
/** |
51 |
* @return Is Edition Core initialized |
52 |
*/ |
53 |
public static boolean isEditionInitialized() { |
54 |
return Paths.getVanillaOnisPath().exists(); |
55 |
} |
56 |
|
57 |
private static void createEmptyPath(File path) throws IOException { |
58 |
if (path.exists()) |
59 |
FileUtils.deleteDirectory(path); |
60 |
path.mkdirs(); |
61 |
} |
62 |
|
63 |
/** |
64 |
* @return list of currently installed mods |
65 |
*/ |
66 |
public static Vector<Integer> getInstalledMods() { |
67 |
File installCfg = new File(Paths.getEditionGDF(), "installed_mods.xml"); |
68 |
return PackageManager.getInstance().loadModSelection(installCfg); |
69 |
} |
70 |
|
71 |
/** |
72 |
* @return Currently installed tools |
73 |
*/ |
74 |
@SuppressWarnings("unchecked") |
75 |
public static TreeSet<Integer> getInstalledTools() { |
76 |
File installCfg = new File(Paths.getInstallerPath(), |
77 |
"installed_tools.xml"); |
78 |
TreeSet<Integer> res = new TreeSet<Integer>(); |
79 |
try { |
80 |
if (installCfg.exists()) { |
81 |
FileInputStream fis = new FileInputStream(installCfg); |
82 |
XStream xs = new XStream(new StaxDriver()); |
83 |
Object obj = xs.fromXML(fis); |
84 |
if (obj instanceof TreeSet<?>) |
85 |
res = (TreeSet<Integer>) obj; |
86 |
fis.close(); |
87 |
} |
88 |
} catch (FileNotFoundException e) { |
89 |
e.printStackTrace(); |
90 |
} catch (IOException e) { |
91 |
e.printStackTrace(); |
92 |
} |
93 |
return res; |
94 |
} |
95 |
|
96 |
private static void writeInstalledTools(TreeSet<Integer> tools) { |
97 |
File installCfg = new File(Paths.getInstallerPath(), |
98 |
"installed_tools.xml"); |
99 |
try { |
100 |
FileOutputStream fos = new FileOutputStream(installCfg); |
101 |
XStream xs = new XStream(new StaxDriver()); |
102 |
xs.toXML(tools, fos); |
103 |
fos.close(); |
104 |
} catch (FileNotFoundException e) { |
105 |
e.printStackTrace(); |
106 |
} catch (IOException e) { |
107 |
e.printStackTrace(); |
108 |
} |
109 |
} |
110 |
|
111 |
/** |
112 |
* @param tools |
113 |
* Tools to install |
114 |
*/ |
115 |
public static void installTools(TreeSet<Package> tools) { |
116 |
TreeSet<Integer> installed = getInstalledTools(); |
117 |
for (Package m : tools) { |
118 |
File plain = new File(m.getLocalPath(), "plain"); |
119 |
if (plain.exists()) { |
120 |
if (m.hasSeparatePlatformDirs()) { |
121 |
File plainCommon = new File(plain, "common"); |
122 |
File plainMac = new File(plain, "mac_only"); |
123 |
File plainWin = new File(plain, "win_only"); |
124 |
if (plainCommon.exists()) |
125 |
copyToolsFiles(plainCommon); |
126 |
if (Settings.getPlatform() == Platform.MACOS |
127 |
&& plainMac.exists()) |
128 |
copyToolsFiles(plainMac); |
129 |
else if (plainWin.exists()) |
130 |
copyToolsFiles(plainWin); |
131 |
} else { |
132 |
copyToolsFiles(plain); |
133 |
} |
134 |
} |
135 |
installed.add(m.getPackageNumber()); |
136 |
} |
137 |
writeInstalledTools(installed); |
138 |
} |
139 |
|
140 |
/** |
141 |
* @param tools |
142 |
* Tools to uninstall |
143 |
*/ |
144 |
public static void uninstallTools(TreeSet<Package> tools) { |
145 |
TreeSet<Integer> installed = getInstalledTools(); |
146 |
for (Package m : tools) { |
147 |
if (installed.contains(m.getPackageNumber())) { |
148 |
File plain = new File(m.getLocalPath(), "plain"); |
149 |
if (plain.exists()) { |
150 |
if (m.hasSeparatePlatformDirs()) { |
151 |
File plainCommon = new File(plain, "common"); |
152 |
File plainMac = new File(plain, "mac_only"); |
153 |
File plainWin = new File(plain, "win_only"); |
154 |
if (plainCommon.exists()) |
155 |
removeToolsFiles(plainCommon, |
156 |
Paths.getEditionBasePath()); |
157 |
if (Settings.getPlatform() == Platform.MACOS |
158 |
&& plainMac.exists()) |
159 |
removeToolsFiles(plainMac, |
160 |
Paths.getEditionBasePath()); |
161 |
else if (plainWin.exists()) |
162 |
removeToolsFiles(plainWin, |
163 |
Paths.getEditionBasePath()); |
164 |
} else { |
165 |
removeToolsFiles(plain, Paths.getEditionBasePath()); |
166 |
} |
167 |
} |
168 |
} |
169 |
installed.remove(m.getPackageNumber()); |
170 |
} |
171 |
writeInstalledTools(installed); |
172 |
} |
173 |
|
174 |
private static void copyToolsFiles(File srcFolder) { |
175 |
for (File f : srcFolder.listFiles()) { |
176 |
try { |
177 |
if (f.isDirectory()) |
178 |
FileUtils.copyDirectoryToDirectory(f, |
179 |
Paths.getEditionBasePath()); |
180 |
else |
181 |
FileUtils |
182 |
.copyFileToDirectory(f, Paths.getEditionBasePath()); |
183 |
} catch (IOException e) { |
184 |
e.printStackTrace(); |
185 |
} |
186 |
} |
187 |
} |
188 |
|
189 |
private static void removeToolsFiles(File srcFolder, File target) { |
190 |
for (File f : srcFolder.listFiles()) { |
191 |
if (f.isDirectory()) |
192 |
removeToolsFiles(f, new File(target, f.getName())); |
193 |
else { |
194 |
File targetFile = new File(target, f.getName()); |
195 |
if (targetFile.exists()) |
196 |
targetFile.delete(); |
197 |
} |
198 |
} |
199 |
if (target.list().length == 0) |
200 |
target.delete(); |
201 |
} |
202 |
|
203 |
/** |
204 |
* Install the given set of mods |
205 |
* |
206 |
* @param mods |
207 |
* Mods to install |
208 |
* @param listener |
209 |
* Listener for install progress updates |
210 |
*/ |
211 |
public static void install(TreeSet<Package> mods, |
212 |
InstallProgressListener listener) { |
213 |
File logFile = new File(Paths.getInstallerPath(), "Installation.log"); |
214 |
PrintWriter log = null; |
215 |
try { |
216 |
log = new PrintWriter(logFile); |
217 |
} catch (FileNotFoundException e) { |
218 |
e.printStackTrace(); |
219 |
} |
220 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
221 |
Date start = new Date(); |
222 |
log.println("Installation of mods started at " + sdf.format(start)); |
223 |
|
224 |
log.println(); |
225 |
log.println("AEI2 version: " |
226 |
+ SwingJavaBuilder.getConfig().getResource("appversion")); |
227 |
log.println("Installed tools:"); |
228 |
for (Package t : PackageManager.getInstance().getInstalledTools()) { |
229 |
log.println(String.format(" - %s (%s)", t.getName(), t.getVersion())); |
230 |
} |
231 |
log.println("Installing mods:"); |
232 |
for (Package m : mods) { |
233 |
log.println(String.format(" - %s (%s)", m.getName(), m.getVersion())); |
234 |
} |
235 |
log.println(); |
236 |
|
237 |
Paths.getEditionGDF().mkdirs(); |
238 |
for (File f : Paths.getEditionGDF().listFiles(new FilenameFilter() { |
239 |
public boolean accept(File arg0, String arg1) { |
240 |
String s = arg1.toLowerCase(); |
241 |
return s.endsWith(".dat") |
242 |
|| s.endsWith(".raw") |
243 |
|| s.endsWith(".sep") |
244 |
|| (s.equals("intro.bik") && !Settings.getInstance() |
245 |
.get("copyintro", false)) |
246 |
|| (s.equals("outro.bik") && !Settings.getInstance() |
247 |
.get("copyoutro", false)); |
248 |
} |
249 |
})) { |
250 |
f.delete(); |
251 |
} |
252 |
File IGMD = new File(Paths.getEditionGDF(), "IGMD"); |
253 |
if (IGMD.exists()) { |
254 |
for (File f : IGMD.listFiles(new FileFilter() { |
255 |
@Override |
256 |
public boolean accept(File pathname) { |
257 |
return pathname.isDirectory(); |
258 |
} |
259 |
})) { |
260 |
File ignore = new File(f, "ignore.txt"); |
261 |
if (!ignore.exists()) { |
262 |
try { |
263 |
FileUtils.deleteDirectory(f); |
264 |
} catch (IOException e) { |
265 |
e.printStackTrace(); |
266 |
} |
267 |
} |
268 |
} |
269 |
} |
270 |
|
271 |
File installCfg = new File(Paths.getEditionGDF(), "installed_mods.xml"); |
272 |
PackageManager.getInstance().saveModSelection(installCfg, mods); |
273 |
|
274 |
TreeSet<Integer> unlockLevels = new TreeSet<Integer>(); |
275 |
|
276 |
Vector<File> foldersOni = new Vector<File>(); |
277 |
foldersOni.add(Paths.getVanillaOnisPath()); |
278 |
|
279 |
Vector<File> foldersPatches = new Vector<File>(); |
280 |
|
281 |
for (Package m : mods) { |
282 |
for (int lev : m.getUnlockLevels()) |
283 |
unlockLevels.add(lev); |
284 |
|
285 |
File oni = new File(m.getLocalPath(), "oni"); |
286 |
if (oni.exists()) { |
287 |
if (m.hasSeparatePlatformDirs()) { |
288 |
File oniCommon = new File(oni, "common"); |
289 |
File oniMac = new File(oni, "mac_only"); |
290 |
File oniWin = new File(oni, "win_only"); |
291 |
if (oniCommon.exists()) |
292 |
foldersOni.add(oniCommon); |
293 |
if (Settings.getPlatform() == Platform.MACOS |
294 |
&& oniMac.exists()) |
295 |
foldersOni.add(oniMac); |
296 |
else if (oniWin.exists()) |
297 |
foldersOni.add(oniWin); |
298 |
} else { |
299 |
foldersOni.add(oni); |
300 |
} |
301 |
} |
302 |
|
303 |
File patches = new File(m.getLocalPath(), "patches"); |
304 |
if (patches.exists()) { |
305 |
if (m.hasSeparatePlatformDirs()) { |
306 |
File patchesCommon = new File(patches, "common"); |
307 |
File patchesMac = new File(patches, "mac_only"); |
308 |
File patchesWin = new File(patches, "win_only"); |
309 |
if (patchesCommon.exists()) |
310 |
foldersPatches.add(patchesCommon); |
311 |
if (Settings.getPlatform() == Platform.MACOS |
312 |
&& patchesMac.exists()) |
313 |
foldersPatches.add(patchesMac); |
314 |
else if (patchesWin.exists()) |
315 |
foldersPatches.add(patchesWin); |
316 |
} else { |
317 |
foldersPatches.add(patches); |
318 |
} |
319 |
} |
320 |
} |
321 |
|
322 |
TreeMap<String, Vector<File>> levels = new TreeMap<String, Vector<File>>(); |
323 |
for (File path : foldersOni) { |
324 |
for (File levelF : path.listFiles()) { |
325 |
String fn = levelF.getName().toLowerCase(); |
326 |
String levelN = null; |
327 |
if (levelF.isDirectory()) { |
328 |
levelN = fn; |
329 |
} else if (fn.endsWith(".dat")) { |
330 |
levelN = fn.substring(0, fn.lastIndexOf('.')).toLowerCase(); |
331 |
} |
332 |
if (levelN != null) { |
333 |
if (!levels.containsKey(levelN)) |
334 |
levels.put(levelN, new Vector<File>()); |
335 |
levels.get(levelN).add(levelF); |
336 |
} |
337 |
} |
338 |
} |
339 |
|
340 |
applyPatches(levels, foldersPatches, listener, log); |
341 |
|
342 |
combineBinaryFiles(levels, listener, log); |
343 |
combineBSLFolders(mods, listener, log); |
344 |
|
345 |
copyVideos(log); |
346 |
|
347 |
if (unlockLevels.size() > 0) { |
348 |
unlockLevels(unlockLevels, log); |
349 |
} |
350 |
|
351 |
log.println(); |
352 |
Date end = new Date(); |
353 |
log.println("Initialization ended at " + sdf.format(end)); |
354 |
log.println("Process took " |
355 |
+ ((end.getTime() - start.getTime()) / 1000) + " seconds"); |
356 |
log.close(); |
357 |
} |
358 |
|
359 |
private static void combineBSLFolders(TreeSet<Package> mods, |
360 |
InstallProgressListener listener, PrintWriter log) { |
361 |
listener.installProgressUpdate(95, 100, "Installing BSL files"); |
362 |
log.println("Installing BSL files"); |
363 |
|
364 |
HashMap<EBSLInstallType, Vector<Package>> modsToInclude = new HashMap<EBSLInstallType, Vector<Package>>(); |
365 |
modsToInclude.put(EBSLInstallType.NORMAL, new Vector<Package>()); |
366 |
modsToInclude.put(EBSLInstallType.ADDON, new Vector<Package>()); |
367 |
|
368 |
for (Package m : mods.descendingSet()) { |
369 |
File bsl = new File(m.getLocalPath(), "bsl"); |
370 |
if (bsl.exists()) { |
371 |
if (m.hasSeparatePlatformDirs()) { |
372 |
File bslCommon = new File(bsl, "common"); |
373 |
File bslMac = new File(bsl, "mac_only"); |
374 |
File bslWin = new File(bsl, "win_only"); |
375 |
if ((Settings.getPlatform() == Platform.MACOS && bslMac |
376 |
.exists()) |
377 |
|| ((Settings.getPlatform() == Platform.WIN || Settings |
378 |
.getPlatform() == Platform.LINUX) && bslWin |
379 |
.exists()) || bslCommon.exists()) { |
380 |
modsToInclude.get(m.getBSLInstallType()).add(m); |
381 |
} |
382 |
} else { |
383 |
modsToInclude.get(m.getBSLInstallType()).add(m); |
384 |
} |
385 |
} |
386 |
} |
387 |
|
388 |
for (Package m : modsToInclude.get(EBSLInstallType.NORMAL)) { |
389 |
copyBSL(m, false); |
390 |
} |
391 |
Vector<Package> addons = modsToInclude.get(EBSLInstallType.ADDON); |
392 |
for (int i = addons.size() - 1; i >= 0; i--) { |
393 |
copyBSL(addons.get(i), true); |
394 |
} |
395 |
} |
396 |
|
397 |
private static void copyBSL(Package sourceMod, boolean addon) { |
398 |
File targetBaseFolder = new File(Paths.getEditionGDF(), "IGMD"); |
399 |
if (!targetBaseFolder.exists()) |
400 |
targetBaseFolder.mkdir(); |
401 |
|
402 |
Vector<File> sources = new Vector<File>(); |
403 |
File bsl = new File(sourceMod.getLocalPath(), "bsl"); |
404 |
if (sourceMod.hasSeparatePlatformDirs()) { |
405 |
File bslCommon = new File(bsl, "common"); |
406 |
File bslMac = new File(bsl, "mac_only"); |
407 |
File bslWin = new File(bsl, "win_only"); |
408 |
if (Settings.getPlatform() == Platform.MACOS && bslMac.exists()) { |
409 |
for (File f : bslMac.listFiles(dirFileFilter)) { |
410 |
File targetBSL = new File(targetBaseFolder, f.getName()); |
411 |
if (addon || !targetBSL.exists()) |
412 |
sources.add(f); |
413 |
} |
414 |
} |
415 |
if ((Settings.getPlatform() == Platform.WIN || Settings |
416 |
.getPlatform() == Platform.LINUX) && bslWin.exists()) { |
417 |
for (File f : bslWin.listFiles(dirFileFilter)) { |
418 |
File targetBSL = new File(targetBaseFolder, f.getName()); |
419 |
if (addon || !targetBSL.exists()) |
420 |
sources.add(f); |
421 |
} |
422 |
} |
423 |
if (bslCommon.exists()) { |
424 |
for (File f : bslCommon.listFiles(dirFileFilter)) { |
425 |
File targetBSL = new File(targetBaseFolder, f.getName()); |
426 |
if (addon || !targetBSL.exists()) |
427 |
sources.add(f); |
428 |
} |
429 |
} |
430 |
} else { |
431 |
for (File f : bsl.listFiles(dirFileFilter)) { |
432 |
File targetBSL = new File(targetBaseFolder, f.getName()); |
433 |
if (addon || !targetBSL.exists()) |
434 |
sources.add(f); |
435 |
} |
436 |
} |
437 |
|
438 |
System.out.println("For mod: " + sourceMod.getName() |
439 |
+ " install BSL folders: " + sources.toString()); |
440 |
for (File f : sources) { |
441 |
File targetPath = new File(targetBaseFolder, f.getName()); |
442 |
if (!targetPath.exists()) |
443 |
targetPath.mkdir(); |
444 |
if (!(new File(targetPath, "ignore.txt").exists())) { |
445 |
for (File fbsl : f.listFiles()) { |
446 |
if (fbsl.getName().toLowerCase().endsWith(".bsl")) { |
447 |
File targetFile = new File(targetPath, fbsl.getName()); |
448 |
if (addon || !targetFile.exists()) { |
449 |
try { |
450 |
FileUtils.copyFile(fbsl, targetFile); |
451 |
} catch (IOException e) { |
452 |
e.printStackTrace(); |
453 |
} |
454 |
} |
455 |
} |
456 |
} |
457 |
} |
458 |
} |
459 |
} |
460 |
|
461 |
private static void applyPatches( |
462 |
TreeMap<String, Vector<File>> oniLevelFolders, |
463 |
List<File> patchFolders, InstallProgressListener listener, |
464 |
PrintWriter log) { |
465 |
// TODO: REMOVE |
466 |
long startMS = new Date().getTime(); |
467 |
// TODO: Implement this |
468 |
String tmpFolderName = "installrun_temp-" |
469 |
+ new SimpleDateFormat("yyyy_MM_dd-HH_mm_ss") |
470 |
.format(new Date()); |
471 |
File tmpFolder = new File(Paths.getTempPath(), tmpFolderName); |
472 |
|
473 |
HashMap<String, Vector<File>> patches = new HashMap<String, Vector<File>>(); |
474 |
for (File patchFolder : patchFolders) { |
475 |
for (File levelFolder : patchFolder.listFiles(dirFileFilter)) { |
476 |
String lvlName = levelFolder.getName().toLowerCase(); |
477 |
for (File f : FileUtils.listFiles(levelFolder, |
478 |
new String[] { "oni-patch" }, true)) { |
479 |
if (!patches.containsKey(lvlName)) |
480 |
patches.put(lvlName, new Vector<File>()); |
481 |
patches.get(lvlName).add(f); |
482 |
} |
483 |
} |
484 |
} |
485 |
|
486 |
for (String level : patches.keySet()) { |
487 |
File levelFolder = new File(tmpFolder, level); |
488 |
levelFolder.mkdir(); |
489 |
|
490 |
// Get files to be patched from vanilla.dat / packages |
491 |
for (File patch : patches.get(level)) { |
492 |
String patternWildcard = patch.getName(); |
493 |
patternWildcard = patternWildcard.substring(0, |
494 |
patternWildcard.indexOf(".oni-patch")); |
495 |
patternWildcard = patternWildcard.replace('-', '*'); |
496 |
final Pattern patternRegex = Pattern.compile( |
497 |
patternWildcard.replaceAll("\\*", ".\\*"), |
498 |
Pattern.CASE_INSENSITIVE); |
499 |
|
500 |
for (File srcFolder : oniLevelFolders.get(level)) { |
501 |
if (srcFolder.isFile()) { |
502 |
// Extract from .dat |
503 |
OniSplit.export(levelFolder, srcFolder, patternWildcard); |
504 |
} else { |
505 |
// Copy from folder with overwrite |
506 |
for (File f : FileUtils.listFiles(srcFolder, |
507 |
new RegexFileFilter(patternRegex), |
508 |
TrueFileFilter.TRUE)) { |
509 |
try { |
510 |
FileUtils.copyFileToDirectory(f, levelFolder); |
511 |
} catch (IOException e) { |
512 |
e.printStackTrace(); |
513 |
} |
514 |
} |
515 |
} |
516 |
} |
517 |
} |
518 |
|
519 |
// Extract files to XML |
520 |
File levelFolderXML = new File(levelFolder, "xml"); |
521 |
Vector<File> files = new Vector<File>(); |
522 |
files.add(new File(levelFolder, "*.oni")); |
523 |
OniSplit.convertOniToXML(levelFolderXML, files); |
524 |
|
525 |
// Apply patches in levelFolderXML |
526 |
for (File patch : patches.get(level)) { |
527 |
String patternWildcard = patch.getName(); |
528 |
patternWildcard = patternWildcard.substring(0, |
529 |
patternWildcard.indexOf(".oni-patch")); |
530 |
patternWildcard = patternWildcard.replace('-', '*'); |
531 |
final Pattern patternRegex = Pattern.compile( |
532 |
patternWildcard.replaceAll("\\*", ".\\*"), |
533 |
Pattern.CASE_INSENSITIVE); |
534 |
|
535 |
for (File toPatch : FileUtils.listFiles(levelFolderXML, |
536 |
new RegexFileFilter(patternRegex), null)) { |
537 |
// Apply patch "patch" to "toPatch" |
538 |
XMLTools.patch(patch, toPatch); |
539 |
} |
540 |
} |
541 |
|
542 |
// Create .oni files from XML |
543 |
files.clear(); |
544 |
files.add(new File(levelFolderXML, "*.xml")); |
545 |
OniSplit.convertXMLtoOni(levelFolder, files); |
546 |
|
547 |
// Remove XML folder as import will only require .oni's |
548 |
try { |
549 |
FileUtils.deleteDirectory(levelFolderXML); |
550 |
} catch (IOException e) { |
551 |
e.printStackTrace(); |
552 |
} |
553 |
|
554 |
oniLevelFolders.get(level).add(levelFolder); |
555 |
} |
556 |
|
557 |
System.out.println("Took: " + (new Date().getTime() - startMS) |
558 |
+ " msec"); |
559 |
System.exit(0); |
560 |
} |
561 |
|
562 |
private static void combineBinaryFiles( |
563 |
TreeMap<String, Vector<File>> oniLevelFolders, |
564 |
InstallProgressListener listener, PrintWriter log) { |
565 |
int totalSteps = 0; |
566 |
int stepsDone = 0; |
567 |
|
568 |
for (@SuppressWarnings("unused") |
569 |
String s : oniLevelFolders.keySet()) |
570 |
totalSteps++; |
571 |
totalSteps++; |
572 |
|
573 |
log.println("Importing levels"); |
574 |
for (String l : oniLevelFolders.keySet()) { |
575 |
log.println("\tLevel " + l); |
576 |
listener.installProgressUpdate(stepsDone, totalSteps, |
577 |
"Installing level " + l); |
578 |
for (File f : oniLevelFolders.get(l)) { |
579 |
log.println("\t\t\t" + f.getPath()); |
580 |
} |
581 |
|
582 |
Vector<String> res = OniSplit.packLevel(oniLevelFolders.get(l), |
583 |
new File(Paths.getEditionGDF(), sanitizeLevelName(l) |
584 |
+ ".dat")); |
585 |
if (res != null && res.size() > 0) { |
586 |
for (String s : res) |
587 |
log.println("\t\t" + s); |
588 |
} |
589 |
|
590 |
log.println(); |
591 |
stepsDone++; |
592 |
} |
593 |
} |
594 |
|
595 |
private static void copyVideos(PrintWriter log) { |
596 |
if (Settings.getInstance().get("copyintro", false)) { |
597 |
File src = new File(Paths.getVanillaGDF(), "intro.bik"); |
598 |
log.println("Copying intro"); |
599 |
if (src.exists()) { |
600 |
try { |
601 |
FileUtils.copyFileToDirectory(src, Paths.getEditionGDF()); |
602 |
} catch (IOException e) { |
603 |
e.printStackTrace(); |
604 |
} |
605 |
} |
606 |
} |
607 |
if (Settings.getInstance().get("copyoutro", true)) { |
608 |
File src = new File(Paths.getVanillaGDF(), "outro.bik"); |
609 |
log.println("Copying outro"); |
610 |
if (src.exists()) { |
611 |
try { |
612 |
FileUtils.copyFileToDirectory(src, Paths.getEditionGDF()); |
613 |
} catch (IOException e) { |
614 |
e.printStackTrace(); |
615 |
} |
616 |
} |
617 |
} |
618 |
} |
619 |
|
620 |
private static void unlockLevels(TreeSet<Integer> unlockLevels, |
621 |
PrintWriter log) { |
622 |
File dat = new File(Paths.getEditionBasePath(), "persist.dat"); |
623 |
log.println("Unlocking levels: " + unlockLevels.toString()); |
624 |
if (!dat.exists()) { |
625 |
InputStream is = AEInstaller2.class |
626 |
.getResourceAsStream("/net/oni2/aeinstaller/resources/persist.dat"); |
627 |
try { |
628 |
FileUtils.copyInputStreamToFile(is, dat); |
629 |
} catch (IOException e) { |
630 |
e.printStackTrace(); |
631 |
} |
632 |
} |
633 |
PersistDat save = new PersistDat(dat); |
634 |
HashSet<Integer> currentlyUnlocked = save.getUnlockedLevels(); |
635 |
currentlyUnlocked.addAll(unlockLevels); |
636 |
save.setUnlockedLevels(currentlyUnlocked); |
637 |
save.close(); |
638 |
} |
639 |
|
640 |
/** |
641 |
* Initializes the Edition core |
642 |
* |
643 |
* @param listener |
644 |
* Listener for status updates |
645 |
*/ |
646 |
public static void initializeEdition(InstallProgressListener listener) { |
647 |
File init = new File(Paths.getTempPath(), "init"); |
648 |
|
649 |
int totalSteps = 0; |
650 |
int stepsDone = 0; |
651 |
|
652 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
653 |
|
654 |
for (@SuppressWarnings("unused") |
655 |
File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() { |
656 |
@Override |
657 |
public boolean accept(File dir, String name) { |
658 |
return name.endsWith(".dat"); |
659 |
} |
660 |
})) { |
661 |
totalSteps++; |
662 |
} |
663 |
totalSteps = totalSteps * 2 + 2; |
664 |
|
665 |
try { |
666 |
File logFile = new File(Paths.getInstallerPath(), |
667 |
"Initialization.log"); |
668 |
PrintWriter log = new PrintWriter(logFile); |
669 |
|
670 |
Date start = new Date(); |
671 |
log.println("Initialization of Edition core started at " |
672 |
+ sdf.format(start)); |
673 |
log.println("Cleaning directories"); |
674 |
|
675 |
listener.installProgressUpdate(stepsDone, totalSteps, |
676 |
"Cleaning up directories"); |
677 |
createEmptyPath(Paths.getVanillaOnisPath()); |
678 |
createEmptyPath(init); |
679 |
File level0Folder = new File(init, "level0_Final"); |
680 |
createEmptyPath(level0Folder); |
681 |
|
682 |
stepsDone++; |
683 |
|
684 |
log.println("Exporting levels and moving files to level0"); |
685 |
|
686 |
for (File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() { |
687 |
@Override |
688 |
public boolean accept(File dir, String name) { |
689 |
return name.endsWith(".dat"); |
690 |
} |
691 |
})) { |
692 |
String levelName = f.getName().substring(0, |
693 |
f.getName().indexOf('.')); |
694 |
Scanner fi = new Scanner(levelName); |
695 |
int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+")); |
696 |
|
697 |
log.println("\t" + levelName + ":"); |
698 |
log.println("\t\tExporting"); |
699 |
listener.installProgressUpdate(stepsDone, totalSteps, |
700 |
"Exporting vanilla level " + levelNumber); |
701 |
|
702 |
// Edition/GameDataFolder/level*_Final/ |
703 |
File tempLevelFolder = new File(init, levelName); |
704 |
|
705 |
// Export Vanilla-Level-Dat -> Temp/Level |
706 |
Vector<String> res = OniSplit.export(tempLevelFolder, f); |
707 |
if (res != null && res.size() > 0) { |
708 |
for (String s : res) |
709 |
log.println("\t\t\t" + s); |
710 |
} |
711 |
|
712 |
log.println("\t\tMoving files"); |
713 |
handleFileGlobalisation(tempLevelFolder, level0Folder, |
714 |
levelNumber); |
715 |
stepsDone++; |
716 |
log.println(); |
717 |
} |
718 |
|
719 |
log.println("Reimporting levels"); |
720 |
|
721 |
for (File f : init.listFiles()) { |
722 |
String levelName = f.getName(); |
723 |
|
724 |
log.println("\t" + levelName); |
725 |
listener.installProgressUpdate(stepsDone, totalSteps, |
726 |
"Creating globalized " + levelName); |
727 |
|
728 |
Vector<File> folders = new Vector<File>(); |
729 |
folders.add(f); |
730 |
|
731 |
Vector<String> res = OniSplit.importLevel(folders, new File( |
732 |
Paths.getVanillaOnisPath(), levelName + ".dat")); |
733 |
if (res != null && res.size() > 0) { |
734 |
for (String s : res) |
735 |
log.println("\t\t" + s); |
736 |
} |
737 |
|
738 |
log.println(); |
739 |
stepsDone++; |
740 |
} |
741 |
|
742 |
listener.installProgressUpdate(stepsDone, totalSteps, |
743 |
"Copying basic files"); |
744 |
// Copy Oni-configs |
745 |
File persistVanilla = new File(Paths.getOniBasePath(), |
746 |
"persist.dat"); |
747 |
File persistEdition = new File(Paths.getEditionBasePath(), |
748 |
"persist.dat"); |
749 |
File keyConfVanilla = new File(Paths.getOniBasePath(), |
750 |
"key_config.txt"); |
751 |
File keyConfEdition = new File(Paths.getEditionBasePath(), |
752 |
"key_config.txt"); |
753 |
if (persistVanilla.exists() && !persistEdition.exists()) |
754 |
FileUtils.copyFile(persistVanilla, persistEdition); |
755 |
if (keyConfVanilla.exists() && !keyConfEdition.exists()) |
756 |
FileUtils.copyFile(keyConfVanilla, keyConfEdition); |
757 |
|
758 |
FileUtils.deleteDirectory(init); |
759 |
|
760 |
Date end = new Date(); |
761 |
log.println("Initialization ended at " + sdf.format(end)); |
762 |
log.println("Process took " |
763 |
+ ((end.getTime() - start.getTime()) / 1000) + " seconds"); |
764 |
log.close(); |
765 |
} catch (IOException e) { |
766 |
e.printStackTrace(); |
767 |
} |
768 |
} |
769 |
|
770 |
private static void moveFileToTargetOrDelete(File source, File target) { |
771 |
if (source.equals(target)) |
772 |
return; |
773 |
if (!target.exists()) { |
774 |
if (!source.renameTo(target)) { |
775 |
System.err.println("File " + source.getPath() + " not moved!"); |
776 |
} |
777 |
} else if (!source.delete()) { |
778 |
System.err.println("File " + source.getPath() + " not deleted!"); |
779 |
} |
780 |
} |
781 |
|
782 |
private static void handleFileGlobalisation(File tempFolder, |
783 |
File level0Folder, int levelNumber) { |
784 |
// Move AKEV and related files to subfolder so they're not globalized: |
785 |
if (levelNumber != 0) { |
786 |
File akevFolder = new File(tempFolder, "AKEV"); |
787 |
akevFolder.mkdir(); |
788 |
OniSplit.move(akevFolder, tempFolder.getPath() + "/AKEV*.oni", |
789 |
"overwrite"); |
790 |
} |
791 |
|
792 |
for (File f : tempFolder.listFiles(new FileFilter() { |
793 |
@Override |
794 |
public boolean accept(File pathname) { |
795 |
return pathname.isFile(); |
796 |
} |
797 |
})) { |
798 |
// Move matching files to subfolder NoGlobal: |
799 |
if (f.getName().startsWith("TXMPfail") |
800 |
|| f.getName().startsWith("TXMPlevel") |
801 |
|| (f.getName().startsWith("TXMP") && f.getName().contains( |
802 |
"intro")) |
803 |
|| f.getName().startsWith("TXMB") |
804 |
|| f.getName().equals("M3GMpowerup_lsi.oni") |
805 |
|| f.getName().equals("TXMPlsi_icon.oni") |
806 |
|| (f.getName().startsWith("TXMB") && f.getName().contains( |
807 |
"splash_screen.oni"))) { |
808 |
File noGlobal = new File(tempFolder, "NoGlobal"); |
809 |
noGlobal.mkdir(); |
810 |
File noGlobalFile = new File(noGlobal, f.getName()); |
811 |
moveFileToTargetOrDelete(f, noGlobalFile); |
812 |
} |
813 |
// Move matching files to level0_Animations/level0_TRAC |
814 |
else if (f.getName().startsWith("TRAC")) { |
815 |
File level0File = new File(level0Folder, f.getName()); |
816 |
moveFileToTargetOrDelete(f, level0File); |
817 |
} |
818 |
// Move matching files to level0_Animations/level0_TRAM |
819 |
else if (f.getName().startsWith("TRAM")) { |
820 |
File level0File = new File(level0Folder, f.getName()); |
821 |
moveFileToTargetOrDelete(f, level0File); |
822 |
} |
823 |
// Move matching files to level0_Textures |
824 |
else if (f.getName().startsWith("ONSK") |
825 |
|| f.getName().startsWith("TXMP")) { |
826 |
File level0File = new File(level0Folder, f.getName()); |
827 |
moveFileToTargetOrDelete(f, level0File); |
828 |
} |
829 |
// Move matching files to *VANILLA*/level0_Characters |
830 |
else if (f.getName().startsWith("ONCC") |
831 |
|| f.getName().startsWith("TRBS") |
832 |
|| f.getName().startsWith("ONCV") |
833 |
|| f.getName().startsWith("ONVL") |
834 |
|| f.getName().startsWith("TRMA") |
835 |
|| f.getName().startsWith("TRSC") |
836 |
|| f.getName().startsWith("TRAS")) { |
837 |
File level0File = new File(level0Folder, f.getName()); |
838 |
moveFileToTargetOrDelete(f, level0File); |
839 |
} |
840 |
// Move matching files to level0_Sounds |
841 |
else if (f.getName().startsWith("OSBD") |
842 |
|| f.getName().startsWith("SNDD")) { |
843 |
File level0File = new File(level0Folder, f.getName()); |
844 |
moveFileToTargetOrDelete(f, level0File); |
845 |
} |
846 |
// Move matching files to level0_Particles |
847 |
else if (f.getName().startsWith("BINA3") |
848 |
|| f.getName().startsWith("M3GMdebris") |
849 |
|| f.getName().equals("M3GMtoxic_bubble.oni") |
850 |
|| f.getName().startsWith("M3GMelec") |
851 |
|| f.getName().startsWith("M3GMrat") |
852 |
|| f.getName().startsWith("M3GMjet") |
853 |
|| f.getName().startsWith("M3GMbomb_") |
854 |
|| f.getName().equals("M3GMbarab_swave.oni") |
855 |
|| f.getName().equals("M3GMbloodyfoot.oni")) { |
856 |
File level0File = new File(level0Folder, f.getName()); |
857 |
moveFileToTargetOrDelete(f, level0File); |
858 |
} |
859 |
// Move matching files to Archive (aka delete them) |
860 |
else if (f.getName().startsWith("AGDB") |
861 |
|| f.getName().startsWith("TRCM")) { |
862 |
f.delete(); |
863 |
} |
864 |
// Move matching files to /level0_Final/ |
865 |
else if (f.getName().startsWith("ONWC")) { |
866 |
File level0File = new File(level0Folder, f.getName()); |
867 |
moveFileToTargetOrDelete(f, level0File); |
868 |
} |
869 |
} |
870 |
} |
871 |
|
872 |
private static String sanitizeLevelName(String ln) { |
873 |
int ind = ln.indexOf("_"); |
874 |
String res = ln.substring(0, ind + 1); |
875 |
res += ln.substring(ind + 1, ind + 2).toUpperCase(); |
876 |
res += ln.substring(ind + 2); |
877 |
return res; |
878 |
} |
879 |
|
880 |
/** |
881 |
* Verify that the Edition is within a subfolder to vanilla Oni |
882 |
* (..../Oni/Edition/AEInstaller) |
883 |
* |
884 |
* @return true if GDF can be found in the parent's parent-path |
885 |
*/ |
886 |
public static boolean verifyRunningDirectory() { |
887 |
return Paths.getVanillaGDF().exists() |
888 |
&& Paths.getVanillaGDF().isDirectory(); |
889 |
} |
890 |
} |