| 1 |
package net.oni2.aeinstaller.backend.oni.management; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.FileFilter; |
| 5 |
import java.io.FilenameFilter; |
| 6 |
import java.io.IOException; |
| 7 |
import java.text.SimpleDateFormat; |
| 8 |
import java.util.Date; |
| 9 |
import java.util.Scanner; |
| 10 |
import java.util.TreeSet; |
| 11 |
import java.util.Vector; |
| 12 |
|
| 13 |
import net.oni2.aeinstaller.AEInstaller2; |
| 14 |
import net.oni2.aeinstaller.backend.Paths; |
| 15 |
import net.oni2.aeinstaller.backend.oni.OniSplit; |
| 16 |
import net.oni2.platformtools.applicationinvoker.ApplicationInvocationResult; |
| 17 |
|
| 18 |
import org.apache.commons.io.FileUtils; |
| 19 |
|
| 20 |
import com.paour.NaturalOrderComparator; |
| 21 |
|
| 22 |
/** |
| 23 |
* @author Christian Illy |
| 24 |
*/ |
| 25 |
public class Initializer { |
| 26 |
private static void createEmptyPath(File path) throws IOException { |
| 27 |
if (path.exists()) |
| 28 |
FileUtils.deleteDirectory(path); |
| 29 |
path.mkdirs(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Initializes the Edition core |
| 34 |
* |
| 35 |
* @param listener |
| 36 |
* Listener for status updates |
| 37 |
*/ |
| 38 |
public static void initializeEdition(InstallProgressListener listener) { |
| 39 |
File init = new File(Paths.getTempPath(), "init"); |
| 40 |
|
| 41 |
int totalSteps = 0; |
| 42 |
int stepsDone = 0; |
| 43 |
|
| 44 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 45 |
|
| 46 |
TreeSet<File> levelFiles = new TreeSet<File>( |
| 47 |
new NaturalOrderComparator()); |
| 48 |
|
| 49 |
for (File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() { |
| 50 |
@Override |
| 51 |
public boolean accept(File dir, String name) { |
| 52 |
return name.endsWith(".dat"); |
| 53 |
} |
| 54 |
})) { |
| 55 |
totalSteps++; |
| 56 |
levelFiles.add(f); |
| 57 |
} |
| 58 |
totalSteps = totalSteps * 2 + 2; |
| 59 |
|
| 60 |
try { |
| 61 |
File logFile = new File(Paths.getInstallerPath(), |
| 62 |
"Initialization.log"); |
| 63 |
Logger log = new Logger(logFile); |
| 64 |
|
| 65 |
Date start = new Date(); |
| 66 |
log.println("Initialization of Edition core started at " |
| 67 |
+ sdf.format(start)); |
| 68 |
log.println("Cleaning directories"); |
| 69 |
|
| 70 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 71 |
AEInstaller2.globalBundle.getString("coreInitializer.cleanup")); |
| 72 |
createEmptyPath(Paths.getVanillaOnisPath()); |
| 73 |
createEmptyPath(init); |
| 74 |
File level0Folder = new File(init, "level0_Final"); |
| 75 |
createEmptyPath(level0Folder); |
| 76 |
|
| 77 |
stepsDone++; |
| 78 |
|
| 79 |
log.println("Exporting levels and moving files to level0"); |
| 80 |
|
| 81 |
for (File f : levelFiles) { |
| 82 |
String levelName = f.getName().substring(0, |
| 83 |
f.getName().indexOf('.')); |
| 84 |
Scanner fi = new Scanner(levelName); |
| 85 |
int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+")); |
| 86 |
|
| 87 |
log.println("\t" + levelName + ":"); |
| 88 |
log.println("\t\tExporting"); |
| 89 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 90 |
AEInstaller2.globalBundle.getString("coreInitializer.exportVanilla").replaceAll("%1", String.valueOf(levelNumber))); |
| 91 |
|
| 92 |
// Edition/GameDataFolder/level*_Final/ |
| 93 |
File tempLevelFolder = new File(init, levelName); |
| 94 |
|
| 95 |
// Export Vanilla-Level-Dat -> Temp/Level |
| 96 |
ApplicationInvocationResult res = OniSplit.export( |
| 97 |
tempLevelFolder, f); |
| 98 |
log.logAppOutput(res, true); |
| 99 |
|
| 100 |
log.println("\t\tMoving files"); |
| 101 |
handleFileGlobalisation(tempLevelFolder, level0Folder, |
| 102 |
levelNumber); |
| 103 |
stepsDone++; |
| 104 |
log.println(); |
| 105 |
} |
| 106 |
|
| 107 |
log.println("Reimporting levels"); |
| 108 |
TreeSet<File> initFolders = new TreeSet<File>( |
| 109 |
new NaturalOrderComparator()); |
| 110 |
for (File f : init.listFiles()) { |
| 111 |
initFolders.add(f); |
| 112 |
} |
| 113 |
|
| 114 |
for (File f : initFolders) { |
| 115 |
String levelName = f.getName(); |
| 116 |
|
| 117 |
log.println("\t" + levelName); |
| 118 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 119 |
AEInstaller2.globalBundle.getString("coreInitializer.globalize").replaceAll("%1", levelName)); |
| 120 |
|
| 121 |
Vector<File> folders = new Vector<File>(); |
| 122 |
folders.add(f); |
| 123 |
|
| 124 |
ApplicationInvocationResult res = OniSplit |
| 125 |
.importLevel(folders, |
| 126 |
new File(Paths.getVanillaOnisPath(), levelName |
| 127 |
+ ".dat")); |
| 128 |
log.logAppOutput(res, true); |
| 129 |
|
| 130 |
log.println(); |
| 131 |
stepsDone++; |
| 132 |
} |
| 133 |
|
| 134 |
listener.installProgressUpdate(stepsDone, totalSteps, |
| 135 |
AEInstaller2.globalBundle.getString("coreInitializer.copyBasicFiles")); |
| 136 |
// Copy Oni-configs |
| 137 |
File persistVanilla = new File(Paths.getOniBasePath(), |
| 138 |
"persist.dat"); |
| 139 |
File persistEdition = new File(Paths.getEditionBasePath(), |
| 140 |
"persist.dat"); |
| 141 |
File keyConfVanilla = new File(Paths.getOniBasePath(), |
| 142 |
"key_config.txt"); |
| 143 |
File keyConfEdition = new File(Paths.getEditionBasePath(), |
| 144 |
"key_config.txt"); |
| 145 |
if (persistVanilla.exists() && !persistEdition.exists()) |
| 146 |
FileUtils.copyFile(persistVanilla, persistEdition); |
| 147 |
if (keyConfVanilla.exists() && !keyConfEdition.exists()) |
| 148 |
FileUtils.copyFile(keyConfVanilla, keyConfEdition); |
| 149 |
|
| 150 |
FileUtils.deleteDirectory(init); |
| 151 |
|
| 152 |
Date end = new Date(); |
| 153 |
log.println("Initialization ended at " + sdf.format(end)); |
| 154 |
log.println("Process took " |
| 155 |
+ ((end.getTime() - start.getTime()) / 1000) + " seconds"); |
| 156 |
log.close(); |
| 157 |
} catch (IOException e) { |
| 158 |
e.printStackTrace(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
private static void moveFileToTargetOrDelete(File source, File target) { |
| 163 |
if (source.equals(target)) |
| 164 |
return; |
| 165 |
if (!target.exists()) { |
| 166 |
if (!source.renameTo(target)) { |
| 167 |
System.err.println("File " + source.getPath() + " not moved!"); |
| 168 |
} |
| 169 |
} else if (!source.delete()) { |
| 170 |
System.err.println("File " + source.getPath() + " not deleted!"); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
private static void handleFileGlobalisation(File tempFolder, |
| 175 |
File level0Folder, int levelNumber) { |
| 176 |
// Move AKEV and related files to subfolder so they're not globalized: |
| 177 |
if (levelNumber != 0) { |
| 178 |
File akevFolder = new File(tempFolder, "AKEV"); |
| 179 |
akevFolder.mkdir(); |
| 180 |
OniSplit.move(akevFolder, tempFolder.getPath() + "/AKEV*.oni", |
| 181 |
"overwrite"); |
| 182 |
} |
| 183 |
|
| 184 |
for (File f : tempFolder.listFiles(new FileFilter() { |
| 185 |
@Override |
| 186 |
public boolean accept(File pathname) { |
| 187 |
return pathname.isFile(); |
| 188 |
} |
| 189 |
})) { |
| 190 |
// Move matching files to subfolder NoGlobal: |
| 191 |
if (f.getName().startsWith("TXMPfail") |
| 192 |
|| f.getName().startsWith("TXMPlevel") |
| 193 |
|| (f.getName().startsWith("TXMP") && f.getName().contains( |
| 194 |
"intro")) |
| 195 |
|| f.getName().startsWith("TXMB") |
| 196 |
|| f.getName().equals("M3GMpowerup_lsi.oni") |
| 197 |
|| f.getName().equals("TXMPlsi_icon.oni") |
| 198 |
|| (f.getName().startsWith("TXMB") && f.getName().contains( |
| 199 |
"splash_screen.oni"))) { |
| 200 |
File noGlobal = new File(tempFolder, "NoGlobal"); |
| 201 |
noGlobal.mkdir(); |
| 202 |
File noGlobalFile = new File(noGlobal, f.getName()); |
| 203 |
moveFileToTargetOrDelete(f, noGlobalFile); |
| 204 |
} |
| 205 |
// Move matching files to level0_Animations/level0_TRAC |
| 206 |
else if (f.getName().startsWith("TRAC")) { |
| 207 |
File level0File = new File(level0Folder, f.getName()); |
| 208 |
moveFileToTargetOrDelete(f, level0File); |
| 209 |
} |
| 210 |
// Move matching files to level0_Animations/level0_TRAM |
| 211 |
else if (f.getName().startsWith("TRAM")) { |
| 212 |
File level0File = new File(level0Folder, f.getName()); |
| 213 |
moveFileToTargetOrDelete(f, level0File); |
| 214 |
} |
| 215 |
// Move matching files to level0_Textures |
| 216 |
else if (f.getName().startsWith("ONSK") |
| 217 |
|| f.getName().startsWith("TXMP")) { |
| 218 |
File level0File = new File(level0Folder, f.getName()); |
| 219 |
moveFileToTargetOrDelete(f, level0File); |
| 220 |
} |
| 221 |
// Move matching files to *VANILLA*/level0_Characters |
| 222 |
else if (f.getName().startsWith("ONCC") |
| 223 |
|| f.getName().startsWith("TRBS") |
| 224 |
|| f.getName().startsWith("ONCV") |
| 225 |
|| f.getName().startsWith("ONVL") |
| 226 |
|| f.getName().startsWith("TRMA") |
| 227 |
|| f.getName().startsWith("TRSC") |
| 228 |
|| f.getName().startsWith("TRAS")) { |
| 229 |
File level0File = new File(level0Folder, f.getName()); |
| 230 |
moveFileToTargetOrDelete(f, level0File); |
| 231 |
} |
| 232 |
// Move matching files to level0_Sounds |
| 233 |
else if (f.getName().startsWith("OSBD") |
| 234 |
|| f.getName().startsWith("SNDD")) { |
| 235 |
File level0File = new File(level0Folder, f.getName()); |
| 236 |
moveFileToTargetOrDelete(f, level0File); |
| 237 |
} |
| 238 |
// Move matching files to level0_Particles |
| 239 |
else if (f.getName().startsWith("BINA3") |
| 240 |
|| f.getName().startsWith("M3GMdebris") |
| 241 |
|| f.getName().equals("M3GMtoxic_bubble.oni") |
| 242 |
|| f.getName().startsWith("M3GMelec") |
| 243 |
|| f.getName().startsWith("M3GMrat") |
| 244 |
|| f.getName().startsWith("M3GMjet") |
| 245 |
|| f.getName().startsWith("M3GMbomb_") |
| 246 |
|| f.getName().equals("M3GMbarab_swave.oni") |
| 247 |
|| f.getName().equals("M3GMbloodyfoot.oni")) { |
| 248 |
File level0File = new File(level0Folder, f.getName()); |
| 249 |
moveFileToTargetOrDelete(f, level0File); |
| 250 |
} |
| 251 |
// Move matching files to Archive (aka delete them) |
| 252 |
else if (f.getName().startsWith("AGDB") |
| 253 |
|| f.getName().startsWith("TRCM")) { |
| 254 |
f.delete(); |
| 255 |
} |
| 256 |
// Move matching files to /level0_Final/ |
| 257 |
else if (f.getName().startsWith("ONWC")) { |
| 258 |
File level0File = new File(level0Folder, f.getName()); |
| 259 |
moveFileToTargetOrDelete(f, level0File); |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
} |