示例#1
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");
      }
    }
示例#2
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;
    }
示例#3
0
    public override void Write(ITag value, NbtOptions options)
    {
      string name;

      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.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");
      }

      _writer.WriteEndElement();
    }
示例#4
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);
    }
示例#5
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.HasFlag(NbtOptions.Header))
      {
        string name;

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

        result.Name = name;
      }

      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;
    }