mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-19 18:47:55 +08:00
perf: 优化热更新
This commit is contained in:
@@ -46,10 +46,6 @@ namespace MaaWpfGui.Helper
|
||||
{
|
||||
_logger.Error(e.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_cache ??= [];
|
||||
}
|
||||
}
|
||||
|
||||
public static void Save()
|
||||
@@ -59,26 +55,26 @@ namespace MaaWpfGui.Helper
|
||||
}
|
||||
|
||||
// ReSharper disable once MemberCanBePrivate.Global
|
||||
public static string Get(string url)
|
||||
public static string Get(string? url)
|
||||
{
|
||||
if (url is null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return _cache.TryGetValue(url.Replace("%23", "#"), out string? ret) ? ret : string.Empty;
|
||||
return _cache.TryGetValue(url, out string? ret) ? ret : string.Empty;
|
||||
}
|
||||
|
||||
// ReSharper disable once MemberCanBePrivate.Global
|
||||
public static void Set(string url, string etag)
|
||||
{
|
||||
_cache[url.Replace("%23", "#")] = etag;
|
||||
_cache[url] = etag;
|
||||
Save();
|
||||
}
|
||||
|
||||
public static void Set(HttpResponseMessage? response)
|
||||
{
|
||||
var etag = response?.Headers?.ETag?.Tag;
|
||||
var etag = response?.Headers.ETag?.Tag;
|
||||
var uri = response?.RequestMessage?.RequestUri?.ToString();
|
||||
if (string.IsNullOrEmpty(uri) || string.IsNullOrEmpty(etag))
|
||||
{
|
||||
@@ -93,7 +89,9 @@ namespace MaaWpfGui.Helper
|
||||
var etag = force ? string.Empty : Get(url);
|
||||
Dictionary<string, string> headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "Accept", "application/octet-stream" },
|
||||
{
|
||||
"Accept", "application/octet-stream"
|
||||
},
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(etag))
|
||||
|
||||
@@ -36,8 +36,8 @@ namespace MaaWpfGui.Helper
|
||||
|
||||
try
|
||||
{
|
||||
using var stream = await GetStreamAsync(response);
|
||||
using var fileStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);
|
||||
await using var stream = await GetStreamAsync(response);
|
||||
await using var fileStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);
|
||||
await stream!.CopyToAsync(fileStream).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -19,7 +19,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Web;
|
||||
using MaaWpfGui.Constants;
|
||||
using MaaWpfGui.Helper;
|
||||
using MaaWpfGui.Main;
|
||||
@@ -141,7 +141,10 @@ namespace MaaWpfGui.Models
|
||||
new Uri(mirrorUrl + MaaResourceVersion),
|
||||
httpCompletionOption: HttpCompletionOption.ResponseHeadersRead);
|
||||
|
||||
return response is { StatusCode: System.Net.HttpStatusCode.OK };
|
||||
return response is
|
||||
{
|
||||
StatusCode: System.Net.HttpStatusCode.OK
|
||||
};
|
||||
}
|
||||
|
||||
private static async Task<bool> CheckUpdateAsync(string baseUrl)
|
||||
@@ -149,7 +152,10 @@ namespace MaaWpfGui.Models
|
||||
var url = baseUrl + MaaResourceVersion;
|
||||
|
||||
using var response = await ETagCache.FetchResponseWithEtag(url);
|
||||
if (response is not { StatusCode: System.Net.HttpStatusCode.OK })
|
||||
if (response is not
|
||||
{
|
||||
StatusCode: System.Net.HttpStatusCode.OK
|
||||
})
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -251,9 +257,7 @@ namespace MaaWpfGui.Models
|
||||
// TODO: 加个文件存这些文件的 hash,如果 hash 没变就不下载了,只需要请求一次
|
||||
foreach (var file in _maaSingleFiles)
|
||||
{
|
||||
await Task.Delay(1000);
|
||||
|
||||
var sRet = await UpdateFileWithETagAsync(baseUrl, file.Replace("#", "%23"), file, maxRetryTime);
|
||||
var sRet = await UpdateFileWithETagAsync(baseUrl, file, file, maxRetryTime);
|
||||
|
||||
if (sRet == UpdateResult.Failed)
|
||||
{
|
||||
@@ -301,9 +305,7 @@ namespace MaaWpfGui.Models
|
||||
.Where(file => !string.IsNullOrEmpty(file))
|
||||
.Where(file => !File.Exists(Path.Combine(Environment.CurrentDirectory, file))))
|
||||
{
|
||||
await Task.Delay(1000);
|
||||
|
||||
var sRet = await UpdateFileWithETagAsync(baseUrl, file.Replace("#", "%23"), file, maxRetryTime);
|
||||
var sRet = await UpdateFileWithETagAsync(baseUrl, file, file, maxRetryTime);
|
||||
if (sRet == UpdateResult.Failed)
|
||||
{
|
||||
OutputDownloadProgress(LocalizationHelper.GetString("GameResourceFailed"));
|
||||
@@ -341,7 +343,8 @@ namespace MaaWpfGui.Models
|
||||
private static async Task<UpdateResult> UpdateFileWithETagAsync(string baseUrl, string file, string saveTo, int maxRetryTime = 0)
|
||||
{
|
||||
saveTo = Path.Combine(Environment.CurrentDirectory, saveTo);
|
||||
var url = baseUrl + file;
|
||||
var encodedFilePath = string.Join('/', file.Split('/').Select(HttpUtility.UrlEncode));
|
||||
var url = baseUrl + encodedFilePath;
|
||||
|
||||
int retryCount = 0;
|
||||
UpdateResult updateResult;
|
||||
|
||||
Reference in New Issue
Block a user