added basic setting persistence system
This commit is contained in:
@@ -4,9 +4,9 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RhythmBullet.Zer01HD.Utilities
|
||||
namespace RhythmBullet.Zer01HD.Utilities.Persistence
|
||||
{
|
||||
class Preferences
|
||||
public interface IPreferences
|
||||
{
|
||||
}
|
||||
}
|
@@ -0,0 +1,53 @@
|
||||
using RhythmBullet.Zer01HD.Utilities.Persistence;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace RhythmBullet.Zer01HD.Utilities
|
||||
{
|
||||
public class PreferencesManager
|
||||
{
|
||||
private readonly Dictionary<Type, IPreferences> preferenceList;
|
||||
public string SavePath;
|
||||
XmlSerializer xmlSerializer;
|
||||
|
||||
public PreferencesManager(string savePath, params IPreferences[] preferences)
|
||||
{
|
||||
this.SavePath = savePath;
|
||||
preferenceList = new Dictionary<Type, IPreferences>();
|
||||
foreach (IPreferences prefs in preferences)
|
||||
{
|
||||
preferenceList.Add(prefs.GetType(), prefs);
|
||||
}
|
||||
}
|
||||
|
||||
public T GetPreferences<T>() where T : IPreferences
|
||||
{
|
||||
return (T) preferenceList[typeof(T)];
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
foreach (KeyValuePair<Type, IPreferences> prefs in preferenceList)
|
||||
{
|
||||
Stream stream = new FileStream(SavePath + nameof(prefs.Key), FileMode.Open);
|
||||
preferenceList[prefs.Key] = (IPreferences) xmlSerializer.Deserialize(stream);
|
||||
stream.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
foreach (KeyValuePair<Type, IPreferences> prefs in preferenceList)
|
||||
{
|
||||
Stream stream = new FileStream(SavePath + nameof(prefs.Key), FileMode.Create);
|
||||
xmlSerializer.Serialize(stream, prefs.Value);
|
||||
stream.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user