Brick icon

MM2 CPVS

Brick | PRO | 01/17/18 03:31:28 AM UTC | 0 ⭐ | 217 👁️ | Never ⏰ | []
C# |

1.21 KB

|

None

|

0 👍

/

0 👎

struct CPVS
{
    int[] Indices;
    byte[] Data;
 
    public int Length => Indices.Length - 1;
 
    public CPVS(BinaryReader reader)
    {
        if (reader.ReadUInt32() != 0x30535650 /*PVS0*/)
        {
            throw new Exception("Invalid Magic");
        }
 
        Indices = new int[reader.ReadUInt32()];
        Indices[0] = 0;
 
        for (int i = 1; i < Indices.Length; ++i)
            Indices[i] = reader.ReadInt32();
 
        Data = reader.ReadBytes((int) (reader.BaseStream.Length - reader.BaseStream.Position));
    }
 
    public byte[] Decompress(int index)
    {
        byte[] result = new byte[512];
 
        for (int input = Indices[index], output = 0; input < Indices[index + 1];)
        {
            byte size = Data[input++];
 
            if (size >= 128) // Copy
            {
                size -= 127;
 
                for (int j = 0; j < size; ++j)
                    result[output++] = Data[input++];
            }
            else // Fill
            {
                byte value = Data[input++];
 
                for (int j = 0; j < size; ++j)
                    result[output++] = value;
            }
        }
 
        return result;
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎