public static ITag CreateTag(TagType tagType, string name, object defaultValue) { ITag result; switch (tagType) { case TagType.Byte: result = new TagByte(); break; case TagType.Short: result = new TagShort(); break; case TagType.Int: result = new TagInt(); break; case TagType.Long: result = new TagLong(); break; case TagType.Float: result = new TagFloat(); break; case TagType.Double: result = new TagDouble(); break; case TagType.ByteArray: result = new TagByteArray(); break; case TagType.String: result = new TagString(); break; case TagType.List: result = new TagList(); break; case TagType.Compound: result = new TagCompound(); break; case TagType.IntArray: result = new TagIntArray(); break; case TagType.End: result = new TagEnd(); break; default: throw new ArgumentException(string.Format("Unrecognized tag type: {0}", tagType)); } result.Name = name; if (defaultValue != null) { result.Value = defaultValue; } return result; }
public override TagCollection ReadCollection(TagList owner) { TagCollection tags; int length; owner.ListType = (TagType)this.ReadByte(); tags = new TagCollection(owner, owner.ListType); length = this.ReadInt(); for (int i = 0; i < length; i++) { ITag tag; switch (owner.ListType) { case TagType.Byte: tag = TagFactory.CreateTag(TagType.Byte, this.ReadByte()); break; case TagType.ByteArray: tag = TagFactory.CreateTag(TagType.ByteArray, this.ReadByteArray()); break; case TagType.Compound: tag = TagFactory.CreateTag(TagType.Compound); tag.Value = this.ReadDictionary((TagCompound)tag); break; case TagType.Double: tag = TagFactory.CreateTag(TagType.Double, this.ReadDouble()); break; case TagType.End: tag = new TagEnd(); break; case TagType.Float: tag = TagFactory.CreateTag(TagType.Float, this.ReadFloat()); break; case TagType.Int: tag = TagFactory.CreateTag(TagType.Int, this.ReadInt()); break; case TagType.IntArray: tag = TagFactory.CreateTag(TagType.IntArray, this.ReadIntArray()); break; case TagType.List: tag = TagFactory.CreateTag(TagType.List); tag.Value = this.ReadCollection((TagList)tag); break; case TagType.Long: tag = TagFactory.CreateTag(TagType.Long, this.ReadLong()); break; case TagType.Short: tag = TagFactory.CreateTag(TagType.Short, this.ReadShort()); break; case TagType.String: tag = TagFactory.CreateTag(TagType.String, this.ReadString()); break; default: throw new InvalidDataException("Invalid list type."); } tags.Add(tag); } return tags; }
/// <summary> /// Creates a tag for the specified parameters. /// </summary> /// <exception cref="ArgumentException">Thrown if <paramref name="tagType"/> is not valid.</exception> /// <param name="tagType">Type of the tag to create.</param> /// <param name="name">The name of the tag create.</param> /// <param name="listType">The type of items a list holds.</param> /// <returns> /// A new tag using the specified parameters. /// </returns> public static Tag CreateTag(string name, TagType tagType, TagType listType) { Tag result; if (tagType != TagType.List) { if (listType != TagType.None) { throw new ArgumentException("Only lists can have a list type.", nameof(listType)); } switch (tagType) { case TagType.Byte: result = new TagByte(name); break; case TagType.Short: result = new TagShort(name); break; case TagType.Int: result = new TagInt(name); break; case TagType.Long: result = new TagLong(name); break; case TagType.Float: result = new TagFloat(name); break; case TagType.Double: result = new TagDouble(name); break; case TagType.ByteArray: result = new TagByteArray(name); break; case TagType.String: result = new TagString(name); break; case TagType.Compound: result = new TagCompound(name); break; case TagType.IntArray: result = new TagIntArray(name); break; case TagType.End: result = new TagEnd(); break; default: throw new ArgumentException("Unrecognized or unsupported tag type.", nameof(tagType)); } } else { result = new TagList(name, listType); } return(result); }