Beispiel #1
0
        public static CompoundTag ReadZLIBFile(string fileName, NBTEndian endian = NBTEndian.LITTLE_ENDIAN)
        {
            byte[] bytes   = File.ReadAllBytes(fileName);
            byte[] payload = new byte[0];
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                if (ms.ReadByte() != 0x78)
                {
                    throw new FormatException();
                }
                ms.ReadByte();
                using (ZlibStream ds = new ZlibStream(ms, CompressionMode.Decompress, false))
                {
                    MemoryStream c = new MemoryStream();
                    ds.CopyTo(c);
                    payload = c.ToArray();
                    c.Close();
                }
            }

            CompoundTag tag = new CompoundTag();

            using (NBTStream nbt = new NBTStream(payload, endian))
            {
                tag.Read(nbt);
                return(tag);
            }
        }
Beispiel #2
0
        public static byte[] WriteZLIBFile(CompoundTag tag, NBTEndian endian = NBTEndian.LITTLE_ENDIAN)
        {
            using (NBTStream stream = new NBTStream(endian))
            {
                tag.Write(stream);

                int sum = 0;
                using (MemoryStream ms = new MemoryStream())
                {
                    ms.WriteByte(0x78);
                    ms.WriteByte(0x01);
                    using (ZlibStream zlib = new ZlibStream(ms, CompressionMode.Compress, true))
                    {
                        zlib.Write(stream.ToArray(), 0, (int)stream.Length);
                        sum = zlib.Checksum;
                    }

                    byte[] sumBytes = BitConverter.GetBytes(sum);
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(sumBytes);
                    }
                    ms.Write(sumBytes, 0, sumBytes.Length);

                    return(ms.ToArray());
                }
            }
        }
Beispiel #3
0
 public static void WriteRawFile(string fileName, CompoundTag tag, NBTEndian endian = NBTEndian.LITTLE_ENDIAN)
 {
     using (NBTStream stream = new NBTStream(endian))
     {
         tag.Write(stream);
         File.WriteAllBytes(fileName, stream.ToArray());
     }
 }
Beispiel #4
0
 public static byte[] WriteTag(CompoundTag tag, NBTEndian endian = NBTEndian.LITTLE_ENDIAN, bool isNetwork = false)
 {
     using (NBTStream stream = new NBTStream(endian))
     {
         stream.Network = isNetwork;
         tag.WriteTag(stream);
         return(stream.ToArray());
     }
 }
Beispiel #5
0
 static bool IsBigEndian(NBTEndian e)
 {
     if (e == NBTEndian.BIG_ENDIAN)
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
Beispiel #6
0
        public static CompoundTag ReadTag(byte[] bytes, NBTEndian endian = NBTEndian.LITTLE_ENDIAN, bool isNetwork = false)
        {
            CompoundTag tag = new CompoundTag();

            using (NBTStream stream = new NBTStream(bytes, endian))
            {
                stream.Network = isNetwork;
                tag.ReadTag(stream);
            }

            return(tag);
        }
Beispiel #7
0
        public static CompoundTag ReadRawFile(string fileName, NBTEndian endian = NBTEndian.LITTLE_ENDIAN)
        {
            CompoundTag tag = new CompoundTag();

            byte[] bytes = File.ReadAllBytes(fileName);
            using (NBTStream stream = new NBTStream(bytes, endian))
            {
                tag.Read(stream);
            }

            return(tag);
        }
Beispiel #8
0
        public static void WriteGZIPFile(string fileName, CompoundTag tag, NBTEndian endian = NBTEndian.LITTLE_ENDIAN)
        {
            using (NBTStream stream = new NBTStream(endian))
            {
                tag.Write(stream);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (GZipStream gs = new GZipStream(ms, CompressionMode.Compress, true))
                    {
                        gs.Write(stream.ToArray(), 0, (int)stream.Length);
                    }

                    File.WriteAllBytes(fileName, ms.ToArray());
                }
            }
        }
Beispiel #9
0
        public static CompoundTag ReadTag(byte[] bytes, out int offset, NBTEndian endian = NBTEndian.LITTLE_ENDIAN,
                                          bool isNetwork = false)
        {
            CompoundTag tag    = new CompoundTag();
            NBTStream   stream = new NBTStream(bytes, endian);

            try
            {
                stream.Network = isNetwork;
                tag.ReadTag(stream);
            }
            catch
            {
            }
            finally
            {
                offset = stream.Offset;
                stream.Dispose();
            }

            return(tag);
        }
Beispiel #10
0
        public static CompoundTag ReadGZIPFile(string fileName, NBTEndian endian = NBTEndian.LITTLE_ENDIAN)
        {
            byte[] bytes   = File.ReadAllBytes(fileName);
            byte[] payload = new byte[0];
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                using (GZipStream gz = new GZipStream(ms, CompressionMode.Decompress, true))
                {
                    MemoryStream c = new MemoryStream();
                    gz.CopyTo(c);
                    payload = c.ToArray();
                    c.Close();
                }
            }

            CompoundTag tag = new CompoundTag();

            using (NBTStream nbt = new NBTStream(payload, endian))
            {
                tag.Read(nbt);
                return(tag);
            }
        }
Beispiel #11
0
 public NBTStream(byte[] buffer, NBTEndian endian)
 {
     this.Swap = (IsBigEndian(endian) == BitConverter.IsLittleEndian);
     this.WriteBytes(buffer);
     this.Reset();
 }
Beispiel #12
0
 public NBTStream(NBTEndian endian = NBTEndian.LITTLE_ENDIAN) : this(new byte[0], endian)
 {
 }