mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-20 02:55:38 +08:00
完成原有接口的重构
This commit is contained in:
@@ -27,7 +27,7 @@ namespace asst {
|
||||
class Assistance
|
||||
{
|
||||
public:
|
||||
Assistance();
|
||||
Assistance(TaskCallback callback = nullptr, void * callback_arg = nullptr);
|
||||
~Assistance();
|
||||
|
||||
std::optional<std::string> catch_emulator(const std::string& emulator_name = std::string());
|
||||
@@ -46,20 +46,13 @@ namespace asst {
|
||||
bool set_param(const std::string& type, const std::string& param, const std::string& value);
|
||||
std::optional<std::string> get_param(const std::string& type, const std::string& param);
|
||||
|
||||
bool print_window(const std::string& filename);
|
||||
|
||||
private:
|
||||
static void working_proc(Assistance* pThis);
|
||||
static void task_callback(TaskMsg msg, const json::value& detail, void* custom_arg);
|
||||
|
||||
void append_match_task(const std::vector<std::string>& tasks);
|
||||
cv::Mat get_format_image();
|
||||
void set_control_scale(int cur_width, int cur_height);
|
||||
void clear_exec_times();
|
||||
|
||||
// for debug
|
||||
bool find_text_and_click(const std::string& text, bool block = true);
|
||||
|
||||
std::shared_ptr<WinMacro> m_window_ptr = nullptr;
|
||||
std::shared_ptr<WinMacro> m_view_ptr = nullptr;
|
||||
std::shared_ptr<WinMacro> m_control_ptr = nullptr;
|
||||
@@ -72,6 +65,9 @@ namespace asst {
|
||||
bool m_thread_exit = false;
|
||||
bool m_thread_idle = true;
|
||||
std::queue<std::shared_ptr<AbstractTask>> m_tasks_queue;
|
||||
|
||||
TaskCallback m_callback = nullptr;
|
||||
void* m_callback_arg = nullptr;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -46,6 +46,7 @@ namespace asst {
|
||||
EndOfSleep,
|
||||
AppendMatchTask,
|
||||
TaskCompleted,
|
||||
PrintWindow,
|
||||
MissionStop,
|
||||
/* Open Recruit Msg */
|
||||
TextDetected,
|
||||
@@ -67,6 +68,7 @@ namespace asst {
|
||||
{TaskMsg::EndOfSleep, "EndOfSleep"},
|
||||
{TaskMsg::AppendMatchTask, "AppendMatchTask"},
|
||||
{TaskMsg::TaskCompleted, "TaskCompleted"},
|
||||
{TaskMsg::PrintWindow, "PrintWindow"},
|
||||
{TaskMsg::MissionStop, "MissionStop"},
|
||||
{TaskMsg::TextDetected, "TextDetected"},
|
||||
{TaskMsg::RecruitTagsDetected, "RecruitTagsDetected"},
|
||||
@@ -108,6 +110,7 @@ namespace asst {
|
||||
virtual cv::Mat get_format_image();
|
||||
virtual bool set_control_scale(int cur_width, int cur_height);
|
||||
virtual void sleep(unsigned millisecond);
|
||||
virtual bool print_window(const std::string& dir);
|
||||
|
||||
std::shared_ptr<WinMacro> m_window_ptr = nullptr;
|
||||
std::shared_ptr<WinMacro> m_view_ptr = nullptr;
|
||||
|
||||
@@ -15,7 +15,8 @@
|
||||
|
||||
using namespace asst;
|
||||
|
||||
Assistance::Assistance()
|
||||
Assistance::Assistance(TaskCallback callback, void* callback_arg)
|
||||
: m_callback(callback), m_callback_arg(callback_arg)
|
||||
{
|
||||
DebugTraceFunction;
|
||||
|
||||
@@ -182,46 +183,6 @@ std::optional<std::string> Assistance::get_param(const std::string& type, const
|
||||
return Configer::get_instance().get_param(type, param);
|
||||
}
|
||||
|
||||
bool asst::Assistance::print_window(const std::string& filename)
|
||||
{
|
||||
DebugTraceFunction;
|
||||
|
||||
const cv::Mat& image = get_format_image();
|
||||
// 保存的截图额外再裁剪掉一圈,不然企鹅物流识别不出来
|
||||
int offset = Configer::get_instance().m_options.print_window_crop_offset;
|
||||
cv::Rect rect(offset, offset, image.cols - offset * 2, image.rows - offset * 2);
|
||||
bool ret = cv::imwrite(filename.c_str(), image(rect));
|
||||
|
||||
if (ret) {
|
||||
DebugTraceInfo("PrintWindow to", filename);
|
||||
}
|
||||
else {
|
||||
DebugTraceError("PrintWindow error", filename);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool asst::Assistance::find_text_and_click(const std::string& text, bool block)
|
||||
{
|
||||
DebugTraceFunction;
|
||||
DebugTrace("find_text_and_click |", Utf8ToGbk(text), block ? "block" : "non block");
|
||||
|
||||
std::unique_lock<std::mutex> lock;
|
||||
if (block) { // 外部调用
|
||||
lock = std::unique_lock<std::mutex>(m_mutex);
|
||||
}
|
||||
const cv::Mat& image = get_format_image();
|
||||
std::optional<Rect>&& result = m_identify_ptr->find_text(image, text);
|
||||
|
||||
if (!result) {
|
||||
DebugTrace("Cannot found", Utf8ToGbk(text));
|
||||
return false;
|
||||
}
|
||||
|
||||
set_control_scale(image.cols, image.rows);
|
||||
return m_control_ptr->click(result.value());
|
||||
}
|
||||
|
||||
void Assistance::working_proc(Assistance* pThis)
|
||||
{
|
||||
DebugTraceFunction;
|
||||
@@ -273,7 +234,7 @@ void Assistance::working_proc(Assistance* pThis)
|
||||
void Assistance::task_callback(TaskMsg msg, const json::value& detail, void* custom_arg)
|
||||
{
|
||||
DebugTraceFunction;
|
||||
DebugTrace(msg, detail.to_string(), custom_arg);
|
||||
DebugTrace(msg, detail.to_string());
|
||||
|
||||
Assistance* p_this = (Assistance*)custom_arg;
|
||||
switch (msg)
|
||||
@@ -315,6 +276,10 @@ void Assistance::task_callback(TaskMsg msg, const json::value& detail, void* cus
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (p_this->m_callback) {
|
||||
p_this->m_callback(msg, detail, m_callback_arg);
|
||||
}
|
||||
}
|
||||
|
||||
void asst::Assistance::append_match_task(const std::vector<std::string>& tasks)
|
||||
@@ -324,41 +289,6 @@ void asst::Assistance::append_match_task(const std::vector<std::string>& tasks)
|
||||
m_tasks_queue.emplace(task_ptr);
|
||||
}
|
||||
|
||||
cv::Mat Assistance::get_format_image()
|
||||
{
|
||||
const cv::Mat& raw_image = m_view_ptr->getImage(m_view_ptr->getWindowRect());
|
||||
if (raw_image.empty() || raw_image.rows < 100) {
|
||||
DebugTraceError("Window image error");
|
||||
return raw_image;
|
||||
}
|
||||
// 把模拟器边框的一圈裁剪掉
|
||||
const EmulatorInfo& window_info = m_view_ptr->getEmulatorInfo();
|
||||
int x_offset = window_info.x_offset;
|
||||
int y_offset = window_info.y_offset;
|
||||
int width = raw_image.cols - x_offset - window_info.right_offset;
|
||||
int height = raw_image.rows - y_offset - window_info.bottom_offset;
|
||||
|
||||
cv::Mat cropped(raw_image, cv::Rect(x_offset, y_offset, width, height));
|
||||
|
||||
//// 调整尺寸,与资源中截图的标准尺寸一致
|
||||
//cv::Mat dst;
|
||||
//cv::resize(cropped, dst, cv::Size(Configer::get_instance().DefaultWindowWidth, Configer::get_instance().DefaultWindowHeight));
|
||||
|
||||
return cropped;
|
||||
}
|
||||
|
||||
void asst::Assistance::set_control_scale(int cur_width, int cur_height)
|
||||
{
|
||||
double scale_width = static_cast<double>(cur_width) / Configer::get_instance().DefaultWindowWidth;
|
||||
double scale_height = static_cast<double>(cur_height) / Configer::get_instance().DefaultWindowHeight;
|
||||
// 有些模拟器有可收缩的侧边,会增加宽度。
|
||||
// config.json中设置的是侧边展开后的offset
|
||||
// 如果用户把侧边收起来了,则有侧边的那头会额外裁剪掉一些,长度偏小
|
||||
// 所以按这里面长、宽里大的那个算,大的那边没侧边
|
||||
double scale = std::max(scale_width, scale_height);
|
||||
m_control_ptr->setControlScale(scale);
|
||||
}
|
||||
|
||||
void Assistance::clear_exec_times()
|
||||
{
|
||||
for (auto&& pair : Configer::get_instance().m_all_tasks_info) {
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <utility>
|
||||
#include <cmath>
|
||||
#include <mutex>
|
||||
#include <filesystem>
|
||||
|
||||
using namespace asst;
|
||||
|
||||
@@ -97,6 +98,31 @@ void AbstractTask::sleep(unsigned millisecond)
|
||||
m_callback(TaskMsg::EndOfSleep, callback_json, m_callback_arg);
|
||||
}
|
||||
|
||||
bool AbstractTask::print_window(const std::string& dir)
|
||||
{
|
||||
const cv::Mat& image = get_format_image();
|
||||
if (image.empty()) {
|
||||
return false;
|
||||
}
|
||||
// 保存的截图额外再裁剪掉一圈,不然企鹅物流识别不出来
|
||||
int offset = Configer::get_instance().m_options.print_window_crop_offset;
|
||||
cv::Rect rect(offset, offset, image.cols - offset * 2, image.rows - offset * 2);
|
||||
|
||||
std::filesystem::create_directory(dir);
|
||||
const std::string time_str = StringReplaceAll(StringReplaceAll(GetFormatTimeString(), " ", "_"), ":", "-");
|
||||
const std::string filename = dir + time_str + ".png";
|
||||
|
||||
bool ret = cv::imwrite(filename.c_str(), image(rect));
|
||||
|
||||
json::value callback_json;
|
||||
callback_json["filename"] = filename;
|
||||
callback_json["ret"] = ret;
|
||||
callback_json["offset"] = offset;
|
||||
m_callback(TaskMsg::PrintWindow, callback_json, m_callback_arg);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
MatchTask::MatchTask(TaskCallback callback, void* callback_arg)
|
||||
: AbstractTask(callback, callback_arg)
|
||||
{
|
||||
@@ -115,7 +141,7 @@ bool MatchTask::run()
|
||||
}
|
||||
|
||||
Rect rect;
|
||||
auto && ret = match_image(&rect);
|
||||
auto&& ret = match_image(&rect);
|
||||
if (!ret) {
|
||||
return false;
|
||||
}
|
||||
@@ -154,6 +180,13 @@ bool MatchTask::run()
|
||||
case MatchTaskType::Stop:
|
||||
m_callback(TaskMsg::MissionStop, json::value(), m_callback_arg);
|
||||
break;
|
||||
case MatchTaskType::PrintWindow:
|
||||
{
|
||||
sleep(Configer::get_instance().m_options.print_window_delay);
|
||||
static const std::string dirname = GetCurrentDir() + "screenshot\\";
|
||||
print_window(dirname);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -181,7 +214,7 @@ bool MatchTask::run()
|
||||
}
|
||||
|
||||
|
||||
std::optional<std::string> MatchTask::match_image(asst::Rect* matched_rect)
|
||||
std::optional<std::string> MatchTask::match_image(Rect* matched_rect)
|
||||
{
|
||||
const cv::Mat& cur_image = get_format_image();
|
||||
if (cur_image.empty() || cur_image.rows < 100) {
|
||||
@@ -191,7 +224,7 @@ std::optional<std::string> MatchTask::match_image(asst::Rect* matched_rect)
|
||||
|
||||
// 逐个匹配当前可能的图像
|
||||
for (const std::string& task_name : m_cur_tasks_name) {
|
||||
TaskInfo & task_info = Configer::get_instance().m_all_tasks_info[task_name];
|
||||
TaskInfo& task_info = Configer::get_instance().m_all_tasks_info[task_name];
|
||||
double templ_threshold = task_info.templ_threshold;
|
||||
double hist_threshold = task_info.hist_threshold;
|
||||
|
||||
|
||||
@@ -239,9 +239,7 @@
|
||||
"StartButton2",
|
||||
"PRTS",
|
||||
"UseMedicine",
|
||||
"UseStone",
|
||||
"PrtsErrorConfirm",
|
||||
"Random"
|
||||
"UseStone"
|
||||
]
|
||||
},
|
||||
"UsePrts": {
|
||||
|
||||
Reference in New Issue
Block a user