Exemple #1
0
        public Map Load(string fileName)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            using (FileStream mapStream = File.OpenRead(fileName)) {
                using (GZipStream gs = new GZipStream(mapStream, CompressionMode.Decompress)) {
                    Map map = LoadHeaderInternal(gs);

                    if (!map.ValidateHeader())
                    {
                        throw new MapFormatException("MapMCSharp: One or more of the map dimensions are invalid.");
                    }

                    // Read in the map data
                    map.Blocks = new byte[map.Volume];
                    MapUtility.ReadAll(gs, map.Blocks);

                    map.ConvertBlockTypes(Mapping);

                    return(map);
                }
            }
        }
Exemple #2
0
        public bool Save([NotNull] Map mapToSave, [NotNull] 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(IPAddress.HostToNetworkOrder(1050));
                    bs.Write((byte)0);
                    bs.Write((byte)0);

                    // 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
                    MapUtility.WriteAll(mapToSave.Blocks, bs);

                    bs.Close();
                    return(true);
                }
            }
        }
Exemple #3
0
        public bool Save([NotNull] Map mapToSave, [NotNull] 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((ushort)0x752);

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

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

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

                    // Write the VistPermission and BuildPermission bytes
                    bs.Write((byte)0);
                    bs.Write((byte)0);

                    // Write the map data
                    MapUtility.WriteAll(mapToSave.Blocks, bs);

                    bs.Close();
                }
                return(true);
            }
        }
Exemple #4
0
        public bool Save([NotNull] Map mapToSave, [NotNull] 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
                    MapUtility.WriteAll(mapToSave.Blocks, bs);
                    return(true);
                }
            }
        }
Exemple #5
0
        public bool Save([NotNull] Map mapToSave, [NotNull] 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
                    MapUtility.WriteAll(mapToSave.Blocks, bs);
                }
                return(true);
            }
        }
Exemple #6
0
        public bool Save([NotNull] Map mapToSave, [NotNull] 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.ToUnixTimeLegacy());
                writer.Write((uint)mapToSave.DateCreated.ToUnixTimeLegacy());

                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);
                        offset    = mapStream.Position;                      // inaccurate, but who cares
                        bs.Flush();
                        MapUtility.WriteAll(blocksCache, bs);
                        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);
                return(true);
            }
        }