public override TagCompound Load(string fileName, NbtOptions options)
    {
      TagCompound tag;
      BinaryTagReader reader;

      if (string.IsNullOrEmpty(fileName))
        throw new ArgumentNullException("fileName");

      if (!File.Exists(fileName))
        throw new FileNotFoundException("Cannot find source file.", fileName);

      //Check if gzipped stream
      try
      {
        using (FileStream input = File.OpenRead(fileName))
        {
          using (GZipStream gzipStream = new GZipStream(input, CompressionMode.Decompress))
          {
            reader = new BinaryTagReader(gzipStream, NbtOptions.Header);
            tag = (TagCompound)reader.Read();
          }
        }
      }
      catch (Exception)
      {
        tag = null;
      }

      if (tag != null)
        return tag;

      //Try Deflate stream
      try
      {
        using (FileStream input = File.OpenRead(fileName))
        {
          using (DeflateStream deflateStream = new DeflateStream(input, CompressionMode.Decompress))
          {
            reader = new BinaryTagReader(deflateStream, NbtOptions.Header);
            tag = (TagCompound)reader.Read();
          }
        }
      }
      catch (Exception)
      {
        tag = null;
      }

      if (tag != null)
        return tag;

      //Assume uncompressed stream
      using (FileStream input = File.OpenRead(fileName))
      {
        reader = new BinaryTagReader(input, NbtOptions.Header);
        tag = (TagCompound)reader.Read();
      }

      return tag;
    }
    public override void Write(ITag value, NbtOptions options)
    {
      if (options.HasFlag(NbtOptions.Header) && value.Type != TagType.End)
        this.WriteHeader(value);

      switch (value.Type)
      {
        case TagType.End:
          this.WriteEnd();
          break;

        case TagType.Byte:
          this.Write((byte)value.Value);
          break;

        case TagType.Short:
          this.Write((short)value.Value);
          break;

        case TagType.Int:
          this.Write((int)value.Value);
          break;

        case TagType.Long:
          this.Write((long)value.Value);
          break;

        case TagType.Float:
          this.Write((float)value.Value);
          break;

        case TagType.Double:
          this.Write((double)value.Value);
          break;

        case TagType.ByteArray:
          this.Write((byte[])value.Value);
          break;

        case TagType.String:
          this.Write((string)value.Value);
          break;

        case TagType.List:
          this.Write((TagCollection)value.Value);
          break;

        case TagType.Compound:
          this.Write((TagDictionary)value.Value);
          break;

        case TagType.IntArray:
          this.Write((int[])value.Value);
          break;

        default:
          throw new ArgumentException("Unrecognized or unsupported tag type.", "value");
      }
    }
Exemple #3
0
    protected TagWriter(Stream output, NbtOptions options)
      : this()
    {
      if (output == null)
        throw new ArgumentNullException("output");

      this.OutputStream = output;
      this.Options = options;
    }
Exemple #4
0
    protected TagReader(Stream input, NbtOptions options)
      : this()
    {
      if (input == null)
        throw new ArgumentNullException("input");

      this.InputStream = input;
      this.Options = options;
    }
    public override TagCompound Load(string fileName, NbtOptions options)
    {
      TagCompound result;

      if (string.IsNullOrEmpty(fileName))
        throw new ArgumentNullException("fileName");

      this.Options = options;

      using (Stream fileStream = File.OpenRead(fileName))
      {
        this.InputStream = fileStream;
        result = (TagCompound)this.Read(options);
      }

      return result;
    }
        public override void Write(ITag value, NbtOptions options)
        {
            string name;

              if ((options & NbtOptions.SingleUse) != 0)
              {
            this.Open();
              }

              name = value.Name;
              if (string.IsNullOrEmpty(name))
              {
            name = "tag";
              }

              if (XmlConvert.EncodeName(name) == name)
              {
            _writer.WriteStartElement(name);
              }
              else
              {
            _writer.WriteStartElement("tag");
            _writer.WriteAttributeString("name", name);
              }

              if ((options & NbtOptions.ReadHeader) != 0 && value.Type != TagType.End)
              {
            this.WriteHeader(value);
              }

              switch (value.Type)
              {
            case TagType.End:
              this.WriteEnd();
              break;

            case TagType.Byte:
              this.Write((byte)value.Value);
              break;

            case TagType.Short:
              this.Write((short)value.Value);
              break;

            case TagType.Int:
              this.Write((int)value.Value);
              break;

            case TagType.Long:
              this.Write((long)value.Value);
              break;

            case TagType.Float:
              this.Write((float)value.Value);
              break;

            case TagType.Double:
              this.Write((double)value.Value);
              break;

            case TagType.ByteArray:
              this.Write((byte[])value.Value);
              break;

            case TagType.String:
              this.Write((string)value.Value);
              break;

            case TagType.List:
              this.Write((TagCollection)value.Value);
              break;

            case TagType.Compound:
              this.Write((TagDictionary)value.Value);
              break;

            case TagType.IntArray:
              this.Write((int[])value.Value);
              break;

            default:
              throw new ArgumentException("Unrecognized or unsupported tag type.", "value");
              }

              _writer.WriteEndElement();

              if ((options & NbtOptions.SingleUse) != 0)
              {
            this.Close();
              }
        }
Exemple #7
0
 TagCompound ITagReader.Load(string fileName, NbtOptions options)
 {
   return this.Load(fileName, options);
 }
        public override void Write(TagCompound tag, string fileName, NbtOptions options)
        {
            if (string.IsNullOrEmpty(fileName))
              {
            throw new ArgumentNullException("fileName");
              }

              if (tag == null)
              {
            throw new ArgumentNullException("tag");
              }

              this.Options = options;

              using (Stream fileStream = File.Create(fileName))
              {
            this.OutputStream = fileStream;
            this.Open();
            this.Write(tag, options);
            this.Close();
              }
        }
 public XmlTagWriter(Stream stream, NbtOptions options)
     : base(stream, options)
 {
 }
        public override void Write(TagCompound tag, string fileName, NbtOptions options)
        {
            if (string.IsNullOrEmpty(fileName))
              {
            throw new ArgumentNullException("fileName");
              }

              if (tag == null)
              {
            throw new ArgumentNullException("tag");
              }

              this.Options = options;

              if ((options & NbtOptions.Compress) != 0)
              {
            this.WriteCompressed(tag, fileName);
              }
              else
              {
            this.WriteUncompressed(tag, fileName);
              }
        }
    public override ITag Read(NbtOptions options)
    {
      int rawType;
      ITag result;
      object value;

      rawType = this.InputStream.ReadByte();
      result = TagFactory.CreateTag((TagType)rawType);

      if (result.Type != TagType.End && options.HasFlag(NbtOptions.Header))
        result.Name = this.ReadString();

      switch (result.Type)
      {
        case TagType.End:
          value = null;
          break;

        case TagType.Byte:
          value = this.ReadByte();
          break;

        case TagType.Short:
          value = this.ReadShort();
          break;

        case TagType.Int:
          value = this.ReadInt();
          break;

        case TagType.IntArray:
          value = this.ReadIntArray();
          break;

        case TagType.Long:
          value = this.ReadLong();
          break;

        case TagType.Float:
          value = this.ReadFloat();
          break;

        case TagType.Double:
          value = this.ReadDouble();
          break;

        case TagType.ByteArray:
          value = this.ReadByteArray();
          break;

        case TagType.String:
          value = this.ReadString();
          break;

        case TagType.List:
          value = this.ReadCollection((TagList)result);
          break;

        case TagType.Compound:
          value = this.ReadDictionary((TagCompound)result);
          break;

        default:
          throw new InvalidDataException(string.Format("Unrecognized tag type: {0}", rawType));
      }

      result.Value = value;

      return result;
    }
Exemple #12
0
 public abstract TagCompound Load(string fileName, NbtOptions options);
Exemple #13
0
 void ITagWriter.Write(TagCompound tag, string fileName, NbtOptions options)
 {
     this.Write(tag, fileName, options);
 }
    public override void Write(TagCompound tag, string fileName, NbtOptions options)
    {
      if (string.IsNullOrEmpty(fileName))
        throw new ArgumentNullException("fileName");
      else if (tag == null)
        throw new ArgumentNullException("tag");

      this.Options = options;

      if (options.HasFlag(NbtOptions.Compress))
        this.WriteCompressed(tag, fileName);
      else
        this.WriteUncompressed(tag, fileName);
    }
 public BinaryTagWriter(Stream stream, NbtOptions options)
   : base(stream, options)
 { }
Exemple #16
0
 public override ITag Read(NbtOptions options)
 {
     return this.Read(options, TagType.None);
 }
Exemple #17
0
        private void LoadChildren(ICollection<ITag> value, NbtOptions options, TagType listType)
        {
            while (_reader.NodeType != XmlNodeType.EndElement && _reader.NodeType != XmlNodeType.None && !_reader.IsEmptyElement)
              {
            _reader.Read();

            if (_reader.NodeType == XmlNodeType.Element)
            {
              value.Add(this.Read(options, listType));
            }
              }

              if (_reader.NodeType == XmlNodeType.EndElement)
              {
            _reader.Read();
              }
        }
Exemple #18
0
        protected ITag Read(NbtOptions options, TagType defaultTagType)
        {
            ITag result;
              TagType type;

              if (defaultTagType != TagType.None)
              {
            type = defaultTagType;
              }
              else
              {
            string typeName;

            typeName = _reader.GetAttribute("type");
            if (string.IsNullOrEmpty(typeName))
            {
              throw new InvalidDataException("Missing type attribute, unable to determine tag type.");
            }

            type = (TagType)Enum.Parse(typeof(TagType), typeName, true);
              }
              result = TagFactory.CreateTag(type);

              if ((options & NbtOptions.ReadHeader) != 0)
              {
            string name;

            name = _reader.GetAttribute("name");
            if (string.IsNullOrEmpty(name))
            {
              name = _reader.Name;
            }

            result.Name = name;
              }

              if ((options & NbtOptions.HeaderOnly) == 0)
              {
            switch (type)
            {
              case TagType.Byte:
            result.Value = this.ReadByte();
            break;

              case TagType.Short:
            result.Value = this.ReadShort();
            break;

              case TagType.Int:
            result.Value = this.ReadInt();
            break;

              case TagType.Long:
            result.Value = this.ReadLong();
            break;

              case TagType.Float:
            result.Value = this.ReadFloat();
            break;

              case TagType.Double:
            result.Value = this.ReadDouble();
            break;

              case TagType.ByteArray:
            result.Value = this.ReadByteArray();
            break;

              case TagType.String:
            result.Value = this.ReadString();
            break;

              case TagType.List:
            result.Value = this.ReadCollection((TagList)result);
            break;

              case TagType.Compound:
            result.Value = this.ReadDictionary((TagCompound)result);
            break;

              case TagType.IntArray:
            result.Value = this.ReadIntArray();
            break;

              default:
            throw new InvalidDataException(string.Format("Unrecognized tag type: {0}", type));
            }
              }

              return result;
        }
Exemple #19
0
 ITag ITagReader.Read(NbtOptions options)
 {
   return this.Read(options);
 }
Exemple #20
0
 public abstract void Write(ITag value, NbtOptions options);
 public BinaryTagReader(Stream input, NbtOptions options)
   : base(input, options)
 { }
Exemple #22
0
 public abstract void Write(TagCompound tag, string fileName, NbtOptions options);
Exemple #23
0
 public abstract ITag Read(NbtOptions options);
Exemple #24
0
 void ITagWriter.Write(ITag value, NbtOptions options)
 {
     this.Write(value, options);
 }