Advertisement
Mygod

Little Inferno Resource Unpacker Source

Dec 5th, 2012
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Linq;
  6.  
  7. namespace Mygod.LittleInferno.Resource.Unpacker
  8. {
  9.     public class Program
  10.     {
  11.         private static void Main(string[] args)
  12.         {
  13.             if (args == null || args.Length == 0)
  14.             {
  15.                 Console.WriteLine("Drag files onto the program to start unpacking!");
  16.                 Console.ReadKey();
  17.                 return;
  18.             }
  19.             var output = new StreamWriter(File.Create("output.txt"));
  20.             Console.SetOut(output);
  21.             foreach (var arg in args.Where(File.Exists))
  22.             {
  23.                 Console.WriteLine("INFO: Unpacking file: {0}", arg);
  24.                 ErrorCount = WarningCount = 0;
  25.                 InformationCount = 1;
  26.                 try
  27.                 {
  28.                     new Package(arg).ExtractAll();
  29.                 }
  30.                 catch (Exception exc)
  31.                 {
  32.                     Console.WriteLine("ERROR: {0}", exc.Message);
  33.                     ErrorCount++;
  34.                 }
  35.                 Console.WriteLine("Unpacking done. {0} error(s), {1} warning(s), {2} info(s).", ErrorCount, WarningCount, InformationCount);
  36.                 Console.WriteLine();
  37.             }
  38.             output.Close();
  39.         }
  40.  
  41.         public static int ErrorCount, WarningCount, InformationCount;
  42.     }
  43.  
  44.     public class Package : IDisposable
  45.     {
  46.         public Package(string filePath)
  47.         {
  48.             path = filePath;
  49.             stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
  50.             if (!stream.CanRead || !stream.CanSeek) throw new NotSupportedException("The file stream is terrible!");
  51.             reader = new BinaryReader(stream);
  52.             PackageVersion = reader.ReadInt32();
  53.             if (PackageVersion != 1) throw new NotSupportedException("Unsupported package version: " + PackageVersion);
  54.             var count = reader.ReadInt32();
  55.             Records = new List<FileRecord>(count);
  56.             for (var i = 0; i < count; i++) Records.Add(new FileRecord(reader));
  57.         }
  58.  
  59.         public void ExtractAll(string directory = null)
  60.         {
  61.             if (directory == null) directory = path + "_EXTRACTED";
  62.             Directory.CreateDirectory(directory);
  63.             foreach (var record in Records) Extract(record, Path.Combine(directory, record.ID.ToString()));
  64.         }
  65.         public void Extract(FileRecord record, string filePath)
  66.         {
  67.             int unpackedSize;
  68.             byte[] data;
  69.             lock (reader)
  70.             {
  71.                 stream.Seek(record.Offset, SeekOrigin.Begin);
  72.                 var packedSize = reader.ReadInt32();
  73.                 unpackedSize = reader.ReadInt32();
  74.                 data = reader.ReadBytes(packedSize);
  75.             }
  76.             using (var fileStream = File.Create(filePath))
  77.             using (var memoryStream = new MemoryStream(data))
  78.             using (var deflateStream = new DeflateStream(memoryStream, CompressionMode.Decompress))
  79.             {
  80.                 memoryStream.ReadByte();
  81.                 memoryStream.ReadByte();
  82.                 try
  83.                 {
  84.                     deflateStream.CopyTo(fileStream);
  85.                     if (fileStream.Length != unpackedSize)
  86.                     {
  87.                         Console.WriteLine("WARNING: File size does not match. (item #{0})", record.ID);
  88.                         Program.WarningCount++;
  89.                     }
  90.                     Console.WriteLine("INFO: Unpacking item #{0} done.", record.ID);
  91.                     Program.InformationCount++;
  92.                 }
  93.                 catch (InvalidDataException)
  94.                 {
  95.                     Console.WriteLine("ERROR: Unpacking item #{0} failed.", record.ID);
  96.                     Program.ErrorCount++;
  97.                 }
  98.             }
  99.         }
  100.  
  101.         public void Dispose()
  102.         {
  103.             reader.Close();
  104.             stream.Close();
  105.         }
  106.  
  107.         private readonly FileStream stream;
  108.         private readonly BinaryReader reader;
  109.         private readonly string path;
  110.         public int PackageVersion;
  111.         public List<FileRecord> Records;
  112.     }
  113.  
  114.     public struct FileRecord
  115.     {
  116.         public uint ID, Flags;
  117.         public int Offset, Size;
  118.  
  119.         public FileRecord(BinaryReader reader)
  120.         {
  121.             ID = reader.ReadUInt32();
  122.             Flags = reader.ReadUInt32();
  123.             if (Flags != 1)
  124.             {
  125.                 //throw new NotSupportedException("Unsupported file record flags: " + Flags);
  126.                 Console.WriteLine("WARNING: Unsupported file record flags value {0}. (item #{1})", Flags, ID);
  127.                 Program.WarningCount++;
  128.             }
  129.             Offset = reader.ReadInt32();
  130.             Size = reader.ReadInt32();
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement