Exemple #1
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());
                }
            }
        }
Exemple #2
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);
            }
        }
Exemple #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());
     }
 }
Exemple #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());
     }
 }
Exemple #5
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);
        }
Exemple #6
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);
        }
Exemple #7
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());
                }
            }
        }
Exemple #8
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);
        }
Exemple #9
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);
            }
        }