From e60a04a2bcf6a32d3854284ad9a1ff0b5cf2f007 Mon Sep 17 00:00:00 2001
From: DismissedLight <1686188646@qq.com>
Date: Wed, 8 Feb 2023 12:28:31 +0800
Subject: [PATCH] impl #117
---
.../Snap.Hutao/Core/CoreEnvironment.cs | 26 +++++++++-----
.../Snap.Hutao/Core/IO/PickerExtension.cs | 26 ++++++++++++++
.../Snap.Hutao/Core/Setting/LocalSetting.cs | 16 +++++++--
.../Snap.Hutao/Core/Setting/SettingKeys.cs | 5 +++
.../Resource/Localization/SH.Designer.cs | 36 +++++++++++++++++++
.../Snap.Hutao/Resource/Localization/SH.resx | 12 +++++++
.../Snap.Hutao/View/Page/SettingPage.xaml | 10 +++++-
.../Snap.Hutao/ViewModel/SettingViewModel.cs | 27 +++++++++++++-
.../Snap.Hutao/Web/Response/Response.cs | 19 +++-------
9 files changed, 149 insertions(+), 28 deletions(-)
diff --git a/src/Snap.Hutao/Snap.Hutao/Core/CoreEnvironment.cs b/src/Snap.Hutao/Snap.Hutao/Core/CoreEnvironment.cs
index 278df815..53c5eb77 100644
--- a/src/Snap.Hutao/Snap.Hutao/Core/CoreEnvironment.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Core/CoreEnvironment.cs
@@ -3,6 +3,7 @@
using Microsoft.Win32;
using Snap.Hutao.Core.Json;
+using Snap.Hutao.Core.Setting;
using Snap.Hutao.Extension;
using Snap.Hutao.Web.Hoyolab.DynamicSecret;
using System.Collections.Immutable;
@@ -123,16 +124,25 @@ internal static class CoreEnvironment
private static string GetDatafolderPath()
{
- string myDocument = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
+ string preferredPath = LocalSetting.Get(SettingKeys.DataFolderPath, string.Empty);
+
+ if (string.IsNullOrEmpty(preferredPath))
+ {
+ string myDocument = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
#if RELEASE
- // 将测试版与正式版的文件目录分离
- string folderName = Package.Current.PublisherDisplayName == "DGP Studio CI" ? "HutaoAlpha" : "Hutao";
+ // 将测试版与正式版的文件目录分离
+ string folderName = Package.Current.PublisherDisplayName == "DGP Studio CI" ? "HutaoAlpha" : "Hutao";
#else
- // 使得迁移能正常生成
- string folderName = "Hutao";
+ // 使得迁移能正常生成
+ string folderName = "Hutao";
#endif
- string path = Path.GetFullPath(Path.Combine(myDocument, folderName));
- Directory.CreateDirectory(path);
- return path;
+ string path = Path.GetFullPath(Path.Combine(myDocument, folderName));
+ Directory.CreateDirectory(path);
+ return path;
+ }
+ else
+ {
+ return preferredPath;
+ }
}
}
\ No newline at end of file
diff --git a/src/Snap.Hutao/Snap.Hutao/Core/IO/PickerExtension.cs b/src/Snap.Hutao/Snap.Hutao/Core/IO/PickerExtension.cs
index 0f0e734a..41a63825 100644
--- a/src/Snap.Hutao/Snap.Hutao/Core/IO/PickerExtension.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Core/IO/PickerExtension.cs
@@ -63,6 +63,32 @@ internal static class PickerExtension
}
}
+ ///
+ public static async Task> TryPickSingleFolderAsync(this FolderPicker picker)
+ {
+ StorageFolder? folder;
+ Exception? exception = null;
+ try
+ {
+ folder = await picker.PickSingleFolderAsync().AsTask().ConfigureAwait(false);
+ }
+ catch (Exception ex)
+ {
+ exception = ex;
+ folder = null;
+ }
+
+ if (folder != null)
+ {
+ return new(true, folder.Path);
+ }
+ else
+ {
+ InfoBarWaringPickerException(exception);
+ return new(false, null!);
+ }
+ }
+
private static void InfoBarWaringPickerException(Exception? exception)
{
if (exception != null)
diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Setting/LocalSetting.cs b/src/Snap.Hutao/Snap.Hutao/Core/Setting/LocalSetting.cs
index 7a8f9640..d1b60676 100644
--- a/src/Snap.Hutao/Snap.Hutao/Core/Setting/LocalSetting.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Core/Setting/LocalSetting.cs
@@ -77,6 +77,12 @@ internal static class LocalSetting
return Get(key, defaultValue);
}
+ ///
+ public static string Get(string key, string defaultValue)
+ {
+ return Get(key, defaultValue);
+ }
+
///
public static DateTimeOffset Get(string key, DateTimeOffset defaultValue)
{
@@ -173,6 +179,12 @@ internal static class LocalSetting
Set(key, value);
}
+ ///
+ public static void Set(string key, string value)
+ {
+ Set(key, value);
+ }
+
///
public static void Set(string key, DateTimeOffset value)
{
@@ -216,8 +228,7 @@ internal static class LocalSetting
/// 键
/// 默认值
/// 获取的值
- private static T Get(string key, T defaultValue = default)
- where T : struct
+ private static T Get(string key, T defaultValue = default!)
{
if (Container.Values.TryGetValue(key, out object? value))
{
@@ -238,7 +249,6 @@ internal static class LocalSetting
/// 键
/// 值
private static void Set(string key, T value)
- where T : struct
{
Container.Values[key] = value;
}
diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Setting/SettingKeys.cs b/src/Snap.Hutao/Snap.Hutao/Core/Setting/SettingKeys.cs
index 43f66722..1f7fed0a 100644
--- a/src/Snap.Hutao/Snap.Hutao/Core/Setting/SettingKeys.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Core/Setting/SettingKeys.cs
@@ -23,6 +23,11 @@ internal static class SettingKeys
///
public const string LaunchTimes = "LaunchTimes";
+ ///
+ /// 数据文件夹
+ ///
+ public const string DataFolderPath = "DataFolderPath";
+
///
/// 静态资源合约
/// 新增合约时 请注意
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 3c6b5c0d..5c21c5a4 100644
--- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs
@@ -1761,6 +1761,15 @@ namespace Snap.Hutao.Resource.Localization {
}
}
+ ///
+ /// 查找类似 设置数据目录成功,重启以应用更改 的本地化字符串。
+ ///
+ internal static string ViewModelSettingSetDataFolderSuccess {
+ get {
+ return ResourceManager.GetString("ViewModelSettingSetDataFolderSuccess", resourceCulture);
+ }
+ }
+
///
/// 查找类似 用户 [{0}] 添加成功 的本地化字符串。
///
@@ -3327,6 +3336,24 @@ namespace Snap.Hutao.Resource.Localization {
}
}
+ ///
+ /// 查找类似 更改目录后需要手动移动目录内的数据,否则会重新创建用户数据 的本地化字符串。
+ ///
+ internal static string ViewPageSettingSetDataFolderDescription {
+ get {
+ return ResourceManager.GetString("ViewPageSettingSetDataFolderDescription", resourceCulture);
+ }
+ }
+
+ ///
+ /// 查找类似 更改数据目录 的本地化字符串。
+ ///
+ internal static string ViewPageSettingSetDataFolderHeader {
+ get {
+ return ResourceManager.GetString("ViewPageSettingSetDataFolderHeader", resourceCulture);
+ }
+ }
+
///
/// 查找类似 设置路径 的本地化字符串。
///
@@ -3372,6 +3399,15 @@ namespace Snap.Hutao.Resource.Localization {
}
}
+ ///
+ /// 查找类似 更改 的本地化字符串。
+ ///
+ internal static string ViewPageSettingStorageSetAction {
+ get {
+ return ResourceManager.GetString("ViewPageSettingStorageSetAction", resourceCulture);
+ }
+ }
+
///
/// 查找类似 前往商店 的本地化字符串。
///
diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx
index 4446d5fc..fda072ad 100644
--- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx
+++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx
@@ -684,6 +684,9 @@
清除完成
+
+ 设置数据目录成功,重启以应用更改
+
用户 [{0}] 添加成功
@@ -1206,6 +1209,12 @@
游戏
+
+ 更改目录后需要手动移动目录内的数据,否则会重新创建用户数据
+
+
+ 更改数据目录
+
设置路径
@@ -1221,6 +1230,9 @@
打开
+
+ 更改
+
前往商店
diff --git a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml
index 70861e4d..541a625e 100644
--- a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml
+++ b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml
@@ -139,8 +139,16 @@
-
+
+
+
+
+
diff --git a/src/Snap.Hutao/Snap.Hutao/ViewModel/SettingViewModel.cs b/src/Snap.Hutao/Snap.Hutao/ViewModel/SettingViewModel.cs
index 5c80fb89..14f701f4 100644
--- a/src/Snap.Hutao/Snap.Hutao/ViewModel/SettingViewModel.cs
+++ b/src/Snap.Hutao/Snap.Hutao/ViewModel/SettingViewModel.cs
@@ -4,17 +4,22 @@
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Snap.Hutao.Core.Database;
+using Snap.Hutao.Core.IO;
+using Snap.Hutao.Core.Setting;
using Snap.Hutao.Core.Windowing;
+using Snap.Hutao.Factory.Abstraction;
using Snap.Hutao.Model;
using Snap.Hutao.Model.Entity;
using Snap.Hutao.Model.Entity.Database;
using Snap.Hutao.Service.Abstraction;
-using Snap.Hutao.Service.GachaLog;
using Snap.Hutao.Service.GachaLog.QueryProvider;
using Snap.Hutao.Service.Game;
using Snap.Hutao.Service.Game.Locator;
using Snap.Hutao.View.Dialog;
using System.IO;
+using System.Runtime.InteropServices;
+using Windows.Storage;
+using Windows.Storage.Pickers;
namespace Snap.Hutao.ViewModel;
@@ -75,6 +80,7 @@ internal class SettingViewModel : Abstraction.ViewModel
UpdateCheckCommand = new AsyncRelayCommand(CheckUpdateAsync);
DeleteGameWebCacheCommand = new RelayCommand(DeleteGameWebCache);
ShowSignInWebViewDialogCommand = new AsyncRelayCommand(ShowSignInWebViewDialogAsync);
+ SetDataFolderCommand = new AsyncRelayCommand(SetDataFolderAsync);
}
///
@@ -178,6 +184,11 @@ internal class SettingViewModel : Abstraction.ViewModel
///
public ICommand ShowSignInWebViewDialogCommand { get; }
+ ///
+ /// 设置数据目录命令
+ ///
+ public ICommand SetDataFolderCommand { get; }
+
private async Task SetGamePathAsync()
{
IGameLocator locator = Ioc.Default.GetRequiredService>()
@@ -242,4 +253,18 @@ internal class SettingViewModel : Abstraction.ViewModel
await Windows.System.Launcher.LaunchUriAsync(new(@"ms-windows-store://pdp/?productid=9PH4NXJ2JN52"));
#endif
}
+
+ private async Task SetDataFolderAsync()
+ {
+ IPickerFactory pickerFactory = Ioc.Default.GetRequiredService();
+ FolderPicker picker = pickerFactory.GetFolderPicker();
+ (bool isOk, string folder) = await picker.TryPickSingleFolderAsync().ConfigureAwait(false);
+
+ IInfoBarService infoBarService = Ioc.Default.GetRequiredService();
+ if (isOk)
+ {
+ LocalSetting.Set(SettingKeys.DataFolderPath, folder);
+ infoBarService.Success(SH.ViewModelSettingSetDataFolderSuccess);
+ }
+ }
}
\ No newline at end of file
diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Response/Response.cs b/src/Snap.Hutao/Snap.Hutao/Web/Response/Response.cs
index 4b6e1a79..2b597677 100644
--- a/src/Snap.Hutao/Snap.Hutao/Web/Response/Response.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Web/Response/Response.cs
@@ -3,6 +3,7 @@
using Snap.Hutao.Service.Abstraction;
using Snap.Hutao.Web.Bridge.Model;
+using System.Runtime.CompilerServices;
namespace Snap.Hutao.Web.Response;
@@ -43,24 +44,12 @@ public class Response
///
/// 类型
/// 本体
+ /// 调用方法名称
/// 本体或默认值,当本体为 null 时 返回默认值
- public static Response DefaultIfNull(Response? response)
+ public static Response DefaultIfNull(Response? response, [CallerMemberName] string callerName = default!)
{
// 0x26F19335 is a magic number that hashed from "Snap.Hutao"
- return response ?? new(0x26F19335, $"[{typeof(TData).Name}] 请求异常", default);
- }
-
- ///
- /// 返回本体或带有消息提示的默认值
- ///
- /// 类型
- /// 本体
- /// 消息
- /// 本体或默认值,当本体为 null 时 返回默认值
- public static Response DefaultIfNull(Response? response, string message)
- {
- // 0x26F19335 is a magic number that hashed from "Snap.Hutao"
- return response ?? new(0x26F19335, message, default);
+ return response ?? new(0x26F19335, $"[{callerName}] 中的 [{typeof(TData).Name}] 请求异常", default);
}
///