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

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

AEI2: Added load/save config

File size: 13.2 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.TreeSet;
15import java.util.Vector;
16
17import net.oni2.aeinstaller.backend.Paths;
18import net.oni2.aeinstaller.backend.Settings;
19import net.oni2.aeinstaller.backend.Settings.Platform;
20import net.oni2.aeinstaller.backend.mods.Mod;
21import net.oni2.aeinstaller.backend.mods.ModManager;
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() {
33 return Paths.getVanillaOnisPath().exists();
34 }
35
36 private static void createEmptyPath(File path) throws IOException {
37 if (path.exists())
38 FileUtils.deleteDirectory(path);
39 path.mkdirs();
40 }
41
42 /**
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 /**
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 */
107 public static void install(TreeSet<Mod> mods,
108 InstallProgressListener listener) {
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
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
141 combineBinaryFiles(folders, listener);
142
143 // TODO: bsl()
144 }
145
146 private static void combineBinaryFiles(List<File> srcFoldersFiles,
147 InstallProgressListener listener) {
148 HashMap<String, Vector<File>> levels = new HashMap<String, Vector<File>>();
149
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('.'));
158 }
159 if (levelN != null) {
160 if (!levels.containsKey(levelN))
161 levels.put(levelN, new Vector<File>());
162 levels.get(levelN).add(levelF);
163 }
164 }
165 }
166
167 int totalSteps = 0;
168 int stepsDone = 0;
169
170 for (@SuppressWarnings("unused")
171 String s : levels.keySet())
172 totalSteps++;
173
174 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
175
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 }
183
184 Date start = new Date();
185 log.println("Installation of mods started at " + sdf.format(start));
186
187 log.println("Importing levels");
188 for (String l : levels.keySet()) {
189 log.println("\tLevel " + l);
190 listener.installProgressUpdate(stepsDone, totalSteps,
191 "Installing level " + l);
192 for (File f : levels.get(l)) {
193 log.println("\t\t\t" + f.getPath());
194 }
195
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);
201 }
202
203 log.println();
204 }
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();
211 }
212
213 /**
214 * Initializes the Edition core
215 *
216 * @param listener
217 * Listener for status updates
218 */
219 public static void initializeEdition(InstallProgressListener listener) {
220 File init = new File(Paths.getTempPath(), "init");
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
238 try {
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");
250 createEmptyPath(Paths.getVanillaOnisPath());
251 createEmptyPath(init);
252 File level0Folder = new File(init, "level0_Final");
253 createEmptyPath(level0Folder);
254
255 stepsDone++;
256
257 log.println("Exporting levels and moving files to level0");
258
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);
268 int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+"));
269
270 log.println("\t" + levelName + ":");
271 log.println("\t\tExporting");
272 listener.installProgressUpdate(stepsDone, totalSteps,
273 "Exporting vanilla level " + levelNumber);
274
275 // Edition/GameDataFolder/level*_Final/
276 File tempLevelFolder = new File(init, levelName);
277
278 // Export Vanilla-Level-Dat -> Temp/Level
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 }
284
285 log.println("\t\tMoving files");
286 handleFileGlobalisation(tempLevelFolder, level0Folder,
287 levelNumber);
288 stepsDone++;
289 log.println();
290 }
291
292 log.println("Reimporting levels");
293
294 for (File f : init.listFiles()) {
295 String levelName = f.getName();
296
297 log.println("\t" + levelName);
298 listener.installProgressUpdate(stepsDone, totalSteps,
299 "Creating globalized " + levelName);
300
301 Vector<File> folders = new Vector<File>();
302 folders.add(f);
303
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++;
313 }
314
315 listener.installProgressUpdate(stepsDone, totalSteps,
316 "Copying basic files");
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
331 FileUtils.deleteDirectory(init);
332
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();
338 } catch (IOException e) {
339 e.printStackTrace();
340 }
341 }
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,
356 File level0Folder, int levelNumber) {
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());
400 moveFileToTargetOrDelete(f, level0File);
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")) {
410 File level0File = new File(level0Folder, f.getName());
411 moveFileToTargetOrDelete(f, level0File);
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 }
437 // Move matching files to /level0_Final/
438 else if (f.getName().startsWith("ONWC")) {
439 File level0File = new File(level0Folder, f.getName());
440 moveFileToTargetOrDelete(f, level0File);
441 }
442 }
443 }
444
445 /**
446 * Verify that the Edition is within a subfolder to vanilla Oni
447 * (..../Oni/Edition/AEInstaller)
448 *
449 * @return true if GDF can be found in the parent's parent-path
450 */
451 public static boolean verifyRunningDirectory() {
452 return Paths.getVanillaGDF().exists()
453 && Paths.getVanillaGDF().isDirectory();
454 }
455}
Note: See TracBrowser for help on using the repository browser.