| 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.Scanner; | 
 
 
 
 
 | 8 |  | 
 
 
 
 
 | 9 | import net.oni2.aeinstaller.backend.Paths; | 
 
 
 
 
 | 10 | import net.oni2.aeinstaller.backend.Settings; | 
 
 
 
 
 | 11 | import net.oni2.aeinstaller.backend.Settings.Platform; | 
 
 
 
 
 | 12 |  | 
 
 
 
 
 | 13 | import org.apache.commons.io.FileUtils; | 
 
 
 
 
 | 14 |  | 
 
 
 
 
 | 15 | /** | 
 
 
 
 
 | 16 | * @author Christian Illy | 
 
 
 
 
 | 17 | */ | 
 
 
 
 
 | 18 | public class Installer { | 
 
 
 
 
 | 19 | /** | 
 
 
 
 
 | 20 | * @return Is Edition Core initialized | 
 
 
 
 
 | 21 | */ | 
 
 
 
 
 | 22 | public static boolean isEditionInitialized() { | 
 
 
 
 
 | 23 | return Paths.getVanillaOnisPath().exists(); | 
 
 
 
 
 | 24 | } | 
 
 
 
 
 | 25 |  | 
 
 
 
 
 | 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 | public static void initializeEdition() { | 
 
 
 
 
 | 36 | File init = new File(Paths.getTempPath(), "init"); | 
 
 
 
 
 | 37 | try { | 
 
 
 
 
 | 38 | createEmptyPath(Paths.getEditionGDF()); | 
 
 
 
 
 | 39 | createEmptyPath(Paths.getVanillaOnisPath()); | 
 
 
 
 
 | 40 | createEmptyPath(init); | 
 
 
 
 
 | 41 | File level0Folder = new File(init, "level0_Final"); | 
 
 
 
 
 | 42 | createEmptyPath(level0Folder); | 
 
 
 
 
 | 43 | File level0FolderVanilla = new File(Paths.getVanillaOnisPath(), | 
 
 
 
 
 | 44 | "level0_Final"); | 
 
 
 
 
 | 45 | createEmptyPath(level0FolderVanilla); | 
 
 
 
 
 | 46 | createEmptyPath(new File(level0FolderVanilla, "characters")); | 
 
 
 
 
 | 47 |  | 
 
 
 
 
 | 48 | for (File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() { | 
 
 
 
 
 | 49 | @Override | 
 
 
 
 
 | 50 | public boolean accept(File dir, String name) { | 
 
 
 
 
 | 51 | return name.endsWith(".dat"); | 
 
 
 
 
 | 52 | } | 
 
 
 
 
 | 53 | })) { | 
 
 
 
 
 | 54 | String levelName = f.getName().substring(0, | 
 
 
 
 
 | 55 | f.getName().indexOf('.')); | 
 
 
 
 
 | 56 | Scanner fi = new Scanner(levelName); | 
 
 
 
 
 | 57 | int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+")); | 
 
 
 
 
 | 58 |  | 
 
 
 
 
 | 59 | // Edition/GameDataFolder/level*_Final/ | 
 
 
 
 
 | 60 | File tempLevelFolder = new File(init, levelName); | 
 
 
 
 
 | 61 |  | 
 
 
 
 
 | 62 | // Export Vanilla-Level-Dat -> Temp/Level | 
 
 
 
 
 | 63 | OniSplit.export(tempLevelFolder, f); | 
 
 
 
 
 | 64 |  | 
 
 
 
 
 | 65 | handleFileGlobalisation(tempLevelFolder, level0Folder, | 
 
 
 
 
 | 66 | level0FolderVanilla, levelNumber); | 
 
 
 
 
 | 67 | } | 
 
 
 
 
 | 68 |  | 
 
 
 
 
 | 69 | for (File f : init.listFiles()) { | 
 
 
 
 
 | 70 | String levelName = f.getName(); | 
 
 
 
 
 | 71 |  | 
 
 
 
 
 | 72 | // Edition/AEInstaller/vanilla/level*_Final/ | 
 
 
 
 
 | 73 | File vanillaFolder = new File(Paths.getVanillaOnisPath(), | 
 
 
 
 
 | 74 | levelName); | 
 
 
 
 
 | 75 | vanillaFolder.mkdirs(); | 
 
 
 
 
 | 76 |  | 
 
 
 
 
 | 77 | OniSplit.importLevel(f, new File(vanillaFolder, levelName | 
 
 
 
 
 | 78 | + ".oni")); | 
 
 
 
 
 | 79 | } | 
 
 
 
 
 | 80 |  | 
 
 
 
 
 | 81 | // Copy Oni-configs | 
 
 
 
 
 | 82 | File persistVanilla = new File(Paths.getOniBasePath(), | 
 
 
 
 
 | 83 | "persist.dat"); | 
 
 
 
 
 | 84 | File persistEdition = new File(Paths.getEditionBasePath(), | 
 
 
 
 
 | 85 | "persist.dat"); | 
 
 
 
 
 | 86 | File keyConfVanilla = new File(Paths.getOniBasePath(), | 
 
 
 
 
 | 87 | "key_config.txt"); | 
 
 
 
 
 | 88 | File keyConfEdition = new File(Paths.getEditionBasePath(), | 
 
 
 
 
 | 89 | "key_config.txt"); | 
 
 
 
 
 | 90 | if (persistVanilla.exists() && !persistEdition.exists()) | 
 
 
 
 
 | 91 | FileUtils.copyFile(persistVanilla, persistEdition); | 
 
 
 
 
 | 92 | if (keyConfVanilla.exists() && !keyConfEdition.exists()) | 
 
 
 
 
 | 93 | FileUtils.copyFile(keyConfVanilla, keyConfEdition); | 
 
 
 
 
 | 94 |  | 
 
 
 
 
 | 95 | if (Settings.getPlatform() == Platform.MACOS) { | 
 
 
 
 
 | 96 | // TODO | 
 
 
 
 
 | 97 | // /* On Mac only, set the current GDF to the AE GDF by writing | 
 
 
 
 
 | 98 | // to Oni's global preferences file (thankfully a standard OS X | 
 
 
 
 
 | 99 | // ".plist" XML file). | 
 
 
 
 
 | 100 | // Tests for presence of prefs with [ -f ] before doing anything | 
 
 
 
 
 | 101 | // so it doesn't create a partial prefs file -- just in case | 
 
 
 
 
 | 102 | // user has never | 
 
 
 
 
 | 103 | // run Oni before :-p */ | 
 
 
 
 
 | 104 | // string fullAEpath = | 
 
 
 
 
 | 105 | // escapePath(system_complete(".").parent_path().parent_path().string()); | 
 
 
 
 
 | 106 | // // get full path for Edition/ (Oni wants folder that | 
 
 
 
 
 | 107 | // *contains* the GDF) | 
 
 
 
 
 | 108 | // string prefsCommand = | 
 
 
 
 
 | 109 | // "[ -f ~/Library/Preferences/com.godgames.oni.plist ] && defaults write com.godgames.oni RetailInstallationPath -string '" | 
 
 
 
 
 | 110 | // + fullAEpath + "'"; | 
 
 
 
 
 | 111 | // system(prefsCommand.c_str()); | 
 
 
 
 
 | 112 | } | 
 
 
 
 
 | 113 | // TODO: FileUtils.deleteDirectory(init); | 
 
 
 
 
 | 114 | } catch (IOException e) { | 
 
 
 
 
 | 115 | e.printStackTrace(); | 
 
 
 
 
 | 116 | } | 
 
 
 
 
 | 117 | } | 
 
 
 
 
 | 118 |  | 
 
 
 
 
 | 119 | private static void moveFileToTargetOrDelete(File source, File target) { | 
 
 
 
 
 | 120 | if (source.equals(target)) | 
 
 
 
 
 | 121 | return; | 
 
 
 
 
 | 122 | if (!target.exists()) { | 
 
 
 
 
 | 123 | if (!source.renameTo(target)) { | 
 
 
 
 
 | 124 | System.err.println("File " + source.getPath() + " not moved!"); | 
 
 
 
 
 | 125 | } | 
 
 
 
 
 | 126 | } else if (!source.delete()) { | 
 
 
 
 
 | 127 | System.err.println("File " + source.getPath() + " not deleted!"); | 
 
 
 
 
 | 128 | } | 
 
 
 
 
 | 129 | } | 
 
 
 
 
 | 130 |  | 
 
 
 
 
 | 131 | private static void handleFileGlobalisation(File tempFolder, | 
 
 
 
 
 | 132 | File level0Folder, File level0FolderVanilla, int levelNumber) { | 
 
 
 
 
 | 133 | // Move AKEV and related files to subfolder so they're not globalized: | 
 
 
 
 
 | 134 | if (levelNumber != 0) { | 
 
 
 
 
 | 135 | File akevFolder = new File(tempFolder, "AKEV"); | 
 
 
 
 
 | 136 | akevFolder.mkdir(); | 
 
 
 
 
 | 137 | OniSplit.move(akevFolder, tempFolder.getPath() + "/AKEV*.oni", | 
 
 
 
 
 | 138 | "overwrite"); | 
 
 
 
 
 | 139 | } | 
 
 
 
 
 | 140 |  | 
 
 
 
 
 | 141 | for (File f : tempFolder.listFiles(new FileFilter() { | 
 
 
 
 
 | 142 | @Override | 
 
 
 
 
 | 143 | public boolean accept(File pathname) { | 
 
 
 
 
 | 144 | return pathname.isFile(); | 
 
 
 
 
 | 145 | } | 
 
 
 
 
 | 146 | })) { | 
 
 
 
 
 | 147 | // Move matching files to subfolder NoGlobal: | 
 
 
 
 
 | 148 | if (f.getName().startsWith("TXMPfail") | 
 
 
 
 
 | 149 | || f.getName().startsWith("TXMPlevel") | 
 
 
 
 
 | 150 | || (f.getName().startsWith("TXMP") && f.getName().contains( | 
 
 
 
 
 | 151 | "intro")) | 
 
 
 
 
 | 152 | || f.getName().startsWith("TXMB") | 
 
 
 
 
 | 153 | || f.getName().equals("M3GMpowerup_lsi.oni") | 
 
 
 
 
 | 154 | || f.getName().equals("TXMPlsi_icon.oni") | 
 
 
 
 
 | 155 | || (f.getName().startsWith("TXMB") && f.getName().contains( | 
 
 
 
 
 | 156 | "splash_screen.oni"))) { | 
 
 
 
 
 | 157 | File noGlobal = new File(tempFolder, "NoGlobal"); | 
 
 
 
 
 | 158 | noGlobal.mkdir(); | 
 
 
 
 
 | 159 | File noGlobalFile = new File(noGlobal, f.getName()); | 
 
 
 
 
 | 160 | moveFileToTargetOrDelete(f, noGlobalFile); | 
 
 
 
 
 | 161 | } | 
 
 
 
 
 | 162 | // Move matching files to level0_Animations/level0_TRAC | 
 
 
 
 
 | 163 | else if (f.getName().startsWith("TRAC")) { | 
 
 
 
 
 | 164 | File level0File = new File(level0Folder, f.getName()); | 
 
 
 
 
 | 165 | moveFileToTargetOrDelete(f, level0File); | 
 
 
 
 
 | 166 | } | 
 
 
 
 
 | 167 | // Move matching files to level0_Animations/level0_TRAM | 
 
 
 
 
 | 168 | else if (f.getName().startsWith("TRAM")) { | 
 
 
 
 
 | 169 | File level0File = new File(level0Folder, f.getName()); | 
 
 
 
 
 | 170 | moveFileToTargetOrDelete(f, level0File); | 
 
 
 
 
 | 171 | } | 
 
 
 
 
 | 172 | // Move matching files to level0_Textures | 
 
 
 
 
 | 173 | else if (f.getName().startsWith("ONSK") | 
 
 
 
 
 | 174 | || f.getName().startsWith("TXMP")) { | 
 
 
 
 
 | 175 | // TODO ? new File(tempFolder, "TexFix").mkdir(); | 
 
 
 
 
 | 176 | File level0File = new File(level0Folder, f.getName()); | 
 
 
 
 
 | 177 | // TODO: delete non-moved? | 
 
 
 
 
 | 178 | if (!level0File.exists()) { | 
 
 
 
 
 | 179 | if (!f.renameTo(level0File)) { | 
 
 
 
 
 | 180 | System.err.println("File " + f.getName() | 
 
 
 
 
 | 181 | + " not moved!"); | 
 
 
 
 
 | 182 | } | 
 
 
 
 
 | 183 | } | 
 
 
 
 
 | 184 | } | 
 
 
 
 
 | 185 | // Move matching files to *VANILLA*/level0_Characters | 
 
 
 
 
 | 186 | else if (f.getName().startsWith("ONCC") | 
 
 
 
 
 | 187 | || f.getName().startsWith("TRBS") | 
 
 
 
 
 | 188 | || f.getName().startsWith("ONCV") | 
 
 
 
 
 | 189 | || f.getName().startsWith("ONVL") | 
 
 
 
 
 | 190 | || f.getName().startsWith("TRMA") | 
 
 
 
 
 | 191 | || f.getName().startsWith("TRSC") | 
 
 
 
 
 | 192 | || f.getName().startsWith("TRAS")) { | 
 
 
 
 
 | 193 | File level0FolderCharactersVanilla = new File( | 
 
 
 
 
 | 194 | level0FolderVanilla, "characters"); | 
 
 
 
 
 | 195 | File level0FileVanilla = new File( | 
 
 
 
 
 | 196 | level0FolderCharactersVanilla, f.getName()); | 
 
 
 
 
 | 197 | moveFileToTargetOrDelete(f, level0FileVanilla); | 
 
 
 
 
 | 198 | } | 
 
 
 
 
 | 199 | // Move matching files to level0_Sounds | 
 
 
 
 
 | 200 | else if (f.getName().startsWith("OSBD") | 
 
 
 
 
 | 201 | || f.getName().startsWith("SNDD")) { | 
 
 
 
 
 | 202 | File level0File = new File(level0Folder, f.getName()); | 
 
 
 
 
 | 203 | moveFileToTargetOrDelete(f, level0File); | 
 
 
 
 
 | 204 | } | 
 
 
 
 
 | 205 | // Move matching files to level0_Particles | 
 
 
 
 
 | 206 | else if (f.getName().startsWith("BINA3") | 
 
 
 
 
 | 207 | || f.getName().startsWith("M3GMdebris") | 
 
 
 
 
 | 208 | || f.getName().equals("M3GMtoxic_bubble.oni") | 
 
 
 
 
 | 209 | || f.getName().startsWith("M3GMelec") | 
 
 
 
 
 | 210 | || f.getName().startsWith("M3GMrat") | 
 
 
 
 
 | 211 | || f.getName().startsWith("M3GMjet") | 
 
 
 
 
 | 212 | || f.getName().startsWith("M3GMbomb_") | 
 
 
 
 
 | 213 | || f.getName().equals("M3GMbarab_swave.oni") | 
 
 
 
 
 | 214 | || f.getName().equals("M3GMbloodyfoot.oni")) { | 
 
 
 
 
 | 215 | File level0File = new File(level0Folder, f.getName()); | 
 
 
 
 
 | 216 | moveFileToTargetOrDelete(f, level0File); | 
 
 
 
 
 | 217 | } | 
 
 
 
 
 | 218 | // Move matching files to Archive (aka delete them) | 
 
 
 
 
 | 219 | else if (f.getName().startsWith("AGDB") | 
 
 
 
 
 | 220 | || f.getName().startsWith("TRCM")) { | 
 
 
 
 
 | 221 | f.delete(); | 
 
 
 
 
 | 222 | } | 
 
 
 
 
 | 223 | // TODO: needed? ONWC to GDF instead? | 
 
 
 
 
 | 224 | // Move matching files to *VANILLADATS*/level0_Final/level0_Final/ | 
 
 
 
 
 | 225 | // fix for buggy ONWC overriding | 
 
 
 
 
 | 226 | else if (f.getName().startsWith("ONWC")) { | 
 
 
 
 
 | 227 | File level0FileVanilla = new File(level0FolderVanilla, | 
 
 
 
 
 | 228 | f.getName()); | 
 
 
 
 
 | 229 | moveFileToTargetOrDelete(f, level0FileVanilla); | 
 
 
 
 
 | 230 | } | 
 
 
 
 
 | 231 | } | 
 
 
 
 
 | 232 | } | 
 
 
 
 
 | 233 | } |