Skip to content

Commit 185e9e8

Browse files
committed
Small app startup speedup (probably unnoticeable)
1 parent 269bcb8 commit 185e9e8

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/LightSteamAccountSwitcher.Core/Services/SettingsService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void Load()
2727
try
2828
{
2929
var json = File.ReadAllText(SettingsPath);
30-
Settings = JsonSerializer.Deserialize<AppSettings>(json) ?? new AppSettings();
30+
Settings = JsonSerializer.Deserialize(json, AppJsonContext.Default.AppSettings) ?? new AppSettings();
3131

3232
if (Settings.Version < CurrentVersion)
3333
{
@@ -46,7 +46,7 @@ public static void Save()
4646
{
4747
try
4848
{
49-
var json = JsonSerializer.Serialize(Settings, JsonOptions.Default);
49+
var json = JsonSerializer.Serialize(Settings, AppJsonContext.Default.AppSettings);
5050
File.WriteAllText(SettingsPath, json);
5151
}
5252
catch

src/LightSteamAccountSwitcher.Core/Services/VacStatusCache.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ public class VacStatusCache
1010
private readonly string _cacheFileName = Path.Combine(AppDataHelper.GetCachePath(), CacheFileName);
1111

1212
private List<SteamProfile> _cache = [];
13+
private bool _isLoaded;
1314

1415
public VacStatusCache()
1516
{
17+
}
18+
19+
private void EnsureLoaded()
20+
{
21+
if (_isLoaded) return;
1622
LoadCache();
23+
_isLoaded = true;
1724
}
1825

1926
public void LoadCache()
@@ -26,7 +33,7 @@ public void LoadCache()
2633
try
2734
{
2835
var json = File.ReadAllText(_cacheFileName);
29-
_cache = JsonSerializer.Deserialize<List<SteamProfile>>(json) ?? [];
36+
_cache = JsonSerializer.Deserialize(json, AppJsonContext.Default.ListSteamProfile) ?? [];
3037
}
3138
catch
3239
{
@@ -38,7 +45,7 @@ private void Save(IEnumerable<SteamProfile> profiles)
3845
{
3946
try
4047
{
41-
var json = JsonSerializer.Serialize(profiles.ToList(), JsonOptions.Default);
48+
var json = JsonSerializer.Serialize(profiles.ToList(), AppJsonContext.Default.ListSteamProfile);
4249
File.WriteAllText(_cacheFileName, json);
4350
}
4451
catch (Exception ex)
@@ -49,6 +56,7 @@ private void Save(IEnumerable<SteamProfile> profiles)
4956

5057
public void Update(string steamId, bool vac, bool limited)
5158
{
59+
EnsureLoaded();
5260
var existing = _cache.FirstOrDefault(x => x.SteamId64 == steamId);
5361

5462
if (existing != null)
@@ -73,17 +81,21 @@ public void Update(string steamId, bool vac, bool limited)
7381

7482
public SteamProfile? Get(string steamId64)
7583
{
84+
EnsureLoaded();
7685
return _cache.FirstOrDefault(x => x.SteamId64 == steamId64);
7786
}
7887

7988
public bool IsCacheValid(string steamId)
8089
{
90+
EnsureLoaded();
8191
var profile = _cache.FirstOrDefault(x => x.SteamId64 == steamId);
8292
return profile != null && DateTime.Now - profile.LastUpdated < TimeSpan.FromDays(1);
8393
}
8494

8595
public void ClearCache()
8696
{
8797
File.Delete(_cacheFileName);
98+
_cache.Clear();
99+
_isLoaded = true;
88100
}
89101
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Text.Json.Serialization;
2+
using LightSteamAccountSwitcher.Core.Models;
3+
using LightSteamAccountSwitcher.Core.Services;
4+
5+
namespace LightSteamAccountSwitcher.Core.Utils;
6+
7+
[JsonSerializable(typeof(AppSettings))]
8+
[JsonSerializable(typeof(List<SteamProfile>))]
9+
public partial class AppJsonContext : JsonSerializerContext
10+
{
11+
}

src/LightSteamAccountSwitcher/LightSteamAccountSwitcher.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<PublishSingleFile>true</PublishSingleFile>
1212
<SelfContained>false</SelfContained>
13+
<EnableCompressionInSingleFile>false</EnableCompressionInSingleFile>
1314
<PublishReadyToRun>true</PublishReadyToRun>
1415
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
1516
<ApplicationIcon>Assets\LSAS.ico</ApplicationIcon>

0 commit comments

Comments
 (0)