ConfigWrapper.cs 962 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. namespace BepInEx.Configuration
  3. {
  4. public class ConfigWrapper<T>
  5. {
  6. public ConfigDefinition Definition { get; protected set; }
  7. public ConfigFile ConfigFile { get; protected set; }
  8. /// <summary>
  9. /// Fired when the setting is changed. Does not detect changes made outside from this object.
  10. /// </summary>
  11. public event EventHandler SettingChanged;
  12. public T Value
  13. {
  14. get => TomlTypeConverter.ConvertToValue<T>(ConfigFile.Cache[Definition]);
  15. set
  16. {
  17. ConfigFile.Cache[Definition] = TomlTypeConverter.ConvertToString(value);
  18. if (ConfigFile.SaveOnConfigSet)
  19. ConfigFile.Save();
  20. SettingChanged?.Invoke(this, EventArgs.Empty);
  21. }
  22. }
  23. public ConfigWrapper(ConfigFile configFile, ConfigDefinition definition)
  24. {
  25. if (!TomlTypeConverter.SupportedTypes.Contains(typeof(T)))
  26. throw new ArgumentException("Unsupported config wrapper type");
  27. ConfigFile = configFile;
  28. Definition = definition;
  29. }
  30. }
  31. }