From fb6a230c503e86981a1ffc6ffc8f5f8c1109659b Mon Sep 17 00:00:00 2001 From: AlexanderDotH Date: Mon, 24 Apr 2023 18:49:04 +0200 Subject: [PATCH] Start of the new settings implementation --- .../Backend/Cache/CacheManager.cs | 4 +- .../Providers/NetEase/NetEaseCollector.cs | 2 +- .../Providers/NetEaseV2/NetEaseV2Collector.cs | 2 +- .../Providers/Textyl/TextylCollector.cs | 2 +- .../Providers/NetEase/NetEaseCollector.cs | 2 +- .../Providers/NetEaseV2/NetEaseV2Collector.cs | 2 +- OpenLyricsClient/Backend/Core.cs | 8 ++- .../Backend/Settings/ISettingSection.cs | 13 ++++ .../Settings/Sections/Lyrics/LyricsSection.cs | 70 +++++++++++++++++++ .../Settings/Sections/Lyrics/Structure.cs | 10 +++ .../Backend/Settings/SettingsHandler.cs | 32 +++++++++ .../Backend/Utils/JsonDeserializer.cs | 13 +++- 12 files changed, 150 insertions(+), 10 deletions(-) create mode 100644 OpenLyricsClient/Backend/Settings/ISettingSection.cs create mode 100644 OpenLyricsClient/Backend/Settings/Sections/Lyrics/LyricsSection.cs create mode 100644 OpenLyricsClient/Backend/Settings/Sections/Lyrics/Structure.cs create mode 100644 OpenLyricsClient/Backend/Settings/SettingsHandler.cs diff --git a/OpenLyricsClient/Backend/Cache/CacheManager.cs b/OpenLyricsClient/Backend/Cache/CacheManager.cs index 1d5f16e..c375d7c 100644 --- a/OpenLyricsClient/Backend/Cache/CacheManager.cs +++ b/OpenLyricsClient/Backend/Cache/CacheManager.cs @@ -60,7 +60,7 @@ private void ReadCache() string id = ifo.FileInfo.Name.Replace(CACHE_EXTENSION, string.Empty); JsonCacheData jsonLyricData = - new JsonDeserializer().Deserialize(FileUtils.ReadFileString(ifo.FileInfo)); + new JsonDeserializer().Deserialize(FileUtils.ReadFileString(ifo.FileInfo)); if (this._cache.Length + 1 > this._maxCapacity) continue; @@ -224,7 +224,7 @@ private CacheData TryGetDataFromDisk(SongRequestObject songRequestObject) return null; JsonCacheData jsonLyricData = - new JsonDeserializer().Deserialize(data); + new JsonDeserializer().Deserialize(data); GCHandle.Alloc(data).Free(); diff --git a/OpenLyricsClient/Backend/Collector/Lyrics/Providers/NetEase/NetEaseCollector.cs b/OpenLyricsClient/Backend/Collector/Lyrics/Providers/NetEase/NetEaseCollector.cs index e6a7035..e40fd3c 100644 --- a/OpenLyricsClient/Backend/Collector/Lyrics/Providers/NetEase/NetEaseCollector.cs +++ b/OpenLyricsClient/Backend/Collector/Lyrics/Providers/NetEase/NetEaseCollector.cs @@ -193,7 +193,7 @@ private async Task GetLyricsFromEndpoint(int songId) if (responseData.StatusCode == HttpStatusCode.OK) { - return new JsonDeserializer().Deserialize(responseData.GetContentAsString()); + return new JsonDeserializer().Deserialize(responseData.GetContentAsString()); } return null; diff --git a/OpenLyricsClient/Backend/Collector/Lyrics/Providers/NetEaseV2/NetEaseV2Collector.cs b/OpenLyricsClient/Backend/Collector/Lyrics/Providers/NetEaseV2/NetEaseV2Collector.cs index 9045790..6140317 100644 --- a/OpenLyricsClient/Backend/Collector/Lyrics/Providers/NetEaseV2/NetEaseV2Collector.cs +++ b/OpenLyricsClient/Backend/Collector/Lyrics/Providers/NetEaseV2/NetEaseV2Collector.cs @@ -145,7 +145,7 @@ private async Task GetLyricsFromEndpoint(int songId) if (responseData.StatusCode == HttpStatusCode.OK) { - return new JsonDeserializer().Deserialize(responseData.GetContentAsString()); + return new JsonDeserializer().Deserialize(responseData.GetContentAsString()); } return null; diff --git a/OpenLyricsClient/Backend/Collector/Lyrics/Providers/Textyl/TextylCollector.cs b/OpenLyricsClient/Backend/Collector/Lyrics/Providers/Textyl/TextylCollector.cs index fd2fb69..c51d3eb 100644 --- a/OpenLyricsClient/Backend/Collector/Lyrics/Providers/Textyl/TextylCollector.cs +++ b/OpenLyricsClient/Backend/Collector/Lyrics/Providers/Textyl/TextylCollector.cs @@ -71,7 +71,7 @@ public async Task FetchLyrics(SongRequestObject songReques return null; TextylLyricReponse[] reponse = - new JsonDeserializer().Deserialize(content); + new JsonDeserializer().Deserialize(content); if (reponse != null) return reponse; diff --git a/OpenLyricsClient/Backend/Collector/Song/Providers/NetEase/NetEaseCollector.cs b/OpenLyricsClient/Backend/Collector/Song/Providers/NetEase/NetEaseCollector.cs index efcd640..22444f4 100644 --- a/OpenLyricsClient/Backend/Collector/Song/Providers/NetEase/NetEaseCollector.cs +++ b/OpenLyricsClient/Backend/Collector/Song/Providers/NetEase/NetEaseCollector.cs @@ -172,7 +172,7 @@ private async Task SearchTrack(SongRequestObject songRequ if (responseData.StatusCode == HttpStatusCode.OK) { - return new JsonDeserializer().Deserialize(responseData.GetContentAsString()); + return new JsonDeserializer().Deserialize(responseData.GetContentAsString()); } return null; diff --git a/OpenLyricsClient/Backend/Collector/Song/Providers/NetEaseV2/NetEaseV2Collector.cs b/OpenLyricsClient/Backend/Collector/Song/Providers/NetEaseV2/NetEaseV2Collector.cs index 09da506..e607190 100644 --- a/OpenLyricsClient/Backend/Collector/Song/Providers/NetEaseV2/NetEaseV2Collector.cs +++ b/OpenLyricsClient/Backend/Collector/Song/Providers/NetEaseV2/NetEaseV2Collector.cs @@ -129,7 +129,7 @@ private async Task SearchTrack(SongRequestObject songRe if (responseData.StatusCode == HttpStatusCode.OK) { - return new JsonDeserializer().Deserialize(responseData.GetContentAsString()); + return new JsonDeserializer().Deserialize(responseData.GetContentAsString()); } return null; diff --git a/OpenLyricsClient/Backend/Core.cs b/OpenLyricsClient/Backend/Core.cs index 283c0d0..eff8c2a 100644 --- a/OpenLyricsClient/Backend/Core.cs +++ b/OpenLyricsClient/Backend/Core.cs @@ -14,6 +14,7 @@ using OpenLyricsClient.Backend.Handler.Song; using OpenLyricsClient.Backend.Helper; using OpenLyricsClient.Backend.Settings; +using OpenLyricsClient.Backend.Settings.Sections.Lyrics; using OpenLyricsClient.Backend.Structure.Enum; using TaskRegister = OpenLyricsClient.Backend.Overwrite.TaskRegister; @@ -33,6 +34,7 @@ class Core private Debugger _debugger; private SettingManager _settingManager; + private SettingsHandler _settingsHandler; private ServiceHandler _serviceHandler; private SongHandler _songHandler; @@ -83,7 +85,11 @@ public Core() this._settingManager = new SettingManager(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + string.Format("{0}OpenLyricsClient{0}", System.IO.Path.DirectorySeparatorChar)); - + + this._settingsHandler = new SettingsHandler( + System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + + string.Format("{0}OpenLyricsClient{0}", System.IO.Path.DirectorySeparatorChar)); + this._cacheManager = new CacheManager(10, TimeSpan.FromMinutes(5).Milliseconds); this._tokenCollector = new TokenCollector(); diff --git a/OpenLyricsClient/Backend/Settings/ISettingSection.cs b/OpenLyricsClient/Backend/Settings/ISettingSection.cs new file mode 100644 index 0000000..7fb9a40 --- /dev/null +++ b/OpenLyricsClient/Backend/Settings/ISettingSection.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; + +namespace OpenLyricsClient.Backend.Settings; + +public interface ISettingSection +{ + Task WriteToDisk(); + Task ReadFromDisk(); + T GetValue(string field); + Task SetValue(string field, T value); + JObject Defaults(); +} \ No newline at end of file diff --git a/OpenLyricsClient/Backend/Settings/Sections/Lyrics/LyricsSection.cs b/OpenLyricsClient/Backend/Settings/Sections/Lyrics/LyricsSection.cs new file mode 100644 index 0000000..1587bc3 --- /dev/null +++ b/OpenLyricsClient/Backend/Settings/Sections/Lyrics/LyricsSection.cs @@ -0,0 +1,70 @@ +using System.IO; +using System.Text.Json.Nodes; +using System.Threading.Tasks; +using DevBase.Api.Serializer; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using OpenLyricsClient.Backend.Structure.Enum; +using OpenLyricsClient.Backend.Utils; + +namespace OpenLyricsClient.Backend.Settings.Sections.Lyrics; + +public class LyricsSection : ISettingSection +{ + private FileInfo _file; + private JObject _data; + + public LyricsSection(string filePath) + { + this._file = new FileInfo(filePath); + } + + public async Task WriteToDisk() + { + await using FileStream stream = this._file.OpenWrite(); + await using StreamWriter writer = new StreamWriter(stream); + + await writer.WriteAsync(this._data?.ToString()); + } + + public async Task ReadFromDisk() + { + if (!this._file.Exists) + { + this._data = Defaults(); + await WriteToDisk(); + return; + } + + await using FileStream stream = this._file.OpenRead(); + using StreamReader reader = new StreamReader(stream); + + this._data = JObject.Parse(reader.ReadToEnd()); + + await stream.FlushAsync(); + + stream.Close(); + reader.Close(); + } + + public T GetValue(string field) + { + return this._data[field].Value(); + } + + public async Task SetValue(string field, T value) + { + this._data[field] = JToken.FromObject(value); + await WriteToDisk(); + } + + public JObject Defaults() + { + Structure structure = new Structure + { + Selection = EnumLyricsDisplayMode.KARAOKE + }; + + return new JsonDeserializer().Serialize(structure); + } +} \ No newline at end of file diff --git a/OpenLyricsClient/Backend/Settings/Sections/Lyrics/Structure.cs b/OpenLyricsClient/Backend/Settings/Sections/Lyrics/Structure.cs new file mode 100644 index 0000000..6a0a235 --- /dev/null +++ b/OpenLyricsClient/Backend/Settings/Sections/Lyrics/Structure.cs @@ -0,0 +1,10 @@ +using Newtonsoft.Json; +using OpenLyricsClient.Backend.Structure.Enum; + +namespace OpenLyricsClient.Backend.Settings.Sections.Lyrics; + +public class Structure +{ + [JsonProperty("Selection")] + public EnumLyricsDisplayMode Selection { get; set; } +} \ No newline at end of file diff --git a/OpenLyricsClient/Backend/Settings/SettingsHandler.cs b/OpenLyricsClient/Backend/Settings/SettingsHandler.cs new file mode 100644 index 0000000..18df292 --- /dev/null +++ b/OpenLyricsClient/Backend/Settings/SettingsHandler.cs @@ -0,0 +1,32 @@ +using System.Threading.Tasks; +using DevBase.Generics; +using OpenLyricsClient.Backend.Settings.Sections; +using OpenLyricsClient.Backend.Settings.Sections.Lyrics; +using OpenLyricsClient.Backend.Structure.Enum; + +namespace OpenLyricsClient.Backend.Settings; + +public class SettingsHandler +{ + private AList _sections; + + public SettingsHandler(string workingDirectory) + { + this._sections = new AList(); + + this._sections.Add(new LyricsSection(string.Format("{0}{1}", + workingDirectory, "lyrics-preferences.json"))); + + Task.Factory.StartNew(Initialize).GetAwaiter().GetResult(); + } + + public async Task Initialize() + { + this._sections.ForEach(async t=> await t.ReadFromDisk()); + } + + public T? Settings() where T : ISettingSection + { + return (T)this._sections.GetAsList().Find(s => s is T); + } +} \ No newline at end of file diff --git a/OpenLyricsClient/Backend/Utils/JsonDeserializer.cs b/OpenLyricsClient/Backend/Utils/JsonDeserializer.cs index d625bae..5ff25a2 100644 --- a/OpenLyricsClient/Backend/Utils/JsonDeserializer.cs +++ b/OpenLyricsClient/Backend/Utils/JsonDeserializer.cs @@ -1,9 +1,10 @@ using DevBase.Generics; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace OpenLyricsClient.Backend.Utils { - public class JsonDeserializer + public class JsonDeserializer { private JsonSerializerSettings _serializerSettings; private AList _errorList; @@ -23,11 +24,19 @@ public JsonDeserializer() }; } - public T Deserialize(string input) + public T Deserialize(string input) { return JsonConvert.DeserializeObject(input, this._serializerSettings); } + public JObject Serialize(object structure) + { + string serialized = JsonConvert.SerializeObject(structure, this._serializerSettings); + return JObject.Parse(serialized); + } + + public JObject ToJObject(T data) => JObject.FromObject(data); + public AList ErrorList { get => _errorList;