From 1bd6023e0aa151f719c39dab88821ddb4eebf7ca Mon Sep 17 00:00:00 2001
From: DismissedLight <1686188646@qq.com>
Date: Sun, 15 Oct 2023 12:30:44 +0800
Subject: [PATCH] fix issues
---
.../Snap.Hutao/Core/Shell/ShellLinkInterop.cs | 9 ++++++++-
.../Snap.Hutao/Resource/Localization/SH.Designer.cs | 9 +++++++++
.../Snap.Hutao/Resource/Localization/SH.resx | 3 +++
.../View/Control/AnnouncementContentViewer.xaml.cs | 5 ++++-
.../Snap.Hutao/ViewModel/SettingViewModel.cs | 11 ++++++++++-
.../Snap.Hutao/Web/Bridge/MiHoYoJSInterface.cs | 8 ++++++--
.../Web/Hutao/Geetest/HomaGeetestClient.cs | 12 +++++++++++-
.../Builder/HttpRequestMessageBuilderExtension.cs | 5 +++++
8 files changed, 56 insertions(+), 6 deletions(-)
diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs b/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs
index 257354c0..051aed33 100644
--- a/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs
@@ -49,7 +49,14 @@ internal sealed partial class ShellLinkInterop : IShellLinkInterop
string target = Path.Combine(desktop, $"{SH.AppNameAndVersion.Format(runtimeOptions.Version)}.lnk");
IPersistFile persistFile = (IPersistFile)shellLink;
- persistFile.Save(target, false);
+ try
+ {
+ persistFile.Save(target, false);
+ }
+ catch (UnauthorizedAccessException)
+ {
+ return false;
+ }
return true;
}
diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs
index 0584223f..10a70e1b 100644
--- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs
@@ -3786,6 +3786,15 @@ namespace Snap.Hutao.Resource.Localization {
}
}
+ ///
+ /// 查找类似 保存游戏路径失败 的本地化字符串。
+ ///
+ internal static string ViewModelSettingSetGamePathDatabaseFailedTitle {
+ get {
+ return ResourceManager.GetString("ViewModelSettingSetGamePathDatabaseFailedTitle", resourceCulture);
+ }
+ }
+
///
/// 查找类似 用户 [{0}] 添加成功 的本地化字符串。
///
diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx
index 42fe516e..4537ef07 100644
--- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx
+++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx
@@ -1415,6 +1415,9 @@
设置数据目录成功,重启以应用更改
+
+ 保存游戏路径失败
+
用户 [{0}] 添加成功
diff --git a/src/Snap.Hutao/Snap.Hutao/View/Control/AnnouncementContentViewer.xaml.cs b/src/Snap.Hutao/Snap.Hutao/View/Control/AnnouncementContentViewer.xaml.cs
index c975fb3e..0da882d0 100644
--- a/src/Snap.Hutao/Snap.Hutao/View/Control/AnnouncementContentViewer.xaml.cs
+++ b/src/Snap.Hutao/Snap.Hutao/View/Control/AnnouncementContentViewer.xaml.cs
@@ -132,7 +132,10 @@ internal sealed partial class AnnouncementContentViewer : UserControl
private void OnUnloaded(object sender, RoutedEventArgs e)
{
- WebView.CoreWebView2.WebMessageReceived -= webMessageReceivedHandler;
+ if (WebView is { CoreWebView2: CoreWebView2 coreWebView2 })
+ {
+ coreWebView2.WebMessageReceived -= webMessageReceivedHandler;
+ }
}
private async ValueTask LoadAnnouncementAsync()
diff --git a/src/Snap.Hutao/Snap.Hutao/ViewModel/SettingViewModel.cs b/src/Snap.Hutao/Snap.Hutao/ViewModel/SettingViewModel.cs
index cdf42e65..f026d392 100644
--- a/src/Snap.Hutao/Snap.Hutao/ViewModel/SettingViewModel.cs
+++ b/src/Snap.Hutao/Snap.Hutao/ViewModel/SettingViewModel.cs
@@ -1,6 +1,7 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
+using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.AppLifecycle;
@@ -126,7 +127,15 @@ internal sealed partial class SettingViewModel : Abstraction.ViewModel
if (isOk)
{
await taskContext.SwitchToMainThreadAsync();
- Options.GamePath = path;
+ try
+ {
+ Options.GamePath = path;
+ }
+ catch (SqliteException ex)
+ {
+ // 文件夹权限不足,无法写入数据库
+ infoBarService.Error(ex, SH.ViewModelSettingSetGamePathDatabaseFailedTitle);
+ }
}
}
diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Bridge/MiHoYoJSInterface.cs b/src/Snap.Hutao/Snap.Hutao/Web/Bridge/MiHoYoJSInterface.cs
index a35d8f2a..a9067aa1 100644
--- a/src/Snap.Hutao/Snap.Hutao/Web/Bridge/MiHoYoJSInterface.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Web/Bridge/MiHoYoJSInterface.cs
@@ -375,14 +375,18 @@ internal class MiHoYoJSInterface
await taskContext.SwitchToMainThreadAsync();
try
{
- return await webView.ExecuteScriptAsync(js);
+ if (webView is not null)
+ {
+ return await webView.ExecuteScriptAsync(js);
+ }
}
catch (COMException)
{
// COMException (0x8007139F): 组或资源的状态不是执行请求操作的正确状态。 (0x8007139F)
// webview is disposing or disposed
- return string.Empty;
}
+
+ return string.Empty;
}
private async void OnWebMessageReceived(CoreWebView2 webView2, CoreWebView2WebMessageReceivedEventArgs args)
diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Geetest/HomaGeetestClient.cs b/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Geetest/HomaGeetestClient.cs
index d33e36b9..a36802d3 100644
--- a/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Geetest/HomaGeetestClient.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Geetest/HomaGeetestClient.cs
@@ -22,7 +22,17 @@ internal sealed partial class HomaGeetestClient
{
string template = appOptions.GeetestCustomCompositeUrl;
- if (string.IsNullOrEmpty(template) || !Uri.TryCreate(template.Format(gt, challenge), UriKind.Absolute, out Uri? uri))
+ string url;
+ try
+ {
+ url = template.Format(gt, challenge);
+ }
+ catch (FormatException)
+ {
+ return GeetestResponse.InternalFailure;
+ }
+
+ if (string.IsNullOrEmpty(template) || !Uri.TryCreate(url, UriKind.Absolute, out Uri? uri))
{
return GeetestResponse.InternalFailure;
}
diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Request/Builder/HttpRequestMessageBuilderExtension.cs b/src/Snap.Hutao/Snap.Hutao/Web/Request/Builder/HttpRequestMessageBuilderExtension.cs
index cd84fae8..263d9825 100644
--- a/src/Snap.Hutao/Snap.Hutao/Web/Request/Builder/HttpRequestMessageBuilderExtension.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Web/Request/Builder/HttpRequestMessageBuilderExtension.cs
@@ -33,6 +33,11 @@ internal static class HttpRequestMessageBuilderExtension
logger.LogWarning(ex, RequestErrorMessage);
return default;
}
+ catch (HttpContentSerializationException ex)
+ {
+ logger.LogWarning(ex, RequestErrorMessage);
+ return default;
+ }
catch (SocketException ex)
{
logger.LogWarning(ex, RequestErrorMessage);