| 1 | package net.oni2.aeinstaller.backend.oni;
 | 
|---|
| 2 | 
 | 
|---|
| 3 | import java.io.File;
 | 
|---|
| 4 | import java.io.FileFilter;
 | 
|---|
| 5 | import java.io.FilenameFilter;
 | 
|---|
| 6 | import java.io.IOException;
 | 
|---|
| 7 | import java.util.HashMap;
 | 
|---|
| 8 | import java.util.List;
 | 
|---|
| 9 | import java.util.Scanner;
 | 
|---|
| 10 | import java.util.TreeSet;
 | 
|---|
| 11 | import java.util.Vector;
 | 
|---|
| 12 | 
 | 
|---|
| 13 | import net.oni2.aeinstaller.backend.Paths;
 | 
|---|
| 14 | import net.oni2.aeinstaller.backend.Settings;
 | 
|---|
| 15 | import net.oni2.aeinstaller.backend.Settings.Platform;
 | 
|---|
| 16 | import net.oni2.aeinstaller.backend.mods.Mod;
 | 
|---|
| 17 | 
 | 
|---|
| 18 | import org.apache.commons.io.FileUtils;
 | 
|---|
| 19 | 
 | 
|---|
| 20 | /**
 | 
|---|
| 21 |  * @author Christian Illy
 | 
|---|
| 22 |  */
 | 
|---|
| 23 | public class Installer {
 | 
|---|
| 24 |         /**
 | 
|---|
| 25 |          * @return Is Edition Core initialized
 | 
|---|
| 26 |          */
 | 
|---|
| 27 |         public static boolean isEditionInitialized() {
 | 
|---|
| 28 |                 return Paths.getVanillaOnisPath().exists();
 | 
|---|
| 29 |         }
 | 
|---|
| 30 | 
 | 
|---|
| 31 |         private static void createEmptyPath(File path) throws IOException {
 | 
|---|
| 32 |                 if (path.exists())
 | 
|---|
| 33 |                         FileUtils.deleteDirectory(path);
 | 
|---|
| 34 |                 path.mkdirs();
 | 
|---|
| 35 |         }
 | 
|---|
| 36 | 
 | 
|---|
| 37 |         public static void install(TreeSet<Mod> mods) {
 | 
|---|
| 38 |                 Vector<File> folders = new Vector<File>();
 | 
|---|
| 39 |                 folders.add(Paths.getVanillaOnisPath());
 | 
|---|
| 40 | 
 | 
|---|
| 41 |                 for (Mod m : mods) {
 | 
|---|
| 42 |                         File oni = new File(m.getLocalPath(), "oni");
 | 
|---|
| 43 |                         if (oni.exists()) {
 | 
|---|
| 44 |                                 if (m.hasSeparatePlatformDirs()) {
 | 
|---|
| 45 |                                         File oniCommon = new File(oni, "common");
 | 
|---|
| 46 |                                         File oniMac = new File(oni, "mac_only");
 | 
|---|
| 47 |                                         File oniWin = new File(oni, "win_only");
 | 
|---|
| 48 |                                         if (oniCommon.exists())
 | 
|---|
| 49 |                                                 folders.add(oniCommon);
 | 
|---|
| 50 |                                         if (Settings.getPlatform() == Platform.MACOS
 | 
|---|
| 51 |                                                         && oniMac.exists())
 | 
|---|
| 52 |                                                 folders.add(oniMac);
 | 
|---|
| 53 |                                         else if (oniWin.exists())
 | 
|---|
| 54 |                                                 folders.add(oniWin);
 | 
|---|
| 55 |                                 } else {
 | 
|---|
| 56 |                                         folders.add(oni);
 | 
|---|
| 57 |                                 }
 | 
|---|
| 58 |                         }
 | 
|---|
| 59 |                 }
 | 
|---|
| 60 | 
 | 
|---|
| 61 |                 for (File f : Paths.getModsPath().listFiles()) {
 | 
|---|
| 62 |                         File oni = new File(f, "oni");
 | 
|---|
| 63 |                         if (oni.exists())
 | 
|---|
| 64 |                                 folders.add(oni);
 | 
|---|
| 65 |                 }
 | 
|---|
| 66 |                 combineBinaryFiles(folders);
 | 
|---|
| 67 | 
 | 
|---|
| 68 |                 // TODO: bsl()
 | 
|---|
| 69 |         }
 | 
|---|
| 70 | 
 | 
|---|
| 71 |         private static void combineBinaryFiles(List<File> srcFolders) {
 | 
|---|
| 72 |                 try {
 | 
|---|
| 73 |                         createEmptyPath(Paths.getEditionGDF());
 | 
|---|
| 74 |                         HashMap<String, Vector<File>> levels = new HashMap<String, Vector<File>>();
 | 
|---|
| 75 | 
 | 
|---|
| 76 |                         for (File path : srcFolders) {
 | 
|---|
| 77 |                                 for (File levelF : path.listFiles()) {
 | 
|---|
| 78 |                                         if (!levels.containsKey(levelF.getName().toLowerCase()))
 | 
|---|
| 79 |                                                 levels.put(levelF.getName().toLowerCase(),
 | 
|---|
| 80 |                                                                 new Vector<File>());
 | 
|---|
| 81 |                                         levels.get(levelF.getName().toLowerCase()).add(levelF);
 | 
|---|
| 82 |                                 }
 | 
|---|
| 83 |                         }
 | 
|---|
| 84 | 
 | 
|---|
| 85 |                         for (String l : levels.keySet()) {
 | 
|---|
| 86 |                                 System.out.println("Level " + l);
 | 
|---|
| 87 |                                 for (File f : levels.get(l)) {
 | 
|---|
| 88 |                                         System.out.println("    " + f.getPath());
 | 
|---|
| 89 | 
 | 
|---|
| 90 |                                 }
 | 
|---|
| 91 | 
 | 
|---|
| 92 |                                 OniSplit.importLevel(levels.get(l),
 | 
|---|
| 93 |                                                 new File(Paths.getEditionGDF(), l + ".dat"));
 | 
|---|
| 94 | 
 | 
|---|
| 95 |                                 System.out.println();
 | 
|---|
| 96 |                         }
 | 
|---|
| 97 |                 } catch (IOException e) {
 | 
|---|
| 98 |                         // TODO Auto-generated catch block
 | 
|---|
| 99 |                         e.printStackTrace();
 | 
|---|
| 100 |                 }
 | 
|---|
| 101 |         }
 | 
|---|
| 102 | 
 | 
|---|
| 103 |         /**
 | 
|---|
| 104 |          * Initializes the Edition core
 | 
|---|
| 105 |          */
 | 
|---|
| 106 |         public static void initializeEdition() {
 | 
|---|
| 107 |                 File init = new File(Paths.getTempPath(), "init");
 | 
|---|
| 108 |                 try {
 | 
|---|
| 109 |                         createEmptyPath(Paths.getVanillaOnisPath());
 | 
|---|
| 110 |                         createEmptyPath(init);
 | 
|---|
| 111 |                         File level0Folder = new File(init, "level0_Final");
 | 
|---|
| 112 |                         createEmptyPath(level0Folder);
 | 
|---|
| 113 |                         File level0FolderVanilla = new File(Paths.getVanillaOnisPath(),
 | 
|---|
| 114 |                                         "level0_Final");
 | 
|---|
| 115 |                         createEmptyPath(level0FolderVanilla);
 | 
|---|
| 116 |                         createEmptyPath(new File(level0FolderVanilla, "characters"));
 | 
|---|
| 117 | 
 | 
|---|
| 118 |                         for (File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() {
 | 
|---|
| 119 |                                 @Override
 | 
|---|
| 120 |                                 public boolean accept(File dir, String name) {
 | 
|---|
| 121 |                                         return name.endsWith(".dat");
 | 
|---|
| 122 |                                 }
 | 
|---|
| 123 |                         })) {
 | 
|---|
| 124 |                                 String levelName = f.getName().substring(0,
 | 
|---|
| 125 |                                                 f.getName().indexOf('.'));
 | 
|---|
| 126 |                                 Scanner fi = new Scanner(levelName);
 | 
|---|
| 127 |                                 int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+"));
 | 
|---|
| 128 | 
 | 
|---|
| 129 |                                 // Edition/GameDataFolder/level*_Final/
 | 
|---|
| 130 |                                 File tempLevelFolder = new File(init, levelName);
 | 
|---|
| 131 | 
 | 
|---|
| 132 |                                 // Export Vanilla-Level-Dat -> Temp/Level
 | 
|---|
| 133 |                                 OniSplit.export(tempLevelFolder, f);
 | 
|---|
| 134 | 
 | 
|---|
| 135 |                                 handleFileGlobalisation(tempLevelFolder, level0Folder,
 | 
|---|
| 136 |                                                 level0FolderVanilla, levelNumber);
 | 
|---|
| 137 |                         }
 | 
|---|
| 138 | 
 | 
|---|
| 139 |                         for (File f : init.listFiles()) {
 | 
|---|
| 140 |                                 String levelName = f.getName();
 | 
|---|
| 141 | 
 | 
|---|
| 142 |                                 // Edition/AEInstaller/vanilla/level*_Final/
 | 
|---|
| 143 |                                 File vanillaFolder = new File(Paths.getVanillaOnisPath(),
 | 
|---|
| 144 |                                                 levelName);
 | 
|---|
| 145 |                                 vanillaFolder.mkdirs();
 | 
|---|
| 146 | 
 | 
|---|
| 147 |                                 Vector<File> folders = new Vector<File>();
 | 
|---|
| 148 |                                 folders.add(f);
 | 
|---|
| 149 | 
 | 
|---|
| 150 |                                 OniSplit.importLevel(folders, new File(vanillaFolder, levelName
 | 
|---|
| 151 |                                                 + ".oni"));
 | 
|---|
| 152 |                         }
 | 
|---|
| 153 | 
 | 
|---|
| 154 |                         // Copy Oni-configs
 | 
|---|
| 155 |                         File persistVanilla = new File(Paths.getOniBasePath(),
 | 
|---|
| 156 |                                         "persist.dat");
 | 
|---|
| 157 |                         File persistEdition = new File(Paths.getEditionBasePath(),
 | 
|---|
| 158 |                                         "persist.dat");
 | 
|---|
| 159 |                         File keyConfVanilla = new File(Paths.getOniBasePath(),
 | 
|---|
| 160 |                                         "key_config.txt");
 | 
|---|
| 161 |                         File keyConfEdition = new File(Paths.getEditionBasePath(),
 | 
|---|
| 162 |                                         "key_config.txt");
 | 
|---|
| 163 |                         if (persistVanilla.exists() && !persistEdition.exists())
 | 
|---|
| 164 |                                 FileUtils.copyFile(persistVanilla, persistEdition);
 | 
|---|
| 165 |                         if (keyConfVanilla.exists() && !keyConfEdition.exists())
 | 
|---|
| 166 |                                 FileUtils.copyFile(keyConfVanilla, keyConfEdition);
 | 
|---|
| 167 | 
 | 
|---|
| 168 |                         // TODO: FileUtils.deleteDirectory(init);
 | 
|---|
| 169 |                 } catch (IOException e) {
 | 
|---|
| 170 |                         e.printStackTrace();
 | 
|---|
| 171 |                 }
 | 
|---|
| 172 |         }
 | 
|---|
| 173 | 
 | 
|---|
| 174 |         private static void moveFileToTargetOrDelete(File source, File target) {
 | 
|---|
| 175 |                 if (source.equals(target))
 | 
|---|
| 176 |                         return;
 | 
|---|
| 177 |                 if (!target.exists()) {
 | 
|---|
| 178 |                         if (!source.renameTo(target)) {
 | 
|---|
| 179 |                                 System.err.println("File " + source.getPath() + " not moved!");
 | 
|---|
| 180 |                         }
 | 
|---|
| 181 |                 } else if (!source.delete()) {
 | 
|---|
| 182 |                         System.err.println("File " + source.getPath() + " not deleted!");
 | 
|---|
| 183 |                 }
 | 
|---|
| 184 |         }
 | 
|---|
| 185 | 
 | 
|---|
| 186 |         private static void handleFileGlobalisation(File tempFolder,
 | 
|---|
| 187 |                         File level0Folder, File level0FolderVanilla, int levelNumber) {
 | 
|---|
| 188 |                 // Move AKEV and related files to subfolder so they're not globalized:
 | 
|---|
| 189 |                 if (levelNumber != 0) {
 | 
|---|
| 190 |                         File akevFolder = new File(tempFolder, "AKEV");
 | 
|---|
| 191 |                         akevFolder.mkdir();
 | 
|---|
| 192 |                         OniSplit.move(akevFolder, tempFolder.getPath() + "/AKEV*.oni",
 | 
|---|
| 193 |                                         "overwrite");
 | 
|---|
| 194 |                 }
 | 
|---|
| 195 | 
 | 
|---|
| 196 |                 for (File f : tempFolder.listFiles(new FileFilter() {
 | 
|---|
| 197 |                         @Override
 | 
|---|
| 198 |                         public boolean accept(File pathname) {
 | 
|---|
| 199 |                                 return pathname.isFile();
 | 
|---|
| 200 |                         }
 | 
|---|
| 201 |                 })) {
 | 
|---|
| 202 |                         // Move matching files to subfolder NoGlobal:
 | 
|---|
| 203 |                         if (f.getName().startsWith("TXMPfail")
 | 
|---|
| 204 |                                         || f.getName().startsWith("TXMPlevel")
 | 
|---|
| 205 |                                         || (f.getName().startsWith("TXMP") && f.getName().contains(
 | 
|---|
| 206 |                                                         "intro"))
 | 
|---|
| 207 |                                         || f.getName().startsWith("TXMB")
 | 
|---|
| 208 |                                         || f.getName().equals("M3GMpowerup_lsi.oni")
 | 
|---|
| 209 |                                         || f.getName().equals("TXMPlsi_icon.oni")
 | 
|---|
| 210 |                                         || (f.getName().startsWith("TXMB") && f.getName().contains(
 | 
|---|
| 211 |                                                         "splash_screen.oni"))) {
 | 
|---|
| 212 |                                 File noGlobal = new File(tempFolder, "NoGlobal");
 | 
|---|
| 213 |                                 noGlobal.mkdir();
 | 
|---|
| 214 |                                 File noGlobalFile = new File(noGlobal, f.getName());
 | 
|---|
| 215 |                                 moveFileToTargetOrDelete(f, noGlobalFile);
 | 
|---|
| 216 |                         }
 | 
|---|
| 217 |                         // Move matching files to level0_Animations/level0_TRAC
 | 
|---|
| 218 |                         else if (f.getName().startsWith("TRAC")) {
 | 
|---|
| 219 |                                 File level0File = new File(level0Folder, f.getName());
 | 
|---|
| 220 |                                 moveFileToTargetOrDelete(f, level0File);
 | 
|---|
| 221 |                         }
 | 
|---|
| 222 |                         // Move matching files to level0_Animations/level0_TRAM
 | 
|---|
| 223 |                         else if (f.getName().startsWith("TRAM")) {
 | 
|---|
| 224 |                                 File level0File = new File(level0Folder, f.getName());
 | 
|---|
| 225 |                                 moveFileToTargetOrDelete(f, level0File);
 | 
|---|
| 226 |                         }
 | 
|---|
| 227 |                         // Move matching files to level0_Textures
 | 
|---|
| 228 |                         else if (f.getName().startsWith("ONSK")
 | 
|---|
| 229 |                                         || f.getName().startsWith("TXMP")) {
 | 
|---|
| 230 |                                 // TODO ? new File(tempFolder, "TexFix").mkdir();
 | 
|---|
| 231 |                                 File level0File = new File(level0Folder, f.getName());
 | 
|---|
| 232 |                                 moveFileToTargetOrDelete(f, level0File);
 | 
|---|
| 233 |                         }
 | 
|---|
| 234 |                         // Move matching files to *VANILLA*/level0_Characters
 | 
|---|
| 235 |                         else if (f.getName().startsWith("ONCC")
 | 
|---|
| 236 |                                         || f.getName().startsWith("TRBS")
 | 
|---|
| 237 |                                         || f.getName().startsWith("ONCV")
 | 
|---|
| 238 |                                         || f.getName().startsWith("ONVL")
 | 
|---|
| 239 |                                         || f.getName().startsWith("TRMA")
 | 
|---|
| 240 |                                         || f.getName().startsWith("TRSC")
 | 
|---|
| 241 |                                         || f.getName().startsWith("TRAS")) {
 | 
|---|
| 242 |                                 // File level0FolderCharactersVanilla = new File(
 | 
|---|
| 243 |                                 // level0FolderVanilla, "characters");
 | 
|---|
| 244 |                                 // File level0FileVanilla = new File(
 | 
|---|
| 245 |                                 // level0FolderCharactersVanilla, f.getName());
 | 
|---|
| 246 |                                 // moveFileToTargetOrDelete(f, level0FileVanilla);
 | 
|---|
| 247 |                                 // TODO: does this work?
 | 
|---|
| 248 |                                 File level0File = new File(level0Folder, f.getName());
 | 
|---|
| 249 |                                 moveFileToTargetOrDelete(f, level0File);
 | 
|---|
| 250 |                         }
 | 
|---|
| 251 |                         // Move matching files to level0_Sounds
 | 
|---|
| 252 |                         else if (f.getName().startsWith("OSBD")
 | 
|---|
| 253 |                                         || f.getName().startsWith("SNDD")) {
 | 
|---|
| 254 |                                 File level0File = new File(level0Folder, f.getName());
 | 
|---|
| 255 |                                 moveFileToTargetOrDelete(f, level0File);
 | 
|---|
| 256 |                         }
 | 
|---|
| 257 |                         // Move matching files to level0_Particles
 | 
|---|
| 258 |                         else if (f.getName().startsWith("BINA3")
 | 
|---|
| 259 |                                         || f.getName().startsWith("M3GMdebris")
 | 
|---|
| 260 |                                         || f.getName().equals("M3GMtoxic_bubble.oni")
 | 
|---|
| 261 |                                         || f.getName().startsWith("M3GMelec")
 | 
|---|
| 262 |                                         || f.getName().startsWith("M3GMrat")
 | 
|---|
| 263 |                                         || f.getName().startsWith("M3GMjet")
 | 
|---|
| 264 |                                         || f.getName().startsWith("M3GMbomb_")
 | 
|---|
| 265 |                                         || f.getName().equals("M3GMbarab_swave.oni")
 | 
|---|
| 266 |                                         || f.getName().equals("M3GMbloodyfoot.oni")) {
 | 
|---|
| 267 |                                 File level0File = new File(level0Folder, f.getName());
 | 
|---|
| 268 |                                 moveFileToTargetOrDelete(f, level0File);
 | 
|---|
| 269 |                         }
 | 
|---|
| 270 |                         // Move matching files to Archive (aka delete them)
 | 
|---|
| 271 |                         else if (f.getName().startsWith("AGDB")
 | 
|---|
| 272 |                                         || f.getName().startsWith("TRCM")) {
 | 
|---|
| 273 |                                 f.delete();
 | 
|---|
| 274 |                         }
 | 
|---|
| 275 |                         // TODO: needed? ONWC to GDF instead?
 | 
|---|
| 276 |                         // Move matching files to *VANILLADATS*/level0_Final/level0_Final/
 | 
|---|
| 277 |                         // fix for buggy ONWC overriding
 | 
|---|
| 278 |                         else if (f.getName().startsWith("ONWC")) {
 | 
|---|
| 279 |                                 // File level0FileVanilla = new File(level0FolderVanilla,
 | 
|---|
| 280 |                                 // f.getName());
 | 
|---|
| 281 |                                 // moveFileToTargetOrDelete(f, level0FileVanilla);
 | 
|---|
| 282 |                                 // TODO: does this work?
 | 
|---|
| 283 |                                 File level0File = new File(level0Folder, f.getName());
 | 
|---|
| 284 |                                 moveFileToTargetOrDelete(f, level0File);
 | 
|---|
| 285 |                         }
 | 
|---|
| 286 |                 }
 | 
|---|
| 287 |         }
 | 
|---|
| 288 | }
 | 
|---|