public void SaveTest()
        {
            // arrange
            NbtDocument expected;
            NbtDocument target;
            string      fileName;

            fileName          = this.GetWorkFile();
            expected          = new NbtDocument(this.CreateComplexData());
            expected.FileName = fileName;

            // act
            try
            {
                expected.Save();
                target = NbtDocument.LoadDocument(fileName);
            }
            finally
            {
                this.DeleteFile(fileName);
            }

            // assert
            NbtAssert.AreEqual(expected, target);
        }
        public ICollection <string> Load(WorldInfo world)
        {
            var playerDataPath = Path.Combine(world.Path, PlayerDataFolder);
            var dataDir        = new DirectoryInfo(playerDataPath);
            var endScreens     = new List <string>();

            if (!dataDir.Exists)
            {
                return(endScreens);
            }

            var playerFiles = dataDir.GetFiles("*.dat");

            foreach (var file in playerFiles)
            {
                var playerId = file.Name.Replace(".dat", string.Empty);

                var nbtDoc           = NbtDocument.LoadDocument(file.FullName);
                var seenCreditsValue = (byte)nbtDoc.Query("seenCredits").GetValue();

                if (seenCreditsValue == 1)
                {
                    endScreens.Add(playerId);
                }
            }

            return(endScreens);
        }
Example #3
0
        public void SaveTest()
        {
            // arrange
            NbtDocument target;

            var fileName = GetWorkFile();
            var expected = new NbtDocument(CreateComplexData())
            {
                FileName = fileName
            };

            // act
            try
            {
                expected.Save();
                target = NbtDocument.LoadDocument(fileName);
            }
            finally
            {
                DeleteFile(fileName);
            }

            // assert
            NbtAssert.Equal(expected, target);
        }
Example #4
0
        public void EmptyListXmlTest()
        {
            // arrange
            NbtDocument reloaded;

            var fileName = GetWorkFile();
            var target   = new NbtDocument
            {
                Format = NbtFormat.Xml
            };

            target.DocumentRoot.Name = "Test";
            target.DocumentRoot.Value.Add("EmptyList", TagType.List, TagType.Compound);

            // act
            try
            {
                target.Save(fileName);
                reloaded = NbtDocument.LoadDocument(fileName);
            }
            finally
            {
                DeleteFile(fileName);
            }

            // assert
            // this test is essentially ensuring that an infinite loop when reloading an XML document is no longer present
            NbtAssert.Equal(target.DocumentRoot, reloaded.DocumentRoot);
        }
Example #5
0
        private void LoadLevelDat()
        {
            var levelDatPath = Path.Combine(_worldPath, "level.dat");

            if (File.Exists(levelDatPath))
            {
                var levelDocument = NbtDocument.LoadDocument(levelDatPath);
                var levelTag      = levelDocument.DocumentRoot.GetCompound("Data");

                Level = new LevelInfo(levelTag);
            }
        }
Example #6
0
        public void LoadDocument_loads_data_from_stream()
        {
            // arrange
            NbtDocument actual;
            var         fileName = ComplexDataFileName;
            var         expected = new NbtDocument(CreateComplexData());

            // act
            using (Stream stream = File.OpenRead(fileName))
            {
                actual = NbtDocument.LoadDocument(stream);
            }

            // assert
            NbtAssert.Equal(expected, actual);
        }
Example #7
0
        public void TestLevelDat()
        {
            var filename = LevelDatFileName;
            var input    = File.OpenRead(filename);

            var document = NbtDocument.LoadDocument(input);

            var tag = document.DocumentRoot;

            Assert.NotNull(tag);

            Assert.Equal(TagType.Compound, tag.GetTag("Data").Type);
            var dataTag = tag.GetCompound("Data");

            var aTag = dataTag.GetTag("RandomSeed");

            Assert.Equal(TagType.Long, aTag.Type);
            var randomSeedTag = aTag as TagLong;

            Assert.Equal(4339722475394340739, randomSeedTag.Value);

            aTag = dataTag.GetTag("LevelName");
            Assert.Equal(TagType.String, aTag.Type);
            var levelNameTag = aTag as TagString;

            Assert.Equal("New World", levelNameTag.Value);

            aTag = dataTag.GetTag("Player");
            Assert.Equal(TagType.Compound, aTag.Type);
            var playerTag = aTag as TagCompound;

            Assert.NotNull(playerTag);

            aTag = playerTag.GetTag("UUIDLeast");
            Assert.Equal(TagType.Long, aTag.Type);
            var uuidLeastTag = aTag as TagLong;

            Assert.Equal(-6361924693575993529, uuidLeastTag.Value);

            aTag = playerTag.GetTag("EnderItems");
            Assert.Equal(TagType.List, aTag.Type);
            var enderItemTag = aTag as TagList;

            Assert.Equal(0, enderItemTag.Count);
        }
        /// <summary>
        /// Retrieve chunk data from minecraft and process it returing a NBTDocument from
        /// our favorite NBT parsing library
        /// </summary>
        /// <param name="startPos"> Chunk position, it will be clamped to chunk coordinates just in case </param>
        /// <param name="dx"> how many chunks to retrieve in X direction </param>
        /// <param name="dz"> how many chunks to retrieve in Z direction </param>
        /// <returns>Parsed data from minecraft as an NBTDocument object</returns>
        public async Task <NbtDocument> GetChunks(Vector2Int startPos, int dx, int dz)
        {
            // Set up expected headers
            _client.DefaultRequestHeaders.Accept.Clear();
            _client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/octet-stream")
                );

            // Clamp coordinates so they are in chunk coordinates
            startPos.X /= 16;
            startPos.Z /= 16;

            // make them possitive
            dx = Math.Clamp(dx, 0, int.MaxValue);
            dz = Math.Clamp(dz, 0, int.MaxValue);

            // Request data
            var ans = await _client.GetAsync($"{_ChunksEndpoint}?x={startPos.X}&z={startPos.Z}&dx={dx}&dz={dz}");

            // Check if request was ok
            if (ans.StatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new HttpRequestException($"Couldn't retrieve data from minecraft, status code: {ans.StatusCode}");
            }

            // parse content as a byte stream
            var content = await ans.Content.ReadAsStreamAsync();

            // Check if this is a valid document
            if (!NbtDocument.IsNbtDocument(content))
            {
                throw new ArgumentException($"Inconsistent data from minecraft. Retrieved data is not a valid NBT document");
            }

            // parse into an actual document object
            var document = NbtDocument.LoadDocument(content);

            // back up our data if it is necessary
            if (_ShouldStoreRawData)
            {
                _rawMcWorkdData = content;
            }

            return(document);
        }
        public void LoadDocument_loads_data_from_stream()
        {
            // arrange
            NbtDocument expected;
            NbtDocument actual;
            string      fileName;

            fileName = this.ComplexDataFileName;
            expected = new NbtDocument(this.CreateComplexData());

            // act
            using (Stream stream = File.OpenRead(fileName))
            {
                actual = NbtDocument.LoadDocument(stream);
            }

            // assert
            NbtAssert.AreEqual(expected, actual);
        }
Example #10
0
        private void LoadPlayerDats()
        {
            var playerDir = Path.Combine(_worldPath, "playerdata");

            if (Directory.Exists(playerDir))
            {
                Players = Directory.EnumerateFiles(playerDir, "*.dat")
                          .Select(path =>
                {
                    var playerDoc = NbtDocument.LoadDocument(path);
                    var uuid      = Path.GetFileNameWithoutExtension(path);

                    return(new Player(uuid, playerDoc.DocumentRoot));
                });
            }
            else
            {
                Players = new List <Player>();
            }
        }
        public void FormatTest()
        {
            // arrange
            NbtDocument source;
            NbtDocument expected;
            NbtDocument target;
            string      fileName1;
            string      fileName2;
            bool        file1IsBinary;
            bool        file2IsXml;

            fileName1 = this.GetWorkFile();
            fileName2 = this.GetWorkFile();
            source    = new NbtDocument(this.CreateComplexData());

            // act
            try
            {
                source.Format = NbtFormat.Binary;
                source.Save(fileName1);
                source.Format = NbtFormat.Xml;
                source.Save(fileName2);

                expected = NbtDocument.LoadDocument(fileName1);
                target   = NbtDocument.LoadDocument(fileName2);

                file1IsBinary = expected.Format == NbtFormat.Binary;
                file2IsXml    = target.Format == NbtFormat.Xml;
            }
            finally
            {
                this.DeleteFile(fileName1);
                this.DeleteFile(fileName2);
            }

            // assert
            Assert.IsTrue(file1IsBinary);
            Assert.IsTrue(file2IsXml);
            NbtAssert.AreEqual(expected, target);
        }
Example #12
0
        public void FormatTest()
        {
            // arrange
            NbtDocument expected;
            NbtDocument target;
            bool        file1IsBinary;
            bool        file2IsXml;

            var fileName1 = GetWorkFile();
            var fileName2 = GetWorkFile();
            var source    = new NbtDocument(CreateComplexData());

            // act
            try
            {
                source.Format = NbtFormat.Binary;
                source.Save(fileName1);
                source.Format = NbtFormat.Xml;
                source.Save(fileName2);

                expected = NbtDocument.LoadDocument(fileName1);
                target   = NbtDocument.LoadDocument(fileName2);

                file1IsBinary = expected.Format == NbtFormat.Binary;
                file2IsXml    = target.Format == NbtFormat.Xml;
            }
            finally
            {
                DeleteFile(fileName1);
                DeleteFile(fileName2);
            }

            // assert
            Assert.True(file1IsBinary);
            Assert.True(file2IsXml);
            NbtAssert.Equal(expected, target);
        }
Example #13
0
        public Column(ChunkFormat format, MemoryStream chunkDataStream, DateTime timestamp)
        {
            Modified = timestamp;

            var stream = GetDecompressedStream(format, chunkDataStream); //TODO: Need to dispose of this manually?

            _nbtDoc = NbtDocument.LoadDocument(stream);

            var level = _nbtDoc.Query <TagCompound>("Level");

            XWorld = level.GetIntValue("xPos") * 16;
            ZWorld = level.GetIntValue("zPos") * 16;

            var terrainPopulated = level.GetByte("TerrainPopulated");

            if (terrainPopulated != null)
            {
                TerrainPopulated = terrainPopulated.Value == 1;
            }
            else
            {
                TerrainPopulated = level.GetLongValue("LastUpdate") > 0;
            }
        }
Example #14
0
 protected TagCompound GetSimpleData()
 {
     return(NbtDocument.LoadDocument(this.SimpleDataFileName).DocumentRoot);
 }
Example #15
0
 protected TagCompound GetComplexData()
 {
     return(NbtDocument.LoadDocument(ComplexDataFileName).DocumentRoot);
 }