Example #1
0
        internal static ITag ParseValue(BinaryReader reader, byte id)
        {
            switch (id)
            {
            case TagBoolean.TypeId: return(TagBoolean.Parse(reader));

            case TagByte.TypeId: return(TagByte.Parse(reader));

            case TagShort.TypeId: return(TagShort.Parse(reader));

            case TagInt.TypeId: return(TagInt.Parse(reader));

            case TagFloat.TypeId: return(TagFloat.Parse(reader));

            case TagLong.TypeId: return(TagLong.Parse(reader));

            case TagDouble.TypeId: return(TagDouble.Parse(reader));

            case TagString.TypeId: return(TagString.Parse(reader));

            case TagCompound.TypeId: return(TagCompound.Parse(reader));

            case TagList.TypeId: return(TagList.Parse(reader));

            default: throw new FormatException("Invalid type ID: " + id + ". At file position " + reader.BaseStream.Position);
            }
        }
Example #2
0
        public override void WritePayload(Stream stream, INamedBinaryTag iTag)
        {
            TagShort tag = iTag as TagShort;

            byte[] data = BitConverter.GetBytes(tag.Value);
            data = data.ReverseIfLittleEndian();
            stream.Write(data, 0, 2);
        }
Example #3
0
    public void ConstructorWithValueTest()
    {
      // arrange
      TagShort tag;
      short value;

      value = short.MaxValue;

      // act
      tag = new TagShort(value);

      // assert
      Assert.IsEmpty(tag.Name);
      Assert.AreEqual(value, tag.Value);
    }
Example #4
0
    public void ConstructorTest()
    {
      // arrange
      TagShort tag;
      short expected;

      expected = 0;

      // act
      tag = new TagShort();

      // assert
      Assert.IsEmpty(tag.Name);
      Assert.AreEqual(expected, tag.Value);
    }
Example #5
0
        public void Equals_returns_false_with_different_value()
        {
            // arrange
            TagShort target;
            TagShort other;
            bool     actual;

            target = new TagShort(string.Empty, (short)(short.MaxValue >> 1));
            other  = new TagShort(string.Empty, 8191);

            // act
            actual = target.Equals(other);

            // assert
            Assert.IsFalse(actual);
        }
Example #6
0
        public void Constructor_sets_name()
        {
            // arrange
            TagShort target;
            string   expected;
            string   actual;

            expected = "Alphatag";

            // act
            target = new TagShort(expected);

            // assert
            actual = target.Name;
            Assert.AreEqual(expected, actual);
        }
Example #7
0
        public void Equals_returns_false_with_different_name()
        {
            // arrange
            TagShort target;
            TagShort other;
            bool     actual;

            target = new TagShort("Alpha", (short)(short.MaxValue >> 1));
            other  = new TagShort("Beta", (short)(short.MaxValue >> 1));

            // act
            actual = target.Equals(other);

            // assert
            Assert.IsFalse(actual);
        }
Example #8
0
        public void Equals_returns_true_for_matching_tag()
        {
            // arrange
            TagShort target;
            TagShort other;
            bool     actual;

            target = new TagShort("alpha", (short)(short.MaxValue >> 1));
            other  = new TagShort("alpha", (short)(short.MaxValue >> 1));

            // act
            actual = target.Equals(other);

            // assert
            Assert.IsTrue(actual);
        }
        public void Constructor_sets_value()
        {
            // arrange
            TagShort target;
            short    expected;
            short    actual;

            expected = (short)(short.MaxValue >> 1);

            // act
            target = new TagShort(string.Empty, expected);

            // assert
            actual = target.Value;
            Assert.Equal(expected, actual);
        }
Example #10
0
        public void Constructor_sets_default_value()
        {
            // arrange
            TagShort target;
            short    expected;
            short    actual;

            expected = 0;

            // act
            target = new TagShort();

            // assert
            actual = target.Value;
            Assert.AreEqual(expected, actual);
        }
Example #11
0
        public void Constructor_sets_value_without_name()
        {
            // arrange
            TagShort target;
            short    expected;
            short    actual;

            expected = (short)(short.MaxValue >> 1);

            // act
            target = new TagShort(expected);

            // assert
            actual = target.Value;
            Assert.AreEqual(expected, actual);
        }
Example #12
0
        public void Constructor_sets_default_name()
        {
            // arrange
            TagShort target;
            string   expected;
            string   actual;

            expected = string.Empty;

            // act
            target = new TagShort();

            // assert
            actual = target.Name;
            Assert.AreEqual(expected, actual);
        }
Example #13
0
        public void GetHashCode_returns_different_values_with_different_value()
        {
            // arrange
            TagShort target;
            int      actual;
            int      notExpected;

            target = new TagShort(string.Empty, (short)(short.MaxValue >> 1));

            notExpected = new TagShort(string.Empty, 8191).GetHashCode();

            // act
            actual = target.GetHashCode();

            // assert
            Assert.AreNotEqual(notExpected, actual);
        }
Example #14
0
    public void ConstructorWithNameTest()
    {
      // arrange
      TagShort tag;
      string name;
      short expected;

      name = "creationDate";
      expected = 0;

      // act
      tag = new TagShort(name);

      // assert
      Assert.AreEqual(name, tag.Name);
      Assert.AreEqual(expected, tag.Value);
    }
Example #15
0
        public void Type_returns_correct_value()
        {
            // arrange
            TagShort target;
            TagType  expected;
            TagType  actual;

            target = new TagShort();

            expected = TagType.Short;

            // act
            actual = target.Type;

            // assert
            Assert.AreEqual(expected, actual);
        }
        public void GetHashCode_returns_different_values_with_different_name()
        {
            // arrange
            TagShort target;
            int      actual;
            int      notExpected;

            target = new TagShort("Alpha", (short)(short.MaxValue >> 1));

            notExpected = new TagShort("Beta", (short)(short.MaxValue >> 1)).GetHashCode();

            // act
            actual = target.GetHashCode();

            // assert
            Assert.NotEqual(notExpected, actual);
        }
Example #17
0
        public void ToString_returns_string_version_of_tag()
        {
            // arrange
            TagShort target;
            string   expected;
            string   actual;

            expected = "[Short: gamma=16383]";

            target = new TagShort("gamma", (short)(short.MaxValue >> 1));

            // act
            actual = target.ToString();

            // assert
            Assert.AreEqual(expected, actual);
        }
Example #18
0
        public void ToValueString_returns_string_version_of_value()
        {
            // arrange
            TagShort target;
            string   expected;
            string   actual;

            expected = "16383";

            target = new TagShort(string.Empty, (short)(short.MaxValue >> 1));

            // act
            actual = target.ToValueString();

            // assert
            Assert.AreEqual(expected, actual);
        }
Example #19
0
    public void ConstructorWithNameAndValueTest()
    {
      // arrange
      TagShort tag;
      string name;
      short value;

      name = "creationDate";
      value = short.MaxValue;

      // act
      tag = new TagShort(name, value);

      // assert
      Assert.AreEqual(name, tag.Name);
      Assert.AreEqual(value, tag.Value);
    }
Example #20
0
        public void GetHashCode_returns_same_value_for_matching_tags()
        {
            // arrange
            TagShort target;
            int      actual;
            int      expected;

            target = new TagShort("beta", (short)(short.MaxValue >> 1));

            expected = new TagShort("beta", (short)(short.MaxValue >> 1)).GetHashCode();

            // act
            actual = target.GetHashCode();

            // assert
            Assert.AreEqual(expected, actual);
        }
Example #21
0
        public void SetValue_updates_value()
        {
            // arrange
            Tag   target;
            short expected;
            short actual;

            target = new TagShort();

            expected = (short)(short.MaxValue >> 1);

            // act
            target.SetValue(expected);

            // assert
            actual = ((TagShort)target).Value;
            Assert.AreEqual(expected, actual);
        }
Example #22
0
        public void Value_can_be_set()
        {
            // arrange
            TagShort target;
            short    expected;
            short    actual;

            expected = (short)(short.MaxValue >> 1);

            target = new TagShort();

            // act
            target.Value = expected;

            // assert
            actual = target.Value;
            Assert.AreEqual(expected, actual);
        }
Example #23
0
        public void Clear_removes_parents()
        {
            // arrange
            var actual1 = new TagByte("alpha", 56);
            var actual2 = new TagShort("beta", 56);

            var owner  = new TagCompound();
            var target = owner.Value;

            target.Add(actual1);
            target.Add(actual2);

            // act
            target.Clear();

            // assert
            Assert.Null(actual1.Parent);
            Assert.Null(actual2.Parent);
        }
Example #24
0
        public override object ParsePayload(Stream stream, INamedBinaryTag tagBase)
        {
            byte[] data = new byte[2];
            if (stream.Read(data, 0, 2) < 2)
            {
                throw new EndOfStreamException("End of stream reached inside of tag. Put those bytes back!");
            }

            data = data.ReverseIfLittleEndian();
            short val = BitConverter.ToInt16(data, 0);

            TagShort tag = tagBase as TagShort;

            if (tag == null)
            {
                throw new InvalidCastException("Wrong NBT type! Expected TagShort, found " + tagBase.GetType().Name);
            }
            tag.Value = val;
            return(val);
        }
        public void Clear_removes_parents()
        {
            // arrange
            TagCompound   owner;
            TagDictionary target;
            Tag           actual1;
            Tag           actual2;

            actual1 = new TagByte("alpha", 56);
            actual2 = new TagShort("beta", 56);

            owner  = new TagCompound();
            target = owner.Value;

            target.Add(actual1);
            target.Add(actual2);

            // act
            target.Clear();

            // assert
            Assert.IsNull(actual1.Parent);
            Assert.IsNull(actual2.Parent);
        }
Example #26
0
    public void ToValueStringTest()
    {
      // arrange
      ITag target;
      string expected;
      string actual;
      short value;

      value = short.MaxValue;
      expected = value.ToString(CultureInfo.InvariantCulture);
      target = new TagShort(value);

      // act
      actual = target.ToValueString();

      // assert
      Assert.AreEqual(expected, actual);
    }
Example #27
0
        public Tag ExportTag()
        {
            tc ["SleepTimer"] = new TagShort(SleepTimer);

            TagList<TagDouble > motion = new TagList<TagDouble>();
            motion [0] = new TagDouble(Motion [0]);
            motion [1] = new TagDouble(Motion [1]);
            motion [2] = new TagDouble(Motion [2]);
            tc ["Motion"] = motion;
            tc ["OnGround"] = new TagByte(){Byte = OnGround};
            tc ["HurtTime"] = new TagShort(HurtTime);
            tc ["Health"] = new TagShort(Health);

            tc ["Dimension"] = new TagInt(Dimension);
            tc ["Air"] = new TagShort(Air);
			
            if (tc ["Inventory"] is TagList<TagCompound> == false)
            {
                tc ["Inventory"] = new TagList<TagCompound>();
            }
            TagList<TagCompound > inv = tc ["Inventory"] as TagList<TagCompound>;
			
            for (byte n = 0; n < 104; n++)
            {
                SlotItem item = null;
                if (n < 36)
                    item = Inventory [n];
                if (n >= 80 && n < 84)
                    item = InventoryCraft [n - 80];
                if (n >= 100)
                    item = InventoryWear [n - 100];

                TagCompound ti = null;
				
                //Find slot item
                foreach (TagCompound itc in inv)
                {
                    if (itc ["Slot"].Byte == n)
                    {
                        ti = itc;
                        break;
                    }
                }
				
                if (item == null)
                {
                    if (ti != null)
                        inv.Remove(ti);
                    continue;
                }
                if (ti == null)
                {
                    ti = new TagCompound();
                    inv.Add(ti);
                }
				
                ti ["id"] = new TagShort((short)item.ItemID);
                ti ["Damage"] = new TagShort((short)item.Uses);
                ti ["Count"] = new TagByte((byte)item.Count);
                ti ["Slot"] = new TagByte(n);
            }
            inv.Sort((x, y) => x ["Slot"].Byte - y ["Slot"].Byte);

            TagList<TagDouble > p = new TagList<TagDouble>();
            p [0] = new TagDouble(Pos .X);
            p [1] = new TagDouble(Pos .Y);
            p [2] = new TagDouble(Pos .Z);
            tc ["Pos"] = p;
            tc ["AttackTime"] = new TagShort(AttackTime);
            tc ["Sleeping"] = new TagByte(Sleeping);
            tc ["Fire"] = new TagShort(Fire);
            tc ["FallDistance"] = new TagFloat(FallDistance);
            TagList<TagFloat > rot = new TagList<TagFloat>();
            rot [0] = new  TagFloat(Rotation [0]);
            rot [1] = new  TagFloat(Rotation [1]);
            tc ["Rotation"] = rot;
            tc ["DeathTime"] = new TagShort(DeathTime);

            if (Spawn != null)
            {
                tc ["SpawnX"] = new TagInt(Spawn.X);
                tc ["SpawnY"] = new TagInt(Spawn.Y);
                tc ["SpawnZ"] = new TagInt(Spawn.Z);
            }
			
            tc ["foodExhaustionLevel"] = new TagFloat(foodExhaustionLevel);
            tc ["foodTickTimer"] = new TagInt(foodTickTimer);
            tc ["foodSaturationLevel"] = new TagFloat(foodSaturationLevel);
            tc ["foodLevel"] = new TagInt(foodLevel);
            tc ["XpLevel"] = new TagInt(XpLevel);
            tc ["XpTotal"] = new TagInt(XpTotal);
            tc ["Xp"] = new TagInt(Xp);
            tc ["playerGameType"] = new TagInt(playerGameType);
			
            return tc;
        }
Example #28
0
        public void TestLoadComplexNbt()
        {
            Tag tag;

            tag = this.CreateComplexData();

            Assert.IsNotNull(tag);
            Assert.IsInstanceOf <TagCompound>(tag);
            TagCompound level = tag as TagCompound;

            Assert.AreEqual("Level", level.Name);

            TagShort shortTest = level.GetShort("shortTest");

            Assert.IsNotNull(shortTest);
            Assert.AreEqual("shortTest", shortTest.Name);
            Assert.AreEqual(32767, shortTest.Value);

            TagLong longTest = level.GetLong("longTest");

            Assert.IsNotNull(longTest);
            Assert.AreEqual("longTest", longTest.Name);
            Assert.AreEqual(9223372036854775807, longTest.Value);

            TagFloat floatTest = level.GetFloat("floatTest");

            Assert.IsNotNull(floatTest);
            Assert.AreEqual("floatTest", floatTest.Name);
            Assert.AreEqual(0.49823147f, floatTest.Value);

            TagString stringTest = level.GetString("stringTest");

            Assert.IsNotNull(stringTest);
            Assert.AreEqual("stringTest", stringTest.Name);
            Assert.AreEqual("HELLO WORLD THIS IS A TEST STRING едж!", stringTest.Value);

            TagInt intTest = level.GetInt("intTest");

            Assert.IsNotNull(intTest);
            Assert.AreEqual("intTest", intTest.Name);
            Assert.AreEqual(2147483647, intTest.Value);

            TagCompound nestedCompoundTest = level.GetCompound("nested compound test");

            Assert.IsNotNull(nestedCompoundTest);
            Assert.AreEqual("nested compound test", nestedCompoundTest.Name);

            TagCompound ham = nestedCompoundTest.GetCompound("ham");

            Assert.IsNotNull(ham);
            Assert.AreEqual("ham", ham.Name);

            TagString ham_name = ham.GetString("name");

            Assert.IsNotNull(ham_name);
            Assert.AreEqual("name", ham_name.Name);
            Assert.AreEqual("Hampus", ham_name.Value);

            TagFloat ham_value = ham.GetFloat("value");

            Assert.IsNotNull(ham_value);
            Assert.AreEqual("value", ham_value.Name);
            Assert.AreEqual(0.75f, ham_value.Value);

            TagCompound egg = nestedCompoundTest.GetCompound("egg");

            Assert.IsNotNull(egg);
            Assert.AreEqual("egg", egg.Name);

            TagString egg_name = egg.GetString("name");

            Assert.IsNotNull(egg_name);
            Assert.AreEqual("name", egg_name.Name);
            Assert.AreEqual("Eggbert", egg_name.Value);

            TagFloat egg_value = egg.GetFloat("value");

            Assert.IsNotNull(egg_value);
            Assert.AreEqual("value", egg_value.Name);
            Assert.AreEqual(0.5f, egg_value.Value);

            TagByte byteTest = level.GetByte("byteTest");

            Assert.IsNotNull(byteTest);
            Assert.AreEqual("byteTest", byteTest.Name);
            Assert.AreEqual(0x7f, byteTest.Value);

            TagDouble doubleTest = level.GetDouble("doubleTest");

            Assert.IsNotNull(doubleTest);
            Assert.AreEqual("doubleTest", doubleTest.Name);
            Assert.AreEqual(0.4931287132182315, doubleTest.Value);

            TagList listTest_long = level.GetList("listTest (long)");

            Assert.IsNotNull(listTest_long);
            Assert.AreEqual("listTest (long)", listTest_long.Name);
            Assert.IsNotNull(listTest_long.Value);
            Assert.AreEqual(5, listTest_long.Value.Count);
            Assert.AreEqual(11, (listTest_long.Value[0] as TagLong).Value);
            Assert.AreEqual(12, (listTest_long.Value[1] as TagLong).Value);
            Assert.AreEqual(13, (listTest_long.Value[2] as TagLong).Value);
            Assert.AreEqual(14, (listTest_long.Value[3] as TagLong).Value);
            Assert.AreEqual(15, (listTest_long.Value[4] as TagLong).Value);

            TagList listTest_compound = level.GetList("listTest (compound)");

            Assert.IsNotNull(listTest_compound);
            Assert.AreEqual("listTest (compound)", listTest_compound.Name);
            Assert.IsNotNull(listTest_compound.Value);
            Assert.AreEqual(2, listTest_compound.Value.Count);
            TagCompound listTest_compound_tag0 = listTest_compound.Value[0] as TagCompound;

            Assert.IsNotNull(listTest_compound_tag0);
            TagString listTest_compound_tag0_name = listTest_compound_tag0.GetString("name");

            Assert.IsNotNull(listTest_compound_tag0_name);
            Assert.AreEqual("name", listTest_compound_tag0_name.Name);
            Assert.AreEqual("Compound tag #0", listTest_compound_tag0_name.Value);
            TagLong listTest_compound_tag0_createdOn = listTest_compound_tag0.GetLong("created-on");

            Assert.IsNotNull(listTest_compound_tag0_createdOn);
            Assert.AreEqual("created-on", listTest_compound_tag0_createdOn.Name);
            Assert.AreEqual(1264099775885, listTest_compound_tag0_createdOn.Value);

            TagCompound listTest_compound_tag1 = listTest_compound.Value[1] as TagCompound;

            Assert.IsNotNull(listTest_compound_tag1);
            TagString listTest_compound_tag1_name = listTest_compound_tag1.GetString("name");

            Assert.IsNotNull(listTest_compound_tag1_name);
            Assert.AreEqual("name", listTest_compound_tag1_name.Name);
            Assert.AreEqual("Compound tag #1", listTest_compound_tag1_name.Value);
            TagLong listTest_compound_tag1_createdOn = listTest_compound_tag1.GetLong("created-on");

            Assert.IsNotNull(listTest_compound_tag1_createdOn);
            Assert.AreEqual("created-on", listTest_compound_tag1_createdOn.Name);
            Assert.AreEqual(1264099775885, listTest_compound_tag1_createdOn.Value);

            TagByteArray byteArrayTest = level.GetByteArray("byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))");

            Assert.IsNotNull(byteArrayTest);
            Assert.AreEqual("byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))", byteArrayTest.Name);
            Assert.IsNotNull(byteArrayTest.Value);
            Assert.AreEqual(1000, byteArrayTest.Value.Length);
        }
Example #29
0
    public void NameTest()
    {
      // arrange
      TagShort target;
      string expected;

      target = new TagShort();
      expected = "newvalue";

      // act
      target.Name = expected;

      // assert
      Assert.AreEqual(expected, target.Name);
    }
Example #30
0
    public void ToStringTest()
    {
      // arrange
      TagShort target;
      string expected;
      string actual;
      string name;
      short value;

      name = "tagname";
      value = short.MaxValue;
      expected = string.Format("[Short: {0}={1}]", name, value);
      target = new TagShort(name, value);

      // act
      actual = target.ToString();

      // assert
      Assert.AreEqual(expected, actual);
    }
Example #31
0
    public void ToStringWithIndentTest()
    {
      // arrange
      TagShort target;
      string expected;
      string actual;
      string name;
      short value;
      string prefix;

      prefix = "test";
      name = "tagname";
      value = short.MaxValue;
      expected = string.Format("{2}[Short: {0}={1}]", name, value, prefix);
      target = new TagShort(name, value);

      // act
      actual = target.ToString(prefix);

      // assert
      Assert.AreEqual(expected, actual);
    }
Example #32
0
    public void TypeTest()
    {
      // arrange
      TagType expected;
      TagType actual;

      expected = TagType.Short;

      // act
      actual = new TagShort().Type;

      // assert
      Assert.AreEqual(expected, actual);
    }
Example #33
0
    public void ValueTest()
    {
      // arrange
      TagShort target;
      short expected;

      target = new TagShort();
      expected = short.MaxValue;

      // act
      target.Value = expected;

      // assert
      Assert.AreEqual(expected, target.Value);
    }