1 |
package net.oni2.aeinstaller.backend.oni; |
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileFilter; |
5 |
import java.io.FileInputStream; |
6 |
import java.io.FileNotFoundException; |
7 |
import java.io.FileOutputStream; |
8 |
import java.io.FilenameFilter; |
9 |
import java.io.IOException; |
10 |
import java.io.InputStream; |
11 |
import java.io.PrintWriter; |
12 |
import java.text.SimpleDateFormat; |
13 |
import java.util.Date; |
14 |
import java.util.HashMap; |
15 |
import java.util.HashSet; |
16 |
import java.util.List; |
17 |
import java.util.Scanner; |
18 |
import java.util.TreeMap; |
19 |
import java.util.TreeSet; |
20 |
import java.util.Vector; |
21 |
import java.util.regex.Pattern; |
22 |
|
23 |
import net.oni2.SettingsManager; |
24 |
import net.oni2.aeinstaller.AEInstaller2; |
25 |
import net.oni2.aeinstaller.backend.CaseInsensitiveFile; |
26 |
import net.oni2.aeinstaller.backend.Paths; |
27 |
import net.oni2.aeinstaller.backend.packages.EBSLInstallType; |
28 |
import net.oni2.aeinstaller.backend.packages.Package; |
29 |
import net.oni2.aeinstaller.backend.packages.PackageManager; |
30 |
import net.oni2.platformtools.PlatformInformation; |
31 |
import net.oni2.platformtools.PlatformInformation.Platform; |
32 |
import net.oni2.platformtools.applicationinvoker.ApplicationInvocationResult; |
33 |
|
34 |
import org.apache.commons.io.FileUtils; |
35 |
import org.apache.commons.io.filefilter.RegexFileFilter; |
36 |
import org.apache.commons.io.filefilter.TrueFileFilter; |
37 |
import org.javabuilders.swing.SwingJavaBuilder; |
38 |
|
39 |
import com.thoughtworks.xstream.XStream; |
40 |
import com.thoughtworks.xstream.io.xml.StaxDriver; |
41 |
|
42 |
/** |
43 |
* @author Christian Illy |
44 |
*/ |
45 |
public class Installer { |
46 |
private static FileFilter dirFileFilter = new FileFilter() { |
47 |
@Override |
48 |
public boolean accept(File pathname) { |
49 |
return pathname.isDirectory(); |
50 |
} |
51 |
}; |
52 |
|
53 |
/** |
54 |
* @return Is Edition Core initialized |
55 |
*/ |
56 |
public static boolean isEditionInitialized() { |
57 |
return Paths.getVanillaOnisPath().exists(); |
58 |
} |
59 |
|
60 |
private static void createEmptyPath(File path) throws IOException { |
61 |
if (path.exists()) |
62 |
FileUtils.deleteDirectory(path); |
63 |
path.mkdirs(); |
64 |
} |
65 |
|
66 |
/** |
67 |
* @return list of currently installed mods |
68 |
*/ |
69 |
public static Vector<Integer> getInstalledMods() { |
70 |
File installCfg = new File(Paths.getEditionGDF(), "installed_mods.xml"); |
71 |
return PackageManager.getInstance().loadModSelection(installCfg); |
72 |
} |
73 |
|
74 |
/** |
75 |
* @return Currently installed tools |
76 |
*/ |
77 |
@SuppressWarnings("unchecked") |
78 |
public static TreeSet<Integer> getInstalledTools() { |
79 |
File installCfg = new File(Paths.getInstallerPath(), |
80 |
"installed_tools.xml"); |
81 |
TreeSet<Integer> res = new TreeSet<Integer>(); |
82 |
try { |
83 |
if (installCfg.exists()) { |
84 |
FileInputStream fis = new FileInputStream(installCfg); |
85 |
XStream xs = new XStream(new StaxDriver()); |
86 |
Object obj = xs.fromXML(fis); |
87 |
if (obj instanceof TreeSet<?>) |
88 |
res = (TreeSet<Integer>) obj; |
89 |
fis.close(); |
90 |
} |
91 |
} catch (FileNotFoundException e) { |
92 |
e.printStackTrace(); |
93 |
} catch (IOException e) { |
94 |
e.printStackTrace(); |
95 |
} |
96 |
return res; |
97 |
} |
98 |
|
99 |
private static void writeInstalledTools(TreeSet<Integer> tools) { |
100 |
File installCfg = new File(Paths.getInstallerPath(), |
101 |
"installed_tools.xml"); |
102 |
try { |
103 |
FileOutputStream fos = new FileOutputStream(installCfg); |
104 |
XStream xs = new XStream(new StaxDriver()); |
105 |
xs.toXML(tools, fos); |
106 |
fos.close(); |
107 |
} catch (FileNotFoundException e) { |
108 |
e.printStackTrace(); |
109 |
} catch (IOException e) { |
110 |
e.printStackTrace(); |
111 |
} |
112 |
} |
113 |
|
114 |
/** |
115 |
* @param tools |
116 |
* Tools to (un)install |
117 |
* @param uninstall |
118 |
* Uninstall tools or install? |
119 |
*/ |
120 |
public static void installTools(TreeSet<Package> tools, boolean uninstall) { |
121 |
TreeSet<Integer> installed = getInstalledTools(); |
122 |
for (Package m : tools) { |
123 |
if (!uninstall || installed.contains(m.getPackageNumber())) { |
124 |
File plain = CaseInsensitiveFile.getCaseInsensitiveFile( |
125 |
m.getLocalPath(), "plain"); |
126 |
if (plain.exists()) { |
127 |
if (m.hasSeparatePlatformDirs()) { |
128 |
File plainCommon = CaseInsensitiveFile |
129 |
.getCaseInsensitiveFile(plain, "common"); |
130 |
File plainMac = CaseInsensitiveFile |
131 |
.getCaseInsensitiveFile(plain, "mac_only"); |
132 |
File plainWin = CaseInsensitiveFile |
133 |
.getCaseInsensitiveFile(plain, "win_only"); |
134 |
if (plainCommon.exists()) |
135 |
copyRemoveToolsFiles(plainCommon, |
136 |
Paths.getEditionBasePath(), uninstall); |
137 |
if (PlatformInformation.getPlatform() == Platform.MACOS |
138 |
&& plainMac.exists()) |
139 |
copyRemoveToolsFiles(plainMac, |
140 |
Paths.getEditionBasePath(), uninstall); |
141 |
else if (plainWin.exists()) |
142 |
copyRemoveToolsFiles(plainWin, |
143 |
Paths.getEditionBasePath(), uninstall); |
144 |
} else { |
145 |
copyRemoveToolsFiles(plain, Paths.getEditionBasePath(), |
146 |
uninstall); |
147 |
} |
148 |
} |
149 |
} |
150 |
if (uninstall) |
151 |
installed.remove(m.getPackageNumber()); |
152 |
else |
153 |
installed.add(m.getPackageNumber()); |
154 |
} |
155 |
writeInstalledTools(installed); |
156 |
} |
157 |
|
158 |
private static void copyRemoveToolsFiles(File srcFolder, File targetFolder, |
159 |
boolean remove) { |
160 |
for (File f : srcFolder.listFiles()) { |
161 |
try { |
162 |
if (f.isDirectory()) |
163 |
copyRemoveToolsFiles(f, |
164 |
CaseInsensitiveFile.getCaseInsensitiveFile( |
165 |
targetFolder, f.getName()), remove); |
166 |
else { |
167 |
File targetFile = CaseInsensitiveFile |
168 |
.getCaseInsensitiveFile(targetFolder, f.getName()); |
169 |
if (remove) { |
170 |
if (targetFile.exists()) |
171 |
targetFile.delete(); |
172 |
} else { |
173 |
if (!targetFile.getName().equals(f.getName())) |
174 |
targetFile.delete(); |
175 |
FileUtils.copyFileToDirectory(f, targetFolder); |
176 |
} |
177 |
} |
178 |
} catch (IOException e) { |
179 |
e.printStackTrace(); |
180 |
} |
181 |
} |
182 |
if (remove) |
183 |
if (targetFolder.list().length == 0) |
184 |
targetFolder.delete(); |
185 |
} |
186 |
|
187 |
/** |
188 |
* Install the given set of mods |
189 |
* |
190 |
* @param mods |
191 |
* Mods to install |
192 |
* @param listener |
193 |
* Listener for install progress updates |
194 |
*/ |
195 |
public static void install(TreeSet<Package> mods, |
196 |
InstallProgressListener listener) { |
197 |
File logFile = new File(Paths.getInstallerPath(), "Installation.log"); |
198 |
PrintWriter log = null; |
199 |
try { |
200 |
log = new PrintWriter(logFile); |
201 |
} catch (FileNotFoundException e) { |
202 |
e.printStackTrace(); |
203 |
} |
204 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
205 |
Date start = new Date(); |
206 |
log.println("Installation of mods started at " + sdf.format(start)); |
207 |
|
208 |
log.println(); |
209 |
log.println("AEI2 version: " |
210 |
+ SwingJavaBuilder.getConfig().getResource("appversion")); |
211 |
log.println("Installed tools:"); |
212 |
for (Package t : PackageManager.getInstance().getInstalledTools()) { |
213 |
log.println(String.format(" - %s (%s)", t.getName(), t.getVersion())); |
214 |
} |
215 |
log.println("Installing mods:"); |
216 |
for (Package m : mods) { |
217 |
log.println(String.format(" - %s (%s)", m.getName(), m.getVersion())); |
218 |
} |
219 |
log.println(); |
220 |
|
221 |
Paths.getEditionGDF().mkdirs(); |
222 |
for (File f : Paths.getEditionGDF().listFiles(new FilenameFilter() { |
223 |
public boolean accept(File arg0, String arg1) { |
224 |
String s = arg1.toLowerCase(); |
225 |
return s.endsWith(".dat") |
226 |
|| s.endsWith(".raw") |
227 |
|| s.endsWith(".sep") |
228 |
|| (s.equals("intro.bik") && !SettingsManager.getInstance() |
229 |
.get("copyintro", false)) |
230 |
|| (s.equals("outro.bik") && !SettingsManager.getInstance() |
231 |
.get("copyoutro", false)); |
232 |
} |
233 |
})) { |
234 |
f.delete(); |
235 |
} |
236 |
File IGMD = new File(Paths.getEditionGDF(), "IGMD"); |
237 |
if (IGMD.exists()) { |
238 |
for (File f : IGMD.listFiles(new FileFilter() { |
239 |
@Override |
240 |
public boolean accept(File pathname) { |
241 |
return pathname.isDirectory(); |
242 |
} |
243 |
})) { |
244 |
File ignore = CaseInsensitiveFile.getCaseInsensitiveFile(f, |
245 |
"ignore.txt"); |
246 |
if (!ignore.exists()) { |
247 |
try { |
248 |
FileUtils.deleteDirectory(f); |
249 |
} catch (IOException e) { |
250 |
e.printStackTrace(); |
251 |
} |
252 |
} |
253 |
} |
254 |
} |
255 |
|
256 |
File installCfg = new File(Paths.getEditionGDF(), "installed_mods.xml"); |
257 |
PackageManager.getInstance().saveModSelection(installCfg, mods); |
258 |
|
259 |
TreeSet<Integer> unlockLevels = new TreeSet<Integer>(); |
260 |
|
261 |
Vector<File> foldersOni = new Vector<File>(); |
262 |
foldersOni.add(Paths.getVanillaOnisPath()); |
263 |
|
264 |
Vector<File> foldersPatches = new Vector<File>(); |
265 |
|
266 |
for (Package m : mods) { |
267 |
for (int lev : m.getUnlockLevels()) |
268 |
unlockLevels.add(lev); |
269 |
|
270 |
File oni = CaseInsensitiveFile.getCaseInsensitiveFile( |
271 |
m.getLocalPath(), "oni"); |
272 |
if (oni.exists()) { |
273 |
if (m.hasSeparatePlatformDirs()) { |
274 |
File oniCommon = CaseInsensitiveFile |
275 |
.getCaseInsensitiveFile(oni, "common"); |
276 |
File oniMac = CaseInsensitiveFile.getCaseInsensitiveFile( |
277 |
oni, "mac_only"); |
278 |
File oniWin = CaseInsensitiveFile.getCaseInsensitiveFile( |
279 |
oni, "win_only"); |
280 |
if (oniCommon.exists()) |
281 |
foldersOni.add(oniCommon); |
282 |
if (PlatformInformation.getPlatform() == Platform.MACOS |
283 |
&& oniMac.exists()) |
284 |
foldersOni.add(oniMac); |
285 |
else if (oniWin.exists()) |
286 |
foldersOni.add(oniWin); |
287 |
} else { |
288 |
foldersOni.add(oni); |
289 |
} |
290 |
} |
291 |
|
292 |
File patches = CaseInsensitiveFile.getCaseInsensitiveFile( |
293 |
m.getLocalPath(), "patches"); |
294 |
if (patches.exists()) { |
295 |
if (m.hasSeparatePlatformDirs()) { |
296 |
File patchesCommon = CaseInsensitiveFile |
297 |
.getCaseInsensitiveFile(patches, "common"); |
298 |
File patchesMac = CaseInsensitiveFile |
299 |
.getCaseInsensitiveFile(patches, "mac_only"); |
300 |
File patchesWin = CaseInsensitiveFile |
301 |
.getCaseInsensitiveFile(patches, "win_only"); |
302 |
if (patchesCommon.exists()) |
303 |
foldersPatches.add(patchesCommon); |
304 |
if (PlatformInformation.getPlatform() == Platform.MACOS |
305 |
&& patchesMac.exists()) |
306 |
foldersPatches.add(patchesMac); |
307 |
else if (patchesWin.exists()) |
308 |
foldersPatches.add(patchesWin); |
309 |
} else { |
310 |
foldersPatches.add(patches); |
311 |
} |
312 |
} |
313 |
} |
314 |
|
315 |
TreeMap<String, Vector<File>> levels = new TreeMap<String, Vector<File>>(); |
316 |
for (File path : foldersOni) { |
317 |
for (File levelF : path.listFiles()) { |
318 |
String fn = levelF.getName().toLowerCase(); |
319 |
String levelN = null; |
320 |
if (levelF.isDirectory()) { |
321 |
levelN = fn; |
322 |
} else if (fn.endsWith(".dat")) { |
323 |
levelN = fn.substring(0, fn.lastIndexOf('.')).toLowerCase(); |
324 |
} |
325 |
if (levelN != null) { |
326 |
if (!levels.containsKey(levelN)) |
327 |
levels.put(levelN, new Vector<File>()); |
328 |
levels.get(levelN).add(levelF); |
329 |
} |
330 |
} |
331 |
} |
332 |
|
333 |
applyPatches(levels, foldersPatches, listener, log); |
334 |
|
335 |
combineBinaryFiles(levels, listener, log); |
336 |
combineBSLFolders(mods, listener, log); |
337 |
|
338 |
copyVideos(log); |
339 |
|
340 |
if (unlockLevels.size() > 0) { |
341 |
unlockLevels(unlockLevels, log); |
342 |
} |
343 |
|
344 |
log.println(); |
345 |
Date end = new Date(); |
346 |
log.println("Initialization ended at " + sdf.format(end)); |
347 |
log.println("Process took " |
348 |
+ ((end.getTime() - start.getTime()) / 1000) + " seconds"); |
349 |
log.close(); |
350 |
} |
351 |
|
352 |
private static void combineBSLFolders(TreeSet<Package> mods, |
353 |
InstallProgressListener listener, PrintWriter log) { |
354 |
listener.installProgressUpdate(95, 100, "Installing BSL files"); |
355 |
log.println("Installing BSL files"); |
356 |
|
357 |
HashMap<EBSLInstallType, Vector<Package>> modsToInclude = new HashMap<EBSLInstallType, Vector<Package>>(); |
358 |
modsToInclude.put(EBSLInstallType.NORMAL, new Vector<Package>()); |
359 |
modsToInclude.put(EBSLInstallType.ADDON, new Vector<Package>()); |
360 |
|
361 |
for (Package m : mods.descendingSet()) { |
362 |
File bsl = CaseInsensitiveFile.getCaseInsensitiveFile( |
363 |
m.getLocalPath(), "bsl"); |
364 |
if (bsl.exists()) { |
365 |
if (m.hasSeparatePlatformDirs()) { |
366 |
File bslCommon = CaseInsensitiveFile |
367 |
.getCaseInsensitiveFile(bsl, "common"); |
368 |
File bslMac = CaseInsensitiveFile.getCaseInsensitiveFile( |
369 |
bsl, "mac_only"); |
370 |
File bslWin = CaseInsensitiveFile.getCaseInsensitiveFile( |
371 |
bsl, "win_only"); |
372 |
if ((PlatformInformation.getPlatform() == Platform.MACOS && bslMac |
373 |
.exists()) |
374 |
|| ((PlatformInformation.getPlatform() == Platform.WIN || PlatformInformation |
375 |
.getPlatform() == Platform.LINUX) && bslWin |
376 |
.exists()) || bslCommon.exists()) { |
377 |
modsToInclude.get(m.getBSLInstallType()).add(m); |
378 |
} |
379 |
} else { |
380 |
modsToInclude.get(m.getBSLInstallType()).add(m); |
381 |
} |
382 |
} |
383 |
} |
384 |
|
385 |
for (Package m : modsToInclude.get(EBSLInstallType.NORMAL)) { |
386 |
copyBSL(m, false); |
387 |
} |
388 |
Vector<Package> addons = modsToInclude.get(EBSLInstallType.ADDON); |
389 |
for (int i = addons.size() - 1; i >= 0; i--) { |
390 |
copyBSL(addons.get(i), true); |
391 |
} |
392 |
} |
393 |
|
394 |
private static void copyBSL(Package sourceMod, boolean addon) { |
395 |
File targetBaseFolder = new File(Paths.getEditionGDF(), "IGMD"); |
396 |
if (!targetBaseFolder.exists()) |
397 |
targetBaseFolder.mkdir(); |
398 |
|
399 |
Vector<File> sources = new Vector<File>(); |
400 |
File bsl = CaseInsensitiveFile.getCaseInsensitiveFile( |
401 |
sourceMod.getLocalPath(), "bsl"); |
402 |
if (sourceMod.hasSeparatePlatformDirs()) { |
403 |
File bslCommon = CaseInsensitiveFile.getCaseInsensitiveFile(bsl, |
404 |
"common"); |
405 |
File bslMac = CaseInsensitiveFile.getCaseInsensitiveFile(bsl, |
406 |
"mac_only"); |
407 |
File bslWin = CaseInsensitiveFile.getCaseInsensitiveFile(bsl, |
408 |
"win_only"); |
409 |
if (PlatformInformation.getPlatform() == Platform.MACOS && bslMac.exists()) { |
410 |
for (File f : bslMac.listFiles(dirFileFilter)) { |
411 |
File targetBSL = new File(targetBaseFolder, f.getName()); |
412 |
if (addon || !targetBSL.exists()) |
413 |
sources.add(f); |
414 |
} |
415 |
} |
416 |
if ((PlatformInformation.getPlatform() == Platform.WIN || PlatformInformation |
417 |
.getPlatform() == Platform.LINUX) && bslWin.exists()) { |
418 |
for (File f : bslWin.listFiles(dirFileFilter)) { |
419 |
File targetBSL = new File(targetBaseFolder, f.getName()); |
420 |
if (addon || !targetBSL.exists()) |
421 |
sources.add(f); |
422 |
} |
423 |
} |
424 |
if (bslCommon.exists()) { |
425 |
for (File f : bslCommon.listFiles(dirFileFilter)) { |
426 |
File targetBSL = new File(targetBaseFolder, f.getName()); |
427 |
if (addon || !targetBSL.exists()) |
428 |
sources.add(f); |
429 |
} |
430 |
} |
431 |
} else { |
432 |
for (File f : bsl.listFiles(dirFileFilter)) { |
433 |
File targetBSL = new File(targetBaseFolder, f.getName()); |
434 |
if (addon || !targetBSL.exists()) |
435 |
sources.add(f); |
436 |
} |
437 |
} |
438 |
|
439 |
System.out.println("For mod: " + sourceMod.getName() |
440 |
+ " install BSL folders: " + sources.toString()); |
441 |
for (File f : sources) { |
442 |
File targetPath = new File(targetBaseFolder, f.getName()); |
443 |
if (!targetPath.exists()) |
444 |
targetPath.mkdir(); |
445 |
if (!(CaseInsensitiveFile.getCaseInsensitiveFile(targetPath, |
446 |
"ignore.txt").exists())) { |
447 |
for (File fbsl : f.listFiles()) { |
448 |
if (fbsl.getName().toLowerCase().endsWith(".bsl")) { |
449 |
File targetFile = new File(targetPath, fbsl.getName()); |
450 |
if (addon || !targetFile.exists()) { |
451 |
try { |
452 |
FileUtils.copyFile(fbsl, targetFile); |
453 |
} catch (IOException e) { |
454 |
e.printStackTrace(); |
455 |
} |
456 |
} |
457 |
} |
458 |
} |
459 |
} |
460 |
} |
461 |
} |
462 |
|
463 |
private static void applyPatches( |
464 |
TreeMap<String, Vector<File>> oniLevelFolders, |
465 |
List<File> patchFolders, InstallProgressListener listener, |
466 |
PrintWriter log) { |
467 |
log.println(); |
468 |
log.println("Applying XML patches"); |
469 |
listener.installProgressUpdate(0, 1, "Applying XML patches"); |
470 |
|
471 |
long startMS = new Date().getTime(); |
472 |
|
473 |
String tmpFolderName = "installrun_temp-" |
474 |
+ new SimpleDateFormat("yyyy_MM_dd-HH_mm_ss") |
475 |
.format(new Date()); |
476 |
File tmpFolder = new File(Paths.getTempPath(), tmpFolderName); |
477 |
tmpFolder.mkdir(); |
478 |
|
479 |
HashMap<String, Vector<File>> patches = new HashMap<String, Vector<File>>(); |
480 |
for (File patchFolder : patchFolders) { |
481 |
for (File levelFolder : patchFolder.listFiles(dirFileFilter)) { |
482 |
String lvlName = levelFolder.getName().toLowerCase(); |
483 |
for (File f : FileUtils.listFiles(levelFolder, |
484 |
new String[] { "oni-patch" }, true)) { |
485 |
if (!patches.containsKey(lvlName)) |
486 |
patches.put(lvlName, new Vector<File>()); |
487 |
patches.get(lvlName).add(f); |
488 |
} |
489 |
} |
490 |
} |
491 |
|
492 |
for (String level : patches.keySet()) { |
493 |
File levelFolder = new File(tmpFolder, level); |
494 |
levelFolder.mkdir(); |
495 |
|
496 |
log.println("\t\tPatches for " + level); |
497 |
|
498 |
Vector<String> exportPatterns = new Vector<String>(); |
499 |
// Get files to be patched from vanilla.dat |
500 |
for (File patch : patches.get(level)) { |
501 |
String patternWildcard = patch.getName(); |
502 |
patternWildcard = patternWildcard.substring(0, |
503 |
patternWildcard.indexOf(".oni-patch")); |
504 |
patternWildcard = patternWildcard.replace('-', '*'); |
505 |
exportPatterns.add(patternWildcard); |
506 |
} |
507 |
for (File srcFolder : oniLevelFolders.get(level)) { |
508 |
if (srcFolder.isFile()) { |
509 |
if (srcFolder.getPath().toLowerCase().contains("vanilla")) { |
510 |
// Extract from .dat |
511 |
ApplicationInvocationResult res = OniSplit.export(levelFolder, |
512 |
srcFolder, exportPatterns); |
513 |
logAppOutput(res, log); |
514 |
} |
515 |
} |
516 |
} |
517 |
|
518 |
// Get files to be patched from packages |
519 |
for (File patch : patches.get(level)) { |
520 |
String patternWildcard = patch.getName(); |
521 |
patternWildcard = patternWildcard.substring(0, |
522 |
patternWildcard.indexOf(".oni-patch")); |
523 |
patternWildcard = patternWildcard.replace('-', '*'); |
524 |
patternWildcard = patternWildcard + ".oni"; |
525 |
Vector<String> patterns = new Vector<String>(); |
526 |
patterns.add(patternWildcard); |
527 |
final Pattern patternRegex = Pattern.compile( |
528 |
patternWildcard.replaceAll("\\*", ".\\*"), |
529 |
Pattern.CASE_INSENSITIVE); |
530 |
|
531 |
for (File srcFolder : oniLevelFolders.get(level)) { |
532 |
if (srcFolder.isFile()) { |
533 |
if (!srcFolder.getPath().toLowerCase() |
534 |
.contains("vanilla")) { |
535 |
// Extract from .dat |
536 |
ApplicationInvocationResult res = OniSplit.export( |
537 |
levelFolder, srcFolder, patterns); |
538 |
logAppOutput(res, log); |
539 |
} |
540 |
} else { |
541 |
// Copy from folder with overwrite |
542 |
for (File f : FileUtils.listFiles(srcFolder, |
543 |
new RegexFileFilter(patternRegex), |
544 |
TrueFileFilter.TRUE)) { |
545 |
try { |
546 |
FileUtils.copyFileToDirectory(f, levelFolder); |
547 |
} catch (IOException e) { |
548 |
e.printStackTrace(); |
549 |
} |
550 |
} |
551 |
} |
552 |
} |
553 |
} |
554 |
|
555 |
// Extract files to XML |
556 |
File levelFolderXML = new File(levelFolder, "xml"); |
557 |
Vector<File> files = new Vector<File>(); |
558 |
files.add(new File(levelFolder, "*.oni")); |
559 |
ApplicationInvocationResult res = OniSplit.convertOniToXML(levelFolderXML, |
560 |
files); |
561 |
logAppOutput(res, log); |
562 |
|
563 |
// Apply patches in levelFolderXML |
564 |
for (File patch : patches.get(level)) { |
565 |
String patternWildcard = patch.getName(); |
566 |
patternWildcard = patternWildcard.substring(0, |
567 |
patternWildcard.indexOf(".oni-patch")); |
568 |
patternWildcard = patternWildcard + ".xml"; |
569 |
patternWildcard = patternWildcard.replace('-', '*'); |
570 |
|
571 |
res = XMLTools.patch(patch, new File(levelFolderXML, |
572 |
patternWildcard)); |
573 |
logAppOutput(res, log); |
574 |
} |
575 |
|
576 |
// Create .oni files from XML |
577 |
files.clear(); |
578 |
files.add(new File(levelFolderXML, "*.xml")); |
579 |
res = OniSplit.convertXMLtoOni(levelFolder, files); |
580 |
logAppOutput(res, log); |
581 |
|
582 |
// Remove XML folder as import will only require .oni's |
583 |
// try { |
584 |
// FileUtils.deleteDirectory(levelFolderXML); |
585 |
// } catch (IOException e) { |
586 |
// e.printStackTrace(); |
587 |
// } |
588 |
|
589 |
oniLevelFolders.get(level).add(levelFolder); |
590 |
} |
591 |
|
592 |
log.println("Applying XML patches took " |
593 |
+ (new Date().getTime() - startMS) + " ms"); |
594 |
} |
595 |
|
596 |
private static void combineBinaryFiles( |
597 |
TreeMap<String, Vector<File>> oniLevelFolders, |
598 |
InstallProgressListener listener, PrintWriter log) { |
599 |
long startMS = new Date().getTime(); |
600 |
|
601 |
int totalSteps = 0; |
602 |
int stepsDone = 0; |
603 |
|
604 |
for (@SuppressWarnings("unused") |
605 |
String s : oniLevelFolders.keySet()) |
606 |
totalSteps++; |
607 |
totalSteps++; |
608 |
|
609 |
log.println(); |
610 |
log.println("Importing levels"); |
611 |
for (String l : oniLevelFolders.keySet()) { |
612 |
log.println("\tLevel " + l); |
613 |
listener.installProgressUpdate(stepsDone, totalSteps, |
614 |
"Installing level " + l); |
615 |
|
616 |
ApplicationInvocationResult res = OniSplit.packLevel(oniLevelFolders.get(l), |
617 |
new File(Paths.getEditionGDF(), sanitizeLevelName(l) |
618 |
+ ".dat")); |
619 |
logAppOutput(res, log); |
620 |
|
621 |
stepsDone++; |
622 |
} |
623 |
|
624 |
log.println("Importing levels took " + (new Date().getTime() - startMS) |
625 |
+ " ms"); |
626 |
log.println(); |
627 |
} |
628 |
|
629 |
private static void copyVideos(PrintWriter log) { |
630 |
if (SettingsManager.getInstance().get("copyintro", false)) { |
631 |
File src = new File(Paths.getVanillaGDF(), "intro.bik"); |
632 |
log.println("Copying intro"); |
633 |
if (src.exists()) { |
634 |
try { |
635 |
FileUtils.copyFileToDirectory(src, Paths.getEditionGDF()); |
636 |
} catch (IOException e) { |
637 |
e.printStackTrace(); |
638 |
} |
639 |
} |
640 |
} else { |
641 |
log.println("NOT copying intro"); |
642 |
} |
643 |
if (SettingsManager.getInstance().get("copyoutro", true)) { |
644 |
File src = new File(Paths.getVanillaGDF(), "outro.bik"); |
645 |
log.println("Copying outro"); |
646 |
if (src.exists()) { |
647 |
try { |
648 |
FileUtils.copyFileToDirectory(src, Paths.getEditionGDF()); |
649 |
} catch (IOException e) { |
650 |
e.printStackTrace(); |
651 |
} |
652 |
} |
653 |
} else { |
654 |
log.println("NOT copying outro"); |
655 |
} |
656 |
} |
657 |
|
658 |
private static void unlockLevels(TreeSet<Integer> unlockLevels, |
659 |
PrintWriter log) { |
660 |
File dat = new File(Paths.getEditionBasePath(), "persist.dat"); |
661 |
log.println("Unlocking levels: " + unlockLevels.toString()); |
662 |
if (!dat.exists()) { |
663 |
InputStream is = AEInstaller2.class |
664 |
.getResourceAsStream("/net/oni2/aeinstaller/resources/persist.dat"); |
665 |
try { |
666 |
FileUtils.copyInputStreamToFile(is, dat); |
667 |
} catch (IOException e) { |
668 |
e.printStackTrace(); |
669 |
} |
670 |
} |
671 |
PersistDat save = new PersistDat(dat); |
672 |
HashSet<Integer> currentlyUnlocked = save.getUnlockedLevels(); |
673 |
currentlyUnlocked.addAll(unlockLevels); |
674 |
save.setUnlockedLevels(currentlyUnlocked); |
675 |
save.close(); |
676 |
} |
677 |
|
678 |
/** |
679 |
* Initializes the Edition core |
680 |
* |
681 |
* @param listener |
682 |
* Listener for status updates |
683 |
*/ |
684 |
public static void initializeEdition(InstallProgressListener listener) { |
685 |
File init = new File(Paths.getTempPath(), "init"); |
686 |
|
687 |
int totalSteps = 0; |
688 |
int stepsDone = 0; |
689 |
|
690 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
691 |
|
692 |
for (@SuppressWarnings("unused") |
693 |
File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() { |
694 |
@Override |
695 |
public boolean accept(File dir, String name) { |
696 |
return name.endsWith(".dat"); |
697 |
} |
698 |
})) { |
699 |
totalSteps++; |
700 |
} |
701 |
totalSteps = totalSteps * 2 + 2; |
702 |
|
703 |
try { |
704 |
File logFile = new File(Paths.getInstallerPath(), |
705 |
"Initialization.log"); |
706 |
PrintWriter log = new PrintWriter(logFile); |
707 |
|
708 |
Date start = new Date(); |
709 |
log.println("Initialization of Edition core started at " |
710 |
+ sdf.format(start)); |
711 |
log.println("Cleaning directories"); |
712 |
|
713 |
listener.installProgressUpdate(stepsDone, totalSteps, |
714 |
"Cleaning up directories"); |
715 |
createEmptyPath(Paths.getVanillaOnisPath()); |
716 |
createEmptyPath(init); |
717 |
File level0Folder = new File(init, "level0_Final"); |
718 |
createEmptyPath(level0Folder); |
719 |
|
720 |
stepsDone++; |
721 |
|
722 |
log.println("Exporting levels and moving files to level0"); |
723 |
|
724 |
for (File f : Paths.getVanillaGDF().listFiles(new FilenameFilter() { |
725 |
@Override |
726 |
public boolean accept(File dir, String name) { |
727 |
return name.endsWith(".dat"); |
728 |
} |
729 |
})) { |
730 |
String levelName = f.getName().substring(0, |
731 |
f.getName().indexOf('.')); |
732 |
Scanner fi = new Scanner(levelName); |
733 |
int levelNumber = Integer.parseInt(fi.findInLine("[0-9]+")); |
734 |
|
735 |
log.println("\t" + levelName + ":"); |
736 |
log.println("\t\tExporting"); |
737 |
listener.installProgressUpdate(stepsDone, totalSteps, |
738 |
"Exporting vanilla level " + levelNumber); |
739 |
|
740 |
// Edition/GameDataFolder/level*_Final/ |
741 |
File tempLevelFolder = new File(init, levelName); |
742 |
|
743 |
// Export Vanilla-Level-Dat -> Temp/Level |
744 |
ApplicationInvocationResult res = OniSplit.export(tempLevelFolder, f); |
745 |
logAppOutput(res, log); |
746 |
|
747 |
log.println("\t\tMoving files"); |
748 |
handleFileGlobalisation(tempLevelFolder, level0Folder, |
749 |
levelNumber); |
750 |
stepsDone++; |
751 |
log.println(); |
752 |
} |
753 |
|
754 |
log.println("Reimporting levels"); |
755 |
|
756 |
for (File f : init.listFiles()) { |
757 |
String levelName = f.getName(); |
758 |
|
759 |
log.println("\t" + levelName); |
760 |
listener.installProgressUpdate(stepsDone, totalSteps, |
761 |
"Creating globalized " + levelName); |
762 |
|
763 |
Vector<File> folders = new Vector<File>(); |
764 |
folders.add(f); |
765 |
|
766 |
ApplicationInvocationResult res = OniSplit |
767 |
.importLevel(folders, |
768 |
new File(Paths.getVanillaOnisPath(), levelName |
769 |
+ ".dat")); |
770 |
logAppOutput(res, log); |
771 |
|
772 |
log.println(); |
773 |
stepsDone++; |
774 |
} |
775 |
|
776 |
listener.installProgressUpdate(stepsDone, totalSteps, |
777 |
"Copying basic files"); |
778 |
// Copy Oni-configs |
779 |
File persistVanilla = new File(Paths.getOniBasePath(), |
780 |
"persist.dat"); |
781 |
File persistEdition = new File(Paths.getEditionBasePath(), |
782 |
"persist.dat"); |
783 |
File keyConfVanilla = new File(Paths.getOniBasePath(), |
784 |
"key_config.txt"); |
785 |
File keyConfEdition = new File(Paths.getEditionBasePath(), |
786 |
"key_config.txt"); |
787 |
if (persistVanilla.exists() && !persistEdition.exists()) |
788 |
FileUtils.copyFile(persistVanilla, persistEdition); |
789 |
if (keyConfVanilla.exists() && !keyConfEdition.exists()) |
790 |
FileUtils.copyFile(keyConfVanilla, keyConfEdition); |
791 |
|
792 |
FileUtils.deleteDirectory(init); |
793 |
|
794 |
Date end = new Date(); |
795 |
log.println("Initialization ended at " + sdf.format(end)); |
796 |
log.println("Process took " |
797 |
+ ((end.getTime() - start.getTime()) / 1000) + " seconds"); |
798 |
log.close(); |
799 |
} catch (IOException e) { |
800 |
e.printStackTrace(); |
801 |
} |
802 |
} |
803 |
|
804 |
private static void moveFileToTargetOrDelete(File source, File target) { |
805 |
if (source.equals(target)) |
806 |
return; |
807 |
if (!target.exists()) { |
808 |
if (!source.renameTo(target)) { |
809 |
System.err.println("File " + source.getPath() + " not moved!"); |
810 |
} |
811 |
} else if (!source.delete()) { |
812 |
System.err.println("File " + source.getPath() + " not deleted!"); |
813 |
} |
814 |
} |
815 |
|
816 |
private static void handleFileGlobalisation(File tempFolder, |
817 |
File level0Folder, int levelNumber) { |
818 |
// Move AKEV and related files to subfolder so they're not globalized: |
819 |
if (levelNumber != 0) { |
820 |
File akevFolder = new File(tempFolder, "AKEV"); |
821 |
akevFolder.mkdir(); |
822 |
OniSplit.move(akevFolder, tempFolder.getPath() + "/AKEV*.oni", |
823 |
"overwrite"); |
824 |
} |
825 |
|
826 |
for (File f : tempFolder.listFiles(new FileFilter() { |
827 |
@Override |
828 |
public boolean accept(File pathname) { |
829 |
return pathname.isFile(); |
830 |
} |
831 |
})) { |
832 |
// Move matching files to subfolder NoGlobal: |
833 |
if (f.getName().startsWith("TXMPfail") |
834 |
|| f.getName().startsWith("TXMPlevel") |
835 |
|| (f.getName().startsWith("TXMP") && f.getName().contains( |
836 |
"intro")) |
837 |
|| f.getName().startsWith("TXMB") |
838 |
|| f.getName().equals("M3GMpowerup_lsi.oni") |
839 |
|| f.getName().equals("TXMPlsi_icon.oni") |
840 |
|| (f.getName().startsWith("TXMB") && f.getName().contains( |
841 |
"splash_screen.oni"))) { |
842 |
File noGlobal = new File(tempFolder, "NoGlobal"); |
843 |
noGlobal.mkdir(); |
844 |
File noGlobalFile = new File(noGlobal, f.getName()); |
845 |
moveFileToTargetOrDelete(f, noGlobalFile); |
846 |
} |
847 |
// Move matching files to level0_Animations/level0_TRAC |
848 |
else if (f.getName().startsWith("TRAC")) { |
849 |
File level0File = new File(level0Folder, f.getName()); |
850 |
moveFileToTargetOrDelete(f, level0File); |
851 |
} |
852 |
// Move matching files to level0_Animations/level0_TRAM |
853 |
else if (f.getName().startsWith("TRAM")) { |
854 |
File level0File = new File(level0Folder, f.getName()); |
855 |
moveFileToTargetOrDelete(f, level0File); |
856 |
} |
857 |
// Move matching files to level0_Textures |
858 |
else if (f.getName().startsWith("ONSK") |
859 |
|| f.getName().startsWith("TXMP")) { |
860 |
File level0File = new File(level0Folder, f.getName()); |
861 |
moveFileToTargetOrDelete(f, level0File); |
862 |
} |
863 |
// Move matching files to *VANILLA*/level0_Characters |
864 |
else if (f.getName().startsWith("ONCC") |
865 |
|| f.getName().startsWith("TRBS") |
866 |
|| f.getName().startsWith("ONCV") |
867 |
|| f.getName().startsWith("ONVL") |
868 |
|| f.getName().startsWith("TRMA") |
869 |
|| f.getName().startsWith("TRSC") |
870 |
|| f.getName().startsWith("TRAS")) { |
871 |
File level0File = new File(level0Folder, f.getName()); |
872 |
moveFileToTargetOrDelete(f, level0File); |
873 |
} |
874 |
// Move matching files to level0_Sounds |
875 |
else if (f.getName().startsWith("OSBD") |
876 |
|| f.getName().startsWith("SNDD")) { |
877 |
File level0File = new File(level0Folder, f.getName()); |
878 |
moveFileToTargetOrDelete(f, level0File); |
879 |
} |
880 |
// Move matching files to level0_Particles |
881 |
else if (f.getName().startsWith("BINA3") |
882 |
|| f.getName().startsWith("M3GMdebris") |
883 |
|| f.getName().equals("M3GMtoxic_bubble.oni") |
884 |
|| f.getName().startsWith("M3GMelec") |
885 |
|| f.getName().startsWith("M3GMrat") |
886 |
|| f.getName().startsWith("M3GMjet") |
887 |
|| f.getName().startsWith("M3GMbomb_") |
888 |
|| f.getName().equals("M3GMbarab_swave.oni") |
889 |
|| f.getName().equals("M3GMbloodyfoot.oni")) { |
890 |
File level0File = new File(level0Folder, f.getName()); |
891 |
moveFileToTargetOrDelete(f, level0File); |
892 |
} |
893 |
// Move matching files to Archive (aka delete them) |
894 |
else if (f.getName().startsWith("AGDB") |
895 |
|| f.getName().startsWith("TRCM")) { |
896 |
f.delete(); |
897 |
} |
898 |
// Move matching files to /level0_Final/ |
899 |
else if (f.getName().startsWith("ONWC")) { |
900 |
File level0File = new File(level0Folder, f.getName()); |
901 |
moveFileToTargetOrDelete(f, level0File); |
902 |
} |
903 |
} |
904 |
} |
905 |
|
906 |
private static String sanitizeLevelName(String ln) { |
907 |
int ind = ln.indexOf("_"); |
908 |
String res = ln.substring(0, ind + 1); |
909 |
res += ln.substring(ind + 1, ind + 2).toUpperCase(); |
910 |
res += ln.substring(ind + 2); |
911 |
return res; |
912 |
} |
913 |
|
914 |
/** |
915 |
* Verify that the Edition is within a subfolder to vanilla Oni |
916 |
* (..../Oni/Edition/AEInstaller) |
917 |
* |
918 |
* @return true if GDF can be found in the parent's parent-path |
919 |
*/ |
920 |
public static boolean verifyRunningDirectory() { |
921 |
return Paths.getVanillaGDF().exists() |
922 |
&& Paths.getVanillaGDF().isDirectory(); |
923 |
} |
924 |
|
925 |
private static void logAppOutput(ApplicationInvocationResult result, PrintWriter log) { |
926 |
if (result != null) { |
927 |
log.println("\t\t\tCalled:"); |
928 |
for (String s : result.cmdLine) |
929 |
log.println("\t\t\t\t" + s); |
930 |
log.println("\t\t\tReturned: " + result.errorCode); |
931 |
for (String s : result.output) |
932 |
log.println("\t\t\t\t" + s); |
933 |
log.println("\t\t\tDuration: " + result.time + " ms"); |
934 |
log.println(); |
935 |
} |
936 |
} |
937 |
} |