From 6df743041fcc66e11c3c8777f1b1a6e7c1245f58 Mon Sep 17 00:00:00 2001 From: uye <99072975+ABA2396@users.noreply.github.com> Date: Wed, 9 Jul 2025 16:26:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E9=9B=B7=E7=94=B5?= =?UTF-8?q?=E4=B8=93=E7=89=88=E6=B3=A8=E5=86=8C=E8=A1=A8=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=EF=BC=8C=E6=96=B0=E5=A2=9E=E9=9B=B7=E7=94=B5?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E8=87=AA=E5=8A=A8=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MAA.sln.DotSettings | 1 + src/MaaCore/Controller/AdbController.cpp | 49 +++++++++++++- src/MaaCore/Controller/AdbController.h | 3 +- src/MaaWpfGui/Constants/ConfigurationKeys.cs | 1 + src/MaaWpfGui/Res/Localizations/en-us.xaml | 3 +- src/MaaWpfGui/Res/Localizations/ja-jp.xaml | 1 + src/MaaWpfGui/Res/Localizations/ko-kr.xaml | 1 + src/MaaWpfGui/Res/Localizations/zh-cn.xaml | 1 + src/MaaWpfGui/Res/Localizations/zh-tw.xaml | 1 + .../ConnectSettingsUserControlModel.cs | 67 ++++++++++++++----- .../Settings/ConnectSettingsUserControl.xaml | 44 +++++++----- 11 files changed, 131 insertions(+), 41 deletions(-) diff --git a/MAA.sln.DotSettings b/MAA.sln.DotSettings index dd72fd54c2..b976e086f5 100644 --- a/MAA.sln.DotSettings +++ b/MAA.sln.DotSettings @@ -105,6 +105,7 @@ True True True + True True True True diff --git a/src/MaaCore/Controller/AdbController.cpp b/src/MaaCore/Controller/AdbController.cpp index a988cc769c..4f9042a305 100644 --- a/src/MaaCore/Controller/AdbController.cpp +++ b/src/MaaCore/Controller/AdbController.cpp @@ -252,7 +252,7 @@ void asst::AdbController::set_mumu_package(const std::string& client_type) #endif } -void asst::AdbController::init_ld_extras([[maybe_unused]] const AdbCfg& adb_cfg) +void asst::AdbController::init_ld_extras([[maybe_unused]] const AdbCfg& adb_cfg, const std::string& address) { #if !ASST_WITH_EMULATOR_EXTRAS Log.error("MaaCore is not compiled with ASST_WITH_EMULATOR_EXTRAS"); @@ -263,12 +263,55 @@ void asst::AdbController::init_ld_extras([[maybe_unused]] const AdbCfg& adb_cfg) } std::filesystem::path ld_path = utils::path(adb_cfg.extras.get("path", "")); - int ld_index = adb_cfg.extras.get("index", 0); + int ld_index; + if (adb_cfg.extras.contains("index")) { + ld_index = adb_cfg.extras.get("index", 0); + } + else { + ld_index = get_ld_index(address); + } int ld_pid = adb_cfg.extras.get("pid", 0); m_ld_extras.init(ld_path, ld_index, ld_pid, m_width, m_height); #endif } +int asst::AdbController::get_ld_index(const std::string& address) +{ + LogTrace << VAR(address); + + // emulator-5554 + if (address.starts_with("emulator-")) { + constexpr int base_emulator_port = 5554; + std::string port_str = address.substr(9); // after "emulator-" + if (port_str.empty() || !ranges::all_of(port_str, [](char c) { return std::isdigit(c); })) { + Log.error("emulator port is invalid", port_str); + return 0; + } + int port = std::stoi(port_str); + int index = (port - base_emulator_port) / 2; + LogInfo << VAR(port_str) << VAR(port) << VAR(index); + return index; + } + + // 127.0.0.1:5555 + auto pos = address.find(':'); + if (pos != std::string::npos && address.substr(0, pos) == "127.0.0.1") { + constexpr int base_adb_port = 5555; + std::string port_str = address.substr(pos + 1); + if (port_str.empty() || !ranges::all_of(port_str, [](char c) { return std::isdigit(c); })) { + Log.error("adb port is invalid", port_str); + return 0; + } + int port = std::stoi(port_str); + int index = (port - base_adb_port) / 2; + LogInfo << VAR(port_str) << VAR(port) << VAR(index); + return index; + } + + Log.error("address is invalid or unsupported", address); + return 0; +} + void asst::AdbController::close_socket() noexcept { m_platform_io->close_socket(); @@ -1054,7 +1097,7 @@ bool asst::AdbController::connect(const std::string& adb_path, const std::string init_mumu_extras(adb_cfg, address); } else if (config == "LDPlayer") { - init_ld_extras(adb_cfg); + init_ld_extras(adb_cfg, address); } if (need_exit()) { diff --git a/src/MaaCore/Controller/AdbController.h b/src/MaaCore/Controller/AdbController.h index fef46d47d4..c752d7e13a 100644 --- a/src/MaaCore/Controller/AdbController.h +++ b/src/MaaCore/Controller/AdbController.h @@ -96,7 +96,8 @@ protected: static int get_mumu_index(const std::string& address); void init_mumu_extras(const AdbCfg& adb_cfg, const std::string& address); void set_mumu_package(const std::string& client_type); - void init_ld_extras([[maybe_unused]] const AdbCfg& adb_cfg); + void init_ld_extras([[maybe_unused]] const AdbCfg& adb_cfg, const std::string& address); + static int get_ld_index(const std::string& address); // 转换 data 中的 CRLF 为 LF:有些模拟器自带的 adb,exec-out 输出的 \n 会被替换成 \r\n, // 导致解码错误,所以这里转一下回来(点名批评 mumu 和雷电) diff --git a/src/MaaWpfGui/Constants/ConfigurationKeys.cs b/src/MaaWpfGui/Constants/ConfigurationKeys.cs index b1d451bbb8..84c88fca80 100644 --- a/src/MaaWpfGui/Constants/ConfigurationKeys.cs +++ b/src/MaaWpfGui/Constants/ConfigurationKeys.cs @@ -73,6 +73,7 @@ namespace MaaWpfGui.Constants public const string MuMu12Display = "Connect.MuMu12Display"; public const string LdPlayerExtrasEnabled = "Connect.LdPlayerExtras.Enabled"; public const string LdPlayerEmulatorPath = "Connect.LdPlayerEmulatorPath"; + public const string LdPlayerManualSetIndex = "Connect.LdPlayerManualSetIndex"; public const string LdPlayerIndex = "Connect.LdPlayerIndex"; public const string RetryOnAdbDisconnected = "Connect.RetryOnDisconnected"; public const string AllowAdbRestart = "Connect.AllowADBRestart"; diff --git a/src/MaaWpfGui/Res/Localizations/en-us.xaml b/src/MaaWpfGui/Res/Localizations/en-us.xaml index d015e722cc..8268a4e6fb 100644 --- a/src/MaaWpfGui/Res/Localizations/en-us.xaml +++ b/src/MaaWpfGui/Res/Localizations/en-us.xaml @@ -500,7 +500,8 @@ The instance number can be viewed in LDPlayer Multi-Instance Manager, The primary instance is 0, and other instances are the numbers after the version/instance ID.\n LD Emulator exec path - Instance Number + Manually enter 「{key=LdIndex}」 + Instance Index LD screenshot enhancement not enabled, please check related settings Retry launching the emulator when ADB connection fails 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 diff --git a/src/MaaWpfGui/Res/Localizations/ja-jp.xaml b/src/MaaWpfGui/Res/Localizations/ja-jp.xaml index 99b3762255..8e0640e951 100644 --- a/src/MaaWpfGui/Res/Localizations/ja-jp.xaml +++ b/src/MaaWpfGui/Res/Localizations/ja-jp.xaml @@ -502,6 +502,7 @@ C:\\leidian\\LDPlayer9 メインインスタンスは 0 で、その他のインスタンスはバージョン/IDの後の番号です。\n LD エミュレータパス + 「{key=LdIndex}」 を手動で入力 インスタンス番号 LD スクリーンショットの強化 が有効になっていません。関連する設定を確認してください ADB接続が失敗した場合、エミュレータの起動を試みる diff --git a/src/MaaWpfGui/Res/Localizations/ko-kr.xaml b/src/MaaWpfGui/Res/Localizations/ko-kr.xaml index 39b9d0f2a7..ae00340810 100644 --- a/src/MaaWpfGui/Res/Localizations/ko-kr.xaml +++ b/src/MaaWpfGui/Res/Localizations/ko-kr.xaml @@ -501,6 +501,7 @@ C:\\leidian\\LDPlayer9 메인 인스턴스는 0이고, 다른 인스턴스는 버전/ID 뒤에 오는 숫자입니다。\n LD 에뮬레이터 경로 + 「{key=LdIndex}」 수동 입력 인스턴스 번호 LD 스크린샷 강화 기능이 활성화되지 않았습니다. 관련 설정을 확인하세요 연결 실패 시 에뮬레이터 재시작 시도 diff --git a/src/MaaWpfGui/Res/Localizations/zh-cn.xaml b/src/MaaWpfGui/Res/Localizations/zh-cn.xaml index a849325447..fb6b1d5f12 100644 --- a/src/MaaWpfGui/Res/Localizations/zh-cn.xaml +++ b/src/MaaWpfGui/Res/Localizations/zh-cn.xaml @@ -501,6 +501,7 @@ C:\\leidian\\LDPlayer9。\n 主多开为 0,其他多开为 版本/编号 ID 后的数字。\n LD 安装路径 + 手动填写 「{key=LdIndex}」 实例编号 LD 截图增强未生效,请检查相关设置 ADB 连接失败时尝试启动模拟器 diff --git a/src/MaaWpfGui/Res/Localizations/zh-tw.xaml b/src/MaaWpfGui/Res/Localizations/zh-tw.xaml index 2f26c9daed..225d715692 100644 --- a/src/MaaWpfGui/Res/Localizations/zh-tw.xaml +++ b/src/MaaWpfGui/Res/Localizations/zh-tw.xaml @@ -499,6 +499,7 @@ C:\\leidian\\LDPlayer9。\n 主多開為 0,其他多開為版本/編號 ID 後的數字。\n LD 模擬器路徑 + 手動填寫 「{key=LdIndex}」 實例號碼 LD 截圖增強未生效,请檢查相關設定 ADB 連線失敗時嘗試啟動模擬器 diff --git a/src/MaaWpfGui/ViewModels/UserControl/Settings/ConnectSettingsUserControlModel.cs b/src/MaaWpfGui/ViewModels/UserControl/Settings/ConnectSettingsUserControlModel.cs index 4309544036..7223a5901e 100644 --- a/src/MaaWpfGui/ViewModels/UserControl/Settings/ConnectSettingsUserControlModel.cs +++ b/src/MaaWpfGui/ViewModels/UserControl/Settings/ConnectSettingsUserControlModel.cs @@ -286,7 +286,10 @@ public class ConnectSettingsUserControlModel : PropertyChangedBase foreach (var keyPath in possibleUninstallKeys) { using var driverKey = Registry.LocalMachine.OpenSubKey(keyPath); - if (driverKey == null) continue; + if (driverKey == null) + { + continue; + } uninstallString = driverKey.GetValue("UninstallString") as string; if (!string.IsNullOrEmpty(uninstallString) && uninstallString.Contains(UninstallExeName)) @@ -443,25 +446,29 @@ public class ConnectSettingsUserControlModel : PropertyChangedBase { try { - const string UninstallKeyPath = @"Software\leidian\ldplayer9"; + string[] possiblePaths = + [ + @"Software\mrfz\mrfz", // 专版路径优先 + @"Software\leidian\ldplayer9" // 原版路径 + ]; + const string InstallDirValueName = "InstallDir"; - using var driverKey = Registry.CurrentUser.OpenSubKey(UninstallKeyPath); - if (driverKey == null) + foreach (var regPath in possiblePaths) { - EmulatorPath = string.Empty; - return; + using var driverKey = Registry.CurrentUser.OpenSubKey(regPath); + if (driverKey == null) + { + continue; + } + + var installDir = driverKey.GetValue(InstallDirValueName) as string; + if (!string.IsNullOrEmpty(installDir)) + { + EmulatorPath = installDir; + break; + } } - - var installDir = driverKey.GetValue(InstallDirValueName) as string; - - if (string.IsNullOrEmpty(installDir)) - { - EmulatorPath = string.Empty; - return; - } - - EmulatorPath = installDir; } catch (Exception e) { @@ -499,6 +506,24 @@ public class ConnectSettingsUserControlModel : PropertyChangedBase } } + private bool _manualSetIndex = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.LdPlayerManualSetIndex, bool.FalseString)); + + public bool ManualSetIndex + { + get => _manualSetIndex; + set + { + if (_manualSetIndex == value) + { + return; + } + + SetAndNotify(ref _manualSetIndex, value); + Instances.AsstProxy.Connected = false; + ConfigurationHelper.SetValue(ConfigurationKeys.LdPlayerManualSetIndex, value.ToString()); + } + } + private string _index = ConfigurationHelper.GetValue(ConfigurationKeys.LdPlayerIndex, "0"); /// @@ -577,12 +602,18 @@ public class ConnectSettingsUserControlModel : PropertyChangedBase return JsonConvert.SerializeObject(new JObject()); } + var index = int.TryParse(Index, out var indexParse) ? indexParse : 0; + var configObject = new JObject { ["path"] = EmulatorPath, - ["index"] = int.TryParse(Index, out var indexParse) ? indexParse : 0, - ["pid"] = GetEmulatorPid(indexParse), + ["pid"] = GetEmulatorPid(index), }; + if (ManualSetIndex) + { + configObject["index"] = index; + } + return JsonConvert.SerializeObject(configObject); } } diff --git a/src/MaaWpfGui/Views/UserControl/Settings/ConnectSettingsUserControl.xaml b/src/MaaWpfGui/Views/UserControl/Settings/ConnectSettingsUserControl.xaml index 68579d534b..ef0d2477d6 100644 --- a/src/MaaWpfGui/Views/UserControl/Settings/ConnectSettingsUserControl.xaml +++ b/src/MaaWpfGui/Views/UserControl/Settings/ConnectSettingsUserControl.xaml @@ -168,24 +168,32 @@ HorizontalAlignment="Center" Content="{DynamicResource LdExtrasEnabled}" IsChecked="{Binding LdPlayerExtras.Enable}" /> - - + + + + + + +