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

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

AEI2:

  • Added BSL handling to installation
  • Updated config-terms for dependencies/incompatibilities
  • Fixed mod counts for types
  • Open Edition folder through menu
  • Open folder of already downloaded mod through context menu
  • (Semi?)Fixed launching Oni on MacOS
  • Settings: Added checkbox for update notification
File size: 16.7 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 }
185 for (Mod m : modsToInclude.get(EBSLInstallType.ADDON)) {
186 copyBSL(m, true);
187 }
[598]188 }
189
[608]190 private static void copyBSL(Mod sourceMod, boolean addon) {
191 File targetBaseFolder = new File(Paths.getEditionGDF(), "IGMD");
192 if (!targetBaseFolder.exists())
193 targetBaseFolder.mkdir();
194
195 Vector<File> sources = new Vector<File>();
196 File bsl = new File(sourceMod.getLocalPath(), "bsl");
197 if (sourceMod.hasSeparatePlatformDirs()) {
198 File bslCommon = new File(bsl, "common");
199 File bslMac = new File(bsl, "mac_only");
200 File bslWin = new File(bsl, "win_only");
201 if (Settings.getPlatform() == Platform.MACOS && bslMac.exists()) {
202 for (File f : bslMac.listFiles(dirFileFilter)) {
203 File targetBSL = new File(targetBaseFolder, f.getName());
204 if (addon || !targetBSL.exists())
205 sources.add(f);
206 }
207 }
208 if ((Settings.getPlatform() == Platform.WIN || Settings
209 .getPlatform() == Platform.LINUX) && bslWin.exists()) {
210 for (File f : bslWin.listFiles(dirFileFilter)) {
211 File targetBSL = new File(targetBaseFolder, f.getName());
212 if (addon || !targetBSL.exists())
213 sources.add(f);
214 }
215 }
216 if (bslCommon.exists()) {
217 for (File f : bslCommon.listFiles(dirFileFilter)) {
218 File targetBSL = new File(targetBaseFolder, f.getName());
219 if (addon || !targetBSL.exists())
220 sources.add(f);
221 }
222 }
223 } else {
224 for (File f : bsl.listFiles(dirFileFilter)) {
225 File targetBSL = new File(targetBaseFolder, f.getName());
226 if (addon || !targetBSL.exists())
227 sources.add(f);
228 }
229 }
230
231 System.out.println("For mod: " + sourceMod.getName()
232 + " install BSL folders: " + sources.toString());
233 for (File f : sources) {
234 File targetPath = new File(targetBaseFolder, f.getName());
235 if (!targetPath.exists())
236 targetPath.mkdir();
237 for (File fbsl : f.listFiles()) {
238 File targetFile = new File(targetPath, fbsl.getName());
239 if (!targetFile.exists()) {
240 try {
241 FileUtils.copyFile(fbsl, targetFile);
242 } catch (IOException e) {
243 // TODO Auto-generated catch block
244 e.printStackTrace();
245 }
246 }
247 }
248 }
249 }
250
[600]251 private static void combineBinaryFiles(List<File> srcFoldersFiles,
252 InstallProgressListener listener) {
[606]253 TreeMap<String, Vector<File>> levels = new TreeMap<String, Vector<File>>();
[598]254
[604]255 for (File path : srcFoldersFiles) {
256 for (File levelF : path.listFiles()) {
257 String fn = levelF.getName().toLowerCase();
258 String levelN = null;
259 if (levelF.isDirectory()) {
260 levelN = fn;
261 } else if (fn.endsWith(".dat")) {
262 levelN = fn.substring(0, fn.lastIndexOf('.'));
[598]263 }
[604]264 if (levelN != null) {
265 if (!levels.containsKey(levelN))
266 levels.put(levelN, new Vector<File>());
267 levels.get(levelN).add(levelF);
268 }
[598]269 }
[604]270 }
[598]271
[604]272 int totalSteps = 0;
273 int stepsDone = 0;
[600]274
[604]275 for (@SuppressWarnings("unused")
276 String s : levels.keySet())
277 totalSteps++;
[608]278 totalSteps++;
[600]279
[604]280 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
[600]281
[604]282 File logFile = new File(Paths.getInstallerPath(), "Installation.log");
283 PrintWriter log = null;
284 try {
285 log = new PrintWriter(logFile);
286 } catch (FileNotFoundException e) {
287 e.printStackTrace();
288 }
[600]289
[604]290 Date start = new Date();
291 log.println("Installation of mods started at " + sdf.format(start));
[600]292
[604]293 log.println("Importing levels");
294 for (String l : levels.keySet()) {
295 log.println("\tLevel " + l);
[600]296 listener.installProgressUpdate(stepsDone, totalSteps,
[604]297 "Installing level " + l);
298 for (File f : levels.get(l)) {
299 log.println("\t\t\t" + f.getPath());
300 }
[600]301
[604]302 Vector<String> res = OniSplit.packLevel(levels.get(l), new File(
303 Paths.getEditionGDF(), l + ".dat"));
304 if (res != null && res.size() > 0) {
305 for (String s : res)
306 log.println("\t\t" + s);
[598]307 }
[602]308
[604]309 log.println();
[608]310 stepsDone++;
[598]311 }
[604]312
313 Date end = new Date();
314 log.println("Initialization ended at " + sdf.format(end));
315 log.println("Process took "
316 + ((end.getTime() - start.getTime()) / 1000) + " seconds");
317 log.close();
[598]318 }
319
[596]320 /**
321 * Initializes the Edition core
[600]322 *
323 * @param listener
324 * Listener for status updates
[596]325 */
[600]326 public static void initializeEdition(InstallProgressListener listener) {
[596]327 File init = new File(Paths.getTempPath(), "init");
[600]328
329 int totalSteps = 0;
330 int stepsDone = 0;
331
332 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
333
334 for (@SuppressWarnings("unused")
335 File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() {
336 @Override
337 public boolean accept(File dir, String name) {
338 return name.endsWith(".dat");
339 }
340 })) {
341 totalSteps++;
342 }
343 totalSteps = totalSteps * 2 + 2;
344
[596]345 try {
[600]346 File logFile = new File(Paths.getInstallerPath(),
347 "Initialization.log");
348 PrintWriter log = new PrintWriter(logFile);
349
350 Date start = new Date();
351 log.println("Initialization of Edition core started at "
352 + sdf.format(start));
353 log.println("Cleaning directories");
354
355 listener.installProgressUpdate(stepsDone, totalSteps,
356 "Cleaning up directories");
[596]357 createEmptyPath(Paths.getVanillaOnisPath());
358 createEmptyPath(init);
[597]359 File level0Folder = new File(init, "level0_Final");
360 createEmptyPath(level0Folder);
[596]361
[600]362 stepsDone++;
363
364 log.println("Exporting levels and moving files to level0");
365
[596]366 for (File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() {
367 @Override
368 public boolean accept(File dir, String name) {
369 return name.endsWith(".dat");
370 }
371 })) {
372 String levelName = f.getName().substring(0,
373 f.getName().indexOf('.'));
374 Scanner fi = new Scanner(levelName);
[597]375 int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+"));
[596]376
[600]377 log.println("\t" + levelName + ":");
378 log.println("\t\tExporting");
379 listener.installProgressUpdate(stepsDone, totalSteps,
380 "Exporting vanilla level " + levelNumber);
381
[597]382 // Edition/GameDataFolder/level*_Final/
383 File tempLevelFolder = new File(init, levelName);
384
385 // Export Vanilla-Level-Dat -> Temp/Level
[600]386 Vector<String> res = OniSplit.export(tempLevelFolder, f);
387 if (res != null && res.size() > 0) {
388 for (String s : res)
389 log.println("\t\t\t" + s);
390 }
[597]391
[600]392 log.println("\t\tMoving files");
[597]393 handleFileGlobalisation(tempLevelFolder, level0Folder,
[600]394 levelNumber);
395 stepsDone++;
396 log.println();
[596]397 }
398
[600]399 log.println("Reimporting levels");
400
[597]401 for (File f : init.listFiles()) {
402 String levelName = f.getName();
403
[600]404 log.println("\t" + levelName);
405 listener.installProgressUpdate(stepsDone, totalSteps,
406 "Creating globalized " + levelName);
[597]407
[598]408 Vector<File> folders = new Vector<File>();
409 folders.add(f);
410
[600]411 Vector<String> res = OniSplit.importLevel(folders, new File(
412 Paths.getVanillaOnisPath(), levelName + ".dat"));
413 if (res != null && res.size() > 0) {
414 for (String s : res)
415 log.println("\t\t" + s);
416 }
417
418 log.println();
419 stepsDone++;
[597]420 }
421
[600]422 listener.installProgressUpdate(stepsDone, totalSteps,
423 "Copying basic files");
[597]424 // Copy Oni-configs
425 File persistVanilla = new File(Paths.getOniBasePath(),
426 "persist.dat");
427 File persistEdition = new File(Paths.getEditionBasePath(),
428 "persist.dat");
429 File keyConfVanilla = new File(Paths.getOniBasePath(),
430 "key_config.txt");
431 File keyConfEdition = new File(Paths.getEditionBasePath(),
432 "key_config.txt");
433 if (persistVanilla.exists() && !persistEdition.exists())
434 FileUtils.copyFile(persistVanilla, persistEdition);
435 if (keyConfVanilla.exists() && !keyConfEdition.exists())
436 FileUtils.copyFile(keyConfVanilla, keyConfEdition);
437
[603]438 FileUtils.deleteDirectory(init);
[604]439
[600]440 Date end = new Date();
441 log.println("Initialization ended at " + sdf.format(end));
442 log.println("Process took "
443 + ((end.getTime() - start.getTime()) / 1000) + " seconds");
444 log.close();
[596]445 } catch (IOException e) {
446 e.printStackTrace();
447 }
448 }
[597]449
450 private static void moveFileToTargetOrDelete(File source, File target) {
451 if (source.equals(target))
452 return;
453 if (!target.exists()) {
454 if (!source.renameTo(target)) {
455 System.err.println("File " + source.getPath() + " not moved!");
456 }
457 } else if (!source.delete()) {
458 System.err.println("File " + source.getPath() + " not deleted!");
459 }
460 }
461
462 private static void handleFileGlobalisation(File tempFolder,
[600]463 File level0Folder, int levelNumber) {
[597]464 // Move AKEV and related files to subfolder so they're not globalized:
465 if (levelNumber != 0) {
466 File akevFolder = new File(tempFolder, "AKEV");
467 akevFolder.mkdir();
468 OniSplit.move(akevFolder, tempFolder.getPath() + "/AKEV*.oni",
469 "overwrite");
470 }
471
472 for (File f : tempFolder.listFiles(new FileFilter() {
473 @Override
474 public boolean accept(File pathname) {
475 return pathname.isFile();
476 }
477 })) {
478 // Move matching files to subfolder NoGlobal:
479 if (f.getName().startsWith("TXMPfail")
480 || f.getName().startsWith("TXMPlevel")
481 || (f.getName().startsWith("TXMP") && f.getName().contains(
482 "intro"))
483 || f.getName().startsWith("TXMB")
484 || f.getName().equals("M3GMpowerup_lsi.oni")
485 || f.getName().equals("TXMPlsi_icon.oni")
486 || (f.getName().startsWith("TXMB") && f.getName().contains(
487 "splash_screen.oni"))) {
488 File noGlobal = new File(tempFolder, "NoGlobal");
489 noGlobal.mkdir();
490 File noGlobalFile = new File(noGlobal, f.getName());
491 moveFileToTargetOrDelete(f, noGlobalFile);
492 }
493 // Move matching files to level0_Animations/level0_TRAC
494 else if (f.getName().startsWith("TRAC")) {
495 File level0File = new File(level0Folder, f.getName());
496 moveFileToTargetOrDelete(f, level0File);
497 }
498 // Move matching files to level0_Animations/level0_TRAM
499 else if (f.getName().startsWith("TRAM")) {
500 File level0File = new File(level0Folder, f.getName());
501 moveFileToTargetOrDelete(f, level0File);
502 }
503 // Move matching files to level0_Textures
504 else if (f.getName().startsWith("ONSK")
505 || f.getName().startsWith("TXMP")) {
506 File level0File = new File(level0Folder, f.getName());
[598]507 moveFileToTargetOrDelete(f, level0File);
[597]508 }
509 // Move matching files to *VANILLA*/level0_Characters
510 else if (f.getName().startsWith("ONCC")
511 || f.getName().startsWith("TRBS")
512 || f.getName().startsWith("ONCV")
513 || f.getName().startsWith("ONVL")
514 || f.getName().startsWith("TRMA")
515 || f.getName().startsWith("TRSC")
516 || f.getName().startsWith("TRAS")) {
[598]517 File level0File = new File(level0Folder, f.getName());
518 moveFileToTargetOrDelete(f, level0File);
[597]519 }
520 // Move matching files to level0_Sounds
521 else if (f.getName().startsWith("OSBD")
522 || f.getName().startsWith("SNDD")) {
523 File level0File = new File(level0Folder, f.getName());
524 moveFileToTargetOrDelete(f, level0File);
525 }
526 // Move matching files to level0_Particles
527 else if (f.getName().startsWith("BINA3")
528 || f.getName().startsWith("M3GMdebris")
529 || f.getName().equals("M3GMtoxic_bubble.oni")
530 || f.getName().startsWith("M3GMelec")
531 || f.getName().startsWith("M3GMrat")
532 || f.getName().startsWith("M3GMjet")
533 || f.getName().startsWith("M3GMbomb_")
534 || f.getName().equals("M3GMbarab_swave.oni")
535 || f.getName().equals("M3GMbloodyfoot.oni")) {
536 File level0File = new File(level0Folder, f.getName());
537 moveFileToTargetOrDelete(f, level0File);
538 }
539 // Move matching files to Archive (aka delete them)
540 else if (f.getName().startsWith("AGDB")
541 || f.getName().startsWith("TRCM")) {
542 f.delete();
543 }
[599]544 // Move matching files to /level0_Final/
[597]545 else if (f.getName().startsWith("ONWC")) {
[598]546 File level0File = new File(level0Folder, f.getName());
547 moveFileToTargetOrDelete(f, level0File);
[597]548 }
549 }
550 }
[603]551
552 /**
[604]553 * Verify that the Edition is within a subfolder to vanilla Oni
554 * (..../Oni/Edition/AEInstaller)
555 *
[603]556 * @return true if GDF can be found in the parent's parent-path
557 */
558 public static boolean verifyRunningDirectory() {
[604]559 return Paths.getVanillaGDF().exists()
560 && Paths.getVanillaGDF().isDirectory();
[603]561 }
[596]562}
Note: See TracBrowser for help on using the repository browser.