示例#1
0
 public void ReadAsTagTest2()
 {
     // read the whole thing as one tag
     byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None);
     {
         var reader = new NbtReader(new MemoryStream(testData));
         var root   = (NbtCompound)reader.ReadAsTag();
         TestFiles.AssertValueTest(new NbtFile(root));
     }
     {
         // Try the same thing but with end tag skipping disabled
         var reader = new NbtReader(new MemoryStream(testData))
         {
             SkipEndTags = false
         };
         var root = (NbtCompound)reader.ReadAsTag();
         TestFiles.AssertValueTest(new NbtFile(root));
     }
 }
示例#2
0
        public void ReadToDescendantTest()
        {
            var reader = new NbtReader(TestFiles.MakeReaderTest());

            Assert.IsTrue(reader.ReadToDescendant("third-comp"));
            Assert.AreEqual("third-comp", reader.TagName);
            Assert.IsTrue(reader.ReadToDescendant("inComp2"));
            Assert.AreEqual("inComp2", reader.TagName);
            Assert.IsFalse(reader.ReadToDescendant("derp"));
            Assert.AreEqual("inComp3", reader.TagName);
            reader.ReadToFollowing(); // at fourth-list
            Assert.IsTrue(reader.ReadToDescendant("inList2"));
            Assert.AreEqual("inList2", reader.TagName);

            // Ensure ReadToDescendant returns false when at end-of-stream
            while (reader.ReadToFollowing())
            {
            }
            Assert.IsFalse(reader.ReadToDescendant("*"));
        }
示例#3
0
        public void ReadToSiblingTest()
        {
            var reader = new NbtReader(TestFiles.MakeReaderTest());

            Assert.IsTrue(reader.ReadToFollowing());
            Assert.AreEqual("root", reader.TagName);
            Assert.IsTrue(reader.ReadToFollowing());
            Assert.AreEqual("first", reader.TagName);
            Assert.IsTrue(reader.ReadToNextSibling("third-comp"));
            Assert.AreEqual("third-comp", reader.TagName);
            Assert.IsTrue(reader.ReadToNextSibling());
            Assert.AreEqual("fourth-list", reader.TagName);
            Assert.IsTrue(reader.ReadToNextSibling());
            Assert.AreEqual("fifth", reader.TagName);
            Assert.IsTrue(reader.ReadToNextSibling());
            Assert.AreEqual("hugeArray", reader.TagName);
            Assert.IsFalse(reader.ReadToNextSibling());
            // Test twice, since we hit different paths through the code
            Assert.IsFalse(reader.ReadToNextSibling());
        }
示例#4
0
 public void WriteTagTest()
 {
     using (var ms = new MemoryStream()) {
         var writer = new NbtWriter(ms, "root");
         {
             foreach (NbtTag tag in TestFiles.MakeValueTest().Tags)
             {
                 writer.WriteTag(tag);
             }
             writer.EndCompound();
             Assert.IsTrue(writer.IsDone);
             writer.Finish();
         }
         ms.Position = 0;
         var  file      = new NbtFile();
         long bytesRead = file.LoadFromBuffer(ms.ToArray(), 0, (int)ms.Length, NbtCompression.None);
         Assert.AreEqual(bytesRead, ms.Length);
         TestFiles.AssertValueTest(file);
     }
 }
示例#5
0
        public void CacheTagValuesTest()
        {
            byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None);
            var    reader   = new NbtReader(new MemoryStream(testData));

            Assert.IsFalse(reader.CacheTagValues);
            reader.CacheTagValues = true;
            Assert.IsTrue(reader.ReadToFollowing()); // root

            Assert.IsTrue(reader.ReadToFollowing()); // byte
            Assert.AreEqual(1, reader.ReadValue());
            Assert.AreEqual(1, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // short
            Assert.AreEqual(2, reader.ReadValue());
            Assert.AreEqual(2, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // int
            Assert.AreEqual(3, reader.ReadValue());
            Assert.AreEqual(3, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // long
            Assert.AreEqual(4L, reader.ReadValue());
            Assert.AreEqual(4L, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // float
            Assert.AreEqual(5f, reader.ReadValue());
            Assert.AreEqual(5f, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // double
            Assert.AreEqual(6d, reader.ReadValue());
            Assert.AreEqual(6d, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // byteArray
            CollectionAssert.AreEqual(new byte[] { 10, 11, 12 }, (byte[])reader.ReadValue());
            CollectionAssert.AreEqual(new byte[] { 10, 11, 12 }, (byte[])reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // intArray
            CollectionAssert.AreEqual(new[] { 20, 21, 22 }, (int[])reader.ReadValue());
            CollectionAssert.AreEqual(new[] { 20, 21, 22 }, (int[])reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing());
            CollectionAssert.AreEqual(new long[] { 200, 210, 220 }, (long[])reader.ReadValue());
            CollectionAssert.AreEqual(new long[] { 200, 210, 220 }, (long[])reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // string
            Assert.AreEqual("123", reader.ReadValue());
            Assert.AreEqual("123", reader.ReadValue());
        }
示例#6
0
        public void PropertiesTest()
        {
            var reader = new NbtReader(TestFiles.MakeReaderTest());

            Assert.AreEqual(0, reader.Depth);
            Assert.AreEqual(0, reader.TagsRead);

            Assert.IsTrue(reader.ReadToFollowing());
            Assert.AreEqual("root", reader.TagName);
            Assert.AreEqual(NbtTagType.Compound, reader.TagType);
            Assert.AreEqual(NbtTagType.Unknown, reader.ListType);
            Assert.IsFalse(reader.HasValue);
            Assert.IsTrue(reader.IsCompound);
            Assert.IsFalse(reader.IsList);
            Assert.IsFalse(reader.IsListElement);
            Assert.IsFalse(reader.HasLength);
            Assert.AreEqual(0, reader.ListIndex);
            Assert.AreEqual(1, reader.Depth);
            Assert.AreEqual(null, reader.ParentName);
            Assert.AreEqual(NbtTagType.Unknown, reader.ParentTagType);
            Assert.AreEqual(0, reader.ParentTagLength);
            Assert.AreEqual(0, reader.TagLength);
            Assert.AreEqual(1, reader.TagsRead);

            Assert.IsTrue(reader.ReadToFollowing());
            Assert.AreEqual("first", reader.TagName);
            Assert.AreEqual(NbtTagType.Int, reader.TagType);
            Assert.AreEqual(NbtTagType.Unknown, reader.ListType);
            Assert.IsTrue(reader.HasValue);
            Assert.IsFalse(reader.IsCompound);
            Assert.IsFalse(reader.IsList);
            Assert.IsFalse(reader.IsListElement);
            Assert.IsFalse(reader.HasLength);
            Assert.AreEqual(0, reader.ListIndex);
            Assert.AreEqual(2, reader.Depth);
            Assert.AreEqual("root", reader.ParentName);
            Assert.AreEqual(NbtTagType.Compound, reader.ParentTagType);
            Assert.AreEqual(0, reader.ParentTagLength);
            Assert.AreEqual(0, reader.TagLength);
            Assert.AreEqual(2, reader.TagsRead);

            Assert.IsTrue(reader.ReadToFollowing("fourth-list"));
            Assert.AreEqual("fourth-list", reader.TagName);
            Assert.AreEqual(NbtTagType.List, reader.TagType);
            Assert.AreEqual(NbtTagType.List, reader.ListType);
            Assert.IsFalse(reader.HasValue);
            Assert.IsFalse(reader.IsCompound);
            Assert.IsTrue(reader.IsList);
            Assert.IsFalse(reader.IsListElement);
            Assert.IsTrue(reader.HasLength);
            Assert.AreEqual(0, reader.ListIndex);
            Assert.AreEqual(2, reader.Depth);
            Assert.AreEqual("root", reader.ParentName);
            Assert.AreEqual(NbtTagType.Compound, reader.ParentTagType);
            Assert.AreEqual(0, reader.ParentTagLength);
            Assert.AreEqual(3, reader.TagLength);
            Assert.AreEqual(8, reader.TagsRead);

            Assert.IsTrue(reader.ReadToFollowing()); // first list element, itself a list
            Assert.AreEqual(null, reader.TagName);
            Assert.AreEqual(NbtTagType.List, reader.TagType);
            Assert.AreEqual(NbtTagType.Compound, reader.ListType);
            Assert.IsFalse(reader.HasValue);
            Assert.IsFalse(reader.IsCompound);
            Assert.IsTrue(reader.IsList);
            Assert.IsTrue(reader.IsListElement);
            Assert.IsTrue(reader.HasLength);
            Assert.AreEqual(0, reader.ListIndex);
            Assert.AreEqual(3, reader.Depth);
            Assert.AreEqual("fourth-list", reader.ParentName);
            Assert.AreEqual(NbtTagType.List, reader.ParentTagType);
            Assert.AreEqual(3, reader.ParentTagLength);
            Assert.AreEqual(1, reader.TagLength);
            Assert.AreEqual(9, reader.TagsRead);

            Assert.IsTrue(reader.ReadToFollowing()); // first nested list element, compound
            Assert.AreEqual(null, reader.TagName);
            Assert.AreEqual(NbtTagType.Compound, reader.TagType);
            Assert.AreEqual(NbtTagType.Unknown, reader.ListType);
            Assert.IsFalse(reader.HasValue);
            Assert.IsTrue(reader.IsCompound);
            Assert.IsFalse(reader.IsList);
            Assert.IsTrue(reader.IsListElement);
            Assert.IsFalse(reader.HasLength);
            Assert.AreEqual(0, reader.ListIndex);
            Assert.AreEqual(4, reader.Depth);
            Assert.AreEqual(null, reader.ParentName);
            Assert.AreEqual(NbtTagType.List, reader.ParentTagType);
            Assert.AreEqual(1, reader.ParentTagLength);
            Assert.AreEqual(0, reader.TagLength);
            Assert.AreEqual(10, reader.TagsRead);

            Assert.IsTrue(reader.ReadToFollowing("fifth"));
            Assert.AreEqual("fifth", reader.TagName);
            Assert.AreEqual(NbtTagType.Int, reader.TagType);
            Assert.AreEqual(NbtTagType.Unknown, reader.ListType);
            Assert.IsTrue(reader.HasValue);
            Assert.IsFalse(reader.IsCompound);
            Assert.IsFalse(reader.IsList);
            Assert.IsFalse(reader.IsListElement);
            Assert.IsFalse(reader.HasLength);
            Assert.AreEqual(0, reader.ListIndex);
            Assert.AreEqual(2, reader.Depth);
            Assert.AreEqual("root", reader.ParentName);
            Assert.AreEqual(NbtTagType.Compound, reader.ParentTagType);
            Assert.AreEqual(0, reader.ParentTagLength);
            Assert.AreEqual(0, reader.TagLength);
            Assert.AreEqual(18, reader.TagsRead);

            Assert.IsTrue(reader.ReadToFollowing());
            Assert.AreEqual("hugeArray", reader.TagName);
            Assert.AreEqual(NbtTagType.ByteArray, reader.TagType);
            Assert.AreEqual(NbtTagType.Unknown, reader.ListType);
            Assert.IsTrue(reader.HasValue);
            Assert.IsFalse(reader.IsCompound);
            Assert.IsFalse(reader.IsList);
            Assert.IsFalse(reader.IsListElement);
            Assert.IsTrue(reader.HasLength);
            Assert.AreEqual(0, reader.ListIndex);
            Assert.AreEqual(2, reader.Depth);
            Assert.AreEqual("root", reader.ParentName);
            Assert.AreEqual(NbtTagType.Compound, reader.ParentTagType);
            Assert.AreEqual(0, reader.ParentTagLength);
            Assert.AreEqual(1024 * 1024, reader.TagLength);
            Assert.AreEqual(19, reader.TagsRead);
        }