Implement ADB hard restart. (#5666)

This commit is contained in:
uye
2023-07-30 19:32:44 +08:00
committed by GitHub
10 changed files with 115 additions and 12 deletions

View File

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

View File

@@ -121,6 +121,9 @@
<PackageReference Include="Vanara.PInvoke.ComCtl32" Version="3.4.14" />
<PackageReference Include="Vanara.PInvoke.Shell32" Version="3.4.14" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Management" />
</ItemGroup>
<ItemGroup>
<Resource Include="newlogo.ico" />
</ItemGroup>

View File

@@ -251,7 +251,8 @@ At the end of the level, enter 「Normal/Hard」 to switch between Standard and
<system:String x:Key="ConnectionPreset">Connection Preset</system:String>
<system:String x:Key="RetryOnDisconnected">Auto restart emulator when ADB reconnection fails</system:String>
<system:String x:Key="RetryOnDisconnectedTip">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</system:String>
<system:String x:Key="AllowADBRestart">Attempt to restart ADB after connection failure</system:String>
<system:String x:Key="AllowADBRestart">Attempt to restart ADB Server after connection failure</system:String>
<system:String x:Key="AllowADBHardRestart">Attempt to kill and restart ADB process after connection failure</system:String>
<!-- Settings -->
<!-- Farming -->
<system:String x:Key="Farming">Farming</system:String>
@@ -537,7 +538,8 @@ The video aspect ratio needs to be 16:9 without interference factors such as bla
<system:String x:Key="RecruitTagsDetected" xml:space="preserve">Identification results: </system:String>
<system:String x:Key="ConnectFailed">Connection Failed</system:String>
<system:String x:Key="TryToStartEmulator">Trying to start the emulator</system:String>
<system:String x:Key="RestartADB">Trying to restart the ADB</system:String>
<system:String x:Key="RestartADB">Trying to restart the ADB Server</system:String>
<system:String x:Key="HardRestartADB">Trying to kill and restart the ADB process</system:String>
<system:String x:Key="CheckSettings">Please check your connection settings or try restarting the emulator and ADB, or restart your computer</system:String>
<!-- AsstProxy -->
<!-- TrayIcon -->

View File

@@ -250,7 +250,8 @@
<system:String x:Key="ConnectionPreset">接続構成</system:String>
<system:String x:Key="RetryOnDisconnected">ADB再接続失敗時にエミュレータを自動で再起動</system:String>
<system:String x:Key="RetryOnDisconnectedTip">再接続に20回失敗した後、エミュレータを再起動してみてくださいバックグラウンドのタイムドタスクを使用し、終了時にエミュレータを終了させるタスクを設定する場合は、必ずチェックを入れること</system:String>
<system:String x:Key="AllowADBRestart">接続失敗後にADBの再起動を試みる</system:String>
<system:String x:Key="AllowADBRestart">接続失敗後にADBサーバーの再起動を試みる</system:String>
<system:String x:Key="AllowADBHardRestart">接続失敗後にADBプロセスの再起動を試みる</system:String>
<!-- 設定 -->
<!-- スタート -->
<system:String x:Key="Farming">スタート</system:String>
@@ -535,7 +536,8 @@
<system:String x:Key="RecruitTagsDetected" xml:space="preserve">認識結果: </system:String>
<system:String x:Key="ConnectFailed">接続失敗</system:String>
<system:String x:Key="TryToStartEmulator">エミュレータを起動しようとしています</system:String>
<system:String x:Key="RestartADB">ADBの再起動を試みています</system:String>
<system:String x:Key="RestartADB">ADBサーバーの再起動を試みています</system:String>
<system:String x:Key="HardRestartADB">ADBプロセスの再起動を試みています</system:String>
<system:String x:Key="CheckSettings">接続設定を確認するか、エミュレータとADBを再起動するか、コンピューターを再起動してください</system:String>
<!-- AsstProxy -->
<!-- TrayIcon -->

View File

@@ -250,7 +250,8 @@ OF-1을 해금하지 않았다면 선택하지 말아 주세요.</system:String>
<system:String x:Key="ConnectionPreset">연결 프리셋</system:String>
<system:String x:Key="RetryOnDisconnected">ADB 재연결이 실패하면 자동으로 에뮬레이터를 다시 시작합니다</system:String>
<system:String x:Key="RetryOnDisconnectedTip">재연결을 20회 시도해도 실패하면 에뮬레이터를 다시 시작합니다. 백그라운드 예약을 사용하고 작업이 완료되면 에뮬레이터가 종료되도록 설정하려면 반드시 선택해야 합니다</system:String>
<system:String x:Key="AllowADBRestart">연결 실패 후 ADB 재시작 시도</system:String>
<system:String x:Key="AllowADBRestart">연결 실패 후 ADB 서버 재시작 시도</system:String>
<system:String x:Key="AllowADBHardRestart">연결 실패 후 ADB 프로세스 재시작 시도</system:String>
<!-- 설정 -->
<!-- 자동화 -->
<system:String x:Key="Farming">시작</system:String>
@@ -536,7 +537,8 @@ OF-1을 해금하지 않았다면 선택하지 말아 주세요.</system:String>
<system:String x:Key="RecruitTagsDetected" xml:space="preserve">인식 결과: </system:String>
<system:String x:Key="ConnectFailed">연결 실패</system:String>
<system:String x:Key="TryToStartEmulator">에뮬레이터를 시작하는 중입니다</system:String>
<system:String x:Key="RestartADB">ADB 재시작을 시도 중입니다</system:String>
<system:String x:Key="RestartADB">ADB 서버 재시작을 시도 중입니다</system:String>
<system:String x:Key="HardRestartADB">ADB 프로세스 재시작을 시도 중입니다</system:String>
<system:String x:Key="CheckSettings">연결 설정을 확인하거나 에뮬레이터와 ADB를 다시 시작하거나 컴퓨터를 재시작해주세요</system:String>
<!-- Current step: {0} {1} -->
<!-- AsstProxy -->

View File

@@ -255,7 +255,8 @@
<system:String x:Key="ConnectionPreset">连接配置</system:String>
<system:String x:Key="RetryOnDisconnected">ADB 重连失败时自动重启模拟器</system:String>
<system:String x:Key="RetryOnDisconnectedTip">重连失败 20 次后尝试重启模拟器,使用后台定时任务且设置任务完成关闭模拟器时请勾选</system:String>
<system:String x:Key="AllowADBRestart">连接失败后尝试重启 ADB</system:String>
<system:String x:Key="AllowADBRestart">连接失败后尝试重启 ADB Server</system:String>
<system:String x:Key="AllowADBHardRestart">连接失败后尝试关闭并重启 ADB 进程</system:String>
<!-- 设置 -->
<!-- 一键长草 -->
<system:String x:Key="Farming">一键长草</system:String>
@@ -541,7 +542,8 @@
<system:String x:Key="RecruitTagsDetected" xml:space="preserve">识别结果: </system:String>
<system:String x:Key="ConnectFailed">连接失败</system:String>
<system:String x:Key="TryToStartEmulator">正在尝试启动模拟器</system:String>
<system:String x:Key="RestartADB">正在尝试重启 ADB</system:String>
<system:String x:Key="RestartADB">正在尝试重启 ADB Server</system:String>
<system:String x:Key="HardRestartADB">正在尝试关闭并重启 ADB 进程</system:String>
<system:String x:Key="CheckSettings">请检查连接设置或尝试重启模拟器与 ADB 或重启电脑</system:String>
<!-- AsstProxy -->
<!-- TrayIcon -->

View File

@@ -250,7 +250,8 @@
<system:String x:Key="ConnectionPreset">連接配置</system:String>
<system:String x:Key="RetryOnDisconnected">ADB 重連失敗時自動重啟模擬器</system:String>
<system:String x:Key="RetryOnDisconnectedTip">重連失敗 20 次後嘗試重啟模擬器,使用後台定時任務且設定任務完成關閉模擬器時必須勾選</system:String>
<system:String x:Key="AllowADBRestart">連接失敗後嘗試重啟 ADB</system:String>
<system:String x:Key="AllowADBRestart">連接失敗後嘗試重啟 ADB 伺服器</system:String>
<system:String x:Key="AllowADBHardRestart">連接失敗後嘗試重啟 ADB 行程</system:String>
<!-- 設定 -->
<!-- 一鍵長草 -->
<system:String x:Key="Farming">一鍵長草</system:String>
@@ -536,7 +537,8 @@
<system:String x:Key="RecruitTagsDetected" xml:space="preserve">辨識結果: </system:String>
<system:String x:Key="ConnectFailed">連接失敗</system:String>
<system:String x:Key="TryToStartEmulator">正在嘗試啟動模擬器</system:String>
<system:String x:Key="RestartADB">正在嘗試重啟 ADB</system:String>
<system:String x:Key="RestartADB">正在嘗試重啟 ADB 伺服器</system:String>
<system:String x:Key="HardRestartADB">正在嘗試重啟 ADB 行程</system:String>
<system:String x:Key="CheckSettings">請檢查連接設定或嘗試重啟模擬器與 ADB 或重啟電腦</system:String>
<!-- About -->
<!-- AutoRecruitSettings -->

View File

@@ -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;
@@ -775,6 +776,57 @@ namespace MaaWpfGui.ViewModels.UI
process.WaitForExit();
}
/// <summary>
/// Kill and restart the ADB (Android Debug Bridge) process.
/// </summary>
public void HardRestartADB()
{
if (!AllowADBHardRestart)
{
return;
}
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<ManagementObject>()
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)
{
// Some emulators start their adb with administrator privilege.
// Not sure if this is necessary
try
{
item.Process.Kill();
item.Process.WaitForExit();
}
catch
{
}
}
}
}
}
/// <summary>
/// Selects the emulator to execute.
/// </summary>
@@ -2318,6 +2370,21 @@ namespace MaaWpfGui.ViewModels.UI
}
}
private bool _allowADBHardRestart = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.AllowADBHardRestart, bool.TrueString));
/// <summary>
/// Gets or sets a value indicating whether to allow for killing adb process.
/// </summary>
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

View File

@@ -758,9 +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)
@@ -773,9 +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)
@@ -787,6 +789,22 @@ namespace MaaWpfGui.ViewModels.UI
connected = await Task.Run(() => Instances.AsstProxy.AsstConnect(ref errMsg));
}
// 尝试杀掉adb进程
if (!connected && Instances.SettingsViewModel.AllowADBHardRestart)
{
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);

View File

@@ -155,6 +155,10 @@
Margin="20,10"
Content="{DynamicResource AllowADBRestart}"
IsChecked="{Binding AllowADBRestart}" />
<CheckBox
Margin="20,10"
Content="{DynamicResource AllowADBHardRestart}"
IsChecked="{Binding AllowADBHardRestart}" />
<CheckBox
Margin="20,10"
Content="{DynamicResource AutoRestartOption}"