Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace AnomieUtilities
- {
- public class MonitoredSettings
- {
- public MonitoredSettings() =>
- _monitoredKeys = new Dictionary<Type, IMonitoredKeys>();
- public int Count => _monitoredKeys.Values.Sum(x => x.Count);
- public bool IsDirty => _monitoredKeys.Any(x => x.Value.IsDirty);
- public IEnumerable<PropertyInfo> DirtiedProperties => _monitoredKeys.SelectMany(ToPropertyInfoCollection);
- IEnumerable<PropertyInfo> ToPropertyInfoCollection(KeyValuePair<Type, IMonitoredKeys> kvp)
- {
- var dirtied = kvp.Value.DirtiedProperties;
- foreach (var prop in dirtied)
- yield return PropertyInfo.Create(kvp.Key, prop);
- }
- public T Get<T>(PropertyInfo info)
- {
- var found = TryGet(info.Type,info.Key,out var val);
- found = found && val is T;
- return found ? (T)val : default;
- }
- public bool TryGet<T>(PropertyInfo info,out T result)
- {
- var found = TryGet(info.Type,info.Key,out var val);
- found = found && val is T;
- result = (T)val;
- return found;
- }
- public bool TryGet<T>(string key,out T result)
- {
- var found = TryGet(typeof(T),key,out var val);
- found = found && val is T;
- result = (T)val;
- return found;
- }
- public bool TryGet(PropertyInfo info,out object result)
- {
- var found = TryGet(info.Type,info.Key,out var val);
- result = val;
- return found;
- }
- public event Action Dirtied;
- public bool TryGet(Type t,string key,out object o)
- {
- if (!TypeIsRegistered(t))
- {
- o = null;
- return false;
- }
- o = _monitoredKeys[t].Get(key);
- return true;
- }
- bool TypeIsRegistered(Type t) => _monitoredKeys.ContainsKey(t);
- public T Get<T>(string name)
- {
- TryRegisterType<T>();
- var handler = _monitoredKeys[typeof(T)];
- return (T)Convert.ChangeType(handler.Get(name), typeof(T));
- }
- public void Set<T>(string name, T val)
- {
- TryRegisterType<T>();
- var mk = (MonitoredKeys<T>)_monitoredKeys[typeof(T)];
- mk.Set(name,val);
- }
- public void Set<T>(PropertyInfo info,T val) =>
- Set(info.Key,val);
- public void Create(PropertyInfo info) =>
- Set(info.Key,GetDefaultValue(info.Type));
- public static object GetDefaultValue(Type t)
- {
- if (t.IsValueType && Nullable.GetUnderlyingType(t) == null)
- return Activator.CreateInstance(t);
- return null;
- }
- public void Resolved()
- {
- foreach (var mk in _monitoredKeys.Values)
- mk.Resolved();
- }
- bool TryRegisterType<T>()
- {
- if (_monitoredKeys.ContainsKey(typeof(T))) return false;
- var mk = new MonitoredKeys<T>();
- mk.Dirtied += OnDirtied;
- _monitoredKeys[typeof(T)] = mk;
- return true;
- }
- void OnDirtied<T>(T val) => OnDirtied();
- Dictionary<Type, IMonitoredKeys> _monitoredKeys;
- protected virtual void OnDirtied()
- {
- Dirtied?.Invoke();
- }
- [Serializable]
- public class PropertyInfo
- {
- public static PropertyInfo Create(Type t, string key) =>
- new PropertyInfo
- {
- Type = t,
- Key = key
- };
- public Type Type;
- public string Key;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement