Exemple #1
0
        /// <summary> Saves given map at the given location. </summary>
        /// <returns> true if saving succeeded. </returns>
        public bool Save([NotNull] Map map, [NotNull] string path)
        {
            if (map == null)
            {
                throw new ArgumentNullException("map");
            }
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            using (FileStream mapStream = File.Create(path)) {
                BinaryWriter writer = new BinaryWriter(mapStream);

                writer.Write(FormatID);

                // write out map dimensions
                writer.Write(map.Width);
                writer.Write(map.Height);
                writer.Write(map.Length);

                // write out the spawn
                Position spawn = map.Spawn;
                writer.Write((int)spawn.X);
                writer.Write((int)spawn.Z);
                writer.Write((int)spawn.Y);
                writer.Write(spawn.R);
                writer.Write(spawn.L);

                // write out creation and modification time
                long createdTime = map.DateCreated.ToUnixTime();
                writer.Write(createdTime);
                map.DateModified = DateTime.UtcNow;
                long modifiedTime = map.DateModified.ToUnixTime();
                writer.Write(modifiedTime);

                // write out metadata
                lock (map.Metadata.SyncRoot) {
                    lock (map.Zones.SyncRoot) {
                        int metaCount = map.Metadata.Count;
                        metaCount += map.Zones.Count;

                        // TODO: count rank spawns

                        writer.Write(metaCount);

                        // write out metadata
                        foreach (var entry in map.Metadata)
                        {
                            WriteString(writer, entry.Group);
                            WriteString(writer, entry.Key);
                            WriteString(writer, entry.Value);
                        }

                        // write out zones
                        foreach (var zone in map.Zones)
                        {
                            WriteString(writer, ZoneMetaGroupName);
                            WriteString(writer, zone.Name);
                            WriteString(writer, zone.Serialize().ToString(SaveOptions.DisableFormatting));
                        }

                        // TODO: write out rank spawns
                    }
                }

                // write out the layer(s)
                writer.Write(1);   // layer count
                WriteString(writer, BlockLayerName);
                writer.Write(map.Volume);
                map.GetCompressedCopy(mapStream, true);

                return(true);
            }
        }