79 lines
2.4 KiB
C#
79 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace RecrownedAthenaeum.Persistence
|
|
{
|
|
public class PreferencesManager
|
|
{
|
|
private readonly Dictionary<Type, object> preferenceList;
|
|
string savePath;
|
|
XmlSerializer xmlSerializer;
|
|
|
|
public PreferencesManager(string savePath, params object[] preferences)
|
|
{
|
|
this.savePath = savePath;
|
|
Directory.CreateDirectory(savePath);
|
|
preferenceList = new Dictionary<Type, object>();
|
|
Debug.WriteLine("Preference manager setting up...");
|
|
Type[] preferenceTypes = new Type[preferences.Length];
|
|
for (int prefID = 0; prefID < preferences.Length; prefID++)
|
|
{
|
|
preferenceList.Add(preferences[prefID].GetType(), preferences[prefID]);
|
|
preferenceTypes[prefID] = preferences[prefID].GetType();
|
|
Debug.WriteLine(preferences[prefID] + " added to list of preferences");
|
|
}
|
|
|
|
xmlSerializer = new XmlSerializer(typeof(object), preferenceTypes);
|
|
}
|
|
|
|
public T GetPreferences<T>()
|
|
{
|
|
return (T)preferenceList[typeof(T)];
|
|
}
|
|
|
|
public void Load()
|
|
{
|
|
List<Type> keys = new List<Type>(preferenceList.Keys);
|
|
|
|
foreach (Type key in keys)
|
|
{
|
|
if (!LoadSpecific(key))
|
|
{
|
|
SaveSpecific(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
foreach (KeyValuePair<Type, object> prefs in preferenceList)
|
|
{
|
|
SaveSpecific(prefs.Key);
|
|
}
|
|
}
|
|
|
|
private bool LoadSpecific(Type preference)
|
|
{
|
|
string path = savePath + "/" + preference.Name + ".xml";
|
|
if (File.Exists(path))
|
|
{
|
|
Stream stream = new FileStream(path, FileMode.Open);
|
|
preferenceList[preference] = xmlSerializer.Deserialize(stream);
|
|
stream.Close();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void SaveSpecific(Type preference)
|
|
{
|
|
Stream stream = new FileStream(savePath + "/" + preference.Name + ".xml", FileMode.Create);
|
|
xmlSerializer.Serialize(stream, preferenceList[preference]);
|
|
stream.Close();
|
|
}
|
|
}
|
|
}
|