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

Last change on this file since 1112 was 1081, checked in by alloc, 7 years ago

AEI2 2.27

File size: 8.4 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.AEInstaller2;
14import net.oni2.aeinstaller.backend.Paths;
15import net.oni2.aeinstaller.backend.oni.OniSplit;
16import net.oni2.platformtools.applicationinvoker.ApplicationInvocationResult;
17
18import org.apache.commons.io.FileUtils;
19
20import com.paour.NaturalOrderComparator;
21
22/**
23 * @author Christian Illy
24 */
25public 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 fi.close();
87
88 log.println("\t" + levelName + ":");
89 log.println("\t\tExporting");
90 listener.installProgressUpdate(stepsDone, totalSteps,
91 AEInstaller2.globalBundle.getString("coreInitializer.exportVanilla").replaceAll("%1", String.valueOf(levelNumber)));
92
93 // Edition/GameDataFolder/level*_Final/
94 File tempLevelFolder = new File(init, levelName);
95
96 // Export Vanilla-Level-Dat -> Temp/Level
97 ApplicationInvocationResult res = OniSplit.export(
98 tempLevelFolder, f);
99 log.logAppOutput(res, true);
100
101 log.println("\t\tMoving files");
102 handleFileGlobalisation(tempLevelFolder, level0Folder,
103 levelNumber);
104 stepsDone++;
105 log.println();
106 }
107
108 log.println("Reimporting levels");
109 TreeSet<File> initFolders = new TreeSet<File>(
110 new NaturalOrderComparator());
111 for (File f : init.listFiles()) {
112 initFolders.add(f);
113 }
114
115 for (File f : initFolders) {
116 String levelName = f.getName();
117
118 log.println("\t" + levelName);
119 listener.installProgressUpdate(stepsDone, totalSteps,
120 AEInstaller2.globalBundle.getString("coreInitializer.globalize").replaceAll("%1", levelName));
121
122 Vector<File> folders = new Vector<File>();
123 folders.add(f);
124
125 ApplicationInvocationResult res = OniSplit
126 .importLevel(folders,
127 new File(Paths.getVanillaOnisPath(), levelName
128 + ".dat"));
129 log.logAppOutput(res, true);
130
131 log.println();
132 stepsDone++;
133 }
134
135 listener.installProgressUpdate(stepsDone, totalSteps,
136 AEInstaller2.globalBundle.getString("coreInitializer.copyBasicFiles"));
137 // Copy Oni-configs
138 File persistVanilla = new File(Paths.getOniBasePath(),
139 "persist.dat");
140 File persistEdition = new File(Paths.getEditionBasePath(),
141 "persist.dat");
142 File keyConfVanilla = new File(Paths.getOniBasePath(),
143 "key_config.txt");
144 File keyConfEdition = new File(Paths.getEditionBasePath(),
145 "key_config.txt");
146 if (persistVanilla.exists() && !persistEdition.exists())
147 FileUtils.copyFile(persistVanilla, persistEdition);
148 if (keyConfVanilla.exists() && !keyConfEdition.exists())
149 FileUtils.copyFile(keyConfVanilla, keyConfEdition);
150
151 FileUtils.deleteDirectory(init);
152
153 Date end = new Date();
154 log.println("Initialization ended at " + sdf.format(end));
155 log.println("Process took "
156 + ((end.getTime() - start.getTime()) / 1000) + " seconds");
157 log.close();
158 } catch (IOException e) {
159 e.printStackTrace();
160 }
161 }
162
163 private static void moveFileToTargetOrDelete(File source, File target) {
164 if (source.equals(target))
165 return;
166 if (!target.exists()) {
167 if (!source.renameTo(target)) {
168 System.err.println("File " + source.getPath() + " not moved!");
169 }
170 } else if (!source.delete()) {
171 System.err.println("File " + source.getPath() + " not deleted!");
172 }
173 }
174
175 private static void handleFileGlobalisation(File tempFolder,
176 File level0Folder, int levelNumber) {
177 // Move AKEV and related files to subfolder so they're not globalized:
178 if (levelNumber != 0) {
179 File akevFolder = new File(tempFolder, "AKEV");
180 akevFolder.mkdir();
181 OniSplit.move(akevFolder, tempFolder.getPath() + "/AKEV*.oni",
182 "overwrite");
183 }
184
185 for (File f : tempFolder.listFiles(new FileFilter() {
186 @Override
187 public boolean accept(File pathname) {
188 return pathname.isFile();
189 }
190 })) {
191 // Move matching files to subfolder NoGlobal:
192 if (f.getName().startsWith("TXMPfail")
193 || f.getName().startsWith("TXMPlevel")
194 || (f.getName().startsWith("TXMP") && f.getName().contains(
195 "intro"))
196 || f.getName().startsWith("TXMB")
197 || f.getName().equals("M3GMpowerup_lsi.oni")
198 || f.getName().equals("TXMPlsi_icon.oni")
199 || (f.getName().startsWith("TXMB") && f.getName().contains(
200 "splash_screen.oni"))) {
201 File noGlobal = new File(tempFolder, "NoGlobal");
202 noGlobal.mkdir();
203 File noGlobalFile = new File(noGlobal, f.getName());
204 moveFileToTargetOrDelete(f, noGlobalFile);
205 }
206 // Move matching files to level0_Animations/level0_TRAC
207 else if (f.getName().startsWith("TRAC")) {
208 File level0File = new File(level0Folder, f.getName());
209 moveFileToTargetOrDelete(f, level0File);
210 }
211 // Move matching files to level0_Animations/level0_TRAM
212 else if (f.getName().startsWith("TRAM")) {
213 File level0File = new File(level0Folder, f.getName());
214 moveFileToTargetOrDelete(f, level0File);
215 }
216 // Move matching files to level0_Textures
217 else if (f.getName().startsWith("ONSK")
218 || f.getName().startsWith("TXMP")) {
219 File level0File = new File(level0Folder, f.getName());
220 moveFileToTargetOrDelete(f, level0File);
221 }
222 // Move matching files to *VANILLA*/level0_Characters
223 else if (f.getName().startsWith("ONCC")
224 || f.getName().startsWith("TRBS")
225 || f.getName().startsWith("ONCV")
226 || f.getName().startsWith("ONVL")
227 || f.getName().startsWith("TRMA")
228 || f.getName().startsWith("TRSC")
229 || f.getName().startsWith("TRAS")) {
230 File level0File = new File(level0Folder, f.getName());
231 moveFileToTargetOrDelete(f, level0File);
232 }
233 // Move matching files to level0_Sounds
234 else if (f.getName().startsWith("OSBD")
235 || f.getName().startsWith("SNDD")) {
236 File level0File = new File(level0Folder, f.getName());
237 moveFileToTargetOrDelete(f, level0File);
238 }
239 // Move matching files to level0_Particles
240 else if (f.getName().startsWith("BINA3")
241 || f.getName().startsWith("M3GMdebris")
242 || f.getName().equals("M3GMtoxic_bubble.oni")
243 || f.getName().startsWith("M3GMelec")
244 || f.getName().startsWith("M3GMrat")
245 || f.getName().startsWith("M3GMjet")
246 || f.getName().startsWith("M3GMbomb_")
247 || f.getName().equals("M3GMbarab_swave.oni")
248 || f.getName().equals("M3GMbloodyfoot.oni")) {
249 File level0File = new File(level0Folder, f.getName());
250 moveFileToTargetOrDelete(f, level0File);
251 }
252 // Move matching files to Archive (aka delete them)
253 else if (f.getName().startsWith("AGDB")
254 || f.getName().startsWith("TRCM")) {
255 f.delete();
256 }
257 // Move matching files to /level0_Final/
258 else if (f.getName().startsWith("ONWC")) {
259 File level0File = new File(level0Folder, f.getName());
260 moveFileToTargetOrDelete(f, level0File);
261 }
262 }
263 }
264
265}
Note: See TracBrowser for help on using the repository browser.