From fbd34e61f55a388540552f6c73c32253abb1ff52 Mon Sep 17 00:00:00 2001 From: Milkitic Date: Wed, 31 Jan 2024 16:47:15 +0800 Subject: [PATCH] Update interface --- unlockfps_gui/App.axaml.cs | 1 + unlockfps_gui/Services/ProcessService.cs | 49 ++++++++++++++---------- unlockfps_gui/Views/MainWindow.axaml.cs | 1 + 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/unlockfps_gui/App.axaml.cs b/unlockfps_gui/App.axaml.cs index 77e36d6..c1774d7 100644 --- a/unlockfps_gui/App.axaml.cs +++ b/unlockfps_gui/App.axaml.cs @@ -31,6 +31,7 @@ public override void RegisterServices() services.AddTransient(); services.AddTransient(); services.AddSingleton(); + services.AddSingleton(); DefaultServices = services.BuildServiceProvider(); } diff --git a/unlockfps_gui/Services/ProcessService.cs b/unlockfps_gui/Services/ProcessService.cs index b7f3739..d2b8f2a 100644 --- a/unlockfps_gui/Services/ProcessService.cs +++ b/unlockfps_gui/Services/ProcessService.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Avalonia; using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.Threading; using Microsoft.Extensions.DependencyInjection; using UnlockFps.Gui.Model; using UnlockFps.Gui.Utils; @@ -64,9 +65,9 @@ public async ValueTask StartAsync() { if (IsGameRunning()) { - var infoWindow = App.DefaultServices.GetRequiredService(); - infoWindow.Text = "An instance of the game is already running."; - await infoWindow.ShowDialog(App.CurrentMainWindow!); + await ShowErrorMessage( + "An instance of the game is already running." + ); return false; } @@ -123,19 +124,17 @@ private async Task Worker() if (!Native.CreateProcess(_config.GamePath, BuildCommandLine(), IntPtr.Zero, IntPtr.Zero, false, creationFlag, IntPtr.Zero, gameFolder, ref si, out var pi)) { - var infoWindow = App.DefaultServices.GetRequiredService(); - infoWindow.Text = - $"CreateProcess failed ({Marshal.GetLastWin32Error()}){Environment.NewLine} {Marshal.GetLastPInvokeErrorMessage()}"; - await infoWindow.ShowDialog(App.CurrentMainWindow!); + await ShowErrorMessage( + $"CreateProcess failed ({Marshal.GetLastWin32Error()}){Environment.NewLine} {Marshal.GetLastPInvokeErrorMessage()}" + ); return; } if (!ProcessUtils.InjectDlls(pi.hProcess, _config.DllList)) { - var infoWindow = App.DefaultServices.GetRequiredService(); - infoWindow.Text = - $"Dll Injection failed ({Marshal.GetLastWin32Error()}){Environment.NewLine} {Marshal.GetLastPInvokeErrorMessage()}"; - await infoWindow.ShowDialog(App.CurrentMainWindow!); + await ShowErrorMessage( + $"Dll Injection failed ({Marshal.GetLastWin32Error()}){Environment.NewLine} {Marshal.GetLastPInvokeErrorMessage()}" + ); } if (_config.SuspendLoad) @@ -212,9 +211,9 @@ private unsafe bool SetupData() if (!pUnityPlayer || !pUserAssembly) { - var infoWindow = App.DefaultServices.GetRequiredService(); - infoWindow.Text = "Failed to load UnityPlayer.dll or UserAssembly.dll"; - infoWindow.ShowDialog(App.CurrentMainWindow!); + ShowErrorMessage( + "Failed to load UnityPlayer.dll or UserAssembly.dll" + ); return false; } @@ -281,9 +280,9 @@ private unsafe bool SetupData() return true; BAD_PATTERN: - var infoWindow1 = App.DefaultServices.GetRequiredService(); - infoWindow1.Text = "outdated fps pattern"; - infoWindow1.ShowDialog(App.CurrentMainWindow!); + ShowErrorMessage( + "outdated fps pattern" + ); return false; } @@ -308,13 +307,23 @@ private async Task UpdateRemoteModules() if (_remoteUnityPlayer == IntPtr.Zero || _remoteUserAssembly == IntPtr.Zero) { - var infoWindow1 = App.DefaultServices.GetRequiredService(); - infoWindow1.Text = "Failed to get remote module base address"; - await infoWindow1.ShowDialog(App.CurrentMainWindow!); + await ShowErrorMessage( + "Failed to get remote module base address" + ); return false; } return true; } + private static async Task ShowErrorMessage(string infoWindowText) + { + await Dispatcher.UIThread.InvokeAsync(async () => + { + var infoWindow = App.DefaultServices.GetRequiredService(); + infoWindow.Text = + infoWindowText; + await infoWindow.ShowDialog(App.CurrentMainWindow!); + }); + } } \ No newline at end of file diff --git a/unlockfps_gui/Views/MainWindow.axaml.cs b/unlockfps_gui/Views/MainWindow.axaml.cs index 279d172..9f86540 100644 --- a/unlockfps_gui/Views/MainWindow.axaml.cs +++ b/unlockfps_gui/Views/MainWindow.axaml.cs @@ -78,6 +78,7 @@ private async void BtnLaunchGame_OnClick(object? sender, RoutedEventArgs e) await MainWindowViewModel.ShowWindow(); } + if (!File.Exists(_viewModel.Config.GamePath)) return; if (await _processService.StartAsync()) { WindowState = WindowState.Minimized;