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

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

AEI2:

  • Sanitizing level file names for MacOS (level*_Final.#)
File size: 17.0 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 e.printStackTrace();
104 }
105 }
106 }
[608]107
[604]108 /**
[602]109 * Install the given set of mods
110 *
111 * @param mods
112 * Mods to install
113 * @param listener
114 * Listener for install progress updates
115 */
[600]116 public static void install(TreeSet<Mod> mods,
117 InstallProgressListener listener) {
[604]118 try {
119 createEmptyPath(Paths.getEditionGDF());
120 } catch (IOException e) {
121 e.printStackTrace();
122 }
123
124 File installCfg = new File(Paths.getEditionGDF(), "installed_mods.xml");
125 ModManager.getInstance().saveModSelection(installCfg, mods);
126
[608]127 Vector<File> foldersOni = new Vector<File>();
128 foldersOni.add(Paths.getVanillaOnisPath());
[598]129
130 for (Mod m : mods) {
131 File oni = new File(m.getLocalPath(), "oni");
132 if (oni.exists()) {
133 if (m.hasSeparatePlatformDirs()) {
134 File oniCommon = new File(oni, "common");
135 File oniMac = new File(oni, "mac_only");
136 File oniWin = new File(oni, "win_only");
137 if (oniCommon.exists())
[608]138 foldersOni.add(oniCommon);
[598]139 if (Settings.getPlatform() == Platform.MACOS
140 && oniMac.exists())
[608]141 foldersOni.add(oniMac);
[598]142 else if (oniWin.exists())
[608]143 foldersOni.add(oniWin);
[598]144 } else {
[608]145 foldersOni.add(oni);
[598]146 }
147 }
148 }
[608]149 combineBinaryFiles(foldersOni, listener);
150 combineBSLFolders(mods, listener);
151 }
[598]152
[608]153 private static void combineBSLFolders(TreeSet<Mod> mods,
154 InstallProgressListener listener) {
155 listener.installProgressUpdate(95, 100, "Installing BSL files");
[598]156
[608]157 HashMap<EBSLInstallType, Vector<Mod>> modsToInclude = new HashMap<EBSLInstallType, Vector<Mod>>();
158 modsToInclude.put(EBSLInstallType.NORMAL, new Vector<Mod>());
159 modsToInclude.put(EBSLInstallType.ADDON, new Vector<Mod>());
160
161 for (Mod m : mods.descendingSet()) {
162 File bsl = new File(m.getLocalPath(), "bsl");
163 if (bsl.exists()) {
164 if (m.hasSeparatePlatformDirs()) {
165 File bslCommon = new File(bsl, "common");
166 File bslMac = new File(bsl, "mac_only");
167 File bslWin = new File(bsl, "win_only");
168 if ((Settings.getPlatform() == Platform.MACOS && bslMac
169 .exists())
170 || ((Settings.getPlatform() == Platform.WIN || Settings
171 .getPlatform() == Platform.LINUX) && bslWin
172 .exists()) || bslCommon.exists()) {
173 modsToInclude.get(m.getBSLInstallType()).add(m);
174 }
175 } else {
176 modsToInclude.get(m.getBSLInstallType()).add(m);
177 }
178 }
179 }
180
181 for (Mod m : modsToInclude.get(EBSLInstallType.NORMAL)) {
182 copyBSL(m, false);
183 }
[610]184 Vector<Mod> addons = modsToInclude.get(EBSLInstallType.ADDON);
185 for (int i = addons.size() - 1; i >= 0; i--) {
186 copyBSL(addons.get(i), true);
[608]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());
[610]239 if (addon || !targetFile.exists()) {
[608]240 try {
241 FileUtils.copyFile(fbsl, targetFile);
242 } catch (IOException e) {
243 e.printStackTrace();
244 }
245 }
246 }
247 }
248 }
249
[600]250 private static void combineBinaryFiles(List<File> srcFoldersFiles,
251 InstallProgressListener listener) {
[606]252 TreeMap<String, Vector<File>> levels = new TreeMap<String, Vector<File>>();
[598]253
[604]254 for (File path : srcFoldersFiles) {
255 for (File levelF : path.listFiles()) {
256 String fn = levelF.getName().toLowerCase();
257 String levelN = null;
258 if (levelF.isDirectory()) {
259 levelN = fn;
260 } else if (fn.endsWith(".dat")) {
261 levelN = fn.substring(0, fn.lastIndexOf('.'));
[598]262 }
[604]263 if (levelN != null) {
264 if (!levels.containsKey(levelN))
265 levels.put(levelN, new Vector<File>());
266 levels.get(levelN).add(levelF);
267 }
[598]268 }
[604]269 }
[598]270
[604]271 int totalSteps = 0;
272 int stepsDone = 0;
[600]273
[604]274 for (@SuppressWarnings("unused")
275 String s : levels.keySet())
276 totalSteps++;
[608]277 totalSteps++;
[600]278
[604]279 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
[600]280
[604]281 File logFile = new File(Paths.getInstallerPath(), "Installation.log");
282 PrintWriter log = null;
283 try {
284 log = new PrintWriter(logFile);
285 } catch (FileNotFoundException e) {
286 e.printStackTrace();
287 }
[600]288
[604]289 Date start = new Date();
290 log.println("Installation of mods started at " + sdf.format(start));
[600]291
[604]292 log.println("Importing levels");
293 for (String l : levels.keySet()) {
294 log.println("\tLevel " + l);
[600]295 listener.installProgressUpdate(stepsDone, totalSteps,
[604]296 "Installing level " + l);
297 for (File f : levels.get(l)) {
298 log.println("\t\t\t" + f.getPath());
299 }
[600]300
[604]301 Vector<String> res = OniSplit.packLevel(levels.get(l), new File(
[619]302 Paths.getEditionGDF(), sanitizeLevelName(l) + ".dat"));
[604]303 if (res != null && res.size() > 0) {
304 for (String s : res)
305 log.println("\t\t" + s);
[598]306 }
[602]307
[604]308 log.println();
[608]309 stepsDone++;
[598]310 }
[604]311
312 Date end = new Date();
313 log.println("Initialization ended at " + sdf.format(end));
314 log.println("Process took "
315 + ((end.getTime() - start.getTime()) / 1000) + " seconds");
316 log.close();
[598]317 }
318
[596]319 /**
320 * Initializes the Edition core
[600]321 *
322 * @param listener
323 * Listener for status updates
[596]324 */
[600]325 public static void initializeEdition(InstallProgressListener listener) {
[596]326 File init = new File(Paths.getTempPath(), "init");
[600]327
328 int totalSteps = 0;
329 int stepsDone = 0;
330
331 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
332
333 for (@SuppressWarnings("unused")
334 File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() {
335 @Override
336 public boolean accept(File dir, String name) {
337 return name.endsWith(".dat");
338 }
339 })) {
340 totalSteps++;
341 }
342 totalSteps = totalSteps * 2 + 2;
343
[596]344 try {
[600]345 File logFile = new File(Paths.getInstallerPath(),
346 "Initialization.log");
347 PrintWriter log = new PrintWriter(logFile);
348
349 Date start = new Date();
350 log.println("Initialization of Edition core started at "
351 + sdf.format(start));
352 log.println("Cleaning directories");
353
354 listener.installProgressUpdate(stepsDone, totalSteps,
355 "Cleaning up directories");
[596]356 createEmptyPath(Paths.getVanillaOnisPath());
357 createEmptyPath(init);
[597]358 File level0Folder = new File(init, "level0_Final");
359 createEmptyPath(level0Folder);
[596]360
[600]361 stepsDone++;
362
363 log.println("Exporting levels and moving files to level0");
364
[596]365 for (File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() {
366 @Override
367 public boolean accept(File dir, String name) {
368 return name.endsWith(".dat");
369 }
370 })) {
371 String levelName = f.getName().substring(0,
372 f.getName().indexOf('.'));
373 Scanner fi = new Scanner(levelName);
[597]374 int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+"));
[596]375
[600]376 log.println("\t" + levelName + ":");
377 log.println("\t\tExporting");
378 listener.installProgressUpdate(stepsDone, totalSteps,
379 "Exporting vanilla level " + levelNumber);
380
[597]381 // Edition/GameDataFolder/level*_Final/
382 File tempLevelFolder = new File(init, levelName);
383
384 // Export Vanilla-Level-Dat -> Temp/Level
[600]385 Vector<String> res = OniSplit.export(tempLevelFolder, f);
386 if (res != null && res.size() > 0) {
387 for (String s : res)
388 log.println("\t\t\t" + s);
389 }
[597]390
[600]391 log.println("\t\tMoving files");
[597]392 handleFileGlobalisation(tempLevelFolder, level0Folder,
[600]393 levelNumber);
394 stepsDone++;
395 log.println();
[596]396 }
397
[600]398 log.println("Reimporting levels");
399
[597]400 for (File f : init.listFiles()) {
401 String levelName = f.getName();
402
[600]403 log.println("\t" + levelName);
404 listener.installProgressUpdate(stepsDone, totalSteps,
405 "Creating globalized " + levelName);
[597]406
[598]407 Vector<File> folders = new Vector<File>();
408 folders.add(f);
409
[600]410 Vector<String> res = OniSplit.importLevel(folders, new File(
411 Paths.getVanillaOnisPath(), levelName + ".dat"));
412 if (res != null && res.size() > 0) {
413 for (String s : res)
414 log.println("\t\t" + s);
415 }
416
417 log.println();
418 stepsDone++;
[597]419 }
420
[600]421 listener.installProgressUpdate(stepsDone, totalSteps,
422 "Copying basic files");
[597]423 // Copy Oni-configs
424 File persistVanilla = new File(Paths.getOniBasePath(),
425 "persist.dat");
426 File persistEdition = new File(Paths.getEditionBasePath(),
427 "persist.dat");
428 File keyConfVanilla = new File(Paths.getOniBasePath(),
429 "key_config.txt");
430 File keyConfEdition = new File(Paths.getEditionBasePath(),
431 "key_config.txt");
432 if (persistVanilla.exists() && !persistEdition.exists())
433 FileUtils.copyFile(persistVanilla, persistEdition);
434 if (keyConfVanilla.exists() && !keyConfEdition.exists())
435 FileUtils.copyFile(keyConfVanilla, keyConfEdition);
436
[603]437 FileUtils.deleteDirectory(init);
[604]438
[600]439 Date end = new Date();
440 log.println("Initialization ended at " + sdf.format(end));
441 log.println("Process took "
442 + ((end.getTime() - start.getTime()) / 1000) + " seconds");
443 log.close();
[596]444 } catch (IOException e) {
445 e.printStackTrace();
446 }
447 }
[597]448
449 private static void moveFileToTargetOrDelete(File source, File target) {
450 if (source.equals(target))
451 return;
452 if (!target.exists()) {
453 if (!source.renameTo(target)) {
454 System.err.println("File " + source.getPath() + " not moved!");
455 }
456 } else if (!source.delete()) {
457 System.err.println("File " + source.getPath() + " not deleted!");
458 }
459 }
460
461 private static void handleFileGlobalisation(File tempFolder,
[600]462 File level0Folder, int levelNumber) {
[597]463 // Move AKEV and related files to subfolder so they're not globalized:
464 if (levelNumber != 0) {
465 File akevFolder = new File(tempFolder, "AKEV");
466 akevFolder.mkdir();
467 OniSplit.move(akevFolder, tempFolder.getPath() + "/AKEV*.oni",
468 "overwrite");
469 }
470
471 for (File f : tempFolder.listFiles(new FileFilter() {
472 @Override
473 public boolean accept(File pathname) {
474 return pathname.isFile();
475 }
476 })) {
477 // Move matching files to subfolder NoGlobal:
478 if (f.getName().startsWith("TXMPfail")
479 || f.getName().startsWith("TXMPlevel")
480 || (f.getName().startsWith("TXMP") && f.getName().contains(
481 "intro"))
482 || f.getName().startsWith("TXMB")
483 || f.getName().equals("M3GMpowerup_lsi.oni")
484 || f.getName().equals("TXMPlsi_icon.oni")
485 || (f.getName().startsWith("TXMB") && f.getName().contains(
486 "splash_screen.oni"))) {
487 File noGlobal = new File(tempFolder, "NoGlobal");
488 noGlobal.mkdir();
489 File noGlobalFile = new File(noGlobal, f.getName());
490 moveFileToTargetOrDelete(f, noGlobalFile);
491 }
492 // Move matching files to level0_Animations/level0_TRAC
493 else if (f.getName().startsWith("TRAC")) {
494 File level0File = new File(level0Folder, f.getName());
495 moveFileToTargetOrDelete(f, level0File);
496 }
497 // Move matching files to level0_Animations/level0_TRAM
498 else if (f.getName().startsWith("TRAM")) {
499 File level0File = new File(level0Folder, f.getName());
500 moveFileToTargetOrDelete(f, level0File);
501 }
502 // Move matching files to level0_Textures
503 else if (f.getName().startsWith("ONSK")
504 || f.getName().startsWith("TXMP")) {
505 File level0File = new File(level0Folder, f.getName());
[598]506 moveFileToTargetOrDelete(f, level0File);
[597]507 }
508 // Move matching files to *VANILLA*/level0_Characters
509 else if (f.getName().startsWith("ONCC")
510 || f.getName().startsWith("TRBS")
511 || f.getName().startsWith("ONCV")
512 || f.getName().startsWith("ONVL")
513 || f.getName().startsWith("TRMA")
514 || f.getName().startsWith("TRSC")
515 || f.getName().startsWith("TRAS")) {
[598]516 File level0File = new File(level0Folder, f.getName());
517 moveFileToTargetOrDelete(f, level0File);
[597]518 }
519 // Move matching files to level0_Sounds
520 else if (f.getName().startsWith("OSBD")
521 || f.getName().startsWith("SNDD")) {
522 File level0File = new File(level0Folder, f.getName());
523 moveFileToTargetOrDelete(f, level0File);
524 }
525 // Move matching files to level0_Particles
526 else if (f.getName().startsWith("BINA3")
527 || f.getName().startsWith("M3GMdebris")
528 || f.getName().equals("M3GMtoxic_bubble.oni")
529 || f.getName().startsWith("M3GMelec")
530 || f.getName().startsWith("M3GMrat")
531 || f.getName().startsWith("M3GMjet")
532 || f.getName().startsWith("M3GMbomb_")
533 || f.getName().equals("M3GMbarab_swave.oni")
534 || f.getName().equals("M3GMbloodyfoot.oni")) {
535 File level0File = new File(level0Folder, f.getName());
536 moveFileToTargetOrDelete(f, level0File);
537 }
538 // Move matching files to Archive (aka delete them)
539 else if (f.getName().startsWith("AGDB")
540 || f.getName().startsWith("TRCM")) {
541 f.delete();
542 }
[599]543 // Move matching files to /level0_Final/
[597]544 else if (f.getName().startsWith("ONWC")) {
[598]545 File level0File = new File(level0Folder, f.getName());
546 moveFileToTargetOrDelete(f, level0File);
[597]547 }
548 }
549 }
[603]550
[619]551 private static String sanitizeLevelName(String ln) {
552 int ind = ln.indexOf("_");
553 String res = ln.substring(0, ind + 1);
554 res += ln.substring(ind + 1, ind + 2).toUpperCase();
555 res += ln.substring(ind + 2);
556 return res;
557 }
558
[603]559 /**
[604]560 * Verify that the Edition is within a subfolder to vanilla Oni
561 * (..../Oni/Edition/AEInstaller)
562 *
[603]563 * @return true if GDF can be found in the parent's parent-path
564 */
565 public static boolean verifyRunningDirectory() {
[604]566 return Paths.getVanillaGDF().exists()
567 && Paths.getVanillaGDF().isDirectory();
[603]568 }
[596]569}
Note: See TracBrowser for help on using the repository browser.