using System.Collections.ObjectModel; using System.Runtime.Serialization; using System.Xml; using Microsoft.VisualBasic; using Microsoft.VisualBasic.FileIO; using Model; using System.Runtime.InteropServices; using System.Diagnostics.CodeAnalysis; namespace StimPersistance { [ExcludeFromCodeCoverage] public class Persistance : IPersistance { private const string gameFileName = "games.xml"; private const string userFileName = "users.xml"; private readonly string fullGamePath; private readonly string fullUserPath; public Persistance(string path) { fullGamePath = Path.Combine(path, gameFileName); fullUserPath = Path.Combine(path, userFileName); } public void SaveGame(List games) { XmlWriterSettings settings = new() { Indent = true }; DataContractSerializer serializer = new(typeof(List)); using (TextWriter tw = File.CreateText(fullGamePath)) using (XmlWriter writer = XmlWriter.Create(tw, settings)) serializer.WriteObject(writer, games); } public void SaveUser(HashSet users) { XmlWriterSettings settings = new() { Indent = true }; DataContractSerializer serializer = new(typeof(HashSet)); using (TextWriter tw = File.CreateText(fullUserPath)) using (XmlWriter writer = XmlWriter.Create(tw, settings)) serializer.WriteObject(writer, users); } public List LoadGame() { if (File.Exists(fullGamePath)) { DataContractSerializer serializer = new(typeof(List)); using (Stream stream = File.OpenRead(fullGamePath)) return serializer.ReadObject(stream) as List ?? new(); } return new(); } public HashSet LoadUser() { if (File.Exists(fullUserPath)) { DataContractSerializer serializer = new(typeof(HashSet)); using (Stream stream = File.OpenRead(fullUserPath)) return serializer.ReadObject(stream) as HashSet ?? new(); } return new(); } } }