From 1fdb131f7b1deeb059dd58fd8f982e44f58f1c3e Mon Sep 17 00:00:00 2001 From: MistEO Date: Fri, 4 Mar 2022 23:51:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor.=E8=AF=BB=E5=8F=96=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=A7=A3=E8=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/AsstCaller.h | 5 +- src/MeoAssistant/AbstractConfiger.cpp | 7 +- src/MeoAssistant/Assistant.cpp | 23 +- src/MeoAssistant/Assistant.h | 3 +- src/MeoAssistant/AsstCaller.cpp | 15 +- src/MeoAssistant/GeneralConfiger.cpp | 3 + src/MeoAssistant/InfrastConfiger.cpp | 4 + src/MeoAssistant/ItemConfiger.cpp | 6 +- src/MeoAssistant/Logger.hpp | 10 +- src/MeoAssistant/OcrPack.cpp | 2 + src/MeoAssistant/PenguinPack.cpp | 3 + src/MeoAssistant/RecruitConfiger.cpp | 6 +- src/MeoAssistant/Resource.cpp | 3 + src/MeoAssistant/TaskData.cpp | 365 +++++++++++++------------ src/MeoAssistant/TemplResource.cpp | 6 + src/Python/interface.py | 141 +++++----- src/Python/sample.py | 6 +- src/Python/timed_start/timed_sample.py | 7 +- tools/TestCaller/main.cpp | 4 +- 19 files changed, 334 insertions(+), 285 deletions(-) diff --git a/include/AsstCaller.h b/include/AsstCaller.h index cb5b7a22d0..8343f1d3a1 100644 --- a/include/AsstCaller.h +++ b/include/AsstCaller.h @@ -12,8 +12,9 @@ extern "C" { #endif typedef void(ASST_CALL* AsstCallback)(int msg, const char* detail_json, void* custom_arg); - ASSTAPI_PORT asst::Assistant* ASST_CALL AsstCreate(const char* dirname); - ASSTAPI_PORT asst::Assistant* ASST_CALL AsstCreateEx(const char* dirname, AsstCallback callback, void* custom_arg); + bool ASSTAPI AsstLoadResource(const char* path); + ASSTAPI_PORT asst::Assistant* ASST_CALL AsstCreate(); + ASSTAPI_PORT asst::Assistant* ASST_CALL AsstCreateEx(AsstCallback callback, void* custom_arg); void ASSTAPI AsstDestroy(asst::Assistant* p_asst); bool ASSTAPI AsstConnect(asst::Assistant* p_asst, const char* adb_path, const char* address, const char* config); diff --git a/src/MeoAssistant/AbstractConfiger.cpp b/src/MeoAssistant/AbstractConfiger.cpp index fa97794d60..bdb608b872 100644 --- a/src/MeoAssistant/AbstractConfiger.cpp +++ b/src/MeoAssistant/AbstractConfiger.cpp @@ -3,9 +3,12 @@ #include #include "AsstUtils.hpp" +#include "Logger.hpp" bool asst::AbstractConfiger::load(const std::string& filename) { + LogTraceFunction; + std::string content = utils::load_file_without_bom(filename); auto&& ret = json::parser::parse(content); @@ -14,7 +17,7 @@ bool asst::AbstractConfiger::load(const std::string& filename) return false; } - json::value root = std::move(ret.value()); + const auto& root = ret.value(); try { return parse(root); @@ -24,4 +27,4 @@ bool asst::AbstractConfiger::load(const std::string& filename) return false; } return true; -} \ No newline at end of file +} diff --git a/src/MeoAssistant/Assistant.cpp b/src/MeoAssistant/Assistant.cpp index 5449d0bd81..ec2b393dbd 100644 --- a/src/MeoAssistant/Assistant.cpp +++ b/src/MeoAssistant/Assistant.cpp @@ -30,31 +30,12 @@ using namespace asst; -Assistant::Assistant(std::string dirname, AsstCallback callback, void* callback_arg) - : m_dirname(std::move(dirname) + "/"), - m_callback(callback), +Assistant::Assistant(AsstCallback callback, void* callback_arg) + : m_callback(callback), m_callback_arg(callback_arg) { - Logger::set_dirname(m_dirname); - LogTraceFunction; - bool resource_ret = Resrc.load(m_dirname + "resource/"); - if (!resource_ret) { - const std::string& error = Resrc.get_last_error(); - Log.error("resource broken:", error); - if (m_callback == nullptr) { - throw error; - } - json::value callback_json; - callback_json["what"] = "resource broken"; - callback_json["details"] = json::object{ - { "error", error } - }; - m_callback(AsstMsg::InitFailed, callback_json, m_callback_arg); - throw error; - } - m_ctrler = std::make_shared(task_callback, (void*)this); m_working_thread = std::thread(std::bind(&Assistant::working_proc, this)); diff --git a/src/MeoAssistant/Assistant.h b/src/MeoAssistant/Assistant.h index 83c457d8c1..d047f39bdb 100644 --- a/src/MeoAssistant/Assistant.h +++ b/src/MeoAssistant/Assistant.h @@ -29,7 +29,7 @@ namespace asst class Assistant { public: - Assistant(std::string dirname, AsstCallback callback = nullptr, void* callback_arg = nullptr); + Assistant(AsstCallback callback = nullptr, void* callback_arg = nullptr); ~Assistant(); // 连接adb @@ -98,7 +98,6 @@ namespace asst void append_callback(AsstMsg msg, json::value detail); void clear_cache(); - std::string m_dirname; bool m_inited = false; std::string m_uuid; diff --git a/src/MeoAssistant/AsstCaller.cpp b/src/MeoAssistant/AsstCaller.cpp index fdb49ff277..24d4beebdb 100644 --- a/src/MeoAssistant/AsstCaller.cpp +++ b/src/MeoAssistant/AsstCaller.cpp @@ -9,6 +9,7 @@ #include "AsstDef.h" #include "Version.h" #include "Logger.hpp" +#include "Resource.h" #if 0 #if _MSC_VER @@ -41,7 +42,13 @@ void CallbackTrans(asst::AsstMsg msg, const json::value& json, void* custom_arg) _callback(static_cast(msg), json.to_string().c_str(), custom_arg); } -asst::Assistant* AsstCreate(const char* dirname) +bool AsstLoadResource(const char* path) +{ + return asst::Logger::set_dirname(std::string(path) + "/") + && asst::Resrc.load(std::string(path) + "/resource/"); +} + +asst::Assistant* AsstCreate() { if (_callback != nullptr) { std::cerr << "An instance of Asst already exists" << std::endl; @@ -49,7 +56,7 @@ asst::Assistant* AsstCreate(const char* dirname) } try { _callback = nullptr; - return new asst::Assistant(dirname); + return new asst::Assistant(); } catch (std::exception& e) { std::cerr << "create failed: " << e.what() << std::endl; @@ -60,7 +67,7 @@ asst::Assistant* AsstCreate(const char* dirname) return nullptr; } -asst::Assistant* AsstCreateEx(const char* dirname, AsstCallback callback, void* custom_arg) +asst::Assistant* AsstCreateEx(AsstCallback callback, void* custom_arg) { if (_callback != nullptr) { std::cerr << "An instance of Asst already exists" << std::endl; @@ -68,7 +75,7 @@ asst::Assistant* AsstCreateEx(const char* dirname, AsstCallback callback, void* } try { _callback = callback; - return new asst::Assistant(dirname, CallbackTrans, custom_arg); + return new asst::Assistant(CallbackTrans, custom_arg); } catch (std::exception& e) { std::cerr << "create failed: " << e.what() << std::endl; diff --git a/src/MeoAssistant/GeneralConfiger.cpp b/src/MeoAssistant/GeneralConfiger.cpp index b27b0e4c1a..b956fa2214 100644 --- a/src/MeoAssistant/GeneralConfiger.cpp +++ b/src/MeoAssistant/GeneralConfiger.cpp @@ -1,9 +1,12 @@ #include "GeneralConfiger.h" #include +#include "Logger.hpp" bool asst::GeneralConfiger::parse(const json::value& json) { + LogTraceFunction; + m_version = json.at("version").as_string(); { diff --git a/src/MeoAssistant/InfrastConfiger.cpp b/src/MeoAssistant/InfrastConfiger.cpp index 17e2f278aa..babb495a17 100644 --- a/src/MeoAssistant/InfrastConfiger.cpp +++ b/src/MeoAssistant/InfrastConfiger.cpp @@ -2,8 +2,12 @@ #include +#include "Logger.hpp" + bool asst::InfrastConfiger::parse(const json::value& json) { + LogTraceFunction; + for (const json::value& facility : json.at("facility").as_array()) { std::string facility_name = facility.as_string(); json::value facility_json = json.at(facility_name); diff --git a/src/MeoAssistant/ItemConfiger.cpp b/src/MeoAssistant/ItemConfiger.cpp index 32c8212a60..3f6d631b93 100644 --- a/src/MeoAssistant/ItemConfiger.cpp +++ b/src/MeoAssistant/ItemConfiger.cpp @@ -2,11 +2,15 @@ #include +#include "Logger.hpp" + bool asst::ItemConfiger::parse(const json::value& json) { + LogTraceFunction; + for (const auto& [id, item_json] : json.as_object()) { std::string name = item_json.at("name_i18n").at("zh").as_string(); m_item_name.emplace(id, std::move(name)); } return true; -} \ No newline at end of file +} diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index bd92be8360..a034038fa1 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -29,9 +29,13 @@ namespace asst return _unique_instance; } - static void set_dirname(std::string dirname) noexcept + static bool set_dirname(std::string dirname) noexcept { + if (!std::filesystem::exists(dirname) || !std::filesystem::is_directory(dirname)) { + return false; + } m_dirname = std::move(dirname); + return true; } template @@ -145,7 +149,7 @@ namespace asst #ifdef ASST_DEBUG stream_args(std::cout, buff, std::forward(args)...); #endif - } + } template inline void stream_args(std::ostream& os, T&& first, Args&&... rest) @@ -177,7 +181,7 @@ namespace asst #else os << first << " "; // Don't fucking use gbk in linux #endif - } + } }; inline static std::string m_dirname; diff --git a/src/MeoAssistant/OcrPack.cpp b/src/MeoAssistant/OcrPack.cpp index a0748216a1..85f7542210 100644 --- a/src/MeoAssistant/OcrPack.cpp +++ b/src/MeoAssistant/OcrPack.cpp @@ -13,6 +13,8 @@ asst::OcrPack::~OcrPack() bool asst::OcrPack::load(const std::string& dir) { + LogTraceFunction; + constexpr static const char* DetName = "/det"; //constexpr static const char* ClsName = "/cls"; constexpr static const char* RecName = "/rec"; diff --git a/src/MeoAssistant/PenguinPack.cpp b/src/MeoAssistant/PenguinPack.cpp index 16786c1a4b..21a54b5c26 100644 --- a/src/MeoAssistant/PenguinPack.cpp +++ b/src/MeoAssistant/PenguinPack.cpp @@ -10,9 +10,12 @@ namespace penguin } #include "AsstUtils.hpp" +#include "Logger.hpp" bool asst::PenguinPack::load(const std::string& dir) { + LogTraceFunction; + bool ret = load_json(dir + "/json/stages.json", dir + "/json/hash_index.json"); for (const auto& file : std::filesystem::directory_iterator(dir + "/items")) { diff --git a/src/MeoAssistant/RecruitConfiger.cpp b/src/MeoAssistant/RecruitConfiger.cpp index 9ff666b6c3..641653876d 100644 --- a/src/MeoAssistant/RecruitConfiger.cpp +++ b/src/MeoAssistant/RecruitConfiger.cpp @@ -4,8 +4,12 @@ #include +#include "Logger.hpp" + bool asst::RecruitConfiger::parse(const json::value& json) { + LogTraceFunction; + for (const json::value& oper : json.as_array()) { RecruitOperInfo oper_temp; oper_temp.name = oper.at("name").as_string(); @@ -34,4 +38,4 @@ bool asst::RecruitConfiger::parse(const json::value& json) }); return true; -} \ No newline at end of file +} diff --git a/src/MeoAssistant/Resource.cpp b/src/MeoAssistant/Resource.cpp index 4ed2ee8c3a..56aeae9ec0 100644 --- a/src/MeoAssistant/Resource.cpp +++ b/src/MeoAssistant/Resource.cpp @@ -7,9 +7,12 @@ #include #include "AsstDef.h" +#include "Logger.hpp" bool asst::Resource::load(const std::string& dir) { + LogTraceFunction; + constexpr static const char* TemplsFilename = "template"; constexpr static const char* GeneralCfgFilename = "config.json"; constexpr static const char* TaskDataFilename = "tasks.json"; diff --git a/src/MeoAssistant/TaskData.cpp b/src/MeoAssistant/TaskData.cpp index 27ed1cd47a..9f4018076d 100644 --- a/src/MeoAssistant/TaskData.cpp +++ b/src/MeoAssistant/TaskData.cpp @@ -7,214 +7,217 @@ #include "AsstDef.h" #include "GeneralConfiger.h" #include "TemplResource.h" +#include "Logger.hpp" const std::shared_ptr asst::TaskData::get(const std::string& name) const noexcept { - if (auto iter = m_all_tasks_info.find(name); - iter != m_all_tasks_info.cend()) { - return iter->second; - } - else { - return nullptr; - } + if (auto iter = m_all_tasks_info.find(name); + iter != m_all_tasks_info.cend()) { + return iter->second; + } + else { + return nullptr; + } } const std::unordered_set& asst::TaskData::get_templ_required() const noexcept { - return m_templ_required; + return m_templ_required; } std::shared_ptr asst::TaskData::get(std::string name) { - return m_all_tasks_info[std::move(name)]; + return m_all_tasks_info[std::move(name)]; } void asst::TaskData::clear_cache() noexcept { - for (auto&& [name, ptr] : m_all_tasks_info) { - ptr->region_of_appeared = Rect(); - } + for (auto&& [name, ptr] : m_all_tasks_info) { + ptr->region_of_appeared = Rect(); + } } bool asst::TaskData::parse(const json::value& json) { - auto to_lower = [](char c) -> char { - return (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c; - }; - for (const auto& [name, task_json] : json.as_object()) { - std::string algorithm_str = task_json.get("algorithm", "matchtemplate"); - std::transform(algorithm_str.begin(), algorithm_str.end(), algorithm_str.begin(), to_lower); - AlgorithmType algorithm = AlgorithmType::Invalid; - if (algorithm_str == "matchtemplate") { - algorithm = AlgorithmType::MatchTemplate; - } - else if (algorithm_str == "justreturn") { - algorithm = AlgorithmType::JustReturn; - } - else if (algorithm_str == "ocrdetect") { - algorithm = AlgorithmType::OcrDetect; - } - else if (algorithm_str == "hash") { - algorithm = AlgorithmType::Hash; - } - else { - m_last_error = "algorithm error " + algorithm_str; - return false; - } + LogTraceFunction; - std::shared_ptr task_info_ptr = nullptr; - switch (algorithm) { - case AlgorithmType::JustReturn: - task_info_ptr = std::make_shared(); - break; - case AlgorithmType::MatchTemplate: { - auto match_task_info_ptr = std::make_shared(); - match_task_info_ptr->templ_name = task_json.get("template", name + ".png"); - m_templ_required.emplace(match_task_info_ptr->templ_name); + auto to_lower = [](char c) -> char { + return (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c; + }; + for (const auto& [name, task_json] : json.as_object()) { + std::string algorithm_str = task_json.get("algorithm", "matchtemplate"); + std::transform(algorithm_str.begin(), algorithm_str.end(), algorithm_str.begin(), to_lower); + AlgorithmType algorithm = AlgorithmType::Invalid; + if (algorithm_str == "matchtemplate") { + algorithm = AlgorithmType::MatchTemplate; + } + else if (algorithm_str == "justreturn") { + algorithm = AlgorithmType::JustReturn; + } + else if (algorithm_str == "ocrdetect") { + algorithm = AlgorithmType::OcrDetect; + } + else if (algorithm_str == "hash") { + algorithm = AlgorithmType::Hash; + } + else { + m_last_error = "algorithm error " + algorithm_str; + return false; + } - match_task_info_ptr->templ_threshold = task_json.get( - "templThreshold", TemplThresholdDefault); - match_task_info_ptr->special_threshold = task_json.get( - "specialThreshold", 0); - if (task_json.contains("maskRange")) { - match_task_info_ptr->mask_range = std::make_pair( - task_json.at("maskRange")[0].as_integer(), - task_json.at("maskRange")[1].as_integer()); - } + std::shared_ptr task_info_ptr = nullptr; + switch (algorithm) { + case AlgorithmType::JustReturn: + task_info_ptr = std::make_shared(); + break; + case AlgorithmType::MatchTemplate: { + auto match_task_info_ptr = std::make_shared(); + match_task_info_ptr->templ_name = task_json.get("template", name + ".png"); + m_templ_required.emplace(match_task_info_ptr->templ_name); - task_info_ptr = match_task_info_ptr; - } break; - case AlgorithmType::OcrDetect: { - auto ocr_task_info_ptr = std::make_shared(); - for (const json::value& text : task_json.at("text").as_array()) { - ocr_task_info_ptr->text.emplace_back(text.as_string()); - } - ocr_task_info_ptr->need_full_match = task_json.get("need_match", false); - if (task_json.contains("ocrReplace")) { - for (const json::value& rep : task_json.at("ocrReplace").as_array()) { - ocr_task_info_ptr->replace_map.emplace(rep.as_array()[0].as_string(), rep.as_array()[1].as_string()); - } - } - task_info_ptr = ocr_task_info_ptr; - } break; - case AlgorithmType::Hash: - { - auto hash_task_info_ptr = std::make_shared(); - for (const json::value& hash : task_json.at("hash").as_array()) { - hash_task_info_ptr->hashs.emplace_back(hash.as_string()); - } - hash_task_info_ptr->dist_threshold = task_json.get("threshold", 0); - if (task_json.contains("maskRange")) { - hash_task_info_ptr->mask_range = std::make_pair( - task_json.at("maskRange")[0].as_integer(), - task_json.at("maskRange")[1].as_integer()); - } - hash_task_info_ptr->bound = task_json.get("bound", true); + match_task_info_ptr->templ_threshold = task_json.get( + "templThreshold", TemplThresholdDefault); + match_task_info_ptr->special_threshold = task_json.get( + "specialThreshold", 0); + if (task_json.contains("maskRange")) { + match_task_info_ptr->mask_range = std::make_pair( + task_json.at("maskRange")[0].as_integer(), + task_json.at("maskRange")[1].as_integer()); + } - task_info_ptr = hash_task_info_ptr; - } break; - } - task_info_ptr->cache = task_json.get("cache", true); - task_info_ptr->algorithm = algorithm; - task_info_ptr->name = name; - std::string action = task_json.get("action", std::string()); - std::transform(action.begin(), action.end(), action.begin(), to_lower); - if (action == "clickself") { - task_info_ptr->action = ProcessTaskAction::ClickSelf; - } - else if (action == "clickrand") { - task_info_ptr->action = ProcessTaskAction::ClickRand; - } - else if (action == "donothing" || action.empty()) { - task_info_ptr->action = ProcessTaskAction::DoNothing; - } - else if (action == "stop") { - task_info_ptr->action = ProcessTaskAction::Stop; - } - else if (action == "clickrect") { - task_info_ptr->action = ProcessTaskAction::ClickRect; - const json::value& rect_json = task_json.at("specificRect"); - task_info_ptr->specific_rect = Rect( - rect_json[0].as_integer(), - rect_json[1].as_integer(), - rect_json[2].as_integer(), - rect_json[3].as_integer()); - } - else if (action == "swipetotheleft") { - task_info_ptr->action = ProcessTaskAction::SwipeToTheLeft; - } - else if (action == "swipetotheright") { - task_info_ptr->action = ProcessTaskAction::SwipeToTheRight; - } - else { - m_last_error = "Task: " + name + " error: " + action; - return false; - } + task_info_ptr = match_task_info_ptr; + } break; + case AlgorithmType::OcrDetect: { + auto ocr_task_info_ptr = std::make_shared(); + for (const json::value& text : task_json.at("text").as_array()) { + ocr_task_info_ptr->text.emplace_back(text.as_string()); + } + ocr_task_info_ptr->need_full_match = task_json.get("need_match", false); + if (task_json.contains("ocrReplace")) { + for (const json::value& rep : task_json.at("ocrReplace").as_array()) { + ocr_task_info_ptr->replace_map.emplace(rep.as_array()[0].as_string(), rep.as_array()[1].as_string()); + } + } + task_info_ptr = ocr_task_info_ptr; + } break; + case AlgorithmType::Hash: + { + auto hash_task_info_ptr = std::make_shared(); + for (const json::value& hash : task_json.at("hash").as_array()) { + hash_task_info_ptr->hashs.emplace_back(hash.as_string()); + } + hash_task_info_ptr->dist_threshold = task_json.get("threshold", 0); + if (task_json.contains("maskRange")) { + hash_task_info_ptr->mask_range = std::make_pair( + task_json.at("maskRange")[0].as_integer(), + task_json.at("maskRange")[1].as_integer()); + } + hash_task_info_ptr->bound = task_json.get("bound", true); - task_info_ptr->max_times = task_json.get("maxTimes", INT_MAX); - if (task_json.contains("exceededNext")) { - const json::array& excceed_next_arr = task_json.at("exceededNext").as_array(); - for (const json::value& excceed_next : excceed_next_arr) { - task_info_ptr->exceeded_next.emplace_back(excceed_next.as_string()); - } - } - else { - task_info_ptr->exceeded_next.emplace_back("Stop"); - } - task_info_ptr->pre_delay = task_json.get("preDelay", 0); - task_info_ptr->rear_delay = task_json.get("rearDelay", 0); - if (task_json.contains("reduceOtherTimes")) { - const json::array& reduce_arr = task_json.at("reduceOtherTimes").as_array(); - for (const json::value& reduce : reduce_arr) { - task_info_ptr->reduce_other_times.emplace_back(reduce.as_string()); - } - } - if (task_json.contains("roi")) { - const json::array& area_arr = task_json.at("roi").as_array(); - int x = area_arr[0].as_integer(); - int y = area_arr[1].as_integer(); - int width = area_arr[2].as_integer(); - int height = area_arr[3].as_integer(); + task_info_ptr = hash_task_info_ptr; + } break; + } + task_info_ptr->cache = task_json.get("cache", true); + task_info_ptr->algorithm = algorithm; + task_info_ptr->name = name; + std::string action = task_json.get("action", std::string()); + std::transform(action.begin(), action.end(), action.begin(), to_lower); + if (action == "clickself") { + task_info_ptr->action = ProcessTaskAction::ClickSelf; + } + else if (action == "clickrand") { + task_info_ptr->action = ProcessTaskAction::ClickRand; + } + else if (action == "donothing" || action.empty()) { + task_info_ptr->action = ProcessTaskAction::DoNothing; + } + else if (action == "stop") { + task_info_ptr->action = ProcessTaskAction::Stop; + } + else if (action == "clickrect") { + task_info_ptr->action = ProcessTaskAction::ClickRect; + const json::value& rect_json = task_json.at("specificRect"); + task_info_ptr->specific_rect = Rect( + rect_json[0].as_integer(), + rect_json[1].as_integer(), + rect_json[2].as_integer(), + rect_json[3].as_integer()); + } + else if (action == "swipetotheleft") { + task_info_ptr->action = ProcessTaskAction::SwipeToTheLeft; + } + else if (action == "swipetotheright") { + task_info_ptr->action = ProcessTaskAction::SwipeToTheRight; + } + else { + m_last_error = "Task: " + name + " error: " + action; + return false; + } + + task_info_ptr->max_times = task_json.get("maxTimes", INT_MAX); + if (task_json.contains("exceededNext")) { + const json::array& excceed_next_arr = task_json.at("exceededNext").as_array(); + for (const json::value& excceed_next : excceed_next_arr) { + task_info_ptr->exceeded_next.emplace_back(excceed_next.as_string()); + } + } + else { + task_info_ptr->exceeded_next.emplace_back("Stop"); + } + task_info_ptr->pre_delay = task_json.get("preDelay", 0); + task_info_ptr->rear_delay = task_json.get("rearDelay", 0); + if (task_json.contains("reduceOtherTimes")) { + const json::array& reduce_arr = task_json.at("reduceOtherTimes").as_array(); + for (const json::value& reduce : reduce_arr) { + task_info_ptr->reduce_other_times.emplace_back(reduce.as_string()); + } + } + if (task_json.contains("roi")) { + const json::array& area_arr = task_json.at("roi").as_array(); + int x = area_arr[0].as_integer(); + int y = area_arr[1].as_integer(); + int width = area_arr[2].as_integer(); + int height = area_arr[3].as_integer(); #ifdef ASST_DEBUG - if (x + width > WindowWidthDefault || y + height > WindowHeightDefault) { - m_last_error = name + " roi is out of bounds"; - return false; - } + if (x + width > WindowWidthDefault || y + height > WindowHeightDefault) { + m_last_error = name + " roi is out of bounds"; + return false; + } #endif - task_info_ptr->roi = Rect(x, y, width, height); - } - else { - task_info_ptr->roi = Rect(); - } + task_info_ptr->roi = Rect(x, y, width, height); + } + else { + task_info_ptr->roi = Rect(); + } - if (task_json.contains("next")) { - for (const json::value& next : task_json.at("next").as_array()) { - task_info_ptr->next.emplace_back(next.as_string()); - } - } - if (task_json.contains("rectMove")) { - const json::array& move_arr = task_json.at("rectMove").as_array(); - task_info_ptr->rect_move = Rect( - move_arr[0].as_integer(), - move_arr[1].as_integer(), - move_arr[2].as_integer(), - move_arr[3].as_integer()); - } - else { - task_info_ptr->rect_move = Rect(); - } + if (task_json.contains("next")) { + for (const json::value& next : task_json.at("next").as_array()) { + task_info_ptr->next.emplace_back(next.as_string()); + } + } + if (task_json.contains("rectMove")) { + const json::array& move_arr = task_json.at("rectMove").as_array(); + task_info_ptr->rect_move = Rect( + move_arr[0].as_integer(), + move_arr[1].as_integer(), + move_arr[2].as_integer(), + move_arr[3].as_integer()); + } + else { + task_info_ptr->rect_move = Rect(); + } - m_all_tasks_info.emplace(name, task_info_ptr); - } + m_all_tasks_info.emplace(name, task_info_ptr); + } #ifdef ASST_DEBUG - for (const auto& [name, task] : m_all_tasks_info) { - for (const auto& next : task->next) { - if (m_all_tasks_info.find(next) == m_all_tasks_info.cend()) { - m_last_error = name + "'s next " + next + " is null"; - return false; - } - } - } + for (const auto& [name, task] : m_all_tasks_info) { + for (const auto& next : task->next) { + if (m_all_tasks_info.find(next) == m_all_tasks_info.cend()) { + m_last_error = name + "'s next " + next + " is null"; + return false; + } + } + } #endif - return true; -} \ No newline at end of file + return true; +} diff --git a/src/MeoAssistant/TemplResource.cpp b/src/MeoAssistant/TemplResource.cpp index 3c22eaa893..6ee05cfb48 100644 --- a/src/MeoAssistant/TemplResource.cpp +++ b/src/MeoAssistant/TemplResource.cpp @@ -4,8 +4,12 @@ #include #include +#include "Logger.hpp" + void asst::TemplResource::append_load_required(std::unordered_set required) noexcept { + LogTraceFunction; + m_templs_filename.insert( std::make_move_iterator(required.begin()), std::make_move_iterator(required.end())); @@ -13,6 +17,8 @@ void asst::TemplResource::append_load_required(std::unordered_set r bool asst::TemplResource::load(const std::string& dir) { + LogTraceFunction; + for (const std::string& filename : m_templs_filename) { std::string filepath = dir + "/" + filename; if (std::filesystem::exists(filepath)) { diff --git a/src/Python/interface.py b/src/Python/interface.py index d3401a690b..6bae4f23e6 100644 --- a/src/Python/interface.py +++ b/src/Python/interface.py @@ -13,30 +13,39 @@ class Asst: :params: ``param1 message``: 消息类型 - ``param2 details``: gbk json string + ``param2 details``: json string ``param3 arg``: 自定义参数 """ - def __init__(self, dirname: str, callback: CallBackType, arg=None): + @staticmethod + def load(path: str) -> bool: + """ + 加载 dll 及资源 + + :params: + ``path``: DLL及资源所在文件夹路径 + """ + if platform.system().lower() == 'windows': + Asst.__libpath = pathlib.Path(path) / 'MeoAssistant.dll' + Asst.__lib = ctypes.WinDLL(str(Asst.__libpath)) + else: + Asst.__libpath = pathlib.Path(path) / 'libMeoAssistant.so' + Asst.__lib = ctypes.CDLL(str(Asst.__libpath)) + Asst.__set_lib_properties() + + return Asst.__lib.AsstLoadResource(path.encode('utf-8')) + + def __init__(self, callback: CallBackType, arg=None): """ :params: - ``dirname``: DLL及资源所在文件夹路径 ``callback``: 回调函数 ``arg``: 自定义参数 """ - if platform.system().lower() == 'windows': - self.__libpath = pathlib.Path(dirname) / 'MeoAssistant.dll' - self.__lib = ctypes.WinDLL(str(self.__libpath)) - else: - self.__libpath = pathlib.Path(dirname) / 'libMeoAssistant.so' - self.__lib = ctypes.CDLL(str(self.__libpath)) - self.__set_lib_properties() - self.__ptr = self.__lib.AsstCreateEx( - dirname.encode('utf-8'), callback, arg) + self.__ptr = Asst.__lib.AsstCreateEx(callback, arg) def __del__(self): - self.__lib.AsstDestroy(self.__ptr) + Asst.__lib.AsstDestroy(self.__ptr) def connect(self, adb_path: str, address: str, config: str = 'General'): """ @@ -49,14 +58,14 @@ class Asst: :return: 是否连接成功 """ - return self.__lib.AsstConnect(self.__ptr, + return Asst.__lib.AsstConnect(self.__ptr, adb_path.encode('utf-8'), address.encode('utf-8'), config.encode('utf-8')) def append_start_up(self) -> bool: """ 添加开始唤醒任务 """ - return self.__lib.AsstAppendStartUp(self.__ptr) + return Asst.__lib.AsstAppendStartUp(self.__ptr) def append_fight(self, stage: str, max_medicine: int, max_stone: int, max_times: int) -> bool: """ @@ -69,19 +78,19 @@ class Asst: ``max_stone``: 最多吃多少源石 ``max_times``: 最多刷多少 """ - return self.__lib.AsstAppendFight(self.__ptr, stage.encode('utf-8'), max_medicine, max_stone, max_times) + return Asst.__lib.AsstAppendFight(self.__ptr, stage.encode('utf-8'), max_medicine, max_stone, max_times) def append_award(self) -> bool: """ 添加领取每日奖励任务 """ - return self.__lib.AsstAppendAward(self.__ptr) + return Asst.__lib.AsstAppendAward(self.__ptr) def append_visit(self) -> bool: """ 添加访问好友任务 """ - return self.__lib.AsstAppendVisit(self.__ptr) + return Asst.__lib.AsstAppendVisit(self.__ptr) def append_mall(self, with_shopping: bool) -> bool: """ @@ -90,7 +99,7 @@ class Asst: :params: ``with_shopping``: 是否信用商店购物 """ - return self.__lib.AsstAppendMall(self.__ptr, with_shopping) + return Asst.__lib.AsstAppendMall(self.__ptr, with_shopping) def append_infrast(self, work_mode: int, order_list: list, uses_of_drones: str, dorm_threshold: float) -> bool: """ @@ -111,7 +120,7 @@ class Asst: order_arr = (ctypes.c_char_p * order_len)() order_arr[:] = order_byte_list - return self.__lib.AsstAppendInfrast(self.__ptr, work_mode, + return Asst.__lib.AsstAppendInfrast(self.__ptr, work_mode, order_arr, order_len, uses_of_drones.encode('utf-8'), dorm_threshold) @@ -134,7 +143,7 @@ class Asst: confirm_arr = (ctypes.c_int * confirm_len)() confirm_arr[:] = confirm_level - return self.__lib.AsstAppendRecruit(self.__ptr, max_times, + return Asst.__lib.AsstAppendRecruit(self.__ptr, max_times, select_arr, select_len, confirm_arr, confirm_len, need_refresh, use_expedited) @@ -150,13 +159,13 @@ class Asst: 2 - 投资过后再退出,没有投资就继续往后打 """ - return self.__lib.AsstAppendRoguelike(self.__ptr, mode) + return Asst.__lib.AsstAppendRoguelike(self.__ptr, mode) def start(self) -> bool: """ 开始任务 """ - return self.__lib.AsstStart(self.__ptr) + return Asst.__lib.AsstStart(self.__ptr) def start_recruit_calc(self, select_level: list, set_time: bool) -> bool: """ @@ -170,15 +179,15 @@ class Asst: select_arr = (ctypes.c_int * select_len)() select_arr[:] = select_level - return self.__lib.AsstStartRecruitCalc(self.__ptr, select_arr, select_len, set_time) + return Asst.__lib.AsstStartRecruitCalc(self.__ptr, select_arr, select_len, set_time) def stop(self) -> bool: """ 停止并清空所有任务 """ - self.__lib.AsstStop.restype = ctypes.c_bool - self.__lib.AsstStop.argtypes = (ctypes.c_void_p,) - return self.__lib.AsstStop(self.__ptr) + Asst.__lib.AsstStop.restype = ctypes.c_bool + Asst.__lib.AsstStop.argtypes = (ctypes.c_void_p,) + return Asst.__lib.AsstStop(self.__ptr) def set_penguin_id(self, id: str) -> bool: """ @@ -188,9 +197,9 @@ class Asst: ``id``: 企鹅物流ID,仅数字部分 """ - return self.__lib.AsstSetPenguinId(self.__ptr, id.encode('utf-8')) + return Asst.__lib.AsstSetPenguinId(self.__ptr, id.encode('utf-8')) - def log(self, level: str, message: str): + def log(self, level: str, message: str) -> None: ''' 打印日志 @@ -199,7 +208,8 @@ class Asst: ``message``: 日志内容 ''' - return self.__lib.AsstLog(self.__ptr, level.encode('utf-8'), message.encode('utf-8')) + Asst.__lib.AsstLog(self.__ptr, level.encode( + 'utf-8'), message.encode('utf-8')) def get_version(self) -> str: """ @@ -207,65 +217,70 @@ class Asst: :return: 版本号 """ - return self.__lib.AsstGetVersion().decode('utf-8') + return Asst.__lib.AsstGetVersion().decode('utf-8') - def __set_lib_properties(self): - self.__lib.AsstCreateEx.restype = ctypes.c_void_p - self.__lib.AsstCreateEx.argtypes = ( - ctypes.c_char_p, ctypes.c_void_p, ctypes.c_void_p,) + @staticmethod + def __set_lib_properties(): + Asst.__lib.AsstLoadResource.restype = ctypes.c_bool + Asst.__lib.AsstLoadResource.argtypes = ( + ctypes.c_char_p,) - self.__lib.AsstDestroy.argtypes = (ctypes.c_void_p,) + Asst.__lib.AsstCreateEx.restype = ctypes.c_void_p + Asst.__lib.AsstCreateEx.argtypes = ( + ctypes.c_void_p, ctypes.c_void_p,) - self.__lib.AsstConnect.restype = ctypes.c_bool - self.__lib.AsstConnect.argtypes = ( + Asst.__lib.AsstDestroy.argtypes = (ctypes.c_void_p,) + + Asst.__lib.AsstConnect.restype = ctypes.c_bool + Asst.__lib.AsstConnect.argtypes = ( ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p,) - self.__lib.AsstAppendStartUp.restype = ctypes.c_bool - self.__lib.AsstAppendStartUp.argtypes = (ctypes.c_void_p,) + Asst.__lib.AsstAppendStartUp.restype = ctypes.c_bool + Asst.__lib.AsstAppendStartUp.argtypes = (ctypes.c_void_p,) - self.__lib.AsstAppendFight.restype = ctypes.c_bool - self.__lib.AsstAppendFight.argtypes = ( + Asst.__lib.AsstAppendFight.restype = ctypes.c_bool + Asst.__lib.AsstAppendFight.argtypes = ( ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int, ctypes.c_int, ctypes.c_int,) - self.__lib.AsstAppendAward.restype = ctypes.c_bool - self.__lib.AsstAppendAward.argtypes = (ctypes.c_void_p,) + Asst.__lib.AsstAppendAward.restype = ctypes.c_bool + Asst.__lib.AsstAppendAward.argtypes = (ctypes.c_void_p,) - self.__lib.AsstAppendVisit.restype = ctypes.c_bool - self.__lib.AsstAppendVisit.argtypes = (ctypes.c_void_p,) + Asst.__lib.AsstAppendVisit.restype = ctypes.c_bool + Asst.__lib.AsstAppendVisit.argtypes = (ctypes.c_void_p,) - self.__lib.AsstAppendMall.restype = ctypes.c_bool - self.__lib.AsstAppendMall.argtypes = (ctypes.c_void_p, ctypes.c_bool) + Asst.__lib.AsstAppendMall.restype = ctypes.c_bool + Asst.__lib.AsstAppendMall.argtypes = (ctypes.c_void_p, ctypes.c_bool) - self.__lib.AsstAppendInfrast.restype = ctypes.c_bool - self.__lib.AsstAppendInfrast.argtypes = ( + Asst.__lib.AsstAppendInfrast.restype = ctypes.c_bool + Asst.__lib.AsstAppendInfrast.argtypes = ( ctypes.c_void_p, ctypes.c_int, ctypes.POINTER(ctypes.c_char_p), ctypes.c_int, ctypes.c_char_p, ctypes.c_double,) - self.__lib.AsstAppendRecruit.restype = ctypes.c_bool - self.__lib.AsstAppendRecruit.argtypes = ( + Asst.__lib.AsstAppendRecruit.restype = ctypes.c_bool + Asst.__lib.AsstAppendRecruit.argtypes = ( ctypes.c_void_p, ctypes.c_int, ctypes.POINTER(ctypes.c_int), ctypes.c_int, ctypes.POINTER(ctypes.c_int), ctypes.c_int, ctypes.c_bool, ctypes.c_bool,) - self.__lib.AsstAppendRoguelike.restype = ctypes.c_bool - self.__lib.AsstAppendRoguelike.argtypes = ( + Asst.__lib.AsstAppendRoguelike.restype = ctypes.c_bool + Asst.__lib.AsstAppendRoguelike.argtypes = ( ctypes.c_void_p, ctypes.c_int,) - self.__lib.AsstStart.restype = ctypes.c_bool - self.__lib.AsstStart.argtypes = (ctypes.c_void_p,) + Asst.__lib.AsstStart.restype = ctypes.c_bool + Asst.__lib.AsstStart.argtypes = (ctypes.c_void_p,) - self.__lib.AsstStartRecruitCalc.restype = ctypes.c_bool - self.__lib.AsstStartRecruitCalc.argtypes = ( + Asst.__lib.AsstStartRecruitCalc.restype = ctypes.c_bool + Asst.__lib.AsstStartRecruitCalc.argtypes = ( ctypes.c_void_p, ctypes.POINTER(ctypes.c_int), ctypes.c_int, ctypes.c_bool,) - self.__lib.AsstSetPenguinId.restype = ctypes.c_bool - self.__lib.AsstSetPenguinId.argtypes = ( + Asst.__lib.AsstSetPenguinId.restype = ctypes.c_bool + Asst.__lib.AsstSetPenguinId.argtypes = ( ctypes.c_void_p, ctypes.c_char_p) - self.__lib.AsstGetVersion.restype = ctypes.c_char_p + Asst.__lib.AsstGetVersion.restype = ctypes.c_char_p - self.__lib.AsstLog.restype = ctypes.c_none - self.__lib.AsstLog.argtypes = ( + Asst.__lib.AsstLog.restype = None + Asst.__lib.AsstLog.argtypes = ( ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p) diff --git a/src/Python/sample.py b/src/Python/sample.py index a6ecb0617c..9004d3fd9c 100644 --- a/src/Python/sample.py +++ b/src/Python/sample.py @@ -12,8 +12,10 @@ if __name__ == "__main__": print(m, d, arg) - dirname: str = (pathlib.Path.cwd()).__str__() - asst = Asst(dirname=dirname, callback=my_callback) + path: str = (pathlib.Path.cwd()).__str__() + Asst.load(path=path) + + asst = Asst(callback=my_callback) print('version', asst.get_version()) diff --git a/src/Python/timed_start/timed_sample.py b/src/Python/timed_start/timed_sample.py index aae364e62c..487edac0e8 100644 --- a/src/Python/timed_start/timed_sample.py +++ b/src/Python/timed_start/timed_sample.py @@ -4,6 +4,7 @@ author: Black Cat Bon version: v2.0.1 """ +from importlib.resources import path import threading import time import schedule @@ -110,8 +111,10 @@ if __name__ == "__main__": print(m, d, arg) - dirname: str = (pathlib.Path.cwd()).__str__() - asst = Asst(dirname=dirname, callback=my_callback) + path: str = (pathlib.Path.cwd()).__str__() + Asst.load(path) + + asst = Asst(callback=my_callback) print('version', asst.get_version()) diff --git a/tools/TestCaller/main.cpp b/tools/TestCaller/main.cpp index df035c8ea7..b5a02ae530 100644 --- a/tools/TestCaller/main.cpp +++ b/tools/TestCaller/main.cpp @@ -13,7 +13,9 @@ std::string get_cur_dir() int main(int argc, char** argv) { // 若使用 VS,请先设置 TestCaller 属性-调试-工作目录为 $(TargetDir) - auto ptr = AsstCreate(get_cur_dir().c_str()); + AsstLoadResource(get_cur_dir().c_str()); + + auto ptr = AsstCreate(); if (ptr == nullptr) { return -1; }