rendered paste bodyusing System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using System.Diagnostics;
using System.Globalization;
using Microsoft.Xna.Framework;
using System.Reflection;
namespace Fds.Xna.Carbine
{
public class GameSettings
{
static Configuration config;
static GameSettings()
{
Process process = Process.GetCurrentProcess();
string file = process.MainModule.FileName;
config = ConfigurationManager.OpenExeConfiguration(Path.Combine(Directory.GetCurrentDirectory(), file));
}
public static T GetSetValue<T>(string key, T defaultValue) where T : struct
{
if (!KeyExists(key))
{
SetValue<T>(key, defaultValue);
}
return GetValue<T>(key, defaultValue);
}
public static T GetValue<T>(string key) where T : struct
{
return GetValue<T>(key, default(T));
}
public static T GetValue<T>(string key, T defaultValue) where T : struct
{
key = key.ToLower();
T ret = defaultValue;
if (KeyExists(key))
{
string value = config.AppSettings.Settings[key].Value;
ret = ConvertValue<T>(value);
}
return ret;
}
public static void SetValue<T>(string key, T value) where T : struct
{
key = key.ToLower();
string valueString = GetValueString<T>(value);
if (KeyExists(key))
{
config.AppSettings.Settings[key].Value = valueString;
}
else
{
config.AppSettings.Settings.Add(key, valueString);
}
config.Save(ConfigurationSaveMode.Modified);
}
private static string GetValueString<T>(T value)
{
Type type = value.GetType();
string typeString = type.ToString();
return String.Format("{0}:{1}", typeString, GetPropertyString<T>(value));
}
private static string GetPropertyString<T>(T value)
{
string propsString = string.Empty;
FieldInfo[] fields = value.GetType().GetFields();
List<FieldInfo> valueFields = new List<FieldInfo>();
for (int i = 0; i < fields.Length; i++)
{
if (!fields[i].IsStatic)
{
valueFields.Add(fields[i]);
}
}
fields = valueFields.ToArray();
if (fields.Length > 0)
{
for (int i = 0; i < fields.Length; i++)
{
FieldInfo info = fields[i];
object obj = info.GetValue(value);
propsString = string.Concat(propsString, string.Format("{0}|{1}{2}", info.Name, obj.ToString(),
((i + 1 == fields.Length) ? "" : ";")));
}
return string.Format("{0}", propsString);
}
else
{
return value.ToString();
}
}
public static bool KeyExists(string key)
{
key = key.ToLower();
string[] keys = config.AppSettings.Settings.AllKeys;
bool exists = false;
for (int i = 0; (i < keys.Length) && !exists; i++)
{
if (keys[i].ToLower() == key)
{
exists = true;
}
}
return exists;
}
private static T ConvertValue<T>(string value) where T : struct
{
string[] split = value.Split(':');
if (split.Length != 2)
{
throw new Exception("Settings string value was an invalid format");
}
string typeString = split[0];
string valueString = split[1];
Type type = typeof(T);
FieldInfo[] fields = type.GetFields();
List<FieldInfo> valueFields = new List<FieldInfo>();
for (int i = 0; i < fields.Length; i++)
{
if (!fields[i].IsStatic)
{
valueFields.Add(fields[i]);
}
}
fields = valueFields.ToArray();
if (fields.Length > 0)
{
T ret = default(T);
string[] props = valueString.Split(';');
foreach(string prop in props)
{
split = prop.Split('|');
if (split.Length != 2)
{
throw new Exception("Value format was invalid");
}
string propName = split[0];
string propValue = split[1];
FieldInfo info = type.GetField(prop);
info.SetValue(ret, Convert.ChangeType(propValue, info.ReflectedType));
}
return ret;
}
else
{
return (T)Convert.ChangeType(valueString, typeof(T));
}
}
}
}