diff --git a/OpenLyricsClient/Backend/Core.cs b/OpenLyricsClient/Backend/Core.cs index dc17a7f..575d966 100644 --- a/OpenLyricsClient/Backend/Core.cs +++ b/OpenLyricsClient/Backend/Core.cs @@ -9,6 +9,7 @@ using OpenLyricsClient.Backend.Debugger; using OpenLyricsClient.Backend.Events.EventHandler; using OpenLyricsClient.Backend.Handler.Artwork; +using OpenLyricsClient.Backend.Handler.License; using OpenLyricsClient.Backend.Handler.Lyrics; using OpenLyricsClient.Backend.Handler.Services; using OpenLyricsClient.Backend.Handler.Song; @@ -37,6 +38,7 @@ class Core private SongHandler _songHandler; private LyricHandler _lyricHandler; private ArtworkHandler _artworkHandler; + private LicenseHandler _licenseHandler; private CacheManager _cacheManager; @@ -88,6 +90,8 @@ public Core() this._settingsHandler = new SettingsHandler(workingDirectory); + this._licenseHandler = new LicenseHandler(); + this._cacheManager = new CacheManager(workingDirectory, 10, TimeSpan.FromMinutes(5).Milliseconds); this._tokenCollector = new TokenCollector(); diff --git a/OpenLyricsClient/Backend/Handler/License/LicenseHandler.cs b/OpenLyricsClient/Backend/Handler/License/LicenseHandler.cs new file mode 100644 index 0000000..c06966d --- /dev/null +++ b/OpenLyricsClient/Backend/Handler/License/LicenseHandler.cs @@ -0,0 +1,67 @@ +using System; +using System.Threading.Tasks; +using DevBase.Api.Apis.OpenLyricsClient.Structure.Enum; +using DevBase.Api.Apis.OpenLyricsClient.Structure.Json; +using DevBase.Async.Task; +using OpenLyricsClient.Backend.Structure.Enum; + +namespace OpenLyricsClient.Backend.Handler.License; + +public class LicenseHandler : IHandler +{ + private TaskSuspensionToken _refreshSuspensionToken; + + private bool _disposed; + + private JsonOpenLyricsClientSubscriptionModel _license; + private DevBase.Api.Apis.OpenLyricsClient.OpenLyricsClient _openLyricsClientApi; + + public LicenseHandler() + { + this._disposed = false; + + this._openLyricsClientApi = new DevBase.Api.Apis.OpenLyricsClient.OpenLyricsClient(Core.INSTANCE.Sealing.ServerPublicKey); + + JsonOpenLyricsClientSubscriptionModel model = new JsonOpenLyricsClientSubscriptionModel + { + Model = EnumSubscriptions.OPENLYRICSCLIENT_STANDARD + }; + this._license = model; + + Core.INSTANCE.TaskRegister.Register( + out _refreshSuspensionToken, + new Task(async () => await RefreshLicense(), Core.INSTANCE.CancellationTokenSource.Token, TaskCreationOptions.LongRunning), + EnumRegisterTypes.REFRESH_LICENSE); + } + + public async Task RefreshLicense() + { + while (!this._disposed) + { + string userID = Core.INSTANCE.SettingsHandler.Settings()!.GetValue("UserID"); + string userSecret = Core.INSTANCE.SettingsHandler.Settings()!.GetValue("UserSecret"); + + JsonOpenLyricsClientSubscription subscription = new JsonOpenLyricsClientSubscription + { + UserID = userID, + UserSecret = userSecret + }; + + JsonOpenLyricsClientSubscriptionModel model = await this._openLyricsClientApi.CheckSubscription(subscription); + this._license = model; + + await Task.Delay((int)TimeSpan.FromMinutes(1).TotalMilliseconds); + } + } + + public void Dispose() + { + this._disposed = true; + Core.INSTANCE.TaskRegister.Kill(EnumRegisterTypes.REFRESH_LICENSE); + } + + public JsonOpenLyricsClientSubscriptionModel License + { + get => _license; + } +} \ No newline at end of file diff --git a/OpenLyricsClient/Backend/Structure/Enum/EnumRegisterTypes.cs b/OpenLyricsClient/Backend/Structure/Enum/EnumRegisterTypes.cs index cf4cfee..d87fb52 100644 --- a/OpenLyricsClient/Backend/Structure/Enum/EnumRegisterTypes.cs +++ b/OpenLyricsClient/Backend/Structure/Enum/EnumRegisterTypes.cs @@ -13,6 +13,7 @@ public enum EnumRegisterTypes APPLY_ARTWORK_TO_SONG, COLLECT_TOKENS, SYNC_SCROLLER, - GLOBAL_TICK + GLOBAL_TICK, + REFRESH_LICENSE } }