fix unlocking fps

This commit is contained in:
DismissedLight
2024-03-14 20:32:06 +08:00
parent b98dc9f5d3
commit 6e8e151fff
6 changed files with 28 additions and 25 deletions

View File

@@ -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)
{

View File

@@ -18,7 +18,7 @@ internal sealed class LaunchExecutionUnlockFpsHandler : ILaunchExecutionDelegate
context.Progress.Report(new(LaunchPhase.UnlockingFps, SH.ServiceGameLaunchPhaseUnlockingFps));
IProgressFactory progressFactory = context.ServiceProvider.GetRequiredService<IProgressFactory>();
IProgress<GameFpsUnlockerState> progress = progressFactory.CreateForMainThread<GameFpsUnlockerState>(status => context.Progress.Report(LaunchStatus.FromUnlockState(status)));
IProgress<GameFpsUnlockerContext> progress = progressFactory.CreateForMainThread<GameFpsUnlockerContext>(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);
}
}
}

View File

@@ -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);

View File

@@ -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<GameFpsUnlockerState> progress)
public GameFpsUnlocker(IServiceProvider serviceProvider, Process gameProcess, in UnlockTimingOptions options, IProgress<GameFpsUnlockerContext> progress)
{
launchOptions = serviceProvider.GetRequiredService<LaunchOptions>();
state.GameProcess = gameProcess;
state.TimingOptions = options;
state.Progress = progress;
context.GameProcess = gameProcess;
context.TimingOptions = options;
context.Progress = progress;
}
/// <inheritdoc/>
public async ValueTask<bool> 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;
}
}

View File

@@ -8,7 +8,7 @@ namespace Snap.Hutao.Service.Game.Unlocker;
/// <summary>
/// 解锁状态
/// </summary>
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<GameFpsUnlockerState> Progress { get; set; } = default!;
public IProgress<GameFpsUnlockerContext> Progress { get; set; } = default!;
public void Report()
{

View File

@@ -13,7 +13,7 @@ namespace Snap.Hutao.Service.Game.Unlocker;
internal static class GameProcessModule
{
public static async ValueTask<ValueResult<FindModuleResult, RequiredGameModule>> FindModuleAsync(GameFpsUnlockerState state)
public static async ValueTask<ValueResult<FindModuleResult, RequiredGameModule>> FindModuleAsync(GameFpsUnlockerContext state)
{
ValueStopwatch watch = ValueStopwatch.StartNew();
using (PeriodicTimer timer = new(state.TimingOptions.FindModuleDelay))