From 69cd6ee1997975e575b1a50f6b054c9e6d69c608 Mon Sep 17 00:00:00 2001 From: barubary Date: Thu, 21 Apr 2011 14:16:59 +0000 Subject: [PATCH] C#: added built-in check for arm9.bin in LZ-Overlay decompression, as it has some extra bytes that should be ignored when decompressing. Only the file-based methods can handle that of course. Perhaps the extra bytes should also be copied to the output file however, which is currently not the case. --- CSharp/DSDecmp/Formats/CompressionFormat.cs | 4 +-- CSharp/DSDecmp/Formats/LZOvl.cs | 36 ++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/CSharp/DSDecmp/Formats/CompressionFormat.cs b/CSharp/DSDecmp/Formats/CompressionFormat.cs index 97d48c4..e54e412 100644 --- a/CSharp/DSDecmp/Formats/CompressionFormat.cs +++ b/CSharp/DSDecmp/Formats/CompressionFormat.cs @@ -19,7 +19,7 @@ namespace DSDecmp.Formats /// The name of the file to check. /// False if the file can certainly not be decompressed using this decompressor. /// True if the file may potentially be decompressed using this decompressor. - public bool Supports(string file) + public virtual bool Supports(string file) { // open the file, and delegate to the decompressor-specific code. using (FileStream fstr = new FileStream(file, FileMode.Open)) @@ -49,7 +49,7 @@ namespace DSDecmp.Formats /// /// The file to decompress. /// The target location of the decompressed file. - public void Decompress(string infile, string outfile) + public virtual void Decompress(string infile, string outfile) { // make sure the output directory exists string outDirectory = Path.GetDirectoryName(outfile); diff --git a/CSharp/DSDecmp/Formats/LZOvl.cs b/CSharp/DSDecmp/Formats/LZOvl.cs index 65e4dcc..dd6bde1 100644 --- a/CSharp/DSDecmp/Formats/LZOvl.cs +++ b/CSharp/DSDecmp/Formats/LZOvl.cs @@ -9,10 +9,24 @@ namespace DSDecmp.Formats /// The LZ-Overlay compression format. Compresses part of the file from end to start. /// Is used for the 'overlay' files in NDS games, as well as arm9.bin. /// Note that the last 12 bytes should not be included in the 'inLength' argument when - /// decompressing arm9.bin. + /// decompressing arm9.bin. This is done automatically if a file is given instead of a stream. /// public class LZOvl : CompressionFormat { + #region Method: Supports(string file) + public override bool Supports(string file) + { + using (FileStream fstr = File.OpenRead(file)) + { + long fLength = fstr.Length; + // arm9.bin is special in the sense that the last 12 bytes should/can be ignored. + if (Path.GetFileName(file) == "arm9.bin") + fLength -= 0xC; + return this.Supports(fstr, fLength); + } + } + #endregion + #region Method: Supports(Stream, long) public override bool Supports(System.IO.Stream stream, long inLength) { @@ -56,6 +70,26 @@ namespace DSDecmp.Formats } #endregion + #region Method: Decompress(string, string) + public override void Decompress(string infile, string outfile) + { + // make sure the output directory exists + string outDirectory = Path.GetDirectoryName(outfile); + if (!Directory.Exists(outDirectory)) + Directory.CreateDirectory(outDirectory); + // open the two given files, and delegate to the format-specific code. + using (FileStream inStream = new FileStream(infile, FileMode.Open), + outStream = new FileStream(outfile, FileMode.Create)) + { + long fLength = inStream.Length; + // arm9.bin needs special attention + if (Path.GetFileName(infile) == "arm9.bin") + fLength -= 0xC; + this.Decompress(inStream, fLength, outStream); + } + } + #endregion + public override void Decompress(System.IO.Stream instream, long inLength, System.IO.Stream outstream) { #region Format description