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

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

AEI2 0.99w:

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