Exemplo n.º 1
0
        public void IsNbtDocument_should_throw_exception_for_null_stream()
        {
            // arrange

            // act
            NbtDocument.IsNbtDocument((string)null);

            // assert
        }
Exemplo n.º 2
0
        public void IsNbtDocument_should_throw_exception_for_null_stream()
        {
            // arrange

            // act
            Assert.Throws <ArgumentNullException>(() => NbtDocument.IsNbtDocument((string)null));

            // assert
        }
Exemplo n.º 3
0
        public void IsNbtDocument_should_throw_exception_if_file_is_missing()
        {
            // arrange
            var fileName = Guid.NewGuid().ToString();

            // act
            Assert.Throws <FileNotFoundException>(() => NbtDocument.IsNbtDocument(fileName));

            // assert
        }
Exemplo n.º 4
0
        public void IsNbtDocument_should_return_true_for_xml_file()
        {
            // arrange
            var fileName = ComplexXmlDataFileName;

            // act
            var actual = NbtDocument.IsNbtDocument(fileName);

            // assert
            Assert.True(actual);
        }
Exemplo n.º 5
0
        public void IsNbtDocument_should_return_true_for_uncompressed_binary_file()
        {
            // arrange
            var fileName = UncompressedComplexDataFileName;

            // act
            var actual = NbtDocument.IsNbtDocument(fileName);

            // assert
            Assert.True(actual);
        }
Exemplo n.º 6
0
        public void IsNbtDocument_should_return_false_for_unknown_file()
        {
            // arrange
            var fileName = BadFileName;

            // act
            var actual = NbtDocument.IsNbtDocument(fileName);

            // assert
            Assert.False(actual);
        }
Exemplo n.º 7
0
        public void IsNbtDocument_should_throw_exception_if_file_is_missing()
        {
            // arrange
            string fileName;

            fileName = Guid.NewGuid().ToString();

            // act
            NbtDocument.IsNbtDocument(fileName);

            // assert
        }
Exemplo n.º 8
0
        public void IsNbtDocument_should_return_true_for_xml_file()
        {
            // arrange
            string fileName;
            bool   actual;

            fileName = this.ComplexXmlDataFileName;

            // act
            actual = NbtDocument.IsNbtDocument(fileName);

            // assert
            Assert.IsTrue(actual);
        }
Exemplo n.º 9
0
        public void IsNbtDocument_should_return_true_for_uncompressed_binary_file()
        {
            // arrange
            string fileName;
            bool   actual;

            fileName = this.UncompressedComplexDataFileName;

            // act
            actual = NbtDocument.IsNbtDocument(fileName);

            // assert
            Assert.IsTrue(actual);
        }
Exemplo n.º 10
0
        public void IsNbtDocument_should_return_false_for_unknown_file()
        {
            // arrange
            bool   actual;
            string fileName;

            fileName = this.BadFileName;

            // act
            actual = NbtDocument.IsNbtDocument(fileName);

            // assert
            Assert.IsFalse(actual);
        }
Exemplo n.º 11
0
        public void IsNbtDocument_should_return_true_for_gzip_binary_stream()
        {
            // arrange
            bool actual;
            var  fileName = ComplexDataFileName;

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

            // assert
            Assert.True(actual);
        }
Exemplo n.º 12
0
        /// <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);
        }