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