From 1cbfb5368484f31234acf868e8d9a14a5b2bf890 Mon Sep 17 00:00:00 2001 From: MistEO Date: Wed, 12 Apr 2023 00:38:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B0=86=E9=80=80=E5=87=BA=E6=97=B6?= =?UTF-8?q?=E4=B8=8D=E9=87=8A=E6=94=BEadb=E4=BD=9C=E4=B8=BA=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resource/config.json | 5 -- src/MaaCore/Assistant.cpp | 10 +++ src/MaaCore/Common/AsstTypes.h | 1 + src/MaaCore/Controller/AdbController.cpp | 62 ++++++------------- src/MaaCore/Controller/AdbController.h | 12 ++-- src/MaaCore/Controller/Controller.cpp | 7 +++ src/MaaCore/Controller/Controller.h | 2 + src/MaaCore/Controller/ControllerAPI.h | 5 +- .../Controller/MinitouchController.cpp | 2 +- src/MaaCore/Controller/PlayToolsController.h | 2 - src/MaaWpfGui/Constants/ConfigurationKeys.cs | 1 + src/MaaWpfGui/Main/AsstProxy.cs | 2 + src/MaaWpfGui/Res/Localizations/en-us.xaml | 2 +- src/MaaWpfGui/Res/Localizations/zh-cn.xaml | 4 +- .../ViewModels/UI/SettingsViewModel.cs | 15 ++++- .../ConnectSettingsUserControl.xaml | 15 ++++- 16 files changed, 78 insertions(+), 69 deletions(-) diff --git a/resource/config.json b/resource/config.json index f5c5991473..a167c01d31 100644 --- a/resource/config.json +++ b/resource/config.json @@ -127,11 +127,6 @@ "baseConfig": "General", "display": "[Adb] -s [AdbSerial] shell dumpsys window displays | grep -o -E cur=+[^\\ ]+ | tail -n 1 | grep -o -E [0-9]+" }, - { - "configName": "NotKillAdb", - "baseConfig": "General", - "release": "[Adb] -s [AdbSerial] shell echo NotKillAdb" - }, { "configName": "CompatMac", "baseConfig": "Compatible", diff --git a/src/MaaCore/Assistant.cpp b/src/MaaCore/Assistant.cpp index a90f8dd0ef..ad60fbfefe 100644 --- a/src/MaaCore/Assistant.cpp +++ b/src/MaaCore/Assistant.cpp @@ -112,6 +112,16 @@ bool asst::Assistant::set_instance_option(InstanceOptionKey key, const std::stri return true; } break; + case InstanceOptionKey::KillAdbOnExit: + if (constexpr std::string_view Enable = "1"; value == Enable) { + m_ctrler->set_kill_adb_on_exit(true); + return true; + } + else if (constexpr std::string_view Disable = "0"; value == Disable) { + m_ctrler->set_kill_adb_on_exit(false); + return true; + } + break; default: break; } diff --git a/src/MaaCore/Common/AsstTypes.h b/src/MaaCore/Common/AsstTypes.h index c4afcd94e0..7b9b25a7ea 100644 --- a/src/MaaCore/Common/AsstTypes.h +++ b/src/MaaCore/Common/AsstTypes.h @@ -41,6 +41,7 @@ namespace asst TouchMode = 2, // 触控模式设置, "minitouch" | "maatouch" | "adb" DeploymentWithPause = 3, // 自动战斗、肉鸽、保全 是否使用 暂停下干员, "0" | "1" AdbLiteEnabled = 4, // 是否使用 AdbLite, "0" | "1" + KillAdbOnExit = 5, // 退出时是否杀掉 Adb 进程, "0" | "1" }; enum class TouchMode diff --git a/src/MaaCore/Controller/AdbController.cpp b/src/MaaCore/Controller/AdbController.cpp index d840621906..3a57040b1e 100644 --- a/src/MaaCore/Controller/AdbController.cpp +++ b/src/MaaCore/Controller/AdbController.cpp @@ -41,8 +41,8 @@ asst::AdbController::~AdbController() { LogTraceFunction; - make_instance_inited(false); - kill_adb_daemon(); + m_inited = false; + release(); } std::optional asst::AdbController::reconnect(const std::string& cmd, int64_t timeout, bool recv_by_socket) @@ -99,7 +99,8 @@ std::optional asst::AdbController::reconnect(const std::string& cmd { "cmd", m_adb.connect }, } }, }; - make_instance_inited(false); // 重连失败,释放 + m_inited = false; // 重连失败,释放 + release(); callback(AsstMsg::ConnectionInfo, info); return std::nullopt; @@ -140,7 +141,7 @@ std::optional asst::AdbController::call_command(const std::string& if (recv_by_socket && !sock_data.empty() && sock_data.size() < 4096) { Log.trace("socket output:", Logger::separator::newline, sock_data); } - // 直接 return,避免走到下面的 else if 里的 make_instance_inited(false) 关闭 adb 连接, + // 直接 return,避免走到下面的 else if 里的 m_inited = false) 关闭 adb 连接, // 导致停止后再开始任务还需要重连一次 if (need_exit()) { return std::nullopt; @@ -176,7 +177,7 @@ std::optional asst::AdbController::init_socket(const std::string void asst::AdbController::clear_info() noexcept { - make_instance_inited(false); + m_inited = false; m_adb = decltype(m_adb)(); m_uuid.clear(); m_width = 0; @@ -276,41 +277,11 @@ void asst::AdbController::release() { close_socket(); - if (!m_adb.release.empty()) { - m_adb_release.clear(); + if (m_kill_adb_on_exit && !m_adb.release.empty()) { m_platform_io->release_adb(m_adb.release, 20000); } } -void asst::AdbController::kill_adb_daemon() -{ - if (m_instance_count) return; - if (!m_adb_release.empty()) { - m_platform_io->release_adb(m_adb_release, 20000); - m_adb_release.clear(); - } -} - -void asst::AdbController::make_instance_inited(bool inited) -{ - Log.trace(__FUNCTION__, "|", inited, ", pre m_inited =", m_inited, ", pre m_instance_count =", m_instance_count); - - if (inited == m_inited) { - return; - } - m_inited = inited; - - if (inited) { - ++m_instance_count; - } - else { - // 所有实例全部释放,执行最终的 release 函数 - if (!--m_instance_count) { - release(); - } - } -} - const std::string& asst::AdbController::get_uuid() const { return m_uuid; @@ -400,7 +371,7 @@ bool asst::AdbController::screencap(cv::Mat& image_payload, bool allow_reconnect auto duration = duration_cast(high_resolution_clock::now() - start_time); if (duration < min_cost) { m_adb.screencap_method = AdbProperty::ScreencapMethod::RawByNc; - make_instance_inited(true); + m_inited = true; min_cost = duration; } Log.info("RawByNc cost", duration.count(), "ms"); @@ -415,7 +386,7 @@ bool asst::AdbController::screencap(cv::Mat& image_payload, bool allow_reconnect auto duration = duration_cast(high_resolution_clock::now() - start_time); if (duration < min_cost) { m_adb.screencap_method = AdbProperty::ScreencapMethod::RawWithGzip; - make_instance_inited(true); + m_inited = true; min_cost = duration; } Log.info("RawWithGzip cost", duration.count(), "ms"); @@ -430,7 +401,7 @@ bool asst::AdbController::screencap(cv::Mat& image_payload, bool allow_reconnect auto duration = duration_cast(high_resolution_clock::now() - start_time); if (duration < min_cost) { m_adb.screencap_method = AdbProperty::ScreencapMethod::Encode; - make_instance_inited(true); + m_inited = true; min_cost = duration; } Log.info("Encode cost", duration.count(), "ms"); @@ -529,7 +500,7 @@ bool asst::AdbController::connect(const std::string& adb_path, const std::string #ifdef ASST_DEBUG if (config == "DEBUG") { - make_instance_inited(true); + m_inited = true; return true; } #endif @@ -579,15 +550,14 @@ bool asst::AdbController::connect(const std::string& adb_path, const std::string /* connect */ { m_adb.connect = cmd_replace(adb_cfg.connect); + m_adb.release = cmd_replace(adb_cfg.release); auto connect_ret = call_command(m_adb.connect, 60LL * 1000, false /* adb 连接时不允许重试 */); bool is_connect_success = false; if (connect_ret) { auto& connect_str = connect_ret.value(); is_connect_success = connect_str.find("error") == std::string::npos; if (connect_str.find("daemon started successfully") != std::string::npos && - connect_str.find("daemon still not running") == std::string::npos) { - m_adb_release = cmd_replace(adb_cfg.release); - } + connect_str.find("daemon still not running") == std::string::npos) {} } if (!is_connect_success) { json::value info = get_info_json() | json::object { @@ -711,7 +681,6 @@ bool asst::AdbController::connect(const std::string& adb_path, const std::string m_adb.press_esc = cmd_replace(adb_cfg.press_esc); m_adb.screencap_raw_with_gzip = cmd_replace(adb_cfg.screencap_raw_with_gzip); m_adb.screencap_encode = cmd_replace(adb_cfg.screencap_encode); - m_adb_release = m_adb.release = cmd_replace(adb_cfg.release); m_adb.start = cmd_replace(adb_cfg.start); m_adb.stop = cmd_replace(adb_cfg.stop); @@ -751,6 +720,11 @@ bool asst::AdbController::connect(const std::string& adb_path, const std::string return true; } +void asst::AdbController::set_kill_adb_on_exit(bool enable) noexcept +{ + m_kill_adb_on_exit = enable; +} + void asst::AdbController::clear_lf_info() { m_adb.screencap_end_of_line = AdbProperty::ScreencapEndOfLine::UnknownYet; diff --git a/src/MaaCore/Controller/AdbController.h b/src/MaaCore/Controller/AdbController.h index 64a329a134..02048d1e33 100644 --- a/src/MaaCore/Controller/AdbController.h +++ b/src/MaaCore/Controller/AdbController.h @@ -21,9 +21,10 @@ namespace asst virtual bool connect(const std::string& adb_path, const std::string& address, const std::string& config) override; - virtual bool inited() const noexcept override; - virtual void set_swipe_with_pause([[maybe_unused]] bool enable) noexcept override {} + virtual void set_kill_adb_on_exit(bool enable) noexcept override; + + virtual bool inited() const noexcept override; virtual const std::string& get_uuid() const override; @@ -54,8 +55,6 @@ namespace asst virtual std::optional reconnect(const std::string& cmd, int64_t timeout, bool recv_by_socket); void release(); - void kill_adb_daemon(); - void make_instance_inited(bool inited); void close_socket() noexcept; std::optional init_socket(const std::string& local_address); @@ -118,15 +117,12 @@ namespace asst } m_adb; std::string m_uuid; - inline static std::string m_adb_release; // 开了 adb daemon,但是没连上模拟器的时候, - // m_adb 并不会存下 release 的命令,但最后仍然需要一次释放。 std::pair m_screen_size = { 0, 0 }; int m_width = 0; int m_height = 0; bool m_support_socket = false; bool m_server_started = false; bool m_inited = false; - - inline static int m_instance_count = 0; + bool m_kill_adb_on_exit = false; }; } // namespace asst diff --git a/src/MaaCore/Controller/Controller.cpp b/src/MaaCore/Controller/Controller.cpp index 60329cb728..383e988853 100644 --- a/src/MaaCore/Controller/Controller.cpp +++ b/src/MaaCore/Controller/Controller.cpp @@ -76,6 +76,7 @@ void asst::Controller::sync_params() { CHECK_EXIST(m_controller, ); m_controller->set_swipe_with_pause(m_swipe_with_pause); + m_controller->set_kill_adb_on_exit(m_kill_adb_on_exit); } cv::Mat asst::Controller::get_resized_image_cache() const @@ -248,6 +249,12 @@ void asst::Controller::set_adb_lite_enabled(bool enable) noexcept m_platform_type = enable ? PlatformType::AdbLite : PlatformType::Native; } +void asst::Controller::set_kill_adb_on_exit(bool enable) noexcept +{ + m_kill_adb_on_exit = enable; + sync_params(); +} + const std::string& asst::Controller::get_uuid() const { return m_uuid; diff --git a/src/MaaCore/Controller/Controller.h b/src/MaaCore/Controller/Controller.h index 59a524fdd5..4c117597dd 100644 --- a/src/MaaCore/Controller/Controller.h +++ b/src/MaaCore/Controller/Controller.h @@ -43,6 +43,7 @@ namespace asst void set_touch_mode(const TouchMode& mode) noexcept; void set_swipe_with_pause(bool enable) noexcept; void set_adb_lite_enabled(bool enable) noexcept; + void set_kill_adb_on_exit(bool enable) noexcept; const std::string& get_uuid() const; cv::Mat get_image(bool raw = false); @@ -96,6 +97,7 @@ namespace asst std::pair m_scale_size = { WindowWidthDefault, WindowHeightDefault }; bool m_swipe_with_pause = false; + bool m_kill_adb_on_exit = false; mutable std::shared_mutex m_image_mutex; cv::Mat m_cache_image; diff --git a/src/MaaCore/Controller/ControllerAPI.h b/src/MaaCore/Controller/ControllerAPI.h index 5af0152d4e..f5c6e574c0 100644 --- a/src/MaaCore/Controller/ControllerAPI.h +++ b/src/MaaCore/Controller/ControllerAPI.h @@ -24,7 +24,8 @@ namespace asst virtual bool connect(const std::string& adb_path, const std::string& address, const std::string& config) = 0; virtual bool inited() const noexcept = 0; - virtual void set_swipe_with_pause(bool enable) noexcept = 0; + virtual void set_swipe_with_pause([[maybe_unused]] bool enable) noexcept {} + virtual void set_kill_adb_on_exit([[maybe_unused]] bool enable) noexcept {} virtual const std::string& get_uuid() const = 0; @@ -70,4 +71,4 @@ namespace asst int keycode = 0; long milisec = 0; }; -} +} // namespace asst diff --git a/src/MaaCore/Controller/MinitouchController.cpp b/src/MaaCore/Controller/MinitouchController.cpp index 5517f31198..5244b9deee 100644 --- a/src/MaaCore/Controller/MinitouchController.cpp +++ b/src/MaaCore/Controller/MinitouchController.cpp @@ -314,7 +314,7 @@ bool asst::MinitouchController::connect(const std::string& adb_path, const std:: #ifdef ASST_DEBUG if (config == "DEBUG") { - make_instance_inited(true); + m_inited = true; return true; } #endif diff --git a/src/MaaCore/Controller/PlayToolsController.h b/src/MaaCore/Controller/PlayToolsController.h index a6b5b18970..8f4d1ad0de 100644 --- a/src/MaaCore/Controller/PlayToolsController.h +++ b/src/MaaCore/Controller/PlayToolsController.h @@ -25,8 +25,6 @@ namespace asst const std::string& config) override; virtual bool inited() const noexcept override; - virtual void set_swipe_with_pause([[maybe_unused]] bool enable) noexcept override {} - virtual const std::string& get_uuid() const override; virtual bool screencap(cv::Mat& image_payload, bool allow_reconnect = false) override; diff --git a/src/MaaWpfGui/Constants/ConfigurationKeys.cs b/src/MaaWpfGui/Constants/ConfigurationKeys.cs index 6e29fdd225..f0efb0a5b7 100644 --- a/src/MaaWpfGui/Constants/ConfigurationKeys.cs +++ b/src/MaaWpfGui/Constants/ConfigurationKeys.cs @@ -45,6 +45,7 @@ namespace MaaWpfGui.Constants public const string ConnectConfig = "Connect.ConnectConfig"; public const string RetryOnAdbDisconnected = "Connect.RetryOnDisconnected"; public const string AdbLiteEnabled = "Connect.AdbLiteEnabled"; + public const string KillAdbOnExit = "Connect.KillAdbOnExit"; public const string TouchMode = "Connect.TouchMode"; public const string AdbReplaced = "Connect.AdbReplaced"; diff --git a/src/MaaWpfGui/Main/AsstProxy.cs b/src/MaaWpfGui/Main/AsstProxy.cs index 9750c58959..3565327ca8 100644 --- a/src/MaaWpfGui/Main/AsstProxy.cs +++ b/src/MaaWpfGui/Main/AsstProxy.cs @@ -1801,5 +1801,7 @@ namespace MaaWpfGui.Main /// Indicates whether AdbLite is used. /// AdbLiteEnabled = 4, + + KillAdbOnExit = 5, } } diff --git a/src/MaaWpfGui/Res/Localizations/en-us.xaml b/src/MaaWpfGui/Res/Localizations/en-us.xaml index 47a205c89b..10b4269e2a 100644 --- a/src/MaaWpfGui/Res/Localizations/en-us.xaml +++ b/src/MaaWpfGui/Res/Localizations/en-us.xaml @@ -66,7 +66,6 @@ MaaTouch Compatible Mode 2nd Resolution - Not Kill Adb Morale Threshold If custom shift is enabled, this field is only valid for autofilled rooms @@ -81,6 +80,7 @@ Enable nonfirend support Deployment with Pause, Works for IS, Copilot and SSS, Beta function, Not recommended yet Use Adb Lite (Experimental) + Kill Adb On Exit Default Mind over matter (to be updated) diff --git a/src/MaaWpfGui/Res/Localizations/zh-cn.xaml b/src/MaaWpfGui/Res/Localizations/zh-cn.xaml index 0367bb951b..00e4a827cd 100644 --- a/src/MaaWpfGui/Res/Localizations/zh-cn.xaml +++ b/src/MaaWpfGui/Res/Localizations/zh-cn.xaml @@ -65,7 +65,6 @@ MaaTouch 兼容模式 第二分辨率 - Not Kill Adb 通用模式(屏蔽异常输出) 基建工作心情阈值 @@ -81,6 +80,7 @@ 可以使用非好友助战 暂停下干员(同时影响肉鸽、自动战斗、保全)(不稳定,暂不推荐开启) 使用 Adb Lite(实验性功能) + 退出时释放 Adb 默认分队 心胜于物分队 @@ -130,7 +130,7 @@ 语言 语言设置已更改,是否现在重启 MAA 以应用语言设置? 稍后 - + 提示 diff --git a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs index c20491f1c1..da514bab1b 100644 --- a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs @@ -234,7 +234,6 @@ namespace MaaWpfGui.ViewModels.UI new CombinedData { Display = LocalizationHelper.GetString("WSA"), Value = "WSA" }, new CombinedData { Display = LocalizationHelper.GetString("Compatible"), Value = "Compatible" }, new CombinedData { Display = LocalizationHelper.GetString("SecondResolution"), Value = "SecondResolution" }, - new CombinedData { Display = LocalizationHelper.GetString("NotKillAdb"), Value = "NotKillAdb" }, new CombinedData { Display = LocalizationHelper.GetString("GeneralWithoutScreencapErr"), Value = "GeneralWithoutScreencapErr" }, }; @@ -2060,6 +2059,19 @@ namespace MaaWpfGui.ViewModels.UI } } + private bool _killAdbOnExit = bool.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.KillAdbOnExit, false.ToString())); + + public bool KillAdbOnExit + { + get => _killAdbOnExit; + set + { + SetAndNotify(ref _killAdbOnExit, value); + ConfigurationHelper.SetValue(ConfigurationKeys.KillAdbOnExit, value.ToString()); + UpdateInstanceSettings(); + } + } + /// /// Gets the default addresses. /// @@ -2233,6 +2245,7 @@ namespace MaaWpfGui.ViewModels.UI _asstProxy.AsstSetInstanceOption(InstanceOptionKey.TouchMode, TouchMode); _asstProxy.AsstSetInstanceOption(InstanceOptionKey.DeploymentWithPause, DeploymentWithPause ? "1" : "0"); _asstProxy.AsstSetInstanceOption(InstanceOptionKey.AdbLiteEnabled, AdbLiteEnabled ? "1" : "0"); + _asstProxy.AsstSetInstanceOption(InstanceOptionKey.KillAdbOnExit, KillAdbOnExit ? "1" : "0"); } private static readonly string GoogleAdbDownloadUrl = "https://dl.google.com/android/repository/platform-tools-latest-windows.zip"; diff --git a/src/MaaWpfGui/Views/UserControl/ConnectSettingsUserControl.xaml b/src/MaaWpfGui/Views/UserControl/ConnectSettingsUserControl.xaml index e71af26412..f8ce4c4a4f 100644 --- a/src/MaaWpfGui/Views/UserControl/ConnectSettingsUserControl.xaml +++ b/src/MaaWpfGui/Views/UserControl/ConnectSettingsUserControl.xaml @@ -178,9 +178,18 @@ +