diff --git a/src/MaaWpfGui/Helper/ETagCache.cs b/src/MaaWpfGui/Helper/ETagCache.cs index 8832b9718b..579a795336 100644 --- a/src/MaaWpfGui/Helper/ETagCache.cs +++ b/src/MaaWpfGui/Helper/ETagCache.cs @@ -10,6 +10,7 @@ // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY // +#nullable enable using System; using System.Collections.Generic; @@ -26,20 +27,20 @@ namespace MaaWpfGui.Helper private static readonly ILogger _logger = Log.ForContext(); private static readonly string _cacheFile = Path.Combine(Environment.CurrentDirectory, "cache/etag.json"); - private static Dictionary _cache; + private static Dictionary _cache = []; public static void Load() { if (File.Exists(_cacheFile) is false) { - _cache = new Dictionary(); + _cache = []; return; } try { var jsonStr = File.ReadAllText(_cacheFile); - _cache = JsonConvert.DeserializeObject>(jsonStr); + _cache = JsonConvert.DeserializeObject>(jsonStr) ?? []; } catch (Exception e) { @@ -47,7 +48,7 @@ namespace MaaWpfGui.Helper } finally { - _cache ??= new Dictionary(); + _cache ??= []; } } @@ -65,7 +66,7 @@ namespace MaaWpfGui.Helper return string.Empty; } - return _cache.TryGetValue(url.Replace("%23", "#"), out string ret) ? ret : string.Empty; + return _cache.TryGetValue(url.Replace("%23", "#"), out string? ret) ? ret : string.Empty; } // ReSharper disable once MemberCanBePrivate.Global @@ -75,18 +76,19 @@ namespace MaaWpfGui.Helper Save(); } - public static void Set(HttpResponseMessage response) + public static void Set(HttpResponseMessage? response) { - var res = response?.Headers?.ETag?.Tag; - if (string.IsNullOrEmpty(res)) + var etag = response?.Headers?.ETag?.Tag; + var uri = response?.RequestMessage?.RequestUri?.ToString(); + if (string.IsNullOrEmpty(uri) || string.IsNullOrEmpty(etag)) { return; } - Set(response.RequestMessage.RequestUri.ToString(), res); + Set(uri, etag); } - public static async Task FetchResponseWithEtag(string url, bool force = false) + public static async Task FetchResponseWithEtag(string url, bool force = false) { var etag = force ? string.Empty : Get(url); Dictionary headers = new Dictionary diff --git a/src/MaaWpfGui/Helper/HttpResponseHelper.cs b/src/MaaWpfGui/Helper/HttpResponseHelper.cs index fe61806fd6..3b09237c09 100644 --- a/src/MaaWpfGui/Helper/HttpResponseHelper.cs +++ b/src/MaaWpfGui/Helper/HttpResponseHelper.cs @@ -10,6 +10,7 @@ // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY // +#nullable enable using System; using System.IO; @@ -23,7 +24,7 @@ namespace MaaWpfGui.Helper { private static readonly ILogger _logger = Log.ForContext("SourceContext", "HttpResponseHelper"); - public static async Task SaveResponseToFileAsync(HttpResponseMessage response, string saveTo, bool saveAndDeleteTmp = true) + public static async Task SaveResponseToFileAsync(HttpResponseMessage? response, string saveTo, bool saveAndDeleteTmp = true) { saveTo = Path.Combine(Environment.CurrentDirectory, saveTo); @@ -37,7 +38,7 @@ namespace MaaWpfGui.Helper { using var stream = await GetStreamAsync(response); using var fileStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true); - await stream.CopyToAsync(fileStream).ConfigureAwait(false); + await stream!.CopyToAsync(fileStream).ConfigureAwait(false); } catch (Exception e) { @@ -56,7 +57,7 @@ namespace MaaWpfGui.Helper } // ReSharper disable once MemberCanBePrivate.Global - public static async Task GetStreamAsync(HttpResponseMessage response) + public static async Task GetStreamAsync(HttpResponseMessage? response) { if (response == null) { @@ -74,7 +75,7 @@ namespace MaaWpfGui.Helper } } - public static async Task GetStringAsync(HttpResponseMessage response) + public static async Task GetStringAsync(HttpResponseMessage? response) { if (response == null) { diff --git a/src/MaaWpfGui/Helper/Instances.cs b/src/MaaWpfGui/Helper/Instances.cs index 2a9fc07885..9818208055 100644 --- a/src/MaaWpfGui/Helper/Instances.cs +++ b/src/MaaWpfGui/Helper/Instances.cs @@ -40,7 +40,7 @@ namespace MaaWpfGui.Helper /// /// Gets 当前理智 / 最大理智 /// - public static int[] Sanity { get; } = { -1, -1 }; + public static int[] Sanity { get; } = [-1, -1]; public static DateTimeOffset ReportTime { get; set; } } diff --git a/src/MaaWpfGui/Models/ResourceUpdater.cs b/src/MaaWpfGui/Models/ResourceUpdater.cs index 2d1968d56a..981b5722b8 100644 --- a/src/MaaWpfGui/Models/ResourceUpdater.cs +++ b/src/MaaWpfGui/Models/ResourceUpdater.cs @@ -10,6 +10,7 @@ // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY // +#nullable enable using System; using System.Collections.Generic; @@ -320,7 +321,7 @@ namespace MaaWpfGui.Models return ret; } - private static UpdateResult ResponseToUpdateResult(HttpResponseMessage response) + private static UpdateResult ResponseToUpdateResult(HttpResponseMessage? response) { if (response == null) { @@ -370,7 +371,7 @@ namespace MaaWpfGui.Models return updateResult; } - private static ObservableCollection _logItemViewModels; + private static ObservableCollection _logItemViewModels = []; private static void OutputDownloadProgress(int index, int count = 0, int maxCount = 1) {