using System; using System.IO; using System.Threading.Tasks; using System.Collections.Generic; using System.Linq; using LibAC.NBT; namespace LibAC.NBT.API { public class NbtIo { private static ByteLayer _io = new ByteLayer(); private static void _Read(string file) { _io = new ByteLayer(); _io.ReadFromFile(file); } public static CompoundTag Read(string file) { _Read(file); if (_io.ReadByte() == (byte)TagType.Compound) { _io.ResetPosition(); return (CompoundTag)Tag.ReadNamedTag(_io); } else { return ReadCompressed(file); } } public static CompoundTag ReadCompressed(string file) { _io = new ByteLayer(); _io.ReadFromFile(file); _io.Decompress(); _io.ResetPosition(); return (CompoundTag)Tag.ReadNamedTag(_io); } public static void Write(string file, CompoundTag tag) { _io = new ByteLayer(); Tag.WriteNamedTag(tag, _io); _io.WriteToFile(file); } public static void WriteCompressedAsync(string file, CompoundTag tag) { _io = new ByteLayer(); Tag.WriteNamedTag(tag, _io); _io.Compress(); _io.WriteToFile(file); } public static ByteLayer GetStream() { return _io; } public static string WriteBase64String(CompoundTag tag) { _io = new ByteLayer(); Tag.WriteNamedTag(tag, _io); _io.Compress(); return Convert.ToBase64String(_io.Bytes); } public static CompoundTag ReadBase64String(string encoded) { _io = new ByteLayer(); byte[] bytes = Convert.FromBase64String(encoded); foreach (byte b in bytes) { _io.WriteByte(b); } _io.Decompress(); _io.ResetPosition(); return (CompoundTag)Tag.ReadNamedTag(_io); } public static byte[] WriteToStreamCompressed(CompoundTag tag) { _io = new ByteLayer(); Tag.WriteNamedTag(tag, _io); _io.Compress(); return _io.Bytes; } public static CompoundTag ReadFromStreamCompressed(byte[] list) { _io = new ByteLayer(); try { _io.WriteBytes(list.ToList()); _io.ResetPosition(); _io.Decompress(); _io.ResetPosition(); } catch (Exception e) { Console.WriteLine(e); } return (CompoundTag)Tag.ReadNamedTag(_io); } public static async Task WriteToStreamAsync(CompoundTag tag) { _io = new ByteLayer(); Tag.WriteNamedTag(tag, _io); return _io.Bytes; } public static CompoundTag ReadFromStream(byte[] list) { _io = new ByteLayer(); try { _io.WriteBytes(list.ToList()); _io.ResetPosition(); } catch (Exception e) { Console.WriteLine(e); } return (CompoundTag)Tag.ReadNamedTag(_io); } } }