source: java/installer2/src/net/oni2/aeinstaller/backend/oni/management/Initializer.java@ 1016

Last change on this file since 1016 was 749, checked in by alloc, 12 years ago

AEI2 0.99x:

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