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

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

AEI2 0.99u:

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