コード例 #1
0
    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;
    }
コード例 #2
0
    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");
      }
    }
コード例 #3
0
ファイル: TagWriter.cs プロジェクト: koponk/Cyotek.Data.Nbt
    protected TagWriter(Stream output, NbtOptions options)
      : this()
    {
      if (output == null)
        throw new ArgumentNullException("output");

      this.OutputStream = output;
      this.Options = options;
    }
コード例 #4
0
ファイル: TagReader.cs プロジェクト: koponk/Cyotek.Data.Nbt
    protected TagReader(Stream input, NbtOptions options)
      : this()
    {
      if (input == null)
        throw new ArgumentNullException("input");

      this.InputStream = input;
      this.Options = options;
    }
コード例 #5
0
    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;
    }
コード例 #6
0
ファイル: XmlTagWriter.cs プロジェクト: Craiel/CarbonProjects
        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();
              }
        }
コード例 #7
0
ファイル: TagReader.cs プロジェクト: koponk/Cyotek.Data.Nbt
 TagCompound ITagReader.Load(string fileName, NbtOptions options)
 {
   return this.Load(fileName, options);
 }
コード例 #8
0
ファイル: XmlTagWriter.cs プロジェクト: Craiel/CarbonProjects
        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();
              }
        }
コード例 #9
0
ファイル: XmlTagWriter.cs プロジェクト: Craiel/CarbonProjects
 public XmlTagWriter(Stream stream, NbtOptions options)
     : base(stream, options)
 {
 }
コード例 #10
0
        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);
              }
        }
コード例 #11
0
    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;
    }
コード例 #12
0
ファイル: TagReader.cs プロジェクト: koponk/Cyotek.Data.Nbt
 public abstract TagCompound Load(string fileName, NbtOptions options);
コード例 #13
0
ファイル: TagWriter.cs プロジェクト: Craiel/CarbonProjects
 void ITagWriter.Write(TagCompound tag, string fileName, NbtOptions options)
 {
     this.Write(tag, fileName, options);
 }
コード例 #14
0
    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);
    }
コード例 #15
0
 public BinaryTagWriter(Stream stream, NbtOptions options)
   : base(stream, options)
 { }
コード例 #16
0
ファイル: XmlTagReader.cs プロジェクト: Craiel/CarbonProjects
 public override ITag Read(NbtOptions options)
 {
     return this.Read(options, TagType.None);
 }
コード例 #17
0
ファイル: XmlTagReader.cs プロジェクト: Craiel/CarbonProjects
        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();
              }
        }
コード例 #18
0
ファイル: XmlTagReader.cs プロジェクト: Craiel/CarbonProjects
        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;
        }
コード例 #19
0
ファイル: TagReader.cs プロジェクト: koponk/Cyotek.Data.Nbt
 ITag ITagReader.Read(NbtOptions options)
 {
   return this.Read(options);
 }
コード例 #20
0
ファイル: TagWriter.cs プロジェクト: Craiel/CarbonProjects
 public abstract void Write(ITag value, NbtOptions options);
コード例 #21
0
 public BinaryTagReader(Stream input, NbtOptions options)
   : base(input, options)
 { }
コード例 #22
0
ファイル: TagWriter.cs プロジェクト: Craiel/CarbonProjects
 public abstract void Write(TagCompound tag, string fileName, NbtOptions options);
コード例 #23
0
ファイル: TagReader.cs プロジェクト: koponk/Cyotek.Data.Nbt
 public abstract ITag Read(NbtOptions options);
コード例 #24
0
ファイル: TagWriter.cs プロジェクト: Craiel/CarbonProjects
 void ITagWriter.Write(ITag value, NbtOptions options)
 {
     this.Write(value, options);
 }