Skip to content

Commit

Permalink
Implement caching of web resources and news
Browse files Browse the repository at this point in the history
  • Loading branch information
Drombeys committed Apr 27, 2024
1 parent 400cce0 commit 0e894bf
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
61 changes: 48 additions & 13 deletions src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Diagnostics;

using ImeSense.Launchers.Belarus.Core.Helpers;
Expand Down Expand Up @@ -34,6 +33,10 @@ public async Task InitializeAsync(ISplashScreenManager splashScreenManager)
var stopwatch = new Stopwatch();
stopwatch.Start();

if (!Directory.Exists(DirectoryStorage.LauncherCache)) {
Directory.CreateDirectory(DirectoryStorage.LauncherCache);
}

splashScreenManager.UpdateInformation(new InformationMessage(
_localeManager.GetStringByKey("LocalizedStrings.Loading"),
_localeManager.GetStringByKey("LocalizedStrings.AccessingRepository")));
Expand Down Expand Up @@ -62,12 +65,24 @@ public async Task InitializeAsync(ISplashScreenManager splashScreenManager)
_launcherStorage.IsGameReleaseCurrent = await IsGameReleaseCurrentAsync(splashScreenManager.CancellationToken);
_launcherStorage.IsUserAuthorized = File.Exists(PathStorage.LauncherSetting);

if (_launcherStorage.IsUserAuthorized) {
await Task.Factory.StartNew(() => LoadNewsAsync(locale, splashScreenManager.CancellationToken));
if (_launcherStorage.IsGameReleaseCurrent) {
var contentNews = await LocaleLoadCacheAsync<LangNewsContent>(PathStorage.NewsCache) ?? [];
if (contentNews.Any(x => x.Locale != null && x.Locale.Key.Equals(_userManager.UserSettings.Locale.Key))) {

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build updater (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build updater (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build updater (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build updater (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build updater (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build updater (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build updater (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build updater (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build updater (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build updater (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build updater (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build updater (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build hasher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build hasher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build hasher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build hasher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build hasher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build hasher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build hasher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build hasher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build hasher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build hasher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build hasher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build hasher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Release, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Build launcher (windows-2022, Debug, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Publish updater (windows-2022, Release, win-x64, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Publish updater (windows-2022, Release, win-x64, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Publish updater (windows-2022, Release, win-x64, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Publish launcher (windows-2022, Release, win-x64, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Publish launcher (windows-2022, Release, win-x64, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Publish launcher (windows-2022, Release, win-x64, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Publish hasher (windows-2022, Release, win-x64, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Publish hasher (windows-2022, Release, win-x64, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Publish hasher (windows-2022, Release, win-x64, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Publish launcher (windows-2022, Release, win-x64, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Publish launcher (windows-2022, Release, win-x64, net8.0)

Dereference of a possibly null reference.

Check warning on line 70 in src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs

View workflow job for this annotation

GitHub Actions / Publish launcher (windows-2022, Release, win-x64, net8.0)

Dereference of a possibly null reference.
_launcherStorage.NewsContents = new(contentNews);
} else {
await LoadRemoteContent(splashScreenManager, locale);
}

var contentRes = await LocaleLoadCacheAsync<WebResource>(PathStorage.WebResourcesCache);
if (contentRes is not null && contentRes.Count != 0) {
_launcherStorage.WebResources = new(contentRes);
} else {
await Task.Factory.StartNew(() => RemoteLoadWebResourcesAsync(cancellationToken: splashScreenManager.CancellationToken));
}
} else {
await Task.Factory.StartNew(() => LoadNewsAsync(cancellationToken: splashScreenManager.CancellationToken));
await LoadRemoteContent(splashScreenManager, locale);
await Task.Factory.StartNew(() => RemoteLoadWebResourcesAsync(cancellationToken: splashScreenManager.CancellationToken));
}
await Task.Factory.StartNew(() => LoadWebResourcesAsync(cancellationToken: splashScreenManager.CancellationToken));
} else {
if (_launcherStorage.IsUserAuthorized) {
_launcherStorage.NewsContents = new(LoadErrorNews(locale) ?? []);
Expand All @@ -87,6 +102,30 @@ public async Task InitializeAsync(ISplashScreenManager splashScreenManager)
}
}

private async Task LoadRemoteContent(ISplashScreenManager splashScreenManager, Locale? locale)
{
if (_launcherStorage.IsUserAuthorized) {
await Task.Factory.StartNew(() => RemoteLoadNewsAsync(locale, splashScreenManager.CancellationToken), splashScreenManager.CancellationToken);
} else {
await Task.Factory.StartNew(() => RemoteLoadNewsAsync(cancellationToken: splashScreenManager.CancellationToken), splashScreenManager.CancellationToken);
}
}

private async Task<List<T>?> LocaleLoadCacheAsync<T>(string cachePath, CancellationToken cancellationToken = default)
{
try {
if (!File.Exists(cachePath)) {
return null;
}

return await FileDataHelper.LoadDataAsync<List<T>>(cachePath, cancellationToken);
} catch (Exception ex) {
_logger.LogError("{Message}", ex.Message);
_logger.LogError("{StackTrace}", ex.StackTrace);
throw;
}
}

private List<LangNewsContent>? LoadErrorNews(Locale? locale = null)
{
// News in all languages
Expand Down Expand Up @@ -132,7 +171,6 @@ private async Task<bool> CheckGitHubConnectionAsync(CancellationToken cancellati
}
}


private async Task<bool> IsLauncherReleaseCurrentAsync(CancellationToken cancellationToken = default)
{
var tags = await _gitStorageApiService.GetTagsAsync(UriStorage.LauncherApiUri, cancellationToken);
Expand Down Expand Up @@ -161,10 +199,6 @@ private async Task<bool> IsGameReleaseCurrentAsync(CancellationToken cancellatio
{
var gitStorageRelease = _launcherStorage.GitHubRelease;

if (!Directory.Exists(DirectoryStorage.LauncherCache)) {
Directory.CreateDirectory(DirectoryStorage.LauncherCache);
}

if (File.Exists(PathStorage.CurrentRelease)) {
var releaseComparer = gitStorageRelease != null && await _releaseComparerService.IsComparerAsync(gitStorageRelease, cancellationToken);
if (!releaseComparer) {
Expand Down Expand Up @@ -195,13 +229,13 @@ public void InitializeLocale()
_localeManager.SetLocale(userSettings.Locale.Key);
}

private async Task LoadWebResourcesAsync(CancellationToken cancellationToken = default)
private async Task RemoteLoadWebResourcesAsync(CancellationToken cancellationToken = default)
{
try {
var contents = await _gitStorageApiService
.DownloadJsonAsync<IEnumerable<WebResource>>(FileNameStorage.WebResources, UriStorage.BelarusApiUri, cancellationToken);

if (contents != null) {
await FileSystemHelper.WriteReleaseAsync(contents, Path.Combine(DirectoryStorage.LauncherCache, FileNameStorage.WebResources), cancellationToken);
_launcherStorage.WebResources = new(contents);
}
} catch (Exception ex) {
Expand All @@ -211,7 +245,7 @@ private async Task LoadWebResourcesAsync(CancellationToken cancellationToken = d
}
}

private async Task LoadNewsAsync(Locale? locale = null, CancellationToken cancellationToken = default)
private async Task RemoteLoadNewsAsync(Locale? locale = null, CancellationToken cancellationToken = default)
{
// News in all languages
var allNews = new List<LangNewsContent>();
Expand All @@ -233,6 +267,7 @@ private async Task LoadNewsAsync(Locale? locale = null, CancellationToken cancel
_logger.LogError("{StackTrace}", ex.StackTrace);
}

await FileSystemHelper.WriteReleaseAsync(allNews, Path.Combine(DirectoryStorage.LauncherCache, "News.json"), cancellationToken);
_launcherStorage.NewsContents = new(allNews);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ namespace ImeSense.Launchers.Belarus.Core;
[JsonSerializable(typeof(NewsContent))]
[JsonSerializable(typeof(IEnumerable<NewsContent>))]
[JsonSerializable(typeof(IEnumerable<LangNewsContent>))]
[JsonSerializable(typeof(List<LangNewsContent>))]
[JsonSerializable(typeof(Tag))]
[JsonSerializable(typeof(IEnumerable<Tag>))]
[JsonSerializable(typeof(UserSettings))]
[JsonSerializable(typeof(WebResource))]
[JsonSerializable(typeof(WebResource[]))]
[JsonSerializable(typeof(List<WebResource>))]
[JsonSerializable(typeof(IEnumerable<WebResource>))]
public partial class SourceGenerationContext : JsonSerializerContext
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ public static class FileNameStorage
public static string GameSetting => "user.ltx";
public static string LauncherSetting => "sblauncher.json";
public static string CurrentRelease => "CurrentRelease.json";

public static string LegacyHash => "hash.json";
public static string LegacyNews => "news.json";
public static string NewsContent => "News.json";
public static string NewsContentRus => "news_content_rus.json";
public static string NewsContentEng => "news_content_eng.json";

public static string LegacyHash => "hash.json";
public static string LegacyNews => "news.json";
}
2 changes: 2 additions & 0 deletions src/ImeSense.Launchers.Belarus.Core/Storage/PathStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ public static class PathStorage
public static string LauncherSetting => Path.Combine(DirectoryStorage.AppData, FileNameStorage.LauncherSetting);
public static string CurrentRelease => Path.Combine(DirectoryStorage.LauncherCache, FileNameStorage.CurrentRelease);
public static string GameUser => Path.Combine(DirectoryStorage.AppData, FileNameStorage.GameSetting);
public static string NewsCache => Path.Combine(DirectoryStorage.LauncherCache, FileNameStorage.NewsContent);
public static string WebResourcesCache => Path.Combine(DirectoryStorage.LauncherCache, FileNameStorage.WebResources);
}

0 comments on commit 0e894bf

Please sign in to comment.