New preference persistence solution.

This commit is contained in:
Harrison Deng 2020-02-29 17:15:33 -05:00
parent 41c4594904
commit 278031ad7d
3 changed files with 134 additions and 103 deletions

View File

@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
namespace RecrownedGTK.Persistence
{
/// <summary>
/// Manages a bundle of preferences.
/// </summary>
public class Preferences
{
private Dictionary<string, string> preferences;
string savePath;
string fileName;
XmlSerializer xmlSerializer;
/// <summary>
/// Constructs the preference manager.
/// </summary>
/// <param name="savePath">The path of the directory in which the preferences should be saved.</param>
/// <param name="preferences">The preferences to be serialized and unserialized in XML format.</param>
public Preferences(string savePath, string fileName)
{
this.savePath = savePath;
this.fileName = fileName;
Directory.CreateDirectory(savePath);
preferences = new Dictionary<string, string>();
xmlSerializer = new XmlSerializer(typeof(PreferencesInfo));
}
public string GetStringValue(string key) {
if (!preferences.ContainsKey(key)) {
preferences.Add(key, "");
}
return preferences[key];
}
public int GetIntValue(string key) {
return int.Parse(GetStringValue(key));
}
public float GetFloatValue(string key) {
return float.Parse(GetStringValue(key));
}
public bool GetBoolValue(string key) {
return bool.Parse(GetStringValue(key));
}
public void SetPreference(string key, string value) {
if (preferences.ContainsKey(key)) {
preferences[key] = value;
} else {
preferences.Add(key, value);
}
}
public void SetPreference(string key, int value) {
SetPreference(key, value.ToString());
}
public void SetPreference(string key, float value) {
SetPreference(key, value.ToString());
}
public void SetPreference(string key, bool value) {
SetPreference(key, value.ToString());
}
public bool DeletePreference(string key) {
if (!preferences.ContainsKey(key)) return false;
preferences.Remove(key);
return true;
}
/// <summary>
/// Loads preferences.
/// </summary>
public bool Load()
{
try {
using (StreamReader stream = new StreamReader(string.Format("{0}/{1}.xml", savePath, fileName))) {
this.preferences = ((PreferencesInfo) xmlSerializer.Deserialize(stream)).GetPreferences();
}
} catch (FileNotFoundException) {
return false;
}
return true;
}
/// <summary>
/// Saves preferences.
/// </summary>
public void Save()
{
using (FileStream fileStream = new FileStream(string.Format("{0}/{1}.xml", savePath, fileName), FileMode.Create)) {
PreferencesInfo preferences = new PreferencesInfo();
preferences.SetPreferences(this.preferences);
xmlSerializer.Serialize(fileStream, preferences);
}
}
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
namespace RecrownedGTK.Persistence {
public struct PreferencesInfo {
public PreferenceInfo[] preferences;
public void SetPreferences(Dictionary<string, string> preferences)
{
this.preferences = new PreferenceInfo[preferences.Count];
int pairIndex = 0;
foreach (KeyValuePair<string, string> item in preferences)
{
this.preferences[pairIndex] = new PreferenceInfo(item.Key, item.Value);
pairIndex++;
}
}
public Dictionary<string, string> GetPreferences() {
Dictionary<string, string> res = new Dictionary<string, string>();
for (int prefID = 0; prefID < preferences.Length; prefID++) {
res.Add(preferences[prefID].key, preferences[prefID].value);
}
return res;
}
public struct PreferenceInfo {
public string key, value;
public PreferenceInfo(string key, string value) {
this.key = key;
this.value = value;
}
}
}
}

View File

@ -1,103 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
namespace RecrownedGTK.Persistence
{
/// <summary>
/// Manages a bundle of preferences.
/// </summary>
public class PreferencesManager
{
private readonly Dictionary<Type, object> preferenceList;
string savePath;
XmlSerializer xmlSerializer;
/// <summary>
/// Constructs the preference manager.
/// </summary>
/// <param name="savePath">The path of the directory in which the preferences should be saved.</param>
/// <param name="preferences">The preferences to be serialized and unserialized in XML format.</param>
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);
}
/// <summary>
/// Returns the preference by type.
/// </summary>
/// <typeparam name="T">The preference needed.</typeparam>
/// <returns>The preference needed.</returns>
public T GetPreferences<T>()
{
return (T)preferenceList[typeof(T)];
}
/// <summary>
/// Loads preferences.
/// </summary>
public void Load()
{
List<Type> keys = new List<Type>(preferenceList.Keys);
foreach (Type key in keys)
{
if (!LoadSpecific(key))
{
SaveSpecific(key);
}
}
}
/// <summary>
/// Saves preferences.
/// </summary>
public void Save()
{
foreach (KeyValuePair<Type, object> prefs in preferenceList)
{
SaveSpecific(prefs.Key);
}
}
private bool LoadSpecific(Type preference)
{
string path = savePath + "/" + preference.Name + ".xml";
try
{
if (File.Exists(path))
{
Stream stream = new FileStream(path, FileMode.Open);
preferenceList[preference] = xmlSerializer.Deserialize(stream);
stream.Close();
return true;
}
} catch (InvalidOperationException)
{
return false;
}
return false;
}
private void SaveSpecific(Type preference)
{
Stream stream = new FileStream(savePath + "/" + preference.Name + ".xml", FileMode.Create);
xmlSerializer.Serialize(stream, preferenceList[preference]);
stream.Close();
}
}
}