ConfigWrapper.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. namespace BepInEx.Configuration
  3. {
  4. /// <summary>
  5. /// Provides access to a single setting inside of a <see cref="Configuration.ConfigFile"/>.
  6. /// </summary>
  7. /// <typeparam name="T">Type of the setting.</typeparam>
  8. public sealed class ConfigWrapper<T> : ConfigEntryBase
  9. {
  10. /// <summary>
  11. /// Fired when the setting is changed. Does not detect changes made outside from this object.
  12. /// </summary>
  13. public event EventHandler SettingChanged;
  14. private T _typedValue;
  15. /// <summary>
  16. /// Value of this setting.
  17. /// </summary>
  18. public T Value
  19. {
  20. get => _typedValue;
  21. set
  22. {
  23. value = ClampValue(value);
  24. if (Equals(_typedValue, value))
  25. return;
  26. _typedValue = value;
  27. OnSettingChanged(this);
  28. }
  29. }
  30. /// <inheritdoc />
  31. public override object BoxedValue
  32. {
  33. get => Value;
  34. set => Value = (T)value;
  35. }
  36. internal ConfigWrapper(ConfigFile configFile, ConfigDefinition definition, T defaultValue) : base(configFile, definition, typeof(T), defaultValue)
  37. {
  38. configFile.SettingChanged += (sender, args) =>
  39. {
  40. if (args.ChangedSetting == this) SettingChanged?.Invoke(sender, args);
  41. };
  42. }
  43. }
  44. }