Skip to content

Instantly share code, notes, and snippets.

@TimTheTerribleCS
Created November 2, 2016 23:56
Show Gist options
  • Save TimTheTerribleCS/8749f2f74b9f35c5a0fcd6d6015a5a02 to your computer and use it in GitHub Desktop.
Save TimTheTerribleCS/8749f2f74b9f35c5a0fcd6d6015a5a02 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.IO;
using System.Xml.Serialization;
using UnityEngine;
public abstract class Configuration<C> where C : class, new()
{
private static C instance;
public static C Load()
{
if (instance == null)
{
var configPath = GetConfigPath();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(C));
try
{
if (File.Exists(configPath))
{
using (StreamReader streamReader = new System.IO.StreamReader(configPath))
{
instance = xmlSerializer.Deserialize(streamReader) as C;
}
}
}
catch (Exception e)
{
Debug.LogException(e);
}
}
return instance ?? (instance = new C());
}
public static void Save()
{
if (instance == null) return;
var configPath = GetConfigPath();
var xmlSerializer = new XmlSerializer(typeof(C));
var noNamespaces = new XmlSerializerNamespaces();
noNamespaces.Add("", "");
try
{
using (var streamWriter = new System.IO.StreamWriter(configPath))
{
xmlSerializer.Serialize(streamWriter, instance, noNamespaces);
}
}
catch (Exception e)
{
Debug.LogException(e);
}
}
private static string GetConfigPath()
{
var configPathAttribute = typeof(C).GetCustomAttributes(typeof(ConfigurationPathAttribute), true)
.FirstOrDefault() as ConfigurationPathAttribute;
if (configPathAttribute != null)
{
return configPathAttribute.Value;
}
else
{
Debug.LogError("ConfigurationPath attribute missing in " + typeof(C).Name);
return typeof(C).Name + ".xml";
}
}
}
[AttributeUsage(AttributeTargets.Class)]
public class ConfigurationPathAttribute : Attribute
{
public ConfigurationPathAttribute(string value)
{
Value = value;
}
public string Value { get; private set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment