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

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

AEI2:

  • Fixed BSL addon handling
File size: 16.8 KB
RevLine 
[596]1package net.oni2.aeinstaller.backend.oni;
2
3import java.io.File;
[597]4import java.io.FileFilter;
[600]5import java.io.FileNotFoundException;
[596]6import java.io.FilenameFilter;
7import java.io.IOException;
[600]8import java.io.PrintWriter;
9import java.text.SimpleDateFormat;
10import java.util.Date;
[608]11import java.util.HashMap;
[598]12import java.util.List;
[596]13import java.util.Scanner;
[606]14import java.util.TreeMap;
[598]15import java.util.TreeSet;
16import java.util.Vector;
[596]17
18import net.oni2.aeinstaller.backend.Paths;
[597]19import net.oni2.aeinstaller.backend.Settings;
20import net.oni2.aeinstaller.backend.Settings.Platform;
[608]21import net.oni2.aeinstaller.backend.mods.EBSLInstallType;
[598]22import net.oni2.aeinstaller.backend.mods.Mod;
[604]23import net.oni2.aeinstaller.backend.mods.ModManager;
[596]24
25import org.apache.commons.io.FileUtils;
26
27/**
28 * @author Christian Illy
29 */
30public class Installer {
[608]31 private static FileFilter dirFileFilter = new FileFilter() {
32 @Override
33 public boolean accept(File pathname) {
34 return pathname.isDirectory();
35 }
36 };
37
[596]38 /**
39 * @return Is Edition Core initialized
40 */
41 public static boolean isEditionInitialized() {
[597]42 return Paths.getVanillaOnisPath().exists();
[596]43 }
44
45 private static void createEmptyPath(File path) throws IOException {
46 if (path.exists())
47 FileUtils.deleteDirectory(path);
48 path.mkdirs();
49 }
50
[602]51 /**
[604]52 * @return list of currently installed mods
53 */
54 public static Vector<Integer> getInstalledMods() {
55 File installCfg = new File(Paths.getEditionGDF(), "installed_mods.xml");
56 return ModManager.getInstance().loadModSelection(installCfg);
57 }
58
59 /**
60 * @param tools
61 * Tools to install
62 */
63 public static void installTools(TreeSet<Mod> tools) {
64 for (Mod m : tools) {
65 File plain = new File(m.getLocalPath(), "plain");
66 if (plain.exists()) {
67 if (m.hasSeparatePlatformDirs()) {
68 File plainCommon = new File(plain, "common");
69 File plainMac = new File(plain, "mac_only");
70 File plainWin = new File(plain, "win_only");
71 if (plainCommon.exists())
72 copyToolsFiles(plainCommon);
73 if (Settings.getPlatform() == Platform.MACOS
74 && plainMac.exists())
75 copyToolsFiles(plainMac);
76 else if (plainWin.exists())
77 copyToolsFiles(plainWin);
78 } else {
79 copyToolsFiles(plain);
80 }
81 }
82 }
83 }
84
85 /**
86 * @param tools
87 * Tools to uninstall
88 */
89 public static void uninstallTools(TreeSet<Mod> tools) {
90 // TODO: implement this!
91 }
92
93 private static void copyToolsFiles(File srcFolder) {
94 for (File f : srcFolder.listFiles()) {
95 try {
96 if (f.isDirectory())
97 FileUtils.copyDirectoryToDirectory(f,
98 Paths.getEditionBasePath());
99 else
100 FileUtils
101 .copyFileToDirectory(f, Paths.getEditionBasePath());
102 } catch (IOException e) {
103 // TODO Auto-generated catch block
104 e.printStackTrace();
105 }
106 }
107 }
[608]108
[604]109 /**
[602]110 * Install the given set of mods
111 *
112 * @param mods
113 * Mods to install
114 * @param listener
115 * Listener for install progress updates
116 */
[600]117 public static void install(TreeSet<Mod> mods,
118 InstallProgressListener listener) {
[604]119 try {
120 createEmptyPath(Paths.getEditionGDF());
121 } catch (IOException e) {
122 e.printStackTrace();
123 }
124
125 File installCfg = new File(Paths.getEditionGDF(), "installed_mods.xml");
126 ModManager.getInstance().saveModSelection(installCfg, mods);
127
[608]128 Vector<File> foldersOni = new Vector<File>();
129 foldersOni.add(Paths.getVanillaOnisPath());
[598]130
131 for (Mod m : mods) {
132 File oni = new File(m.getLocalPath(), "oni");
133 if (oni.exists()) {
134 if (m.hasSeparatePlatformDirs()) {
135 File oniCommon = new File(oni, "common");
136 File oniMac = new File(oni, "mac_only");
137 File oniWin = new File(oni, "win_only");
138 if (oniCommon.exists())
[608]139 foldersOni.add(oniCommon);
[598]140 if (Settings.getPlatform() == Platform.MACOS
141 && oniMac.exists())
[608]142 foldersOni.add(oniMac);
[598]143 else if (oniWin.exists())
[608]144 foldersOni.add(oniWin);
[598]145 } else {
[608]146 foldersOni.add(oni);
[598]147 }
148 }
149 }
[608]150 combineBinaryFiles(foldersOni, listener);
151 combineBSLFolders(mods, listener);
152 }
[598]153
[608]154 private static void combineBSLFolders(TreeSet<Mod> mods,
155 InstallProgressListener listener) {
156 listener.installProgressUpdate(95, 100, "Installing BSL files");
[598]157
[608]158 HashMap<EBSLInstallType, Vector<Mod>> modsToInclude = new HashMap<EBSLInstallType, Vector<Mod>>();
159 modsToInclude.put(EBSLInstallType.NORMAL, new Vector<Mod>());
160 modsToInclude.put(EBSLInstallType.ADDON, new Vector<Mod>());
161
162 for (Mod m : mods.descendingSet()) {
163 File bsl = new File(m.getLocalPath(), "bsl");
164 if (bsl.exists()) {
165 if (m.hasSeparatePlatformDirs()) {
166 File bslCommon = new File(bsl, "common");
167 File bslMac = new File(bsl, "mac_only");
168 File bslWin = new File(bsl, "win_only");
169 if ((Settings.getPlatform() == Platform.MACOS && bslMac
170 .exists())
171 || ((Settings.getPlatform() == Platform.WIN || Settings
172 .getPlatform() == Platform.LINUX) && bslWin
173 .exists()) || bslCommon.exists()) {
174 modsToInclude.get(m.getBSLInstallType()).add(m);
175 }
176 } else {
177 modsToInclude.get(m.getBSLInstallType()).add(m);
178 }
179 }
180 }
181
182 for (Mod m : modsToInclude.get(EBSLInstallType.NORMAL)) {
183 copyBSL(m, false);
184 }
[610]185 Vector<Mod> addons = modsToInclude.get(EBSLInstallType.ADDON);
186 for (int i = addons.size() - 1; i >= 0; i--) {
187 copyBSL(addons.get(i), true);
[608]188 }
[598]189 }
190
[608]191 private static void copyBSL(Mod sourceMod, boolean addon) {
192 File targetBaseFolder = new File(Paths.getEditionGDF(), "IGMD");
193 if (!targetBaseFolder.exists())
194 targetBaseFolder.mkdir();
195
196 Vector<File> sources = new Vector<File>();
197 File bsl = new File(sourceMod.getLocalPath(), "bsl");
198 if (sourceMod.hasSeparatePlatformDirs()) {
199 File bslCommon = new File(bsl, "common");
200 File bslMac = new File(bsl, "mac_only");
201 File bslWin = new File(bsl, "win_only");
202 if (Settings.getPlatform() == Platform.MACOS && bslMac.exists()) {
203 for (File f : bslMac.listFiles(dirFileFilter)) {
204 File targetBSL = new File(targetBaseFolder, f.getName());
205 if (addon || !targetBSL.exists())
206 sources.add(f);
207 }
208 }
209 if ((Settings.getPlatform() == Platform.WIN || Settings
210 .getPlatform() == Platform.LINUX) && bslWin.exists()) {
211 for (File f : bslWin.listFiles(dirFileFilter)) {
212 File targetBSL = new File(targetBaseFolder, f.getName());
213 if (addon || !targetBSL.exists())
214 sources.add(f);
215 }
216 }
217 if (bslCommon.exists()) {
218 for (File f : bslCommon.listFiles(dirFileFilter)) {
219 File targetBSL = new File(targetBaseFolder, f.getName());
220 if (addon || !targetBSL.exists())
221 sources.add(f);
222 }
223 }
224 } else {
225 for (File f : bsl.listFiles(dirFileFilter)) {
226 File targetBSL = new File(targetBaseFolder, f.getName());
227 if (addon || !targetBSL.exists())
228 sources.add(f);
229 }
230 }
231
232 System.out.println("For mod: " + sourceMod.getName()
233 + " install BSL folders: " + sources.toString());
234 for (File f : sources) {
235 File targetPath = new File(targetBaseFolder, f.getName());
236 if (!targetPath.exists())
237 targetPath.mkdir();
238 for (File fbsl : f.listFiles()) {
239 File targetFile = new File(targetPath, fbsl.getName());
[610]240 if (addon || !targetFile.exists()) {
[608]241 try {
242 FileUtils.copyFile(fbsl, targetFile);
243 } catch (IOException e) {
244 // TODO Auto-generated catch block
245 e.printStackTrace();
246 }
247 }
248 }
249 }
250 }
251
[600]252 private static void combineBinaryFiles(List<File> srcFoldersFiles,
253 InstallProgressListener listener) {
[606]254 TreeMap<String, Vector<File>> levels = new TreeMap<String, Vector<File>>();
[598]255
[604]256 for (File path : srcFoldersFiles) {
257 for (File levelF : path.listFiles()) {
258 String fn = levelF.getName().toLowerCase();
259 String levelN = null;
260 if (levelF.isDirectory()) {
261 levelN = fn;
262 } else if (fn.endsWith(".dat")) {
263 levelN = fn.substring(0, fn.lastIndexOf('.'));
[598]264 }
[604]265 if (levelN != null) {
266 if (!levels.containsKey(levelN))
267 levels.put(levelN, new Vector<File>());
268 levels.get(levelN).add(levelF);
269 }
[598]270 }
[604]271 }
[598]272
[604]273 int totalSteps = 0;
274 int stepsDone = 0;
[600]275
[604]276 for (@SuppressWarnings("unused")
277 String s : levels.keySet())
278 totalSteps++;
[608]279 totalSteps++;
[600]280
[604]281 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
[600]282
[604]283 File logFile = new File(Paths.getInstallerPath(), "Installation.log");
284 PrintWriter log = null;
285 try {
286 log = new PrintWriter(logFile);
287 } catch (FileNotFoundException e) {
288 e.printStackTrace();
289 }
[600]290
[604]291 Date start = new Date();
292 log.println("Installation of mods started at " + sdf.format(start));
[600]293
[604]294 log.println("Importing levels");
295 for (String l : levels.keySet()) {
296 log.println("\tLevel " + l);
[600]297 listener.installProgressUpdate(stepsDone, totalSteps,
[604]298 "Installing level " + l);
299 for (File f : levels.get(l)) {
300 log.println("\t\t\t" + f.getPath());
301 }
[600]302
[604]303 Vector<String> res = OniSplit.packLevel(levels.get(l), new File(
304 Paths.getEditionGDF(), l + ".dat"));
305 if (res != null && res.size() > 0) {
306 for (String s : res)
307 log.println("\t\t" + s);
[598]308 }
[602]309
[604]310 log.println();
[608]311 stepsDone++;
[598]312 }
[604]313
314 Date end = new Date();
315 log.println("Initialization ended at " + sdf.format(end));
316 log.println("Process took "
317 + ((end.getTime() - start.getTime()) / 1000) + " seconds");
318 log.close();
[598]319 }
320
[596]321 /**
322 * Initializes the Edition core
[600]323 *
324 * @param listener
325 * Listener for status updates
[596]326 */
[600]327 public static void initializeEdition(InstallProgressListener listener) {
[596]328 File init = new File(Paths.getTempPath(), "init");
[600]329
330 int totalSteps = 0;
331 int stepsDone = 0;
332
333 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
334
335 for (@SuppressWarnings("unused")
336 File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() {
337 @Override
338 public boolean accept(File dir, String name) {
339 return name.endsWith(".dat");
340 }
341 })) {
342 totalSteps++;
343 }
344 totalSteps = totalSteps * 2 + 2;
345
[596]346 try {
[600]347 File logFile = new File(Paths.getInstallerPath(),
348 "Initialization.log");
349 PrintWriter log = new PrintWriter(logFile);
350
351 Date start = new Date();
352 log.println("Initialization of Edition core started at "
353 + sdf.format(start));
354 log.println("Cleaning directories");
355
356 listener.installProgressUpdate(stepsDone, totalSteps,
357 "Cleaning up directories");
[596]358 createEmptyPath(Paths.getVanillaOnisPath());
359 createEmptyPath(init);
[597]360 File level0Folder = new File(init, "level0_Final");
361 createEmptyPath(level0Folder);
[596]362
[600]363 stepsDone++;
364
365 log.println("Exporting levels and moving files to level0");
366
[596]367 for (File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() {
368 @Override
369 public boolean accept(File dir, String name) {
370 return name.endsWith(".dat");
371 }
372 })) {
373 String levelName = f.getName().substring(0,
374 f.getName().indexOf('.'));
375 Scanner fi = new Scanner(levelName);
[597]376 int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+"));
[596]377
[600]378 log.println("\t" + levelName + ":");
379 log.println("\t\tExporting");
380 listener.installProgressUpdate(stepsDone, totalSteps,
381 "Exporting vanilla level " + levelNumber);
382
[597]383 // Edition/GameDataFolder/level*_Final/
384 File tempLevelFolder = new File(init, levelName);
385
386 // Export Vanilla-Level-Dat -> Temp/Level
[600]387 Vector<String> res = OniSplit.export(tempLevelFolder, f);
388 if (res != null && res.size() > 0) {
389 for (String s : res)
390 log.println("\t\t\t" + s);
391 }
[597]392
[600]393 log.println("\t\tMoving files");
[597]394 handleFileGlobalisation(tempLevelFolder, level0Folder,
[600]395 levelNumber);
396 stepsDone++;
397 log.println();
[596]398 }
399
[600]400 log.println("Reimporting levels");
401
[597]402 for (File f : init.listFiles()) {
403 String levelName = f.getName();
404
[600]405 log.println("\t" + levelName);
406 listener.installProgressUpdate(stepsDone, totalSteps,
407 "Creating globalized " + levelName);
[597]408
[598]409 Vector<File> folders = new Vector<File>();
410 folders.add(f);
411
[600]412 Vector<String> res = OniSplit.importLevel(folders, new File(
413 Paths.getVanillaOnisPath(), levelName + ".dat"));
414 if (res != null && res.size() > 0) {
415 for (String s : res)
416 log.println("\t\t" + s);
417 }
418
419 log.println();
420 stepsDone++;
[597]421 }
422
[600]423 listener.installProgressUpdate(stepsDone, totalSteps,
424 "Copying basic files");
[597]425 // Copy Oni-configs
426 File persistVanilla = new File(Paths.getOniBasePath(),
427 "persist.dat");
428 File persistEdition = new File(Paths.getEditionBasePath(),
429 "persist.dat");
430 File keyConfVanilla = new File(Paths.getOniBasePath(),
431 "key_config.txt");
432 File keyConfEdition = new File(Paths.getEditionBasePath(),
433 "key_config.txt");
434 if (persistVanilla.exists() && !persistEdition.exists())
435 FileUtils.copyFile(persistVanilla, persistEdition);
436 if (keyConfVanilla.exists() && !keyConfEdition.exists())
437 FileUtils.copyFile(keyConfVanilla, keyConfEdition);
438
[603]439 FileUtils.deleteDirectory(init);
[604]440
[600]441 Date end = new Date();
442 log.println("Initialization ended at " + sdf.format(end));
443 log.println("Process took "
444 + ((end.getTime() - start.getTime()) / 1000) + " seconds");
445 log.close();
[596]446 } catch (IOException e) {
447 e.printStackTrace();
448 }
449 }
[597]450
451 private static void moveFileToTargetOrDelete(File source, File target) {
452 if (source.equals(target))
453 return;
454 if (!target.exists()) {
455 if (!source.renameTo(target)) {
456 System.err.println("File " + source.getPath() + " not moved!");
457 }
458 } else if (!source.delete()) {
459 System.err.println("File " + source.getPath() + " not deleted!");
460 }
461 }
462
463 private static void handleFileGlobalisation(File tempFolder,
[600]464 File level0Folder, int levelNumber) {
[597]465 // Move AKEV and related files to subfolder so they're not globalized:
466 if (levelNumber != 0) {
467 File akevFolder = new File(tempFolder, "AKEV");
468 akevFolder.mkdir();
469 OniSplit.move(akevFolder, tempFolder.getPath() + "/AKEV*.oni",
470 "overwrite");
471 }
472
473 for (File f : tempFolder.listFiles(new FileFilter() {
474 @Override
475 public boolean accept(File pathname) {
476 return pathname.isFile();
477 }
478 })) {
479 // Move matching files to subfolder NoGlobal:
480 if (f.getName().startsWith("TXMPfail")
481 || f.getName().startsWith("TXMPlevel")
482 || (f.getName().startsWith("TXMP") && f.getName().contains(
483 "intro"))
484 || f.getName().startsWith("TXMB")
485 || f.getName().equals("M3GMpowerup_lsi.oni")
486 || f.getName().equals("TXMPlsi_icon.oni")
487 || (f.getName().startsWith("TXMB") && f.getName().contains(
488 "splash_screen.oni"))) {
489 File noGlobal = new File(tempFolder, "NoGlobal");
490 noGlobal.mkdir();
491 File noGlobalFile = new File(noGlobal, f.getName());
492 moveFileToTargetOrDelete(f, noGlobalFile);
493 }
494 // Move matching files to level0_Animations/level0_TRAC
495 else if (f.getName().startsWith("TRAC")) {
496 File level0File = new File(level0Folder, f.getName());
497 moveFileToTargetOrDelete(f, level0File);
498 }
499 // Move matching files to level0_Animations/level0_TRAM
500 else if (f.getName().startsWith("TRAM")) {
501 File level0File = new File(level0Folder, f.getName());
502 moveFileToTargetOrDelete(f, level0File);
503 }
504 // Move matching files to level0_Textures
505 else if (f.getName().startsWith("ONSK")
506 || f.getName().startsWith("TXMP")) {
507 File level0File = new File(level0Folder, f.getName());
[598]508 moveFileToTargetOrDelete(f, level0File);
[597]509 }
510 // Move matching files to *VANILLA*/level0_Characters
511 else if (f.getName().startsWith("ONCC")
512 || f.getName().startsWith("TRBS")
513 || f.getName().startsWith("ONCV")
514 || f.getName().startsWith("ONVL")
515 || f.getName().startsWith("TRMA")
516 || f.getName().startsWith("TRSC")
517 || f.getName().startsWith("TRAS")) {
[598]518 File level0File = new File(level0Folder, f.getName());
519 moveFileToTargetOrDelete(f, level0File);
[597]520 }
521 // Move matching files to level0_Sounds
522 else if (f.getName().startsWith("OSBD")
523 || f.getName().startsWith("SNDD")) {
524 File level0File = new File(level0Folder, f.getName());
525 moveFileToTargetOrDelete(f, level0File);
526 }
527 // Move matching files to level0_Particles
528 else if (f.getName().startsWith("BINA3")
529 || f.getName().startsWith("M3GMdebris")
530 || f.getName().equals("M3GMtoxic_bubble.oni")
531 || f.getName().startsWith("M3GMelec")
532 || f.getName().startsWith("M3GMrat")
533 || f.getName().startsWith("M3GMjet")
534 || f.getName().startsWith("M3GMbomb_")
535 || f.getName().equals("M3GMbarab_swave.oni")
536 || f.getName().equals("M3GMbloodyfoot.oni")) {
537 File level0File = new File(level0Folder, f.getName());
538 moveFileToTargetOrDelete(f, level0File);
539 }
540 // Move matching files to Archive (aka delete them)
541 else if (f.getName().startsWith("AGDB")
542 || f.getName().startsWith("TRCM")) {
543 f.delete();
544 }
[599]545 // Move matching files to /level0_Final/
[597]546 else if (f.getName().startsWith("ONWC")) {
[598]547 File level0File = new File(level0Folder, f.getName());
548 moveFileToTargetOrDelete(f, level0File);
[597]549 }
550 }
551 }
[603]552
553 /**
[604]554 * Verify that the Edition is within a subfolder to vanilla Oni
555 * (..../Oni/Edition/AEInstaller)
556 *
[603]557 * @return true if GDF can be found in the parent's parent-path
558 */
559 public static boolean verifyRunningDirectory() {
[604]560 return Paths.getVanillaGDF().exists()
561 && Paths.getVanillaGDF().isDirectory();
[603]562 }
[596]563}
Note: See TracBrowser for help on using the repository browser.