Advertisement
apieceoffruit

MonitoredSettings

Jul 6th, 2022
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace AnomieUtilities
  6. {
  7.     public class MonitoredSettings
  8.     {
  9.  
  10.         public MonitoredSettings() =>
  11.             _monitoredKeys = new Dictionary<Type, IMonitoredKeys>();
  12.  
  13.         public int Count => _monitoredKeys.Values.Sum(x => x.Count);
  14.         public bool IsDirty => _monitoredKeys.Any(x => x.Value.IsDirty);
  15.         public IEnumerable<PropertyInfo> DirtiedProperties => _monitoredKeys.SelectMany(ToPropertyInfoCollection);
  16.  
  17.         IEnumerable<PropertyInfo> ToPropertyInfoCollection(KeyValuePair<Type, IMonitoredKeys> kvp)
  18.         {
  19.             var dirtied = kvp.Value.DirtiedProperties;
  20.             foreach (var prop in dirtied)
  21.                 yield return PropertyInfo.Create(kvp.Key, prop);
  22.         }
  23.  
  24.         public T Get<T>(PropertyInfo info)
  25.         {
  26.             var found = TryGet(info.Type,info.Key,out var val);
  27.             found = found && val is T;
  28.             return found ? (T)val : default;
  29.         }
  30.        
  31.         public bool TryGet<T>(PropertyInfo info,out T result)
  32.         {
  33.             var found = TryGet(info.Type,info.Key,out var val);
  34.             found = found && val is T;
  35.             result = (T)val;
  36.             return found;
  37.         }
  38.        
  39.         public bool TryGet<T>(string key,out T result)
  40.         {
  41.             var found = TryGet(typeof(T),key,out var val);
  42.             found = found && val is T;
  43.             result = (T)val;
  44.             return found;
  45.         }
  46.        
  47.        
  48.         public bool TryGet(PropertyInfo info,out object result)
  49.         {
  50.             var found = TryGet(info.Type,info.Key,out var val);
  51.             result = val;
  52.             return found;
  53.         }
  54.        
  55.         public event Action Dirtied;
  56.  
  57.         public bool TryGet(Type t,string key,out object o)
  58.         {
  59.             if (!TypeIsRegistered(t))
  60.             {
  61.                 o = null;
  62.                 return false;
  63.             }
  64.  
  65.             o = _monitoredKeys[t].Get(key);
  66.             return true;
  67.         }
  68.  
  69.         bool TypeIsRegistered(Type t) => _monitoredKeys.ContainsKey(t);
  70.  
  71.         public T Get<T>(string name)
  72.         {
  73.             TryRegisterType<T>();
  74.             var handler = _monitoredKeys[typeof(T)];
  75.             return (T)Convert.ChangeType(handler.Get(name), typeof(T));
  76.         }
  77.  
  78.         public void Set<T>(string name, T val)
  79.         {
  80.             TryRegisterType<T>();
  81.             var mk = (MonitoredKeys<T>)_monitoredKeys[typeof(T)];
  82.             mk.Set(name,val);
  83.         }
  84.  
  85.         public void Set<T>(PropertyInfo info,T val) =>
  86.             Set(info.Key,val);
  87.        
  88.         public void Create(PropertyInfo info) =>
  89.             Set(info.Key,GetDefaultValue(info.Type));
  90.        
  91.         public static object GetDefaultValue(Type t)
  92.         {
  93.             if (t.IsValueType && Nullable.GetUnderlyingType(t) == null)
  94.                 return Activator.CreateInstance(t);
  95.             return null;
  96.         }
  97.        
  98.         public void Resolved()
  99.         {
  100.             foreach (var mk in _monitoredKeys.Values)
  101.                 mk.Resolved();
  102.         }
  103.  
  104.         bool TryRegisterType<T>()
  105.         {
  106.             if (_monitoredKeys.ContainsKey(typeof(T))) return false;
  107.             var mk = new MonitoredKeys<T>();
  108.             mk.Dirtied += OnDirtied;
  109.             _monitoredKeys[typeof(T)] = mk;
  110.            
  111.             return true;
  112.         }
  113.  
  114.         void OnDirtied<T>(T val) => OnDirtied();
  115.  
  116.  
  117.         Dictionary<Type, IMonitoredKeys> _monitoredKeys;
  118.  
  119.         protected virtual void OnDirtied()
  120.         {
  121.             Dirtied?.Invoke();
  122.         }
  123.  
  124.         [Serializable]
  125.         public class PropertyInfo
  126.         {
  127.             public static PropertyInfo Create(Type t, string key) =>
  128.                 new PropertyInfo
  129.                 {
  130.                     Type = t,
  131.                     Key = key
  132.                 };
  133.  
  134.             public Type Type;
  135.             public string Key;
  136.         }
  137.        
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement