예제 #1
0
    public static long getLong(this NbtCompound tag, string name, long defaultValue = 0)
    {
        NbtLong tag1 = tag.Get <NbtLong>(name);

        if (tag1 == null)
        {
            return(defaultValue);
        }
        else
        {
            return(tag1.Value);
        }
    }
예제 #2
0
        public void NbtLongTest()
        {
            object dummy;
            NbtTag test = new NbtLong(9223372036854775807);

            Assert.Throws <InvalidCastException>(() => dummy = test.ByteArrayValue);
            Assert.Throws <InvalidCastException>(() => dummy = test.ByteValue);
            Assert.AreEqual((double)9223372036854775807, test.DoubleValue);
            Assert.AreEqual((float)9223372036854775807, test.FloatValue);
            Assert.Throws <InvalidCastException>(() => dummy = test.IntArrayValue);
            Assert.Throws <InvalidCastException>(() => dummy = test.IntValue);
            Assert.AreEqual(9223372036854775807, test.LongValue);
            Assert.Throws <InvalidCastException>(() => dummy = test.ShortValue);
            Assert.AreEqual("9223372036854775807", test.StringValue);
        }
예제 #3
0
        public void SetPropertyValue <T>(NbtTag tag, Expression <Func <T> > property, bool upperFirst = true)
        {
            var propertyInfo = ((MemberExpression)property.Body).Member as PropertyInfo;

            if (propertyInfo == null)
            {
                throw new ArgumentException("The lambda expression 'property' should point to a valid Property");
            }

            NbtTag nbtTag = tag[propertyInfo.Name];

            if (nbtTag == null)
            {
                nbtTag = tag[LowercaseFirst(propertyInfo.Name)];
            }

            if (nbtTag == null)
            {
                if (propertyInfo.PropertyType == typeof(bool))
                {
                    nbtTag = new NbtByte(propertyInfo.Name);
                }
                else if (propertyInfo.PropertyType == typeof(byte))
                {
                    nbtTag = new NbtByte(LowercaseFirst(propertyInfo.Name));
                }
                else if (propertyInfo.PropertyType == typeof(short))
                {
                    nbtTag = new NbtShort(LowercaseFirst(propertyInfo.Name));
                }
                else if (propertyInfo.PropertyType == typeof(int))
                {
                    nbtTag = new NbtInt(LowercaseFirst(propertyInfo.Name));
                }
                else if (propertyInfo.PropertyType == typeof(long))
                {
                    nbtTag = new NbtLong(LowercaseFirst(propertyInfo.Name));
                }
                else if (propertyInfo.PropertyType == typeof(float))
                {
                    nbtTag = new NbtFloat(LowercaseFirst(propertyInfo.Name));
                }
                else if (propertyInfo.PropertyType == typeof(double))
                {
                    nbtTag = new NbtDouble(LowercaseFirst(propertyInfo.Name));
                }
                else if (propertyInfo.PropertyType == typeof(string))
                {
                    nbtTag = new NbtString(LowercaseFirst(propertyInfo.Name), "");
                }
                else
                {
                    return;
                }
            }

            var mex    = property.Body as MemberExpression;
            var target = Expression.Lambda(mex.Expression).Compile().DynamicInvoke();

            switch (nbtTag.TagType)
            {
            case NbtTagType.Unknown:
                break;

            case NbtTagType.End:
                break;

            case NbtTagType.Byte:
                if (propertyInfo.PropertyType == typeof(bool))
                {
                    tag[nbtTag.Name] = new NbtByte(nbtTag.Name, (byte)((bool)propertyInfo.GetValue(target) ? 1 : 0));
                }
                else
                {
                    tag[nbtTag.Name] = new NbtByte(nbtTag.Name, (byte)propertyInfo.GetValue(target));
                }
                break;

            case NbtTagType.Short:
                tag[nbtTag.Name] = new NbtShort(nbtTag.Name, (short)propertyInfo.GetValue(target));
                break;

            case NbtTagType.Int:
                if (propertyInfo.PropertyType == typeof(bool))
                {
                    tag[nbtTag.Name] = new NbtInt(nbtTag.Name, (bool)propertyInfo.GetValue(target) ? 1 : 0);
                }
                else
                {
                    tag[nbtTag.Name] = new NbtInt(nbtTag.Name, (int)propertyInfo.GetValue(target));
                }
                break;

            case NbtTagType.Long:
                tag[nbtTag.Name] = new NbtLong(nbtTag.Name, (long)propertyInfo.GetValue(target));
                break;

            case NbtTagType.Float:
                tag[nbtTag.Name] = new NbtFloat(nbtTag.Name, (float)propertyInfo.GetValue(target));
                break;

            case NbtTagType.Double:
                tag[nbtTag.Name] = new NbtDouble(nbtTag.Name, (double)propertyInfo.GetValue(target));
                break;

            case NbtTagType.ByteArray:
                tag[nbtTag.Name] = new NbtByteArray(nbtTag.Name, (byte[])propertyInfo.GetValue(target));
                break;

            case NbtTagType.String:
                tag[nbtTag.Name] = new NbtString(nbtTag.Name, (string)propertyInfo.GetValue(target) ?? "");
                break;

            case NbtTagType.List:
                break;

            case NbtTagType.Compound:
                break;

            case NbtTagType.IntArray:
                tag[nbtTag.Name] = new NbtIntArray(nbtTag.Name, (int[])propertyInfo.GetValue(target));
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            //return (T) propertyInfo.GetValue(target);
        }
예제 #4
0
        public void CopyConstructorTest()
        {
            NbtByte byteTag      = new NbtByte("byteTag", 1);
            NbtByte byteTagClone = (NbtByte)byteTag.Clone();

            Assert.AreNotSame(byteTag, byteTagClone);
            Assert.AreEqual(byteTag.Name, byteTagClone.Name);
            Assert.AreEqual(byteTag.Value, byteTagClone.Value);
            Assert.Throws <ArgumentNullException>(() => new NbtByte((NbtByte)null));

            NbtByteArray byteArrTag      = new NbtByteArray("byteArrTag", new byte[] { 1, 2, 3, 4 });
            NbtByteArray byteArrTagClone = (NbtByteArray)byteArrTag.Clone();

            Assert.AreNotSame(byteArrTag, byteArrTagClone);
            Assert.AreEqual(byteArrTag.Name, byteArrTagClone.Name);
            Assert.AreNotSame(byteArrTag.Value, byteArrTagClone.Value);
            CollectionAssert.AreEqual(byteArrTag.Value, byteArrTagClone.Value);
            Assert.Throws <ArgumentNullException>(() => new NbtByteArray((NbtByteArray)null));

            NbtCompound compTag      = new NbtCompound("compTag", new NbtTag[] { new NbtByte("innerTag", 1) });
            NbtCompound compTagClone = (NbtCompound)compTag.Clone();

            Assert.AreNotSame(compTag, compTagClone);
            Assert.AreEqual(compTag.Name, compTagClone.Name);
            Assert.AreNotSame(compTag["innerTag"], compTagClone["innerTag"]);
            Assert.AreEqual(compTag["innerTag"].Name, compTagClone["innerTag"].Name);
            Assert.AreEqual(compTag["innerTag"].ByteValue, compTagClone["innerTag"].ByteValue);
            Assert.Throws <ArgumentNullException>(() => new NbtCompound((NbtCompound)null));

            NbtDouble doubleTag      = new NbtDouble("doubleTag", 1);
            NbtDouble doubleTagClone = (NbtDouble)doubleTag.Clone();

            Assert.AreNotSame(doubleTag, doubleTagClone);
            Assert.AreEqual(doubleTag.Name, doubleTagClone.Name);
            Assert.AreEqual(doubleTag.Value, doubleTagClone.Value);
            Assert.Throws <ArgumentNullException>(() => new NbtDouble((NbtDouble)null));

            NbtFloat floatTag      = new NbtFloat("floatTag", 1);
            NbtFloat floatTagClone = (NbtFloat)floatTag.Clone();

            Assert.AreNotSame(floatTag, floatTagClone);
            Assert.AreEqual(floatTag.Name, floatTagClone.Name);
            Assert.AreEqual(floatTag.Value, floatTagClone.Value);
            Assert.Throws <ArgumentNullException>(() => new NbtFloat((NbtFloat)null));

            NbtInt intTag      = new NbtInt("intTag", 1);
            NbtInt intTagClone = (NbtInt)intTag.Clone();

            Assert.AreNotSame(intTag, intTagClone);
            Assert.AreEqual(intTag.Name, intTagClone.Name);
            Assert.AreEqual(intTag.Value, intTagClone.Value);
            Assert.Throws <ArgumentNullException>(() => new NbtInt((NbtInt)null));

            NbtIntArray intArrTag      = new NbtIntArray("intArrTag", new[] { 1, 2, 3, 4 });
            NbtIntArray intArrTagClone = (NbtIntArray)intArrTag.Clone();

            Assert.AreNotSame(intArrTag, intArrTagClone);
            Assert.AreEqual(intArrTag.Name, intArrTagClone.Name);
            Assert.AreNotSame(intArrTag.Value, intArrTagClone.Value);
            CollectionAssert.AreEqual(intArrTag.Value, intArrTagClone.Value);
            Assert.Throws <ArgumentNullException>(() => new NbtIntArray((NbtIntArray)null));

            NbtList listTag      = new NbtList("listTag", new NbtTag[] { new NbtByte(1) });
            NbtList listTagClone = (NbtList)listTag.Clone();

            Assert.AreNotSame(listTag, listTagClone);
            Assert.AreEqual(listTag.Name, listTagClone.Name);
            Assert.AreNotSame(listTag[0], listTagClone[0]);
            Assert.AreEqual(listTag[0].ByteValue, listTagClone[0].ByteValue);
            Assert.Throws <ArgumentNullException>(() => new NbtList((NbtList)null));

            NbtLong longTag      = new NbtLong("longTag", 1);
            NbtLong longTagClone = (NbtLong)longTag.Clone();

            Assert.AreNotSame(longTag, longTagClone);
            Assert.AreEqual(longTag.Name, longTagClone.Name);
            Assert.AreEqual(longTag.Value, longTagClone.Value);
            Assert.Throws <ArgumentNullException>(() => new NbtLong((NbtLong)null));

            NbtShort shortTag      = new NbtShort("shortTag", 1);
            NbtShort shortTagClone = (NbtShort)shortTag.Clone();

            Assert.AreNotSame(shortTag, shortTagClone);
            Assert.AreEqual(shortTag.Name, shortTagClone.Name);
            Assert.AreEqual(shortTag.Value, shortTagClone.Value);
            Assert.Throws <ArgumentNullException>(() => new NbtShort((NbtShort)null));

            NbtString stringTag      = new NbtString("stringTag", "foo");
            NbtString stringTagClone = (NbtString)stringTag.Clone();

            Assert.AreNotSame(stringTag, stringTagClone);
            Assert.AreEqual(stringTag.Name, stringTagClone.Name);
            Assert.AreEqual(stringTag.Value, stringTagClone.Value);
            Assert.Throws <ArgumentNullException>(() => new NbtString((NbtString)null));
        }
예제 #5
0
        public static NbtTag TreeToNBT(TreeNodeCollection tree)
        {
            NbtTag NBTTag = null;

            if (tree != null)
            {
                foreach (TreeNode node in tree)
                {
                    string value = node.Text.Replace($"{node.Name}: ", string.Empty).Replace(".", ",");//TODO , or . by language
                    try
                    {
                        switch (node.ImageKey)
                        {
                        case "buttonstring.png":
                        {
                            NBTTag = new NbtString(node.Name, value);
                            break;
                        }

                        case "buttonint.png":
                        {
                            NBTTag = new NbtInt(node.Name, int.Parse(value));
                            break;
                        }

                        case "buttonbyte.png":
                        {
                            NBTTag = new NbtByte(node.Name, byte.Parse(value));
                            break;
                        }

                        case "buttonlong.png":
                        {
                            NBTTag = new NbtLong(node.Name, long.Parse(value));
                            break;
                        }

                        case "buttonshort.png":
                        {
                            NBTTag = new NbtShort(node.Name, short.Parse(value));
                            break;
                        }

                        case "buttonfloat.png":
                        {
                            NBTTag = new NbtFloat(node.Name, float.Parse(value));
                            break;
                        }

                        case "buttondouble.png":
                        {
                            NBTTag = new NbtDouble(node.Name, double.Parse(value));
                            break;
                        }

                        case "buttoncompound.png":
                        {
                            NBTTag = new NbtCompound(node.Name);
                            foreach (object c in node.Nodes)
                            {
                                (new AlertForm(c.ToString())).ShowDialog();
                                if (c is TreeNode && ((TreeNode)c).Nodes.Count > 0)
                                {
                                    ((NbtCompound)NBTTag).Add(TreeToNBT(((TreeNode)c).Nodes));
                                }
                                else
                                {
                                    (new AlertForm(c.GetType().ToString())).ShowDialog();
                                }
                            }
                            break;
                        }

                        case "buttonlist.png":
                        {
                            NBTTag = new NbtList(node.Name);
                            foreach (object c in node.Nodes)
                            {
                                (new AlertForm(c.ToString())).ShowDialog();
                                if (c is TreeNode && ((TreeNode)c).Nodes.Count > 0)
                                {
                                    ((NbtList)NBTTag).Add(TreeToNBT(((TreeNode)c).Nodes));
                                }
                                else
                                {
                                    (new AlertForm(c.GetType().ToString())).ShowDialog();
                                }
                            }
                            break;
                        }

                        case "buttonbytearray.png":
                        {
                            NBTTag = new NbtByteArray(node.Name, NodesToByteArray(node.Nodes));
                            (new AlertForm(NBTTag.ToString())).ShowDialog();
                            break;
                        }

                        case "buttonintarray.png":
                        {
                            NBTTag = new NbtIntArray(node.Name, NodesToIntArray(node.Nodes));
                            (new AlertForm(NBTTag.ToString())).ShowDialog();
                            break;
                        }

                        default:
                        {
                            throw new WarningException($"Warning: unhandeled node {node.ImageKey} can not be edited");
                        }
                        }
                    }
                    catch (Exception exception)
                    {
                        if (exception is InvalidOperationException || exception is WarningException)
                        {
                            new AlertForm(exception.ToString())
                            {
                                Text = "Warning!",
                                Icon = System.Drawing.SystemIcons.Warning
                            }.ShowDialog();
                        }
                        else
                        {
                            new AlertForm(exception.ToString())
                            {
                                Text = exception.GetType().ToString(),
                                Icon = System.Drawing.SystemIcons.Error
                            }.ShowDialog();
                        }
                    }
                }
            }
            return(NBTTag);
        }
예제 #6
0
 public void NbtLongTest()
 {
     object dummy;
     NbtTag test = new NbtLong( 9223372036854775807 );
     Assert.Throws<InvalidCastException>( () => dummy = test.ByteArrayValue );
     Assert.Throws<InvalidCastException>( () => dummy = test.ByteValue );
     Assert.AreEqual( (double)9223372036854775807, test.DoubleValue );
     Assert.AreEqual( (float)9223372036854775807, test.FloatValue );
     Assert.Throws<InvalidCastException>( () => dummy = test.IntArrayValue );
     Assert.Throws<InvalidCastException>( () => dummy = test.IntValue );
     Assert.AreEqual( 9223372036854775807, test.LongValue );
     Assert.Throws<InvalidCastException>( () => dummy = test.ShortValue );
     Assert.AreEqual( "9223372036854775807", test.StringValue );
 }
예제 #7
0
        /**
         * BEFORE any digits, requiring backtracking.
         */
        private NbtTag ParseNumber()
        {
            bool       more    = true;
            NbtTagType type    = NbtTagType.Int;
            String     literal = "";

            while (more)
            {
                char next;
                try
                {
                    next = NextChar();
                }
                catch (Exception)
                {
                    next = ' '; //just end the number
                }
                if (next <= '9' && next >= '0' || next == '-')
                {
                    literal += next;
                }
                else if (next == '.')
                {
                    literal += next;
                    type     = NbtTagType.Double;
                }
                else
                {
                    more = false;
                    switch (next)
                    {
                    case 'l':
                    case 'L':
                        type = NbtTagType.Long;
                        break;

                    case 'b':
                    case 'B':
                        type = NbtTagType.Byte;
                        break;

                    case 'f':
                    case 'F':
                        type = NbtTagType.Float;
                        break;

                    default:
                        //End of number, put it back.
                        Position--;
                        break;
                    }
                }
            }
            NbtTag ret;

            switch (type)
            {
            case NbtTagType.Byte:
                ret = new NbtByte(byte.Parse(literal));
                break;

            case NbtTagType.Int:
                ret = new NbtInt(int.Parse(literal));
                break;

            case NbtTagType.Long:
                ret = new NbtLong(long.Parse(literal));
                break;

            case NbtTagType.Float:
                ret = new NbtFloat(float.Parse(literal));
                break;

            case NbtTagType.Double:
                ret = new NbtDouble(double.Parse(literal));
                break;

            default:
                ret = null;
                break;
            }
            return(ret);
        }
예제 #8
0
파일: MiscTests.cs 프로젝트: fragmer/fNbt
        public void CopyConstructorTest()
        {
            NbtByte byteTag = new NbtByte("byteTag", 1);
            NbtByte byteTagClone = (NbtByte)byteTag.Clone();
            Assert.AreNotSame(byteTag, byteTagClone);
            Assert.AreEqual(byteTag.Name, byteTagClone.Name);
            Assert.AreEqual(byteTag.Value, byteTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtByte((NbtByte)null));

            NbtByteArray byteArrTag = new NbtByteArray("byteArrTag", new byte[] { 1, 2, 3, 4 });
            NbtByteArray byteArrTagClone = (NbtByteArray)byteArrTag.Clone();
            Assert.AreNotSame(byteArrTag, byteArrTagClone);
            Assert.AreEqual(byteArrTag.Name, byteArrTagClone.Name);
            Assert.AreNotSame(byteArrTag.Value, byteArrTagClone.Value);
            CollectionAssert.AreEqual(byteArrTag.Value, byteArrTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtByteArray((NbtByteArray)null));

            NbtCompound compTag = new NbtCompound("compTag", new NbtTag[] { new NbtByte("innerTag", 1) });
            NbtCompound compTagClone = (NbtCompound)compTag.Clone();
            Assert.AreNotSame(compTag, compTagClone);
            Assert.AreEqual(compTag.Name, compTagClone.Name);
            Assert.AreNotSame(compTag["innerTag"], compTagClone["innerTag"]);
            Assert.AreEqual(compTag["innerTag"].Name, compTagClone["innerTag"].Name);
            Assert.AreEqual(compTag["innerTag"].ByteValue, compTagClone["innerTag"].ByteValue);
            Assert.Throws<ArgumentNullException>(() => new NbtCompound((NbtCompound)null));

            NbtDouble doubleTag = new NbtDouble("doubleTag", 1);
            NbtDouble doubleTagClone = (NbtDouble)doubleTag.Clone();
            Assert.AreNotSame(doubleTag, doubleTagClone);
            Assert.AreEqual(doubleTag.Name, doubleTagClone.Name);
            Assert.AreEqual(doubleTag.Value, doubleTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtDouble((NbtDouble)null));

            NbtFloat floatTag = new NbtFloat("floatTag", 1);
            NbtFloat floatTagClone = (NbtFloat)floatTag.Clone();
            Assert.AreNotSame(floatTag, floatTagClone);
            Assert.AreEqual(floatTag.Name, floatTagClone.Name);
            Assert.AreEqual(floatTag.Value, floatTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtFloat((NbtFloat)null));

            NbtInt intTag = new NbtInt("intTag", 1);
            NbtInt intTagClone = (NbtInt)intTag.Clone();
            Assert.AreNotSame(intTag, intTagClone);
            Assert.AreEqual(intTag.Name, intTagClone.Name);
            Assert.AreEqual(intTag.Value, intTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtInt((NbtInt)null));

            NbtIntArray intArrTag = new NbtIntArray("intArrTag", new[] { 1, 2, 3, 4 });
            NbtIntArray intArrTagClone = (NbtIntArray)intArrTag.Clone();
            Assert.AreNotSame(intArrTag, intArrTagClone);
            Assert.AreEqual(intArrTag.Name, intArrTagClone.Name);
            Assert.AreNotSame(intArrTag.Value, intArrTagClone.Value);
            CollectionAssert.AreEqual(intArrTag.Value, intArrTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtIntArray((NbtIntArray)null));

            NbtList listTag = new NbtList("listTag", new NbtTag[] { new NbtByte(1) });
            NbtList listTagClone = (NbtList)listTag.Clone();
            Assert.AreNotSame(listTag, listTagClone);
            Assert.AreEqual(listTag.Name, listTagClone.Name);
            Assert.AreNotSame(listTag[0], listTagClone[0]);
            Assert.AreEqual(listTag[0].ByteValue, listTagClone[0].ByteValue);
            Assert.Throws<ArgumentNullException>(() => new NbtList((NbtList)null));

            NbtLong longTag = new NbtLong("longTag", 1);
            NbtLong longTagClone = (NbtLong)longTag.Clone();
            Assert.AreNotSame(longTag, longTagClone);
            Assert.AreEqual(longTag.Name, longTagClone.Name);
            Assert.AreEqual(longTag.Value, longTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtLong((NbtLong)null));

            NbtShort shortTag = new NbtShort("shortTag", 1);
            NbtShort shortTagClone = (NbtShort)shortTag.Clone();
            Assert.AreNotSame(shortTag, shortTagClone);
            Assert.AreEqual(shortTag.Name, shortTagClone.Name);
            Assert.AreEqual(shortTag.Value, shortTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtShort((NbtShort)null));

            NbtString stringTag = new NbtString("stringTag", "foo");
            NbtString stringTagClone = (NbtString)stringTag.Clone();
            Assert.AreNotSame(stringTag, stringTagClone);
            Assert.AreEqual(stringTag.Name, stringTagClone.Name);
            Assert.AreEqual(stringTag.Value, stringTagClone.Value);
            Assert.Throws<ArgumentNullException>(() => new NbtString((NbtString)null));
        }
예제 #9
0
        public static NbtTag Serialize <T>(this T obj, NbtTag tag = null) where T : new()
        {
            tag ??= new NbtCompound(string.Empty);

            if (obj == null)
            {
                throw new NullReferenceException();
            }

            PropertyInfo[] properties = obj.GetType().GetProperties();
            foreach (PropertyInfo propertyInfo in properties)
            {
                var    attribute    = propertyInfo.GetCustomAttribute(typeof(JsonPropertyNameAttribute)) as JsonPropertyNameAttribute;
                string propertyName = attribute?.Name ?? propertyInfo.Name;
                NbtTag nbtTag       = tag[propertyName] ?? tag[LowercaseFirst(propertyName)];

                if (nbtTag == null)
                {
                    if (propertyInfo.PropertyType == typeof(bool))
                    {
                        nbtTag = new NbtByte(propertyName);
                    }
                    else if (propertyInfo.PropertyType == typeof(byte))
                    {
                        nbtTag = new NbtByte(propertyName);
                    }
                    else if (propertyInfo.PropertyType == typeof(short))
                    {
                        nbtTag = new NbtShort(propertyName);
                    }
                    else if (propertyInfo.PropertyType == typeof(int))
                    {
                        nbtTag = new NbtInt(propertyName);
                    }
                    else if (propertyInfo.PropertyType == typeof(long))
                    {
                        nbtTag = new NbtLong(propertyName);
                    }
                    else if (propertyInfo.PropertyType == typeof(float))
                    {
                        nbtTag = new NbtFloat(propertyName);
                    }
                    else if (propertyInfo.PropertyType == typeof(double))
                    {
                        nbtTag = new NbtDouble(propertyName);
                    }
                    else if (propertyInfo.PropertyType == typeof(string))
                    {
                        nbtTag = new NbtString(propertyName, "");
                    }
                    else
                    {
                        continue;
                    }
                }

                //var mex = property.Body as MemberExpression;
                //var target = Expression.Lambda(mex.Expression).Compile().DynamicInvoke();

                switch (nbtTag.TagType)
                {
                case NbtTagType.Unknown:
                    break;

                case NbtTagType.End:
                    break;

                case NbtTagType.Byte:
                    if (propertyInfo.PropertyType == typeof(bool))
                    {
                        tag[nbtTag.Name] = new NbtByte(nbtTag.Name, (byte)((bool)propertyInfo.GetValue(obj) ? 1 : 0));
                    }
                    else
                    {
                        tag[nbtTag.Name] = new NbtByte(nbtTag.Name, (byte)propertyInfo.GetValue(obj));
                    }
                    break;

                case NbtTagType.Short:
                    tag[nbtTag.Name] = new NbtShort(nbtTag.Name, (short)propertyInfo.GetValue(obj));
                    break;

                case NbtTagType.Int:
                    if (propertyInfo.PropertyType == typeof(bool))
                    {
                        tag[nbtTag.Name] = new NbtInt(nbtTag.Name, (bool)propertyInfo.GetValue(obj) ? 1 : 0);
                    }
                    else
                    {
                        tag[nbtTag.Name] = new NbtInt(nbtTag.Name, (int)propertyInfo.GetValue(obj));
                    }
                    break;

                case NbtTagType.Long:
                    tag[nbtTag.Name] = new NbtLong(nbtTag.Name, (long)propertyInfo.GetValue(obj));
                    break;

                case NbtTagType.Float:
                    tag[nbtTag.Name] = new NbtFloat(nbtTag.Name, (float)propertyInfo.GetValue(obj));
                    break;

                case NbtTagType.Double:
                    tag[nbtTag.Name] = new NbtDouble(nbtTag.Name, (double)propertyInfo.GetValue(obj));
                    break;

                case NbtTagType.ByteArray:
                    tag[nbtTag.Name] = new NbtByteArray(nbtTag.Name, (byte[])propertyInfo.GetValue(obj));
                    break;

                case NbtTagType.String:
                    tag[nbtTag.Name] = new NbtString(nbtTag.Name, (string)propertyInfo.GetValue(obj) ?? "");
                    break;

                case NbtTagType.List:
                    break;

                case NbtTagType.Compound:
                    break;

                case NbtTagType.IntArray:
                    tag[nbtTag.Name] = new NbtIntArray(nbtTag.Name, (int[])propertyInfo.GetValue(obj));
                    break;
                }
            }

            return(tag);
        }
예제 #10
0
		public void Write_LongTag()
		{
			// Arrange
			MemoryStream stream = new MemoryStream();
			NbtWriter writer = new NbtWriter(stream);
			NbtLong tag = new NbtLong("asdf", 81985529216486895);
			byte[] expected = new byte[] { 0x04, 0x00, 0x04, 0x61, 0x73, 0x64, 0x66, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };

			// Act
			writer.Write(tag);
			byte[] result = stream.ToArray();

			// Assert
			CollectionAssert.AreEqual(expected, result);
		}
예제 #11
0
		internal void Write(NbtLong tag, bool writeHeader)
		{
			Write(tag.Name, tag.Value, writeHeader);
		}
예제 #12
0
		/// <summary>
		/// Writes out the specified tag.
		/// </summary>
		/// <param name="tag">The tag.</param>
		/// <exception cref="System.ArgumentNullException"><paramref name="tag"/> is <c>null</c>.</exception>
		/// <exception cref="System.ObjectDisposedException">The stream is closed.</exception>
		/// <exception cref="System.IO.IOException">An I/O error occured.</exception>
		public void Write(NbtLong tag)
		{
			if (tag == null)
				throw new ArgumentNullException("tag", "tag is null.");

			Write(tag, true);
		}
예제 #13
0
 public static string ToSnbt(this NbtLong tag, SnbtOptions options) => tag.Value + OptionalSuffix(options, LONG_SUFFIX);
예제 #14
0
        public Map Load(string path)
        {
            NbtFile     file = new NbtFile(path);
            NbtCompound root = file.RootTag;

            int formatVersion = root["FormatVersion"].ByteValue;

            if (formatVersion != 1)
            {
                throw new MapFormatException("Unsupported format version: " + formatVersion);
            }

            // Read dimensions and create the map
            Map map = new Map(null,
                              root["X"].ShortValue,
                              root["Z"].ShortValue,
                              root["Y"].ShortValue,
                              false);

            // read spawn coordinates
            NbtCompound spawn = (NbtCompound)root["Spawn"];

            map.Spawn = new Position(spawn["X"].ShortValue,
                                     spawn["Z"].ShortValue,
                                     spawn["Y"].ShortValue,
                                     spawn["H"].ByteValue,
                                     spawn["P"].ByteValue);

            // read UUID
            map.Guid = new Guid(root["UUID"].ByteArrayValue);

            // read creation/modification dates of the file (for fallback)
            DateTime fileCreationDate = File.GetCreationTime(path);
            DateTime fileModTime      = File.GetCreationTime(path);

            // try to read embedded creation date
            NbtLong creationDate = root.Get <NbtLong>("TimeCreated");

            if (creationDate != null)
            {
                map.DateCreated = DateTimeUtil.ToDateTime(creationDate.Value);
            }
            else
            {
                // for fallback, pick the older of two filesystem dates
                map.DateCreated = (fileModTime > fileCreationDate) ? fileCreationDate : fileModTime;
            }

            // try to read embedded modification date
            NbtLong modTime = root.Get <NbtLong>("LastModified");

            if (modTime != null)
            {
                map.DateModified = DateTimeUtil.ToDateTime(modTime.Value);
            }
            else
            {
                // for fallback, use file modification date
                map.DateModified = fileModTime;
            }

            // TODO: LastAccessed

            // TODO: read CreatedBy and MapGenerator

            // read blocks
            map.Blocks = root["BlockArray"].ByteArrayValue;

            // TODO: CPE CustomBlock conversion

            // read metadata, if present
            NbtCompound metadata = root.Get <NbtCompound>("Metadata");

            if (metadata == null)
            {
                return(map);
            }

            NbtCompound fCraftMetadata = metadata.Get <NbtCompound>("fCraft");

            if (fCraftMetadata != null)
            {
                foreach (NbtCompound groupTag in fCraftMetadata)
                {
                    string groupName = groupTag.Name;
                    foreach (NbtString keyValueTag in groupTag)
                    {
                        // ReSharper disable AssignNullToNotNullAttribute // names are never null within compound
                        map.Metadata.Add(groupName, keyValueTag.Name, keyValueTag.Value);
                        // ReSharper restore AssignNullToNotNullAttribute
                    }
                }
            }

            // read CPE settings
            NbtCompound cpeMetadata = metadata.Get <NbtCompound>("CPE");

            if (cpeMetadata != null)
            {
                // TODO: CPE metadata
            }

            // preserve foreign metadata, if needed
            if (MapUtility.PreserveForeignMetadata)
            {
                metadata.Remove("fCraft");
                metadata.Remove("CPE");
                map.ForeignMetadata = metadata;
            }

            return(map);
        }