Exemplo n.º 1
0
        /// <summary>
        ///		Saves this map into the given file you pass.
        /// </summary>
        /// <param name="url">Url of file to save map to.</param>
        /// <param name="baseNode">The base node that all saving should be started from, this allows you to ignore this node and any nodes higher in the graph like the Root Camera.</param>
        public void Save(object url, SceneNode baseNode)
        {
            // Try and open a stream to the file.
            Stream stream = StreamFactory.RequestStream(url, StreamMode.Truncate);

            if (stream == null)
            {
                return;
            }
            BinaryWriter writer = new BinaryWriter(stream);

            // Write in the magic number (Always FMP).
            writer.Write(new char[] { 'F', 'M', 'P' });

            // Write in the file header.
            byte stateHeader = 0;

            if (_mapProperties.Compress == true)
            {
                stateHeader |= 1;
            }
            if (_mapProperties.Encrypt == true)
            {
                stateHeader |= 2;
            }
            if (_mapProperties.Name != null)
            {
                stateHeader |= 4;
            }
            if (_mapProperties.Author != null)
            {
                stateHeader |= 8;
            }
            if (_mapProperties.Description != null)
            {
                stateHeader |= 16;
            }
            if (_mapProperties.Version != 0)
            {
                stateHeader |= 32;
            }
            writer.Write(stateHeader);
            if (_mapProperties.Name != null)
            {
                writer.Write(_mapProperties.Name);
            }
            if (_mapProperties.Author != null)
            {
                writer.Write(_mapProperties.Author);
            }
            if (_mapProperties.Description != null)
            {
                writer.Write(_mapProperties.Description);
            }
            if (_mapProperties.Version != 0)
            {
                writer.Write(_mapProperties.Version);
            }

            // Create a new memory stream to write main data into.
            MemoryStream memStream       = new MemoryStream();
            BinaryWriter memStreamWriter = new BinaryWriter(memStream);

            // Write in a encryption / compression checker.
            if (_mapProperties.Compress == true || (_mapProperties.Encrypt && _mapProperties.Password != ""))
            {
                memStreamWriter.Write(new char[] { 'C', 'H', 'K' });
            }

            // Save in the tileset pool used to render tilemap segments.
            memStreamWriter.Write((byte)Tileset.TilesetPool.Count);
            foreach (Tileset tileset in Tileset.TilesetPool)
            {
                memStreamWriter.Write(tileset.Url is string);
                if (tileset.Url is string)
                {
                    memStreamWriter.Write((string)tileset.Url);
                }
            }

            // Tell the scene graph to save itself into the stream.
            _sceneGraph.Save(memStreamWriter, baseNode);

            // Close the memory stream.
            byte[] buffer = memStream.ToArray();
            memStreamWriter.Close();
            memStream.Close();

            // Compress the data if required.
            if (_mapProperties.Compress == true)
            {
                buffer = DataMethods.Deflate(buffer);
            }

            // Encrypt the data if required.
            if (_mapProperties.Encrypt == true && _mapProperties.Password != "")
            {
                buffer = DataMethods.Encrypt(buffer, _mapProperties.Password);
            }

            // Write main data into main stream.
            writer.Write(buffer, 0, buffer.Length);

            // Sweep up the dirt.
            writer.Close();
        }