From 6e8e151fff108799477b430c6bf3ce91f2f2b8a4 Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Thu, 14 Mar 2024 20:32:06 +0800 Subject: [PATCH] fix unlocking fps --- .../Snap.Hutao/Service/Game/LaunchStatus.cs | 2 +- .../LaunchExecutionUnlockFpsHandler.cs | 9 +++-- .../Service/Game/Unlocker/GameFpsAddress.cs | 2 +- .../Service/Game/Unlocker/GameFpsUnlocker.cs | 34 +++++++++---------- ...ckerState.cs => GameFpsUnlockerContext.cs} | 4 +-- .../Game/Unlocker/GameProcessModule.cs | 2 +- 6 files changed, 28 insertions(+), 25 deletions(-) rename src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/{GameFpsUnlockerState.cs => GameFpsUnlockerContext.cs} (83%) diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchStatus.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchStatus.cs index 0c4a305d..f1c8d91e 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchStatus.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchStatus.cs @@ -17,7 +17,7 @@ internal sealed class LaunchStatus public string Description { get; set; } - public static LaunchStatus FromUnlockState(GameFpsUnlockerState unlockerState) + public static LaunchStatus FromUnlockerContext(GameFpsUnlockerContext unlockerState) { if (unlockerState.FindModuleResult == FindModuleResult.Ok) { diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/Launching/Handler/LaunchExecutionUnlockFpsHandler.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/Launching/Handler/LaunchExecutionUnlockFpsHandler.cs index 28df662b..3a88a2d6 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/Launching/Handler/LaunchExecutionUnlockFpsHandler.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/Launching/Handler/LaunchExecutionUnlockFpsHandler.cs @@ -18,7 +18,7 @@ internal sealed class LaunchExecutionUnlockFpsHandler : ILaunchExecutionDelegate context.Progress.Report(new(LaunchPhase.UnlockingFps, SH.ServiceGameLaunchPhaseUnlockingFps)); IProgressFactory progressFactory = context.ServiceProvider.GetRequiredService(); - IProgress progress = progressFactory.CreateForMainThread(status => context.Progress.Report(LaunchStatus.FromUnlockState(status))); + IProgress progress = progressFactory.CreateForMainThread(c => context.Progress.Report(LaunchStatus.FromUnlockerContext(c))); GameFpsUnlocker unlocker = new(context.ServiceProvider, context.Process, new(100, 20000, 3000), progress); try @@ -28,7 +28,7 @@ internal sealed class LaunchExecutionUnlockFpsHandler : ILaunchExecutionDelegate unlocker.PostUnlockAsync(context.CancellationToken).SafeForget(); } } - catch (InvalidOperationException ex) + catch (Exception ex) { context.Logger.LogCritical(ex, "Unlocking FPS failed"); @@ -41,6 +41,9 @@ internal sealed class LaunchExecutionUnlockFpsHandler : ILaunchExecutionDelegate } } - await next().ConfigureAwait(false); + if (context.Result.Kind is LaunchExecutionResultKind.Ok) + { + await next().ConfigureAwait(false); + } } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsAddress.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsAddress.cs index 26e94494..24243004 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsAddress.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsAddress.cs @@ -16,7 +16,7 @@ internal static class GameFpsAddress private const byte ASM_JMP = 0xE9; #pragma warning restore SA1310 - public static unsafe void UnsafeFindFpsAddress(GameFpsUnlockerState state, in RequiredGameModule requiredGameModule) + public static unsafe void UnsafeFindFpsAddress(GameFpsUnlockerContext state, in RequiredGameModule requiredGameModule) { bool readOk = UnsafeReadModulesMemory(state.GameProcess, requiredGameModule, out VirtualMemory localMemory); HutaoException.ThrowIfNot(readOk, HutaoExceptionKind.GameFpsUnlockingFailed, SH.ServiceGameUnlockerReadModuleMemoryCopyVirtualMemoryFailed); diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsUnlocker.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsUnlocker.cs index 71ba37be..04b62d3f 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsUnlocker.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsUnlocker.cs @@ -16,46 +16,46 @@ namespace Snap.Hutao.Service.Game.Unlocker; internal sealed class GameFpsUnlocker : IGameFpsUnlocker { private readonly LaunchOptions launchOptions; - private readonly GameFpsUnlockerState state = new(); + private readonly GameFpsUnlockerContext context = new(); - public GameFpsUnlocker(IServiceProvider serviceProvider, Process gameProcess, in UnlockTimingOptions options, IProgress progress) + public GameFpsUnlocker(IServiceProvider serviceProvider, Process gameProcess, in UnlockTimingOptions options, IProgress progress) { launchOptions = serviceProvider.GetRequiredService(); - state.GameProcess = gameProcess; - state.TimingOptions = options; - state.Progress = progress; + context.GameProcess = gameProcess; + context.TimingOptions = options; + context.Progress = progress; } /// public async ValueTask UnlockAsync(CancellationToken token = default) { - HutaoException.ThrowIfNot(state.IsUnlockerValid, HutaoExceptionKind.GameFpsUnlockingFailed, "This Unlocker is invalid"); - (FindModuleResult result, RequiredGameModule gameModule) = await GameProcessModule.FindModuleAsync(state).ConfigureAwait(false); + HutaoException.ThrowIfNot(context.IsUnlockerValid, HutaoExceptionKind.GameFpsUnlockingFailed, "This Unlocker is invalid"); + (FindModuleResult result, RequiredGameModule gameModule) = await GameProcessModule.FindModuleAsync(context).ConfigureAwait(false); HutaoException.ThrowIfNot(result != FindModuleResult.TimeLimitExeeded, HutaoExceptionKind.GameFpsUnlockingFailed, SH.ServiceGameUnlockerFindModuleTimeLimitExeeded); HutaoException.ThrowIfNot(result != FindModuleResult.NoModuleFound, HutaoExceptionKind.GameFpsUnlockingFailed, SH.ServiceGameUnlockerFindModuleNoModuleFound); - GameFpsAddress.UnsafeFindFpsAddress(state, gameModule); - state.Report(); - return state.FpsAddress != 0U; + GameFpsAddress.UnsafeFindFpsAddress(context, gameModule); + context.Report(); + return context.FpsAddress != 0U; } public async ValueTask PostUnlockAsync(CancellationToken token = default) { - using (PeriodicTimer timer = new(state.TimingOptions.AdjustFpsDelay)) + using (PeriodicTimer timer = new(context.TimingOptions.AdjustFpsDelay)) { while (await timer.WaitForNextTickAsync(token).ConfigureAwait(false)) { - if (!state.GameProcess.HasExited && state.FpsAddress != 0U) + if (!context.GameProcess.HasExited && context.FpsAddress != 0U) { - UnsafeWriteProcessMemory(state.GameProcess, state.FpsAddress, launchOptions.TargetFps); - state.Report(); + UnsafeWriteProcessMemory(context.GameProcess, context.FpsAddress, launchOptions.TargetFps); + context.Report(); } else { - state.IsUnlockerValid = false; - state.FpsAddress = 0; - state.Report(); + context.IsUnlockerValid = false; + context.FpsAddress = 0; + context.Report(); return; } } diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsUnlockerState.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsUnlockerContext.cs similarity index 83% rename from src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsUnlockerState.cs rename to src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsUnlockerContext.cs index d7897b4b..ab674d18 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsUnlockerState.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsUnlockerContext.cs @@ -8,7 +8,7 @@ namespace Snap.Hutao.Service.Game.Unlocker; /// /// 解锁状态 /// -internal sealed class GameFpsUnlockerState +internal sealed class GameFpsUnlockerContext { public string Description { get; set; } = default!; @@ -22,7 +22,7 @@ internal sealed class GameFpsUnlockerState public Process GameProcess { get; set; } = default!; - public IProgress Progress { get; set; } = default!; + public IProgress Progress { get; set; } = default!; public void Report() { diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameProcessModule.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameProcessModule.cs index 7831d02b..ece2ad89 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameProcessModule.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameProcessModule.cs @@ -13,7 +13,7 @@ namespace Snap.Hutao.Service.Game.Unlocker; internal static class GameProcessModule { - public static async ValueTask> FindModuleAsync(GameFpsUnlockerState state) + public static async ValueTask> FindModuleAsync(GameFpsUnlockerContext state) { ValueStopwatch watch = ValueStopwatch.StartNew(); using (PeriodicTimer timer = new(state.TimingOptions.FindModuleDelay))