Ejemplo n.º 1
0
        /// <summary>
        /// A method to serialize the game repository
        /// </summary>
        /// <param name="filename">A string representation of the output file name</param>
        /// <param name="savedGameRepository">The saved game repository</param>
        public void SerializeRepository(string filename, SavedUserRepository savedUserRepository)
        {
            string filePath = ObtainPathToDatFile(filename);

            using (Stream stream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                using (DeflateStream compressedStream = new DeflateStream(stream, CompressionMode.Compress))
                {
                    BinaryFormatter bFormatter = new BinaryFormatter();
                    bFormatter.Serialize(compressedStream, savedUserRepository);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// A method to deserialize the game repository
        /// </summary>
        /// <param name="filename">A string representation of the input file name</param>
        /// <returns>A SavedGameRepository object</returns>
        public SavedUserRepository DeserializeRepository(string filename)
        {
            SavedUserRepository savedUserRepository = new SavedUserRepository();

            string filePath = ObtainPathToDatFile(filename);

            using (Stream stream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                using (DeflateStream decompressStream = new DeflateStream(stream, CompressionMode.Decompress))
                {
                    BinaryFormatter bFormatter = new BinaryFormatter();
                    if (stream.Length > 0)
                    {
                        savedUserRepository = (SavedUserRepository)bFormatter.Deserialize(decompressStream);
                    }
                }
            }

            return(savedUserRepository);
        }