From 35e704d74b999cf79945b9a0467567625d6f3bed Mon Sep 17 00:00:00 2001 From: dongwlin Date: Wed, 11 Dec 2024 12:44:49 +0800 Subject: [PATCH] refactor: remove adb config cache --- source/cli/CMakeLists.txt | 1 - source/cli/Cache/AdbConfigCache.cpp | 96 ----------------------------- source/cli/Cache/AdbConfigCache.h | 34 ---------- source/cli/Cache/Cache.h | 14 ----- source/cli/device.cpp | 2 - source/cli/device.h | 1 - source/cli/main.cpp | 22 ++++++- source/cli/main.h | 3 +- 8 files changed, 21 insertions(+), 152 deletions(-) delete mode 100644 source/cli/Cache/AdbConfigCache.cpp delete mode 100644 source/cli/Cache/AdbConfigCache.h delete mode 100644 source/cli/Cache/Cache.h diff --git a/source/cli/CMakeLists.txt b/source/cli/CMakeLists.txt index c8934f6..0dfc6eb 100644 --- a/source/cli/CMakeLists.txt +++ b/source/cli/CMakeLists.txt @@ -25,7 +25,6 @@ add_executable(MAABH3_CLI Config/DeviceConfig.cpp Config/TasksConfig.cpp Config/ConfigMgr.cpp - Cache/AdbConfigCache.cpp ) target_link_libraries(MAABH3_CLI PRIVATE MaaFramework MaaToolkit) diff --git a/source/cli/Cache/AdbConfigCache.cpp b/source/cli/Cache/AdbConfigCache.cpp deleted file mode 100644 index 811e32a..0000000 --- a/source/cli/Cache/AdbConfigCache.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include "AdbConfigCache.h" - -bool AdbConfigCache::has(const std::string& device_name, const std::string& device_SN) -{ - std::string cache_key = generate_cache_key(device_name, device_SN); - std::filesystem::path file_path = adb_config_cache_dir / cache_key; - if (!std::filesystem::exists(file_path)) { - return false; - } - return true; -} - -bool AdbConfigCache::set_adb_config(const std::string& device_name, const std::string& device_SN, - const std::string& adb_config) -{ - std::string cache_key = generate_cache_key(device_name, device_SN); - if (!save(cache_key, adb_config)) { - return false; - } - return true; -} - -std::string AdbConfigCache::get_adb_config(const std::string& device_name, const std::string& device_SN) -{ - std::string cache_key = generate_cache_key(device_name, device_SN); - std::string adb_config; - if (!load(cache_key, adb_config)) { - return get_default_adb_config(); - } - return adb_config; -} - -std::string AdbConfigCache::get_default_adb_config() -{ - std::filesystem::path file_path = local_dir / "resource" / "adb_config.json"; - std::ifstream ifs(file_path, std::ios::in); - if (!ifs.is_open()) { - std::cerr << "Failed to open default adb config file: " << file_path << std::endl; - return std::string(); - } - - std::stringstream buffer; - buffer << ifs.rdbuf(); - ifs.close(); - - return buffer.str(); -} - -std::string AdbConfigCache::generate_cache_key(const std::string& device_name, const std::string& device_SN) -{ - std::string cache_key = device_name + "_" + device_SN; - size_t hash = std::hash {}(cache_key); - cache_key = std::to_string(hash); - return cache_key; -} - -bool AdbConfigCache::save(const std::string& cache_key, const std::string& adb_config) -{ - std::filesystem::create_directories(adb_config_cache_dir); - std::filesystem::path file_path = adb_config_cache_dir / cache_key; - std::ofstream ofs(file_path, std::ios::out); - if (!ofs.is_open()) { - std::cerr << "Failed to open cache file: " << file_path << std::endl; - return false; - } - ofs << adb_config; - ofs.close(); - - // Update cache map - cache_map[cache_key] = adb_config; - return true; -} - -bool AdbConfigCache::load(const std::string& cache_key, std::string& adb_config) -{ - if (cache_map.count(cache_key) > 0) { - adb_config = cache_map[cache_key]; - return true; - } - - std::filesystem::path file_path = adb_config_cache_dir / cache_key; - std::ifstream ifs(file_path, std::ios::in); - if (!ifs.is_open()) { - std::cerr << "Failed to open cache file: " << file_path << std::endl; - return false; - } - - std::stringstream buffer; - buffer << ifs.rdbuf(); - adb_config = buffer.str(); - ifs.close(); - - // Update cache map - cache_map[cache_key] = adb_config; - return true; -} \ No newline at end of file diff --git a/source/cli/Cache/AdbConfigCache.h b/source/cli/Cache/AdbConfigCache.h deleted file mode 100644 index 17fbced..0000000 --- a/source/cli/Cache/AdbConfigCache.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include "Cache.h" - -#include -#include -#include -#include -#include - -class AdbConfigCache : public Cache -{ -public: - virtual ~AdbConfigCache() = default; - -public: - inline static const std::filesystem::path adb_config_cache_dir = cache_dir / "adb_config"; - -public: - bool has(const std::string& device_name, const std::string& device_SN); - -public: - bool set_adb_config(const std::string& device_name, const std::string& device_SN, const std::string& adb_config); - std::string get_adb_config(const std::string& device_name, const std::string& device_SN); - std::string get_default_adb_config(); - -private: - std::string generate_cache_key(const std::string& device_name, const std::string& device_SN); - bool save(const std::string& cache_key, const std::string& adb_config); - bool load(const std::string& cache_key, std::string& adb_config); - -private: - std::unordered_map cache_map; -}; \ No newline at end of file diff --git a/source/cli/Cache/Cache.h b/source/cli/Cache/Cache.h deleted file mode 100644 index 0e038bc..0000000 --- a/source/cli/Cache/Cache.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include -#include - -class Cache -{ -public: - virtual ~Cache() = default; - -public: - inline static const std::filesystem::path local_dir = "."; - inline static const std::filesystem::path cache_dir = local_dir / "cache"; -}; \ No newline at end of file diff --git a/source/cli/device.cpp b/source/cli/device.cpp index 216934e..cd906c2 100644 --- a/source/cli/device.cpp +++ b/source/cli/device.cpp @@ -48,8 +48,6 @@ bool select_device(std::string& name, std::string& SN, std::string& adb) name = MaaToolkitGetDeviceName(device_index); SN = MaaToolkitGetDeviceAdbSerial(device_index); adb = MaaToolkitGetDeviceAdbPath(device_index); - AdbConfigCache adb_config_cache; - adb_config_cache.set_adb_config(name, SN, MaaToolkitGetDeviceAdbConfig(device_index)); return true; } diff --git a/source/cli/device.h b/source/cli/device.h index a65346b..209944d 100644 --- a/source/cli/device.h +++ b/source/cli/device.h @@ -4,7 +4,6 @@ #include #include "Config/DeviceConfig.h" -#include "Cache/AdbConfigCache.h" bool default_device_init(DeviceConfig& device); diff --git a/source/cli/main.cpp b/source/cli/main.cpp index 3bd9d33..02f394f 100644 --- a/source/cli/main.cpp +++ b/source/cli/main.cpp @@ -86,8 +86,7 @@ int main(int argc, char** argv) config.init(); - AdbConfigCache adb_config_cache; - adb_config = adb_config_cache.get_adb_config(device.get_config_device_name(), device.get_config_device_SN()); + adb_config = get_default_adb_config(); bool identified = app_package_and_activity(control.get_config_server(), package, activity); if (!identified) { @@ -247,7 +246,24 @@ void print_version() << std::endl; } +std::string get_default_adb_config() +{ + const std::filesystem::path local_dir = "."; + std::filesystem::path file_path = local_dir / "resource" / "adb_config.json"; + std::ifstream ifs(file_path, std::ios::in); + if (!ifs.is_open()) { + std::cerr << "Failed to open default adb config file: " << file_path << std::endl; + return std::string(); + } + + std::stringstream buffer; + buffer << ifs.rdbuf(); + ifs.close(); + + return buffer.str(); +} + void mpause() { std::ignore = getchar(); -} \ No newline at end of file +} diff --git a/source/cli/main.h b/source/cli/main.h index f003d1c..fbf9a44 100644 --- a/source/cli/main.h +++ b/source/cli/main.h @@ -9,7 +9,6 @@ #include #include -#include "Cache/AdbConfigCache.h" #include "Config/ConfigMgr.h" #include "CustomAction/CustomActionRegistry.h" @@ -26,4 +25,6 @@ void print_help(); void print_version(); +std::string get_default_adb_config(); + void mpause(); \ No newline at end of file