Ejemplo n.º 1
0
        public void WriteTag(NbtTag tag)
        {
            var name = tag.Name ?? "";

            _writer.Write((sbyte)tag.Type);
            _writer.Write(name.Length);
            _writer.Write(name.ToCharArray());
            WritePayload(tag);
        }
Ejemplo n.º 2
0
        public static string ToStringedNbt(this NbtTag tag, bool format = false)
        {
            var builder = new StringBuilder();
            var writer  = new SNbtWriter(new StringWriter(builder), format);

            void CheckTag(NbtTag nbtTag)
            {
                var value     = nbtTag as NbtValue;
                var needClose = false;

                switch (nbtTag.Type)
                {
                case NbtTagType.End:
                    break;

                case NbtTagType.Byte:
                    writer.WriteByteTag((sbyte)value !.Value, nbtTag.Name);
                    break;

                case NbtTagType.Short:
                    writer.WriteShortTag((short)value !.Value, nbtTag.Name);
                    break;

                case NbtTagType.Int:
                    writer.WriteIntTag((int)value !.Value, nbtTag.Name);
                    break;

                case NbtTagType.Long:
                    writer.WriteLongTag((long)value !.Value, nbtTag.Name);
                    break;

                case NbtTagType.Float:
                    writer.WriteFloatTag((float)value !.Value, nbtTag.Name);
                    break;

                case NbtTagType.Double:
                    writer.WriteDoubleTag((double)value !.Value, nbtTag.Name);
                    break;

                case NbtTagType.ByteArray:
                    writer.WriteByteArrayTag((NbtByteArray)nbtTag, nbtTag.Name);
                    break;

                case NbtTagType.String:
                    writer.WriteStringTag((string)value !.Value, nbtTag.Name);
                    break;

                case NbtTagType.List:
                    writer.WriteListTagStart(nbtTag.Name);
                    needClose = true;
                    break;

                case NbtTagType.Compound:
                    writer.WriteCompoundTagStart(nbtTag.Name);
                    needClose = true;
                    break;

                case NbtTagType.IntArray:
                    writer.WriteIntArrayTag((NbtIntArray)nbtTag, nbtTag.Name);
                    break;

                case NbtTagType.LongArray:
                    writer.WriteLongArrayTag((NbtLongArray)nbtTag, nbtTag.Name);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(nbtTag));
                }

                foreach (var children in nbtTag)
                {
                    CheckTag(children);
                }

                if (needClose)
                {
                    writer.WriteEndTag();
                }
            }

            CheckTag(tag);

            return(builder.ToString());
        }
Ejemplo n.º 3
0
        private void WritePayload(NbtTag tag)
        {
            switch (tag.Type)
            {
            case NbtTagType.End:
                return;

            case NbtTagType.Byte:
            {
                var value = (sbyte)((NbtValue)tag).Value;
                _writer.Write(value);
                return;
            }

            case NbtTagType.Short:
            {
                var value = (short)((NbtValue)tag).Value;
                _writer.Write(value);
                return;
            }

            case NbtTagType.Int:
            {
                var value = (int)((NbtValue)tag).Value;
                _writer.Write(value);
                return;
            }

            case NbtTagType.Long:
            {
                var value = (long)((NbtValue)tag).Value;
                _writer.Write(value);
                return;
            }

            case NbtTagType.Float:
            {
                var value = (float)((NbtValue)tag).Value;
                _writer.Write(value);
                return;
            }

            case NbtTagType.Double:
            {
                var value = (double)((NbtValue)tag).Value;
                _writer.Write(value);
                return;
            }

            case NbtTagType.ByteArray:
            {
                var value = ((NbtArray <sbyte>)tag).Value;
                for (var i = 0; i < value.Length; i++)
                {
                    _writer.Write(value[i]);
                }
                return;
            }

            case NbtTagType.String:
            {
                var value = ((string)((NbtValue)tag).Value).ToCharArray();
                _writer.Write(value);
                return;
            }

            case NbtTagType.List:
            {
                var value = (NbtList)tag;
                _writer.Write((sbyte)value.ContentType);
                _writer.Write(value.Count);
                foreach (var v in value)
                {
                    WritePayload(v);
                }
                return;
            }

            case NbtTagType.Compound:
            {
                var value = (NbtCompound)tag;
                foreach (var(_, v) in value)
                {
                    WriteTag(v);
                }
                return;
            }

            case NbtTagType.IntArray:
            {
                var value = ((NbtArray <int>)tag).Value;
                for (var i = 0; i < value.Length; i++)
                {
                    _writer.Write(value[i]);
                }
                return;
            }

            case NbtTagType.LongArray:
            {
                var value = ((NbtArray <long>)tag).Value;
                for (var i = 0; i < value.Length; i++)
                {
                    _writer.Write(value[i]);
                }
                return;
            }

            default:
                throw new NbtException($"Unknow tag. Tag id: {(sbyte)tag.Type}");
            }
        }