Skip to content

Commit 454289f

Browse files
committed
Change config over to ini file and remove json format
1 parent d16478e commit 454289f

File tree

2 files changed

+69
-176
lines changed

2 files changed

+69
-176
lines changed

BeatSaberOnline/Plugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private void Init()
2929
instance = this;
3030

3131
Logger.Init();
32-
Config.Init();
32+
new Config($"./UserData/{Name}.ini");
3333
Sprites.Init();
3434

3535
SceneManager.sceneLoaded += SceneLoaded;

BeatSaberOnline/Utils/Config.cs

Lines changed: 68 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -1,211 +1,104 @@
11
using System;
22
using System.IO;
3-
using UnityEngine;
43

54
namespace BeatSaberOnline.Data
65
{
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))
4242
{
43-
FileLocation?.Directory?.Create();
44-
_instance = JsonUtility.FromJson<Config>(File.ReadAllText(FileLocation.FullName));
45-
_instance.MarkDirty();
46-
_instance.Save();
43+
Load();
4744
}
48-
catch (Exception)
45+
Save();
46+
_configWatcher = new FileSystemWatcher(Path.Combine(Environment.CurrentDirectory, "UserData"))
4947
{
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;
5453
}
5554

56-
private static bool Create()
55+
~Config()
5756
{
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;
7058
}
7159

72-
public static Config Instance
60+
public void Load()
7361
{
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);
8363

84-
public bool AutoStartLobby
85-
{
86-
get { return _autoStartLobby; }
87-
set
88-
{
89-
_autoStartLobby = value;
90-
MarkDirty();
91-
}
64+
CorrectConfigSettings();
9265
}
9366

94-
public bool IsPublic
67+
private void CorrectConfigSettings()
9568
{
96-
get { return _isPublic; }
97-
set
69+
if (volume > 20)
9870
{
99-
_isPublic = value;
100-
MarkDirty();
71+
volume = 20;
10172
}
102-
}
103-
104-
public int MaxLobbySize
105-
{
106-
get { return _maxLobbySize; }
107-
set
73+
else if (volume < 0)
10874
{
109-
_maxLobbySize = value;
110-
MarkDirty();
75+
volume = 0;
11176
}
112-
}
113-
public bool NoFailMode
114-
{
115-
get { return _noFailMode; }
116-
set
77+
if (networkQuality > 5)
11778
{
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)
15981
{
160-
_volume = 20f;
161-
MarkDirty();
82+
networkQuality = 0;
16283
}
16384
}
16485

165-
Config()
86+
public void Save(bool callback = false)
16687
{
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);
17791
}
178-
179-
public bool Save()
92+
private void ConfigWatcherOnChanged(object sender, FileSystemEventArgs fileSystemEventArgs)
18093
{
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)
19495
{
195-
Logger.Error($"Unable to write the config file! Exception: {ex}");
196-
return false;
96+
_saving = false;
97+
return;
19798
}
198-
}
19999

200-
void MarkDirty()
201-
{
202-
IsDirty = true;
203-
Save();
204-
}
205-
206-
void MarkClean()
207-
{
208-
IsDirty = false;
100+
Load();
101+
ConfigChangedEvent?.Invoke(this);
209102
}
210103
}
211104
}

0 commit comments

Comments
 (0)