ConfigWrapper.cs 1.3 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>
  9. {
  10. /// <summary>
  11. /// Entry of this setting in the <see cref="Configuration.ConfigFile"/>.
  12. /// </summary>
  13. public ConfigEntry ConfigEntry { get; }
  14. /// <summary>
  15. /// Unique definition of this setting.
  16. /// </summary>
  17. public ConfigDefinition Definition => ConfigEntry.Definition;
  18. /// <summary>
  19. /// Config file this setting is inside of.
  20. /// </summary>
  21. public ConfigFile ConfigFile => ConfigEntry.ConfigFile;
  22. /// <summary>
  23. /// Fired when the setting is changed. Does not detect changes made outside from this object.
  24. /// </summary>
  25. public event EventHandler SettingChanged;
  26. /// <summary>
  27. /// Value of this setting.
  28. /// </summary>
  29. public T Value
  30. {
  31. get => (T)ConfigEntry.Value;
  32. set => ConfigEntry.SetValue(value, true, this);
  33. }
  34. internal ConfigWrapper(ConfigEntry configEntry)
  35. {
  36. ConfigEntry = configEntry ?? throw new ArgumentNullException(nameof(configEntry));
  37. configEntry.ConfigFile.SettingChanged += (sender, args) =>
  38. {
  39. if (args.ChangedSetting == configEntry) SettingChanged?.Invoke(sender, args);
  40. };
  41. }
  42. }
  43. }