chore: 支持实体机检测

This commit is contained in:
uye
2025-04-13 15:39:38 +08:00
parent 2d4e1f0121
commit bbbc19bf52
4 changed files with 73 additions and 68 deletions

View File

@@ -153,6 +153,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Todays/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=txwy/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ucrtbase/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=vmonitor/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=XYAZ/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Yahei/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=yituliu/@EntryIndexedValue">True</s:Boolean>

View File

@@ -27,38 +27,36 @@ namespace MaaWpfGui.Helper
{
private static readonly ILogger _logger = Log.ForContext<WinAdapter>();
private static readonly Dictionary<string, string> _emulatorIdDict = new Dictionary<string, string>
private static readonly Dictionary<string, string> _emulatorIdDict = new()
{
{ "HD-Player", "BlueStacks" },
{ "HD-Player", "BlueStacks" },
{ "dnplayer", "LDPlayer" },
{ "Nox", "Nox" },
{ "MuMuPlayer", "MuMuEmulator12" }, // MuMu 12
{ "MEmu", "XYAZ" },
};
private static readonly Dictionary<string, List<string>> _adbRelativePathDict = new Dictionary<string, List<string>>
private static readonly Dictionary<string, List<string>> _adbRelativePathDict = new()
{
{
"BlueStacks", new List<string>
{
"BlueStacks", [
@".\HD-Adb.exe",
@".\Engine\ProgramFiles\HD-Adb.exe",
}
@".\Engine\ProgramFiles\HD-Adb.exe"
]
},
{ "LDPlayer", new List<string> { @".\adb.exe" } },
{ "Nox", new List<string> { @".\nox_adb.exe" } },
{ "LDPlayer", [@".\adb.exe"] },
{ "Nox", [@".\nox_adb.exe"] },
{
"MuMuEmulator12", new List<string>
{
"MuMuEmulator12", [
@"..\vmonitor\bin\adb_server.exe",
@"..\..\MuMu\emulator\nemu\vmonitor\bin\adb_server.exe",
@".\adb.exe",
}
@".\adb.exe"
]
},
{ "XYAZ", new List<string> { @".\adb.exe" } },
{ "XYAZ", [@".\adb.exe"] },
};
private readonly Dictionary<string, string> _adbAbsolutePathDict = new Dictionary<string, string>();
private readonly Dictionary<string, string> _adbAbsolutePathDict = new();
/// <summary>
/// Refreshes emulator information.
@@ -70,12 +68,11 @@ namespace MaaWpfGui.Helper
var emulators = new List<string>();
foreach (var process in allProcess)
{
if (!_emulatorIdDict.ContainsKey(process.ProcessName))
if (!_emulatorIdDict.TryGetValue(process.ProcessName, out var emulatorId))
{
continue;
}
var emulatorId = _emulatorIdDict[process.ProcessName];
emulators.Add(emulatorId);
var processPath = process.MainModule?.FileName;
foreach (string adbPath in _adbRelativePathDict[emulatorId]
@@ -96,9 +93,7 @@ namespace MaaWpfGui.Helper
/// <returns>The ADB path of the emulator.</returns>
public string GetAdbPathByEmulatorName(string emulatorName)
{
return _adbAbsolutePathDict.Keys.Contains(emulatorName)
? _adbAbsolutePathDict[emulatorName]
: null;
return _adbAbsolutePathDict.GetValueOrDefault(emulatorName);
}
/// <summary>
@@ -106,29 +101,49 @@ namespace MaaWpfGui.Helper
/// </summary>
/// <param name="adbPath">The ADB path.</param>
/// <returns>The list of ADB addresses.</returns>
public List<string> GetAdbAddresses(string adbPath)
public static List<string> GetAdbAddresses(string adbPath)
{
string output = ExecuteAdbCommand(adbPath, "devices");
var lines = output.Split('\r', '\n');
return lines
.Where(line => !line.StartsWith("List of devices attached") &&
!string.IsNullOrWhiteSpace(line) &&
line.Contains("device"))
.Select(line => line.Split('\t')[0])
.ToList();
}
/// <summary>
/// Executes an ADB command.
/// </summary>
/// <param name="adbPath">adb path</param>
/// <param name="command">adb command</param>
/// <returns>output</returns>
public static string ExecuteAdbCommand(string adbPath, string command)
{
try
{
using Process process = new Process();
process.StartInfo.FileName = adbPath;
process.StartInfo.Arguments = "devices";
var process = new Process
{
StartInfo = new()
{
FileName = adbPath,
Arguments = command,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
},
};
// 禁用操作系统外壳程序
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
var output = process.StandardOutput.ReadToEnd();
_logger.Information(adbPath + " devices | output:\n" + output);
var outLines = output.Split('\r', '\n');
return (from line in outLines where !line.StartsWith("List of devices attached") && line.Length != 0 && line.Contains("device") select line.Split('\t')[0]).ToList();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
catch (Exception e)
{
_logger.Error(e.Message);
return new List<string>();
return string.Empty;
}
}
}

View File

@@ -1796,46 +1796,45 @@ namespace MaaWpfGui.Main
private static readonly bool _forcedReloadResource = File.Exists("DEBUG") || File.Exists("DEBUG.txt");
/// <summary>
/// 检查端口是否有效
/// 使用 TCP 或 adb devices 命令检查连接。TCP 检测相比 adb devices 更快,但不支持实体机
/// </summary>
/// <param name="address">连接地址。</param>
/// <returns>是否有效。</returns>
private static bool IfPortEstablished(string? address)
/// <param name="adbPath">adb path用于实体机检测</param>
/// <param name="address">连接地址</param>
/// <returns>设备是否在线</returns>
private static bool CheckConnection(string adbPath, string? address)
{
if (string.IsNullOrEmpty(address) || (!address.Contains(':') && !address.Contains('-')))
if (string.IsNullOrEmpty(address))
{
return false;
}
// 实体机可能设备名 -> [host]
if (!address.Contains(':') && !address.Contains('-'))
{
return WinAdapter.GetAdbAddresses(adbPath).Contains(address);
}
// normal -> [host]:[port]
// LdPlayer -> emulator-[port]
string[] hostAndPort = address.Split([':', '-']);
if (hostAndPort.Length != 2)
string[] hostAndPort = address.Split([':', '-'], StringSplitOptions.RemoveEmptyEntries);
if (hostAndPort.Length != 2 || !int.TryParse(hostAndPort[1], out var port))
{
return false;
}
string host;
if (!int.TryParse(hostAndPort[1], out var port))
{
return false;
}
if (hostAndPort[0].StartsWith("emulator"))
string host = hostAndPort[0];
if (host.StartsWith("emulator"))
{
host = "127.0.0.1";
port += 1;
}
else
{
host = hostAndPort[0];
}
using var client = new TcpClient();
try
{
IAsyncResult result = client.BeginConnect(host, port, null, null);
bool success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(.5));
bool success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(0.5));
if (success)
{
@@ -1846,7 +1845,7 @@ namespace MaaWpfGui.Main
client.Close();
return false;
}
catch (Exception)
catch
{
return false;
}
@@ -1880,7 +1879,7 @@ namespace MaaWpfGui.Main
if (Connected && _connectedAdb == SettingsViewModel.ConnectSettings.AdbPath &&
_connectedAddress == SettingsViewModel.ConnectSettings.ConnectAddress)
{
var actualConnectionStatus = IfPortEstablished(SettingsViewModel.ConnectSettings.ConnectAddress);
var actualConnectionStatus = CheckConnection(_connectedAdb, _connectedAddress);
if (!actualConnectionStatus)
{
Connected = false;
@@ -1951,21 +1950,11 @@ namespace MaaWpfGui.Main
private static bool AutoDetectConnection(ref string error)
{
// 本地设备如果选了自动检测,还是重新检测一下,不然重新插拔地址变了之后就再也不会检测了
/*
// tcp连接测试端口是否有效超时时间500ms
// 如果是本地设备,没有冒号
bool adbResult = !string.IsNullOrEmpty(Instances.SettingsViewModel.AdbPath) &&
((!Instances.SettingsViewModel.ConnectAddress.Contains(':') &&
!string.IsNullOrEmpty(Instances.SettingsViewModel.ConnectAddress)) ||
IfPortEstablished(Instances.SettingsViewModel.ConnectAddress));
*/
var adbPath = SettingsViewModel.ConnectSettings.AdbPath;
bool adbResult = !string.IsNullOrEmpty(adbPath) &&
File.Exists(adbPath) &&
Path.GetFileName(adbPath).Contains("adb", StringComparison.InvariantCultureIgnoreCase) &&
IfPortEstablished(SettingsViewModel.ConnectSettings.ConnectAddress);
CheckConnection(adbPath, SettingsViewModel.ConnectSettings.ConnectAddress);
if (adbResult)
{
@@ -1976,7 +1965,7 @@ namespace MaaWpfGui.Main
// 蓝叠的特殊处理
{
string bsHvAddress = SettingsViewModel.ConnectSettings.TryToSetBlueStacksHyperVAddress() ?? string.Empty;
bool bsResult = IfPortEstablished(bsHvAddress);
bool bsResult = CheckConnection(adbPath, bsHvAddress);
if (bsResult)
{
error = string.Empty;

View File

@@ -708,7 +708,7 @@ public class ConnectSettingsUserControlModel : PropertyChangedBase
return false;
}
var addresses = adapter.GetAdbAddresses(AdbPath);
var addresses = WinAdapter.GetAdbAddresses(AdbPath);
switch (addresses.Count)
{