@@ -7,7 +7,7 @@ public static class Config { // stores dynamic configuration, including
77 const string PATH = "settings" ;
88 static Dictionary < string , string > variables = new Dictionary < string , string > ( ) ;
99
10- const int settingsNum = 10 ; // currently - ProgressiveScan, StartInTray + special buttons
10+ const int settingsNum = 11 ; // currently - ProgressiveScan, StartInTray + special buttons
1111
1212 public static string GetDefaultValue ( string s ) {
1313 switch ( s ) {
@@ -21,15 +21,36 @@ public static string GetDefaultValue(string s) {
2121 return "0" ;
2222 }
2323
24+ // Helper function to count how many lines are in a file
25+ // https://www.dotnetperls.com/line-count
26+ static long CountLinesInFile ( string f ) {
27+ // Zero based count
28+ long count = - 1 ;
29+ using ( StreamReader r = new StreamReader ( f ) ) {
30+ string line ;
31+ while ( ( line = r . ReadLine ( ) ) != null ) {
32+ count ++ ;
33+ }
34+ }
35+ return count ;
36+ }
37+
2438 public static void Init ( List < KeyValuePair < string , float [ ] > > caliData ) {
25- foreach ( string s in new string [ ] { "ProgressiveScan" , "StartInTray" , "capture" , "home" , "sl_l" , "sl_r" , "sr_l" , "sr_r" , "reset_mouse" , "active_gyro" } )
39+ foreach ( string s in new string [ ] { "ProgressiveScan" , "StartInTray" , "capture" , "home" , "sl_l" , "sl_r" , "sr_l" , "sr_r" , "shake" , " reset_mouse", "active_gyro" } )
2640 variables [ s ] = GetDefaultValue ( s ) ;
2741
2842 if ( File . Exists ( PATH ) ) {
29- int lineNO = 0 ;
43+
44+ // Reset settings file if old settings
45+ if ( CountLinesInFile ( PATH ) < settingsNum ) {
46+ File . Delete ( PATH ) ;
47+ Init ( caliData ) ;
48+ return ;
49+ }
50+
3051 using ( StreamReader file = new StreamReader ( PATH ) ) {
3152 string line = String . Empty ;
32-
53+ int lineNO = 0 ;
3354 while ( ( line = file . ReadLine ( ) ) != null ) {
3455 string [ ] vs = line . Split ( ) ;
3556 try {
@@ -52,14 +73,6 @@ public static void Init(List<KeyValuePair<string, float[]>> caliData) {
5273 } catch { }
5374 lineNO ++ ;
5475 }
55-
56-
57- }
58-
59- // if old settings
60- if ( lineNO < settingsNum ) {
61- File . Delete ( PATH ) ;
62- Init ( caliData ) ;
6376 }
6477 } else {
6578 using ( StreamWriter file = new StreamWriter ( PATH ) ) {
0 commit comments