Replace single cache with two separate caches for Games and DLC

This commit is contained in:
Jeddunk 2021-01-13 18:57:10 +01:00
parent 46d894c9c4
commit cf4c5ea551
2 changed files with 121 additions and 70 deletions

View File

@ -28,34 +28,58 @@ namespace GoldbergGUI.Core.Models
} }
} }
public bool CompareName(string value) public bool CompareName(string value) => _comparableName.Equals(value);
{
return _comparableName.Equals(value); public AppType type { get; set; }
}
public override string ToString() public override string ToString()
{ {
return $"{AppId}={Name}"; return $"{AppId}={Name}";
} }
[JsonPropertyName("last_modified")] public long LastModified { get; set; }
[JsonPropertyName("price_change_number")]
public long PriceChangeNumber { get; set; }
} }
public class AppList public class AppList
{ {
[JsonPropertyName("apps")] public List<SteamApp> Apps { get; set; } [JsonPropertyName("apps")] public List<SteamApp> Apps { get; set; }
[JsonPropertyName("have_more_results")]
public bool HaveMoreResults { get; set; }
[JsonPropertyName("last_appid")] public long LastAppid { get; set; }
} }
public class SteamApps public class SteamApps
{ {
[JsonPropertyName("applist")] public AppList AppList { get; set; } public virtual AppList AppList { get; set; }
} }
public static class AppType
public class SteamAppsV2 : SteamApps
{ {
public const string Game = "game"; [JsonPropertyName("applist")] public override AppList AppList { get; set; }
public const string DLC = "dlc"; }
public const string Music = "music";
public const string Demo = "demo"; public class SteamAppsV1 : SteamApps
public const string Ad = "advertising"; {
public const string Mod = "mod"; [JsonPropertyName("response")] public override AppList AppList { get; set; }
public const string Video = "video"; }
public class AppType
{
private AppType(string value) => Value = value;
public string Value { get; }
public static AppType Game => new AppType("game");
public static AppType DLC => new AppType("dlc");
public static AppType Music => new AppType("music");
public static AppType Demo => new AppType("demo");
public static AppType Ad => new AppType("advertising");
public static AppType Mod => new AppType("mod");
public static AppType Video => new AppType("video");
} }
} }

View File

@ -32,26 +32,29 @@ namespace GoldbergGUI.Core.Services
public class SteamService : ISteamService public class SteamService : ISteamService
{ {
// ReSharper disable StringLiteralTypo // ReSharper disable StringLiteralTypo
private readonly List<(string filename, string uri, string type)> _caches = new List<(string, string, string)> private readonly List<(string filename, string uri, Type jsonType, AppType type)> _cacheSetup =
{ new List<(string, string, Type, AppType)>
( {
"steamapps.json", (
"https://api.steampowered.com/ISteamApps/GetAppList/v2/", "steamapps_games.json",
null "https://api.steampowered.com/IStoreService/GetAppList/v1/" +
), "?max_results=50000" +
( "&include_games=1" +
"steamapps_games.json", "&key=" + Secrets.SteamWebApiKey(),
"https://api.steampowered.com/IStoreService/GetAppList/v1/?include_games=1&key=" + typeof(SteamAppsV1),
Secrets.SteamWebApiKey(), AppType.Game
AppType.Game ),
), (
( "steamapps_dlc.json",
"steamapps_dlc.json", "https://api.steampowered.com/IStoreService/GetAppList/v1/" +
"https://api.steampowered.com/IStoreService/GetAppList/v1/?include_games=0&include_dlc=1&key=" + "?max_results=50000" +
Secrets.SteamWebApiKey(), "&include_games=0" +
AppType.DLC "&include_dlc=1" +
) "&key=" + Secrets.SteamWebApiKey(),
}; typeof(SteamAppsV1),
AppType.DLC
)
};
// ReSharper enable StringLiteralTypo // ReSharper enable StringLiteralTypo
private static readonly Secrets Secrets = new Secrets(); private static readonly Secrets Secrets = new Secrets();
@ -60,30 +63,69 @@ namespace GoldbergGUI.Core.Services
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/87.0.4280.88 Safari/537.36"; "Chrome/87.0.4280.88 Safari/537.36";
private HashSet<SteamApp> _cache = new HashSet<SteamApp>(); //private HashSet<SteamApp> _cacheAll = new HashSet<SteamApp>();
//private HashSet<SteamApp> _cacheGame = new HashSet<SteamApp>();
//private HashSet<SteamApp> _cacheDlc = new HashSet<SteamApp>();
private Dictionary<string, HashSet<SteamApp>> _caches = new Dictionary<string, HashSet<SteamApp>>();
private IMvxLog _log; private IMvxLog _log;
public async Task Initialize(IMvxLog log) public async Task Initialize(IMvxLog log)
{ {
var path = _caches.First().filename; //var (path, uri, jsonType, appType) = _caches[0];
var uri = _caches.First().uri; static SteamApps SteamApps(Type type, string s)
_log = log;
_log.Info("Updating cache...");
var updateNeeded = DateTime.Now.Subtract(File.GetLastWriteTimeUtc(path)).TotalDays >= 1;
var cacheString = await GetCache(updateNeeded, uri, path).ConfigureAwait(false);
SteamApps steamApps;
try
{ {
steamApps = JsonSerializer.Deserialize<SteamApps>(cacheString); SteamApps steamApps1 = null;
} if (type == typeof(SteamAppsV1))
catch (JsonException) steamApps1 = JsonSerializer.Deserialize<SteamAppsV1>(s);
{ else if (type == typeof(SteamAppsV2))
cacheString = await GetCache(true, uri, path).ConfigureAwait(false); steamApps1 = JsonSerializer.Deserialize<SteamAppsV2>(s);
steamApps = JsonSerializer.Deserialize<SteamApps>(cacheString); return steamApps1;
} }
_cache = new HashSet<SteamApp>(steamApps.AppList.Apps); foreach (var (path, uri, jsonType, appType) in _cacheSetup)
_log.Info("Loaded cache into memory!"); {
_log = log;
_log.Info("Updating cache...");
var updateNeeded = DateTime.Now.Subtract(File.GetLastWriteTimeUtc(path)).TotalDays >= 1;
var cacheString = await GetCache(updateNeeded, uri, path).ConfigureAwait(false);
SteamApps steamApps = null;
try
{
steamApps = SteamApps(jsonType, cacheString);
}
catch (JsonException)
{
_log.Error("Local cache broken, forcing update...");
cacheString = await GetCache(true, uri, path).ConfigureAwait(false);
steamApps = SteamApps(jsonType, cacheString);
}
try
{
var cache = new HashSet<SteamApp>(steamApps.AppList.Apps);
if (appType != null)
{
var cacheEdited = new HashSet<SteamApp>();
foreach (var steamApp in cache)
{
steamApp.type = appType;
cacheEdited.Add(steamApp);
}
cache = cacheEdited;
}
_caches.Add(path, cache);
_log.Info("Loaded cache into memory!");
}
catch (NullReferenceException e)
{
Console.WriteLine(e);
throw;
}
}
} }
private async Task<string> GetCache(bool updateNeeded, string steamUri, string cachePath) private async Task<string> GetCache(bool updateNeeded, string steamUri, string cachePath)
@ -113,32 +155,17 @@ namespace GoldbergGUI.Core.Services
public IEnumerable<SteamApp> GetListOfAppsByName(string name) public IEnumerable<SteamApp> GetListOfAppsByName(string name)
{ {
var listOfAppsByName = _cache.Search(x => x.Name) var listOfAppsByName = _caches["steamapps_games.json"].Search(x => x.Name)
.SetCulture(StringComparison.OrdinalIgnoreCase) .SetCulture(StringComparison.OrdinalIgnoreCase)
.ContainingAll(name.Split(' ')); .ContainingAll(name.Split(' '));
return listOfAppsByName; return listOfAppsByName;
/*var filteredList = new HashSet<SteamApp>();
foreach (var steamApp in listOfAppsByName)
{
try
{
var task = AppDetails.GetAsync(steamApp.AppId);
var details = await task.ConfigureAwait(false);
if (details?.Type != null && details.Type == AppType.Game) filteredList.Add(steamApp);
}
catch (Exception e)
{
_log.Debug($"{e.GetType()}: {steamApp}");
}
}
return filteredList;*/
} }
public SteamApp GetAppByName(string name) public SteamApp GetAppByName(string name)
{ {
_log.Info($"Trying to get app {name}"); _log.Info($"Trying to get app {name}");
var comparableName = Regex.Replace(name, Misc.SpecialCharsRegex, "").ToLower(); var comparableName = Regex.Replace(name, Misc.SpecialCharsRegex, "").ToLower();
var app = _cache.FirstOrDefault(x => x.CompareName(comparableName)); var app = _caches["steamapps_games.json"].FirstOrDefault(x => x.CompareName(comparableName));
if (app != null) _log.Info($"Successfully got app {app}"); if (app != null) _log.Info($"Successfully got app {app}");
return app; return app;
} }
@ -146,7 +173,7 @@ namespace GoldbergGUI.Core.Services
public SteamApp GetAppById(int appid) public SteamApp GetAppById(int appid)
{ {
_log.Info($"Trying to get app with ID {appid}"); _log.Info($"Trying to get app with ID {appid}");
var app = _cache.FirstOrDefault(x => x.AppId.Equals(appid)); var app = _caches["steamapps_games.json"].FirstOrDefault(x => x.AppId.Equals(appid));
if (app != null) _log.Info($"Successfully got app {app}"); if (app != null) _log.Info($"Successfully got app {app}");
return app; return app;
} }
@ -159,11 +186,11 @@ namespace GoldbergGUI.Core.Services
{ {
var task = AppDetails.GetAsync(steamApp.AppId); var task = AppDetails.GetAsync(steamApp.AppId);
var steamAppDetails = await task.ConfigureAwait(true); var steamAppDetails = await task.ConfigureAwait(true);
if (steamAppDetails.Type == AppType.Game) if (steamAppDetails.Type == AppType.Game.Value)
{ {
steamAppDetails.DLC.ForEach(x => steamAppDetails.DLC.ForEach(x =>
{ {
var result = _cache.FirstOrDefault(y => y.AppId.Equals(x)) ?? var result = _caches["steamapps_dlc.json"].FirstOrDefault(y => y.AppId.Equals(x)) ??
new SteamApp {AppId = x, Name = $"Unknown DLC {x}"}; new SteamApp {AppId = x, Name = $"Unknown DLC {x}"};
dlcList.Add(result); dlcList.Add(result);
}); });