ConfigWrapper.cs 846 B

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. namespace BepInEx.Configuration
  3. {
  4. public sealed class ConfigWrapper<T>
  5. {
  6. public ConfigEntry ConfigEntry { get; }
  7. public ConfigDefinition Definition => ConfigEntry.Definition;
  8. public ConfigFile ConfigFile => ConfigEntry.ConfigFile;
  9. /// <summary>
  10. /// Fired when the setting is changed. Does not detect changes made outside from this object.
  11. /// </summary>
  12. public event EventHandler SettingChanged;
  13. public T Value
  14. {
  15. get => (T)ConfigEntry.Value;
  16. set => ConfigEntry.SetValue(value, true, this);
  17. }
  18. internal ConfigWrapper(ConfigEntry configEntry)
  19. {
  20. ConfigEntry = configEntry ?? throw new ArgumentNullException(nameof(configEntry));
  21. configEntry.ConfigFile.SettingChanged += (sender, args) =>
  22. {
  23. if (args.ChangedSetting == configEntry) SettingChanged?.Invoke(sender, args);
  24. };
  25. }
  26. }
  27. }