perf: nullable

[skip changelog]
This commit is contained in:
status102
2024-04-03 10:54:01 +08:00
parent f896ab1f84
commit f9717be116
4 changed files with 21 additions and 17 deletions

View File

@@ -10,6 +10,7 @@
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
// </copyright>
#nullable enable
using System;
using System.Collections.Generic;
@@ -26,20 +27,20 @@ namespace MaaWpfGui.Helper
private static readonly ILogger _logger = Log.ForContext<ETagCache>();
private static readonly string _cacheFile = Path.Combine(Environment.CurrentDirectory, "cache/etag.json");
private static Dictionary<string, string> _cache;
private static Dictionary<string, string> _cache = [];
public static void Load()
{
if (File.Exists(_cacheFile) is false)
{
_cache = new Dictionary<string, string>();
_cache = [];
return;
}
try
{
var jsonStr = File.ReadAllText(_cacheFile);
_cache = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);
_cache = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr) ?? [];
}
catch (Exception e)
{
@@ -47,7 +48,7 @@ namespace MaaWpfGui.Helper
}
finally
{
_cache ??= new Dictionary<string, string>();
_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<HttpResponseMessage> FetchResponseWithEtag(string url, bool force = false)
public static async Task<HttpResponseMessage?> FetchResponseWithEtag(string url, bool force = false)
{
var etag = force ? string.Empty : Get(url);
Dictionary<string, string> headers = new Dictionary<string, string>

View File

@@ -10,6 +10,7 @@
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
// </copyright>
#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<bool> SaveResponseToFileAsync(HttpResponseMessage response, string saveTo, bool saveAndDeleteTmp = true)
public static async Task<bool> 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<Stream> GetStreamAsync(HttpResponseMessage response)
public static async Task<Stream?> GetStreamAsync(HttpResponseMessage? response)
{
if (response == null)
{
@@ -74,7 +75,7 @@ namespace MaaWpfGui.Helper
}
}
public static async Task<string> GetStringAsync(HttpResponseMessage response)
public static async Task<string> GetStringAsync(HttpResponseMessage? response)
{
if (response == null)
{

View File

@@ -40,7 +40,7 @@ namespace MaaWpfGui.Helper
/// <summary>
/// Gets 当前理智 / 最大理智
/// </summary>
public static int[] Sanity { get; } = { -1, -1 };
public static int[] Sanity { get; } = [-1, -1];
public static DateTimeOffset ReportTime { get; set; }
}

View File

@@ -10,6 +10,7 @@
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
// </copyright>
#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<LogItemViewModel> _logItemViewModels;
private static ObservableCollection<LogItemViewModel> _logItemViewModels = [];
private static void OutputDownloadProgress(int index, int count = 0, int maxCount = 1)
{