1 | package net.oni2.aeinstaller.backend.oni;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileFilter;
|
---|
5 | import java.io.FileInputStream;
|
---|
6 | import java.io.FileNotFoundException;
|
---|
7 | import java.io.FileOutputStream;
|
---|
8 | import java.io.FilenameFilter;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.io.PrintWriter;
|
---|
12 | import java.text.SimpleDateFormat;
|
---|
13 | import java.util.Date;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.HashSet;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Scanner;
|
---|
18 | import java.util.TreeMap;
|
---|
19 | import java.util.TreeSet;
|
---|
20 | import java.util.Vector;
|
---|
21 |
|
---|
22 | import net.oni2.aeinstaller.AEInstaller2;
|
---|
23 | import net.oni2.aeinstaller.backend.Paths;
|
---|
24 | import net.oni2.aeinstaller.backend.Settings;
|
---|
25 | import net.oni2.aeinstaller.backend.Settings.Platform;
|
---|
26 | import net.oni2.aeinstaller.backend.packages.EBSLInstallType;
|
---|
27 | import net.oni2.aeinstaller.backend.packages.Package;
|
---|
28 | import net.oni2.aeinstaller.backend.packages.PackageManager;
|
---|
29 |
|
---|
30 | import org.apache.commons.io.FileUtils;
|
---|
31 | import org.javabuilders.swing.SwingJavaBuilder;
|
---|
32 |
|
---|
33 | import com.thoughtworks.xstream.XStream;
|
---|
34 | import com.thoughtworks.xstream.io.xml.StaxDriver;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @author Christian Illy
|
---|
38 | */
|
---|
39 | public class Installer {
|
---|
40 | private static FileFilter dirFileFilter = new FileFilter() {
|
---|
41 | @Override
|
---|
42 | public boolean accept(File pathname) {
|
---|
43 | return pathname.isDirectory();
|
---|
44 | }
|
---|
45 | };
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * @return Is Edition Core initialized
|
---|
49 | */
|
---|
50 | public static boolean isEditionInitialized() {
|
---|
51 | return Paths.getVanillaOnisPath().exists();
|
---|
52 | }
|
---|
53 |
|
---|
54 | private static void createEmptyPath(File path) throws IOException {
|
---|
55 | if (path.exists())
|
---|
56 | FileUtils.deleteDirectory(path);
|
---|
57 | path.mkdirs();
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * @return list of currently installed mods
|
---|
62 | */
|
---|
63 | public static Vector<Integer> getInstalledMods() {
|
---|
64 | File installCfg = new File(Paths.getEditionGDF(), "installed_mods.xml");
|
---|
65 | return PackageManager.getInstance().loadModSelection(installCfg);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * @return Currently installed tools
|
---|
70 | */
|
---|
71 | @SuppressWarnings("unchecked")
|
---|
72 | public static TreeSet<Integer> getInstalledTools() {
|
---|
73 | File installCfg = new File(Paths.getInstallerPath(),
|
---|
74 | "installed_tools.xml");
|
---|
75 | TreeSet<Integer> res = new TreeSet<Integer>();
|
---|
76 | try {
|
---|
77 | if (installCfg.exists()) {
|
---|
78 | FileInputStream fis = new FileInputStream(installCfg);
|
---|
79 | XStream xs = new XStream(new StaxDriver());
|
---|
80 | Object obj = xs.fromXML(fis);
|
---|
81 | if (obj instanceof TreeSet<?>)
|
---|
82 | res = (TreeSet<Integer>) obj;
|
---|
83 | fis.close();
|
---|
84 | }
|
---|
85 | } catch (FileNotFoundException e) {
|
---|
86 | e.printStackTrace();
|
---|
87 | } catch (IOException e) {
|
---|
88 | e.printStackTrace();
|
---|
89 | }
|
---|
90 | return res;
|
---|
91 | }
|
---|
92 |
|
---|
93 | private static void writeInstalledTools(TreeSet<Integer> tools) {
|
---|
94 | File installCfg = new File(Paths.getInstallerPath(),
|
---|
95 | "installed_tools.xml");
|
---|
96 | try {
|
---|
97 | FileOutputStream fos = new FileOutputStream(installCfg);
|
---|
98 | XStream xs = new XStream(new StaxDriver());
|
---|
99 | xs.toXML(tools, fos);
|
---|
100 | fos.close();
|
---|
101 | } catch (FileNotFoundException e) {
|
---|
102 | e.printStackTrace();
|
---|
103 | } catch (IOException e) {
|
---|
104 | e.printStackTrace();
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * @param tools
|
---|
110 | * Tools to install
|
---|
111 | */
|
---|
112 | public static void installTools(TreeSet<Package> tools) {
|
---|
113 | TreeSet<Integer> installed = getInstalledTools();
|
---|
114 | for (Package m : tools) {
|
---|
115 | File plain = new File(m.getLocalPath(), "plain");
|
---|
116 | if (plain.exists()) {
|
---|
117 | if (m.hasSeparatePlatformDirs()) {
|
---|
118 | File plainCommon = new File(plain, "common");
|
---|
119 | File plainMac = new File(plain, "mac_only");
|
---|
120 | File plainWin = new File(plain, "win_only");
|
---|
121 | if (plainCommon.exists())
|
---|
122 | copyToolsFiles(plainCommon);
|
---|
123 | if (Settings.getPlatform() == Platform.MACOS
|
---|
124 | && plainMac.exists())
|
---|
125 | copyToolsFiles(plainMac);
|
---|
126 | else if (plainWin.exists())
|
---|
127 | copyToolsFiles(plainWin);
|
---|
128 | } else {
|
---|
129 | copyToolsFiles(plain);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | installed.add(m.getPackageNumber());
|
---|
133 | }
|
---|
134 | writeInstalledTools(installed);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * @param tools
|
---|
139 | * Tools to uninstall
|
---|
140 | */
|
---|
141 | public static void uninstallTools(TreeSet<Package> tools) {
|
---|
142 | TreeSet<Integer> installed = getInstalledTools();
|
---|
143 | for (Package m : tools) {
|
---|
144 | if (installed.contains(m.getPackageNumber())) {
|
---|
145 | File plain = new File(m.getLocalPath(), "plain");
|
---|
146 | if (plain.exists()) {
|
---|
147 | if (m.hasSeparatePlatformDirs()) {
|
---|
148 | File plainCommon = new File(plain, "common");
|
---|
149 | File plainMac = new File(plain, "mac_only");
|
---|
150 | File plainWin = new File(plain, "win_only");
|
---|
151 | if (plainCommon.exists())
|
---|
152 | removeToolsFiles(plainCommon,
|
---|
153 | Paths.getEditionBasePath());
|
---|
154 | if (Settings.getPlatform() == Platform.MACOS
|
---|
155 | && plainMac.exists())
|
---|
156 | removeToolsFiles(plainMac,
|
---|
157 | Paths.getEditionBasePath());
|
---|
158 | else if (plainWin.exists())
|
---|
159 | removeToolsFiles(plainWin,
|
---|
160 | Paths.getEditionBasePath());
|
---|
161 | } else {
|
---|
162 | removeToolsFiles(plain, Paths.getEditionBasePath());
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 | installed.remove(m.getPackageNumber());
|
---|
167 | }
|
---|
168 | writeInstalledTools(installed);
|
---|
169 | }
|
---|
170 |
|
---|
171 | private static void copyToolsFiles(File srcFolder) {
|
---|
172 | for (File f : srcFolder.listFiles()) {
|
---|
173 | try {
|
---|
174 | if (f.isDirectory())
|
---|
175 | FileUtils.copyDirectoryToDirectory(f,
|
---|
176 | Paths.getEditionBasePath());
|
---|
177 | else
|
---|
178 | FileUtils
|
---|
179 | .copyFileToDirectory(f, Paths.getEditionBasePath());
|
---|
180 | } catch (IOException e) {
|
---|
181 | e.printStackTrace();
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | private static void removeToolsFiles(File srcFolder, File target) {
|
---|
187 | for (File f : srcFolder.listFiles()) {
|
---|
188 | if (f.isDirectory())
|
---|
189 | removeToolsFiles(f, new File(target, f.getName()));
|
---|
190 | else {
|
---|
191 | File targetFile = new File(target, f.getName());
|
---|
192 | if (targetFile.exists())
|
---|
193 | targetFile.delete();
|
---|
194 | }
|
---|
195 | }
|
---|
196 | if (target.list().length == 0)
|
---|
197 | target.delete();
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Install the given set of mods
|
---|
202 | *
|
---|
203 | * @param mods
|
---|
204 | * Mods to install
|
---|
205 | * @param listener
|
---|
206 | * Listener for install progress updates
|
---|
207 | */
|
---|
208 | public static void install(TreeSet<Package> mods,
|
---|
209 | InstallProgressListener listener) {
|
---|
210 | File logFile = new File(Paths.getInstallerPath(), "Installation.log");
|
---|
211 | PrintWriter log = null;
|
---|
212 | try {
|
---|
213 | log = new PrintWriter(logFile);
|
---|
214 | } catch (FileNotFoundException e) {
|
---|
215 | e.printStackTrace();
|
---|
216 | }
|
---|
217 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
---|
218 | Date start = new Date();
|
---|
219 | log.println("Installation of mods started at " + sdf.format(start));
|
---|
220 |
|
---|
221 | log.println();
|
---|
222 | log.println("AEI2 version: "
|
---|
223 | + SwingJavaBuilder.getConfig().getResource("appversion"));
|
---|
224 | log.println("Installed tools:");
|
---|
225 | for (Package t : PackageManager.getInstance().getInstalledTools()) {
|
---|
226 | log.println(String.format(" - %s (%s)", t.getName(), t.getVersion()));
|
---|
227 | }
|
---|
228 | log.println("Installing mods:");
|
---|
229 | for (Package m : mods) {
|
---|
230 | log.println(String.format(" - %s (%s)", m.getName(), m.getVersion()));
|
---|
231 | }
|
---|
232 | log.println();
|
---|
233 |
|
---|
234 | Paths.getEditionGDF().mkdirs();
|
---|
235 | for (File f : Paths.getEditionGDF().listFiles(new FilenameFilter() {
|
---|
236 | public boolean accept(File arg0, String arg1) {
|
---|
237 | String s = arg1.toLowerCase();
|
---|
238 | return s.endsWith(".dat")
|
---|
239 | || s.endsWith(".raw")
|
---|
240 | || s.endsWith(".sep")
|
---|
241 | || (s.equals("intro.bik") && !Settings.getInstance()
|
---|
242 | .get("copyintro", false))
|
---|
243 | || (s.equals("outro.bik") && !Settings.getInstance()
|
---|
244 | .get("copyoutro", false));
|
---|
245 | }
|
---|
246 | })) {
|
---|
247 | f.delete();
|
---|
248 | }
|
---|
249 | File IGMD = new File(Paths.getEditionGDF(), "IGMD");
|
---|
250 | if (IGMD.exists()) {
|
---|
251 | for (File f : IGMD.listFiles(new FileFilter() {
|
---|
252 | @Override
|
---|
253 | public boolean accept(File pathname) {
|
---|
254 | return pathname.isDirectory();
|
---|
255 | }
|
---|
256 | })) {
|
---|
257 | File ignore = new File(f, "ignore.txt");
|
---|
258 | if (!ignore.exists()) {
|
---|
259 | try {
|
---|
260 | FileUtils.deleteDirectory(f);
|
---|
261 | } catch (IOException e) {
|
---|
262 | // TODO Auto-generated catch block
|
---|
263 | e.printStackTrace();
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | File installCfg = new File(Paths.getEditionGDF(), "installed_mods.xml");
|
---|
270 | PackageManager.getInstance().saveModSelection(installCfg, mods);
|
---|
271 |
|
---|
272 | TreeSet<Integer> unlockLevels = new TreeSet<Integer>();
|
---|
273 |
|
---|
274 | Vector<File> foldersOni = new Vector<File>();
|
---|
275 | foldersOni.add(Paths.getVanillaOnisPath());
|
---|
276 |
|
---|
277 | for (Package m : mods) {
|
---|
278 | for (int lev : m.getUnlockLevels())
|
---|
279 | unlockLevels.add(lev);
|
---|
280 |
|
---|
281 | File oni = new File(m.getLocalPath(), "oni");
|
---|
282 | if (oni.exists()) {
|
---|
283 | if (m.hasSeparatePlatformDirs()) {
|
---|
284 | File oniCommon = new File(oni, "common");
|
---|
285 | File oniMac = new File(oni, "mac_only");
|
---|
286 | File oniWin = new File(oni, "win_only");
|
---|
287 | if (oniCommon.exists())
|
---|
288 | foldersOni.add(oniCommon);
|
---|
289 | if (Settings.getPlatform() == Platform.MACOS
|
---|
290 | && oniMac.exists())
|
---|
291 | foldersOni.add(oniMac);
|
---|
292 | else if (oniWin.exists())
|
---|
293 | foldersOni.add(oniWin);
|
---|
294 | } else {
|
---|
295 | foldersOni.add(oni);
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | combineBinaryFiles(foldersOni, listener, log);
|
---|
301 | combineBSLFolders(mods, listener, log);
|
---|
302 |
|
---|
303 | copyVideos(log);
|
---|
304 |
|
---|
305 | if (unlockLevels.size() > 0) {
|
---|
306 | unlockLevels(unlockLevels, log);
|
---|
307 | }
|
---|
308 |
|
---|
309 | log.println();
|
---|
310 | Date end = new Date();
|
---|
311 | log.println("Initialization ended at " + sdf.format(end));
|
---|
312 | log.println("Process took "
|
---|
313 | + ((end.getTime() - start.getTime()) / 1000) + " seconds");
|
---|
314 | log.close();
|
---|
315 | }
|
---|
316 |
|
---|
317 | private static void combineBSLFolders(TreeSet<Package> mods,
|
---|
318 | InstallProgressListener listener, PrintWriter log) {
|
---|
319 | listener.installProgressUpdate(95, 100, "Installing BSL files");
|
---|
320 | log.println("Installing BSL files");
|
---|
321 |
|
---|
322 | HashMap<EBSLInstallType, Vector<Package>> modsToInclude = new HashMap<EBSLInstallType, Vector<Package>>();
|
---|
323 | modsToInclude.put(EBSLInstallType.NORMAL, new Vector<Package>());
|
---|
324 | modsToInclude.put(EBSLInstallType.ADDON, new Vector<Package>());
|
---|
325 |
|
---|
326 | for (Package m : mods.descendingSet()) {
|
---|
327 | File bsl = new File(m.getLocalPath(), "bsl");
|
---|
328 | if (bsl.exists()) {
|
---|
329 | if (m.hasSeparatePlatformDirs()) {
|
---|
330 | File bslCommon = new File(bsl, "common");
|
---|
331 | File bslMac = new File(bsl, "mac_only");
|
---|
332 | File bslWin = new File(bsl, "win_only");
|
---|
333 | if ((Settings.getPlatform() == Platform.MACOS && bslMac
|
---|
334 | .exists())
|
---|
335 | || ((Settings.getPlatform() == Platform.WIN || Settings
|
---|
336 | .getPlatform() == Platform.LINUX) && bslWin
|
---|
337 | .exists()) || bslCommon.exists()) {
|
---|
338 | modsToInclude.get(m.getBSLInstallType()).add(m);
|
---|
339 | }
|
---|
340 | } else {
|
---|
341 | modsToInclude.get(m.getBSLInstallType()).add(m);
|
---|
342 | }
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | for (Package m : modsToInclude.get(EBSLInstallType.NORMAL)) {
|
---|
347 | copyBSL(m, false);
|
---|
348 | }
|
---|
349 | Vector<Package> addons = modsToInclude.get(EBSLInstallType.ADDON);
|
---|
350 | for (int i = addons.size() - 1; i >= 0; i--) {
|
---|
351 | copyBSL(addons.get(i), true);
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | private static void copyBSL(Package sourceMod, boolean addon) {
|
---|
356 | File targetBaseFolder = new File(Paths.getEditionGDF(), "IGMD");
|
---|
357 | if (!targetBaseFolder.exists())
|
---|
358 | targetBaseFolder.mkdir();
|
---|
359 |
|
---|
360 | Vector<File> sources = new Vector<File>();
|
---|
361 | File bsl = new File(sourceMod.getLocalPath(), "bsl");
|
---|
362 | if (sourceMod.hasSeparatePlatformDirs()) {
|
---|
363 | File bslCommon = new File(bsl, "common");
|
---|
364 | File bslMac = new File(bsl, "mac_only");
|
---|
365 | File bslWin = new File(bsl, "win_only");
|
---|
366 | if (Settings.getPlatform() == Platform.MACOS && bslMac.exists()) {
|
---|
367 | for (File f : bslMac.listFiles(dirFileFilter)) {
|
---|
368 | File targetBSL = new File(targetBaseFolder, f.getName());
|
---|
369 | if (addon || !targetBSL.exists())
|
---|
370 | sources.add(f);
|
---|
371 | }
|
---|
372 | }
|
---|
373 | if ((Settings.getPlatform() == Platform.WIN || Settings
|
---|
374 | .getPlatform() == Platform.LINUX) && bslWin.exists()) {
|
---|
375 | for (File f : bslWin.listFiles(dirFileFilter)) {
|
---|
376 | File targetBSL = new File(targetBaseFolder, f.getName());
|
---|
377 | if (addon || !targetBSL.exists())
|
---|
378 | sources.add(f);
|
---|
379 | }
|
---|
380 | }
|
---|
381 | if (bslCommon.exists()) {
|
---|
382 | for (File f : bslCommon.listFiles(dirFileFilter)) {
|
---|
383 | File targetBSL = new File(targetBaseFolder, f.getName());
|
---|
384 | if (addon || !targetBSL.exists())
|
---|
385 | sources.add(f);
|
---|
386 | }
|
---|
387 | }
|
---|
388 | } else {
|
---|
389 | for (File f : bsl.listFiles(dirFileFilter)) {
|
---|
390 | File targetBSL = new File(targetBaseFolder, f.getName());
|
---|
391 | if (addon || !targetBSL.exists())
|
---|
392 | sources.add(f);
|
---|
393 | }
|
---|
394 | }
|
---|
395 |
|
---|
396 | System.out.println("For mod: " + sourceMod.getName()
|
---|
397 | + " install BSL folders: " + sources.toString());
|
---|
398 | for (File f : sources) {
|
---|
399 | File targetPath = new File(targetBaseFolder, f.getName());
|
---|
400 | if (!targetPath.exists())
|
---|
401 | targetPath.mkdir();
|
---|
402 | if (!(new File(targetPath, "ignore.txt").exists())) {
|
---|
403 | for (File fbsl : f.listFiles()) {
|
---|
404 | if (fbsl.getName().toLowerCase().endsWith(".bsl")) {
|
---|
405 | File targetFile = new File(targetPath, fbsl.getName());
|
---|
406 | if (addon || !targetFile.exists()) {
|
---|
407 | try {
|
---|
408 | FileUtils.copyFile(fbsl, targetFile);
|
---|
409 | } catch (IOException e) {
|
---|
410 | e.printStackTrace();
|
---|
411 | }
|
---|
412 | }
|
---|
413 | }
|
---|
414 | }
|
---|
415 | }
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | private static void combineBinaryFiles(List<File> srcFoldersFiles,
|
---|
420 | InstallProgressListener listener, PrintWriter log) {
|
---|
421 | TreeMap<String, Vector<File>> levels = new TreeMap<String, Vector<File>>();
|
---|
422 |
|
---|
423 | for (File path : srcFoldersFiles) {
|
---|
424 | for (File levelF : path.listFiles()) {
|
---|
425 | String fn = levelF.getName().toLowerCase();
|
---|
426 | String levelN = null;
|
---|
427 | if (levelF.isDirectory()) {
|
---|
428 | levelN = fn;
|
---|
429 | } else if (fn.endsWith(".dat")) {
|
---|
430 | levelN = fn.substring(0, fn.lastIndexOf('.'));
|
---|
431 | }
|
---|
432 | if (levelN != null) {
|
---|
433 | if (!levels.containsKey(levelN))
|
---|
434 | levels.put(levelN, new Vector<File>());
|
---|
435 | levels.get(levelN).add(levelF);
|
---|
436 | }
|
---|
437 | }
|
---|
438 | }
|
---|
439 |
|
---|
440 | int totalSteps = 0;
|
---|
441 | int stepsDone = 0;
|
---|
442 |
|
---|
443 | for (@SuppressWarnings("unused")
|
---|
444 | String s : levels.keySet())
|
---|
445 | totalSteps++;
|
---|
446 | totalSteps++;
|
---|
447 |
|
---|
448 | log.println("Importing levels");
|
---|
449 | for (String l : levels.keySet()) {
|
---|
450 | log.println("\tLevel " + l);
|
---|
451 | listener.installProgressUpdate(stepsDone, totalSteps,
|
---|
452 | "Installing level " + l);
|
---|
453 | for (File f : levels.get(l)) {
|
---|
454 | log.println("\t\t\t" + f.getPath());
|
---|
455 | }
|
---|
456 |
|
---|
457 | Vector<String> res = OniSplit.packLevel(levels.get(l), new File(
|
---|
458 | Paths.getEditionGDF(), sanitizeLevelName(l) + ".dat"));
|
---|
459 | if (res != null && res.size() > 0) {
|
---|
460 | for (String s : res)
|
---|
461 | log.println("\t\t" + s);
|
---|
462 | }
|
---|
463 |
|
---|
464 | log.println();
|
---|
465 | stepsDone++;
|
---|
466 | }
|
---|
467 | }
|
---|
468 |
|
---|
469 | private static void copyVideos(PrintWriter log) {
|
---|
470 | if (Settings.getInstance().get("copyintro", false)) {
|
---|
471 | File src = new File(Paths.getVanillaGDF(), "intro.bik");
|
---|
472 | log.println("Copying intro");
|
---|
473 | if (src.exists()) {
|
---|
474 | try {
|
---|
475 | FileUtils.copyFileToDirectory(src, Paths.getEditionGDF());
|
---|
476 | } catch (IOException e) {
|
---|
477 | e.printStackTrace();
|
---|
478 | }
|
---|
479 | }
|
---|
480 | }
|
---|
481 | if (Settings.getInstance().get("copyoutro", true)) {
|
---|
482 | File src = new File(Paths.getVanillaGDF(), "outro.bik");
|
---|
483 | log.println("Copying outro");
|
---|
484 | if (src.exists()) {
|
---|
485 | try {
|
---|
486 | FileUtils.copyFileToDirectory(src, Paths.getEditionGDF());
|
---|
487 | } catch (IOException e) {
|
---|
488 | e.printStackTrace();
|
---|
489 | }
|
---|
490 | }
|
---|
491 | }
|
---|
492 | }
|
---|
493 |
|
---|
494 | private static void unlockLevels(TreeSet<Integer> unlockLevels,
|
---|
495 | PrintWriter log) {
|
---|
496 | File dat = new File(Paths.getEditionBasePath(), "persist.dat");
|
---|
497 | log.println("Unlocking levels: " + unlockLevels.toString());
|
---|
498 | if (!dat.exists()) {
|
---|
499 | InputStream is = AEInstaller2.class
|
---|
500 | .getResourceAsStream("/net/oni2/aeinstaller/resources/persist.dat");
|
---|
501 | try {
|
---|
502 | FileUtils.copyInputStreamToFile(is, dat);
|
---|
503 | } catch (IOException e) {
|
---|
504 | // TODO Auto-generated catch block
|
---|
505 | e.printStackTrace();
|
---|
506 | }
|
---|
507 | }
|
---|
508 | PersistDat save = new PersistDat(dat);
|
---|
509 | HashSet<Integer> currentlyUnlocked = save.getUnlockedLevels();
|
---|
510 | currentlyUnlocked.addAll(unlockLevels);
|
---|
511 | save.setUnlockedLevels(currentlyUnlocked);
|
---|
512 | save.close();
|
---|
513 | }
|
---|
514 |
|
---|
515 | /**
|
---|
516 | * Initializes the Edition core
|
---|
517 | *
|
---|
518 | * @param listener
|
---|
519 | * Listener for status updates
|
---|
520 | */
|
---|
521 | public static void initializeEdition(InstallProgressListener listener) {
|
---|
522 | File init = new File(Paths.getTempPath(), "init");
|
---|
523 |
|
---|
524 | int totalSteps = 0;
|
---|
525 | int stepsDone = 0;
|
---|
526 |
|
---|
527 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
---|
528 |
|
---|
529 | for (@SuppressWarnings("unused")
|
---|
530 | File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() {
|
---|
531 | @Override
|
---|
532 | public boolean accept(File dir, String name) {
|
---|
533 | return name.endsWith(".dat");
|
---|
534 | }
|
---|
535 | })) {
|
---|
536 | totalSteps++;
|
---|
537 | }
|
---|
538 | totalSteps = totalSteps * 2 + 2;
|
---|
539 |
|
---|
540 | try {
|
---|
541 | File logFile = new File(Paths.getInstallerPath(),
|
---|
542 | "Initialization.log");
|
---|
543 | PrintWriter log = new PrintWriter(logFile);
|
---|
544 |
|
---|
545 | Date start = new Date();
|
---|
546 | log.println("Initialization of Edition core started at "
|
---|
547 | + sdf.format(start));
|
---|
548 | log.println("Cleaning directories");
|
---|
549 |
|
---|
550 | listener.installProgressUpdate(stepsDone, totalSteps,
|
---|
551 | "Cleaning up directories");
|
---|
552 | createEmptyPath(Paths.getVanillaOnisPath());
|
---|
553 | createEmptyPath(init);
|
---|
554 | File level0Folder = new File(init, "level0_Final");
|
---|
555 | createEmptyPath(level0Folder);
|
---|
556 |
|
---|
557 | stepsDone++;
|
---|
558 |
|
---|
559 | log.println("Exporting levels and moving files to level0");
|
---|
560 |
|
---|
561 | for (File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() {
|
---|
562 | @Override
|
---|
563 | public boolean accept(File dir, String name) {
|
---|
564 | return name.endsWith(".dat");
|
---|
565 | }
|
---|
566 | })) {
|
---|
567 | String levelName = f.getName().substring(0,
|
---|
568 | f.getName().indexOf('.'));
|
---|
569 | Scanner fi = new Scanner(levelName);
|
---|
570 | int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+"));
|
---|
571 |
|
---|
572 | log.println("\t" + levelName + ":");
|
---|
573 | log.println("\t\tExporting");
|
---|
574 | listener.installProgressUpdate(stepsDone, totalSteps,
|
---|
575 | "Exporting vanilla level " + levelNumber);
|
---|
576 |
|
---|
577 | // Edition/GameDataFolder/level*_Final/
|
---|
578 | File tempLevelFolder = new File(init, levelName);
|
---|
579 |
|
---|
580 | // Export Vanilla-Level-Dat -> Temp/Level
|
---|
581 | Vector<String> res = OniSplit.export(tempLevelFolder, f);
|
---|
582 | if (res != null && res.size() > 0) {
|
---|
583 | for (String s : res)
|
---|
584 | log.println("\t\t\t" + s);
|
---|
585 | }
|
---|
586 |
|
---|
587 | log.println("\t\tMoving files");
|
---|
588 | handleFileGlobalisation(tempLevelFolder, level0Folder,
|
---|
589 | levelNumber);
|
---|
590 | stepsDone++;
|
---|
591 | log.println();
|
---|
592 | }
|
---|
593 |
|
---|
594 | log.println("Reimporting levels");
|
---|
595 |
|
---|
596 | for (File f : init.listFiles()) {
|
---|
597 | String levelName = f.getName();
|
---|
598 |
|
---|
599 | log.println("\t" + levelName);
|
---|
600 | listener.installProgressUpdate(stepsDone, totalSteps,
|
---|
601 | "Creating globalized " + levelName);
|
---|
602 |
|
---|
603 | Vector<File> folders = new Vector<File>();
|
---|
604 | folders.add(f);
|
---|
605 |
|
---|
606 | Vector<String> res = OniSplit.importLevel(folders, new File(
|
---|
607 | Paths.getVanillaOnisPath(), levelName + ".dat"));
|
---|
608 | if (res != null && res.size() > 0) {
|
---|
609 | for (String s : res)
|
---|
610 | log.println("\t\t" + s);
|
---|
611 | }
|
---|
612 |
|
---|
613 | log.println();
|
---|
614 | stepsDone++;
|
---|
615 | }
|
---|
616 |
|
---|
617 | listener.installProgressUpdate(stepsDone, totalSteps,
|
---|
618 | "Copying basic files");
|
---|
619 | // Copy Oni-configs
|
---|
620 | File persistVanilla = new File(Paths.getOniBasePath(),
|
---|
621 | "persist.dat");
|
---|
622 | File persistEdition = new File(Paths.getEditionBasePath(),
|
---|
623 | "persist.dat");
|
---|
624 | File keyConfVanilla = new File(Paths.getOniBasePath(),
|
---|
625 | "key_config.txt");
|
---|
626 | File keyConfEdition = new File(Paths.getEditionBasePath(),
|
---|
627 | "key_config.txt");
|
---|
628 | if (persistVanilla.exists() && !persistEdition.exists())
|
---|
629 | FileUtils.copyFile(persistVanilla, persistEdition);
|
---|
630 | if (keyConfVanilla.exists() && !keyConfEdition.exists())
|
---|
631 | FileUtils.copyFile(keyConfVanilla, keyConfEdition);
|
---|
632 |
|
---|
633 | FileUtils.deleteDirectory(init);
|
---|
634 |
|
---|
635 | Date end = new Date();
|
---|
636 | log.println("Initialization ended at " + sdf.format(end));
|
---|
637 | log.println("Process took "
|
---|
638 | + ((end.getTime() - start.getTime()) / 1000) + " seconds");
|
---|
639 | log.close();
|
---|
640 | } catch (IOException e) {
|
---|
641 | e.printStackTrace();
|
---|
642 | }
|
---|
643 | }
|
---|
644 |
|
---|
645 | private static void moveFileToTargetOrDelete(File source, File target) {
|
---|
646 | if (source.equals(target))
|
---|
647 | return;
|
---|
648 | if (!target.exists()) {
|
---|
649 | if (!source.renameTo(target)) {
|
---|
650 | System.err.println("File " + source.getPath() + " not moved!");
|
---|
651 | }
|
---|
652 | } else if (!source.delete()) {
|
---|
653 | System.err.println("File " + source.getPath() + " not deleted!");
|
---|
654 | }
|
---|
655 | }
|
---|
656 |
|
---|
657 | private static void handleFileGlobalisation(File tempFolder,
|
---|
658 | File level0Folder, int levelNumber) {
|
---|
659 | // Move AKEV and related files to subfolder so they're not globalized:
|
---|
660 | if (levelNumber != 0) {
|
---|
661 | File akevFolder = new File(tempFolder, "AKEV");
|
---|
662 | akevFolder.mkdir();
|
---|
663 | OniSplit.move(akevFolder, tempFolder.getPath() + "/AKEV*.oni",
|
---|
664 | "overwrite");
|
---|
665 | }
|
---|
666 |
|
---|
667 | for (File f : tempFolder.listFiles(new FileFilter() {
|
---|
668 | @Override
|
---|
669 | public boolean accept(File pathname) {
|
---|
670 | return pathname.isFile();
|
---|
671 | }
|
---|
672 | })) {
|
---|
673 | // Move matching files to subfolder NoGlobal:
|
---|
674 | if (f.getName().startsWith("TXMPfail")
|
---|
675 | || f.getName().startsWith("TXMPlevel")
|
---|
676 | || (f.getName().startsWith("TXMP") && f.getName().contains(
|
---|
677 | "intro"))
|
---|
678 | || f.getName().startsWith("TXMB")
|
---|
679 | || f.getName().equals("M3GMpowerup_lsi.oni")
|
---|
680 | || f.getName().equals("TXMPlsi_icon.oni")
|
---|
681 | || (f.getName().startsWith("TXMB") && f.getName().contains(
|
---|
682 | "splash_screen.oni"))) {
|
---|
683 | File noGlobal = new File(tempFolder, "NoGlobal");
|
---|
684 | noGlobal.mkdir();
|
---|
685 | File noGlobalFile = new File(noGlobal, f.getName());
|
---|
686 | moveFileToTargetOrDelete(f, noGlobalFile);
|
---|
687 | }
|
---|
688 | // Move matching files to level0_Animations/level0_TRAC
|
---|
689 | else if (f.getName().startsWith("TRAC")) {
|
---|
690 | File level0File = new File(level0Folder, f.getName());
|
---|
691 | moveFileToTargetOrDelete(f, level0File);
|
---|
692 | }
|
---|
693 | // Move matching files to level0_Animations/level0_TRAM
|
---|
694 | else if (f.getName().startsWith("TRAM")) {
|
---|
695 | File level0File = new File(level0Folder, f.getName());
|
---|
696 | moveFileToTargetOrDelete(f, level0File);
|
---|
697 | }
|
---|
698 | // Move matching files to level0_Textures
|
---|
699 | else if (f.getName().startsWith("ONSK")
|
---|
700 | || f.getName().startsWith("TXMP")) {
|
---|
701 | File level0File = new File(level0Folder, f.getName());
|
---|
702 | moveFileToTargetOrDelete(f, level0File);
|
---|
703 | }
|
---|
704 | // Move matching files to *VANILLA*/level0_Characters
|
---|
705 | else if (f.getName().startsWith("ONCC")
|
---|
706 | || f.getName().startsWith("TRBS")
|
---|
707 | || f.getName().startsWith("ONCV")
|
---|
708 | || f.getName().startsWith("ONVL")
|
---|
709 | || f.getName().startsWith("TRMA")
|
---|
710 | || f.getName().startsWith("TRSC")
|
---|
711 | || f.getName().startsWith("TRAS")) {
|
---|
712 | File level0File = new File(level0Folder, f.getName());
|
---|
713 | moveFileToTargetOrDelete(f, level0File);
|
---|
714 | }
|
---|
715 | // Move matching files to level0_Sounds
|
---|
716 | else if (f.getName().startsWith("OSBD")
|
---|
717 | || f.getName().startsWith("SNDD")) {
|
---|
718 | File level0File = new File(level0Folder, f.getName());
|
---|
719 | moveFileToTargetOrDelete(f, level0File);
|
---|
720 | }
|
---|
721 | // Move matching files to level0_Particles
|
---|
722 | else if (f.getName().startsWith("BINA3")
|
---|
723 | || f.getName().startsWith("M3GMdebris")
|
---|
724 | || f.getName().equals("M3GMtoxic_bubble.oni")
|
---|
725 | || f.getName().startsWith("M3GMelec")
|
---|
726 | || f.getName().startsWith("M3GMrat")
|
---|
727 | || f.getName().startsWith("M3GMjet")
|
---|
728 | || f.getName().startsWith("M3GMbomb_")
|
---|
729 | || f.getName().equals("M3GMbarab_swave.oni")
|
---|
730 | || f.getName().equals("M3GMbloodyfoot.oni")) {
|
---|
731 | File level0File = new File(level0Folder, f.getName());
|
---|
732 | moveFileToTargetOrDelete(f, level0File);
|
---|
733 | }
|
---|
734 | // Move matching files to Archive (aka delete them)
|
---|
735 | else if (f.getName().startsWith("AGDB")
|
---|
736 | || f.getName().startsWith("TRCM")) {
|
---|
737 | f.delete();
|
---|
738 | }
|
---|
739 | // Move matching files to /level0_Final/
|
---|
740 | else if (f.getName().startsWith("ONWC")) {
|
---|
741 | File level0File = new File(level0Folder, f.getName());
|
---|
742 | moveFileToTargetOrDelete(f, level0File);
|
---|
743 | }
|
---|
744 | }
|
---|
745 | }
|
---|
746 |
|
---|
747 | private static String sanitizeLevelName(String ln) {
|
---|
748 | int ind = ln.indexOf("_");
|
---|
749 | String res = ln.substring(0, ind + 1);
|
---|
750 | res += ln.substring(ind + 1, ind + 2).toUpperCase();
|
---|
751 | res += ln.substring(ind + 2);
|
---|
752 | return res;
|
---|
753 | }
|
---|
754 |
|
---|
755 | /**
|
---|
756 | * Verify that the Edition is within a subfolder to vanilla Oni
|
---|
757 | * (..../Oni/Edition/AEInstaller)
|
---|
758 | *
|
---|
759 | * @return true if GDF can be found in the parent's parent-path
|
---|
760 | */
|
---|
761 | public static boolean verifyRunningDirectory() {
|
---|
762 | return Paths.getVanillaGDF().exists()
|
---|
763 | && Paths.getVanillaGDF().isDirectory();
|
---|
764 | }
|
---|
765 | }
|
---|