Beispiel #1
0
 public void Save(Map mapToSave, string fileName)
 {
     if (mapToSave == null)
     {
         throw new ArgumentNullException("mapToSave");
     }
     if (fileName == null)
     {
         throw new ArgumentNullException("fileName");
     }
     using (FileStream mapStream = File.Create(fileName)) {
         BufferUtil.WriteAll(mapToSave.Blocks, mapStream);
     }
 }
Beispiel #2
0
        public void Save(Map mapToSave, string fileName)
        {
            if (mapToSave == null)
            {
                throw new ArgumentNullException("mapToSave");
            }
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            using (FileStream mapStream = File.Create(fileName)) {
                using (GZipStream gs = new GZipStream(mapStream, CompressionMode.Compress)) {
                    BinaryWriter bs = new BinaryWriter(gs);

                    // Write out the magic number
                    bs.Write(new byte[] { 0xbe, 0xee, 0xef });

                    // Save the map dimensions
                    // XYZ(?)
                    bs.Write((ushort)IPAddress.HostToNetworkOrder((short)mapToSave.Width));
                    bs.Write((ushort)IPAddress.HostToNetworkOrder((short)mapToSave.Height));
                    bs.Write((ushort)IPAddress.HostToNetworkOrder((short)mapToSave.Length));

                    // Save the spawn location
                    bs.Write(IPAddress.HostToNetworkOrder(mapToSave.Spawn.X));
                    bs.Write(IPAddress.HostToNetworkOrder(mapToSave.Spawn.Z));
                    bs.Write(IPAddress.HostToNetworkOrder(mapToSave.Spawn.Y));

                    // Save the spawn orientation
                    bs.Write(mapToSave.Spawn.R);
                    bs.Write(mapToSave.Spawn.L);

                    // Write out the block count (which is totally useless, can't stress that enough.)
                    bs.Write(IPAddress.HostToNetworkOrder(mapToSave.Blocks.Length));

                    // Write out the map data
                    BufferUtil.WriteAll(mapToSave.Blocks, gs);
                }
            }
        }
Beispiel #3
0
        public void Save(Map mapToSave, string fileName)
        {
            if (mapToSave == null)
            {
                throw new ArgumentNullException("mapToSave");
            }
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            using (FileStream mapStream = File.Create(fileName)) {
                using (GZipStream gs = new GZipStream(mapStream, CompressionMode.Compress)) {
                    BinaryWriter bs = new BinaryWriter(gs);

                    // Write the magic number
                    bs.Write((byte)0x01);

                    // Write the spawn location
                    bs.Write(IPAddress.NetworkToHostOrder((short)(mapToSave.Spawn.X / 32)));
                    bs.Write(IPAddress.NetworkToHostOrder((short)(mapToSave.Spawn.Z / 32)));
                    bs.Write(IPAddress.NetworkToHostOrder((short)(mapToSave.Spawn.Y / 32)));

                    //Write the spawn orientation
                    bs.Write(mapToSave.Spawn.R);
                    bs.Write(mapToSave.Spawn.L);

                    // Write the map dimensions
                    bs.Write(IPAddress.NetworkToHostOrder(mapToSave.Width));
                    bs.Write(IPAddress.NetworkToHostOrder(mapToSave.Length));
                    bs.Write(IPAddress.NetworkToHostOrder(mapToSave.Height));

                    // Write the map data
                    BufferUtil.WriteAll(mapToSave.Blocks, gs);
                }
            }
        }
Beispiel #4
0
        public void Save(Map mapToSave, string fileName)
        {
            if (mapToSave == null)
            {
                throw new ArgumentNullException("mapToSave");
            }
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            using (FileStream mapStream = File.Create(fileName)) {
                BinaryWriter writer = new BinaryWriter(mapStream);

                writer.Write(Identifier);
                writer.Write(Revision);

                writer.Write((short)mapToSave.Width);
                writer.Write((short)mapToSave.Height);
                writer.Write((short)mapToSave.Length);

                writer.Write((int)mapToSave.Spawn.X);
                writer.Write((int)mapToSave.Spawn.Z);
                writer.Write((int)mapToSave.Spawn.Y);

                writer.Write(mapToSave.Spawn.R);
                writer.Write(mapToSave.Spawn.L);

                mapToSave.DateModified = DateTime.UtcNow;
                writer.Write((uint)mapToSave.DateModified.ToUnixTime());
                writer.Write((uint)mapToSave.DateCreated.ToUnixTime());

                writer.Write(mapToSave.Guid.ToByteArray());

                writer.Write((byte)1); // layer count

                // skip over index and metacount
                long indexOffset = mapStream.Position;
                writer.Seek(29, SeekOrigin.Current);

                byte[] blocksCache = mapToSave.Blocks;
                int    metaCount, compressedLength;
                long   offset;
                using (DeflateStream ds = new DeflateStream(mapStream, CompressionMode.Compress, true)) {
                    using (BufferedStream bs = new BufferedStream(ds)) {
                        // write metadata
                        metaCount = WriteMetadata(bs, mapToSave);
                        bs.Flush();
                        offset = mapStream.Position;

                        BufferUtil.WriteAll(blocksCache, bs);

                        bs.Flush();
                        compressedLength = (int)(mapStream.Position - offset);
                    }
                }

                // come back to write the index
                writer.BaseStream.Seek(indexOffset, SeekOrigin.Begin);

                writer.Write((byte)0);            // data layer type (Blocks)
                writer.Write(offset);             // offset, in bytes, from start of stream
                writer.Write(compressedLength);   // compressed length, in bytes
                writer.Write(0);                  // general purpose field
                writer.Write(1);                  // element size
                writer.Write(blocksCache.Length); // element count

                writer.Write(metaCount);
            }
        }
Beispiel #5
0
 protected virtual void SaveBlockData([NotNull] Map mapToSave, [NotNull] Stream stream)
 {
     BufferUtil.WriteAll(mapToSave.Blocks, stream);
 }