|
|
|
@ -12,9 +12,15 @@ namespace StimPersistance
|
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
|
|
|
public class Persistance : IPersistance
|
|
|
|
|
{
|
|
|
|
|
public Persistance(string chemin)
|
|
|
|
|
private const string gameFileName = "games.xml";
|
|
|
|
|
private const string userFileName = "users.xml";
|
|
|
|
|
private readonly string fullGamePath;
|
|
|
|
|
private readonly string fullUserPath;
|
|
|
|
|
|
|
|
|
|
public Persistance(string path)
|
|
|
|
|
{
|
|
|
|
|
Directory.SetCurrentDirectory(chemin);
|
|
|
|
|
fullGamePath = Path.Combine(path, gameFileName);
|
|
|
|
|
fullUserPath = Path.Combine(path, userFileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SaveGame(List<Game> games)
|
|
|
|
@ -22,7 +28,7 @@ namespace StimPersistance
|
|
|
|
|
XmlWriterSettings settings = new() { Indent = true };
|
|
|
|
|
DataContractSerializer serializer = new(typeof(List<Game>));
|
|
|
|
|
|
|
|
|
|
using (TextWriter tw = File.CreateText("games.xml"))
|
|
|
|
|
using (TextWriter tw = File.CreateText(fullGamePath))
|
|
|
|
|
using (XmlWriter writer = XmlWriter.Create(tw, settings)) serializer.WriteObject(writer, games);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -31,26 +37,26 @@ namespace StimPersistance
|
|
|
|
|
XmlWriterSettings settings = new() { Indent = true };
|
|
|
|
|
DataContractSerializer serializer = new(typeof(HashSet<User>));
|
|
|
|
|
|
|
|
|
|
using (TextWriter tw = File.CreateText("users.xml"))
|
|
|
|
|
using (TextWriter tw = File.CreateText(fullUserPath))
|
|
|
|
|
using (XmlWriter writer = XmlWriter.Create(tw, settings)) serializer.WriteObject(writer, users);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Game> LoadGame()
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists("games.xml"))
|
|
|
|
|
if (File.Exists(fullGamePath))
|
|
|
|
|
{
|
|
|
|
|
DataContractSerializer serializer = new(typeof(List<Game>));
|
|
|
|
|
using (Stream stream = File.OpenRead("games.xml")) return serializer.ReadObject(stream) as List<Game> ?? new();
|
|
|
|
|
using (Stream stream = File.OpenRead(fullGamePath)) return serializer.ReadObject(stream) as List<Game> ?? new();
|
|
|
|
|
}
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HashSet<User> LoadUser()
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists("users.xml"))
|
|
|
|
|
if (File.Exists(fullUserPath))
|
|
|
|
|
{
|
|
|
|
|
DataContractSerializer serializer = new(typeof(HashSet<User>));
|
|
|
|
|
using (Stream stream = File.OpenRead("users.xml")) return serializer.ReadObject(stream) as HashSet<User> ?? new();
|
|
|
|
|
using (Stream stream = File.OpenRead(fullUserPath)) return serializer.ReadObject(stream) as HashSet<User> ?? new();
|
|
|
|
|
}
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|