diff --git a/auto-creamapi/Models/SteamAppModel.cs b/auto-creamapi/Models/SteamAppModel.cs index 5c85687..3f74258 100644 --- a/auto-creamapi/Models/SteamAppModel.cs +++ b/auto-creamapi/Models/SteamAppModel.cs @@ -1,24 +1,36 @@ using System.Collections.Generic; using System.Text.Json.Serialization; +using System.Text.RegularExpressions; +using auto_creamapi.Utils; namespace auto_creamapi.Models { public class SteamApp { + private string _name; + private string _comparableName; [JsonPropertyName("appid")] public int AppId { get; set; } - [JsonPropertyName("name")] public string Name { get; set; } + [JsonPropertyName("name")] + public string Name + { + get => _name; + set + { + _name = value; + _comparableName = Regex.Replace(value, Misc.SpecialCharsRegex, "").ToLower(); + } + } + + public bool CompareName(string value) + { + return _comparableName.Equals(value); + } public override string ToString() { - //return $"AppId: {AppId}, Name: {Name}"; return $"{AppId}={Name}"; } - - public bool CompareId(SteamApp steamApp) - { - return AppId.Equals(steamApp.AppId); - } } public class AppList diff --git a/auto-creamapi/Services/CacheService.cs b/auto-creamapi/Services/CacheService.cs index 1be6d7f..efacfd2 100644 --- a/auto-creamapi/Services/CacheService.cs +++ b/auto-creamapi/Services/CacheService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Net.Http; using System.Text; using System.Text.Json; @@ -35,24 +36,7 @@ namespace auto_creamapi.Services "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/87.0.4280.88 Safari/537.36"; - private const string SpecialCharsRegex = "[^0-9a-zA-Z]+"; - - private List _cache = new List(); - - /*private static readonly Lazy Lazy = - new Lazy(() => new CacheService()); - - public static CacheService Instance => Lazy.Value;*/ - - public CacheService() - { - } - - /*public async void Initialize() - { - //Languages = _defaultLanguages; - await UpdateCache(); - }*/ + private HashSet _cache = new HashSet(); public async Task Initialize() { @@ -81,7 +65,7 @@ namespace auto_creamapi.Services } var steamApps = JsonSerializer.Deserialize(cacheString); - _cache = steamApps.AppList.Apps; + _cache = new HashSet(steamApps.AppList.Apps); MyLogger.Log.Information("Loaded cache into memory!"); } @@ -96,9 +80,8 @@ namespace auto_creamapi.Services public SteamApp GetAppByName(string name) { MyLogger.Log.Information($"Trying to get app {name}"); - var app = _cache.Find(x => - Regex.Replace(x.Name, SpecialCharsRegex, "").ToLower() - .Equals(Regex.Replace(name, SpecialCharsRegex, "").ToLower())); + var comparableName = Regex.Replace(name, Misc.SpecialCharsRegex, "").ToLower(); + var app = _cache.FirstOrDefault(x => x.CompareName(comparableName)); if (app != null) MyLogger.Log.Information($"Successfully got app {app}"); return app; } @@ -106,7 +89,7 @@ namespace auto_creamapi.Services public SteamApp GetAppById(int appid) { MyLogger.Log.Information($"Trying to get app with ID {appid}"); - var app = _cache.Find(x => x.AppId.Equals(appid)); + var app = _cache.FirstOrDefault(x => x.AppId.Equals(appid)); if (app != null) MyLogger.Log.Information($"Successfully got app {app}"); return app; } @@ -121,7 +104,7 @@ namespace auto_creamapi.Services var steamAppDetails = await task; steamAppDetails?.DLC.ForEach(x => { - var result = _cache.Find(y => y.AppId.Equals(x)) ?? + var result = _cache.FirstOrDefault(y => y.AppId.Equals(x)) ?? new SteamApp {AppId = x, Name = $"Unknown DLC {x}"}; dlcList.Add(result); }); @@ -173,7 +156,7 @@ namespace auto_creamapi.Services if (query3 != null) dlcName = query3[1].Text().Replace("\n", "").Trim(); var dlcApp = new SteamApp {AppId = Convert.ToInt32(dlcId), Name = dlcName}; - var i = dlcList.FindIndex(x => x.CompareId(dlcApp)); + var i = dlcList.FindIndex(x => x.AppId.Equals(dlcApp.AppId)); if (i > -1) { if (dlcList[i].Name.Contains("Unknown DLC")) dlcList[i] = dlcApp; @@ -189,7 +172,7 @@ namespace auto_creamapi.Services } else { - MyLogger.Log.Error("Could not get DLC from SteamDB1"); + MyLogger.Log.Error("Could not get DLC from SteamDB!"); } } } diff --git a/auto-creamapi/Utils/Misc.cs b/auto-creamapi/Utils/Misc.cs index 2bd85a2..3290f47 100644 --- a/auto-creamapi/Utils/Misc.cs +++ b/auto-creamapi/Utils/Misc.cs @@ -5,7 +5,7 @@ namespace auto_creamapi.Utils { public class Misc { - + public const string SpecialCharsRegex = "[^0-9a-zA-Z]+"; public const string DefaultLanguageSelection = "english"; public static readonly ObservableCollection DefaultLanguages = new ObservableCollection(new[] { diff --git a/auto-creamapi/ViewModels/MainViewModel.cs b/auto-creamapi/ViewModels/MainViewModel.cs index b4ee4d9..7479306 100644 --- a/auto-creamapi/ViewModels/MainViewModel.cs +++ b/auto-creamapi/ViewModels/MainViewModel.cs @@ -26,7 +26,7 @@ namespace auto_creamapi.ViewModels private ObservableCollection _dlcs; private bool _dllApplied; private string _dllPath; - private bool _extraprotection; + private bool _extraProtection; private string _gameName; private string _lang; private ObservableCollection _languages; @@ -35,7 +35,7 @@ namespace auto_creamapi.ViewModels private bool _mainWindowEnabled; private bool _offline; private string _status; - private bool _unlockall; + private bool _unlockAll; private bool _useSteamDb; //private const string DlcRegexPattern = @"(?.*) *= *(?.*)"; @@ -144,23 +144,23 @@ namespace auto_creamapi.ViewModels } } - public bool Extraprotection + public bool ExtraProtection { - get => _extraprotection; + get => _extraProtection; set { - _extraprotection = value; - RaisePropertyChanged(() => Extraprotection); + _extraProtection = value; + RaisePropertyChanged(() => ExtraProtection); } } - public bool Unlockall + public bool UnlockAll { - get => _unlockall; + get => _unlockAll; set { - _unlockall = value; - RaisePropertyChanged(() => Unlockall); + _unlockAll = value; + RaisePropertyChanged(() => UnlockAll); } } @@ -319,8 +319,8 @@ namespace auto_creamapi.ViewModels _config.SetConfigData( AppId, Lang, - Unlockall, - Extraprotection, + UnlockAll, + ExtraProtection, Offline, Dlcs ); @@ -334,8 +334,8 @@ namespace auto_creamapi.ViewModels { AppId = _config.Config.AppId; Lang = _config.Config.Language; - Unlockall = _config.Config.UnlockAll; - Extraprotection = _config.Config.ExtraProtection; + UnlockAll = _config.Config.UnlockAll; + ExtraProtection = _config.Config.ExtraProtection; Offline = _config.Config.ForceOffline; Dlcs = new ObservableCollection(_config.Config.DlcList); Status = "Changes have been reset."; diff --git a/auto-creamapi/Views/MainView.xaml b/auto-creamapi/Views/MainView.xaml index 7324e3d..021d61b 100644 --- a/auto-creamapi/Views/MainView.xaml +++ b/auto-creamapi/Views/MainView.xaml @@ -1,3 +1,4 @@ + - - +