feat: 注册表查询bluestacks hyper-v的conf位置 (#6083)

This commit is contained in:
MistEO
2023-08-31 22:00:03 +08:00
committed by GitHub
2 changed files with 111 additions and 4 deletions

View File

@@ -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");
/// <summary>
/// 检查端口是否有效。
/// </summary>
/// <param name="address">连接地址。</param>
/// <returns>是否有效。</returns>
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;
}
}
}
/// <summary>
/// 连接模拟器。
/// </summary>
@@ -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 &&

View File

@@ -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<string> { "127.0.0.1:58526" } },
};
/// <summary>
/// RegisterKey of Bluestacks_Nxt
/// </summary>
public static string BluestacksNxtRegistryKey = @"SOFTWARE\BlueStacks_nxt";
public static string BluestacksNxtValueName = "UserDefinedDir";
/// <summary>
/// Refreshes ADB config.
/// </summary>
@@ -2526,6 +2535,33 @@ namespace MaaWpfGui.ViewModels.UI
return true;
}
/// <summary>
/// Get the path of bluestacks.conf
/// </summary>
/// <returns>path</returns>
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;
}
/// <summary>
/// Selects ADB program file.
/// </summary>
@@ -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);
/// <summary>