Пример #1
0
        /// <summary>
        /// Returns a new tag from the given data buffer.
        /// </summary>
        /// <param name="buffer">An array of unsigned bytes containing the tag data.</param>
        /// <param name="index">The index of the starting position in buffer that contains the tag data.</param>
        /// <param name="count">The number of bytes to use from the buffer that are allocated to the tag data.</param>
        /// <returns>Returns a new tag from the given data buffer.</returns>
        public static Tag FromBuffer(byte[] buffer, int index, int count)
        {
            using (MemoryStream ms = new MemoryStream(buffer, index, count))
            {
                BinaryReader reader = new BinaryReader(ms);

                byte    id   = reader.ReadByte();
                TagType type = TagType.GetTagTypeById(id);
                string  name = reader.ReadString().Shift(-32);

                int length;

                if (type.Length < 0)
                {
                    length = reader.ReadInt32();
                }
                else
                {
                    length = type.Length;
                }

                byte[] value = reader.ReadBytes(length);

                return(new Tag(name, type, type.ConvertToValue(value)));
            }
        }
        private void LoadCompound(BinaryReader reader, TagCompound compound)
        {
            while (true)
            {
                byte id = reader.ReadByte();

                if (id == TagType.TagEnd.Identifier)
                {
                    break;
                }
                else
                {
                    string name = reader.ReadString();
                    name = name.Shift(-32);

                    if (id == TagType.TagCompound.Identifier)
                    {
                        TagCompound tag = new TagCompound(name);
                        compound.AddCompound(tag);
                        LoadCompound(reader, tag);
                    }
                    else if (id == TagType.TagList.Identifier)
                    {
                        int count = reader.ReadInt32();
                        byte listId = reader.ReadByte();

                        TagType listType = TagType.GetTagTypeById(listId);

                        Type genericClass = typeof(TagList<>);
                        Type constructedClass = genericClass.MakeGenericType(listType.DataType);

                        dynamic tag = Activator.CreateInstance(constructedClass, name);
                        tag.ListType = TagType.GetTagTypeById(listId);

                        for (int i = 0; i < count; i++)
                        {
                            if (tag.ListType == TagType.TagCompound)
                            {
                                TagCompound c = new TagCompound(null);
                                LoadCompound(reader, c);
                                tag.Add(c);
                            }
                            else
                            {
                                int length = tag.ListType.Length >= 0 ? tag.ListType.Length : reader.ReadInt32();
                                byte[] data = reader.ReadBytes(length);
                                tag.Add(tag.ListType.ConvertToValue(data));
                            }
                        }

                        compound.AddTag(tag);
                    }
                    else
                    {
                        TagType type = TagType.GetTagTypeById(id);

                        int length = type.Length >= 0 ? type.Length : reader.ReadInt32();
                        byte[] data = reader.ReadBytes(length);

                        compound.AddTag(name, type, type.ConvertToValue(data));
                    }
                }
            }
        }