source: java/installer2/src/net/oni2/aeinstaller/backend/oni/management/Installer.java@ 808

Last change on this file since 808 was 804, checked in by alloc, 12 years ago

AEI2.02:

File size: 18.7 KB
Line 
1package net.oni2.aeinstaller.backend.oni.management;
2
3import java.io.File;
4import java.io.FileFilter;
5import java.io.FileNotFoundException;
6import java.io.FilenameFilter;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.PrintWriter;
10import java.text.SimpleDateFormat;
11import java.util.Date;
12import java.util.HashMap;
13import java.util.HashSet;
14import java.util.List;
15import java.util.TreeMap;
16import java.util.TreeSet;
17import java.util.Vector;
18import java.util.regex.Pattern;
19
20import net.oni2.SettingsManager;
21import net.oni2.aeinstaller.AEInstaller2;
22import net.oni2.aeinstaller.backend.CaseInsensitiveFile;
23import net.oni2.aeinstaller.backend.Paths;
24import net.oni2.aeinstaller.backend.oni.OniSplit;
25import net.oni2.aeinstaller.backend.oni.PersistDat;
26import net.oni2.aeinstaller.backend.oni.XMLTools;
27import net.oni2.aeinstaller.backend.oni.management.tools.ToolInstallationList;
28import net.oni2.aeinstaller.backend.packages.EBSLInstallType;
29import net.oni2.aeinstaller.backend.packages.Package;
30import net.oni2.aeinstaller.backend.packages.PackageManager;
31import net.oni2.platformtools.PlatformInformation;
32import net.oni2.platformtools.PlatformInformation.Platform;
33import net.oni2.platformtools.applicationinvoker.ApplicationInvocationResult;
34
35import org.apache.commons.io.FileUtils;
36import org.apache.commons.io.filefilter.RegexFileFilter;
37import org.apache.commons.io.filefilter.TrueFileFilter;
38import org.javabuilders.swing.SwingJavaBuilder;
39
40import com.paour.NaturalOrderComparator;
41
42/**
43 * @author Christian Illy
44 */
45public class Installer {
46 private static FileFilter dirFileFilter = new FileFilter() {
47 @Override
48 public boolean accept(File pathname) {
49 return pathname.isDirectory();
50 }
51 };
52
53 /**
54 * Verify that the Edition is within a subfolder to vanilla Oni
55 * (..../Oni/Edition/AEInstaller)
56 *
57 * @return true if GDF can be found in the parent's parent-path
58 */
59 public static boolean verifyRunningDirectory() {
60 return Paths.getVanillaGDF().exists()
61 && Paths.getVanillaGDF().isDirectory();
62 }
63
64 /**
65 * @return Is Edition Core initialized
66 */
67 public static boolean isEditionInitialized() {
68 return Paths.getVanillaOnisPath().exists();
69 }
70
71 /**
72 * @return list of currently installed mods
73 */
74 public static Vector<Integer> getInstalledMods() {
75 File installCfg = new File(Paths.getEditionGDF(), "installed_mods.xml");
76 return PackageManager.getInstance().loadModSelection(installCfg);
77 }
78
79 /**
80 * Install the given set of mods
81 *
82 * @param mods
83 * Mods to install
84 * @param listener
85 * Listener for install progress updates
86 */
87 public static void install(TreeSet<Package> mods,
88 InstallProgressListener listener) {
89 File logFile = new File(Paths.getInstallerPath(), "Installation.log");
90 Logger log = null;
91 try {
92 log = new Logger(logFile);
93 } catch (FileNotFoundException e) {
94 e.printStackTrace();
95 }
96 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
97 Date start = new Date();
98 log.println("Installation of mods started at " + sdf.format(start));
99
100 log.println();
101 log.println("AEI2 version: "
102 + SwingJavaBuilder.getConfig().getResource("appversion"));
103
104 ToolInstallationList til = ToolInstallationList.getInstance();
105 log.println("Installed tools:");
106 for (Package t : PackageManager.getInstance().getInstalledTools()) {
107 log.println(String.format(" - %s (%s)", t.getName(), t.getVersion())
108 + (til.isModified(t.getPackageNumber()) ? " (! LOCALLY MODIFIED !)"
109 : ""));
110 }
111 log.println("Installing mods:");
112 for (Package m : mods) {
113 log.println(String.format(" - %s (%s)", m.getName(), m.getVersion()));
114 }
115 log.println();
116
117 Paths.getEditionGDF().mkdirs();
118 for (File f : Paths.getEditionGDF().listFiles(new FilenameFilter() {
119 public boolean accept(File arg0, String arg1) {
120 String s = arg1.toLowerCase();
121 return s.endsWith(".dat")
122 || s.endsWith(".raw")
123 || s.endsWith(".sep")
124 || (s.equals("intro.bik") && !SettingsManager
125 .getInstance().get("copyintro", false))
126 || (s.equals("outro.bik") && !SettingsManager
127 .getInstance().get("copyoutro", false));
128 }
129 })) {
130 f.delete();
131 }
132 File IGMD = new File(Paths.getEditionGDF(), "IGMD");
133 if (IGMD.exists()) {
134 for (File f : IGMD.listFiles(new FileFilter() {
135 @Override
136 public boolean accept(File pathname) {
137 return pathname.isDirectory();
138 }
139 })) {
140 File ignore = CaseInsensitiveFile.getCaseInsensitiveFile(f,
141 "ignore.txt");
142 if (!ignore.exists()) {
143 try {
144 FileUtils.deleteDirectory(f);
145 } catch (IOException e) {
146 e.printStackTrace();
147 }
148 }
149 }
150 }
151
152 File installCfg = new File(Paths.getEditionGDF(), "installed_mods.xml");
153 PackageManager.getInstance().saveModSelection(installCfg, mods);
154
155 TreeSet<Integer> unlockLevels = new TreeSet<Integer>();
156
157 Vector<File> foldersOni = new Vector<File>();
158 foldersOni.add(Paths.getVanillaOnisPath());
159
160 Vector<File> foldersPatches = new Vector<File>();
161
162 for (Package m : mods) {
163 for (int lev : m.getUnlockLevels())
164 unlockLevels.add(lev);
165
166 File oni = CaseInsensitiveFile.getCaseInsensitiveFile(
167 m.getLocalPath(), "oni");
168 if (oni.exists()) {
169 if (m.hasSeparatePlatformDirs()) {
170 File oniCommon = CaseInsensitiveFile
171 .getCaseInsensitiveFile(oni, "common");
172 File oniMac = CaseInsensitiveFile.getCaseInsensitiveFile(
173 oni, "mac_only");
174 File oniWin = CaseInsensitiveFile.getCaseInsensitiveFile(
175 oni, "win_only");
176 if (oniCommon.exists())
177 foldersOni.add(oniCommon);
178 if (PlatformInformation.getPlatform() == Platform.MACOS
179 && oniMac.exists())
180 foldersOni.add(oniMac);
181 else if (oniWin.exists())
182 foldersOni.add(oniWin);
183 } else {
184 foldersOni.add(oni);
185 }
186 }
187
188 File patches = CaseInsensitiveFile.getCaseInsensitiveFile(
189 m.getLocalPath(), "patches");
190 if (patches.exists()) {
191 if (m.hasSeparatePlatformDirs()) {
192 File patchesCommon = CaseInsensitiveFile
193 .getCaseInsensitiveFile(patches, "common");
194 File patchesMac = CaseInsensitiveFile
195 .getCaseInsensitiveFile(patches, "mac_only");
196 File patchesWin = CaseInsensitiveFile
197 .getCaseInsensitiveFile(patches, "win_only");
198 if (patchesCommon.exists())
199 foldersPatches.add(patchesCommon);
200 if (PlatformInformation.getPlatform() == Platform.MACOS
201 && patchesMac.exists())
202 foldersPatches.add(patchesMac);
203 else if (patchesWin.exists())
204 foldersPatches.add(patchesWin);
205 } else {
206 foldersPatches.add(patches);
207 }
208 }
209 }
210
211 TreeMap<String, Vector<File>> levels = new TreeMap<String, Vector<File>>(
212 new NaturalOrderComparator());
213 for (File path : foldersOni) {
214 for (File levelF : path.listFiles()) {
215 String fn = levelF.getName().toLowerCase();
216 String levelN = null;
217 if (levelF.isDirectory()) {
218 levelN = fn;
219 } else if (fn.endsWith(".dat")) {
220 levelN = fn.substring(0, fn.lastIndexOf('.')).toLowerCase();
221 }
222 if (levelN != null) {
223 if (!levels.containsKey(levelN))
224 levels.put(levelN, new Vector<File>());
225 levels.get(levelN).add(levelF);
226 }
227 }
228 }
229
230 applyPatches(levels, foldersPatches, listener, log);
231
232 combineBinaryFiles(levels, listener, log);
233 combineBSLFolders(mods, listener, log);
234
235 copyVideos(log);
236
237 if (unlockLevels.size() > 0) {
238 unlockLevels(unlockLevels, log);
239 }
240
241 log.println();
242 Date end = new Date();
243 log.println("Installation ended at " + sdf.format(end));
244 log.println("Process took "
245 + ((end.getTime() - start.getTime()) / 1000) + " seconds");
246 log.close();
247 }
248
249 private static void combineBSLFolders(TreeSet<Package> mods,
250 InstallProgressListener listener, Logger log) {
251 listener.installProgressUpdate(95, 100, "Installing BSL files");
252 log.println();
253 log.println("Installing BSL files");
254
255 HashMap<EBSLInstallType, Vector<Package>> modsToInclude = new HashMap<EBSLInstallType, Vector<Package>>();
256 modsToInclude.put(EBSLInstallType.NORMAL, new Vector<Package>());
257 modsToInclude.put(EBSLInstallType.ADDON, new Vector<Package>());
258
259 for (Package m : mods.descendingSet()) {
260 File bsl = CaseInsensitiveFile.getCaseInsensitiveFile(
261 m.getLocalPath(), "bsl");
262 if (bsl.exists()) {
263 if (m.hasSeparatePlatformDirs()) {
264 File bslCommon = CaseInsensitiveFile
265 .getCaseInsensitiveFile(bsl, "common");
266 File bslMac = CaseInsensitiveFile.getCaseInsensitiveFile(
267 bsl, "mac_only");
268 File bslWin = CaseInsensitiveFile.getCaseInsensitiveFile(
269 bsl, "win_only");
270 if ((PlatformInformation.getPlatform() == Platform.MACOS && bslMac
271 .exists())
272 || ((PlatformInformation.getPlatform() == Platform.WIN || PlatformInformation
273 .getPlatform() == Platform.LINUX) && bslWin
274 .exists()) || bslCommon.exists()) {
275 modsToInclude.get(m.getBSLInstallType()).add(m);
276 }
277 } else {
278 modsToInclude.get(m.getBSLInstallType()).add(m);
279 }
280 }
281 }
282
283 for (Package m : modsToInclude.get(EBSLInstallType.NORMAL)) {
284 copyBSL(m, false, log);
285 }
286 Vector<Package> addons = modsToInclude.get(EBSLInstallType.ADDON);
287 for (int i = addons.size() - 1; i >= 0; i--) {
288 copyBSL(addons.get(i), true, log);
289 }
290 }
291
292 private static void copyBSL(Package sourceMod, boolean addon, Logger log) {
293 File targetBaseFolder = new File(Paths.getEditionGDF(), "IGMD");
294 if (!targetBaseFolder.exists())
295 targetBaseFolder.mkdir();
296
297 Vector<File> sources = new Vector<File>();
298 File bsl = CaseInsensitiveFile.getCaseInsensitiveFile(
299 sourceMod.getLocalPath(), "bsl");
300 if (sourceMod.hasSeparatePlatformDirs()) {
301 File bslCommon = CaseInsensitiveFile.getCaseInsensitiveFile(bsl,
302 "common");
303 File bslMac = CaseInsensitiveFile.getCaseInsensitiveFile(bsl,
304 "mac_only");
305 File bslWin = CaseInsensitiveFile.getCaseInsensitiveFile(bsl,
306 "win_only");
307 if (PlatformInformation.getPlatform() == Platform.MACOS
308 && bslMac.exists()) {
309 for (File f : bslMac.listFiles(dirFileFilter)) {
310 File targetBSL = new File(targetBaseFolder, f.getName());
311 if (addon || !targetBSL.exists())
312 sources.add(f);
313 }
314 }
315 if ((PlatformInformation.getPlatform() == Platform.WIN || PlatformInformation
316 .getPlatform() == Platform.LINUX) && bslWin.exists()) {
317 for (File f : bslWin.listFiles(dirFileFilter)) {
318 File targetBSL = new File(targetBaseFolder, f.getName());
319 if (addon || !targetBSL.exists())
320 sources.add(f);
321 }
322 }
323 if (bslCommon.exists()) {
324 for (File f : bslCommon.listFiles(dirFileFilter)) {
325 File targetBSL = new File(targetBaseFolder, f.getName());
326 if (addon || !targetBSL.exists())
327 sources.add(f);
328 }
329 }
330 } else {
331 for (File f : bsl.listFiles(dirFileFilter)) {
332 File targetBSL = new File(targetBaseFolder, f.getName());
333 if (addon || !targetBSL.exists())
334 sources.add(f);
335 }
336 }
337
338 log.println("\tMod \"" + sourceMod.getName() + "\"");
339 for (File f : sources) {
340 log.println("\t\t" + f.getName());
341 File targetPath = new File(targetBaseFolder, f.getName());
342 if (!targetPath.exists())
343 targetPath.mkdir();
344 if (!(CaseInsensitiveFile.getCaseInsensitiveFile(targetPath,
345 "ignore.txt").exists())) {
346 for (File fbsl : f.listFiles()) {
347 if (fbsl.getName().toLowerCase().endsWith(".bsl")) {
348 File targetFile = new File(targetPath, fbsl.getName());
349 if (addon || !targetFile.exists()) {
350 try {
351 FileUtils.copyFile(fbsl, targetFile);
352 } catch (IOException e) {
353 e.printStackTrace();
354 }
355 }
356 }
357 }
358 }
359 }
360 }
361
362 private static void applyPatches(
363 TreeMap<String, Vector<File>> oniLevelFolders,
364 List<File> patchFolders, InstallProgressListener listener,
365 Logger log) {
366 log.println();
367 log.println("Applying XML patches");
368 listener.installProgressUpdate(0, 1, "Applying XML patches");
369
370 long startMS = new Date().getTime();
371
372 String tmpFolderName = "installrun_temp-"
373 + new SimpleDateFormat("yyyy_MM_dd-HH_mm_ss")
374 .format(new Date());
375 File tmpFolder = new File(Paths.getTempPath(), tmpFolderName);
376 tmpFolder.mkdir();
377
378 TreeMap<String, Vector<File>> patches = new TreeMap<String, Vector<File>>(
379 new NaturalOrderComparator());
380 for (File patchFolder : patchFolders) {
381 for (File levelFolder : patchFolder.listFiles(dirFileFilter)) {
382 String lvlName = levelFolder.getName().toLowerCase();
383 for (File f : FileUtils.listFiles(levelFolder,
384 new String[] { "oni-patch" }, true)) {
385 if (!patches.containsKey(lvlName))
386 patches.put(lvlName, new Vector<File>());
387 patches.get(lvlName).add(f);
388 }
389 }
390 }
391
392 for (String level : patches.keySet()) {
393 File levelFolder = new File(tmpFolder, level);
394 levelFolder.mkdir();
395
396 log.println("\t\tPatches for " + level);
397
398 Vector<String> exportPatterns = new Vector<String>();
399 // Get files to be patched from vanilla.dat
400 for (File patch : patches.get(level)) {
401 String patternWildcard = patch.getName();
402 patternWildcard = patternWildcard.substring(0,
403 patternWildcard.indexOf(".oni-patch"));
404 patternWildcard = patternWildcard.replace('-', '*');
405 exportPatterns.add(patternWildcard);
406 }
407 for (File srcFolder : oniLevelFolders.get(level)) {
408 if (srcFolder.isFile()) {
409 if (srcFolder.getPath().toLowerCase().contains("vanilla")) {
410 // Extract from .dat
411 ApplicationInvocationResult res = OniSplit.export(
412 levelFolder, srcFolder, exportPatterns);
413 log.logAppOutput(res, true);
414 }
415 }
416 }
417
418 // Get files to be patched from packages
419 for (File patch : patches.get(level)) {
420 String patternWildcard = patch.getName();
421 patternWildcard = patternWildcard.substring(0,
422 patternWildcard.indexOf(".oni-patch"));
423 patternWildcard = patternWildcard.replace('-', '*');
424 patternWildcard = patternWildcard + ".oni";
425 Vector<String> patterns = new Vector<String>();
426 patterns.add(patternWildcard);
427 final Pattern patternRegex = Pattern.compile(
428 patternWildcard.replaceAll("\\*", ".\\*"),
429 Pattern.CASE_INSENSITIVE);
430
431 for (File srcFolder : oniLevelFolders.get(level)) {
432 if (srcFolder.isFile()) {
433 if (!srcFolder.getPath().toLowerCase()
434 .contains("vanilla")) {
435 // Extract from .dat
436 ApplicationInvocationResult res = OniSplit.export(
437 levelFolder, srcFolder, patterns);
438 log.logAppOutput(res, true);
439 }
440 } else {
441 // Copy from folder with overwrite
442 for (File f : FileUtils.listFiles(srcFolder,
443 new RegexFileFilter(patternRegex),
444 TrueFileFilter.TRUE)) {
445 try {
446 FileUtils.copyFileToDirectory(f, levelFolder);
447 } catch (IOException e) {
448 e.printStackTrace();
449 }
450 }
451 }
452 }
453 }
454
455 // Extract files to XML
456 File levelFolderXML = new File(levelFolder, "xml");
457 Vector<File> files = new Vector<File>();
458 files.add(new File(levelFolder, "*.oni"));
459 ApplicationInvocationResult res = OniSplit.convertOniToXML(
460 levelFolderXML, files);
461 log.logAppOutput(res, true);
462
463 // Create masterpatch file (containing calls to all individual
464 // patches)
465 File masterpatch = new File(levelFolderXML, "masterpatch.oni-patch");
466 PrintWriter masterpatchWriter = null;
467 try {
468 masterpatchWriter = new PrintWriter(masterpatch);
469 } catch (FileNotFoundException e) {
470 e.printStackTrace();
471 }
472 for (File patch : patches.get(level)) {
473 String patternWildcard = patch.getName();
474 patternWildcard = patternWildcard.substring(0,
475 patternWildcard.indexOf(".oni-patch"));
476 patternWildcard = patternWildcard + ".xml";
477 patternWildcard = patternWildcard.replace('-', '*');
478 File xmlFilePath = new File(levelFolderXML, patternWildcard);
479 masterpatchWriter
480 .println(String
481 .format("@COMMAND patchfile -filename:\"%s\" -forceinfiles:\"%s\"",
482 patch.getPath(), xmlFilePath.getPath()));
483 }
484 masterpatchWriter.close();
485 // Apply patches through masterpatch in levelFolderXML
486 res = XMLTools.patch(masterpatch, null);
487 log.logAppOutput(res, false);
488
489 // Create .oni files from XML
490 files.clear();
491 files.add(new File(levelFolderXML, "*.xml"));
492 res = OniSplit.convertXMLtoOni(levelFolder, files);
493 log.logAppOutput(res, true);
494
495 // Remove XML folder as import will only require .oni's
496 try {
497 FileUtils.deleteDirectory(levelFolderXML);
498 } catch (IOException e) {
499 e.printStackTrace();
500 }
501
502 oniLevelFolders.get(level).add(levelFolder);
503 }
504
505 log.println("Applying XML patches took "
506 + (new Date().getTime() - startMS) + " ms");
507 }
508
509 private static void combineBinaryFiles(
510 TreeMap<String, Vector<File>> oniLevelFolders,
511 InstallProgressListener listener, Logger log) {
512 long startMS = new Date().getTime();
513
514 int totalSteps = oniLevelFolders.size() + 1;
515 int stepsDone = 0;
516
517 log.println();
518 log.println("Importing levels");
519 for (String l : oniLevelFolders.keySet()) {
520 log.println("\tLevel " + l);
521 listener.installProgressUpdate(stepsDone, totalSteps,
522 "Installing level " + l);
523
524 ApplicationInvocationResult res = OniSplit.packLevel(
525 oniLevelFolders.get(l), new File(Paths.getEditionGDF(),
526 sanitizeLevelName(l) + ".dat"));
527 log.logAppOutput(res, true);
528
529 stepsDone++;
530 }
531
532 log.println("Importing levels took " + (new Date().getTime() - startMS)
533 + " ms");
534 log.println();
535 }
536
537 private static void copyVideos(Logger log) {
538 log.println();
539 if (SettingsManager.getInstance().get("copyintro", false)) {
540 File src = new File(Paths.getVanillaGDF(), "intro.bik");
541 log.println("Copying intro");
542 if (src.exists()) {
543 try {
544 FileUtils.copyFileToDirectory(src, Paths.getEditionGDF());
545 } catch (IOException e) {
546 e.printStackTrace();
547 }
548 }
549 } else {
550 log.println("NOT copying intro");
551 }
552 if (SettingsManager.getInstance().get("copyoutro", true)) {
553 File src = new File(Paths.getVanillaGDF(), "outro.bik");
554 log.println("Copying outro");
555 if (src.exists()) {
556 try {
557 FileUtils.copyFileToDirectory(src, Paths.getEditionGDF());
558 } catch (IOException e) {
559 e.printStackTrace();
560 }
561 }
562 } else {
563 log.println("NOT copying outro");
564 }
565 }
566
567 private static void unlockLevels(TreeSet<Integer> unlockLevels, Logger log) {
568 File dat = new File(Paths.getEditionBasePath(), "persist.dat");
569 log.println();
570 log.println("Unlocking levels: " + unlockLevels.toString());
571 if (!dat.exists()) {
572 InputStream is = AEInstaller2.class
573 .getResourceAsStream("/net/oni2/aeinstaller/resources/persist.dat");
574 try {
575 FileUtils.copyInputStreamToFile(is, dat);
576 } catch (IOException e) {
577 e.printStackTrace();
578 }
579 }
580 PersistDat save = new PersistDat(dat);
581 HashSet<Integer> currentlyUnlocked = save.getUnlockedLevels();
582 currentlyUnlocked.addAll(unlockLevels);
583 save.setUnlockedLevels(currentlyUnlocked);
584 save.close();
585 }
586
587 private static String sanitizeLevelName(String ln) {
588 int ind = ln.indexOf("_");
589 String res = ln.substring(0, ind + 1);
590 res += ln.substring(ind + 1, ind + 2).toUpperCase();
591 res += ln.substring(ind + 2);
592 return res;
593 }
594
595}
Note: See TracBrowser for help on using the repository browser.