private static void Read(string file, out Data d) { Debug.WriteLine("Task={0}, Thread={1}, File={2}", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, file); byte[] decmp; MemoryStream ms = null; FileStream fs = null; // fs is disposed by binaryreader. using (BinaryReader br = new BinaryReader( fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { int size = br.ReadInt32(); byte[] tmp = br.ReadBytes((int)fs.Length - sizeof(uint)); decmp = LZSS.DecompressAllNew(tmp); fs = null; } using (BinaryReader br = new BinaryReader(ms = new MemoryStream(decmp))) { ms.Seek(SteamOffset, SeekOrigin.Begin); d = new Data(); d.Read(br); ms = null; } }
//Splash is 640x400 16BPP typical TIM with palette of ggg bbbbb a rrrrr gg public static void ReadSplash(bool bLogo = false) { string[] lof = aw.GetListOfFiles(); string filename; if (splashName > 0x0f) { return; } filename = !bLogo ? bNames ? lof.First(x => x.ToLower().Contains($"{names}{splashName.ToString("D2")}")) : lof.First(x => x.ToLower().Contains($"{loops}{splashLoop.ToString("D2")}")) : lof.First(x => x.ToLower().Contains($"ff8.lzs")); byte[] buffer = ArchiveWorker.GetBinaryFile(Memory.Archives.A_MAIN, filename); uint uncompSize = BitConverter.ToUInt32(buffer, 0); buffer = buffer.Skip(4).ToArray(); //hotfix for new LZSS buffer = LZSS.DecompressAllNew(buffer); if (splashTex != null && !splashTex.IsDisposed) { splashTex.Dispose(); } splashTex = TIM2.Overture(buffer); //using (FileStream fs = File.Create(Path.Combine("D:\\main", Path.GetFileNameWithoutExtension(filename) + ".png"))) // splashTex.SaveAsPng(fs, splashTex.Width, splashTex.Height); GC.Collect(); GC.WaitForPendingFinalizers(); }
private static Data read(string file) { byte[] decmp; using (FileStream fs = File.OpenRead(file)) using (BinaryReader br = new BinaryReader(fs)) { uint size = br.ReadUInt32(); //uint fsLen = BitConverter.ToUInt32(FI, loc * 12); //uint fSpos = BitConverter.ToUInt32(FI, (loc * 12) + 4); //bool compe = BitConverter.ToUInt32(FI, (loc * 12) + 8) != 0; //fs.Seek(0, SeekOrigin.Begin); byte[] tmp = br.ReadBytes((int)fs.Length - 4); decmp = LZSS.DecompressAllNew(tmp); } //using (FileStream fs = File.Create(Path.Combine(@"d:\", Path.GetFileName(file)))) //using (BinaryWriter bw = new BinaryWriter(fs)) //{ // bw.Write(decmp); //} using (MemoryStream ms = new MemoryStream(decmp)) using (BinaryReader br = new BinaryReader(ms)) { ms.Seek(0x184, SeekOrigin.Begin); Data d = new Data(); d.Read(br); return(d); } }
private byte[] GetBinaryFile(string fileName, int loc, bool cache) { byte[] temp = null; if (ArchiveCache.ContainsKey(_path) && ArchiveCache[_path].ContainsKey(fileName)) { return(ArchiveCache[_path][fileName]); } //read index data using (BinaryReader br = new BinaryReader(new FileStream(_path.FI, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))//File.OpenRead(_path.FI))) { br.BaseStream.Seek(loc * 12, SeekOrigin.Begin); _unpackedFileSize = br.ReadUInt32(); //fs.Seek(4, SeekOrigin.Current); _locationInFs = br.ReadUInt32(); _compressed = br.ReadUInt32() != 0; } //read binary data. using (BinaryReader br = new BinaryReader(new FileStream(_path.FS, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))//File.OpenRead(_path.FS))) { br.BaseStream.Seek(_locationInFs, SeekOrigin.Begin); temp = br.ReadBytes(_compressed ? br.ReadInt32() : (int)_unpackedFileSize); } temp = temp == null ? null : _compressed?LZSS.DecompressAllNew(temp) : temp; if (temp != null && cache) { ArchiveCache[_path][fileName] = temp; } return(temp); }
//Splash is 640x400 16BPP typical TIM with palette of ggg bbbbb a rrrrr gg private void ReadSplash() { byte[] buffer = ArchiveWorker.GetBinaryFile(Memory.Archives.A_MAIN, filename); uint uncompSize = BitConverter.ToUInt32(buffer, 0); buffer = buffer.Skip(4).ToArray(); //hotfix for new LZSS buffer = LZSS.DecompressAllNew(buffer); tex = TIM2.Overture(buffer); //using (FileStream fs = File.Create(Path.Combine("D:\\main", Path.GetFileNameWithoutExtension(filename) + ".png"))) // splashTex.SaveAsPng(fs, splashTex.Width, splashTex.Height); GC.Collect(); GC.WaitForPendingFinalizers(); }
/// <summary> /// GetBinary /// </summary> /// <param name="fileName"></param> /// <returns></returns> public byte[] GetBinaryFile(string fileName) { if (fileName.Length < 1) { throw new FileNotFoundException("NO FILENAME"); } int loc = FindFile(fileName, File.OpenRead(_path.FL)); byte[] temp = null; // read file list if (loc == -1) { Debug.WriteLine("ArchiveWorker: NO SUCH FILE!"); //throw new Exception("ArchiveWorker: No such file!"); } else { //read index data using (BinaryReader br = new BinaryReader(File.OpenRead(_path.FI))) { br.BaseStream.Seek(loc * 12, SeekOrigin.Begin); _unpackedFileSize = br.ReadUInt32(); //fs.Seek(4, SeekOrigin.Current); _locationInFs = br.ReadUInt32(); _compressed = br.ReadUInt32() != 0; } //read binary data. using (BinaryReader br = new BinaryReader(File.OpenRead(_path.FS))) { br.BaseStream.Seek(_locationInFs, SeekOrigin.Begin); temp = br.ReadBytes(_compressed ? br.ReadInt32() : (int)_unpackedFileSize); } } if (temp == null) { throw new FileNotFoundException($"Searched {_path} and could not find {fileName}.", fileName); } return(temp == null ? null : _compressed?LZSS.DecompressAllNew(temp) : temp); }
/// <summary> /// Give me three archives as bytes uncompressed please! /// </summary> /// <param name="FI">FileIndex</param> /// <param name="FS">FileSystem</param> /// <param name="FL">FileList</param> /// <param name="filename">Filename of the file to get</param> /// <returns></returns> /// <remarks> /// Does the same thing as Get Binary file, but it reads from byte arrays in ram because the /// data was already pulled from a file earlier. /// </remarks> public byte[] FileInTwoArchives(byte[] FI, byte[] FS, byte[] FL, string filename) { int loc = FindFile(filename, new MemoryStream(FL, false)); if (loc == -1) { Debug.WriteLine("ArchiveWorker: NO SUCH FILE!"); return(null); //throw new Exception("ArchiveWorker: No such file!"); } // get params from index uint fsLen = BitConverter.ToUInt32(FI, loc * 12); uint fSpos = BitConverter.ToUInt32(FI, (loc * 12) + 4); bool compe = BitConverter.ToUInt32(FI, (loc * 12) + 8) != 0; // copy binary data byte[] file = new byte[fsLen]; Array.Copy(FS, fSpos, file, 0, file.Length); return(compe ? LZSS.DecompressAllNew(file) : file); }
private static void Read(string file, out Data d) { Debug.WriteLine("Task={0}, Thread={1}, File={2}", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, file); byte[] decmp; using (FileStream fs = File.OpenRead(file)) using (BinaryReader br = new BinaryReader(fs)) { int size = br.ReadInt32(); byte[] tmp = br.ReadBytes((int)fs.Length - sizeof(uint)); decmp = LZSS.DecompressAllNew(tmp); } using (MemoryStream ms = new MemoryStream(decmp)) using (BinaryReader br = new BinaryReader(ms)) { ms.Seek(SteamOffset, SeekOrigin.Begin); d = new Data(); d.Read(br); } }
private static void Read(string file, out Data d) { d = null; int size = 0; Memory.Log.WriteLine($"{nameof(Saves)}::{nameof(Read)} Extracting: {file}"); byte[] decmp = null; MemoryStream ms = null; FileStream fs = null; // fs is disposed by binaryreader. using (BinaryReader br = new BinaryReader( fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { if (fs.Length >= 5) { size = br.ReadInt32(); if ((int)fs.Length - sizeof(uint) == size) { byte[] tmp = br.ReadBytes((int)fs.Length - sizeof(uint)); decmp = LZSS.DecompressAllNew(tmp, 0); } } fs = null; } if (decmp == null) { Memory.Log.WriteLine($"{nameof(Saves)}::{nameof(Read)} Invalid file: {file}"); } else { using (BinaryReader br = new BinaryReader(ms = new MemoryStream(decmp))) { ms.Seek(SteamOffset, SeekOrigin.Begin); d = new Data(); d.Read(br); ms = null; } } }
//Splash is 640x400 16BPP typical TIM with palette of ggg bbbbb a rrrrr gg private void ReadSplash() { byte[] buffer = ArchiveWorker.GetBinaryFile(Memory.Archives.A_MAIN, filename); string fn = Path.GetFileNameWithoutExtension(filename); uint uncompSize = BitConverter.ToUInt32(buffer, 0); buffer = buffer.Skip(4).ToArray(); //hotfix for new LZSS buffer = LZSS.DecompressAllNew(buffer); TIM_OVERTURE tim = new TIM_OVERTURE(buffer); if ((fn.Equals("ff8", StringComparison.OrdinalIgnoreCase)) || (fn.IndexOf("loop", StringComparison.OrdinalIgnoreCase) >= 0)) { tim.IgnoreAlpha = true; } tex = (Texture2D)TextureHandler.Create(fn, tim, 0);//TIM2.Overture(buffer); //using (FileStream fs = File.Create(Path.Combine("D:\\main", Path.GetFileNameWithoutExtension(filename) + ".png"))) // splashTex.SaveAsPng(fs, splashTex.Width, splashTex.Height); GC.Collect(); GC.WaitForPendingFinalizers(); }
private byte[] GetBinaryFile(string fileName, int loc, bool cache) { fileName = FileList.FirstOrDefault(x => x.IndexOf(fileName, StringComparison.OrdinalIgnoreCase) >= 0); if (LocalTryGetValue(fileName, out var b)) { Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)} :: read from cache: {fileName}"); return(b); } if (IsDir) { if (FileList == null || FileList.Length == 0) { ProduceFileLists(); } if (!string.IsNullOrWhiteSpace(fileName)) { using (var br = new BinaryReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)} :: reading: {fileName}"); var buffer = br.ReadBytes(checked ((int)br.BaseStream.Length)); if (cache && LocalTryAdd(fileName, buffer)) { Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)} :: cached: {fileName}"); } return(buffer); } } } //read index data var fi = GetFi(loc); //read binary data. var temp = GetCompressedData(fi, out var _); Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)} :: extracting: {fileName}"); if (temp != null) { switch (fi.CompressionType) { case CompressionType.None: break; case CompressionType.LZSS: LZSS.DecompressAllNew(temp, fi.UncompressedSize); break; case CompressionType.LZ4: temp = ArchiveMap.Lz4Uncompress(temp, fi.UncompressedSize); break; case CompressionType.LZSS_UnknownSize: LZSS.DecompressAllNew(temp, 0); break; case CompressionType.LZSS_LZSS: LZSS.DecompressAllNew(LZSS.DecompressAllNew(temp, fi.UncompressedSize), 0); break; default: throw new ArgumentOutOfRangeException(); } } if (temp != null && cache && LocalTryAdd(fileName, temp)) { Memory.Log.WriteLine($"{nameof(ArchiveWorker)}::{nameof(GetBinaryFile)} :: cached: {fileName}"); } return(temp); }