Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.CompilerServices;
- using AnomieUtilities;
- using JetBrains.Annotations;
- using UnityEngine;
- using AnomieProp = AnomieUtilities.MonitoredSettings.PropertyInfo;
- namespace JasonStorey
- {
- public abstract class MonitoredSo : ScriptableObject,INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- MonitoredSettings _settings;
- public MonitoredSettings Settings => _settings ?? (_settings = new MonitoredSettings());
- [NotifyPropertyChangedInvocator]
- protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
- {
- if(string.IsNullOrWhiteSpace(propertyName)) return;
- SaveValue(Settings,propertyName);
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- protected abstract void SaveValue(MonitoredSettings settings, string prop);
- protected void HandleProperty<TType>(TType owner,string propName)
- {
- var prop = typeof(TType).GetProperty(propName, BindingFlags.Public | BindingFlags.Instance);
- var t = prop.GetType();
- var info = new AnomieProp
- {
- Type = t,
- Key = propName
- };
- var val = prop.GetValue(owner);
- Settings.Set(info,val);
- if(Settings.IsDirty && !_wasDirty)
- OnDirtied();
- if(!Settings.IsDirty && _wasDirty)
- OnResolved();
- OnChanged();
- _wasDirty = Settings.IsDirty;
- }
- bool _wasDirty;
- public bool IsDirty => Settings.IsDirty;
- public IEnumerable<AnomieProp> DirtyProperties => Settings.DirtiedProperties;
- public event Action Dirtied;
- public event Action Resolved;
- public event Action Changed;
- protected virtual void OnDirtied() => Dirtied?.Invoke();
- public void Clean() => Settings.Resolved();
- protected virtual void OnResolved() => Resolved?.Invoke();
- protected virtual void OnChanged() => Changed?.Invoke();
- public object GetValue(MonitoredSettings.PropertyInfo propertyInfo) =>
- Settings.TryGet(propertyInfo, out var obj) ? obj : null;
- public void Set<T>(MonitoredSettings.PropertyInfo modified, T val) =>
- Settings.Set(modified,val);
- }
- }
Add Comment
Please, Sign In to add comment