Ejemplo n.º 1
0
        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()); // longArray
            CollectionAssert.AreEqual(new long[] { 200, 210, 220 }, reader.ReadValueAs <long[]>());
            Assert.IsTrue(reader.ReadToFollowing()); // string
            Assert.AreEqual("123", reader.ReadValueAs <string>());
        }
Ejemplo n.º 2
0
        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.");
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        private static (string, int) GetNbtVal(MemoryStream ms)
        {
            int    value = 0;
            string name  = "";
            var    nbt   = new NbtReader(ms, false);

            nbt.ReadToFollowing();
            if (!nbt.IsCompound)
            {
                throw new Exception("Could not read nbt");
            }

            if (nbt.ReadToDescendant("name"))
            {
                name = nbt.ReadValueAs <string>();
            }
            if (nbt.ReadToNextSibling("val"))
            {
                switch (nbt.TagType)
                {
                case NbtTagType.Int:
                    value = nbt.ReadValueAs <int>();
                    break;

                case NbtTagType.Short:
                    value = nbt.ReadValueAs <short>();
                    break;

                case NbtTagType.Long:
                    value = (int)nbt.ReadValueAs <long>();
                    break;

                default:
                    Console.WriteLine("could not comprehend val");
                    break;
                }
            }

            while (!nbt.IsAtStreamEnd)
            {
                nbt.ReadToFollowing();
            }

            return(name, value);
        }
Ejemplo n.º 5
0
        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 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>());
        }
Ejemplo n.º 7
0
 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.");
 }
Ejemplo n.º 8
0
        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(reader.ReadValueAs<byte>(), 1);
            Assert.IsTrue(reader.ReadToFollowing()); // short
            Assert.AreEqual(reader.ReadValueAs<short>(), 2);
            Assert.IsTrue(reader.ReadToFollowing()); // int
            Assert.AreEqual(reader.ReadValueAs<int>(), 3);
            Assert.IsTrue(reader.ReadToFollowing()); // long
            Assert.AreEqual(reader.ReadValueAs<long>(), 4L);
            Assert.IsTrue(reader.ReadToFollowing()); // float
            Assert.AreEqual(reader.ReadValueAs<float>(), 5f);
            Assert.IsTrue(reader.ReadToFollowing()); // double
            Assert.AreEqual(reader.ReadValueAs<double>(), 6d);
            Assert.IsTrue(reader.ReadToFollowing()); // byteArray
            CollectionAssert.AreEqual(reader.ReadValueAs<byte[]>(),
                                      new byte[] {
                                          10, 11, 12
                                      });
            Assert.IsTrue(reader.ReadToFollowing()); // intArray
            CollectionAssert.AreEqual(reader.ReadValueAs<int[]>(),
                                      new[] {
                                          20, 21, 22
                                      });
            Assert.IsTrue(reader.ReadToFollowing()); // string
            Assert.AreEqual(reader.ReadValueAs<string>(), "123");
        }