Advertisement
Brick

MM2 CPVS

Jan 16th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. struct CPVS
  2. {
  3.     int[] Indices;
  4.     byte[] Data;
  5.  
  6.     public int Length => Indices.Length - 1;
  7.  
  8.     public CPVS(BinaryReader reader)
  9.     {
  10.         if (reader.ReadUInt32() != 0x30535650 /*PVS0*/)
  11.         {
  12.             throw new Exception("Invalid Magic");
  13.         }
  14.  
  15.         Indices = new int[reader.ReadUInt32()];
  16.         Indices[0] = 0;
  17.  
  18.         for (int i = 1; i < Indices.Length; ++i)
  19.             Indices[i] = reader.ReadInt32();
  20.  
  21.         Data = reader.ReadBytes((int) (reader.BaseStream.Length - reader.BaseStream.Position));
  22.     }
  23.  
  24.     public byte[] Decompress(int index)
  25.     {
  26.         byte[] result = new byte[512];
  27.  
  28.         for (int input = Indices[index], output = 0; input < Indices[index + 1];)
  29.         {
  30.             byte size = Data[input++];
  31.  
  32.             if (size >= 128) // Copy
  33.             {
  34.                 size -= 127;
  35.  
  36.                 for (int j = 0; j < size; ++j)
  37.                     result[output++] = Data[input++];
  38.             }
  39.             else // Fill
  40.             {
  41.                 byte value = Data[input++];
  42.  
  43.                 for (int j = 0; j < size; ++j)
  44.                     result[output++] = value;
  45.             }
  46.         }
  47.  
  48.         return result;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement