apieceoffruit

MonitoredSO

Jul 6th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.CompilerServices;
  7. using AnomieUtilities;
  8. using JetBrains.Annotations;
  9. using UnityEngine;
  10. using AnomieProp = AnomieUtilities.MonitoredSettings.PropertyInfo;
  11. namespace JasonStorey
  12. {
  13.     public abstract class MonitoredSo : ScriptableObject,INotifyPropertyChanged
  14.     {
  15.         public event PropertyChangedEventHandler PropertyChanged;
  16.  
  17.         MonitoredSettings _settings;
  18.         public MonitoredSettings Settings => _settings ?? (_settings = new MonitoredSettings());
  19.        
  20.         [NotifyPropertyChangedInvocator]
  21.         protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  22.         {
  23.             if(string.IsNullOrWhiteSpace(propertyName)) return;
  24.             SaveValue(Settings,propertyName);    
  25.             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  26.         }
  27.  
  28.         protected abstract void SaveValue(MonitoredSettings settings, string prop);
  29.        
  30.         protected void HandleProperty<TType>(TType owner,string propName)
  31.         {
  32.             var prop = typeof(TType).GetProperty(propName, BindingFlags.Public | BindingFlags.Instance);
  33.             var t = prop.GetType();
  34.             var info = new AnomieProp
  35.             {
  36.                 Type = t,
  37.                 Key = propName
  38.             };
  39.             var val = prop.GetValue(owner);
  40.            
  41.             Settings.Set(info,val);
  42.             if(Settings.IsDirty && !_wasDirty)
  43.                 OnDirtied();
  44.             if(!Settings.IsDirty && _wasDirty)
  45.                 OnResolved();
  46.            
  47.             OnChanged();
  48.             _wasDirty = Settings.IsDirty;
  49.         }
  50.  
  51.         bool _wasDirty;
  52.  
  53.         public bool IsDirty => Settings.IsDirty;
  54.  
  55.         public IEnumerable<AnomieProp> DirtyProperties => Settings.DirtiedProperties;
  56.  
  57.         public event Action Dirtied;
  58.         public event Action Resolved;
  59.         public event Action Changed;
  60.         protected virtual void OnDirtied() => Dirtied?.Invoke();
  61.        
  62.         public void Clean() => Settings.Resolved();
  63.         protected virtual void OnResolved() => Resolved?.Invoke();
  64.  
  65.         protected virtual void OnChanged() => Changed?.Invoke();
  66.  
  67.  
  68.         public object GetValue(MonitoredSettings.PropertyInfo propertyInfo) =>
  69.             Settings.TryGet(propertyInfo, out var obj) ? obj : null;
  70.  
  71.         public void Set<T>(MonitoredSettings.PropertyInfo modified, T val) =>
  72.             Settings.Set(modified,val);
  73.     }
  74. }
  75.  
Add Comment
Please, Sign In to add comment