Exemplo n.º 1
0
 public RegionRenderer(McRegion region)
 {
     // TODO: Complete member initialization
     Region = region;
 }
Exemplo n.º 2
0
        public static McRegion LoadRegion(string regionPath)
        {
            McRegion region = new McRegion(regionPath);

            using (BinaryReader file = new BinaryReader(File.Open(regionPath, FileMode.Open)))
            {
                byte[] chunkLocations = new byte[4096];
                byte[] chunkTimestamps = new byte[4096];

                file.Read(chunkLocations, 0, 4095);
                file.Read(chunkTimestamps, 4096, 8191);

                for (int headerPosition = 0; headerPosition < 4096; headerPosition += 4) // 32*32 chunks
                {
                    //Get info about the chunk in the file
                    byte[] chunkOffsetInfo = new byte[4];
                    chunkOffsetInfo[0] = 0;
                    Array.Copy(chunkLocations, headerPosition, chunkOffsetInfo, 1, 3);

                    long chunkOffset = BitConverter.ToInt32(BitConverter.IsLittleEndian ? chunkOffsetInfo.Reverse().ToArray() : chunkOffsetInfo, 0) * 4096;
                    byte chunkSectors = chunkLocations[headerPosition + 3];

                    byte[] chunkTimestampInfo = new byte[4];
                    Array.Copy(chunkTimestamps, headerPosition, chunkTimestampInfo, 0, 4);

                    int chunkTimestamp = BitConverter.ToInt32(BitConverter.IsLittleEndian ? chunkTimestampInfo.Reverse().ToArray() : chunkTimestampInfo, 0);

                    if (chunkOffset < 2 || chunkSectors < 1) continue; //Non existing or invalid chunk

                    //Read the chunk info
                    file.BaseStream.Seek(chunkOffset, SeekOrigin.Begin);

                    byte[] chunkLengthInfo = new byte[4];
                    file.Read(chunkLengthInfo, 0, 4);

                    uint chunkLength = BitConverter.ToUInt32(BitConverter.IsLittleEndian ? chunkLengthInfo.Reverse().ToArray() : chunkLengthInfo, 0);

                    ChunkCompressionTypes chunkCompressionType = (ChunkCompressionTypes)file.ReadByte();

                    byte[] compressedChunkData = new byte[chunkLength - 1];
                    file.Read(compressedChunkData, 0, (int)chunkLength - 1);

                    MemoryStream uncompressedChunkData = new MemoryStream();

                    switch (chunkCompressionType)
                    {
                        case ChunkCompressionTypes.GZip:
                            new GZipStream(new MemoryStream(compressedChunkData), CompressionMode.Decompress).CopyTo(uncompressedChunkData);
                            break;

                        case ChunkCompressionTypes.ZLib:
                            new ZlibStream(new MemoryStream(compressedChunkData), CompressionMode.Decompress).CopyTo(uncompressedChunkData);
                            break;

                        default:
                            throw new Exception("Unrecognized compression type");
                            break;
                    }

                    region.Chunks.Add(new McChunk(uncompressedChunkData, chunkTimestamp));
                }

                file.Close();
            }

            return region;
        }
Exemplo n.º 3
0
        public static McRegion LoadRegion(string regionPath)
        {
            McRegion region = new McRegion(regionPath);

            using (BinaryReader file = new BinaryReader(File.Open(regionPath, FileMode.Open)))
            {
                byte[] chunkLocations  = new byte[4096];
                byte[] chunkTimestamps = new byte[4096];

                file.Read(chunkLocations, 0, 4095);
                file.Read(chunkTimestamps, 4096, 8191);

                for (int headerPosition = 0; headerPosition < 4096; headerPosition += 4) // 32*32 chunks
                {
                    //Get info about the chunk in the file
                    byte[] chunkOffsetInfo = new byte[4];
                    chunkOffsetInfo[0] = 0;
                    Array.Copy(chunkLocations, headerPosition, chunkOffsetInfo, 1, 3);

                    long chunkOffset  = BitConverter.ToInt32(BitConverter.IsLittleEndian ? chunkOffsetInfo.Reverse().ToArray() : chunkOffsetInfo, 0) * 4096;
                    byte chunkSectors = chunkLocations[headerPosition + 3];

                    byte[] chunkTimestampInfo = new byte[4];
                    Array.Copy(chunkTimestamps, headerPosition, chunkTimestampInfo, 0, 4);

                    int chunkTimestamp = BitConverter.ToInt32(BitConverter.IsLittleEndian ? chunkTimestampInfo.Reverse().ToArray() : chunkTimestampInfo, 0);

                    if (chunkOffset < 2 || chunkSectors < 1)
                    {
                        continue;                                      //Non existing or invalid chunk
                    }
                    //Read the chunk info
                    file.BaseStream.Seek(chunkOffset, SeekOrigin.Begin);

                    byte[] chunkLengthInfo = new byte[4];
                    file.Read(chunkLengthInfo, 0, 4);

                    uint chunkLength = BitConverter.ToUInt32(BitConverter.IsLittleEndian ? chunkLengthInfo.Reverse().ToArray() : chunkLengthInfo, 0);

                    ChunkCompressionTypes chunkCompressionType = (ChunkCompressionTypes)file.ReadByte();

                    byte[] compressedChunkData = new byte[chunkLength - 1];
                    file.Read(compressedChunkData, 0, (int)chunkLength - 1);

                    MemoryStream uncompressedChunkData = new MemoryStream();

                    switch (chunkCompressionType)
                    {
                    case ChunkCompressionTypes.GZip:
                        new GZipStream(new MemoryStream(compressedChunkData), CompressionMode.Decompress).CopyTo(uncompressedChunkData);
                        break;

                    case ChunkCompressionTypes.ZLib:
                        new ZlibStream(new MemoryStream(compressedChunkData), CompressionMode.Decompress).CopyTo(uncompressedChunkData);
                        break;

                    default:
                        throw new Exception("Unrecognized compression type");
                        break;
                    }

                    region.Chunks.Add(new McChunk(uncompressedChunkData, chunkTimestamp));
                }

                file.Close();
            }

            return(region);
        }
Exemplo n.º 4
0
        public static World LoadWorld(string worldPath)
        {
            World world = new World(worldPath);

            Parallel.ForEach(Directory.GetFiles(worldPath + "\\region", "*.mca"), regionFilePath => world.Regions.Add(McRegion.LoadRegion(regionFilePath)));
            return(world);
        }