88
99namespace IPT . Common . User
1010{
11- /// <summary>
12- /// A base class for generating a plugin-specific Configuration class.
13- /// </summary>
14- public abstract class Configuration
11+ public abstract class Configuration : IDisposable
1512 {
16- #pragma warning disable S1104 , SA1401 , CS1591 , SA1600
13+ public delegate void SettingChangedEventHandler ( Setting setting ) ;
14+ public event SettingChangedEventHandler SettingChanged ;
1715 public SettingInt LogLevel = Logging . GetLogLevelSetting ( ) ;
18- #pragma warning restore S1104 , SA1401 , CS1591 , SA1600
1916
20- private readonly List < Setting > allSettings ;
17+ private readonly List < Setting > _allSettings ;
2118
22- /// <summary>
23- /// Initializes a new instance of the <see cref="Configuration"/> class.
24- /// </summary>
2519 protected Configuration ( )
2620 {
27- this . allSettings = this . GetAllSettings ( ) ;
21+ _allSettings = GetAllSettings ( ) ;
22+ foreach ( var setting in _allSettings ) setting . OnValueChanged += OnSettingValueChanged ;
2823 }
2924
30- /// <summary>
31- /// Gets a list of all settings objects.
32- /// </summary>
33- public List < Setting > AllSettings
25+ public List < Setting > AllSettings { get => _allSettings ; }
26+
27+ public void Dispose ( )
3428 {
35- get
36- {
37- return this . allSettings ;
38- }
29+ foreach ( var setting in _allSettings ) setting . OnValueChanged -= OnSettingValueChanged ;
3930 }
4031
41- /// <summary>
42- /// Gets a list of generic combos defined in the settings.
43- /// </summary>
44- /// <returns>A list of combos.</returns>
4532 public List < GenericCombo > GetInputCombos ( )
4633 {
4734 var combos = new List < GenericCombo > ( ) ;
4835 foreach ( var setting in this . AllSettings )
4936 {
50- if ( setting . GetValue ( ) is GenericCombo combo )
51- {
52- combos . Add ( combo ) ;
53- }
37+ if ( setting . GetValue ( ) is GenericCombo combo ) combos . Add ( combo ) ;
5438 }
5539
5640 return combos ;
5741 }
5842
59- /// <summary>
60- /// Load the settings.
61- /// </summary>
6243 public abstract void Load ( ) ;
6344
64- /// <summary>
65- /// Logs all of the settings.
66- /// </summary>
6745 public void Log ( )
6846 {
6947 Logging . Info ( "============================================================" ) ;
@@ -77,10 +55,6 @@ public void Log()
7755 Logging . Info ( "============================================================" ) ;
7856 }
7957
80- /// <summary>
81- /// Loads settings from INI file.
82- /// </summary>
83- /// <param name="filename">The filename of the INI file. Expects path relative to the GTAV folder.</param>
8458 protected void LoadINI ( string filename )
8559 {
8660 var ini = new InitializationFile ( filename ) ;
@@ -100,37 +74,25 @@ protected void LoadINI(string filename)
10074 }
10175 }
10276
103- /// <summary>
104- /// Saves settings to the INI file.
105- /// </summary>
106- /// <param name="filename">The filename of the INI file. Expects path relative to the GTAV folder.</param>
10777 protected void SaveINI ( string filename )
10878 {
10979 InitializationFile ini = new InitializationFile ( filename ) ;
110- if ( ini . Exists ( ) )
111- {
112- ini . Delete ( ) ;
113- }
114-
80+ if ( ini . Exists ( ) ) ini . Delete ( ) ;
11581 ini . Create ( ) ;
116- foreach ( var entry in this . AllSettings )
117- {
118- entry . Save ( ini ) ;
119- }
82+ foreach ( var entry in this . AllSettings ) entry . Save ( ini ) ;
12083 }
12184
12285 private List < Setting > GetAllSettings ( )
12386 {
12487 var settings = new List < Setting > ( ) ;
12588 foreach ( var field in this . GetType ( ) . GetFields ( BindingFlags . Public | BindingFlags . Instance ) )
12689 {
127- if ( field . GetValue ( this ) is Setting setting )
128- {
129- settings . Add ( setting ) ;
130- }
90+ if ( field . GetValue ( this ) is Setting setting ) settings . Add ( setting ) ;
13191 }
13292
13393 return settings ;
13494 }
95+
96+ private void OnSettingValueChanged ( Setting setting ) => SettingChanged ? . Invoke ( setting ) ;
13597 }
13698}
0 commit comments