예제 #1
0
        private static void AddSnbtList(NbtList tag, SnbtOptions options, StringBuilder sb, string indent_string, int indent_level, bool include_name)
        {
            AddIndents(sb, indent_string, indent_level);
            if (include_name)
            {
                sb.Append(GetNameBeforeValue(tag, options));
            }
            bool compressed = ShouldCompressListOf(tag.ListType);

            if (compressed)
            {
                sb.Append(ListToString("", x => x.ToSnbt(options, include_name: false), tag, options));
            }
            else
            {
                sb.Append(LIST_OPEN);
                if (tag.Count > 0)
                {
                    sb.Append(Environment.NewLine);
                    for (int i = 0; i < tag.Count; i++)
                    {
                        AddSnbt(((NbtContainerTag)tag)[i], options, sb, indent_string, indent_level + 1, false);
                        if (i < tag.Count - 1)
                        {
                            sb.Append(VALUE_SEPARATOR);
                        }
                        sb.Append(Environment.NewLine);
                    }
                    AddIndents(sb, indent_string, indent_level);
                }
                sb.Append(LIST_CLOSE);
            }
        }
예제 #2
0
 private static void AddSnbtCompound(NbtCompound tag, SnbtOptions options, StringBuilder sb, string indent_string, int indent_level, bool include_name)
 {
     AddIndents(sb, indent_string, indent_level);
     if (include_name)
     {
         sb.Append(GetNameBeforeValue(tag, options));
     }
     sb.Append(COMPOUND_OPEN);
     if (tag.Count > 0)
     {
         sb.Append(Environment.NewLine);
         var children = tag.Tags.ToArray();
         for (int i = 0; i < children.Length; i++)
         {
             AddSnbt(children[i], options, sb, indent_string, indent_level + 1, true);
             if (i < children.Length - 1)
             {
                 sb.Append(VALUE_SEPARATOR);
             }
             sb.Append(Environment.NewLine);
         }
         AddIndents(sb, indent_string, indent_level);
     }
     sb.Append('}');
 }
예제 #3
0
 private static string GetNameBeforeValue(NbtTag tag, SnbtOptions options)
 {
     if (tag.Name == null)
     {
         return(String.Empty);
     }
     return(GetName(tag, options) + NAME_VALUE_SEPARATOR + (options.Minified ? String.Empty : VALUE_SPACING));
 }
예제 #4
0
        // convert a tag to its string form
        // expanded: for compounds and lists of non-numeric type, creates pretty indented structure. for all tags, causes spaces between values
        // delimit: for numeric types, determines whether the suffix should be included. for strings, determines whether the value should be quoted
        // include_name: whether to put the tag's name (if it has one) in front of its value
        public static string ToSnbt(this NbtTag tag, SnbtOptions options, bool include_name = false)
        {
            string name = include_name ? GetNameBeforeValue(tag, options) : String.Empty;

            if (tag is NbtByte b)
            {
                return(name + b.ToSnbt(options));
            }
            if (tag is NbtShort s)
            {
                return(name + s.ToSnbt(options));
            }
            if (tag is NbtInt i)
            {
                return(name + i.ToSnbt(options));
            }
            if (tag is NbtLong l)
            {
                return(name + l.ToSnbt(options));
            }
            if (tag is NbtFloat f)
            {
                return(name + f.ToSnbt(options));
            }
            if (tag is NbtDouble d)
            {
                return(name + d.ToSnbt(options));
            }
            if (tag is NbtString str)
            {
                return(name + str.ToSnbt(options));
            }
            if (tag is NbtByteArray ba)
            {
                return(name + ba.ToSnbt(options));
            }
            if (tag is NbtIntArray ia)
            {
                return(name + ia.ToSnbt(options));
            }
            if (tag is NbtLongArray la)
            {
                return(name + la.ToSnbt(options));
            }
            if (tag is NbtList list)
            {
                return(name + list.ToSnbt(options));
            }
            if (tag is NbtCompound compound)
            {
                return(name + compound.ToSnbt(options));
            }
            throw new ArgumentException($"Can't convert tag of type {tag.TagType} to SNBT");
        }
예제 #5
0
 public static string ToSnbt(this NbtList tag, SnbtOptions options)
 {
     if (options.Minified)
     {
         return(ListToString("", x => x.ToSnbt(options, include_name: false), tag, options));
     }
     else
     {
         var sb = new StringBuilder();
         AddSnbtList(tag, options, sb, INDENTATION, 0, false);
         return(sb.ToString());
     }
 }
예제 #6
0
 // add contents of tag to stringbuilder
 // used for aligning indents for multiline compounds and lists
 private static void AddSnbt(NbtTag tag, SnbtOptions options, StringBuilder sb, string indent_string, int indent_level, bool include_name)
 {
     if (tag is NbtCompound compound)
     {
         AddSnbtCompound(compound, options, sb, indent_string, indent_level, include_name);
     }
     else if (tag is NbtList list)
     {
         AddSnbtList(list, options, sb, indent_string, indent_level, include_name);
     }
     else
     {
         AddIndents(sb, indent_string, indent_level);
         sb.Append(tag.ToSnbt(options, include_name: include_name));
     }
 }
예제 #7
0
        public static string ToSnbt(this NbtCompound tag, SnbtOptions options)
        {
            var sb = new StringBuilder();

            if (options.Minified)
            {
                sb.Append(COMPOUND_OPEN);
                sb.Append(String.Join(VALUE_SEPARATOR.ToString(), tag.Select(x => x.ToSnbt(options, include_name: true)).ToArray()));
                sb.Append(COMPOUND_CLOSE);
            }
            else
            {
                AddSnbtCompound(tag, options, sb, INDENTATION, 0, false);
            }
            return(sb.ToString());
        }
예제 #8
0
        public static string ToSnbt(this NbtDouble tag, SnbtOptions options)
        {
            string result;

            if (double.IsPositiveInfinity(tag.Value))
            {
                result = "Infinity";
            }
            else if (double.IsNegativeInfinity(tag.Value))
            {
                result = "-Infinity";
            }
            else if (double.IsNaN(tag.Value))
            {
                result = "NaN";
            }
            else
            {
                result = DataUtils.DoubleToString(tag.Value);
            }
            return(result + OptionalSuffix(options, DOUBLE_SUFFIX));
        }
예제 #9
0
        public static string ToSnbt(this NbtFloat tag, SnbtOptions options)
        {
            string result;

            if (float.IsPositiveInfinity(tag.Value))
            {
                result = "Infinity";
            }
            else if (float.IsNegativeInfinity(tag.Value))
            {
                result = "-Infinity";
            }
            else if (float.IsNaN(tag.Value))
            {
                result = "NaN";
            }
            else
            {
                result = DataUtils.FloatToString(tag.Value);
            }
            return(result + OptionalSuffix(options, FLOAT_SUFFIX));
        }
예제 #10
0
 public static string ToSnbt(this NbtIntArray tag, SnbtOptions options)
 {
     return(ListToString("" + INT_ARRAY_PREFIX + ARRAY_DELIMITER, x => x.ToString(), tag.Value, options));
 }
예제 #11
0
 public static string ToSnbt(this NbtShort tag, SnbtOptions options) => tag.Value + OptionalSuffix(options, SHORT_SUFFIX);
예제 #12
0
 private static string GetName(NbtTag tag, SnbtOptions options)
 {
     return(QuoteIfRequested(tag.Name, options.ShouldQuoteKeys, options.KeyQuoteMode, options.EscapeNewlines));
 }
예제 #13
0
        // shared technique for single-line arrays
        // (list, int array, byte array)
        private static string ListToString <T>(string list_prefix, Func <T, string> function, IEnumerable <T> values, SnbtOptions options)
        {
            if (!options.ArrayPrefixes)
            {
                list_prefix = String.Empty;
            }
            // spacing between values
            string spacing = options.Minified ? String.Empty : VALUE_SPACING;
            // spacing between list prefix and first value
            string prefix_separator = !options.Minified && list_prefix.Length > 0 && values.Any() ? VALUE_SPACING : String.Empty;
            var    s        = new StringBuilder(LIST_OPEN + list_prefix + prefix_separator);
            string contents = String.Join(VALUE_SEPARATOR + spacing, values.Select(x => function(x)));

            s.Append(contents);
            s.Append(LIST_CLOSE);
            return(s.ToString());
        }
예제 #14
0
 public static string ToSnbt(this NbtByte tag, SnbtOptions options) => (sbyte)tag.Value + OptionalSuffix(options, BYTE_SUFFIX);
예제 #15
0
 public static string ToSnbt(this NbtInt tag, SnbtOptions options) => tag.Value.ToString();
예제 #16
0
 public static string ToSnbt(this NbtByteArray tag, SnbtOptions options)
 {
     return(ListToString("" + BYTE_ARRAY_PREFIX + ARRAY_DELIMITER, x => ((sbyte)x).ToString() + (options.NumberSuffixes ? BYTE_SUFFIX.ToString() : String.Empty), tag.Value, options));
 }
예제 #17
0
 public static string ToSnbt(this NbtString tag, SnbtOptions options)
 {
     return(QuoteIfRequested(tag.Value, options.ShouldQuoteStrings, options.StringQuoteMode, options.EscapeNewlines));
 }
예제 #18
0
 public static string ToSnbt(this NbtLong tag, SnbtOptions options) => tag.Value + OptionalSuffix(options, LONG_SUFFIX);
예제 #19
0
 public static string ToSnbt(this NbtLongArray tag, SnbtOptions options)
 {
     return(ListToString("" + LONG_ARRAY_PREFIX + ARRAY_DELIMITER, x => x.ToString() + (options.NumberSuffixes ? LONG_SUFFIX.ToString() : String.Empty), tag.Value, options));
 }
예제 #20
0
 private static string OptionalSuffix(SnbtOptions options, char suffix)
 {
     return(options.NumberSuffixes ? suffix.ToString() : String.Empty);
 }