| 1 |
package net.oni2.aeinstaller.backend.oni; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.FileFilter; |
| 5 |
import java.io.FileNotFoundException; |
| 6 |
import java.io.FilenameFilter; |
| 7 |
import java.io.IOException; |
| 8 |
import java.io.PrintWriter; |
| 9 |
import java.text.SimpleDateFormat; |
| 10 |
import java.util.Date; |
| 11 |
import java.util.HashMap; |
| 12 |
import java.util.List; |
| 13 |
import java.util.Scanner; |
| 14 |
import java.util.TreeSet; |
| 15 |
import java.util.Vector; |
| 16 |
|
| 17 |
import net.oni2.aeinstaller.backend.Paths; |
| 18 |
import net.oni2.aeinstaller.backend.Settings; |
| 19 |
import net.oni2.aeinstaller.backend.Settings.Platform; |
| 20 |
import net.oni2.aeinstaller.backend.mods.Mod; |
| 21 |
|
| 22 |
import org.apache.commons.io.FileUtils; |
| 23 |
|
| 24 |
/** |
| 25 |
* @author Christian Illy |
| 26 |
*/ |
| 27 |
public class Installer { |
| 28 |
/** |
| 29 |
* @return Is Edition Core initialized |
| 30 |
*/ |
| 31 |
public static boolean isEditionInitialized() { |
| 32 |
return Paths.getVanillaOnisPath().exists(); |
| 33 |
} |
| 34 |
|
| 35 |
private static void createEmptyPath(File path) throws IOException { |
| 36 |
if (path.exists()) |
| 37 |
FileUtils.deleteDirectory(path); |
| 38 |
path.mkdirs(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Install the given set of mods |
| 43 |
* |
| 44 |
* @param mods |
| 45 |
* Mods to install |
| 46 |
* @param listener |
| 47 |
* Listener for install progress updates |
| 48 |
*/ |
| 49 |
public static void install(TreeSet<Mod> mods, |
| 50 |
InstallProgressListener listener) { |
| 51 |
Vector<File> folders = new Vector<File>(); |
| 52 |
folders.add(Paths.getVanillaOnisPath()); |
| 53 |
|
| 54 |
for (Mod m : mods) { |
| 55 |
File oni = new File(m.getLocalPath(), "oni"); |
| 56 |
if (oni.exists()) { |
| 57 |
if (m.hasSeparatePlatformDirs()) { |
| 58 |
File oniCommon = new File(oni, "common"); |
| 59 |
File oniMac = new File(oni, "mac_only"); |
| 60 |
File oniWin = new File(oni, "win_only"); |
| 61 |
if (oniCommon.exists()) |
| 62 |
folders.add(oniCommon); |
| 63 |
if (Settings.getPlatform() == Platform.MACOS |
| 64 |
&& oniMac.exists()) |
| 65 |
folders.add(oniMac); |
| 66 |
else if (oniWin.exists()) |
| 67 |
folders.add(oniWin); |
| 68 |
} else { |
| 69 |
folders.add(oni); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
combineBinaryFiles(folders, listener); |
| 75 |
|
| 76 |
// TODO: bsl() |
| 77 |
} |
| 78 |
|
| 79 |
private static void combineBinaryFiles(List<File> srcFoldersFiles, |
| 80 |
InstallProgressListener listener) { |
| 81 |
try { |
| 82 |
HashMap<String, Vector<File>> levels = new HashMap<String, Vector<File>>(); |
| 83 |
|
| 84 |
for (File path : srcFoldersFiles) { |
| 85 |
for (File levelF : path.listFiles()) { |
| 86 |
String fn = levelF.getName().toLowerCase(); |
| 87 |
String levelN = null; |
| 88 |
if (levelF.isDirectory()) { |
| 89 |
levelN = fn; |
| 90 |
} else if (fn.endsWith(".dat")) { |
| 91 |
levelN = fn.substring(0, fn.lastIndexOf('.')); |
| 92 |
} |
| 93 |
if (levelN != null) { |
| 94 |
if (!levels.containsKey(levelN)) |
| 95 |
levels.put(levelN, new Vector<File>()); |
| 96 |
levels.get(levelN).add(levelF); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
int totalSteps = 0; |
| 102 |
int stepsDone = 0; |
| 103 |
|
| 104 |
for (@SuppressWarnings("unused") |
| 105 |
String s : levels.keySet()) |
| 106 |
totalSteps++; |
| 107 |
|
| 108 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 109 |
|
| 110 |
File logFile = new File(Paths.getInstallerPath(), |
| 111 |
"Installation.log"); |
| 112 |
PrintWriter log = null; |
| 113 |
try { |
| 114 |
log = new PrintWriter(logFile); |
| 115 |
} catch (FileNotFoundException e) { |
| 116 |
e.printStackTrace(); |
| 117 |
} |
| 118 |
|
| 119 |
Date start = new Date(); |
| 120 |
log.println("Installation of mods started at " + sdf.format(start)); |
| 121 |
|
| 122 |
log.println("Cleaning directories"); |
| 123 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 124 |
"Cleaning up directories"); |
| 125 |
|
| 126 |
createEmptyPath(Paths.getEditionGDF()); |
| 127 |
|
| 128 |
log.println("Importing levels"); |
| 129 |
for (String l : levels.keySet()) { |
| 130 |
log.println("\tLevel " + l); |
| 131 |
for (File f : levels.get(l)) { |
| 132 |
log.println("\t\t\t" + f.getPath()); |
| 133 |
} |
| 134 |
|
| 135 |
Vector<String> res = OniSplit.packLevel(levels.get(l), |
| 136 |
new File(Paths.getEditionGDF(), l + ".dat")); |
| 137 |
if (res != null && res.size() > 0) { |
| 138 |
for (String s : res) |
| 139 |
log.println("\t\t" + s); |
| 140 |
} |
| 141 |
|
| 142 |
log.println(); |
| 143 |
} |
| 144 |
|
| 145 |
Date end = new Date(); |
| 146 |
log.println("Initialization ended at " + sdf.format(end)); |
| 147 |
log.println("Process took " |
| 148 |
+ ((end.getTime() - start.getTime()) / 1000) + " seconds"); |
| 149 |
log.close(); |
| 150 |
} catch (IOException e) { |
| 151 |
// TODO Auto-generated catch block |
| 152 |
e.printStackTrace(); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Initializes the Edition core |
| 158 |
* |
| 159 |
* @param listener |
| 160 |
* Listener for status updates |
| 161 |
*/ |
| 162 |
public static void initializeEdition(InstallProgressListener listener) { |
| 163 |
File init = new File(Paths.getTempPath(), "init"); |
| 164 |
|
| 165 |
int totalSteps = 0; |
| 166 |
int stepsDone = 0; |
| 167 |
|
| 168 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 169 |
|
| 170 |
for (@SuppressWarnings("unused") |
| 171 |
File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() { |
| 172 |
@Override |
| 173 |
public boolean accept(File dir, String name) { |
| 174 |
return name.endsWith(".dat"); |
| 175 |
} |
| 176 |
})) { |
| 177 |
totalSteps++; |
| 178 |
} |
| 179 |
totalSteps = totalSteps * 2 + 2; |
| 180 |
|
| 181 |
try { |
| 182 |
File logFile = new File(Paths.getInstallerPath(), |
| 183 |
"Initialization.log"); |
| 184 |
PrintWriter log = new PrintWriter(logFile); |
| 185 |
|
| 186 |
Date start = new Date(); |
| 187 |
log.println("Initialization of Edition core started at " |
| 188 |
+ sdf.format(start)); |
| 189 |
log.println("Cleaning directories"); |
| 190 |
|
| 191 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 192 |
"Cleaning up directories"); |
| 193 |
createEmptyPath(Paths.getVanillaOnisPath()); |
| 194 |
createEmptyPath(init); |
| 195 |
File level0Folder = new File(init, "level0_Final"); |
| 196 |
createEmptyPath(level0Folder); |
| 197 |
|
| 198 |
stepsDone++; |
| 199 |
|
| 200 |
log.println("Exporting levels and moving files to level0"); |
| 201 |
|
| 202 |
for (File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() { |
| 203 |
@Override |
| 204 |
public boolean accept(File dir, String name) { |
| 205 |
return name.endsWith(".dat"); |
| 206 |
} |
| 207 |
})) { |
| 208 |
String levelName = f.getName().substring(0, |
| 209 |
f.getName().indexOf('.')); |
| 210 |
Scanner fi = new Scanner(levelName); |
| 211 |
int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+")); |
| 212 |
|
| 213 |
log.println("\t" + levelName + ":"); |
| 214 |
log.println("\t\tExporting"); |
| 215 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 216 |
"Exporting vanilla level " + levelNumber); |
| 217 |
|
| 218 |
// Edition/GameDataFolder/level*_Final/ |
| 219 |
File tempLevelFolder = new File(init, levelName); |
| 220 |
|
| 221 |
// Export Vanilla-Level-Dat -> Temp/Level |
| 222 |
Vector<String> res = OniSplit.export(tempLevelFolder, f); |
| 223 |
if (res != null && res.size() > 0) { |
| 224 |
for (String s : res) |
| 225 |
log.println("\t\t\t" + s); |
| 226 |
} |
| 227 |
|
| 228 |
log.println("\t\tMoving files"); |
| 229 |
handleFileGlobalisation(tempLevelFolder, level0Folder, |
| 230 |
levelNumber); |
| 231 |
stepsDone++; |
| 232 |
log.println(); |
| 233 |
} |
| 234 |
|
| 235 |
log.println("Reimporting levels"); |
| 236 |
|
| 237 |
for (File f : init.listFiles()) { |
| 238 |
String levelName = f.getName(); |
| 239 |
|
| 240 |
log.println("\t" + levelName); |
| 241 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 242 |
"Creating globalized " + levelName); |
| 243 |
|
| 244 |
Vector<File> folders = new Vector<File>(); |
| 245 |
folders.add(f); |
| 246 |
|
| 247 |
Vector<String> res = OniSplit.importLevel(folders, new File( |
| 248 |
Paths.getVanillaOnisPath(), levelName + ".dat")); |
| 249 |
if (res != null && res.size() > 0) { |
| 250 |
for (String s : res) |
| 251 |
log.println("\t\t" + s); |
| 252 |
} |
| 253 |
|
| 254 |
log.println(); |
| 255 |
stepsDone++; |
| 256 |
} |
| 257 |
|
| 258 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 259 |
"Copying basic files"); |
| 260 |
// Copy Oni-configs |
| 261 |
File persistVanilla = new File(Paths.getOniBasePath(), |
| 262 |
"persist.dat"); |
| 263 |
File persistEdition = new File(Paths.getEditionBasePath(), |
| 264 |
"persist.dat"); |
| 265 |
File keyConfVanilla = new File(Paths.getOniBasePath(), |
| 266 |
"key_config.txt"); |
| 267 |
File keyConfEdition = new File(Paths.getEditionBasePath(), |
| 268 |
"key_config.txt"); |
| 269 |
if (persistVanilla.exists() && !persistEdition.exists()) |
| 270 |
FileUtils.copyFile(persistVanilla, persistEdition); |
| 271 |
if (keyConfVanilla.exists() && !keyConfEdition.exists()) |
| 272 |
FileUtils.copyFile(keyConfVanilla, keyConfEdition); |
| 273 |
|
| 274 |
FileUtils.deleteDirectory(init); |
| 275 |
|
| 276 |
Date end = new Date(); |
| 277 |
log.println("Initialization ended at " + sdf.format(end)); |
| 278 |
log.println("Process took " |
| 279 |
+ ((end.getTime() - start.getTime()) / 1000) + " seconds"); |
| 280 |
log.close(); |
| 281 |
} catch (IOException e) { |
| 282 |
e.printStackTrace(); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
private static void moveFileToTargetOrDelete(File source, File target) { |
| 287 |
if (source.equals(target)) |
| 288 |
return; |
| 289 |
if (!target.exists()) { |
| 290 |
if (!source.renameTo(target)) { |
| 291 |
System.err.println("File " + source.getPath() + " not moved!"); |
| 292 |
} |
| 293 |
} else if (!source.delete()) { |
| 294 |
System.err.println("File " + source.getPath() + " not deleted!"); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
private static void handleFileGlobalisation(File tempFolder, |
| 299 |
File level0Folder, int levelNumber) { |
| 300 |
// Move AKEV and related files to subfolder so they're not globalized: |
| 301 |
if (levelNumber != 0) { |
| 302 |
File akevFolder = new File(tempFolder, "AKEV"); |
| 303 |
akevFolder.mkdir(); |
| 304 |
OniSplit.move(akevFolder, tempFolder.getPath() + "/AKEV*.oni", |
| 305 |
"overwrite"); |
| 306 |
} |
| 307 |
|
| 308 |
for (File f : tempFolder.listFiles(new FileFilter() { |
| 309 |
@Override |
| 310 |
public boolean accept(File pathname) { |
| 311 |
return pathname.isFile(); |
| 312 |
} |
| 313 |
})) { |
| 314 |
// Move matching files to subfolder NoGlobal: |
| 315 |
if (f.getName().startsWith("TXMPfail") |
| 316 |
|| f.getName().startsWith("TXMPlevel") |
| 317 |
|| (f.getName().startsWith("TXMP") && f.getName().contains( |
| 318 |
"intro")) |
| 319 |
|| f.getName().startsWith("TXMB") |
| 320 |
|| f.getName().equals("M3GMpowerup_lsi.oni") |
| 321 |
|| f.getName().equals("TXMPlsi_icon.oni") |
| 322 |
|| (f.getName().startsWith("TXMB") && f.getName().contains( |
| 323 |
"splash_screen.oni"))) { |
| 324 |
File noGlobal = new File(tempFolder, "NoGlobal"); |
| 325 |
noGlobal.mkdir(); |
| 326 |
File noGlobalFile = new File(noGlobal, f.getName()); |
| 327 |
moveFileToTargetOrDelete(f, noGlobalFile); |
| 328 |
} |
| 329 |
// Move matching files to level0_Animations/level0_TRAC |
| 330 |
else if (f.getName().startsWith("TRAC")) { |
| 331 |
File level0File = new File(level0Folder, f.getName()); |
| 332 |
moveFileToTargetOrDelete(f, level0File); |
| 333 |
} |
| 334 |
// Move matching files to level0_Animations/level0_TRAM |
| 335 |
else if (f.getName().startsWith("TRAM")) { |
| 336 |
File level0File = new File(level0Folder, f.getName()); |
| 337 |
moveFileToTargetOrDelete(f, level0File); |
| 338 |
} |
| 339 |
// Move matching files to level0_Textures |
| 340 |
else if (f.getName().startsWith("ONSK") |
| 341 |
|| f.getName().startsWith("TXMP")) { |
| 342 |
File level0File = new File(level0Folder, f.getName()); |
| 343 |
moveFileToTargetOrDelete(f, level0File); |
| 344 |
} |
| 345 |
// Move matching files to *VANILLA*/level0_Characters |
| 346 |
else if (f.getName().startsWith("ONCC") |
| 347 |
|| f.getName().startsWith("TRBS") |
| 348 |
|| f.getName().startsWith("ONCV") |
| 349 |
|| f.getName().startsWith("ONVL") |
| 350 |
|| f.getName().startsWith("TRMA") |
| 351 |
|| f.getName().startsWith("TRSC") |
| 352 |
|| f.getName().startsWith("TRAS")) { |
| 353 |
File level0File = new File(level0Folder, f.getName()); |
| 354 |
moveFileToTargetOrDelete(f, level0File); |
| 355 |
} |
| 356 |
// Move matching files to level0_Sounds |
| 357 |
else if (f.getName().startsWith("OSBD") |
| 358 |
|| f.getName().startsWith("SNDD")) { |
| 359 |
File level0File = new File(level0Folder, f.getName()); |
| 360 |
moveFileToTargetOrDelete(f, level0File); |
| 361 |
} |
| 362 |
// Move matching files to level0_Particles |
| 363 |
else if (f.getName().startsWith("BINA3") |
| 364 |
|| f.getName().startsWith("M3GMdebris") |
| 365 |
|| f.getName().equals("M3GMtoxic_bubble.oni") |
| 366 |
|| f.getName().startsWith("M3GMelec") |
| 367 |
|| f.getName().startsWith("M3GMrat") |
| 368 |
|| f.getName().startsWith("M3GMjet") |
| 369 |
|| f.getName().startsWith("M3GMbomb_") |
| 370 |
|| f.getName().equals("M3GMbarab_swave.oni") |
| 371 |
|| f.getName().equals("M3GMbloodyfoot.oni")) { |
| 372 |
File level0File = new File(level0Folder, f.getName()); |
| 373 |
moveFileToTargetOrDelete(f, level0File); |
| 374 |
} |
| 375 |
// Move matching files to Archive (aka delete them) |
| 376 |
else if (f.getName().startsWith("AGDB") |
| 377 |
|| f.getName().startsWith("TRCM")) { |
| 378 |
f.delete(); |
| 379 |
} |
| 380 |
// Move matching files to /level0_Final/ |
| 381 |
else if (f.getName().startsWith("ONWC")) { |
| 382 |
File level0File = new File(level0Folder, f.getName()); |
| 383 |
moveFileToTargetOrDelete(f, level0File); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Verify that the Edition is within a subfolder to vanilla Oni (..../Oni/Edition/AEInstaller) |
| 390 |
* @return true if GDF can be found in the parent's parent-path |
| 391 |
*/ |
| 392 |
public static boolean verifyRunningDirectory() { |
| 393 |
return Paths.getVanillaGDF().exists() && Paths.getVanillaGDF().isDirectory(); |
| 394 |
} |
| 395 |
} |