From a56c1562997043fd6817891705b05bf4f912b690 Mon Sep 17 00:00:00 2001 From: AsterNighT Date: Thu, 27 Jul 2023 16:47:43 +0800 Subject: [PATCH 1/5] Feat: Implement ADB hard restart. --- src/MaaWpfGui/Constants/ConfigurationKeys.cs | 1 + src/MaaWpfGui/MaaWpfGui.csproj | 3 + src/MaaWpfGui/Res/Localizations/en-us.xaml | 6 +- src/MaaWpfGui/Res/Localizations/zh-cn.xaml | 6 +- .../ViewModels/UI/SettingsViewModel.cs | 65 +++++++++++++++++++ .../ViewModels/UI/TaskQueueViewModel.cs | 15 +++++ .../ConnectSettingsUserControl.xaml | 4 ++ 7 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/MaaWpfGui/Constants/ConfigurationKeys.cs b/src/MaaWpfGui/Constants/ConfigurationKeys.cs index 11e778da15..1ac2e1f792 100644 --- a/src/MaaWpfGui/Constants/ConfigurationKeys.cs +++ b/src/MaaWpfGui/Constants/ConfigurationKeys.cs @@ -57,6 +57,7 @@ namespace MaaWpfGui.Constants public const string ConnectConfig = "Connect.ConnectConfig"; public const string RetryOnAdbDisconnected = "Connect.RetryOnDisconnected"; public const string AllowADBRestart = "Connect.AllowADBRestart"; + public const string AllowADBHardRestart = "Connect.AllowADBHardRestart"; public const string AdbLiteEnabled = "Connect.AdbLiteEnabled"; public const string KillAdbOnExit = "Connect.KillAdbOnExit"; public const string TouchMode = "Connect.TouchMode"; diff --git a/src/MaaWpfGui/MaaWpfGui.csproj b/src/MaaWpfGui/MaaWpfGui.csproj index c430e53607..aa49eed1ab 100644 --- a/src/MaaWpfGui/MaaWpfGui.csproj +++ b/src/MaaWpfGui/MaaWpfGui.csproj @@ -121,6 +121,9 @@ + + + diff --git a/src/MaaWpfGui/Res/Localizations/en-us.xaml b/src/MaaWpfGui/Res/Localizations/en-us.xaml index 8958f34e36..4300a208cb 100644 --- a/src/MaaWpfGui/Res/Localizations/en-us.xaml +++ b/src/MaaWpfGui/Res/Localizations/en-us.xaml @@ -251,7 +251,8 @@ At the end of the level, enter 「Normal/Hard」 to switch between Standard and Connection Preset Auto restart emulator when ADB reconnection fails Try restarting the emulator after 20 failed reconnect attempts, must be checked when using a background timed task and setting the task to close the emulator when finished - Attempt to restart ADB after connection failure + Attempt to restart ADB Server after connection failure + Attempt to kill and restart ADB process after connection failure Farming @@ -536,7 +537,8 @@ The video aspect ratio needs to be 16:9 without interference factors such as bla Identification results: Connection Failed Trying to start the emulator - Trying to restart the ADB + Trying to restart the ADB Server + Trying to kill and restart the ADB process Please check your connection settings or try restarting the emulator and ADB, or restart your computer diff --git a/src/MaaWpfGui/Res/Localizations/zh-cn.xaml b/src/MaaWpfGui/Res/Localizations/zh-cn.xaml index 9ed1f710cd..69bc7a8c0c 100644 --- a/src/MaaWpfGui/Res/Localizations/zh-cn.xaml +++ b/src/MaaWpfGui/Res/Localizations/zh-cn.xaml @@ -255,7 +255,8 @@ 连接配置 ADB 重连失败时自动重启模拟器 重连失败 20 次后尝试重启模拟器,使用后台定时任务且设置任务完成关闭模拟器时请勾选 - 连接失败后尝试重启 ADB + 连接失败后尝试重启 ADB Server + 连接失败后尝试关闭并重启 ADB 进程 一键长草 @@ -540,7 +541,8 @@ 识别结果: 连接失败 正在尝试启动模拟器 - 正在尝试重启 ADB + 正在尝试重启 ADB Server + 正在尝试关闭并重启 ADB 进程 请检查连接设置或尝试重启模拟器与 ADB 或重启电脑 diff --git a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs index 932d2d0e14..ea87a93264 100644 --- a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs @@ -20,6 +20,7 @@ using System.Globalization; using System.IO; using System.IO.Compression; using System.Linq; +using System.Management; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; @@ -573,6 +574,8 @@ namespace MaaWpfGui.ViewModels.UI return; } + Instances.TaskQueueViewModel.AddLog(LocalizationHelper.GetString("ConnectFailed") + "\n" + LocalizationHelper.GetString("TryToStartEmulator")); + if (!int.TryParse(EmulatorWaitSeconds, out int delay)) { delay = 60; @@ -747,6 +750,8 @@ namespace MaaWpfGui.ViewModels.UI return; } + Instances.TaskQueueViewModel.AddLog(LocalizationHelper.GetString("ConnectFailed") + "\n" + LocalizationHelper.GetString("RestartADB")); + string adbPath = AdbPath; if (string.IsNullOrEmpty(adbPath)) @@ -775,6 +780,51 @@ namespace MaaWpfGui.ViewModels.UI process.WaitForExit(); } + /// + /// Kill and restart the ADB (Android Debug Bridge) process. + /// + public void HardRestartADB() + { + if (!AllowADBHardRestart) + { + return; + } + + Instances.TaskQueueViewModel.AddLog(LocalizationHelper.GetString("ConnectFailed") + "\n" + LocalizationHelper.GetString("HardRestartADB")); + + string adbPath = AdbPath; + + if (string.IsNullOrEmpty(adbPath)) + { + return; + } + + // This allows for SQL injection, but since it is not on a real database nothing horrible would happen. + // The following query string does what I want, but WMI does not accept it. + // var wmiQueryString = string.Format("SELECT ProcessId, CommandLine FROM Win32_Process WHERE ExecutablePath='{0}'", adbPath); + var wmiQueryString = "SELECT ProcessId, ExecutablePath, CommandLine FROM Win32_Process"; + using (var searcher = new ManagementObjectSearcher(wmiQueryString)) + using (var results = searcher.Get()) + { + var query = from p in Process.GetProcesses() + join mo in results.Cast() + on p.Id equals (int)(uint)mo["ProcessId"] + select new + { + Process = p, + Path = (string)mo["ExecutablePath"], + }; + foreach (var item in query) + { + if (item.Path == adbPath) + { + item.Process.Kill(); + item.Process.WaitForExit(); + } + } + } + } + /// /// Selects the emulator to execute. /// @@ -2318,6 +2368,21 @@ namespace MaaWpfGui.ViewModels.UI } } + private bool _allowADBHardRestart = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.AllowADBHardRestart, bool.TrueString)); + + /// + /// Gets or sets a value indicating whether to allow for killing adb process. + /// + public bool AllowADBHardRestart + { + get => _allowADBHardRestart; + set + { + SetAndNotify(ref _allowADBHardRestart, value); + ConfigurationHelper.SetValue(ConfigurationKeys.AllowADBHardRestart, value.ToString()); + } + } + private bool _deploymentWithPause = bool.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.RoguelikeDeploymentWithPause, false.ToString())); public bool DeploymentWithPause diff --git a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs index 5370bf719b..6182242423 100644 --- a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs @@ -787,6 +787,21 @@ namespace MaaWpfGui.ViewModels.UI connected = await Task.Run(() => Instances.AsstProxy.AsstConnect(ref errMsg)); } + // 尝试杀掉adb进程 + if (!connected) + { + AddLog(LocalizationHelper.GetString("ConnectFailed") + "\n" + LocalizationHelper.GetString("HardRestartADB")); + await Task.Run(() => Instances.SettingsViewModel.HardRestartADB()); + + if (Stopping) + { + SetStopped(); + return; + } + + connected = await Task.Run(() => Instances.AsstProxy.AsstConnect(ref errMsg)); + } + if (!connected) { AddLog(errMsg, UiLogColor.Error); diff --git a/src/MaaWpfGui/Views/UserControl/ConnectSettingsUserControl.xaml b/src/MaaWpfGui/Views/UserControl/ConnectSettingsUserControl.xaml index 219dc7b457..a57ca288e4 100644 --- a/src/MaaWpfGui/Views/UserControl/ConnectSettingsUserControl.xaml +++ b/src/MaaWpfGui/Views/UserControl/ConnectSettingsUserControl.xaml @@ -155,6 +155,10 @@ Margin="20,10" Content="{DynamicResource AllowADBRestart}" IsChecked="{Binding AllowADBRestart}" /> + Date: Thu, 27 Jul 2023 17:11:48 +0800 Subject: [PATCH 2/5] Fix: Remove redundant logs in TaskQueueViewModel.cs --- src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs index 6182242423..d709eb20bd 100644 --- a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs @@ -760,7 +760,6 @@ namespace MaaWpfGui.ViewModels.UI // 尝试启动模拟器 if (!connected) { - AddLog(LocalizationHelper.GetString("ConnectFailed") + "\n" + LocalizationHelper.GetString("TryToStartEmulator")); await Task.Run(() => Instances.SettingsViewModel.TryToStartEmulator(true)); if (Stopping) @@ -775,7 +774,6 @@ namespace MaaWpfGui.ViewModels.UI // 尝试重启adb if (!connected) { - AddLog(LocalizationHelper.GetString("ConnectFailed") + "\n" + LocalizationHelper.GetString("RestartADB")); await Task.Run(() => Instances.SettingsViewModel.RestartADB()); if (Stopping) @@ -790,7 +788,6 @@ namespace MaaWpfGui.ViewModels.UI // 尝试杀掉adb进程 if (!connected) { - AddLog(LocalizationHelper.GetString("ConnectFailed") + "\n" + LocalizationHelper.GetString("HardRestartADB")); await Task.Run(() => Instances.SettingsViewModel.HardRestartADB()); if (Stopping) From 8961a42945e0258000b3db99960c95b47ed6b801 Mon Sep 17 00:00:00 2001 From: AsterNighT Date: Thu, 27 Jul 2023 20:03:20 +0800 Subject: [PATCH 3/5] Fix: Move logs back to TaskQueueViewModel.cs --- src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs | 6 ------ src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs | 12 +++++++++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs index ea87a93264..961bb35434 100644 --- a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs @@ -574,8 +574,6 @@ namespace MaaWpfGui.ViewModels.UI return; } - Instances.TaskQueueViewModel.AddLog(LocalizationHelper.GetString("ConnectFailed") + "\n" + LocalizationHelper.GetString("TryToStartEmulator")); - if (!int.TryParse(EmulatorWaitSeconds, out int delay)) { delay = 60; @@ -750,8 +748,6 @@ namespace MaaWpfGui.ViewModels.UI return; } - Instances.TaskQueueViewModel.AddLog(LocalizationHelper.GetString("ConnectFailed") + "\n" + LocalizationHelper.GetString("RestartADB")); - string adbPath = AdbPath; if (string.IsNullOrEmpty(adbPath)) @@ -790,8 +786,6 @@ namespace MaaWpfGui.ViewModels.UI return; } - Instances.TaskQueueViewModel.AddLog(LocalizationHelper.GetString("ConnectFailed") + "\n" + LocalizationHelper.GetString("HardRestartADB")); - string adbPath = AdbPath; if (string.IsNullOrEmpty(adbPath)) diff --git a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs index d709eb20bd..8c070b226a 100644 --- a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs @@ -758,8 +758,10 @@ namespace MaaWpfGui.ViewModels.UI } // 尝试启动模拟器 - if (!connected) + if (!connected && Instances.SettingsViewModel.StartEmulator) { + AddLog(LocalizationHelper.GetString("ConnectFailed") + "\n" + LocalizationHelper.GetString("TryToStartEmulator")); + await Task.Run(() => Instances.SettingsViewModel.TryToStartEmulator(true)); if (Stopping) @@ -772,8 +774,10 @@ namespace MaaWpfGui.ViewModels.UI } // 尝试重启adb - if (!connected) + if (!connected && Instances.SettingsViewModel.AllowADBRestart) { + AddLog(LocalizationHelper.GetString("ConnectFailed") + "\n" + LocalizationHelper.GetString("RestartADB")); + await Task.Run(() => Instances.SettingsViewModel.RestartADB()); if (Stopping) @@ -786,8 +790,10 @@ namespace MaaWpfGui.ViewModels.UI } // 尝试杀掉adb进程 - if (!connected) + if (!connected && Instances.SettingsViewModel.AllowADBHardRestart) { + AddLog(LocalizationHelper.GetString("ConnectFailed") + "\n" + LocalizationHelper.GetString("HardRestartADB")); + await Task.Run(() => Instances.SettingsViewModel.HardRestartADB()); if (Stopping) From 9c66e103adda361696150eef070d8399fa2b56bf Mon Sep 17 00:00:00 2001 From: AsterNighT Date: Sat, 29 Jul 2023 12:57:02 +0800 Subject: [PATCH 4/5] Fix: Localizations for other languages and fix a bug that crashes maa in jp --- src/MaaWpfGui/Res/Localizations/en-us.xaml | 2 +- src/MaaWpfGui/Res/Localizations/ja-jp.xaml | 6 ++++-- src/MaaWpfGui/Res/Localizations/ko-kr.xaml | 6 ++++-- src/MaaWpfGui/Res/Localizations/zh-tw.xaml | 6 ++++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/MaaWpfGui/Res/Localizations/en-us.xaml b/src/MaaWpfGui/Res/Localizations/en-us.xaml index 4300a208cb..71d0f2e7fd 100644 --- a/src/MaaWpfGui/Res/Localizations/en-us.xaml +++ b/src/MaaWpfGui/Res/Localizations/en-us.xaml @@ -205,7 +205,7 @@ I.S. Theme Phantom Mizuki - + Sami Strategy Stops after starting N times Stops after investing N times diff --git a/src/MaaWpfGui/Res/Localizations/ja-jp.xaml b/src/MaaWpfGui/Res/Localizations/ja-jp.xaml index 623b939222..ad0918bc9e 100644 --- a/src/MaaWpfGui/Res/Localizations/ja-jp.xaml +++ b/src/MaaWpfGui/Res/Localizations/ja-jp.xaml @@ -250,7 +250,8 @@ 接続構成 ADB再接続失敗時にエミュレータを自動で再起動 再接続に20回失敗した後、エミュレータを再起動してみてください,バックグラウンドのタイムドタスクを使用し、終了時にエミュレータを終了させるタスクを設定する場合は、必ずチェックを入れること - 接続失敗後にADBの再起動を試みる + 接続失敗後にADBサーバーの再起動を試みる + 接続失敗後にADBプロセスの再起動を試みる スタート @@ -534,7 +535,8 @@ 認識結果: 接続失敗 エミュレータを起動しようとしています - ADBの再起動を試みています + ADBサーバーの再起動を試みています + ADBプロセスの再起動を試みています 接続設定を確認するか、エミュレータとADBを再起動するか、コンピューターを再起動してください diff --git a/src/MaaWpfGui/Res/Localizations/ko-kr.xaml b/src/MaaWpfGui/Res/Localizations/ko-kr.xaml index ccd8c4b8e7..f221223e2f 100644 --- a/src/MaaWpfGui/Res/Localizations/ko-kr.xaml +++ b/src/MaaWpfGui/Res/Localizations/ko-kr.xaml @@ -250,7 +250,8 @@ OF-1을 해금하지 않았다면 선택하지 말아 주세요. 연결 프리셋 ADB 재연결이 실패하면 자동으로 에뮬레이터를 다시 시작합니다 재연결을 20회 시도해도 실패하면 에뮬레이터를 다시 시작합니다. 백그라운드 예약을 사용하고 작업이 완료되면 에뮬레이터가 종료되도록 설정하려면 반드시 선택해야 합니다 - 연결 실패 후 ADB 재시작 시도 + 연결 실패 후 ADB 서버 재시작 시도 + 연결 실패 후 ADB 프로세스 재시작 시도 시작 @@ -535,7 +536,8 @@ OF-1을 해금하지 않았다면 선택하지 말아 주세요. 인식 결과: 연결 실패 에뮬레이터를 시작하는 중입니다 - ADB 재시작을 시도 중입니다 + ADB 서버 재시작을 시도 중입니다 + ADB 프로세스 재시작을 시도 중입니다 연결 설정을 확인하거나 에뮬레이터와 ADB를 다시 시작하거나 컴퓨터를 재시작해주세요 diff --git a/src/MaaWpfGui/Res/Localizations/zh-tw.xaml b/src/MaaWpfGui/Res/Localizations/zh-tw.xaml index c8c0b29501..121669b2f9 100644 --- a/src/MaaWpfGui/Res/Localizations/zh-tw.xaml +++ b/src/MaaWpfGui/Res/Localizations/zh-tw.xaml @@ -250,7 +250,8 @@ 連接配置 ADB 重連失敗時自動重啟模擬器 重連失敗 20 次後嘗試重啟模擬器,使用後台定時任務且設定任務完成關閉模擬器時必須勾選 - 連接失敗後嘗試重啟 ADB + 連接失敗後嘗試重啟 ADB 伺服器 + 連接失敗後嘗試重啟 ADB 行程 一鍵長草 @@ -535,7 +536,8 @@ 辨識結果: 連接失敗 正在嘗試啟動模擬器 - 正在嘗試重啟 ADB + 正在嘗試重啟 ADB 伺服器 + 正在嘗試重啟 ADB 行程 請檢查連接設定或嘗試重啟模擬器與 ADB 或重啟電腦 From 5d8eb72e74c41f164bd66259e0c522cf2d505e60 Mon Sep 17 00:00:00 2001 From: AsterNighT Date: Sun, 30 Jul 2023 19:04:35 +0800 Subject: [PATCH 5/5] Fix: Wrap Process.Kill in try/catch --- src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs index 961bb35434..123b28c013 100644 --- a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs @@ -812,8 +812,16 @@ namespace MaaWpfGui.ViewModels.UI { if (item.Path == adbPath) { - item.Process.Kill(); - item.Process.WaitForExit(); + // Some emulators start their adb with administrator privilege. + // Not sure if this is necessary + try + { + item.Process.Kill(); + item.Process.WaitForExit(); + } + catch + { + } } } }