using 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(string key, T defaultValue) where T : struct { if (!KeyExists(key)) { SetValue(key, defaultValue); } return GetValue(key, defaultValue); } public static T GetValue(string key) where T : struct { return GetValue(key, default(T)); } public static T GetValue(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(value); } return ret; } public static void SetValue(string key, T value) where T : struct { key = key.ToLower(); string valueString = GetValueString(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 value) { Type type = value.GetType(); string typeString = type.ToString(); return String.Format("{0}:{1}", typeString, GetPropertyString(value)); } private static string GetPropertyString(T value) { string propsString = string.Empty; FieldInfo[] fields = value.GetType().GetFields(); List valueFields = new List(); 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(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 valueFields = new List(); 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)); } } } }