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
Line 
1package net.oni2.aeinstaller.backend.oni;
2
3import java.io.File;
4import java.io.FileFilter;
5import java.io.FileNotFoundException;
6import java.io.FilenameFilter;
7import java.io.IOException;
8import java.io.PrintWriter;
9import java.text.SimpleDateFormat;
10import java.util.Date;
11import java.util.HashMap;
12import java.util.List;
13import java.util.Scanner;
14import java.util.TreeMap;
15import java.util.TreeSet;
16import java.util.Vector;
17
18import net.oni2.aeinstaller.backend.Paths;
19import net.oni2.aeinstaller.backend.Settings;
20import net.oni2.aeinstaller.backend.Settings.Platform;
21import net.oni2.aeinstaller.backend.mods.EBSLInstallType;
22import net.oni2.aeinstaller.backend.mods.Mod;
23import net.oni2.aeinstaller.backend.mods.ModManager;
24
25import org.apache.commons.io.FileUtils;
26
27/**
28 * @author Christian Illy
29 */
30public class Installer {
31 private static FileFilter dirFileFilter = new FileFilter() {
32 @Override
33 public boolean accept(File pathname) {
34 return pathname.isDirectory();
35 }
36 };
37
38 /**
39 * @return Is Edition Core initialized
40 */
41 public static boolean isEditionInitialized() {
42 return Paths.getVanillaOnisPath().exists();
43 }
44
45 private static void createEmptyPath(File path) throws IOException {
46 if (path.exists())
47 FileUtils.deleteDirectory(path);
48 path.mkdirs();
49 }
50
51 /**
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 }
107
108 /**
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 */
116 public static void install(TreeSet<Mod> mods,
117 InstallProgressListener listener) {
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
127 Vector<File> foldersOni = new Vector<File>();
128 foldersOni.add(Paths.getVanillaOnisPath());
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())
138 foldersOni.add(oniCommon);
139 if (Settings.getPlatform() == Platform.MACOS
140 && oniMac.exists())
141 foldersOni.add(oniMac);
142 else if (oniWin.exists())
143 foldersOni.add(oniWin);
144 } else {
145 foldersOni.add(oni);
146 }
147 }
148 }
149 combineBinaryFiles(foldersOni, listener);
150 combineBSLFolders(mods, listener);
151 }
152
153 private static void combineBSLFolders(TreeSet<Mod> mods,
154 InstallProgressListener listener) {
155 listener.installProgressUpdate(95, 100, "Installing BSL files");
156
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 }
184 Vector<Mod> addons = modsToInclude.get(EBSLInstallType.ADDON);
185 for (int i = addons.size() - 1; i >= 0; i--) {
186 copyBSL(addons.get(i), true);
187 }
188 }
189
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 (addon || !targetFile.exists()) {
240 try {
241 FileUtils.copyFile(fbsl, targetFile);
242 } catch (IOException e) {
243 e.printStackTrace();
244 }
245 }
246 }
247 }
248 }
249
250 private static void combineBinaryFiles(List<File> srcFoldersFiles,
251 InstallProgressListener listener) {
252 TreeMap<String, Vector<File>> levels = new TreeMap<String, Vector<File>>();
253
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('.'));
262 }
263 if (levelN != null) {
264 if (!levels.containsKey(levelN))
265 levels.put(levelN, new Vector<File>());
266 levels.get(levelN).add(levelF);
267 }
268 }
269 }
270
271 int totalSteps = 0;
272 int stepsDone = 0;
273
274 for (@SuppressWarnings("unused")
275 String s : levels.keySet())
276 totalSteps++;
277 totalSteps++;
278
279 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
280
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 }
288
289 Date start = new Date();
290 log.println("Installation of mods started at " + sdf.format(start));
291
292 log.println("Importing levels");
293 for (String l : levels.keySet()) {
294 log.println("\tLevel " + l);
295 listener.installProgressUpdate(stepsDone, totalSteps,
296 "Installing level " + l);
297 for (File f : levels.get(l)) {
298 log.println("\t\t\t" + f.getPath());
299 }
300
301 Vector<String> res = OniSplit.packLevel(levels.get(l), new File(
302 Paths.getEditionGDF(), sanitizeLevelName(l) + ".dat"));
303 if (res != null && res.size() > 0) {
304 for (String s : res)
305 log.println("\t\t" + s);
306 }
307
308 log.println();
309 stepsDone++;
310 }
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();
317 }
318
319 /**
320 * Initializes the Edition core
321 *
322 * @param listener
323 * Listener for status updates
324 */
325 public static void initializeEdition(InstallProgressListener listener) {
326 File init = new File(Paths.getTempPath(), "init");
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
344 try {
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");
356 createEmptyPath(Paths.getVanillaOnisPath());
357 createEmptyPath(init);
358 File level0Folder = new File(init, "level0_Final");
359 createEmptyPath(level0Folder);
360
361 stepsDone++;
362
363 log.println("Exporting levels and moving files to level0");
364
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);
374 int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+"));
375
376 log.println("\t" + levelName + ":");
377 log.println("\t\tExporting");
378 listener.installProgressUpdate(stepsDone, totalSteps,
379 "Exporting vanilla level " + levelNumber);
380
381 // Edition/GameDataFolder/level*_Final/
382 File tempLevelFolder = new File(init, levelName);
383
384 // Export Vanilla-Level-Dat -> Temp/Level
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 }
390
391 log.println("\t\tMoving files");
392 handleFileGlobalisation(tempLevelFolder, level0Folder,
393 levelNumber);
394 stepsDone++;
395 log.println();
396 }
397
398 log.println("Reimporting levels");
399
400 for (File f : init.listFiles()) {
401 String levelName = f.getName();
402
403 log.println("\t" + levelName);
404 listener.installProgressUpdate(stepsDone, totalSteps,
405 "Creating globalized " + levelName);
406
407 Vector<File> folders = new Vector<File>();
408 folders.add(f);
409
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++;
419 }
420
421 listener.installProgressUpdate(stepsDone, totalSteps,
422 "Copying basic files");
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
437 FileUtils.deleteDirectory(init);
438
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();
444 } catch (IOException e) {
445 e.printStackTrace();
446 }
447 }
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,
462 File level0Folder, int levelNumber) {
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());
506 moveFileToTargetOrDelete(f, level0File);
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")) {
516 File level0File = new File(level0Folder, f.getName());
517 moveFileToTargetOrDelete(f, level0File);
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 }
543 // Move matching files to /level0_Final/
544 else if (f.getName().startsWith("ONWC")) {
545 File level0File = new File(level0Folder, f.getName());
546 moveFileToTargetOrDelete(f, level0File);
547 }
548 }
549 }
550
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
559 /**
560 * Verify that the Edition is within a subfolder to vanilla Oni
561 * (..../Oni/Edition/AEInstaller)
562 *
563 * @return true if GDF can be found in the parent's parent-path
564 */
565 public static boolean verifyRunningDirectory() {
566 return Paths.getVanillaGDF().exists()
567 && Paths.getVanillaGDF().isDirectory();
568 }
569}
Note: See TracBrowser for help on using the repository browser.