Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- namespace Mygod.LittleInferno.Resource.Unpacker
- {
- public class Program
- {
- private static void Main(string[] args)
- {
- if (args == null || args.Length == 0)
- {
- Console.WriteLine("Drag files onto the program to start unpacking!");
- Console.ReadKey();
- return;
- }
- var output = new StreamWriter(File.Create("output.txt"));
- Console.SetOut(output);
- foreach (var arg in args.Where(File.Exists))
- {
- Console.WriteLine("INFO: Unpacking file: {0}", arg);
- ErrorCount = WarningCount = 0;
- InformationCount = 1;
- try
- {
- new Package(arg).ExtractAll();
- }
- catch (Exception exc)
- {
- Console.WriteLine("ERROR: {0}", exc.Message);
- ErrorCount++;
- }
- Console.WriteLine("Unpacking done. {0} error(s), {1} warning(s), {2} info(s).", ErrorCount, WarningCount, InformationCount);
- Console.WriteLine();
- }
- output.Close();
- }
- public static int ErrorCount, WarningCount, InformationCount;
- }
- public class Package : IDisposable
- {
- public Package(string filePath)
- {
- path = filePath;
- stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
- if (!stream.CanRead || !stream.CanSeek) throw new NotSupportedException("The file stream is terrible!");
- reader = new BinaryReader(stream);
- PackageVersion = reader.ReadInt32();
- if (PackageVersion != 1) throw new NotSupportedException("Unsupported package version: " + PackageVersion);
- var count = reader.ReadInt32();
- Records = new List<FileRecord>(count);
- for (var i = 0; i < count; i++) Records.Add(new FileRecord(reader));
- }
- public void ExtractAll(string directory = null)
- {
- if (directory == null) directory = path + "_EXTRACTED";
- Directory.CreateDirectory(directory);
- foreach (var record in Records) Extract(record, Path.Combine(directory, record.ID.ToString()));
- }
- public void Extract(FileRecord record, string filePath)
- {
- int unpackedSize;
- byte[] data;
- lock (reader)
- {
- stream.Seek(record.Offset, SeekOrigin.Begin);
- var packedSize = reader.ReadInt32();
- unpackedSize = reader.ReadInt32();
- data = reader.ReadBytes(packedSize);
- }
- using (var fileStream = File.Create(filePath))
- using (var memoryStream = new MemoryStream(data))
- using (var deflateStream = new DeflateStream(memoryStream, CompressionMode.Decompress))
- {
- memoryStream.ReadByte();
- memoryStream.ReadByte();
- try
- {
- deflateStream.CopyTo(fileStream);
- if (fileStream.Length != unpackedSize)
- {
- Console.WriteLine("WARNING: File size does not match. (item #{0})", record.ID);
- Program.WarningCount++;
- }
- Console.WriteLine("INFO: Unpacking item #{0} done.", record.ID);
- Program.InformationCount++;
- }
- catch (InvalidDataException)
- {
- Console.WriteLine("ERROR: Unpacking item #{0} failed.", record.ID);
- Program.ErrorCount++;
- }
- }
- }
- public void Dispose()
- {
- reader.Close();
- stream.Close();
- }
- private readonly FileStream stream;
- private readonly BinaryReader reader;
- private readonly string path;
- public int PackageVersion;
- public List<FileRecord> Records;
- }
- public struct FileRecord
- {
- public uint ID, Flags;
- public int Offset, Size;
- public FileRecord(BinaryReader reader)
- {
- ID = reader.ReadUInt32();
- Flags = reader.ReadUInt32();
- if (Flags != 1)
- {
- //throw new NotSupportedException("Unsupported file record flags: " + Flags);
- Console.WriteLine("WARNING: Unsupported file record flags value {0}. (item #{1})", Flags, ID);
- Program.WarningCount++;
- }
- Offset = reader.ReadInt32();
- Size = reader.ReadInt32();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement