Content loading system functioning.

This commit is contained in:
2018-10-30 18:29:54 -05:00
parent 3a4dfb94ac
commit a9647f2146
14 changed files with 189 additions and 87 deletions

View File

@@ -3,10 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace RhythmBullet.Zer01HD.Utilities.Persistence
{
public interface IPreferences
public class Preferences
{
}
}

View File

@@ -11,43 +11,67 @@ namespace RhythmBullet.Zer01HD.Utilities
{
public class PreferencesManager
{
private readonly Dictionary<Type, IPreferences> preferenceList;
private readonly Dictionary<Type, Preferences> preferenceList;
public string SavePath;
XmlSerializer xmlSerializer;
public PreferencesManager(string savePath, params IPreferences[] preferences)
public PreferencesManager(string savePath, params Preferences[] preferences)
{
this.SavePath = savePath;
preferenceList = new Dictionary<Type, IPreferences>();
foreach (IPreferences prefs in preferences)
preferenceList = new Dictionary<Type, Preferences>();
Type[] preferenceTypes = new Type[preferences.Length];
for (int prefID = 0; prefID < preferences.Length; prefID++)
{
preferenceList.Add(prefs.GetType(), prefs);
preferenceList.Add(preferences[prefID].GetType(), preferences[prefID]);
preferenceTypes[prefID] = preferences[prefID].GetType();
}
xmlSerializer = new XmlSerializer(typeof(Preferences), preferenceTypes);
}
public T GetPreferences<T>() where T : IPreferences
public T GetPreferences<T>() where T : Preferences
{
return (T) preferenceList[typeof(T)];
return (T)preferenceList[typeof(T)];
}
public void Load()
{
foreach (KeyValuePair<Type, IPreferences> prefs in preferenceList)
foreach (KeyValuePair<Type, Preferences> prefs in preferenceList)
{
Stream stream = new FileStream(SavePath + nameof(prefs.Key), FileMode.Open);
preferenceList[prefs.Key] = (IPreferences) xmlSerializer.Deserialize(stream);
stream.Close();
if (!LoadSpecific(prefs.Key))
{
SaveSpecific(prefs.Key);
}
}
}
public void Save()
{
foreach (KeyValuePair<Type, IPreferences> prefs in preferenceList)
foreach (KeyValuePair<Type, Preferences> prefs in preferenceList)
{
Stream stream = new FileStream(SavePath + nameof(prefs.Key), FileMode.Create);
xmlSerializer.Serialize(stream, prefs.Value);
stream.Close();
SaveSpecific(prefs.Key);
}
}
private bool LoadSpecific(Type preference)
{
string path = SavePath + "/" + preference.Name;
if (File.Exists(path))
{
Stream stream = new FileStream(SavePath + "/" + preference.Name, FileMode.Open);
preferenceList[preference] = (Preferences)xmlSerializer.Deserialize(stream);
stream.Close();
return true;
}
return false;
}
private void SaveSpecific(Type preference)
{
Stream stream = new FileStream(SavePath + preference.Name, FileMode.Create);
xmlSerializer.Serialize(stream, preferenceList[preference]);
stream.Close();
}
}
}