1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Reflection ;
3
4
using System . IO ;
4
5
5
6
namespace FanControl . StorageSpacePlugin
6
7
{
7
8
internal static class Config
8
9
{
9
- private static int _defaultRefreshRate = 30 ;
10
+ private static readonly Dictionary < string , Action < string > > ConfigActions = new Dictionary < string , Action < string > > ( ) ;
11
+
12
+ static Config ( )
13
+ {
14
+ ConfigActions . Add (
15
+ ConfigValues . RefreshRateKey ,
16
+ value =>
17
+ {
18
+ var result = int . TryParse ( value , out var refreshRate ) ;
19
+ _refreshRate = result ? refreshRate : Defaults . RefreshRateValue ;
20
+ } ) ;
21
+
22
+ ConfigActions . Add (
23
+ ConfigValues . FallbackTemperatureKey ,
24
+ value =>
25
+ {
26
+ var result = float . TryParse ( value , out var fallbackTemperature ) ;
27
+ _fallbackTemperature = result ? fallbackTemperature : Defaults . FallbackTemperatureValue ;
28
+ } ) ;
29
+ }
10
30
11
31
public static string ApplicationPath ( )
12
32
{
@@ -24,13 +44,13 @@ public static string ConfigPath()
24
44
return Path . Combine ( PluginsPath ( ) , "FanControl.StorageSpacePlugin.ini" ) ;
25
45
}
26
46
27
- public static int RefreshRate ( )
47
+ public static void ReadConfig ( )
28
48
{
29
- string configText ;
49
+ string [ ] lines ;
30
50
31
51
try
32
52
{
33
- configText = File . ReadAllText ( ConfigPath ( ) ) ;
53
+ lines = File . ReadAllLines ( ConfigPath ( ) ) ;
34
54
}
35
55
catch ( Exception exception )
36
56
{
@@ -41,22 +61,44 @@ public static int RefreshRate()
41
61
case PathTooLongException _:
42
62
case IOException _:
43
63
case UnauthorizedAccessException _:
44
- return _defaultRefreshRate ;
64
+ return ;
45
65
default :
46
66
throw ; // Let FanControl handle and log the Exception
47
67
}
48
68
}
49
69
50
- var configsArray = configText . Split ( '=' ) ;
51
-
52
- var result = int . TryParse ( configsArray [ 1 ] , out var refreshRate ) ;
70
+ foreach ( var line in lines )
71
+ {
72
+ var configsArray = line . Split ( '=' ) ;
53
73
54
- return result ? refreshRate : _defaultRefreshRate ;
74
+ if ( configsArray . Length != 2 )
75
+ {
76
+ continue ;
77
+ }
78
+
79
+ if ( ConfigActions . TryGetValue ( configsArray [ 0 ] , out var configUpdate ) )
80
+ {
81
+ configUpdate . Invoke ( configsArray [ 1 ] ) ;
82
+ }
83
+ }
55
84
}
56
85
86
+ private static int _refreshRate = Defaults . RefreshRateValue ;
87
+ public static int RefreshRate => _refreshRate ;
88
+
89
+ private static float _fallbackTemperature = Defaults . FallbackTemperatureValue ;
90
+ public static float FallbackTemperature => _fallbackTemperature ;
91
+
57
92
public static class Defaults
58
93
{
59
- public static float FallbackTemperature => 36 ; // Default reported temperature in case of wrong PowerShell value
94
+ public static int RefreshRateValue => 30 ;
95
+ public static float FallbackTemperatureValue => 36 ; // Default reported temperature in case of wrong PowerShell value
96
+ }
97
+
98
+ public static class ConfigValues
99
+ {
100
+ public static string RefreshRateKey => "RefreshRate" ;
101
+ public static string FallbackTemperatureKey => "FallbackTemperature" ;
60
102
}
61
103
}
62
- }
104
+ }
0 commit comments