public void ReadValueTest()
        {
            byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None);
            var    reader   = new NbtReader(new MemoryStream(testData));

            Assert.True(reader.ReadToFollowing()); // root

            Assert.True(reader.ReadToFollowing()); // byte
            Assert.Equal((byte)1, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // short
            Assert.Equal((Int16)2, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // int
            Assert.Equal(3, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // long
            Assert.Equal(4L, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // float
            Assert.Equal(5f, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // double
            Assert.Equal(6d, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // byteArray
            Assert.Equal(new byte[] { 10, 11, 12 }, (byte[])reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // intArray
            Assert.Equal(new[] { 20, 21, 22 }, (int[])reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // string
            Assert.Equal("123", reader.ReadValue());

            // Skip to the very end and make sure that we can't read any more values
            reader.ReadToFollowing();
            Assert.Throws <EndOfStreamException>(() => reader.ReadValue());
        }
 public void PartialReadTest()
 {
     // read the whole thing as one tag
     TestFiles.AssertValueTest(PartialReadTestInternal(new NbtFile(TestFiles.MakeValueTest())));
     TestFiles.AssertNbtSmallFile(PartialReadTestInternal(TestFiles.MakeSmallFile()));
     TestFiles.AssertNbtBigFile(PartialReadTestInternal(new NbtFile(TestFiles.Big)));
 }
        public void ReadValueAsTest()
        {
            byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None);
            var    reader   = new NbtReader(new MemoryStream(testData));

            Assert.True(reader.ReadToFollowing()); // root

            Assert.True(reader.ReadToFollowing()); // byte
            Assert.Equal(1, reader.ReadValueAs <byte>());
            Assert.True(reader.ReadToFollowing()); // short
            Assert.Equal(2, reader.ReadValueAs <short>());
            Assert.True(reader.ReadToFollowing()); // int
            Assert.Equal(3, reader.ReadValueAs <int>());
            Assert.True(reader.ReadToFollowing()); // long
            Assert.Equal(4L, reader.ReadValueAs <long>());
            Assert.True(reader.ReadToFollowing()); // float
            Assert.Equal(5f, reader.ReadValueAs <float>());
            Assert.True(reader.ReadToFollowing()); // double
            Assert.Equal(6d, reader.ReadValueAs <double>());
            Assert.True(reader.ReadToFollowing()); // byteArray
            Assert.Equal(new byte[] { 10, 11, 12 }, reader.ReadValueAs <byte[]>());
            Assert.True(reader.ReadToFollowing()); // intArray
            Assert.Equal(new[] { 20, 21, 22 }, reader.ReadValueAs <int[]>());
            Assert.True(reader.ReadToFollowing()); // string
            Assert.Equal("123", reader.ReadValueAs <string>());
        }
Beispiel #4
0
 public void PartialBatchReadTest()
 {
     // read the whole thing as one tag, in batches of 4 bytes
     // Verifies fix for https://github.com/fragmer/fNbt/issues/26
     TestFiles.AssertValueTest(PartialReadTestInternal(new NbtFile(TestFiles.MakeValueTest()), 4));
     TestFiles.AssertNbtSmallFile(PartialReadTestInternal(TestFiles.MakeSmallFile(), 4));
     TestFiles.AssertNbtBigFile(PartialReadTestInternal(new NbtFile(TestFiles.Big), 4));
 }
Beispiel #5
0
    public void SkippingValuesInCompoundTest()
    {
        NbtCompound root       = TestFiles.MakeValueTest();
        NbtCompound nestedComp = TestFiles.MakeValueTest();

        nestedComp.Name = "NestedComp";
        root.Add(nestedComp);

        var file = new NbtFile(root);

        byte[] savedFile = file.SaveToBuffer(NbtCompression.None);
        file.LoadFromBuffer(savedFile, 0, savedFile.Length, NbtCompression.None, tag => false);
        Assert.Empty(file.RootTag);
    }
        public void ReadAsTagTest3()
        {
            // read values as tags
            byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None);
            var    reader   = new NbtReader(new MemoryStream(testData));
            var    root     = new NbtCompound("root");

            // skip root
            reader.ReadToFollowing();
            reader.ReadToFollowing();

            while (!reader.IsAtStreamEnd)
            {
                root.Add(reader.ReadAsTag());
            }

            TestFiles.AssertValueTest(new NbtFile(root));
        }
 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));
     }
 }
Beispiel #8
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.True(writer.IsDone);
            writer.Finish();
        }
        ms.Position = 0;
        var  file      = new NbtFile();
        long bytesRead = file.LoadFromBuffer(ms.ToArray(), 0, (int)ms.Length, NbtCompression.None);

        Assert.Equal(bytesRead, ms.Length);
        TestFiles.AssertValueTest(file);
    }
        public void CacheTagValuesTest()
        {
            byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None);
            var    reader   = new NbtReader(new MemoryStream(testData));

            Assert.False(reader.CacheTagValues);
            reader.CacheTagValues = true;
            Assert.True(reader.ReadToFollowing()); // root

            Assert.True(reader.ReadToFollowing()); // byte
            Assert.Equal((byte)1, reader.ReadValue());
            Assert.Equal((byte)1, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // short
            Assert.Equal((Int16)2, reader.ReadValue());
            Assert.Equal((Int16)2, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // int
            Assert.Equal(3, reader.ReadValue());
            Assert.Equal(3, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // long
            Assert.Equal(4L, reader.ReadValue());
            Assert.Equal(4L, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // float
            Assert.Equal(5f, reader.ReadValue());
            Assert.Equal(5f, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // double
            Assert.Equal(6d, reader.ReadValue());
            Assert.Equal(6d, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // byteArray
            Assert.Equal(new byte[] { 10, 11, 12 }, (byte[])reader.ReadValue());
            Assert.Equal(new byte[] { 10, 11, 12 }, (byte[])reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // intArray
            Assert.Equal(new[] { 20, 21, 22 }, (int[])reader.ReadValue());
            Assert.Equal(new[] { 20, 21, 22 }, (int[])reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // string
            Assert.Equal("123", reader.ReadValue());
            Assert.Equal("123", reader.ReadValue());
        }