Exemplo n.º 1
0
		static IEnumerable<List<KeyValuePair<string, object>>> ExtractRegion(Stream stream)
		{
			ArrayBuffer<byte> buffer = new ArrayBuffer<byte>();
			//stream.Seek(8192, SeekOrigin.Begin);
			EndianBinaryReader ebr = new EndianBinaryReader(stream, false);
			NBTSerializer nbs = new NBTSerializer();
			List<long> chunks = new List<long>();

			byte[] buf;
			
			buf = buffer.Buffer(4);
			for(int i=0;i<1024;i++)
			{
				ebr.Read(buf, 0, 4);

				if (buf[0] != 0 || buf[1] != 0 | buf[2] != 0 | buf[3] != 0)
				{
					chunks.Add(((long)(buf[0] << 16 | buf[1] << 8 | buf[2])) << 12);
				}
			}
			

			foreach (long offset in chunks)
			{
				stream.Seek(offset, SeekOrigin.Begin);
				uint length = ebr.ReadUInt32();

				byte compression = ebr.ReadByte();

				if (compression != 2)
				{
					Console.Error.WriteLine("Compression invalid: {0}", compression);
					continue;
				}

				if (length == 0)
				{
					Console.Error.WriteLine("Length is 0");
					continue;
				}

				length--;

				//Console.WriteLine(length);
				buf = buffer.Buffer(length);
				ebr.Read(buf, 0, (int)length);

				using (var ms = new MemoryStream(buf, 0, (int)length))
				using (var iis = new InflaterInputStream(ms))
				{
					yield return nbs.Deserialize(iis);
				}
			}
		}