From cec4b2a23aafa85d842adaca99b8aed4382714cc Mon Sep 17 00:00:00 2001
From: HolographicHat <58809250+HolographicHat@users.noreply.github.com>
Date: Tue, 14 Mar 2023 15:53:28 +0000
Subject: [PATCH 1/2] Use exclusive access instead of renaming
---
.../Snap.Hutao/Service/Game/GameService.cs | 2 +-
.../Snap.Hutao/Service/Game/ProcessInterop.cs | 19 ++++++++++++-------
2 files changed, 13 insertions(+), 8 deletions(-)
diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/GameService.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/GameService.cs
index 97d78cc2..6c7d14ac 100644
--- a/src/Snap.Hutao/Snap.Hutao/Service/Game/GameService.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/GameService.cs
@@ -306,7 +306,7 @@ internal sealed class GameService : IGameService
game.Start();
if (isElevated && launchOptions.MultipleInstances)
{
- await ProcessInterop.DisableProtectionAsync(gamePath).ConfigureAwait(false);
+ await ProcessInterop.DisableProtectionAsync(game, gamePath).ConfigureAwait(false);
}
if (isElevated && launchOptions.UnlockFps)
diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/ProcessInterop.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/ProcessInterop.cs
index 8c284af0..d5fcb0fb 100644
--- a/src/Snap.Hutao/Snap.Hutao/Service/Game/ProcessInterop.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/ProcessInterop.cs
@@ -6,6 +6,7 @@ using Snap.Hutao.Core.IO;
using Snap.Hutao.Service.Game.Unlocker;
using System.Diagnostics;
using System.IO;
+using System.Runtime.InteropServices;
namespace Snap.Hutao.Service.Game;
@@ -65,19 +66,23 @@ internal static class ProcessInterop
///
/// 尝试禁用mhypbase
///
+ /// 游戏进程
/// 游戏路径
/// 是否禁用成功
- public static async Task DisableProtectionAsync(string gamePath)
+ public static async Task DisableProtectionAsync(Process game, string gamePath)
{
string? gameFolder = Path.GetDirectoryName(gamePath);
if (!string.IsNullOrEmpty(gameFolder))
{
- string mhypbaseDll = Path.Combine(gameFolder, "mhypbase.dll");
- string mhypbaseDllBackup = Path.Combine(gameFolder, "mhypbase.dll.backup");
-
- File.Move(mhypbaseDll, mhypbaseDllBackup, true);
- await Task.Delay(TimeSpan.FromSeconds(12)).ConfigureAwait(false);
- File.Move(mhypbaseDllBackup, mhypbaseDll, true);
+ string pbasePath = Path.Combine(gameFolder, "mhypbase.dll");
+ SafeHandle handle = File.OpenHandle(pbasePath, share: FileShare.None);
+ while (true) {
+ if (game.Any(process => process.MainWindowHandle != nint.Zero)) {
+ handle.Close();
+ break;
+ }
+ await Task.Delay(100).ConfigureAwait(false);
+ }
return true;
}
From 770cabce816cf3b7850f7323ad011e531211e8de Mon Sep 17 00:00:00 2001
From: HolographicHat <58809250+HolographicHat@users.noreply.github.com>
Date: Wed, 15 Mar 2023 00:23:14 +0800
Subject: [PATCH 2/2] Update ProcessInterop.cs
---
src/Snap.Hutao/Snap.Hutao/Service/Game/ProcessInterop.cs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Game/ProcessInterop.cs b/src/Snap.Hutao/Snap.Hutao/Service/Game/ProcessInterop.cs
index d5fcb0fb..ee12a971 100644
--- a/src/Snap.Hutao/Snap.Hutao/Service/Game/ProcessInterop.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Service/Game/ProcessInterop.cs
@@ -77,7 +77,7 @@ internal static class ProcessInterop
string pbasePath = Path.Combine(gameFolder, "mhypbase.dll");
SafeHandle handle = File.OpenHandle(pbasePath, share: FileShare.None);
while (true) {
- if (game.Any(process => process.MainWindowHandle != nint.Zero)) {
+ if (game.MainWindowHandle != nint.Zero) {
handle.Close();
break;
}
@@ -88,4 +88,4 @@ internal static class ProcessInterop
return false;
}
-}
\ No newline at end of file
+}