You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
247 lines
8.7 KiB
247 lines
8.7 KiB
using Model.Stub;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.Serialization;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml;
|
|
using Model.Classes;
|
|
using Model.Managers;
|
|
|
|
namespace Model.Serializer
|
|
{
|
|
public class XML_Serializer : IDataManager
|
|
{
|
|
public string Chemin { get; set; }
|
|
public XML_Serializer()
|
|
{
|
|
StubManager stubManager = new StubManager();
|
|
Chemin = Directory.GetCurrentDirectory();
|
|
InitialiserFichiers(stubManager);
|
|
}
|
|
public XML_Serializer(string path)
|
|
{
|
|
Chemin= path;
|
|
StubManager stubManager = new StubManager();
|
|
InitialiserFichiers(stubManager);
|
|
}
|
|
public void InitialiserFichiers(StubManager stubManager)
|
|
{
|
|
if (!File.Exists(Path.Combine(Chemin, "./personnage.xml")))
|
|
{
|
|
var personnages = stubManager.GetPersonnages();
|
|
if(personnages!=null)
|
|
SetPersonnage(personnages.ToList());
|
|
}
|
|
if (!File.Exists(Path.Combine(Chemin, "./bateau.xml")))
|
|
{
|
|
var bateaux = stubManager.GetBateaux();
|
|
if(bateaux !=null)
|
|
SetBateau(bateaux.ToList());
|
|
}
|
|
if (!File.Exists(Path.Combine(Chemin, "./fruitdudemon.xml")))
|
|
{
|
|
var fruits = stubManager.GetFruits();
|
|
if(fruits!=null)
|
|
SetFDD(fruits.ToList());
|
|
}
|
|
if (!File.Exists(Path.Combine(Chemin, "./bestiaire.xml")))
|
|
{
|
|
var bestiaires= stubManager.GetBestiaires();
|
|
if(bestiaires!=null)
|
|
SetBestiaire(bestiaires.ToList());
|
|
}
|
|
if (!File.Exists(Path.Combine(Chemin, "./equipage.xml")))
|
|
{
|
|
var equipages= stubManager.GetEquipages();
|
|
if(equipages!=null)
|
|
SetEquipage(equipages.ToList());
|
|
}
|
|
if (!File.Exists(Path.Combine(Chemin, "./ile.xml")))
|
|
{
|
|
var iles = stubManager.GetIles();
|
|
if(iles!=null)
|
|
SetIle(iles.ToList());
|
|
}
|
|
}
|
|
|
|
public void SetPersonnage(List<Personnage> listePerso)
|
|
{
|
|
var serializer = new DataContractSerializer(typeof(List<Personnage>));
|
|
string xmlFile = "personnage.xml";
|
|
Directory.SetCurrentDirectory(Path.Combine(Chemin, "./"));
|
|
XmlWriterSettings settings = new XmlWriterSettings() { Indent = true };
|
|
using (TextWriter tw = File.CreateText(xmlFile))
|
|
{
|
|
|
|
using (XmlWriter writer = XmlWriter.Create(tw, settings))
|
|
{
|
|
|
|
serializer.WriteObject(writer, listePerso);
|
|
|
|
}
|
|
}
|
|
}
|
|
public void SetFDD(List<FruitDuDemon> listeFDD)
|
|
{
|
|
var serializer = new DataContractSerializer(typeof(List<FruitDuDemon>));
|
|
string xmlFile = "fruitdudemon.xml";
|
|
Directory.SetCurrentDirectory(Path.Combine(Chemin, "./"));
|
|
XmlWriterSettings settings = new XmlWriterSettings() { Indent = true };
|
|
using (TextWriter tw = File.CreateText(xmlFile))
|
|
{
|
|
|
|
using (XmlWriter writer = XmlWriter.Create(tw, settings))
|
|
{
|
|
|
|
serializer.WriteObject(writer, listeFDD);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetBestiaire(List<Bestiaire> listeBest)
|
|
{
|
|
var serializer = new DataContractSerializer(typeof(List<Bestiaire>));
|
|
string xmlFile = "bestiaire.xml";
|
|
Directory.SetCurrentDirectory(Path.Combine(Chemin, "./"));
|
|
XmlWriterSettings settings = new XmlWriterSettings() { Indent = true };
|
|
using (TextWriter tw = File.CreateText(xmlFile))
|
|
{
|
|
|
|
using (XmlWriter writer = XmlWriter.Create(tw, settings))
|
|
{
|
|
|
|
serializer.WriteObject(writer, listeBest);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetEquipage(List<Equipage> listeEquip)
|
|
{
|
|
var serializer = new DataContractSerializer(typeof(List<Equipage>));
|
|
string xmlFile = "equipage.xml";
|
|
Directory.SetCurrentDirectory(Path.Combine(Chemin, "./"));
|
|
XmlWriterSettings settings = new XmlWriterSettings() { Indent = true };
|
|
using (TextWriter tw = File.CreateText(xmlFile))
|
|
{
|
|
|
|
using (XmlWriter writer = XmlWriter.Create(tw, settings))
|
|
{
|
|
|
|
serializer.WriteObject(writer, listeEquip);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetIle(List<Ile> listeIle)
|
|
{
|
|
var serializer = new DataContractSerializer(typeof(List<Ile>));
|
|
string xmlFile = "ile.xml";
|
|
Directory.SetCurrentDirectory(Path.Combine(Chemin, "./"));
|
|
XmlWriterSettings settings = new XmlWriterSettings() { Indent = true };
|
|
using (TextWriter tw = File.CreateText(xmlFile))
|
|
{
|
|
|
|
using (XmlWriter writer = XmlWriter.Create(tw, settings))
|
|
{
|
|
|
|
serializer.WriteObject(writer, listeIle);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetBateau(List<Bateau> listeBateaux)
|
|
{
|
|
var serializer = new DataContractSerializer(typeof(List<Bateau>));
|
|
string xmlFile = "bateau.xml";
|
|
Directory.SetCurrentDirectory(Path.Combine(Chemin, "./"));
|
|
XmlWriterSettings settings = new XmlWriterSettings() { Indent = true };
|
|
using (TextWriter tw = File.CreateText(xmlFile))
|
|
{
|
|
|
|
using (XmlWriter writer = XmlWriter.Create(tw, settings))
|
|
{
|
|
|
|
serializer.WriteObject(writer, listeBateaux);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public IEnumerable<Bateau>? GetBateaux()
|
|
{
|
|
|
|
var serializer = new DataContractSerializer(typeof(List<Bateau>));
|
|
string xmlFile = "bateau.xml";
|
|
Directory.SetCurrentDirectory(Path.Combine(Chemin, "./"));
|
|
using (Stream s = File.OpenRead(xmlFile))
|
|
{
|
|
return serializer.ReadObject(s) as List<Bateau>;
|
|
}
|
|
}
|
|
public IEnumerable<Bestiaire>? GetBestiaires()
|
|
{
|
|
|
|
var serializer = new DataContractSerializer(typeof(List<Bestiaire>));
|
|
string xmlFile = "bestiaire.xml";
|
|
Directory.SetCurrentDirectory(Path.Combine(Chemin, "./"));
|
|
|
|
using (Stream s = File.OpenRead(xmlFile))
|
|
{
|
|
return serializer.ReadObject(s) as List<Bestiaire>;
|
|
}
|
|
|
|
}
|
|
public IEnumerable<Equipage>? GetEquipages()
|
|
{
|
|
var serializer = new DataContractSerializer(typeof(List<Equipage>));
|
|
string xmlFile = "equipage.xml";
|
|
Directory.SetCurrentDirectory(Path.Combine(Chemin, "./"));
|
|
|
|
using (Stream s = File.OpenRead(xmlFile))
|
|
{
|
|
return serializer.ReadObject(s) as List<Equipage>;
|
|
}
|
|
|
|
}
|
|
public IEnumerable<FruitDuDemon>? GetFruits()
|
|
{
|
|
var serializer = new DataContractSerializer(typeof(List<FruitDuDemon>));
|
|
string xmlFile = "fruitdudemon.xml";
|
|
Directory.SetCurrentDirectory(Path.Combine(Chemin, "./"));
|
|
|
|
using (Stream s = File.OpenRead(xmlFile))
|
|
{
|
|
return serializer.ReadObject(s) as List<FruitDuDemon>;
|
|
}
|
|
}
|
|
public IEnumerable<Ile>? GetIles()
|
|
{
|
|
var serializer = new DataContractSerializer(typeof(List<Ile>));
|
|
string xmlFile = "ile.xml";
|
|
Directory.SetCurrentDirectory(Path.Combine(Chemin, "./"));
|
|
|
|
using (Stream s = File.OpenRead(xmlFile))
|
|
{
|
|
return serializer.ReadObject(s) as List<Ile>;
|
|
}
|
|
}
|
|
public IEnumerable<Personnage>? GetPersonnages()
|
|
{
|
|
var serializer = new DataContractSerializer(typeof(List<Personnage>));
|
|
string xmlFile = "personnage.xml";
|
|
Directory.SetCurrentDirectory(Path.Combine(Chemin, "./"));
|
|
|
|
using (Stream s = File.OpenRead(xmlFile))
|
|
{
|
|
return serializer.ReadObject(s) as List<Personnage>;
|
|
}
|
|
}
|
|
}
|
|
}
|