Esempio n. 1
0
        /// <summary>
        /// A method to serialize the game repository
        /// </summary>
        /// <param name="filename">The output file name</param>
        /// <param name="savedGameRepository">The saved game repository</param>
        public void SerializeRepository(string filename, SavedGameRepository savedGameRepository)
        {
            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, savedGameRepository);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// A method to deserialize the game repository
        /// </summary>
        /// <param name="filename">The input file name</param>
        /// <returns>The saved game repository</returns>
        public SavedGameRepository DeserializeRepository(string filename)
        {
            SavedGameRepository savedGameRepository = new SavedGameRepository();

            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)
                    {
                        savedGameRepository = (SavedGameRepository)bFormatter.Deserialize(decompressStream);
                    }
                }
            }

            return(savedGameRepository);
        }