1
1
using System ;
2
2
using System . IO ;
3
- using UnityEngine ;
4
3
5
4
namespace BeatSaberOnline . Data
6
5
{
7
- [ Serializable ]
8
- public class Config
9
- {
10
- [ SerializeField ] private bool _autoStartLobby ;
11
- [ SerializeField ] private bool _isPublic ;
12
- [ SerializeField ] private int _maxLobbySize ;
13
- [ SerializeField ] private bool _noFailMode ;
14
- [ SerializeField ] private bool _avatarsInLobby ;
15
- [ SerializeField ] private bool _avatarsInGame ;
16
- [ SerializeField ] private int _networkQuality ;
17
- [ SerializeField ] private float _volume ;
18
-
19
- private static Config _instance ;
20
-
21
- private static FileInfo FileLocation { get ; } = new FileInfo ( $ "./UserData/{ Plugin . instance . Name } .json") ;
22
-
23
- public static void Init ( )
24
- {
25
- if ( ! Load ( ) )
26
- Create ( ) ;
27
- }
28
-
29
- public static void Reload ( )
30
- {
31
- if ( _instance . IsDirty )
32
- _instance . Save ( ) ;
33
-
34
- _instance = null ;
35
- Load ( ) ;
36
- }
37
-
38
- private static bool Load ( )
39
- {
40
- if ( _instance != null ) return false ;
41
- try
6
+ public class Config
7
+ {
8
+ public static Config Instance ;
9
+
10
+ public string FilePath { get ; }
11
+ public bool autoStartLobby = false ;
12
+ public bool isPublic = true ;
13
+ public int maxLobbySize = 5 ;
14
+ public bool avatarsInLobby = true ;
15
+ public bool avatarsInGame = true ;
16
+ public int networkQuality = 5 ;
17
+ public float volume = 20 ;
18
+
19
+ public bool AutoStartLobby { get { return autoStartLobby ; } set { autoStartLobby = value ; Save ( ) ; } }
20
+ public bool IsPublic { get { return isPublic ; } set { isPublic = value ; Save ( ) ; } }
21
+ public int MaxLobbySize { get { return maxLobbySize ; } set { maxLobbySize = value ; Save ( ) ; } }
22
+ public bool AvatarsInLobby { get { return avatarsInLobby ; } set { avatarsInLobby = value ; Save ( ) ; } }
23
+ public bool AvatarsInGame { get { return avatarsInGame ; } set { avatarsInGame = value ; Save ( ) ; } }
24
+ public int NetworkQuality { get { return networkQuality ; } set { networkQuality = value ; Save ( ) ; } }
25
+ public float Volume { get { return volume ; } set { volume = value ; Save ( ) ; } }
26
+
27
+ public event Action < Config > ConfigChangedEvent ;
28
+ private readonly FileSystemWatcher _configWatcher ;
29
+ private bool _saving ;
30
+
31
+ public Config ( string filePath )
32
+ {
33
+ Instance = this ;
34
+ FilePath = filePath ;
35
+
36
+ if ( ! Directory . Exists ( Path . GetDirectoryName ( FilePath ) ) )
37
+ Directory . CreateDirectory ( Path . GetDirectoryName ( FilePath ) ) ;
38
+ if ( File . Exists ( Path . Combine ( Environment . CurrentDirectory , "UserData/BeatSaberOnline.json" ) ) )
39
+ File . Delete ( Path . Combine ( Environment . CurrentDirectory , "UserData/BeatSaberOnline.json" ) ) ;
40
+
41
+ if ( File . Exists ( FilePath ) )
42
42
{
43
- FileLocation ? . Directory ? . Create ( ) ;
44
- _instance = JsonUtility . FromJson < Config > ( File . ReadAllText ( FileLocation . FullName ) ) ;
45
- _instance . MarkDirty ( ) ;
46
- _instance . Save ( ) ;
43
+ Load ( ) ;
47
44
}
48
- catch ( Exception )
45
+ Save ( ) ;
46
+ _configWatcher = new FileSystemWatcher ( Path . Combine ( Environment . CurrentDirectory , "UserData" ) )
49
47
{
50
- Logger . Error ( $ "Unable to load config @ { FileLocation . FullName } ") ;
51
- return false ;
52
- }
53
- return true ;
48
+ NotifyFilter = NotifyFilters . LastWrite ,
49
+ Filter = "BeatSaberOnline.ini" ,
50
+ EnableRaisingEvents = true
51
+ } ;
52
+ _configWatcher . Changed += ConfigWatcherOnChanged ;
54
53
}
55
54
56
- private static bool Create ( )
55
+ ~ Config ( )
57
56
{
58
- if ( _instance != null ) return false ;
59
- try
60
- {
61
- FileLocation ? . Directory ? . Create ( ) ;
62
- Instance . Save ( ) ;
63
- }
64
- catch ( Exception )
65
- {
66
- Logger . Error ( $ "Unable to create new config @ { FileLocation . FullName } ") ;
67
- return false ;
68
- }
69
- return true ;
57
+ _configWatcher . Changed -= ConfigWatcherOnChanged ;
70
58
}
71
59
72
- public static Config Instance
60
+ public void Load ( )
73
61
{
74
- get
75
- {
76
- if ( _instance == null )
77
- _instance = new Config ( ) ;
78
- return _instance ;
79
- }
80
- }
81
-
82
- private bool IsDirty { get ; set ; }
62
+ ConfigSerializer . LoadConfig ( this , FilePath ) ;
83
63
84
- public bool AutoStartLobby
85
- {
86
- get { return _autoStartLobby ; }
87
- set
88
- {
89
- _autoStartLobby = value ;
90
- MarkDirty ( ) ;
91
- }
64
+ CorrectConfigSettings ( ) ;
92
65
}
93
66
94
- public bool IsPublic
67
+ private void CorrectConfigSettings ( )
95
68
{
96
- get { return _isPublic ; }
97
- set
69
+ if ( volume > 20 )
98
70
{
99
- _isPublic = value ;
100
- MarkDirty ( ) ;
71
+ volume = 20 ;
101
72
}
102
- }
103
-
104
- public int MaxLobbySize
105
- {
106
- get { return _maxLobbySize ; }
107
- set
73
+ else if ( volume < 0 )
108
74
{
109
- _maxLobbySize = value ;
110
- MarkDirty ( ) ;
75
+ volume = 0 ;
111
76
}
112
- }
113
- public bool NoFailMode
114
- {
115
- get { return _noFailMode ; }
116
- set
77
+ if ( networkQuality > 5 )
117
78
{
118
- _noFailMode = value ;
119
- MarkDirty ( ) ;
120
- }
121
- }
122
- public bool AvatarsInLobby
123
- {
124
- get { return _avatarsInLobby ; }
125
- set
126
- {
127
- _avatarsInLobby = value ;
128
- MarkDirty ( ) ;
129
- }
130
- }
131
-
132
-
133
- public bool AvatarsInGame
134
- {
135
- get { return _avatarsInGame ; }
136
- set
137
- {
138
- _avatarsInGame = value ;
139
- MarkDirty ( ) ;
140
- }
141
- }
142
-
143
-
144
- public int NetworkQuality
145
- {
146
- get { return _networkQuality ; }
147
- set
148
- {
149
- if ( value > 5 ) { value = 5 ; }
150
- _networkQuality = value ;
151
- MarkDirty ( ) ;
152
- }
153
- }
154
-
155
- public float Volume
156
- {
157
- get { return _volume ; }
158
- set
79
+ networkQuality = 5 ;
80
+ } else if ( networkQuality < 0 )
159
81
{
160
- _volume = 20f ;
161
- MarkDirty ( ) ;
82
+ networkQuality = 0 ;
162
83
}
163
84
}
164
85
165
- Config ( )
86
+ public void Save ( bool callback = false )
166
87
{
167
- _autoStartLobby = false ;
168
- _isPublic = true ;
169
- _maxLobbySize = 5 ;
170
- _noFailMode = true ;
171
- _avatarsInLobby = true ;
172
- _avatarsInGame = true ;
173
- _networkQuality = 4 ;
174
- _volume = 20 ;
175
-
176
- IsDirty = true ;
88
+ if ( ! callback )
89
+ _saving = true ;
90
+ ConfigSerializer . SaveConfig ( this , FilePath ) ;
177
91
}
178
-
179
- public bool Save ( )
92
+ private void ConfigWatcherOnChanged ( object sender , FileSystemEventArgs fileSystemEventArgs )
180
93
{
181
- if ( ! IsDirty ) return false ;
182
- try
183
- {
184
-
185
- using ( var f = new StreamWriter ( FileLocation . FullName ) )
186
- {
187
- var json = JsonUtility . ToJson ( this , true ) ;
188
- f . Write ( json ) ;
189
- }
190
- MarkClean ( ) ;
191
- return true ;
192
- }
193
- catch ( Exception ex )
94
+ if ( _saving )
194
95
{
195
- Logger . Error ( $ "Unable to write the config file! Exception: { ex } " ) ;
196
- return false ;
96
+ _saving = false ;
97
+ return ;
197
98
}
198
- }
199
99
200
- void MarkDirty ( )
201
- {
202
- IsDirty = true ;
203
- Save ( ) ;
204
- }
205
-
206
- void MarkClean ( )
207
- {
208
- IsDirty = false ;
100
+ Load ( ) ;
101
+ ConfigChangedEvent ? . Invoke ( this ) ;
209
102
}
210
103
}
211
104
}
0 commit comments