Skip to content

Commit

Permalink
feat: adb config cache
Browse files Browse the repository at this point in the history
  • Loading branch information
dongwlin committed Nov 20, 2023
1 parent b477181 commit ae99063
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 29 deletions.
206 changes: 206 additions & 0 deletions assets/resource/adb_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"prebuilt": {
"minicap": {
"arch": [
"x86",
"armeabi-v7a",
"armeabi"
],
"sdk": [
31,
29,
28,
27,
26,
25,
24,
23,
22,
21,
19,
18,
17,
16,
15,
14
]
},
"minitouch": {
"arch": [
"x86_64",
"x86",
"arm64-v8a",
"armeabi-v7a",
"armeabi"
]
},
"maatouch": {
"package": "com.shxyke.MaaTouch.App"
}
},
"command": {
"Devices": [
"{ADB}",
"devices"
],
"Connect": [
"{ADB}",
"connect",
"{ADB_SERIAL}"
],
"KillServer": [
"{ADB}",
"kill-server"
],
"UUID": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"settings get secure android_id"
],
"Resolution": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"dumpsys window displays | grep -o -E cur=+[^\\ ]+ | grep -o -E [0-9]+"
],
"StartApp": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"am start -n {INTENT}"
],
"StopApp": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"am force-stop {INTENT}"
],
"Click": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"input tap {X} {Y}"
],
"Swipe": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"input swipe {X1} {Y1} {X2} {Y2} {DURATION}"
],
"PressKey": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"input keyevent {KEY}"
],
"ForwardSocket": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"forward",
"tcp:{FOWARD_PORT}",
"localabstract:{LOCAL_SOCKET}"
],
"NetcatAddress": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"cat /proc/net/arp | grep : "
],
"ScreencapRawByNetcat": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"exec-out",
"screencap | nc -w 3 {NETCAT_ADDRESS} {NETCAT_PORT}"
],
"ScreencapRawWithGzip": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"exec-out",
"screencap | gzip -1"
],
"ScreencapEncode": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"exec-out",
"screencap -p"
],
"ScreencapEncodeToFile": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"screencap -p > \"/data/local/tmp/{TEMP_FILE}\""
],
"PullFile": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"pull",
"/data/local/tmp/{TEMP_FILE}",
"{DST_PATH}"
],
"Abilist": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"getprop ro.product.cpu.abilist | tr -d '\n\r'"
],
"SDK": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"getprop ro.build.version.sdk | tr -d '\n\r'"
],
"Orientation": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"dumpsys input | grep SurfaceOrientation | grep -m 1 -o -E [0-9]"
],
"PushBin": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"push",
"{BIN_PATH}",
"/data/local/tmp/{BIN_WORKING_FILE}"
],
"ChmodBin": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"chmod 700 \"/data/local/tmp/{BIN_WORKING_FILE}\""
],
"InvokeBin": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"exec-out",
"export LD_LIBRARY_PATH=/data/local/tmp/; \"/data/local/tmp/{BIN_WORKING_FILE}\" {BIN_EXTRA_PARAMS}"
],
"InvokeApp": [
"{ADB}",
"-s",
"{ADB_SERIAL}",
"shell",
"export CLASSPATH=\"/data/local/tmp/{APP_WORKING_FILE}\"; app_process /data/local/tmp {PACKAGE_NAME}"
]
}
}
1 change: 1 addition & 0 deletions source/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_executable(MAABH3_CLI
Config/DeviceConfig.cpp
Config/TasksConfig.cpp
Config/ConfigMgr.cpp
Cache/AdbConfigCache.cpp
)
target_link_libraries(MAABH3_CLI PRIVATE MaaFramework MaaToolKit)

Expand Down
98 changes: 98 additions & 0 deletions source/cli/Cache/AdbConfigCache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#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::string filename = cache_key + ".json";
std::filesystem::path file_path = adb_config_cache_dir / filename;
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 std::string();
}
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();
std::string adb_config = buffer.str();

return adb_config;
}

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<std::string> {}(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::string filename = cache_key + ".json";
std::filesystem::path file_path = adb_config_cache_dir / filename;
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::string filename = cache_key + ".json";
std::filesystem::path file_path = adb_config_cache_dir / filename;
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();

// Update cache map
cache_map[cache_key] = adb_config;
return true;
}
33 changes: 33 additions & 0 deletions source/cli/Cache/AdbConfigCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "Cache.h"

#include <fstream>
#include <functional>
#include <iostream>
#include <unordered_map>

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<std::string, std::string> cache_map;
};
14 changes: 14 additions & 0 deletions source/cli/Cache/Cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <filesystem>
#include <string>

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";
};
Loading

0 comments on commit ae99063

Please sign in to comment.