1 | package net.oni2.aeinstaller.backend.oni;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileFilter;
|
---|
5 | import java.io.FileNotFoundException;
|
---|
6 | import java.io.FilenameFilter;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.PrintWriter;
|
---|
9 | import java.text.SimpleDateFormat;
|
---|
10 | import java.util.Date;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Scanner;
|
---|
14 | import java.util.TreeSet;
|
---|
15 | import java.util.Vector;
|
---|
16 |
|
---|
17 | import net.oni2.aeinstaller.backend.Paths;
|
---|
18 | import net.oni2.aeinstaller.backend.Settings;
|
---|
19 | import net.oni2.aeinstaller.backend.Settings.Platform;
|
---|
20 | import net.oni2.aeinstaller.backend.mods.Mod;
|
---|
21 |
|
---|
22 | import org.apache.commons.io.FileUtils;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * @author Christian Illy
|
---|
26 | */
|
---|
27 | public class Installer {
|
---|
28 | /**
|
---|
29 | * @return Is Edition Core initialized
|
---|
30 | */
|
---|
31 | public static boolean isEditionInitialized() {
|
---|
32 | return Paths.getVanillaOnisPath().exists();
|
---|
33 | }
|
---|
34 |
|
---|
35 | private static void createEmptyPath(File path) throws IOException {
|
---|
36 | if (path.exists())
|
---|
37 | FileUtils.deleteDirectory(path);
|
---|
38 | path.mkdirs();
|
---|
39 | }
|
---|
40 |
|
---|
41 | public static void install(TreeSet<Mod> mods,
|
---|
42 | InstallProgressListener listener) {
|
---|
43 |
|
---|
44 | Vector<File> folders = new Vector<File>();
|
---|
45 | folders.add(Paths.getVanillaOnisPath());
|
---|
46 |
|
---|
47 | for (Mod m : mods) {
|
---|
48 | File oni = new File(m.getLocalPath(), "oni");
|
---|
49 | if (oni.exists()) {
|
---|
50 | if (m.hasSeparatePlatformDirs()) {
|
---|
51 | File oniCommon = new File(oni, "common");
|
---|
52 | File oniMac = new File(oni, "mac_only");
|
---|
53 | File oniWin = new File(oni, "win_only");
|
---|
54 | if (oniCommon.exists())
|
---|
55 | folders.add(oniCommon);
|
---|
56 | if (Settings.getPlatform() == Platform.MACOS
|
---|
57 | && oniMac.exists())
|
---|
58 | folders.add(oniMac);
|
---|
59 | else if (oniWin.exists())
|
---|
60 | folders.add(oniWin);
|
---|
61 | } else {
|
---|
62 | folders.add(oni);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | // for (File f : Paths.getModsPath().listFiles()) {
|
---|
68 | // File oni = new File(f, "oni");
|
---|
69 | // if (oni.exists())
|
---|
70 | // folders.add(oni);
|
---|
71 | // }
|
---|
72 | combineBinaryFiles(folders, listener);
|
---|
73 |
|
---|
74 | // TODO: bsl()
|
---|
75 | }
|
---|
76 |
|
---|
77 | private static void combineBinaryFiles(List<File> srcFoldersFiles,
|
---|
78 | InstallProgressListener listener) {
|
---|
79 | try {
|
---|
80 | HashMap<String, Vector<File>> levels = new HashMap<String, Vector<File>>();
|
---|
81 |
|
---|
82 | for (File path : srcFoldersFiles) {
|
---|
83 | for (File levelF : path.listFiles()) {
|
---|
84 | String fn = levelF.getName().toLowerCase();
|
---|
85 | String levelN = null;
|
---|
86 | if (levelF.isDirectory()) {
|
---|
87 | levelN = fn;
|
---|
88 | } else if (fn.endsWith(".dat")) {
|
---|
89 | levelN = fn.substring(0, fn.lastIndexOf('.'));
|
---|
90 | }
|
---|
91 | if (levelN != null) {
|
---|
92 | if (!levels.containsKey(levelN))
|
---|
93 | levels.put(levelN, new Vector<File>());
|
---|
94 | levels.get(levelN).add(levelF);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | int totalSteps = 0;
|
---|
100 | int stepsDone = 0;
|
---|
101 |
|
---|
102 | for (@SuppressWarnings("unused")
|
---|
103 | String s : levels.keySet())
|
---|
104 | totalSteps++;
|
---|
105 |
|
---|
106 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
---|
107 |
|
---|
108 | File logFile = new File(Paths.getInstallerPath(),
|
---|
109 | "Installation.log");
|
---|
110 | PrintWriter log = null;
|
---|
111 | try {
|
---|
112 | log = new PrintWriter(logFile);
|
---|
113 | } catch (FileNotFoundException e) {
|
---|
114 | e.printStackTrace();
|
---|
115 | }
|
---|
116 |
|
---|
117 | Date start = new Date();
|
---|
118 | log.println("Installation of mods started at " + sdf.format(start));
|
---|
119 |
|
---|
120 | log.println("Cleaning directories");
|
---|
121 | listener.installProgressUpdate(stepsDone, totalSteps,
|
---|
122 | "Cleaning up directories");
|
---|
123 |
|
---|
124 | createEmptyPath(Paths.getEditionGDF());
|
---|
125 |
|
---|
126 | log.println("Importing levels");
|
---|
127 | for (String l : levels.keySet()) {
|
---|
128 | log.println("\tLevel " + l);
|
---|
129 | for (File f : levels.get(l)) {
|
---|
130 | log.println("\t\t\t" + f.getPath());
|
---|
131 | }
|
---|
132 |
|
---|
133 | Vector<String> res = OniSplit.packLevel(levels.get(l),
|
---|
134 | new File(Paths.getEditionGDF(), l + ".dat"));
|
---|
135 | if (res != null && res.size() > 0) {
|
---|
136 | for (String s : res)
|
---|
137 | log.println("\t\t" + s);
|
---|
138 | }
|
---|
139 |
|
---|
140 | log.println();
|
---|
141 | }
|
---|
142 |
|
---|
143 | Date end = new Date();
|
---|
144 | log.println("Initialization ended at " + sdf.format(end));
|
---|
145 | log.println("Process took "
|
---|
146 | + ((end.getTime() - start.getTime()) / 1000) + " seconds");
|
---|
147 | log.close();
|
---|
148 | } catch (IOException e) {
|
---|
149 | // TODO Auto-generated catch block
|
---|
150 | e.printStackTrace();
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Initializes the Edition core
|
---|
156 | *
|
---|
157 | * @param listener
|
---|
158 | * Listener for status updates
|
---|
159 | */
|
---|
160 | public static void initializeEdition(InstallProgressListener listener) {
|
---|
161 | File init = new File(Paths.getTempPath(), "init");
|
---|
162 |
|
---|
163 | int totalSteps = 0;
|
---|
164 | int stepsDone = 0;
|
---|
165 |
|
---|
166 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
---|
167 |
|
---|
168 | for (@SuppressWarnings("unused")
|
---|
169 | File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() {
|
---|
170 | @Override
|
---|
171 | public boolean accept(File dir, String name) {
|
---|
172 | return name.endsWith(".dat");
|
---|
173 | }
|
---|
174 | })) {
|
---|
175 | totalSteps++;
|
---|
176 | }
|
---|
177 | totalSteps = totalSteps * 2 + 2;
|
---|
178 |
|
---|
179 | try {
|
---|
180 | File logFile = new File(Paths.getInstallerPath(),
|
---|
181 | "Initialization.log");
|
---|
182 | PrintWriter log = new PrintWriter(logFile);
|
---|
183 |
|
---|
184 | Date start = new Date();
|
---|
185 | log.println("Initialization of Edition core started at "
|
---|
186 | + sdf.format(start));
|
---|
187 | log.println("Cleaning directories");
|
---|
188 |
|
---|
189 | listener.installProgressUpdate(stepsDone, totalSteps,
|
---|
190 | "Cleaning up directories");
|
---|
191 | createEmptyPath(Paths.getVanillaOnisPath());
|
---|
192 | createEmptyPath(init);
|
---|
193 | File level0Folder = new File(init, "level0_Final");
|
---|
194 | createEmptyPath(level0Folder);
|
---|
195 |
|
---|
196 | stepsDone++;
|
---|
197 |
|
---|
198 | log.println("Exporting levels and moving files to level0");
|
---|
199 |
|
---|
200 | for (File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() {
|
---|
201 | @Override
|
---|
202 | public boolean accept(File dir, String name) {
|
---|
203 | return name.endsWith(".dat");
|
---|
204 | }
|
---|
205 | })) {
|
---|
206 | String levelName = f.getName().substring(0,
|
---|
207 | f.getName().indexOf('.'));
|
---|
208 | Scanner fi = new Scanner(levelName);
|
---|
209 | int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+"));
|
---|
210 |
|
---|
211 | log.println("\t" + levelName + ":");
|
---|
212 | log.println("\t\tExporting");
|
---|
213 | listener.installProgressUpdate(stepsDone, totalSteps,
|
---|
214 | "Exporting vanilla level " + levelNumber);
|
---|
215 |
|
---|
216 | // Edition/GameDataFolder/level*_Final/
|
---|
217 | File tempLevelFolder = new File(init, levelName);
|
---|
218 |
|
---|
219 | // Export Vanilla-Level-Dat -> Temp/Level
|
---|
220 | Vector<String> res = OniSplit.export(tempLevelFolder, f);
|
---|
221 | if (res != null && res.size() > 0) {
|
---|
222 | for (String s : res)
|
---|
223 | log.println("\t\t\t" + s);
|
---|
224 | }
|
---|
225 |
|
---|
226 | log.println("\t\tMoving files");
|
---|
227 | handleFileGlobalisation(tempLevelFolder, level0Folder,
|
---|
228 | levelNumber);
|
---|
229 | stepsDone++;
|
---|
230 | log.println();
|
---|
231 | }
|
---|
232 |
|
---|
233 | log.println("Reimporting levels");
|
---|
234 |
|
---|
235 | for (File f : init.listFiles()) {
|
---|
236 | String levelName = f.getName();
|
---|
237 |
|
---|
238 | log.println("\t" + levelName);
|
---|
239 | listener.installProgressUpdate(stepsDone, totalSteps,
|
---|
240 | "Creating globalized " + levelName);
|
---|
241 |
|
---|
242 | Vector<File> folders = new Vector<File>();
|
---|
243 | folders.add(f);
|
---|
244 |
|
---|
245 | Vector<String> res = OniSplit.importLevel(folders, new File(
|
---|
246 | Paths.getVanillaOnisPath(), levelName + ".dat"));
|
---|
247 | if (res != null && res.size() > 0) {
|
---|
248 | for (String s : res)
|
---|
249 | log.println("\t\t" + s);
|
---|
250 | }
|
---|
251 |
|
---|
252 | log.println();
|
---|
253 | stepsDone++;
|
---|
254 | }
|
---|
255 |
|
---|
256 | listener.installProgressUpdate(stepsDone, totalSteps,
|
---|
257 | "Copying basic files");
|
---|
258 | // Copy Oni-configs
|
---|
259 | File persistVanilla = new File(Paths.getOniBasePath(),
|
---|
260 | "persist.dat");
|
---|
261 | File persistEdition = new File(Paths.getEditionBasePath(),
|
---|
262 | "persist.dat");
|
---|
263 | File keyConfVanilla = new File(Paths.getOniBasePath(),
|
---|
264 | "key_config.txt");
|
---|
265 | File keyConfEdition = new File(Paths.getEditionBasePath(),
|
---|
266 | "key_config.txt");
|
---|
267 | if (persistVanilla.exists() && !persistEdition.exists())
|
---|
268 | FileUtils.copyFile(persistVanilla, persistEdition);
|
---|
269 | if (keyConfVanilla.exists() && !keyConfEdition.exists())
|
---|
270 | FileUtils.copyFile(keyConfVanilla, keyConfEdition);
|
---|
271 |
|
---|
272 | // TODO: FileUtils.deleteDirectory(init);
|
---|
273 | Date end = new Date();
|
---|
274 | log.println("Initialization ended at " + sdf.format(end));
|
---|
275 | log.println("Process took "
|
---|
276 | + ((end.getTime() - start.getTime()) / 1000) + " seconds");
|
---|
277 | log.close();
|
---|
278 | } catch (IOException e) {
|
---|
279 | e.printStackTrace();
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | private static void moveFileToTargetOrDelete(File source, File target) {
|
---|
284 | if (source.equals(target))
|
---|
285 | return;
|
---|
286 | if (!target.exists()) {
|
---|
287 | if (!source.renameTo(target)) {
|
---|
288 | System.err.println("File " + source.getPath() + " not moved!");
|
---|
289 | }
|
---|
290 | } else if (!source.delete()) {
|
---|
291 | System.err.println("File " + source.getPath() + " not deleted!");
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | private static void handleFileGlobalisation(File tempFolder,
|
---|
296 | File level0Folder, int levelNumber) {
|
---|
297 | // Move AKEV and related files to subfolder so they're not globalized:
|
---|
298 | if (levelNumber != 0) {
|
---|
299 | File akevFolder = new File(tempFolder, "AKEV");
|
---|
300 | akevFolder.mkdir();
|
---|
301 | OniSplit.move(akevFolder, tempFolder.getPath() + "/AKEV*.oni",
|
---|
302 | "overwrite");
|
---|
303 | }
|
---|
304 |
|
---|
305 | for (File f : tempFolder.listFiles(new FileFilter() {
|
---|
306 | @Override
|
---|
307 | public boolean accept(File pathname) {
|
---|
308 | return pathname.isFile();
|
---|
309 | }
|
---|
310 | })) {
|
---|
311 | // Move matching files to subfolder NoGlobal:
|
---|
312 | if (f.getName().startsWith("TXMPfail")
|
---|
313 | || f.getName().startsWith("TXMPlevel")
|
---|
314 | || (f.getName().startsWith("TXMP") && f.getName().contains(
|
---|
315 | "intro"))
|
---|
316 | || f.getName().startsWith("TXMB")
|
---|
317 | || f.getName().equals("M3GMpowerup_lsi.oni")
|
---|
318 | || f.getName().equals("TXMPlsi_icon.oni")
|
---|
319 | || (f.getName().startsWith("TXMB") && f.getName().contains(
|
---|
320 | "splash_screen.oni"))) {
|
---|
321 | File noGlobal = new File(tempFolder, "NoGlobal");
|
---|
322 | noGlobal.mkdir();
|
---|
323 | File noGlobalFile = new File(noGlobal, f.getName());
|
---|
324 | moveFileToTargetOrDelete(f, noGlobalFile);
|
---|
325 | }
|
---|
326 | // Move matching files to level0_Animations/level0_TRAC
|
---|
327 | else if (f.getName().startsWith("TRAC")) {
|
---|
328 | File level0File = new File(level0Folder, f.getName());
|
---|
329 | moveFileToTargetOrDelete(f, level0File);
|
---|
330 | }
|
---|
331 | // Move matching files to level0_Animations/level0_TRAM
|
---|
332 | else if (f.getName().startsWith("TRAM")) {
|
---|
333 | File level0File = new File(level0Folder, f.getName());
|
---|
334 | moveFileToTargetOrDelete(f, level0File);
|
---|
335 | }
|
---|
336 | // Move matching files to level0_Textures
|
---|
337 | else if (f.getName().startsWith("ONSK")
|
---|
338 | || f.getName().startsWith("TXMP")) {
|
---|
339 | File level0File = new File(level0Folder, f.getName());
|
---|
340 | moveFileToTargetOrDelete(f, level0File);
|
---|
341 | }
|
---|
342 | // Move matching files to *VANILLA*/level0_Characters
|
---|
343 | else if (f.getName().startsWith("ONCC")
|
---|
344 | || f.getName().startsWith("TRBS")
|
---|
345 | || f.getName().startsWith("ONCV")
|
---|
346 | || f.getName().startsWith("ONVL")
|
---|
347 | || f.getName().startsWith("TRMA")
|
---|
348 | || f.getName().startsWith("TRSC")
|
---|
349 | || f.getName().startsWith("TRAS")) {
|
---|
350 | File level0File = new File(level0Folder, f.getName());
|
---|
351 | moveFileToTargetOrDelete(f, level0File);
|
---|
352 | }
|
---|
353 | // Move matching files to level0_Sounds
|
---|
354 | else if (f.getName().startsWith("OSBD")
|
---|
355 | || f.getName().startsWith("SNDD")) {
|
---|
356 | File level0File = new File(level0Folder, f.getName());
|
---|
357 | moveFileToTargetOrDelete(f, level0File);
|
---|
358 | }
|
---|
359 | // Move matching files to level0_Particles
|
---|
360 | else if (f.getName().startsWith("BINA3")
|
---|
361 | || f.getName().startsWith("M3GMdebris")
|
---|
362 | || f.getName().equals("M3GMtoxic_bubble.oni")
|
---|
363 | || f.getName().startsWith("M3GMelec")
|
---|
364 | || f.getName().startsWith("M3GMrat")
|
---|
365 | || f.getName().startsWith("M3GMjet")
|
---|
366 | || f.getName().startsWith("M3GMbomb_")
|
---|
367 | || f.getName().equals("M3GMbarab_swave.oni")
|
---|
368 | || f.getName().equals("M3GMbloodyfoot.oni")) {
|
---|
369 | File level0File = new File(level0Folder, f.getName());
|
---|
370 | moveFileToTargetOrDelete(f, level0File);
|
---|
371 | }
|
---|
372 | // Move matching files to Archive (aka delete them)
|
---|
373 | else if (f.getName().startsWith("AGDB")
|
---|
374 | || f.getName().startsWith("TRCM")) {
|
---|
375 | f.delete();
|
---|
376 | }
|
---|
377 | // Move matching files to /level0_Final/
|
---|
378 | else if (f.getName().startsWith("ONWC")) {
|
---|
379 | File level0File = new File(level0Folder, f.getName());
|
---|
380 | moveFileToTargetOrDelete(f, level0File);
|
---|
381 | }
|
---|
382 | }
|
---|
383 | }
|
---|
384 | }
|
---|