public NbtTag GetChunkData(int chunk) { if (chunkSizes[chunk] == 0) //Chunk is not generated { return(null); } file.Seek(chunkOffsets[chunk] * SizeOfChunkPiece, SeekOrigin.Begin); byte[] sizeAndCompression = new byte[5]; file.Read(sizeAndCompression, 0, 5); // First 4 are exact chunk size, last is compression scheme (1 = GZip, not used; 2 = ZLib). BIG F*****G ENDIAN. uint size = (uint)sizeAndCompression[0] * 256 * 256 * 256 + (uint)sizeAndCompression[1] * 256 * 256 + (uint)sizeAndCompression[2] * 256 + (uint)sizeAndCompression[3]; byte compression = sizeAndCompression[4]; if (compression == 1) { throw new NotImplementedException("GZip Not Supported"); } byte[] compressed = new byte[size]; file.Read(compressed, 0, (int)size); Stream cStream = new MemoryStream(compressed, false); Stream rawStream = new InflaterInputStream(cStream); NbtTag nbt = new NbtReader(rawStream).ReadAsTag(); return(nbt); }
public void ReadValueTest() { byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None); var reader = new NbtReader(new MemoryStream(testData)); Assert.IsTrue(reader.ReadToFollowing()); // root Assert.IsTrue(reader.ReadToFollowing()); // byte Assert.AreEqual(1, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // short Assert.AreEqual(2, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // int Assert.AreEqual(3, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // long Assert.AreEqual(4L, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // float Assert.AreEqual(5f, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // double Assert.AreEqual(6d, reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // byteArray CollectionAssert.AreEqual(new byte[] { 10, 11, 12 }, (byte[])reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // intArray CollectionAssert.AreEqual(new[] { 20, 21, 22 }, (int[])reader.ReadValue()); Assert.IsTrue(reader.ReadToFollowing()); // string Assert.AreEqual("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 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 ErrorTest() { var root = new NbtCompound("root"); byte[] testData = new NbtFile(root).SaveToBuffer(NbtCompression.None); // creating NbtReader without a stream, or with a non-readable stream Assert.Throws<ArgumentNullException>(() => new NbtReader(null)); Assert.Throws<ArgumentException>(() => new NbtReader(new NonReadableStream())); // corrupt the data testData[0] = 123; var reader = new NbtReader(new MemoryStream(testData)); // attempt to use ReadValue when not at value Assert.Throws<InvalidOperationException>(() => reader.ReadValue()); reader.CacheTagValues = true; Assert.Throws<InvalidOperationException>(() => reader.ReadValue()); // attempt to read a corrupt stream Assert.Throws<NbtFormatException>(() => reader.ReadToFollowing()); // make sure we've properly entered the error state Assert.IsTrue(reader.IsInErrorState); Assert.IsFalse(reader.HasName); Assert.Throws<InvalidReaderStateException>(() => reader.ReadToFollowing()); Assert.Throws<InvalidReaderStateException>(() => reader.ReadListAsArray<int>()); Assert.Throws<InvalidReaderStateException>(() => reader.ReadToNextSibling()); Assert.Throws<InvalidReaderStateException>(() => reader.ReadToDescendant("derp")); Assert.Throws<InvalidReaderStateException>(() => reader.ReadAsTag()); Assert.Throws<InvalidReaderStateException>(() => reader.Skip()); }
public void ReadValueAsTest() { byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None); var reader = new NbtReader(new MemoryStream(testData)); Assert.IsTrue(reader.ReadToFollowing()); // root Assert.IsTrue(reader.ReadToFollowing()); // byte Assert.AreEqual(1, reader.ReadValueAs <byte>()); Assert.IsTrue(reader.ReadToFollowing()); // short Assert.AreEqual(2, reader.ReadValueAs <short>()); Assert.IsTrue(reader.ReadToFollowing()); // int Assert.AreEqual(3, reader.ReadValueAs <int>()); Assert.IsTrue(reader.ReadToFollowing()); // long Assert.AreEqual(4L, reader.ReadValueAs <long>()); Assert.IsTrue(reader.ReadToFollowing()); // float Assert.AreEqual(5f, reader.ReadValueAs <float>()); Assert.IsTrue(reader.ReadToFollowing()); // double Assert.AreEqual(6d, reader.ReadValueAs <double>()); Assert.IsTrue(reader.ReadToFollowing()); // byteArray CollectionAssert.AreEqual(new byte[] { 10, 11, 12 }, reader.ReadValueAs <byte[]>()); Assert.IsTrue(reader.ReadToFollowing()); // intArray CollectionAssert.AreEqual(new[] { 20, 21, 22 }, reader.ReadValueAs <int[]>()); Assert.IsTrue(reader.ReadToFollowing()); // string Assert.AreEqual("123", reader.ReadValueAs <string>()); }
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); // Read through the rest of the file until we run out of tags in a compound Assert.IsFalse(reader.ReadToDescendant("*")); // Ensure ReadToDescendant returns false when at end-of-stream while (reader.ReadToFollowing()) { } Assert.IsFalse(reader.ReadToDescendant("*")); // Ensure that this works even on the root Assert.IsFalse(new NbtReader(TestFiles.MakeReaderTest()).ReadToDescendant("*")); }
public void EndTagTest() { using (MemoryStream ms = new MemoryStream()) { var root = new NbtCompound("root") { new NbtInt("test", 0) }; new NbtFile(root).SaveToStream(ms, NbtCompression.None); ms.Position = 0; NbtReader reader = new NbtReader(ms) { SkipEndTags = false }; reader.ReadToDescendant("test"); Assert.AreEqual(NbtTagType.Int, reader.TagType); Assert.IsTrue(reader.ReadToNextSibling()); // should be at root's End tag now Assert.AreEqual(NbtTagType.End, reader.TagType); Assert.IsFalse(reader.IsInErrorState); Assert.IsFalse(reader.IsAtStreamEnd); Assert.IsFalse(reader.IsCompound); Assert.IsFalse(reader.IsList); Assert.IsFalse(reader.IsListElement); Assert.IsFalse(reader.HasValue); Assert.IsFalse(reader.HasName); Assert.IsFalse(reader.HasLength); Assert.Throws <InvalidOperationException>(() => reader.ReadAsTag()); // Cannot create NbtTag from TAG_END // We done now Assert.IsFalse(reader.ReadToFollowing()); Assert.IsTrue(reader.IsAtStreamEnd); } }
public void NestedListTest() { var root = new NbtCompound("root") { new NbtList("OuterList") { new NbtList { new NbtByte() }, new NbtList { new NbtShort() }, new NbtList { new NbtInt() } } }; byte[] testData = new NbtFile(root).SaveToBuffer(NbtCompression.None); using (var ms = new MemoryStream(testData)) { var reader = new NbtReader(ms); while (reader.ReadToFollowing()) { Console.WriteLine(reader.ToString(true)); } } }
public void ErrorTest() { var root = new NbtCompound("root"); byte[] testData = new NbtFile(root).SaveToBuffer(NbtCompression.None); // creating NbtReader without a stream, or with a non-readable stream Assert.Throws <ArgumentNullException>(() => new NbtReader(null)); Assert.Throws <ArgumentException>(() => new NbtReader(new NonReadableStream())); // corrupt the data testData[0] = 123; var reader = new NbtReader(new MemoryStream(testData)); // attempt to use ReadValue when not at value Assert.Throws <InvalidOperationException>(() => reader.ReadValue()); reader.CacheTagValues = true; Assert.Throws <InvalidOperationException>(() => reader.ReadValue()); // attempt to read a corrupt stream Assert.Throws <NbtFormatException>(() => reader.ReadToFollowing()); // make sure we've properly entered the error state Assert.IsTrue(reader.IsInErrorState); Assert.IsFalse(reader.HasName); Assert.Throws <InvalidReaderStateException>(() => reader.ReadToFollowing()); Assert.Throws <InvalidReaderStateException>(() => reader.ReadListAsArray <int>()); Assert.Throws <InvalidReaderStateException>(() => reader.ReadToNextSibling()); Assert.Throws <InvalidReaderStateException>(() => reader.ReadToDescendant("derp")); Assert.Throws <InvalidReaderStateException>(() => reader.ReadAsTag()); Assert.Throws <InvalidReaderStateException>(() => reader.Skip()); }
public Map LoadHeader(string path) { using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress)) { NbtReader reader = new NbtReader(gs); reader.ReadToFollowing(); // skip root tag reader.ReadToFollowing(); // skip to first inner tag int width = 0, length = 0, height = 0; do { switch (reader.TagName) { case "X": width = reader.ReadValueAs <short>(); break; case "Y": height = reader.ReadValueAs <short>(); break; case "Z": length = reader.ReadValueAs <short>(); break; } if (width > 0 && length > 0 && height > 0) { return(new Map(null, width, length, height, false)); } } while (reader.ReadToNextSibling()); } } throw new MapFormatException("Could not locate map dimensions."); }
public override void Load(NbtReader reader, INbtContainerNode parent) { base.Load(reader, parent); dataLength = reader.ReadInt32(); dataLocation = reader.BaseStream.Position; this.reader = reader; }
public void LoadRegion(int RegionX, int RegionZ) { R = 2; Xs = RegionX; Zs = RegionZ; if (lastX != RegionX || lastZ != RegionZ) { lastX = RegionX; lastZ = RegionZ; FileInfo f = new FileInfo(Path.Combine(mcaFilePath, $@"r.{lastX}.{lastZ}.mca")); LastModified = DateTime.MaxValue; if (f.Exists) { LastModified = f.LastWriteTime; for (int c = 0; c < 1024; c++) { chunks[c] = null; } Count = 0; using (FileStream fs = f.OpenRead()) { for (int c = 0; c < 1024; c++) { chunkhdr[c] = NbtReader.TagInt24(fs) * 4096; chunksect[c] = NbtReader.TagByte(fs) * 4096; } for (int c = 0; c < 1024; c++) { timehdr[c] = DateTime.FromBinary(NbtReader.TagInt(fs)); } for (int c = 0; c < 1024; c++) { try { fs.Seek(chunkhdr[c], SeekOrigin.Begin); chunks[c] = new ChunkMCAEx(chunkhdr[c], chunksect[c], fs); Count += 1; } catch (Exception) { break; } } fs.Close(); } } } }
private static (string, Dictionary <string, Object>) GetNbtVal(MemoryStream ms) { string name = ""; var nbt = new NbtReader(ms, false); var dictParams = new Dictionary <string, Object>(); nbt.ReadToFollowing(); if (!nbt.IsCompound) { throw new Exception("Could not read nbt"); } if (nbt.ReadToDescendant("name")) { name = nbt.ReadValueAs <string>(); nbt.ReadToNextSibling(); while (nbt.TagType != NbtTagType.End) { fNbt.Tags.NbtTag tag = nbt.ReadAsTag(); switch (tag.Name) { case "version": continue; case "states": IEnumerable <fNbt.Tags.NbtTag> enumTag = (IEnumerable <fNbt.Tags.NbtTag>)tag; foreach (var subtag in enumTag) { if ((subtag.Name == "direction") || (subtag.Name == "facing_direction") || (subtag.Name == "open_bit")) { int subtagvalue = GetTagValue(subtag); dictParams.Add(subtag.Name, subtagvalue); } if ((subtag.Name == "color") || (subtag.Name == "lever_direction")) { if (subtag.TagType == NbtTagType.String) { dictParams.Add(subtag.Name, subtag.StringValue); } } } break; case "val": int value = GetTagValue(tag); dictParams.Add(tag.Name, value); break; } } } while (!nbt.IsAtStreamEnd) { nbt.ReadToFollowing(); } return(name, dictParams); }
static NbtFile PartialReadTestInternal(NbtFile comp) { byte[] testData = comp.SaveToBuffer(NbtCompression.None); var reader = new NbtReader(new PartialReadStream(new MemoryStream(testData))); var root = (NbtCompound)reader.ReadAsTag(); return(new NbtFile(root)); }
public static NbtCompound GetNbtCompoundFromBytes(byte[] data) { using (MemoryStream stream = new MemoryStream(data)) { NbtReader reader = new NbtReader(stream); return((NbtCompound)reader.ReadAsTag()); } }
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 NbtReadBasic() { var nbtReader = new NbtReader(_testBytes); var compound = nbtReader.ReadCompound(); var originalCompound = (NbtCompound)_testCompound.Value["hello world"]; var readCompound = (NbtCompound)compound.Value["hello world"]; Assert.AreEqual(originalCompound.Value["name"], readCompound.Value["name"]); }
public void ReadToFollowingNotFound() { var reader = new NbtReader(TestFiles.MakeReaderTest()); Assert.IsTrue(reader.ReadToFollowing()); // at "root" Assert.IsFalse(reader.ReadToFollowing("no such tag")); Assert.IsFalse(reader.ReadToFollowing("not this one either")); Assert.IsTrue(reader.IsAtStreamEnd); }
public virtual void Load(NbtReader reader, INbtContainerNode parent) { Parent = parent; if (parent == null || parent.Type != NbtType.List) { reader.ReadType(); // Skip type byte Name = reader.ReadString(); } }
private static (string, Dictionary <string, Object>) GetNbtVal(MemoryStream ms) { string name = ""; var nbt = new NbtReader(ms, false); var dictParams = new Dictionary <string, Object>(); nbt.ReadToFollowing(); if (!nbt.IsCompound) { throw new Exception("Could not read nbt"); } if (nbt.ReadToDescendant("name")) { name = nbt.ReadValueAs <string>(); nbt.ReadToNextSibling(); while (nbt.TagType != NbtTagType.End) { fNbt.Tags.NbtTag tag = nbt.ReadAsTag(); switch (tag.Name) { case "version": continue; case "states": IEnumerable <fNbt.Tags.NbtTag> enumTag = (IEnumerable <fNbt.Tags.NbtTag>)tag; foreach (var subtag in enumTag) { if ((subtag.Name == "direction") || (subtag.Name == "facing_direction") || (subtag.Name == "open_bit") || (subtag.Name == "multi_face_direction_bits") || (subtag.Name == "stripped_bit") || (subtag.Name == "wall_post_bit") || (subtag.Name == "button_pressed_bit") || (subtag.Name == "rail_direction") || (subtag.Name == "rail_data_bit") || (subtag.Name == "bite_counter") || (subtag.Name == "respawn_anchor_charge") || (subtag.Name == "growth") || (subtag.Name == "powered_bit") || (subtag.Name == "upper_block_bit") || (subtag.Name == "hanging") || (subtag.Name == "coral_direction") || (subtag.Name == "coral_hang_type_bit") || (subtag.Name == "dead_bit") || (subtag.Name == "end_portal_eye_bit") || (subtag.Name == "head_piece_bit") || (subtag.Name == "age")) { int subtagvalue = GetTagValue(subtag); dictParams.Add(subtag.Name, subtagvalue); } if ((subtag.Name == "color") || (subtag.Name == "lever_direction") || (subtag.Name == "wall_block_type") || (subtag.Name == "pillar_axis") || (subtag.Name == "wood_type") || (subtag.Name == "old_log_type") || (subtag.Name == "new_log_type") || (subtag.Name == "old_leaf_type") || (subtag.Name == "new_leaf_type") || (subtag.Name == "sapling_type") || subtag.Name.StartsWith("wall_connection_type_") || (subtag.Name == "chisel_type") || subtag.Name.StartsWith("stone_slab_type") || (subtag.Name == "sponge_type") || (subtag.Name == "double_plant_type") || (subtag.Name == "flower_type") || (subtag.Name == "sand_type") || (subtag.Name == "stone_type") || (subtag.Name == "damage") || (subtag.Name == "dripstone_thickness") || (subtag.Name == "coral_color") || (subtag.Name == "monster_egg_stone_type") || (subtag.Name == "stone_brick_type")) { dictParams.Add(subtag.Name, subtag.StringValue); } } break; case "val": int value = GetTagValue(tag); dictParams.Add(tag.Name, value); break; } } } while (!nbt.IsAtStreamEnd) { nbt.ReadToFollowing(); } return(name, dictParams); }
public void ReadToSiblingTest2() { var reader = new NbtReader(TestFiles.MakeReaderTest()); Assert.IsTrue(reader.ReadToFollowing("inComp1")); // Expect all siblings to be read while we search for a non-existent one Assert.IsFalse(reader.ReadToNextSibling("no such tag")); // Expect to pop out of "third-comp" by now Assert.AreEqual("fourth-list", reader.TagName); }
public object Deserialize(NbtReader nbtReader) { NbtTagInfo tagInfo = nbtReader.ReadTagInfo(); if (tagInfo.Type != NbtTagType.Compound) throw new FormatException("the deserialized stream must contain root tag which is compound."); NbtCompound root = nbtReader.ReadCompound(tagInfo); return DeserializeTag(root, m_type); }
public static Chunk LoadFrom(Stream stream) { using (var wrapper = new StreamWrapper(stream)) using (var reader = new NbtReader(wrapper)) { var rootTag = reader.ReadTag().AsCompound(); return(new Chunk(rootTag)); } }
public void NonSeekableStreamSkip2() { using (var ms = TestFiles.MakeReaderTest()) { using (var nss = new NonSeekableStream(ms)) { var reader = new NbtReader(nss); reader.ReadToFollowing(); reader.Skip(); } } }
public void PrintBigFileUncompressed() { using( FileStream fs = File.OpenRead( "TestFiles/bigtest.nbt" ) ) { NbtReader reader = new NbtReader( fs ); while( reader.ReadToFollowing() ) { Console.Write( "@" + reader.TagStartOffset + " " ); Console.WriteLine( reader.ToString( true ) ); } Assert.AreEqual( reader.RootName, "Level" ); } }
public void NonSeekableStreamSkip1() { byte[] fileBytes = File.ReadAllBytes("TestFiles/bigtest.nbt"); using (var ms = new MemoryStream(fileBytes)) { using (var nss = new NonSeekableStream(ms)) { var reader = new NbtReader(nss); reader.ReadToFollowing(); reader.Skip(); } } }
public void NbtParser_ParseItem_ResultShouldBeDouble() { // Arrange IStringReader reader = new StringReader("1.0"); // Act NbtReader.ReadItem(reader, out INbtArgument result); // Assert Assert.IsTrue(result is NbtDouble); }
public void NbtParser_ParseArrayShouldFail_BecauseInvalidArrayType() { // Arrange IStringReader reader = new StringReader("[M; 1, 2, 3]"); // Act ReadResults readResults = NbtReader.ReadArray(reader, out _); // Assert Assert.IsFalse(readResults.Successful); }
public void NbtParser_ParseListShouldFail_BecauseInvalidListItem() { // Arrange IStringReader reader = new StringReader("[1, 3L]"); // Act ReadResults readResults = NbtReader.ReadArray(reader, out _); // Assert Assert.IsFalse(readResults.Successful); }
public void NbtParser_ParseCompoundShouldFail_BecauseExpectedValue() { // Arrange IStringReader reader = new StringReader("{foo:}"); // Act ReadResults readResults = NbtReader.ReadCompound(reader, out _); // Assert Assert.IsFalse(readResults.Successful); }
public void NbtParser_ResultsShouldBeInteger() { // Arrange IStringReader reader = new StringReader("8"); // Act NbtReader.ReadValue(reader, out INbtArgument result); // Assert Assert.IsTrue(result is NbtInteger); }
public void NbtParser_ParseItem_ResultShouldBeString_Quoted() { // Arrange IStringReader reader = new StringReader("'foo bar baz'"); // Act NbtReader.ReadItem(reader, out INbtArgument result); // Assert Assert.IsTrue(result is NbtString); }
public void NbtParser_ResultsShouldBeCompound() { // Arrange IStringReader reader = new StringReader("{foo: 'bar', baz: 3}"); // Act NbtReader.ReadValue(reader, out INbtArgument result); // Assert Assert.IsTrue(result is NbtCompound); }
public void ReadTagInfo_ArgumentOutOfRangeException() { // Arrange byte[] data = new byte[] { 0xFF, 0x00, 0x0D }; MemoryStream stream = new MemoryStream(data); NbtReader reader = new NbtReader(stream); NbtTagType expectedTagType = (NbtTagType)0x04; int expectedNameLength = 0x0D; // Act NbtTagInfo tagInfo = reader.ReadTagInfo(); }
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(reader.ReadValue(), 1); Assert.AreEqual(reader.ReadValue(), 1); Assert.IsTrue(reader.ReadToFollowing()); // short Assert.AreEqual(reader.ReadValue(), 2); Assert.AreEqual(reader.ReadValue(), 2); Assert.IsTrue(reader.ReadToFollowing()); // int Assert.AreEqual(reader.ReadValue(), 3); Assert.AreEqual(reader.ReadValue(), 3); Assert.IsTrue(reader.ReadToFollowing()); // long Assert.AreEqual(reader.ReadValue(), 4L); Assert.AreEqual(reader.ReadValue(), 4L); Assert.IsTrue(reader.ReadToFollowing()); // float Assert.AreEqual(reader.ReadValue(), 5f); Assert.AreEqual(reader.ReadValue(), 5f); Assert.IsTrue(reader.ReadToFollowing()); // double Assert.AreEqual(reader.ReadValue(), 6d); Assert.AreEqual(reader.ReadValue(), 6d); Assert.IsTrue(reader.ReadToFollowing()); // byteArray CollectionAssert.AreEqual((byte[])reader.ReadValue(), new byte[] { 10, 11, 12 }); CollectionAssert.AreEqual((byte[])reader.ReadValue(), new byte[] { 10, 11, 12 }); Assert.IsTrue(reader.ReadToFollowing()); // intArray CollectionAssert.AreEqual((int[])reader.ReadValue(), new[] { 20, 21, 22 }); CollectionAssert.AreEqual((int[])reader.ReadValue(), new[] { 20, 21, 22 }); Assert.IsTrue(reader.ReadToFollowing()); // string Assert.AreEqual(reader.ReadValue(), "123"); Assert.AreEqual(reader.ReadValue(), "123"); }
public void PrintBigFileUncompressedNoSkip() { using (FileStream fs = File.OpenRead("TestFiles/bigtest.nbt")) { var reader = new NbtReader(fs) { SkipEndTags = false }; Assert.Equal(fs, reader.BaseStream); while (reader.ReadToFollowing()) { Console.Write("@" + reader.TagStartOffset + " "); Console.WriteLine(reader.ToString()); } Assert.Equal("Level", reader.RootName); } }
public void ReadTagInfo_EndTag() { // Arrange byte[] data = new byte[] { 0x00}; MemoryStream stream = new MemoryStream(data); NbtReader reader = new NbtReader(stream); NbtTagType expectedTagType = NbtTagType.End; int expectedNameLength = 0x00; // Act NbtTagInfo tagInfo = reader.ReadTagInfo(); // Assert Assert.AreEqual(expectedTagType, tagInfo.Type); Assert.AreEqual(expectedNameLength, tagInfo.NameLength); }
static void Main(string[] args) { NbtSerializer s = new NbtSerializer(typeof(Player)); using (System.IO.FileStream file = new System.IO.FileStream(@"e:\Users\Stephen\Desktop\mcedit\world\players\skonves.dat",System.IO.FileMode.Open)) { using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(file,System.IO.Compression.CompressionMode.Decompress)) { using (NbtReader reader = new NbtReader(zip)) { object obj = s.Deserialize(reader); } } } //System.IO.Compression.GZipStream }
public void ReadByte_Normal() { // Arrange NbtTagInfo tagInfo = (NbtTagInfo)new byte[] { 0x01, 0x00, 0x04 }; byte[] data = new byte[] { 0x61, 0x73, 0x64, 0x66, 0x7B }; MemoryStream stream = new MemoryStream(data); NbtReader reader = new NbtReader(stream); NbtTagType expectedTagType = NbtTagType.Byte; string expectedName = "asdf"; byte expectedValue = 123; // Act NbtByte result = reader.ReadByte(tagInfo); // Assert Assert.AreEqual(expectedName, result.Name); Assert.AreEqual(expectedTagType, result.Type); Assert.AreEqual(expectedValue, result.Value); }
public void NestedListTest() { NbtCompound root = new NbtCompound( "root" ) { new NbtList( "OuterList" ) { new NbtList { new NbtByte() }, new NbtList { new NbtShort() }, new NbtList { new NbtInt() } } }; byte[] testData = new NbtFile( root ).SaveToBuffer( NbtCompression.None ); using( MemoryStream ms = new MemoryStream( testData ) ) { NbtReader reader = new NbtReader( ms ); while( reader.ReadToFollowing() ) { Console.WriteLine( reader.ToString( true ) ); } } }
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()); }
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>()); }
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)); } }
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>(); Assert.Equal(bytes, new short[] { 100, 20, 3 }); }
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 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>(); Assert.Equal(new byte[] {100, 20, 3}, bytes); // test double values reader.ReadToFollowing("DoubleList"); double[] doubles = reader.ReadListAsArray<double>(); Assert.Equal(doubles, new[] {1d, 2000d, -3000000d}); // test float values reader.ReadToFollowing("FloatList"); float[] floats = reader.ReadListAsArray<float>(); Assert.Equal(new[] {1f, 2000f, -3000000f}, floats); // test int values reader.ReadToFollowing("IntList"); int[] ints = reader.ReadListAsArray<int>(); Assert.Equal(new[] {1, 2000, -3000000}, ints); // test long values reader.ReadToFollowing("LongList"); long[] longs = reader.ReadListAsArray<long>(); Assert.Equal(new[] {1L, 2000L, -3000000L}, longs); // test short values reader.ReadToFollowing("ShortList"); short[] shorts = reader.ReadListAsArray<short>(); Assert.Equal(new short[] {1, 200, -30000}, shorts); // test short values reader.ReadToFollowing("StringList"); string[] strings = reader.ReadListAsArray<string>(); Assert.Equal(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 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 SkipTest() { var reader = new NbtReader(TestFiles.MakeReaderTest()); reader.ReadToFollowing(); // at root reader.ReadToFollowing(); // at first reader.ReadToFollowing(); // at second reader.ReadToFollowing(); // at third-comp reader.ReadToFollowing(); // at inComp1 Assert.AreEqual(reader.TagName, "inComp1"); Assert.AreEqual(reader.Skip(), 2); Assert.AreEqual(reader.TagName, "fourth-list"); Assert.AreEqual(reader.Skip(), 10); Assert.IsFalse(reader.ReadToFollowing()); }
public void ReadAsTagTest1() { // read various lists/compounds as tags var reader = new NbtReader(TestFiles.MakeReaderTest()); reader.ReadToFollowing(); // skip root while (!reader.IsAtStreamEnd) { reader.ReadAsTag(); } Assert.Throws<EndOfStreamException>(() => reader.ReadAsTag()); }
public void SkipTest() { var reader = new NbtReader(TestFiles.MakeReaderTest()); reader.ReadToFollowing(); // at root reader.ReadToFollowing(); // at first reader.ReadToFollowing(); // at second reader.ReadToFollowing(); // at third-comp reader.ReadToFollowing(); // at inComp1 Assert.Equal("inComp1", reader.TagName); Assert.Equal(2, reader.Skip()); Assert.Equal("fourth-list", reader.TagName); Assert.Equal(11, reader.Skip()); Assert.False(reader.ReadToFollowing()); Assert.Equal(0, reader.Skip()); }
public void ReadToDescendantTest() { var reader = new NbtReader(TestFiles.MakeReaderTest()); Assert.True(reader.ReadToDescendant("third-comp")); Assert.Equal("third-comp", reader.TagName); Assert.True(reader.ReadToDescendant("inComp2")); Assert.Equal("inComp2", reader.TagName); Assert.False(reader.ReadToDescendant("derp")); Assert.Equal("inComp3", reader.TagName); reader.ReadToFollowing(); // at fourth-list Assert.True(reader.ReadToDescendant("inList2")); Assert.Equal("inList2", reader.TagName); // Ensure ReadToDescendant returns false when at end-of-stream while (reader.ReadToFollowing()) { } Assert.False(reader.ReadToDescendant("*")); }
public void ReadToSiblingTest() { var reader = new NbtReader(TestFiles.MakeReaderTest()); Assert.True(reader.ReadToFollowing()); Assert.Equal("root", reader.TagName); Assert.True(reader.ReadToFollowing()); Assert.Equal("first", reader.TagName); Assert.True(reader.ReadToNextSibling("third-comp")); Assert.Equal("third-comp", reader.TagName); Assert.True(reader.ReadToNextSibling()); Assert.Equal("fourth-list", reader.TagName); Assert.True(reader.ReadToNextSibling()); Assert.Equal("fifth", reader.TagName); Assert.True(reader.ReadToNextSibling()); Assert.Equal("hugeArray", reader.TagName); Assert.False(reader.ReadToNextSibling()); // Test twice, since we hit different paths through the code Assert.False(reader.ReadToNextSibling()); }
public void PropertiesTest() { var reader = new NbtReader(TestFiles.MakeReaderTest()); Assert.Equal(0, reader.Depth); Assert.Equal(0, reader.TagsRead); Assert.True(reader.ReadToFollowing()); Assert.Equal("root", reader.TagName); Assert.Equal(NbtTagType.Compound, reader.TagType); Assert.Equal(NbtTagType.Unknown, reader.ListType); Assert.False(reader.HasValue); Assert.True(reader.IsCompound); Assert.False(reader.IsList); Assert.False(reader.IsListElement); Assert.False(reader.HasLength); Assert.Equal(0, reader.ListIndex); Assert.Equal(1, reader.Depth); Assert.Equal(null, reader.ParentName); Assert.Equal(NbtTagType.Unknown, reader.ParentTagType); Assert.Equal(0, reader.ParentTagLength); Assert.Equal(0, reader.TagLength); Assert.Equal(1, reader.TagsRead); Assert.True(reader.ReadToFollowing()); Assert.Equal("first", reader.TagName); Assert.Equal(NbtTagType.Int, reader.TagType); Assert.Equal(NbtTagType.Unknown, reader.ListType); Assert.True(reader.HasValue); Assert.False(reader.IsCompound); Assert.False(reader.IsList); Assert.False(reader.IsListElement); Assert.False(reader.HasLength); Assert.Equal(0, reader.ListIndex); Assert.Equal(2, reader.Depth); Assert.Equal("root", reader.ParentName); Assert.Equal(NbtTagType.Compound, reader.ParentTagType); Assert.Equal(0, reader.ParentTagLength); Assert.Equal(0, reader.TagLength); Assert.Equal(2, reader.TagsRead); Assert.True(reader.ReadToFollowing("fourth-list")); Assert.Equal("fourth-list", reader.TagName); Assert.Equal(NbtTagType.List, reader.TagType); Assert.Equal(NbtTagType.List, reader.ListType); Assert.False(reader.HasValue); Assert.False(reader.IsCompound); Assert.True(reader.IsList); Assert.False(reader.IsListElement); Assert.True(reader.HasLength); Assert.Equal(0, reader.ListIndex); Assert.Equal(2, reader.Depth); Assert.Equal("root", reader.ParentName); Assert.Equal(NbtTagType.Compound, reader.ParentTagType); Assert.Equal(0, reader.ParentTagLength); Assert.Equal(3, reader.TagLength); Assert.Equal(8, reader.TagsRead); Assert.True(reader.ReadToFollowing()); // first list element, itself a list Assert.Equal(null, reader.TagName); Assert.Equal(NbtTagType.List, reader.TagType); Assert.Equal(NbtTagType.Compound, reader.ListType); Assert.False(reader.HasValue); Assert.False(reader.IsCompound); Assert.True(reader.IsList); Assert.True(reader.IsListElement); Assert.True(reader.HasLength); Assert.Equal(0, reader.ListIndex); Assert.Equal(3, reader.Depth); Assert.Equal("fourth-list", reader.ParentName); Assert.Equal(NbtTagType.List, reader.ParentTagType); Assert.Equal(3, reader.ParentTagLength); Assert.Equal(1, reader.TagLength); Assert.Equal(9, reader.TagsRead); Assert.True(reader.ReadToFollowing()); // first nested list element, compound Assert.Equal(null, reader.TagName); Assert.Equal(NbtTagType.Compound, reader.TagType); Assert.Equal(NbtTagType.Unknown, reader.ListType); Assert.False(reader.HasValue); Assert.True(reader.IsCompound); Assert.False(reader.IsList); Assert.True(reader.IsListElement); Assert.False(reader.HasLength); Assert.Equal(0, reader.ListIndex); Assert.Equal(4, reader.Depth); Assert.Equal(null, reader.ParentName); Assert.Equal(NbtTagType.List, reader.ParentTagType); Assert.Equal(1, reader.ParentTagLength); Assert.Equal(0, reader.TagLength); Assert.Equal(10, reader.TagsRead); Assert.True(reader.ReadToFollowing("fifth")); Assert.Equal("fifth", reader.TagName); Assert.Equal(NbtTagType.Int, reader.TagType); Assert.Equal(NbtTagType.Unknown, reader.ListType); Assert.True(reader.HasValue); Assert.False(reader.IsCompound); Assert.False(reader.IsList); Assert.False(reader.IsListElement); Assert.False(reader.HasLength); Assert.Equal(0, reader.ListIndex); Assert.Equal(2, reader.Depth); Assert.Equal("root", reader.ParentName); Assert.Equal(NbtTagType.Compound, reader.ParentTagType); Assert.Equal(0, reader.ParentTagLength); Assert.Equal(0, reader.TagLength); Assert.Equal(18, reader.TagsRead); Assert.True(reader.ReadToFollowing()); Assert.Equal("hugeArray", reader.TagName); Assert.Equal(NbtTagType.ByteArray, reader.TagType); Assert.Equal(NbtTagType.Unknown, reader.ListType); Assert.True(reader.HasValue); Assert.False(reader.IsCompound); Assert.False(reader.IsList); Assert.False(reader.IsListElement); Assert.True(reader.HasLength); Assert.Equal(0, reader.ListIndex); Assert.Equal(2, reader.Depth); Assert.Equal("root", reader.ParentName); Assert.Equal(NbtTagType.Compound, reader.ParentTagType); Assert.Equal(0, reader.ParentTagLength); Assert.Equal(1024*1024, reader.TagLength); Assert.Equal(19, reader.TagsRead); }
static NbtFile PartialReadTestInternal(NbtFile comp) { byte[] testData = comp.SaveToBuffer(NbtCompression.None); var reader = new NbtReader(new PartialReadStream(new MemoryStream(testData))); var root = (NbtCompound) reader.ReadAsTag(); return new NbtFile(root); }
public void ListTest() { // write short (1-element) lists of every possible kind using (var ms = new MemoryStream()) { var writer = new NbtWriter(ms, "Test"); writer.BeginList("LotsOfLists", NbtTagType.List, 11); { writer.BeginList(NbtTagType.Byte, 1); writer.WriteByte(1); writer.EndList(); writer.BeginList(NbtTagType.ByteArray, 1); writer.WriteByteArray(new byte[] { 1 }); writer.EndList(); writer.BeginList(NbtTagType.Compound, 1); writer.BeginCompound(); writer.EndCompound(); writer.EndList(); writer.BeginList(NbtTagType.Double, 1); writer.WriteDouble(1); writer.EndList(); writer.BeginList(NbtTagType.Float, 1); writer.WriteFloat(1); writer.EndList(); writer.BeginList(NbtTagType.Int, 1); writer.WriteInt(1); writer.EndList(); writer.BeginList(NbtTagType.IntArray, 1); writer.WriteIntArray(new[] { 1 }); writer.EndList(); writer.BeginList(NbtTagType.List, 1); writer.BeginList(NbtTagType.List, 0); writer.EndList(); writer.EndList(); writer.BeginList(NbtTagType.Long, 1); writer.WriteLong(1); writer.EndList(); writer.BeginList(NbtTagType.Short, 1); writer.WriteShort(1); writer.EndList(); writer.BeginList(NbtTagType.String, 1); writer.WriteString("ponies"); writer.EndList(); } writer.EndList(); writer.EndCompound(); writer.Finish(); ms.Position = 0; var reader = new NbtReader(ms); Assert.DoesNotThrow(() => reader.ReadAsTag()); } }
void TryReadBadFile(byte[] data) { using (MemoryStream ms = new MemoryStream(data)) { NbtReader reader = new NbtReader(ms); try { while (reader.ReadToFollowing()) { } } catch (Exception) { Assert.True(reader.IsInErrorState); throw; } } }
public Map LoadHeader(string path) { using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress)) { NbtReader reader = new NbtReader(gs); reader.ReadToFollowing(); // skip root tag reader.ReadToFollowing(); // skip to first inner tag int width = 0, length = 0, height = 0; do { switch (reader.TagName) { case "X": width = reader.ReadValueAs<short>(); break; case "Y": height = reader.ReadValueAs<short>(); break; case "Z": length = reader.ReadValueAs<short>(); break; } if (width > 0 && length > 0 && height > 0) { return new Map(null, width, length, height, false); } } while (reader.ReadToNextSibling()); } } throw new MapFormatException("Could not locate map dimensions."); }