diff --git a/BetterGenshinImpact/App.xaml.cs b/BetterGenshinImpact/App.xaml.cs index c49ce5a0..1f997d2c 100644 --- a/BetterGenshinImpact/App.xaml.cs +++ b/BetterGenshinImpact/App.xaml.cs @@ -23,6 +23,7 @@ using System.IO; using System.Threading.Tasks; using System.Windows; using BetterGenshinImpact.Helpers.Device; +using BetterGenshinImpact.Service.Singletons; using BetterGenshinImpact.View.Pages.View; using BetterGenshinImpact.ViewModel.Pages.OneDragon; using BetterGenshinImpact.ViewModel.Pages.View; @@ -155,8 +156,7 @@ public partial class App : Application /// protected override async void OnStartup(StartupEventArgs e) { - var homePageViewModel = GetService(); - homePageViewModel?.ChangeResolution(); + StartEndSingleton.Instance.OnStartup(); base.OnStartup(e); @@ -183,12 +183,7 @@ public partial class App : Application /// protected override async void OnExit(ExitEventArgs e) { - TouchpadSoft.Instance.RestoreTouchpadByHotKey(); - - var homePageViewModel = GetService(); - homePageViewModel?.ResetResolution(); - SysDpi.Instance.ResetDpi(); - + StartEndSingleton.Instance.OnExit(); base.OnExit(e); await _host.StopAsync(); diff --git a/BetterGenshinImpact/Core/Config/CommonConfig.cs b/BetterGenshinImpact/Core/Config/CommonConfig.cs index 83a5a95c..5c92e1f6 100644 --- a/BetterGenshinImpact/Core/Config/CommonConfig.cs +++ b/BetterGenshinImpact/Core/Config/CommonConfig.cs @@ -34,4 +34,12 @@ public partial class CommonConfig : ObservableObject [ObservableProperty] private string _ffmpegCommand = " -f gdigrab -framerate 60 -use_wallclock_as_timestamps 1 -i title=原神 -pix_fmt yuv420p -c:v libx264 -preset ultrafast -f segment -segment_time 1800 -reset_timestamps 1 -strftime 1 "; + + // 启动时修改分辨率 + [ObservableProperty] + private bool _changeResolutionOnStart = true; + + // 关闭时还原分辨率 + [ObservableProperty] + private bool _restoreResolutionOnExit; } diff --git a/BetterGenshinImpact/Helpers/Device/Resolution.cs b/BetterGenshinImpact/Helpers/Device/Resolution.cs index 11fb75df..81aade70 100644 --- a/BetterGenshinImpact/Helpers/Device/Resolution.cs +++ b/BetterGenshinImpact/Helpers/Device/Resolution.cs @@ -1,13 +1,15 @@ using System; using System.Runtime.InteropServices; using System.Windows.Forms; +using BetterGenshinImpact.GameTask; +using BetterGenshinImpact.GameTask.Common; namespace BetterGenshinImpact.Helpers.Device; public class Resolution { - public int autoWidth = Screen.PrimaryScreen.Bounds.Width; - public int autoHeight = Screen.PrimaryScreen.Bounds.Height; + public int autoWidth = PrimaryScreen.WorkingArea.Width; + public int autoHeight = PrimaryScreen.WorkingArea.Height; [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct DEVMODE @@ -113,7 +115,7 @@ public class Resolution } default: { - System.Windows.Forms.MessageBox.Show("改变屏幕分辨率失败,错误码:" + iRet, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); + System.Windows.Forms.MessageBox.Show($"改变屏幕分辨率失败,目标{width}x{height},错误码:{iRet}", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); break; } } diff --git a/BetterGenshinImpact/Service/Singletons/StartEndSingleton.cs b/BetterGenshinImpact/Service/Singletons/StartEndSingleton.cs new file mode 100644 index 00000000..26e40b2e --- /dev/null +++ b/BetterGenshinImpact/Service/Singletons/StartEndSingleton.cs @@ -0,0 +1,62 @@ +using System; +using System.IO; +using BetterGenshinImpact.Core.Config; +using BetterGenshinImpact.GameTask; +using BetterGenshinImpact.GameTask.Common; +using BetterGenshinImpact.Helpers.Device; +using BetterGenshinImpact.Model; +using BetterGenshinImpact.ViewModel.Pages; +using Microsoft.Extensions.Logging; + +namespace BetterGenshinImpact.Service.Singletons; + +public class StartEndSingleton: Singleton +{ + private Resolution? _resolution; + + public void OnStartup() + { + + } + + public void OnMainWindowLoad() + { + + TouchpadSoft.Instance.CheckAndRecordStatus(); + TouchpadSoft.Instance.DisableTouchpadWhenEnabledByHotKey(); + + + if (TaskContext.Instance().Config.CommonConfig.ChangeResolutionOnStart) + { + ChangeResolution(); + // 设置DPI + SysDpi.Instance.SetDpi(); + } + + + } + + public void OnExit() + { + TouchpadSoft.Instance.RestoreTouchpadByHotKey(); + + if (TaskContext.Instance().Config.CommonConfig.RestoreResolutionOnExit) + { + ResetResolution(); + SysDpi.Instance.ResetDpi(); + } + + } + + + public void ChangeResolution() + { + _resolution = new Resolution(); + _resolution.ChangeResolution(1920, 1080); + } + + public void ResetResolution() + { + _resolution?.ChangeResolution(_resolution.autoWidth, _resolution.autoHeight); + } +} \ No newline at end of file diff --git a/BetterGenshinImpact/View/Pages/CommonSettingsPage.xaml b/BetterGenshinImpact/View/Pages/CommonSettingsPage.xaml index 288b8908..1a5f27c5 100644 --- a/BetterGenshinImpact/View/Pages/CommonSettingsPage.xaml +++ b/BetterGenshinImpact/View/Pages/CommonSettingsPage.xaml @@ -424,6 +424,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/BetterGenshinImpact/ViewModel/MainWindowViewModel.cs b/BetterGenshinImpact/ViewModel/MainWindowViewModel.cs index 7c2ed8f3..26044231 100644 --- a/BetterGenshinImpact/ViewModel/MainWindowViewModel.cs +++ b/BetterGenshinImpact/ViewModel/MainWindowViewModel.cs @@ -22,6 +22,7 @@ using System.Windows; using System.Windows.Forms; using System.Windows.Interop; using BetterGenshinImpact.Helpers.Device; +using BetterGenshinImpact.Service.Singletons; using Wpf.Ui; using Application = System.Windows.Application; @@ -129,25 +130,8 @@ public partial class MainWindowViewModel : ObservableObject, IViewModel EnvironmentUtil.PrintMouseSettings(); - - TouchpadSoft.Instance.CheckAndRecordStatus(); - TouchpadSoft.Instance.DisableTouchpadWhenEnabledByHotKey(); - try - { - var json = GetPCInfo.GetJson(); - // 保存 - File.WriteAllText(Global.Absolute("User\\pc.json"), json); - } - catch (Exception e) - { - _logger.LogError("获取PC信息失败:" + e.Source + "\r\n--" + Environment.NewLine + e.StackTrace + "\r\n---" + Environment.NewLine + e.Message); - } - - - - // 设置DPI - SysDpi.Instance.SetDpi(); + StartEndSingleton.Instance.OnMainWindowLoad(); } diff --git a/BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs index 7d1b323c..63a82849 100644 --- a/BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs @@ -63,8 +63,6 @@ public partial class HomePageViewModel : ObservableObject, INavigationAware, IVi [ObservableProperty] private string[] _inferenceDeviceTypes = BgiSessionOption.InferenceDeviceTypes; - Resolution? resolution; - public HomePageViewModel(IConfigService configService, TaskTriggerDispatcher taskTriggerDispatcher) { _taskDispatcher = taskTriggerDispatcher; @@ -380,15 +378,4 @@ public partial class HomePageViewModel : ObservableObject, INavigationAware, IVi win.NavigateToHtml(html); win.ShowDialog(); } - - public void ChangeResolution() - { - resolution = new Resolution(); - resolution.ChangeResolution(1920, 1080); - } - - public void ResetResolution() - { - resolution?.ChangeResolution(resolution.autoWidth, resolution.autoHeight); - } } \ No newline at end of file diff --git a/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs index 7b139f52..c037a4ae 100644 --- a/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs @@ -17,6 +17,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows; +using System.Windows.Interop; using BetterGenshinImpact.Core.Recorder.Model; using BetterGenshinImpact.GameTask.Common; using BetterGenshinImpact.Helpers; @@ -54,31 +55,31 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation private void InitScriptListViewData() { _scriptItems.Clear(); - var fileInfos = LoadScriptFiles(scriptPath) - .OrderByDescending(f => f.CreationTime) + var directoryInfos = LoadScriptDirectories(scriptPath) + .OrderByDescending(d => d.CreationTime) .ToList(); - foreach (var f in fileInfos) + foreach (var d in directoryInfos) { _scriptItems.Add(new KeyMouseScriptItem { - Name = f.Name, - Path = f.FullName, - CreateTime = f.CreationTime, - CreateTimeStr = f.CreationTime.ToString("yyyy-MM-dd HH:mm:ss") + Name = d.Name, + Path = d.FullName, + CreateTime = d.CreationTime, + CreateTimeStr = d.CreationTime.ToString("yyyy-MM-dd HH:mm:ss") }); } } - private List LoadScriptFiles(string folder) + private List LoadScriptDirectories(string folder) { if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } - var files = Directory.GetFiles(folder, "*.json", SearchOption.AllDirectories); + var directories = Directory.GetDirectories(folder); - return files.Select(file => new FileInfo(file)).ToList(); + return directories.Select(dir => new DirectoryInfo(dir)).ToList(); } public void OnNavigatedTo() @@ -120,21 +121,36 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation } catch (Exception e) { - await MessageBox.ShowAsync(e.Message, "校验错误", System.Windows.MessageBoxButton.OK, MessageBoxImage.Error); + // await MessageBox.ShowAsync(e.Message, "校验错误", System.Windows.MessageBoxButton.OK, MessageBoxImage.Error); + // SystemControl.ActivateWindow(new WindowInteropHelper(UIDispatcherHelper.MainWindow).Handle); + Toast.Error(e.Message); + _logger.LogError(e.Message); return; } } + fileName = $"{DateTime.Now:yyyy_MM_dd_HH_mm_ss}"; + + try + { + var json = GetPCInfo.GetJson(); + // 保存 + await File.WriteAllTextAsync(Global.Absolute(@$"User/KeyMouseScript/{fileName}/pc.json"), json); + } + catch (Exception e) + { + TaskControl.Logger.LogDebug("获取PC信息失败:" + e.Source + "\r\n--" + Environment.NewLine + e.StackTrace + "\r\n---" + Environment.NewLine + e.Message); + } + if (!IsRecording) { IsRecording = true; SystemSettingsManager.GetSystemSettings(); SystemSettingsManager.SetSystemSettings(); - - fileName = $"{DateTime.Now:yyyy_MM_dd_HH_mm_ss}"; + await GlobalKeyMouseRecord.Instance.StartRecord(fileName); } } @@ -165,13 +181,13 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation [RelayCommand] public async Task OnStartPlay(string path) { - var file = new FileInfo(path); - var name = file.Name; + var file = new FileInfo(Path.Combine(path, "systemInfo.json")); + var name = file.Directory?.Name; _logger.LogInformation("重放开始:{Name}", name); try { await new TaskRunner(DispatcherTimerOperationEnum.UseSelfCaptureImage) - .RunAsync(async () => await KeyMouseMacroPlayerJsonLine.PlayMacro(path, CancellationContext.Instance.Cts.Token)); + .RunAsync(async () => await KeyMouseMacroPlayerJsonLine.PlayMacro(file.FullName, CancellationContext.Instance.Cts.Token)); } catch (Exception e) { @@ -244,7 +260,7 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation try { - var path = new FileInfo(item.Path).Directory!.FullName; + var path = item.Path; // 校验文件夹是否在 scriptPath 下 if (!path.StartsWith(scriptPath)) {