feat.新增C#侧自动检测adb端口

This commit is contained in:
MistEO
2022-05-31 22:09:43 +08:00
parent 06044c9f01
commit 5ca4f736b4
3 changed files with 64 additions and 25 deletions

View File

@@ -609,6 +609,7 @@ namespace MeoAsstGui
}
}
settings.TryToSetBlueStacksHyperVAddress();
bool ret = AsstConnect(_handle, settings.AdbPath, settings.ConnectAddress, settings.ConnectConfig);
if (!ret)
{

View File

@@ -77,5 +77,34 @@ namespace MeoAsstGui
}
return null;
}
public List<string> GetAdbAddresses(string adbPath)
{
var addresses = new List<string>();
using (Process process = new System.Diagnostics.Process())
{
process.StartInfo.FileName = adbPath;
process.StartInfo.Arguments = "devices";
// 禁用操作系统外壳程序
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
var output = process.StandardOutput.ReadToEnd();
var outLines = output.Split(new[] { '\r', '\n' });
foreach (var line in outLines)
{
if (line.StartsWith("List of devices attached") ||
line.Length == 0 ||
!line.Contains("device"))
{
continue;
}
var address = line.Split('\t')[0];
addresses.Add(address);
}
}
return addresses;
}
}
}

View File

@@ -964,35 +964,20 @@ namespace MeoAsstGui
{
SetAndNotify(ref _connectConfig, value);
ViewStatusStorage.Set("Connect.ConnectConfig", value);
if (ConnectAddress.Length == 0)
{
UpdateAddressByConfig();
}
}
}
private readonly Dictionary<string, List<string>> ConfigAddressesMapping = new Dictionary<string, List<string>>
private readonly Dictionary<string, string> DefaultAddress = new Dictionary<string, string>
{
{ "General", new List<string> {""} },
{ "BlueStacks", new List<string> {"127.0.0.1:5555", "127.0.0.1:5556", "127.0.0.1:5557" } },
{ "MuMuEmulator", new List<string>{"127.0.0.1:7555"} },
{ "LDPlayer", new List<string>{ "127.0.0.1:5555", "emulator-5554" } },
{ "Nox", new List<string> { "127.0.0.1:62001", "127.0.0.1:59865" } },
{ "XYAZ", new List<string> {"127.0.0.1:21503" } },
{ "WSA", new List<string> { "127.0.0.1:58526" } },
{ "General", "" },
{ "BlueStacks", "127.0.0.1:5555" },
{ "MuMuEmulator", "127.0.0.1:7555" },
{ "LDPlayer", "emulator-5554" },
{ "Nox", "127.0.0.1:62001" },
{ "XYAZ", "127.0.0.1:21503" },
{ "WSA","127.0.0.1:58526" },
};
private void UpdateAddressByConfig()
{
var addresses = ConfigAddressesMapping[ConnectConfig];
ConnectAddress = addresses.FirstOrDefault();
//ConnectAddressList.Clear();
//foreach (var address in addresses)
//{
// ConnectAddressList.Add(address);
//}
}
public bool RefreshAdbConfig(ref string error)
{
var adapter = new WinAdapter();
@@ -1018,8 +1003,32 @@ namespace MeoAsstGui
}
ConnectConfig = emulators.First();
AdbPath = adapter.GetAdbPathByEmulatorName(ConnectConfig) ?? AdbPath;
UpdateAddressByConfig();
TryToSetBlueStacksHyperVAddress();
if (ConnectAddress.Length == 0)
{
var addresses = adapter.GetAdbAddresses(AdbPath);
// 傻逼雷电已经关掉了用别的 adb 还能检测出来这个端口 device
if (addresses.Count == 1 && addresses.First() != "emulator-5554")
{
ConnectAddress = addresses.First();
}
else if (addresses.Count > 1)
{
foreach (var address in addresses)
{
if (address == "emulator-5554" && ConnectConfig != "LDPlayer")
{
continue;
}
ConnectAddress = address;
break;
}
}
if (ConnectAddress.Length == 0)
{
ConnectAddress = DefaultAddress[ConnectConfig];
}
}
return true;
}