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
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 // TODO Auto-generated catch block
104 e.printStackTrace();
105 }
106 }
107 }
108
109 /**
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 */
117 public static void install(TreeSet<Mod> mods,
118 InstallProgressListener listener) {
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
128 Vector<File> foldersOni = new Vector<File>();
129 foldersOni.add(Paths.getVanillaOnisPath());
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())
139 foldersOni.add(oniCommon);
140 if (Settings.getPlatform() == Platform.MACOS
141 && oniMac.exists())
142 foldersOni.add(oniMac);
143 else if (oniWin.exists())
144 foldersOni.add(oniWin);
145 } else {
146 foldersOni.add(oni);
147 }
148 }
149 }
150 combineBinaryFiles(foldersOni, listener);
151 combineBSLFolders(mods, listener);
152 }
153
154 private static void combineBSLFolders(TreeSet<Mod> mods,
155 InstallProgressListener listener) {
156 listener.installProgressUpdate(95, 100, "Installing BSL files");
157
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 }
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 (!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
251 private static void combineBinaryFiles(List<File> srcFoldersFiles,
252 InstallProgressListener listener) {
253 TreeMap<String, Vector<File>> levels = new TreeMap<String, Vector<File>>();
254
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('.'));
263 }
264 if (levelN != null) {
265 if (!levels.containsKey(levelN))
266 levels.put(levelN, new Vector<File>());
267 levels.get(levelN).add(levelF);
268 }
269 }
270 }
271
272 int totalSteps = 0;
273 int stepsDone = 0;
274
275 for (@SuppressWarnings("unused")
276 String s : levels.keySet())
277 totalSteps++;
278 totalSteps++;
279
280 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
281
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 }
289
290 Date start = new Date();
291 log.println("Installation of mods started at " + sdf.format(start));
292
293 log.println("Importing levels");
294 for (String l : levels.keySet()) {
295 log.println("\tLevel " + l);
296 listener.installProgressUpdate(stepsDone, totalSteps,
297 "Installing level " + l);
298 for (File f : levels.get(l)) {
299 log.println("\t\t\t" + f.getPath());
300 }
301
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);
307 }
308
309 log.println();
310 stepsDone++;
311 }
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();
318 }
319
320 /**
321 * Initializes the Edition core
322 *
323 * @param listener
324 * Listener for status updates
325 */
326 public static void initializeEdition(InstallProgressListener listener) {
327 File init = new File(Paths.getTempPath(), "init");
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
345 try {
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");
357 createEmptyPath(Paths.getVanillaOnisPath());
358 createEmptyPath(init);
359 File level0Folder = new File(init, "level0_Final");
360 createEmptyPath(level0Folder);
361
362 stepsDone++;
363
364 log.println("Exporting levels and moving files to level0");
365
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);
375 int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+"));
376
377 log.println("\t" + levelName + ":");
378 log.println("\t\tExporting");
379 listener.installProgressUpdate(stepsDone, totalSteps,
380 "Exporting vanilla level " + levelNumber);
381
382 // Edition/GameDataFolder/level*_Final/
383 File tempLevelFolder = new File(init, levelName);
384
385 // Export Vanilla-Level-Dat -> Temp/Level
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 }
391
392 log.println("\t\tMoving files");
393 handleFileGlobalisation(tempLevelFolder, level0Folder,
394 levelNumber);
395 stepsDone++;
396 log.println();
397 }
398
399 log.println("Reimporting levels");
400
401 for (File f : init.listFiles()) {
402 String levelName = f.getName();
403
404 log.println("\t" + levelName);
405 listener.installProgressUpdate(stepsDone, totalSteps,
406 "Creating globalized " + levelName);
407
408 Vector<File> folders = new Vector<File>();
409 folders.add(f);
410
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++;
420 }
421
422 listener.installProgressUpdate(stepsDone, totalSteps,
423 "Copying basic files");
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
438 FileUtils.deleteDirectory(init);
439
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();
445 } catch (IOException e) {
446 e.printStackTrace();
447 }
448 }
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,
463 File level0Folder, int levelNumber) {
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());
507 moveFileToTargetOrDelete(f, level0File);
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")) {
517 File level0File = new File(level0Folder, f.getName());
518 moveFileToTargetOrDelete(f, level0File);
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 }
544 // Move matching files to /level0_Final/
545 else if (f.getName().startsWith("ONWC")) {
546 File level0File = new File(level0Folder, f.getName());
547 moveFileToTargetOrDelete(f, level0File);
548 }
549 }
550 }
551
552 /**
553 * Verify that the Edition is within a subfolder to vanilla Oni
554 * (..../Oni/Edition/AEInstaller)
555 *
556 * @return true if GDF can be found in the parent's parent-path
557 */
558 public static boolean verifyRunningDirectory() {
559 return Paths.getVanillaGDF().exists()
560 && Paths.getVanillaGDF().isDirectory();
561 }
562}
Note: See TracBrowser for help on using the repository browser.