Пример #1
0
        /// <summary>
        /// Deserializes the given stream into a deserializable scene.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public Scene2DLayeredSource DeSerialize(Stream stream)
        {
            byte[] intBytes = new byte[4];
            stream.Read(intBytes, 0, 4);
            int sceneIndexIndex = BitConverter.ToInt32(intBytes, 0);

            // move to the index position.
            stream.Seek(sceneIndexIndex, SeekOrigin.Begin);
            RuntimeTypeModel typeModel = TypeModel.Create();

            typeModel.Add(typeof(Scene2DLayeredIndex), true); // the tile metadata.
            Scene2DLayeredIndex index = typeModel.Deserialize(stream, null, typeof(Scene2DLayeredIndex)) as Scene2DLayeredIndex;

            return(new Scene2DLayeredSource(stream, index));
        }
        /// <summary>
        /// Serializes the given scene to the given stream.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="scenes"></param>
        /// <param name="zoomCutoffs"></param>
        /// <param name="compress"></param>
        public void Serialize(Stream stream, Scene2DSimple nonSimplifiedScene,
                              Scene2DSimple[] scenes, List <float> zoomCutoffs, bool compress)
        {
            // first move to store the position of the index.
            stream.Write(new byte[4], 0, 4);

            // serialize all the scenes.
            List <long>  sceneIndexes = new List <long>();
            MemoryStream sceneStream;

            for (int idx = 0; idx < scenes.Length; idx++)
            {
                if (scenes[idx] != null)
                {
                    sceneStream = new MemoryStream();
                    scenes[idx].SerializeStyled(sceneStream, compress);

                    sceneIndexes.Add(stream.Position);
                    sceneStream.WriteTo(stream);
                    sceneStream.Dispose();
                }
            }
            sceneIndexes.Add(stream.Position);

            // serialize the non-simplified scene.
            sceneStream = new MemoryStream();
            nonSimplifiedScene.SerializeStyled(sceneStream, compress);
            sceneStream.WriteTo(stream);
            sceneStream.Dispose();

            // serialize the index.
            Scene2DLayeredIndex sceneIndex = new Scene2DLayeredIndex();

            sceneIndex.SceneCutoffs = zoomCutoffs.ToArray();
            sceneIndex.SceneIndexes = sceneIndexes.ToArray();
            int sceneIndexIndex = (int)stream.Position;

            RuntimeTypeModel typeModel = TypeModel.Create();

            typeModel.Add(typeof(Scene2DLayeredIndex), true); // the tile metadata.
            typeModel.Serialize(stream, sceneIndex);

            // write the position.
            stream.Seek(0, SeekOrigin.Begin);
            stream.Write(BitConverter.GetBytes(sceneIndexIndex), 0, 4);
            stream.Flush();
        }
        /// <summary>
        /// Serializes the given scene to the given stream.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="scenes"></param>
        /// <param name="zoomCutoffs"></param>
        /// <param name="compress"></param>
        public void Serialize(Stream stream, Scene2DSimple nonSimplifiedScene,
            Scene2DSimple[] scenes, List<float> zoomCutoffs, bool compress)
        {
            // first move to store the position of the index.
            stream.Write(new byte[4], 0, 4);

            // serialize all the scenes.
            List<long> sceneIndexes = new List<long>();
            MemoryStream sceneStream;
            for(int idx = 0; idx < scenes.Length; idx++)
            {
                if(scenes[idx] != null)
                {
                    sceneStream = new MemoryStream();
                    scenes[idx].SerializeStyled(sceneStream, compress);

                    sceneIndexes.Add(stream.Position);
                    sceneStream.WriteTo(stream);
                    sceneStream.Dispose();
                }
            }
            sceneIndexes.Add(stream.Position);

            // serialize the non-simplified scene.
            sceneStream = new MemoryStream();
            nonSimplifiedScene.SerializeStyled(sceneStream, compress);
            sceneStream.WriteTo(stream);
            sceneStream.Dispose();

            // serialize the index.
            Scene2DLayeredIndex sceneIndex = new Scene2DLayeredIndex();
            sceneIndex.SceneCutoffs = zoomCutoffs.ToArray();
            sceneIndex.SceneIndexes = sceneIndexes.ToArray();
            int sceneIndexIndex = (int)stream.Position;

            RuntimeTypeModel typeModel = TypeModel.Create();
            typeModel.Add(typeof(Scene2DLayeredIndex), true); // the tile metadata.
            typeModel.Serialize(stream, sceneIndex);

            // write the position.
            stream.Seek(0, SeekOrigin.Begin);
            stream.Write(BitConverter.GetBytes(sceneIndexIndex), 0, 4);
            stream.Flush();
        }