using Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.Serialization; using System.Xml; using System.IO; using System.Collections.ObjectModel; using static System.Net.Mime.MediaTypeNames; using System.Runtime.CompilerServices; using System.Dynamic; using System.Xml.Linq; namespace Persistance { public class LoaderXml : IUserDataManager, IMonsterDataManager { static string path = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "../../")); //static string path = Directory.GetCurrentDirectory() + "/../../"; static string fichierUserXML = "users.xml"; static string fichierMonstreXML = "monsters.xml"; // Serialisation / Deserialisation de Monstres void IMonsterDataManager.saveMonsters(List monstres) { Directory.SetCurrentDirectory(Path.Combine(Directory.GetCurrentDirectory())); // Setup le chemin d'accès var serialiserXML = new DataContractSerializer(typeof(List)); XmlWriterSettings xmlSettings = new XmlWriterSettings() { Indent = true }; // Pour avoir le format xml dans le fichier ( indentation etc... ) using (TextWriter tw = File.CreateText(Path.Combine(path, fichierMonstreXML))) { using (XmlWriter writer = XmlWriter.Create(tw, xmlSettings)) { serialiserXML.WriteObject(writer, monstres); } } } public List loadMonsters() { var serialiserXML = new DataContractSerializer(typeof(List)); List? monsters; try { using (Stream s = File.OpenRead(Path.Combine(path, fichierMonstreXML))) { monsters = serialiserXML.ReadObject(s) as List; } } catch (FileNotFoundException e) { throw new FileNotFoundException("The XML file used to load a list of monsters is missing.", e); } if (monsters != null) return monsters; // ça va faire un code smells return new List { }; } // Serialisation / Deserialisation de Users public List loadUsers() { var serialiserXML = new DataContractSerializer(typeof(List)); List? users; using (Stream s = File.OpenRead(Path.Combine(path, fichierUserXML))) { users = serialiserXML.ReadObject(s) as List; } if (users == null) { return null; // Surement que ça va faire un code smells, mais j'ai pas trop cherché pour le moment } return users; } void IUserDataManager.saveUsers(List users)// Serialise correctement juste voir comment l'appelé en fonction de IUserDataManager { Directory.CreateDirectory("Saves"); Directory.SetCurrentDirectory(path); var serialiserXML = new DataContractSerializer(typeof(List)); XmlWriterSettings xmlSettings = new XmlWriterSettings() { Indent = true }; // Pour avoir le format xml dans le fichier ( indentation etc... ) using (TextWriter tw = File.CreateText("users.xml")) { using (XmlWriter writer = XmlWriter.Create(tw, xmlSettings)) { serialiserXML.WriteObject(writer, users); } } } } }