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

Last change on this file since 605 was 604, checked in by alloc, 12 years ago

AEI2: Added load/save config

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