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