rename some function and variable

This commit is contained in:
MistEO
2021-07-24 16:23:36 +08:00
parent 5de4aa5074
commit 9418a0c857
15 changed files with 106 additions and 103 deletions

View File

@@ -20,7 +20,7 @@ namespace asst {
Assistance();
~Assistance();
std::optional<std::string> setSimulator(const std::string & simulator_name = std::string());
std::optional<std::string> set_emulator(const std::string & emulator_name = std::string());
void start(const std::string & task);
void stop(bool block = true);
@@ -30,7 +30,7 @@ namespace asst {
bool print_window(const std::string& filename, bool block = true);
private:
static void workingProc(Assistance* pThis);
static void working_proc(Assistance* pThis);
std::shared_ptr<WinMacro> m_pWindow = nullptr;
std::shared_ptr<WinMacro> m_pView = nullptr;

View File

@@ -8,7 +8,7 @@ extern "C" {
extern __declspec(dllexport) asst::Assistance* CreateAsst();
extern __declspec(dllexport) void DestoryAsst(asst::Assistance* p_asst);
extern __declspec(dllexport) bool AsstCatchSimulator(asst::Assistance* p_asst);
extern __declspec(dllexport) bool AsstCatchEmulator(asst::Assistance* p_asst);
extern __declspec(dllexport) void AsstStart(asst::Assistance* p_asst, const char* task);
extern __declspec(dllexport) void AsstStop(asst::Assistance* p_asst);
extern __declspec(dllexport) bool AsstSetParam(asst::Assistance* p_asst, const char* type, const char* param, const char* value);

View File

@@ -100,8 +100,8 @@ namespace asst {
};
struct HandleInfo {
std::string className;
std::string windowName;
std::string class_name;
std::string window_name;
};
struct AdbCmd {
@@ -110,7 +110,7 @@ namespace asst {
std::string click;
};
struct SimulatorInfo {
struct EmulatorInfo {
std::string name;
std::vector<HandleInfo> window;
std::vector<HandleInfo> view;

View File

@@ -33,7 +33,7 @@ namespace asst {
std::string m_version;
Options m_options;
std::unordered_map<std::string, TaskInfo> m_tasks;
std::unordered_map<std::string, SimulatorInfo> m_handles;
std::unordered_map<std::string, EmulatorInfo> m_handles;
private:

View File

@@ -18,22 +18,22 @@ namespace asst {
Identify() = default;
~Identify() = default;
void setUseCache(bool b) noexcept;
bool addImage(const std::string& name, const std::string& path);
void set_use_cache(bool b) noexcept;
bool add_image(const std::string& name, const std::string& path);
// return tuple< algorithmType, suitability, scaled asst::rect>
std::tuple<AlgorithmType, double, asst::Rect> findImage(const cv::Mat& image, const std::string& templ, double threshold = 0.99);
std::tuple<AlgorithmType, double, asst::Rect> find_image(const cv::Mat& image, const std::string& templ, double threshold = 0.99);
void clear_cache();
private:
cv::Mat image2Hist(const cv::Mat& src);
double imageHistComp(const cv::Mat& src, const cv::MatND& hist);
asst::Rect cvRect2Rect(const cv::Rect& cvRect) {
cv::Mat image2hist(const cv::Mat& src);
double image_hist_comp(const cv::Mat& src, const cv::MatND& hist);
static asst::Rect cvrect2rect(const cv::Rect& cvRect) {
return asst::Rect(cvRect.x, cvRect.y, cvRect.width, cvRect.height);
}
// return pair< suitability, raw opencv::point>
std::pair<double, cv::Point> findImage(const cv::Mat& cur, const cv::Mat& templ);
std::pair<double, cv::Point> find_image(const cv::Mat& cur, const cv::Mat& templ);
std::unordered_map<std::string, cv::Mat> m_mat_map;
bool m_use_cache = true;

View File

@@ -21,7 +21,7 @@ namespace asst {
public:
~Updater() = default;
static Updater& instance();
static Updater& get_instance();
bool has_new_version();
const VersionInfo & get_version_info() const noexcept;

View File

@@ -11,7 +11,7 @@ namespace asst {
class WinMacro
{
public:
WinMacro(const SimulatorInfo & info, HandleType type);
WinMacro(const EmulatorInfo & info, HandleType type);
~WinMacro() = default;
bool captured() const noexcept;
@@ -20,14 +20,17 @@ namespace asst {
bool showWindow();
bool hideWindow();
bool click(const Point & p);
bool clickRange(const Rect & rect);
bool click(const Rect & rect);
cv::Mat getImage(const Rect& rect);
Rect getWindowRect();
const EmulatorInfo& getEmulatorInfo() const noexcept { return m_emulator_info; }
const HandleType& getHandleType() const noexcept { return m_handle_type; }
static double getScreenScale();
private:
bool findHandle();
const SimulatorInfo m_simulator_info;
const EmulatorInfo m_emulator_info;
const HandleType m_handle_type;
HWND m_handle = NULL;
bool m_is_adb = false;

View File

@@ -20,11 +20,11 @@ Assistance::Assistance()
m_pIder = std::make_shared<Identify>();
for (auto&& [name, info] : m_configer.m_tasks)
{
m_pIder->addImage(name, GetResourceDir() + info.filename);
m_pIder->add_image(name, GetResourceDir() + info.filename);
}
m_pIder->setUseCache(m_configer.m_options.identify_cache);
m_pIder->set_use_cache(m_configer.m_options.identify_cache);
m_working_thread = std::thread(workingProc, this);
m_working_thread = std::thread(working_proc, this);
}
Assistance::~Assistance()
@@ -44,13 +44,13 @@ Assistance::~Assistance()
}
}
std::optional<std::string> Assistance::setSimulator(const std::string& simulator_name)
std::optional<std::string> Assistance::set_emulator(const std::string& emulator_name)
{
DebugTraceFunction;
stop();
auto create_handles = [&](const SimulatorInfo& info) -> bool {
auto create_handles = [&](const EmulatorInfo& info) -> bool {
m_pWindow = std::make_shared<WinMacro>(info, HandleType::Window);
m_pView = std::make_shared<WinMacro>(info, HandleType::View);
m_pCtrl = std::make_shared<WinMacro>(info, HandleType::Control);
@@ -58,11 +58,11 @@ std::optional<std::string> Assistance::setSimulator(const std::string& simulator
};
bool ret = false;
std::string cor_name = simulator_name;
std::string cor_name = emulator_name;
std::unique_lock<std::mutex> lock(m_mutex);
if (simulator_name.empty()) {
if (emulator_name.empty()) {
for (auto&& [name, info] : m_configer.m_handles)
{
ret = create_handles(info);
@@ -73,7 +73,7 @@ std::optional<std::string> Assistance::setSimulator(const std::string& simulator
}
}
else {
ret = create_handles(m_configer.m_handles[simulator_name]);
ret = create_handles(m_configer.m_handles[emulator_name]);
}
if (ret && m_pWindow->showWindow() && m_pWindow->resizeWindow()) {
m_inited = true;
@@ -163,7 +163,7 @@ bool asst::Assistance::print_window(const std::string& filename, bool block)
return ret;
}
void Assistance::workingProc(Assistance* pThis)
void Assistance::working_proc(Assistance* pThis)
{
DebugTraceFunction;
@@ -194,7 +194,7 @@ void Assistance::workingProc(Assistance* pThis)
double threshold = pThis->m_configer.m_tasks[task_name].threshold;
double cache_threshold = pThis->m_configer.m_tasks[task_name].cache_threshold;
auto&& [algorithm, value, rect] = pThis->m_pIder->findImage(curImg, task_name, threshold);
auto&& [algorithm, value, rect] = pThis->m_pIder->find_image(curImg, task_name, threshold);
DebugTrace(task_name, "Type:", algorithm, "Value:", value);
if (algorithm == AlgorithmType::JustReturn ||
(algorithm == AlgorithmType::MatchTemplate && value >= threshold)
@@ -235,10 +235,10 @@ void Assistance::workingProc(Assistance* pThis)
matched_rect = task.specific_area;
[[fallthrough]];
case TaskType::ClickSelf:
pThis->m_pCtrl->clickRange(matched_rect);
pThis->m_pCtrl->click(matched_rect);
break;
case TaskType::ClickRand:
pThis->m_pCtrl->clickRange(pThis->m_pCtrl->getWindowRect());
pThis->m_pCtrl->click(pThis->m_pCtrl->getWindowRect());
break;
case TaskType::DoNothing:
break;

View File

@@ -18,13 +18,13 @@ void DestoryAsst(asst::Assistance* p_asst)
p_asst = NULL;
}
bool AsstCatchSimulator(asst::Assistance* p_asst)
bool AsstCatchEmulator(asst::Assistance* p_asst)
{
if (p_asst == NULL) {
return false;
}
auto ret = p_asst->setSimulator();
auto ret = p_asst->set_emulator();
if (ret) {
return true;
}
@@ -74,11 +74,11 @@ bool AsstGetParam(asst::Assistance* p_asst, const char* type, const char* param,
bool CheckVersionUpdate(char* tag_buffer, int tag_bufsize, char* html_url_buffer, int html_bufsize, char* body_buffer, int body_bufsize)
{
bool ret = asst::Updater::instance().has_new_version();
bool ret = asst::Updater::get_instance().has_new_version();
if (!ret) {
return false;
}
auto && info = asst::Updater::instance().get_version_info();
auto && info = asst::Updater::get_instance().get_version_info();
strcpy_s(tag_buffer, tag_bufsize, info.tag_name.c_str());
strcpy_s(html_url_buffer, html_bufsize, info.html_url.c_str());
strcpy_s(body_buffer, body_bufsize, info.body.c_str());

View File

@@ -65,8 +65,8 @@ bool Configer::reload(const std::string& filename)
else {
task_info.threshold = DefaultThreshold;
}
if (task_json.exist("cache_threshold")) {
task_info.cache_threshold = task_json["cache_threshold"].as_double();
if (task_json.exist("cacheThreshold")) {
task_info.cache_threshold = task_json["cacheThreshold"].as_double();
}
else {
task_info.cache_threshold = DefaultCacheThreshold;
@@ -137,44 +137,44 @@ bool Configer::reload(const std::string& filename)
}
auto handle_obj = root["handle"].as_object();
for (auto&& [name, simulator_json] : handle_obj) {
SimulatorInfo simulator_info;
simulator_info.name = name;
for (auto&& [name, emulator_json] : handle_obj) {
EmulatorInfo emulator_info;
emulator_info.name = name;
auto window_arr = simulator_json["window"].as_array();
auto window_arr = emulator_json["window"].as_array();
for (auto&& info : window_arr) {
HandleInfo handle_info;
handle_info.className = info["class"].as_string();
handle_info.windowName = info["window"].as_string();
simulator_info.window.emplace_back(handle_info);
handle_info.class_name = info["class"].as_string();
handle_info.window_name = info["window"].as_string();
emulator_info.window.emplace_back(handle_info);
}
auto view_arr = simulator_json["view"].as_array();
auto view_arr = emulator_json["view"].as_array();
for (auto&& info : view_arr) {
HandleInfo handle_info;
handle_info.className = info["class"].as_string();
handle_info.windowName = info["window"].as_string();
simulator_info.view.emplace_back(handle_info);
handle_info.class_name = info["class"].as_string();
handle_info.window_name = info["window"].as_string();
emulator_info.view.emplace_back(handle_info);
}
auto ctrl_arr = simulator_json["control"].as_array();
auto ctrl_arr = emulator_json["control"].as_array();
for (auto&& info : ctrl_arr) {
HandleInfo handle_info;
handle_info.className = info["class"].as_string();
handle_info.windowName = info["window"].as_string();
simulator_info.control.emplace_back(handle_info);
handle_info.class_name = info["class"].as_string();
handle_info.window_name = info["window"].as_string();
emulator_info.control.emplace_back(handle_info);
}
if (simulator_json.exist("adbControl")) {
simulator_info.is_adb = true;
if (emulator_json.exist("adbControl")) {
emulator_info.is_adb = true;
// meojson的bug暂时没空修先转个字符串
simulator_info.adb.path = StringReplaceAll(simulator_json["adbControl"]["path"].as_string(), "\\\\", "\\");
simulator_info.adb.connect = simulator_json["adbControl"]["connect"].as_string();
simulator_info.adb.click = simulator_json["adbControl"]["click"].as_string();
emulator_info.adb.path = StringReplaceAll(emulator_json["adbControl"]["path"].as_string(), "\\\\", "\\");
emulator_info.adb.connect = emulator_json["adbControl"]["connect"].as_string();
emulator_info.adb.click = emulator_json["adbControl"]["click"].as_string();
}
simulator_info.width = simulator_json["width"].as_integer();
simulator_info.height = simulator_json["height"].as_integer();
simulator_info.x_offset = simulator_json["xOffset"].as_integer();
simulator_info.y_offset = simulator_json["yOffset"].as_integer();
emulator_info.width = emulator_json["width"].as_integer();
emulator_info.height = emulator_json["height"].as_integer();
emulator_info.x_offset = emulator_json["xOffset"].as_integer();
emulator_info.y_offset = emulator_json["yOffset"].as_integer();
temp.m_handles.emplace(name, simulator_info);
temp.m_handles.emplace(name, emulator_info);
}
}
catch (json::exception& e) {

View File

@@ -6,7 +6,7 @@
using namespace asst;
using namespace cv;
bool Identify::addImage(const std::string& name, const std::string& path)
bool Identify::add_image(const std::string& name, const std::string& path)
{
Mat mat = imread(path);
if (mat.empty()) {
@@ -16,7 +16,7 @@ bool Identify::addImage(const std::string& name, const std::string& path)
return true;
}
void Identify::setUseCache(bool b) noexcept
void Identify::set_use_cache(bool b) noexcept
{
if (b) {
m_use_cache = true;
@@ -27,7 +27,7 @@ void Identify::setUseCache(bool b) noexcept
}
}
Mat Identify::image2Hist(const cv::Mat& src)
Mat Identify::image2hist(const cv::Mat& src)
{
Mat src_hsv;
cvtColor(src, src_hsv, COLOR_BGR2HSV);
@@ -46,13 +46,13 @@ Mat Identify::image2Hist(const cv::Mat& src)
return src_hist;
}
double Identify::imageHistComp(const cv::Mat& src, const cv::MatND& hist)
double Identify::image_hist_comp(const cv::Mat& src, const cv::MatND& hist)
{
// keep the interface return value unchanged
return 1 - compareHist(image2Hist(src), hist, CV_COMP_BHATTACHARYYA);
return 1 - compareHist(image2hist(src), hist, CV_COMP_BHATTACHARYYA);
}
std::pair<double, cv::Point> Identify::findImage(const cv::Mat& image, const cv::Mat& templ)
std::pair<double, cv::Point> Identify::find_image(const cv::Mat& image, const cv::Mat& templ)
{
Mat image_hsv;
Mat templ_hsv;
@@ -68,7 +68,7 @@ std::pair<double, cv::Point> Identify::findImage(const cv::Mat& image, const cv:
return { maxVal, maxLoc };
}
std::tuple<AlgorithmType, double, asst::Rect> Identify::findImage(const Mat& cur, const std::string& templ, double threshold)
std::tuple<AlgorithmType, double, asst::Rect> Identify::find_image(const Mat& cur, const std::string& templ, double threshold)
{
if (m_mat_map.find(templ) == m_mat_map.cend()) {
return { AlgorithmType::JustReturn, 0, asst::Rect() };
@@ -76,19 +76,19 @@ std::tuple<AlgorithmType, double, asst::Rect> Identify::findImage(const Mat& cur
if (m_use_cache && m_cache_map.find(templ) != m_cache_map.cend()) {
auto&& [rect, hist] = m_cache_map.at(templ);
double value = imageHistComp(cur(rect), hist);
return { AlgorithmType::CompareHist, value, cvRect2Rect(rect).center_zoom(0.8) };
double value = image_hist_comp(cur(rect), hist);
return { AlgorithmType::CompareHist, value, cvrect2rect(rect).center_zoom(0.8) };
}
else {
auto&& templ_mat = m_mat_map.at(templ);
auto&& [value, point] = findImage(cur, templ_mat);
auto&& [value, point] = find_image(cur, templ_mat);
cv::Rect raw_rect(point.x, point.y, templ_mat.cols, templ_mat.rows);
if (m_use_cache && value >= threshold) {
m_cache_map.emplace(templ, std::make_pair(raw_rect, image2Hist(cur(raw_rect))));
m_cache_map.emplace(templ, std::make_pair(raw_rect, image2hist(cur(raw_rect))));
}
return { AlgorithmType::MatchTemplate, value, cvRect2Rect(raw_rect).center_zoom(0.8) };
return { AlgorithmType::MatchTemplate, value, cvrect2rect(raw_rect).center_zoom(0.8) };
}
}

View File

@@ -13,7 +13,7 @@ using namespace asst;
const std::string Updater::GithubReleaseLastestApiUrl = "https://api.github.com/repos/MistEO/MeoAssistance/releases/latest";
const std::string Updater::GithubReleaseApiUrl = "https://api.github.com/repos/MistEO/MeoAssistance/releases/latest";
Updater& Updater::instance()
Updater& Updater::get_instance()
{
static Updater unique_instance;
return unique_instance;

View File

@@ -13,8 +13,8 @@
using namespace asst;
WinMacro::WinMacro(const SimulatorInfo& info, HandleType type)
: m_simulator_info(info),
WinMacro::WinMacro(const EmulatorInfo& info, HandleType type)
: m_emulator_info(info),
m_handle_type(type),
m_rand_engine(std::chrono::system_clock::now().time_since_epoch().count())
{
@@ -31,18 +31,18 @@ bool WinMacro::findHandle()
std::vector<HandleInfo> handle_vec;
switch (m_handle_type) {
case HandleType::Window:
m_width = m_simulator_info.width;
m_height = m_simulator_info.height;
handle_vec = m_simulator_info.window;
m_width = m_emulator_info.width;
m_height = m_emulator_info.height;
handle_vec = m_emulator_info.window;
break;
case HandleType::View:
handle_vec = m_simulator_info.view;
handle_vec = m_emulator_info.view;
break;
case HandleType::Control:
m_is_adb = m_simulator_info.is_adb;
m_x_offset = m_simulator_info.x_offset;
m_y_offset = m_simulator_info.y_offset;
handle_vec = m_simulator_info.control;
m_is_adb = m_emulator_info.is_adb;
m_x_offset = m_emulator_info.x_offset;
m_y_offset = m_emulator_info.y_offset;
handle_vec = m_emulator_info.control;
break;
default:
DebugTraceError("Handle type error!", m_handle_type);
@@ -53,17 +53,17 @@ bool WinMacro::findHandle()
for (auto&& handle_info : handle_vec)
{
wchar_t* class_wbuff = NULL;
if (!handle_info.className.empty()) {
size_t class_len = (handle_info.className.size() + 1) * 2;
if (!handle_info.class_name.empty()) {
size_t class_len = (handle_info.class_name.size() + 1) * 2;
class_wbuff = new wchar_t[class_len];
::MultiByteToWideChar(CP_UTF8, 0, handle_info.className.c_str(), -1, class_wbuff, class_len);
::MultiByteToWideChar(CP_UTF8, 0, handle_info.class_name.c_str(), -1, class_wbuff, class_len);
}
wchar_t* window_wbuff = NULL;
if (!handle_info.windowName.empty()) {
size_t window_len = (handle_info.windowName.size() + 1) * 2;
if (!handle_info.window_name.empty()) {
size_t window_len = (handle_info.window_name.size() + 1) * 2;
window_wbuff = new wchar_t[window_len];
memset(window_wbuff, 0, window_len);
::MultiByteToWideChar(CP_UTF8, 0, handle_info.windowName.c_str(), -1, window_wbuff, window_len);
::MultiByteToWideChar(CP_UTF8, 0, handle_info.window_name.c_str(), -1, window_wbuff, window_len);
}
m_handle = ::FindWindowExW(m_handle, NULL, class_wbuff, window_wbuff);
@@ -93,14 +93,14 @@ bool WinMacro::findHandle()
}
size_t pos = adb_dir.find_last_of('\\') + 1;
adb_dir = adb_dir.substr(0, pos);
adb_dir = '"' + StringReplaceAll(m_simulator_info.adb.path, "[EmulatorPath]", adb_dir) + '"';
std::string connect_cmd = adb_dir + m_simulator_info.adb.connect;
adb_dir = '"' + StringReplaceAll(m_emulator_info.adb.path, "[EmulatorPath]", adb_dir) + '"';
std::string connect_cmd = adb_dir + m_emulator_info.adb.connect;
int ret = system(connect_cmd.c_str());
DebugTrace("Call", connect_cmd, "—— ret", ret);
m_click_cmd = adb_dir + m_simulator_info.adb.click;
m_click_cmd = adb_dir + m_emulator_info.adb.click;
}
DebugTrace("Handle:", m_handle, "Name:", m_simulator_info.name, "Type:", m_handle_type);
DebugTrace("Handle:", m_handle, "Name:", m_emulator_info.name, "Type:", m_handle_type);
if (m_handle != NULL) {
return true;
@@ -208,7 +208,7 @@ bool WinMacro::click(const Point& p)
}
}
bool WinMacro::clickRange(const Rect& rect)
bool WinMacro::click(const Rect& rect)
{
if (m_handle_type != HandleType::Control || !::IsWindow(m_handle)) {
return false;

View File

@@ -25,7 +25,7 @@ namespace MeoAsstGui
[DllImport("MeoAssistance.dll")] static private extern IntPtr CreateAsst();
[DllImport("MeoAssistance.dll")] static private extern void DestoryAsst(IntPtr ptr);
[DllImport("MeoAssistance.dll")] static private extern bool AsstCatchSimulator(IntPtr ptr);
[DllImport("MeoAssistance.dll")] static private extern bool AsstCatchEmulator(IntPtr ptr);
[DllImport("MeoAssistance.dll")] static private extern void AsstStart(IntPtr ptr, string task);
[DllImport("MeoAssistance.dll")] static private extern void AsstStop(IntPtr ptr);
[DllImport("MeoAssistance.dll")] static private extern bool AsstSetParam(IntPtr p_asst, string type, string param, string value);
@@ -60,7 +60,7 @@ namespace MeoAsstGui
}
private void button_Click_startSanity(object sender, RoutedEventArgs e)
{
bool catched = AsstCatchSimulator(p_asst);
bool catched = AsstCatchEmulator(p_asst);
catch_status.Content = "捕获模拟器窗口:" + catched;
AsstStart(p_asst, "SanityBegin");
update_times.Start();
@@ -113,7 +113,7 @@ namespace MeoAsstGui
private void button_Click_visit(object sender, RoutedEventArgs e)
{
bool catched = AsstCatchSimulator(p_asst);
bool catched = AsstCatchEmulator(p_asst);
catch_status.Content = "捕获模拟器窗口:" + catched;
AsstStart(p_asst, "VisitBegin");
}

View File

@@ -6,23 +6,23 @@ int main(int argc, char** argv)
{
using namespace asst;
bool up = Updater::instance().has_new_version();
bool up = Updater::get_instance().has_new_version();
if (up) {
auto && info = Updater::instance().get_version_info();
auto && info = Updater::get_instance().get_version_info();
std::cout << "ÓÐа汾£º" << info.tag_name << std::endl;
std::cout << "µØÖ·£º" << info.html_url << std::endl;
}
Assistance asst;
auto ret = asst.setSimulator();
auto ret = asst.set_emulator();
if (!ret) {
DebugTraceError("Can't Find Simulator or Permission denied.");
DebugTraceError("Can't Find Emulator or Permission denied.");
getchar();
return -1;
}
else {
DebugTraceInfo("Find Simulator:", ret.value());
DebugTraceInfo("Find Emulator:", ret.value());
}
DebugTraceInfo("Start");