From fdcefc3a692256243e27cf4e732cee9c052f5769 Mon Sep 17 00:00:00 2001
From: Zhanyuan Guo <2364319479@qq.com>
Date: Mon, 21 Oct 2024 00:36:24 +0800
Subject: [PATCH] feat(#3767):restricted multi-instance under same MAA (#10869)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* feat(#3767):restricted multi-instance under same MAA.exe
使用了 @THSLP13 的方案。
* Revert "feat(#3767):restricted multi-instance under same MAA.exe"
This reverts commit 53f50c646d83ab09d3fb4c4ddf6f11aae06afffe.
* feat(#3767):restricted multi-instance for the same MAA
取消注释并使用变量记录互斥锁存在情况,拆分了提示信息的i18n。
---
src/MaaWpfGui/Main/Bootstrapper.cs | 50 +++++++++++-----------
src/MaaWpfGui/Res/Localizations/en-us.xaml | 3 ++
src/MaaWpfGui/Res/Localizations/ja-jp.xaml | 3 ++
src/MaaWpfGui/Res/Localizations/ko-kr.xaml | 3 ++
src/MaaWpfGui/Res/Localizations/zh-cn.xaml | 3 ++
src/MaaWpfGui/Res/Localizations/zh-tw.xaml | 3 ++
6 files changed, 40 insertions(+), 25 deletions(-)
diff --git a/src/MaaWpfGui/Main/Bootstrapper.cs b/src/MaaWpfGui/Main/Bootstrapper.cs
index 5537562260..cfc7685504 100644
--- a/src/MaaWpfGui/Main/Bootstrapper.cs
+++ b/src/MaaWpfGui/Main/Bootstrapper.cs
@@ -18,6 +18,7 @@ using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Principal;
+using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
@@ -51,30 +52,13 @@ namespace MaaWpfGui.Main
private static readonly RunningState _runningState = RunningState.Instance;
private static ILogger _logger = Logger.None;
- // private static Mutex _mutex;
+ private static Mutex _mutex;
+ private static bool hasMutex = false;
///
/// 初始化些啥自己加。
protected override void OnStart()
{
- /*
- // 设置互斥量的名称
- string mutexName = "MAA_" + Directory.GetCurrentDirectory().Replace("\\", "_").Replace(":", string.Empty);
- _mutex = new Mutex(true, mutexName);
-
- if (!_mutex.WaitOne(TimeSpan.Zero, true))
- {
- // 这里还没加载语言包,就不 GetString 了
- MessageBox.Show("同一路径下只能启动一个实例!\n\n" +
- "同一路徑下只能啟動一個實例!\n\n" +
- "Only one instance can be launched under the same path!\n\n" +
- "同じパスの下で1つのインスタンスしか起動できません!\n\n" +
- "동일한 경로에는 하나의 인스턴스만 실행할 수 있습니다!");
- Bootstrapper.Shutdown();
- return;
- }
- */
-
Directory.SetCurrentDirectory(AppContext.BaseDirectory);
if (!Directory.Exists("debug"))
{
@@ -179,6 +163,21 @@ namespace MaaWpfGui.Main
base.OnStart();
ConfigurationHelper.Load();
LocalizationHelper.Load();
+
+ // 设置互斥量的名称
+ string mutexName = "MAA_" + Directory.GetCurrentDirectory().Replace("\\", "_").Replace(":", string.Empty);
+ _mutex = new Mutex(true, mutexName);
+
+ if (!_mutex.WaitOne(TimeSpan.Zero, true))
+ {
+ // 这里还没加载语言包,就不 GetString 了
+ MessageBox.Show(LocalizationHelper.GetString("MultiInstanceUnderSamePath"));
+ Bootstrapper.Shutdown();
+ return;
+ }
+
+ hasMutex = true;
+
ETagCache.Load();
// 检查 MaaCore.dll 是否存在
@@ -244,12 +243,6 @@ namespace MaaWpfGui.Main
/// 退出时执行啥自己加。
protected override void OnExit(ExitEventArgs e)
{
- /*
- // 释放互斥量
- _mutex?.ReleaseMutex();
- _mutex?.Dispose();
- */
-
// MessageBox.Show("O(∩_∩)O 拜拜");
Release();
@@ -287,6 +280,13 @@ namespace MaaWpfGui.Main
ConfigurationHelper.Release();
ConfigFactory.Release();
+
+ // 释放互斥量
+ if (hasMutex)
+ {
+ _mutex?.ReleaseMutex();
+ _mutex?.Dispose();
+ }
}
private static bool _isRestartingWithoutArgs;
diff --git a/src/MaaWpfGui/Res/Localizations/en-us.xaml b/src/MaaWpfGui/Res/Localizations/en-us.xaml
index c719e06ca5..ebae1d7859 100644
--- a/src/MaaWpfGui/Res/Localizations/en-us.xaml
+++ b/src/MaaWpfGui/Res/Localizations/en-us.xaml
@@ -906,4 +906,7 @@ With save file: farm points through crafting (advanced).
Support prop name
Glow Stick
+
+ Only one instance can be launched under the same path!
+
diff --git a/src/MaaWpfGui/Res/Localizations/ja-jp.xaml b/src/MaaWpfGui/Res/Localizations/ja-jp.xaml
index e2cdf1a45d..e77aa1ec82 100644
--- a/src/MaaWpfGui/Res/Localizations/ja-jp.xaml
+++ b/src/MaaWpfGui/Res/Localizations/ja-jp.xaml
@@ -909,4 +909,7 @@ C:\\leidian\\LDPlayer9\n
支援道具名
ケミカルライト
+
+ 同じパスの下で1つのインスタンスしか起動できません!
+
diff --git a/src/MaaWpfGui/Res/Localizations/ko-kr.xaml b/src/MaaWpfGui/Res/Localizations/ko-kr.xaml
index 22ea78ff35..1f57cd5267 100644
--- a/src/MaaWpfGui/Res/Localizations/ko-kr.xaml
+++ b/src/MaaWpfGui/Res/Localizations/ko-kr.xaml
@@ -907,4 +907,7 @@ C:\\leidian\\LDPlayer9\n
제작할 도구 이름
형광봉 (荧光棒)
+
+ 동일한 경로에는 하나의 인스턴스만 실행할 수 있습니다!
+
diff --git a/src/MaaWpfGui/Res/Localizations/zh-cn.xaml b/src/MaaWpfGui/Res/Localizations/zh-cn.xaml
index 6f4b7c87d6..b6c4ad95b2 100644
--- a/src/MaaWpfGui/Res/Localizations/zh-cn.xaml
+++ b/src/MaaWpfGui/Res/Localizations/zh-cn.xaml
@@ -909,4 +909,7 @@ C:\\leidian\\LDPlayer9。\n\n
支援道具名称
荧光棒
+
+ 同一路径下只能启动一个实例!
+
diff --git a/src/MaaWpfGui/Res/Localizations/zh-tw.xaml b/src/MaaWpfGui/Res/Localizations/zh-tw.xaml
index 21a938f65f..6304405bf1 100644
--- a/src/MaaWpfGui/Res/Localizations/zh-tw.xaml
+++ b/src/MaaWpfGui/Res/Localizations/zh-tw.xaml
@@ -910,4 +910,7 @@ C:\\leidian\\LDPlayer9。\n\n
支援道具名稱
螢光棒
+
+ 同一路徑下只能啟動一個實例!
+