| 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 |
File targetFile = new File(targetPath, fbsl.getName()); |
| 405 |
if (addon || !targetFile.exists()) { |
| 406 |
try { |
| 407 |
FileUtils.copyFile(fbsl, targetFile); |
| 408 |
} catch (IOException e) { |
| 409 |
e.printStackTrace(); |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
private static void combineBinaryFiles(List<File> srcFoldersFiles, |
| 418 |
InstallProgressListener listener, PrintWriter log) { |
| 419 |
TreeMap<String, Vector<File>> levels = new TreeMap<String, Vector<File>>(); |
| 420 |
|
| 421 |
for (File path : srcFoldersFiles) { |
| 422 |
for (File levelF : path.listFiles()) { |
| 423 |
String fn = levelF.getName().toLowerCase(); |
| 424 |
String levelN = null; |
| 425 |
if (levelF.isDirectory()) { |
| 426 |
levelN = fn; |
| 427 |
} else if (fn.endsWith(".dat")) { |
| 428 |
levelN = fn.substring(0, fn.lastIndexOf('.')); |
| 429 |
} |
| 430 |
if (levelN != null) { |
| 431 |
if (!levels.containsKey(levelN)) |
| 432 |
levels.put(levelN, new Vector<File>()); |
| 433 |
levels.get(levelN).add(levelF); |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
int totalSteps = 0; |
| 439 |
int stepsDone = 0; |
| 440 |
|
| 441 |
for (@SuppressWarnings("unused") |
| 442 |
String s : levels.keySet()) |
| 443 |
totalSteps++; |
| 444 |
totalSteps++; |
| 445 |
|
| 446 |
log.println("Importing levels"); |
| 447 |
for (String l : levels.keySet()) { |
| 448 |
log.println("\tLevel " + l); |
| 449 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 450 |
"Installing level " + l); |
| 451 |
for (File f : levels.get(l)) { |
| 452 |
log.println("\t\t\t" + f.getPath()); |
| 453 |
} |
| 454 |
|
| 455 |
Vector<String> res = OniSplit.packLevel(levels.get(l), new File( |
| 456 |
Paths.getEditionGDF(), sanitizeLevelName(l) + ".dat")); |
| 457 |
if (res != null && res.size() > 0) { |
| 458 |
for (String s : res) |
| 459 |
log.println("\t\t" + s); |
| 460 |
} |
| 461 |
|
| 462 |
log.println(); |
| 463 |
stepsDone++; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
private static void copyVideos(PrintWriter log) { |
| 468 |
if (Settings.getInstance().get("copyintro", false)) { |
| 469 |
File src = new File(Paths.getVanillaGDF(), "intro.bik"); |
| 470 |
log.println("Copying intro"); |
| 471 |
if (src.exists()) { |
| 472 |
try { |
| 473 |
FileUtils.copyFileToDirectory(src, Paths.getEditionGDF()); |
| 474 |
} catch (IOException e) { |
| 475 |
e.printStackTrace(); |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
if (Settings.getInstance().get("copyoutro", true)) { |
| 480 |
File src = new File(Paths.getVanillaGDF(), "outro.bik"); |
| 481 |
log.println("Copying outro"); |
| 482 |
if (src.exists()) { |
| 483 |
try { |
| 484 |
FileUtils.copyFileToDirectory(src, Paths.getEditionGDF()); |
| 485 |
} catch (IOException e) { |
| 486 |
e.printStackTrace(); |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
private static void unlockLevels(TreeSet<Integer> unlockLevels, |
| 493 |
PrintWriter log) { |
| 494 |
File dat = new File(Paths.getEditionBasePath(), "persist.dat"); |
| 495 |
log.println("Unlocking levels: " + unlockLevels.toString()); |
| 496 |
if (!dat.exists()) { |
| 497 |
InputStream is = AEInstaller2.class |
| 498 |
.getResourceAsStream("/net/oni2/aeinstaller/resources/persist.dat"); |
| 499 |
try { |
| 500 |
FileUtils.copyInputStreamToFile(is, dat); |
| 501 |
} catch (IOException e) { |
| 502 |
// TODO Auto-generated catch block |
| 503 |
e.printStackTrace(); |
| 504 |
} |
| 505 |
} |
| 506 |
PersistDat save = new PersistDat(dat); |
| 507 |
HashSet<Integer> currentlyUnlocked = save.getUnlockedLevels(); |
| 508 |
currentlyUnlocked.addAll(unlockLevels); |
| 509 |
save.setUnlockedLevels(currentlyUnlocked); |
| 510 |
save.close(); |
| 511 |
} |
| 512 |
|
| 513 |
/** |
| 514 |
* Initializes the Edition core |
| 515 |
* |
| 516 |
* @param listener |
| 517 |
* Listener for status updates |
| 518 |
*/ |
| 519 |
public static void initializeEdition(InstallProgressListener listener) { |
| 520 |
File init = new File(Paths.getTempPath(), "init"); |
| 521 |
|
| 522 |
int totalSteps = 0; |
| 523 |
int stepsDone = 0; |
| 524 |
|
| 525 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 526 |
|
| 527 |
for (@SuppressWarnings("unused") |
| 528 |
File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() { |
| 529 |
@Override |
| 530 |
public boolean accept(File dir, String name) { |
| 531 |
return name.endsWith(".dat"); |
| 532 |
} |
| 533 |
})) { |
| 534 |
totalSteps++; |
| 535 |
} |
| 536 |
totalSteps = totalSteps * 2 + 2; |
| 537 |
|
| 538 |
try { |
| 539 |
File logFile = new File(Paths.getInstallerPath(), |
| 540 |
"Initialization.log"); |
| 541 |
PrintWriter log = new PrintWriter(logFile); |
| 542 |
|
| 543 |
Date start = new Date(); |
| 544 |
log.println("Initialization of Edition core started at " |
| 545 |
+ sdf.format(start)); |
| 546 |
log.println("Cleaning directories"); |
| 547 |
|
| 548 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 549 |
"Cleaning up directories"); |
| 550 |
createEmptyPath(Paths.getVanillaOnisPath()); |
| 551 |
createEmptyPath(init); |
| 552 |
File level0Folder = new File(init, "level0_Final"); |
| 553 |
createEmptyPath(level0Folder); |
| 554 |
|
| 555 |
stepsDone++; |
| 556 |
|
| 557 |
log.println("Exporting levels and moving files to level0"); |
| 558 |
|
| 559 |
for (File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() { |
| 560 |
@Override |
| 561 |
public boolean accept(File dir, String name) { |
| 562 |
return name.endsWith(".dat"); |
| 563 |
} |
| 564 |
})) { |
| 565 |
String levelName = f.getName().substring(0, |
| 566 |
f.getName().indexOf('.')); |
| 567 |
Scanner fi = new Scanner(levelName); |
| 568 |
int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+")); |
| 569 |
|
| 570 |
log.println("\t" + levelName + ":"); |
| 571 |
log.println("\t\tExporting"); |
| 572 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 573 |
"Exporting vanilla level " + levelNumber); |
| 574 |
|
| 575 |
// Edition/GameDataFolder/level*_Final/ |
| 576 |
File tempLevelFolder = new File(init, levelName); |
| 577 |
|
| 578 |
// Export Vanilla-Level-Dat -> Temp/Level |
| 579 |
Vector<String> res = OniSplit.export(tempLevelFolder, f); |
| 580 |
if (res != null && res.size() > 0) { |
| 581 |
for (String s : res) |
| 582 |
log.println("\t\t\t" + s); |
| 583 |
} |
| 584 |
|
| 585 |
log.println("\t\tMoving files"); |
| 586 |
handleFileGlobalisation(tempLevelFolder, level0Folder, |
| 587 |
levelNumber); |
| 588 |
stepsDone++; |
| 589 |
log.println(); |
| 590 |
} |
| 591 |
|
| 592 |
log.println("Reimporting levels"); |
| 593 |
|
| 594 |
for (File f : init.listFiles()) { |
| 595 |
String levelName = f.getName(); |
| 596 |
|
| 597 |
log.println("\t" + levelName); |
| 598 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 599 |
"Creating globalized " + levelName); |
| 600 |
|
| 601 |
Vector<File> folders = new Vector<File>(); |
| 602 |
folders.add(f); |
| 603 |
|
| 604 |
Vector<String> res = OniSplit.importLevel(folders, new File( |
| 605 |
Paths.getVanillaOnisPath(), levelName + ".dat")); |
| 606 |
if (res != null && res.size() > 0) { |
| 607 |
for (String s : res) |
| 608 |
log.println("\t\t" + s); |
| 609 |
} |
| 610 |
|
| 611 |
log.println(); |
| 612 |
stepsDone++; |
| 613 |
} |
| 614 |
|
| 615 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 616 |
"Copying basic files"); |
| 617 |
// Copy Oni-configs |
| 618 |
File persistVanilla = new File(Paths.getOniBasePath(), |
| 619 |
"persist.dat"); |
| 620 |
File persistEdition = new File(Paths.getEditionBasePath(), |
| 621 |
"persist.dat"); |
| 622 |
File keyConfVanilla = new File(Paths.getOniBasePath(), |
| 623 |
"key_config.txt"); |
| 624 |
File keyConfEdition = new File(Paths.getEditionBasePath(), |
| 625 |
"key_config.txt"); |
| 626 |
if (persistVanilla.exists() && !persistEdition.exists()) |
| 627 |
FileUtils.copyFile(persistVanilla, persistEdition); |
| 628 |
if (keyConfVanilla.exists() && !keyConfEdition.exists()) |
| 629 |
FileUtils.copyFile(keyConfVanilla, keyConfEdition); |
| 630 |
|
| 631 |
FileUtils.deleteDirectory(init); |
| 632 |
|
| 633 |
Date end = new Date(); |
| 634 |
log.println("Initialization ended at " + sdf.format(end)); |
| 635 |
log.println("Process took " |
| 636 |
+ ((end.getTime() - start.getTime()) / 1000) + " seconds"); |
| 637 |
log.close(); |
| 638 |
} catch (IOException e) { |
| 639 |
e.printStackTrace(); |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
private static void moveFileToTargetOrDelete(File source, File target) { |
| 644 |
if (source.equals(target)) |
| 645 |
return; |
| 646 |
if (!target.exists()) { |
| 647 |
if (!source.renameTo(target)) { |
| 648 |
System.err.println("File " + source.getPath() + " not moved!"); |
| 649 |
} |
| 650 |
} else if (!source.delete()) { |
| 651 |
System.err.println("File " + source.getPath() + " not deleted!"); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
private static void handleFileGlobalisation(File tempFolder, |
| 656 |
File level0Folder, int levelNumber) { |
| 657 |
// Move AKEV and related files to subfolder so they're not globalized: |
| 658 |
if (levelNumber != 0) { |
| 659 |
File akevFolder = new File(tempFolder, "AKEV"); |
| 660 |
akevFolder.mkdir(); |
| 661 |
OniSplit.move(akevFolder, tempFolder.getPath() + "/AKEV*.oni", |
| 662 |
"overwrite"); |
| 663 |
} |
| 664 |
|
| 665 |
for (File f : tempFolder.listFiles(new FileFilter() { |
| 666 |
@Override |
| 667 |
public boolean accept(File pathname) { |
| 668 |
return pathname.isFile(); |
| 669 |
} |
| 670 |
})) { |
| 671 |
// Move matching files to subfolder NoGlobal: |
| 672 |
if (f.getName().startsWith("TXMPfail") |
| 673 |
|| f.getName().startsWith("TXMPlevel") |
| 674 |
|| (f.getName().startsWith("TXMP") && f.getName().contains( |
| 675 |
"intro")) |
| 676 |
|| f.getName().startsWith("TXMB") |
| 677 |
|| f.getName().equals("M3GMpowerup_lsi.oni") |
| 678 |
|| f.getName().equals("TXMPlsi_icon.oni") |
| 679 |
|| (f.getName().startsWith("TXMB") && f.getName().contains( |
| 680 |
"splash_screen.oni"))) { |
| 681 |
File noGlobal = new File(tempFolder, "NoGlobal"); |
| 682 |
noGlobal.mkdir(); |
| 683 |
File noGlobalFile = new File(noGlobal, f.getName()); |
| 684 |
moveFileToTargetOrDelete(f, noGlobalFile); |
| 685 |
} |
| 686 |
// Move matching files to level0_Animations/level0_TRAC |
| 687 |
else if (f.getName().startsWith("TRAC")) { |
| 688 |
File level0File = new File(level0Folder, f.getName()); |
| 689 |
moveFileToTargetOrDelete(f, level0File); |
| 690 |
} |
| 691 |
// Move matching files to level0_Animations/level0_TRAM |
| 692 |
else if (f.getName().startsWith("TRAM")) { |
| 693 |
File level0File = new File(level0Folder, f.getName()); |
| 694 |
moveFileToTargetOrDelete(f, level0File); |
| 695 |
} |
| 696 |
// Move matching files to level0_Textures |
| 697 |
else if (f.getName().startsWith("ONSK") |
| 698 |
|| f.getName().startsWith("TXMP")) { |
| 699 |
File level0File = new File(level0Folder, f.getName()); |
| 700 |
moveFileToTargetOrDelete(f, level0File); |
| 701 |
} |
| 702 |
// Move matching files to *VANILLA*/level0_Characters |
| 703 |
else if (f.getName().startsWith("ONCC") |
| 704 |
|| f.getName().startsWith("TRBS") |
| 705 |
|| f.getName().startsWith("ONCV") |
| 706 |
|| f.getName().startsWith("ONVL") |
| 707 |
|| f.getName().startsWith("TRMA") |
| 708 |
|| f.getName().startsWith("TRSC") |
| 709 |
|| f.getName().startsWith("TRAS")) { |
| 710 |
File level0File = new File(level0Folder, f.getName()); |
| 711 |
moveFileToTargetOrDelete(f, level0File); |
| 712 |
} |
| 713 |
// Move matching files to level0_Sounds |
| 714 |
else if (f.getName().startsWith("OSBD") |
| 715 |
|| f.getName().startsWith("SNDD")) { |
| 716 |
File level0File = new File(level0Folder, f.getName()); |
| 717 |
moveFileToTargetOrDelete(f, level0File); |
| 718 |
} |
| 719 |
// Move matching files to level0_Particles |
| 720 |
else if (f.getName().startsWith("BINA3") |
| 721 |
|| f.getName().startsWith("M3GMdebris") |
| 722 |
|| f.getName().equals("M3GMtoxic_bubble.oni") |
| 723 |
|| f.getName().startsWith("M3GMelec") |
| 724 |
|| f.getName().startsWith("M3GMrat") |
| 725 |
|| f.getName().startsWith("M3GMjet") |
| 726 |
|| f.getName().startsWith("M3GMbomb_") |
| 727 |
|| f.getName().equals("M3GMbarab_swave.oni") |
| 728 |
|| f.getName().equals("M3GMbloodyfoot.oni")) { |
| 729 |
File level0File = new File(level0Folder, f.getName()); |
| 730 |
moveFileToTargetOrDelete(f, level0File); |
| 731 |
} |
| 732 |
// Move matching files to Archive (aka delete them) |
| 733 |
else if (f.getName().startsWith("AGDB") |
| 734 |
|| f.getName().startsWith("TRCM")) { |
| 735 |
f.delete(); |
| 736 |
} |
| 737 |
// Move matching files to /level0_Final/ |
| 738 |
else if (f.getName().startsWith("ONWC")) { |
| 739 |
File level0File = new File(level0Folder, f.getName()); |
| 740 |
moveFileToTargetOrDelete(f, level0File); |
| 741 |
} |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
private static String sanitizeLevelName(String ln) { |
| 746 |
int ind = ln.indexOf("_"); |
| 747 |
String res = ln.substring(0, ind + 1); |
| 748 |
res += ln.substring(ind + 1, ind + 2).toUpperCase(); |
| 749 |
res += ln.substring(ind + 2); |
| 750 |
return res; |
| 751 |
} |
| 752 |
|
| 753 |
/** |
| 754 |
* Verify that the Edition is within a subfolder to vanilla Oni |
| 755 |
* (..../Oni/Edition/AEInstaller) |
| 756 |
* |
| 757 |
* @return true if GDF can be found in the parent's parent-path |
| 758 |
*/ |
| 759 |
public static boolean verifyRunningDirectory() { |
| 760 |
return Paths.getVanillaGDF().exists() |
| 761 |
&& Paths.getVanillaGDF().isDirectory(); |
| 762 |
} |
| 763 |
} |