From: Jean-Philippe Bruyère Date: Fri, 6 Jan 2017 19:55:59 +0000 (+0100) Subject: optimize configuration class X-Git-Tag: v0.5.1~43 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=b741fbb78971f336a26614732e95ac21c760bf1a;p=jp%2Fcrow.git optimize configuration class --- 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(){ + 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; + } + } + /// + /// Application wide Configuration utility + /// public static class Configuration { + volatile static bool isDirty = false; static string configPath, configFileName = "app.config"; - static Dictionary items; + static Dictionary items; static Configuration () { - items = new Dictionary (); + items = new Dictionary (); 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(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 (); } public static void Set(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 ()); } } }