Advertisement
C0BRA

GMOD Data Unpacker

Feb 12th, 2012
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. using SevenZip;
  8. using SevenZip.Compression.LZMA;
  9.  
  10. namespace GMODDataPack
  11. {
  12.     class Program
  13.     {
  14.         static string ReadCString(BinaryReader r)
  15.         {
  16.             StringBuilder sb = new StringBuilder();
  17.             char c = r.ReadChar();
  18.             while (c != '\0')
  19.             {
  20.                 sb.Append(c);
  21.                 c = r.ReadChar();
  22.             }
  23.             return sb.ToString();
  24.         }
  25.  
  26.         static void Main(string[] args)
  27.         {
  28.             if (args.Length == 0) return;
  29.  
  30.             byte[] bytes = File.ReadAllBytes(args[0]);
  31.             BinaryReader r = new BinaryReader(new MemoryStream(bytes));
  32.  
  33.             string lzma = "" + r.ReadChar() + r.ReadChar() + r.ReadChar() + r.ReadChar();
  34.             if(lzma != "LZMA")
  35.                 return;
  36.            
  37.             UInt32 size = r.ReadUInt32();
  38.             UInt32 size_or = r.ReadUInt32();
  39.  
  40.             byte[] lzma_props = r.ReadBytes(5);
  41.             byte[] dp = r.ReadBytes((int)size);
  42.  
  43.             SevenZip.Compression.LZMA.Decoder dec = new SevenZip.Compression.LZMA.Decoder();
  44.             dec.SetDecoderProperties(lzma_props);
  45.  
  46.             MemoryStream ms = new MemoryStream(dp);
  47.             MemoryStream msout = new MemoryStream();
  48.  
  49.             int readsize = int.MaxValue;
  50.             while (true) // Fuck this shit, but it works
  51.             {
  52.                 try
  53.                 {
  54.                     dec.Code(ms, msout, size,
  55.                         readsize, null);
  56.                     if (readsize < 1) break;
  57.                 }
  58.                 catch
  59.                 {
  60.                     readsize = readsize / 10;
  61.                 }
  62.             }
  63.  
  64.             msout.Position = 0;
  65.             r = new BinaryReader(msout);
  66.             byte[] datapack = r.ReadBytes((int)msout.Length);//(int)size_or);
  67.  
  68.             r = new BinaryReader(new MemoryStream(datapack));
  69.             string cats = "" + r.ReadChar() + r.ReadChar() + r.ReadChar() + r.ReadChar();
  70.             if (cats != "CATS")
  71.                 return;
  72.  
  73.             UInt16 vers = r.ReadUInt16();
  74.             UInt16 filecount = r.ReadUInt16();
  75.  
  76.             for (int i = 0; i < filecount; i++)
  77.             {
  78.                 UInt32 fsizetotal = r.ReadUInt32();
  79.                 string fname = ReadCString(r);
  80.                 byte[] hash = r.ReadBytes(16);
  81.  
  82.                 uint fsize = fsizetotal - (uint)(fname.Length + 1 + 16); /*+1 for \0*/
  83.  
  84.                 if (fsize == 1)
  85.                 {
  86.                     Console.WriteLine("Not writing {0} (Null)", fname);
  87.                     r.ReadByte();
  88.                 }
  89.                 else
  90.                 {
  91.                     byte[] filecont = r.ReadBytes((int)fsize);
  92.  
  93.                     string fnametowrite = Path.GetFileName(args[0]).Replace('.', '_') + "/" + fname;
  94.                     Directory.CreateDirectory(Path.GetDirectoryName(fnametowrite));
  95.  
  96.                     File.WriteAllBytes(fnametowrite, filecont);
  97.                     Console.WriteLine("Writing {0}...", fname);
  98.                 }
  99.             }
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement