Skip to content

Commit

Permalink
resume unlock state
Browse files Browse the repository at this point in the history
  • Loading branch information
qhy040404 committed Mar 2, 2025
1 parent 756da5f commit 8a456e2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
19 changes: 11 additions & 8 deletions src/Snap.Hutao/Snap.Hutao/Service/Game/Unlocker/GameFpsUnlocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public GameFpsUnlocker(IServiceProvider serviceProvider, Process gameProcess, st
context.Logger = serviceProvider.GetRequiredService<ILogger<GameFpsUnlocker>>();
}

public async ValueTask<bool> UnlockAsync(CancellationToken token = default)
public async ValueTask<bool> UnlockAsync(bool resume = false, CancellationToken token = default)
{
HutaoException.ThrowIfNot(context.IsUnlockerValid, "This Unlocker is invalid");

Expand All @@ -59,14 +59,17 @@ public async ValueTask<bool> UnlockAsync(CancellationToken token = default)

DebugReplaceOffsets(ref offsets);

try
{
InstalledLocation.CopyFileFromApplicationUri("ms-appx:///Snap.Hutao.UnlockerIsland.dll", dataFolderIslandPath);
}
catch
if (!resume)
{
context.Logger.LogError("Failed to copy island file.");
throw;
try
{
InstalledLocation.CopyFileFromApplicationUri("ms-appx:///Snap.Hutao.UnlockerIsland.dll", dataFolderIslandPath);
}
catch
{
context.Logger.LogError("Failed to copy island file.");
throw;
}
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ internal interface IGameFpsUnlocker
{
ValueTask PostUnlockAsync(CancellationToken token = default);

ValueTask<bool> UnlockAsync(CancellationToken token = default);
ValueTask<bool> UnlockAsync(bool resume = false, CancellationToken token = default);
}
39 changes: 34 additions & 5 deletions src/Snap.Hutao/Snap.Hutao/ViewModel/Game/LaunchGameViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Snap.Hutao.Service.Game.Locator;
using Snap.Hutao.Service.Game.PathAbstraction;
using Snap.Hutao.Service.Game.Scheme;
using Snap.Hutao.Service.Game.Unlocker;
using Snap.Hutao.Service.Navigation;
using Snap.Hutao.Service.Notification;
using Snap.Hutao.Service.User;
Expand All @@ -21,6 +22,7 @@
using Snap.Hutao.Web.Hoyolab.HoyoPlay.Connect.Package;
using Snap.Hutao.Web.Response;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;

namespace Snap.Hutao.ViewModel.Game;
Expand Down Expand Up @@ -197,10 +199,7 @@ protected override async ValueTask<bool> LoadOverrideAsync()
await serviceProvider.GetRequiredService<IGamePathService>().SilentLocateAllGamePathAsync().ConfigureAwait(false);
}

if (LaunchOptions.IsGameRunning)
{
WaitForGameProcessExitAsync().SafeForget(logger);
}
ResumeLaunchExecutionAsync().SafeForget(logger);

await taskContext.SwitchToMainThreadAsync();
this.SetGamePathEntriesAndSelectedGamePathEntry(LaunchOptions);
Expand Down Expand Up @@ -368,14 +367,44 @@ private async Task SetSelectedSchemeAsync(LaunchScheme? value)
}
}

private async ValueTask WaitForGameProcessExitAsync()
private async ValueTask ResumeLaunchExecutionAsync()
{
await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding);
if (!LaunchExecutionEnsureGameNotRunningHandler.IsGameRunning(out Process? gameProcess))
{
return;
}

taskContext.BeginInvokeOnMainThread(() => CanResetGamePathEntry = false);

if (HutaoRuntime.IsProcessElevated && LaunchOptions.IsIslandEnabled)
{
if (!LaunchOptions.TryGetGameFileSystem(out IGameFileSystem? gameFileSystem))
{
return;
}

using (gameFileSystem)
{
if (!gameFileSystem.TryGetGameVersion(out string? gameVersion))
{
return;
}

GameFpsUnlocker unlocker = new(serviceProvider, gameProcess, gameVersion);
if (await unlocker.UnlockAsync(true).ConfigureAwait(false))
{
await unlocker.PostUnlockAsync().ConfigureAwait(false);
}
}
}

unsafe
{
SpinWaitPolyfill.SpinWhile(&LaunchExecutionEnsureGameNotRunningHandler.IsGameRunning);
}

serviceProvider.GetRequiredService<IMessenger>().Send<LaunchExecutionProcessStatusChangedMessage>();
taskContext.BeginInvokeOnMainThread(() => CanResetGamePathEntry = true);
}
}

0 comments on commit 8a456e2

Please sign in to comment.