diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2017-01-06 20:55:59 +0100 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2017-01-06 21:04:43 +0100 |
| commit | b741fbb78971f336a26614732e95ac21c760bf1a (patch) | |
| tree | 9c5758382e23eae9d5ec4db14cfb2b6aba0e8b77 /src/Configuration.cs | |
| parent | aa6799060989c548197998b0e3f1ed47e066e99e (diff) | |
optimize configuration class
Diffstat (limited to 'src/Configuration.cs')
| -rw-r--r-- | src/Configuration.cs | 98 |
1 files changed, 72 insertions, 26 deletions
diff --git a/src/Configuration.cs b/src/Configuration.cs index 48c57c87..70923e60 100644 --- a/src/Configuration.cs +++ b/src/Configuration.cs @@ -22,18 +22,48 @@ using System; using System.Reflection; using System.IO; using System.Collections.Generic; +using System.Threading; namespace Crow { - //TODO:maybe create iterator + public class ConfigItem { + Type type; + internal object curVal; + bool parsingNeeded = false; + public ConfigItem(object newVal){ + curVal = newVal; + } + public ConfigItem(string newVal){ + curVal = newVal; + parsingNeeded = true; + } + public T GetValue<T>(){ + if (type == null) + type = typeof(T); + if (parsingNeeded) { + MethodInfo miParse = type.GetMethod ("Parse", new Type[] {typeof(string)}); + if (miParse != null) + curVal = miParse.Invoke (null, new object[]{ curVal }); + parsingNeeded = false; + } + return (T)Convert.ChangeType (curVal, type); + } + public void Set(object value){ + curVal = value; + } + } + /// <summary> + /// Application wide Configuration utility + /// </summary> public static class Configuration { + volatile static bool isDirty = false; static string configPath, configFileName = "app.config"; - static Dictionary<string, string> items; + static Dictionary<string, ConfigItem> items; static Configuration () { - items = new Dictionary<string, string> (); + items = new Dictionary<string, ConfigItem> (); string configRoot = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), @@ -50,42 +80,58 @@ namespace Crow string path = Path.Combine(configPath, configFileName); if (File.Exists (path)) { - using (Stream s = new FileStream(path, FileMode.Open)) + using (Stream s = new FileStream (path, FileMode.Open)) load (s); - return; + } else { + string defaultConfigResID = appName + ".default.config"; + bool found = false; + foreach (string resIds in a.GetManifestResourceNames()) { + if (string.Equals (defaultConfigResID, resIds, StringComparison.OrdinalIgnoreCase)) { + using (Stream s = a.GetManifestResourceStream (defaultConfigResID)) + load (s); + found = true; + break; + } + } + if (!found) + Console.WriteLine ("No Default Config found. ({0})", defaultConfigResID); } - - string defaultConfigResID = appName + ".default.config"; - foreach (string resIds in a.GetManifestResourceNames()) { - if (string.Equals (defaultConfigResID, resIds, StringComparison.OrdinalIgnoreCase)) { - using (Stream s = a.GetManifestResourceStream (defaultConfigResID)) - load (s); - return; + startSavingThread (); + } + static void startSavingThread(){ + Thread t = new Thread (savingThread); + t.IsBackground = true; + t.Start (); + } + static void savingThread(){ + while(true){ + if (isDirty) { + save (); + isDirty = false; } + Thread.Sleep (1000); } - Console.WriteLine ("No Default Config found. ({0})", defaultConfigResID); } public static T Get<T>(string key) { - if (!items.ContainsKey (key)) - return default(T); - Type type = typeof(T); - MethodInfo miParse = type.GetMethod ("Parse", new Type[] {typeof(string)}); - if (miParse == null) - return (T)Convert.ChangeType (items [key], typeof(T)); - - return (T)Convert.ChangeType (miParse.Invoke (null, new object[]{ items [key] }), type); + return !items.ContainsKey (key) ? default(T) : items [key].GetValue<T> (); } public static void Set<T>(string key, T value) { - items [key] = value.ToString(); - save (); + if (!items.ContainsKey (key)) { + lock(items) + items[key] = new ConfigItem (value); + }else + items[key].Set (value); + isDirty = true; } static void save(){ using (Stream s = new FileStream(Path.Combine(configPath, configFileName),FileMode.Create)){ using (StreamWriter sw = new StreamWriter (s)) { - foreach (string key in items.Keys) { - sw.WriteLine(key + "=" + items[key]); + lock (items) { + foreach (string key in items.Keys) { + sw.WriteLine (key + "=" + (string)items [key].curVal.ToString ()); + } } } } @@ -99,7 +145,7 @@ namespace Crow string[] tmp = l.Trim ().Split ('='); if (tmp.Length != 2) continue; - items[tmp [0].Trim ()] = tmp [1].Trim (); + items[tmp [0].Trim ()] = new ConfigItem(tmp [1].Trim ()); } } } |