diff --git a/src/MaaWpfGui/Main/AsstProxy.cs b/src/MaaWpfGui/Main/AsstProxy.cs
index c765f3f8a3..144cf0b110 100644
--- a/src/MaaWpfGui/Main/AsstProxy.cs
+++ b/src/MaaWpfGui/Main/AsstProxy.cs
@@ -15,6 +15,8 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
+using System.Net.NetworkInformation;
+using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
@@ -1134,6 +1136,50 @@ namespace MaaWpfGui.Main
private static readonly bool ForcedReloadResource = File.Exists("DEBUG") || File.Exists("DEBUG.txt");
+
+ ///
+ /// 检查端口是否有效。
+ ///
+ /// 连接地址。
+ /// 是否有效。
+ public bool IfPortEstablished(string address)
+ {
+ if (string.IsNullOrEmpty(address))
+ {
+ return false;
+ }
+
+ // normal -> [host]:[port]
+ // LDPlayer -> emulator-[port]
+ string[] address_ = address.Contains(":") ? address.Split(':') : address.Split('-');
+ string host = address_[0].Equals("emulator") ? "127.0.0.1" : address_[0];
+ int port = int.Parse(address_[1]);
+
+ using (var client = new TcpClient())
+ {
+ try
+ {
+ IAsyncResult result = client.BeginConnect(host, port, null, null);
+ bool success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(.5));
+
+ if (success)
+ {
+ client.EndConnect(result);
+ return true;
+ }
+ else
+ {
+ client.Close();
+ return false;
+ }
+ }
+ catch (Exception)
+ {
+ return false;
+ }
+ }
+ }
+
///
/// 连接模拟器。
///
@@ -1144,14 +1190,39 @@ namespace MaaWpfGui.Main
if (Instances.SettingsViewModel.AutoDetectConnection)
{
string bsHvAddress = Instances.SettingsViewModel.TryToSetBlueStacksHyperVAddress();
- if (bsHvAddress != null)
+ bool adbConfResult = Instances.SettingsViewModel.DetectAdbConfig(ref error);
+
+ if (String.Equals(Instances.SettingsViewModel.ConnectAddress, bsHvAddress))
{
- Instances.SettingsViewModel.ConnectAddress = bsHvAddress;
+ // 防止bsHvAddress和connectAddress重合
+ bsHvAddress = String.Empty;
}
- else if (!Instances.SettingsViewModel.DetectAdbConfig(ref error))
+
+ // tcp连接测试端口是否有效,超时时间500ms
+ bool adbResult = IfPortEstablished(Instances.SettingsViewModel.ConnectAddress);
+ bool bsResult = IfPortEstablished(bsHvAddress);
+
+ // 枚举所有情况
+ if (adbResult && bsResult)
{
+ // 2 connections(s)
+ error = LocalizationHelper.GetString("EmulatorTooMany");
return false;
}
+ else if (adbResult || bsResult)
+ {
+ // 1 connections(s)
+ Instances.SettingsViewModel.ConnectAddress = adbResult ? Instances.SettingsViewModel.ConnectAddress : bsHvAddress;
+ error = string.Empty;
+ }
+ else
+ {
+ // 0 connections(s)
+ if (!adbConfResult)
+ {
+ return false;
+ }
+ }
}
if (connected && connectedAdb == Instances.SettingsViewModel.AdbPath &&
diff --git a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs
index ccd520f34a..80a1c64c69 100644
--- a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs
+++ b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs
@@ -21,6 +21,7 @@ using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Management;
+using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
@@ -43,6 +44,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serilog;
using Stylet;
+using Windows.Devices.Geolocation;
using ComboBox = System.Windows.Controls.ComboBox;
using Timer = System.Timers.Timer;
@@ -2459,6 +2461,13 @@ namespace MaaWpfGui.ViewModels.UI
{ "WSA", new List { "127.0.0.1:58526" } },
};
+ ///
+ /// RegisterKey of Bluestacks_Nxt
+ ///
+ public static string BluestacksNxtRegistryKey = @"SOFTWARE\BlueStacks_nxt";
+ public static string BluestacksNxtValueName = "UserDefinedDir";
+
+
///
/// Refreshes ADB config.
///
@@ -2526,6 +2535,33 @@ namespace MaaWpfGui.ViewModels.UI
return true;
}
+ ///
+ /// Get the path of bluestacks.conf
+ ///
+ /// path
+ public static string GetBluestacksConfig()
+ {
+ var conf = ConfigurationHelper.GetValue(ConfigurationKeys.BluestacksConfigPath, string.Empty);
+ if (!string.IsNullOrEmpty(conf))
+ {
+ return conf;
+ }
+
+ using (RegistryKey key = Registry.LocalMachine.OpenSubKey(BluestacksNxtRegistryKey))
+ {
+ if (key != null)
+ {
+ object value = key.GetValue(BluestacksNxtValueName);
+ if (value != null)
+ {
+ return (string)value + "\\bluestacks.conf";
+ }
+ }
+ }
+
+ return null;
+ }
+
///
/// Selects ADB program file.
///
@@ -2605,7 +2641,7 @@ namespace MaaWpfGui.ViewModels.UI
rvm.WindowTitle = $"{prefix}MAA ({CurrentConfiguration}) - {VersionId}{poolString} - {connectConfigName} ({ConnectAddress}) - {ClientName}";
}
- private readonly string _bluestacksConfig = ConfigurationHelper.GetValue(ConfigurationKeys.BluestacksConfigPath, string.Empty);
+ private readonly string _bluestacksConfig = GetBluestacksConfig();
private string _bluestacksKeyWord = ConfigurationHelper.GetValue(ConfigurationKeys.BluestacksConfigKeyword, string.Empty);
///