public static MakeListTest ( ) : NbtCompound | ||
Résultat | NbtCompound |
public void ReadAsTagTest4() { // read a bunch of lists as tags byte[] testData = new NbtFile(TestFiles.MakeListTest()).SaveToBuffer(NbtCompression.None); // first, read everything all-at-once { var reader = new NbtReader(new MemoryStream(testData)); while (!reader.IsAtStreamEnd) { Console.WriteLine(reader.ReadAsTag()); } } // next, read each list individually { var reader = new NbtReader(new MemoryStream(testData)); reader.ReadToFollowing(); // read to root reader.ReadToFollowing(); // read to first list tag while (!reader.IsAtStreamEnd) { Console.WriteLine(reader.ReadAsTag()); } } }
public void SkippingLists() { { var file = new NbtFile(TestFiles.MakeListTest()); byte[] savedFile = file.SaveToBuffer(NbtCompression.None); file.LoadFromBuffer(savedFile, 0, savedFile.Length, NbtCompression.None, tag => tag.TagType != NbtTagType.List); Assert.AreEqual(0, file.RootTag.Count); } { // Check list-compound interaction NbtCompound comp = new NbtCompound("root") { new NbtCompound("compOfLists") { new NbtList("listOfComps") { new NbtCompound { new NbtList("emptyList", NbtTagType.Compound) } } } }; var file = new NbtFile(comp); byte[] savedFile = file.SaveToBuffer(NbtCompression.None); file.LoadFromBuffer(savedFile, 0, savedFile.Length, NbtCompression.None, tag => tag.TagType != NbtTagType.List); Assert.AreEqual(1, file.RootTag.Count); } }
public void ReadListAsArray() { NbtCompound intList = TestFiles.MakeListTest(); var ms = new MemoryStream(); new NbtFile(intList).SaveToStream(ms, NbtCompression.None); ms.Seek(0, SeekOrigin.Begin); var reader = new NbtReader(ms); // attempt to read value before we're in a list Assert.Throws <InvalidOperationException>(() => reader.ReadListAsArray <int>()); // test byte values reader.ReadToFollowing("ByteList"); byte[] bytes = reader.ReadListAsArray <byte>(); CollectionAssert.AreEqual(new byte[] { 100, 20, 3 }, bytes); // test double values reader.ReadToFollowing("DoubleList"); double[] doubles = reader.ReadListAsArray <double>(); CollectionAssert.AreEqual(new[] { 1d, 2000d, -3000000d }, doubles); // test float values reader.ReadToFollowing("FloatList"); float[] floats = reader.ReadListAsArray <float>(); CollectionAssert.AreEqual(new[] { 1f, 2000f, -3000000f }, floats); // test int values reader.ReadToFollowing("IntList"); int[] ints = reader.ReadListAsArray <int>(); CollectionAssert.AreEqual(new[] { 1, 2000, -3000000 }, ints); // test long values reader.ReadToFollowing("LongList"); long[] longs = reader.ReadListAsArray <long>(); CollectionAssert.AreEqual(new[] { 1L, 2000L, -3000000L }, longs); // test short values reader.ReadToFollowing("ShortList"); short[] shorts = reader.ReadListAsArray <short>(); CollectionAssert.AreEqual(new short[] { 1, 200, -30000 }, shorts); // test short values reader.ReadToFollowing("StringList"); string[] strings = reader.ReadListAsArray <string>(); CollectionAssert.AreEqual(new[] { "one", "two thousand", "negative three million" }, strings); // try reading list of compounds (should fail) reader.ReadToFollowing("CompoundList"); Assert.Throws <InvalidOperationException>(() => reader.ReadListAsArray <NbtCompound>()); // skip to the end of the stream while (reader.ReadToFollowing()) { } Assert.Throws <EndOfStreamException>(() => reader.ReadListAsArray <int>()); }
public void Serializing2() { // check saving/loading lists of all possible value types var testFile = new NbtFile(TestFiles.MakeListTest()); byte[] buffer = testFile.SaveToBuffer(NbtCompression.None); long bytesRead = testFile.LoadFromBuffer(buffer, 0, buffer.Length, NbtCompression.None); Assert.AreEqual(bytesRead, buffer.Length); }
public void ReadListAsArrayRecast() { NbtCompound intList = TestFiles.MakeListTest(); var ms = new MemoryStream(); new NbtFile(intList).SaveToStream(ms, NbtCompression.None); ms.Seek(0, SeekOrigin.Begin); var reader = new NbtReader(ms); // test bytes as shorts reader.ReadToFollowing("ByteList"); short[] bytes = reader.ReadListAsArray <short>(); CollectionAssert.AreEqual(bytes, new short[] { 100, 20, 3 }); }