diff --git a/src/MeoAssistant/AbstractConfiger.cpp b/src/MeoAssistant/AbstractConfiger.cpp index 30fcb6b586..84f052cbb1 100644 --- a/src/MeoAssistant/AbstractConfiger.cpp +++ b/src/MeoAssistant/AbstractConfiger.cpp @@ -5,25 +5,21 @@ #include "AsstUtils.hpp" #include "Logger.hpp" -bool asst::AbstractConfiger::load(const std::string& filename) +bool asst::AbstractConfiger::load(const std::filesystem::path& path) { LogTraceFunction; -#ifdef _WIN32 - Log.info("Load:", utils::ansi_to_utf8(filename)); -#else - Log.info("Load:", filename); -#endif + Log.info("Load:", path); - if (!std::filesystem::exists(filename)) { + if (!std::filesystem::exists(path)) { return false; } // std::string content = utils::load_file_without_bom(filename); - auto&& ret = json::open(filename, true); + auto&& ret = json::open(path, true); if (!ret) { - m_last_error = "json passing error, filename: " + filename; + Log.error("Json open failed", path); return false; } @@ -33,11 +29,11 @@ bool asst::AbstractConfiger::load(const std::string& filename) return parse(root); } catch (json::exception& e) { - m_last_error = std::string("json field error ") + e.what(); + Log.error("Json parse failed", path, e.what()); return false; } catch (std::exception& e) { - m_last_error = std::string("json field error ") + e.what(); + Log.error("Json parse failed", path, e.what()); return false; } } diff --git a/src/MeoAssistant/AbstractConfiger.h b/src/MeoAssistant/AbstractConfiger.h index 7cfaacaf32..0dad0b57ea 100644 --- a/src/MeoAssistant/AbstractConfiger.h +++ b/src/MeoAssistant/AbstractConfiger.h @@ -2,8 +2,6 @@ #include "AbstractResource.h" -#include - namespace json { class value; @@ -15,7 +13,7 @@ namespace asst { public: virtual ~AbstractConfiger() override = default; - virtual bool load(const std::string& filename) override; + virtual bool load(const std::filesystem::path& path) override; protected: virtual bool parse(const json::value& json) = 0; diff --git a/src/MeoAssistant/AbstractConfigerWithTempl.h b/src/MeoAssistant/AbstractConfigerWithTempl.h new file mode 100644 index 0000000000..1b89e0c199 --- /dev/null +++ b/src/MeoAssistant/AbstractConfigerWithTempl.h @@ -0,0 +1,16 @@ +#pragma once + +#include "AbstractConfiger.h" + +#include + +namespace asst +{ + class AbstractConfigerWithTempl : public AbstractConfiger + { + public: + virtual ~AbstractConfigerWithTempl() override = default; + + virtual const std::unordered_set& get_templ_required() const noexcept = 0; + }; +} diff --git a/src/MeoAssistant/AbstractResource.cpp b/src/MeoAssistant/AbstractResource.cpp deleted file mode 100644 index aa45693c25..0000000000 --- a/src/MeoAssistant/AbstractResource.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "AbstractResource.h" diff --git a/src/MeoAssistant/AbstractResource.h b/src/MeoAssistant/AbstractResource.h index f8443cfa13..dd27da354b 100644 --- a/src/MeoAssistant/AbstractResource.h +++ b/src/MeoAssistant/AbstractResource.h @@ -1,25 +1,25 @@ #pragma once -#include +#include + +#include "SingletonHolder.hpp" namespace asst { class AbstractResource { public: - AbstractResource() = default; + virtual ~AbstractResource() = default; + virtual bool load(const std::filesystem::path& path) = 0; + + public: AbstractResource(const AbstractResource& rhs) = delete; AbstractResource(AbstractResource&& rhs) noexcept = delete; - virtual ~AbstractResource() = default; - - virtual bool load(const std::string& filename) = 0; - virtual const std::string& get_last_error() const noexcept { return m_last_error; } - AbstractResource& operator=(const AbstractResource& rhs) = delete; AbstractResource& operator=(AbstractResource&& rhs) noexcept = delete; protected: - std::string m_last_error; + AbstractResource() = default; }; } diff --git a/src/MeoAssistant/AbstractTask.cpp b/src/MeoAssistant/AbstractTask.cpp index d47aff6e0e..14c7df84bd 100644 --- a/src/MeoAssistant/AbstractTask.cpp +++ b/src/MeoAssistant/AbstractTask.cpp @@ -11,9 +11,9 @@ #include "AbstractTaskPlugin.h" #include "AsstUtils.hpp" #include "Controller.h" +#include "GeneralConfiger.h" #include "Logger.hpp" #include "ProcessTask.h" -#include "Resource.h" using namespace asst; @@ -36,7 +36,7 @@ bool asst::AbstractTask::run() if (need_exit()) { return false; } - int delay = Resrc.cfg().get_options().task_delay; + int delay = GeneralConfiger::get_instance().get_options().task_delay; sleep(delay); if (!on_run_fails()) { diff --git a/src/MeoAssistant/Assistant.cpp b/src/MeoAssistant/Assistant.cpp index 8cc97bebfa..eb1e07e991 100644 --- a/src/MeoAssistant/Assistant.cpp +++ b/src/MeoAssistant/Assistant.cpp @@ -6,8 +6,8 @@ #include "AsstUtils.hpp" #include "Controller.h" +#include "GeneralConfiger.h" #include "Logger.hpp" -#include "Resource.h" #include "RuntimeStatus.h" #include "AwardTask.h" @@ -267,7 +267,7 @@ void Assistant::working_proc() finished_tasks.clear(); } - auto delay = Resrc.cfg().get_options().task_delay; + auto delay = GeneralConfiger::get_instance().get_options().task_delay; lock.lock(); m_condvar.wait_for(lock, std::chrono::milliseconds(delay), [&]() -> bool { return m_thread_idle; }); } diff --git a/src/MeoAssistant/AsstCaller.cpp b/src/MeoAssistant/AsstCaller.cpp index 80afe5b994..18804d60af 100644 --- a/src/MeoAssistant/AsstCaller.cpp +++ b/src/MeoAssistant/AsstCaller.cpp @@ -1,6 +1,7 @@ #include "AsstCaller.h" #include +#include #include #include @@ -8,7 +9,7 @@ #include "Assistant.h" #include "AsstTypes.h" #include "Logger.hpp" -#include "Resource.h" +#include "ResourceLoader.h" #include "Version.h" static constexpr unsigned long long NullSize = static_cast(-1); @@ -42,16 +43,13 @@ static bool inited = false; bool AsstLoadResource(const char* path) { #ifdef _WIN32 - std::string working_path = asst::utils::utf8_to_ansi(path); + std::filesystem::path working_path = asst::utils::utf8_to_ansi(path); #else - std::string working_path = std::string(path); + std::filesystem::path working_path = path; #endif - bool log_inited = asst::Logger::set_dirname(working_path + "/"); - bool resrc_inited = asst::Resrc.load(working_path + "/resource/"); - if (!resrc_inited) { - std::cerr << asst::Resrc.get_last_error() << std::endl; - } - inited = log_inited && resrc_inited; + bool log_inited = asst::Logger::set_directory(working_path); + bool res_inited = asst::ResourceLoader::get_instance().load(working_path / "resource"); + inited = log_inited && res_inited; return inited; } diff --git a/src/MeoAssistant/AsstUtils.hpp b/src/MeoAssistant/AsstUtils.hpp index 72637bea64..b4417dc756 100644 --- a/src/MeoAssistant/AsstUtils.hpp +++ b/src/MeoAssistant/AsstUtils.hpp @@ -3,6 +3,7 @@ #include "AsstConf.h" #include "AsstRanges.hpp" +#include #include #include #include @@ -223,9 +224,9 @@ namespace asst::utils return RetTy { rect.x, rect.y, rect.width, rect.height }; } - inline std::string load_file_without_bom(const std::string& filename) + inline std::string load_file_without_bom(const std::filesystem::path& path) { - std::ifstream ifs(filename, std::ios::in); + std::ifstream ifs(path, std::ios::in); if (!ifs.is_open()) { return {}; } diff --git a/src/MeoAssistant/AutoRecruitTask.cpp b/src/MeoAssistant/AutoRecruitTask.cpp index cb9af0fde9..70b345a5e3 100644 --- a/src/MeoAssistant/AutoRecruitTask.cpp +++ b/src/MeoAssistant/AutoRecruitTask.cpp @@ -1,6 +1,7 @@ #include "AutoRecruitTask.h" #include "Controller.h" +#include "GeneralConfiger.h" #include "Logger.hpp" #include "MultiMatchImageAnalyzer.h" #include "OcrImageAnalyzer.h" @@ -8,7 +9,6 @@ #include "RecruitConfiger.h" #include "RecruitImageAnalyzer.h" #include "ReportDataTask.h" -#include "Resource.h" #include "AsstRanges.hpp" #include @@ -18,7 +18,7 @@ namespace asst::recruit_calc { // all combinations and their operator list, excluding empty set and 6-star operators while there is no senior tag auto get_all_combs(const std::vector& tags, - const std::vector& all_ops = Resrc.recruit().get_all_opers()) + const std::vector& all_ops = RecruitConfiger::get_instance().get_all_opers()) { std::vector rcs_with_single_tag; @@ -273,7 +273,7 @@ bool asst::AutoRecruitTask::recruit_one(const Rect& button) { LogTraceFunction; - int delay = Resrc.cfg().get_options().task_delay; + int delay = GeneralConfiger::get_instance().get_options().task_delay; m_ctrler->click(button); sleep(delay); diff --git a/src/MeoAssistant/BattleDataConfiger.h b/src/MeoAssistant/BattleDataConfiger.h index d9953fecb7..d0b3010431 100644 --- a/src/MeoAssistant/BattleDataConfiger.h +++ b/src/MeoAssistant/BattleDataConfiger.h @@ -8,7 +8,7 @@ namespace asst { - class BattleDataConfiger : public AbstractConfiger + class BattleDataConfiger final : public SingletonHolder, public AbstractConfiger { public: virtual ~BattleDataConfiger() override = default; diff --git a/src/MeoAssistant/BattleFormationTask.cpp b/src/MeoAssistant/BattleFormationTask.cpp index 132f6a3235..a178b7af7a 100644 --- a/src/MeoAssistant/BattleFormationTask.cpp +++ b/src/MeoAssistant/BattleFormationTask.cpp @@ -2,11 +2,13 @@ #include "AsstRanges.hpp" +#include "BattleDataConfiger.h" #include "Controller.h" +#include "CopilotConfiger.h" #include "Logger.hpp" #include "OcrWithFlagTemplImageAnalyzer.h" #include "ProcessTask.h" -#include "Resource.h" +#include "TaskData.h" void asst::BattleFormationTask::set_stage_name(std::string name) { @@ -155,13 +157,13 @@ bool asst::BattleFormationTask::click_role_table(BattleRole role) bool asst::BattleFormationTask::parse_formation() { - if (!Resrc.copilot().contains_actions(m_stage_name)) { + if (!CopilotConfiger::get_instance().contains_actions(m_stage_name)) { Log.error("Unknown stage name", m_stage_name); return false; } - const auto& group = Resrc.copilot().get_actions(m_stage_name).groups; - const auto& battle_data = Resrc.battle_data(); + const auto& group = CopilotConfiger::get_instance().get_actions(m_stage_name).groups; + const auto& battle_data = BattleDataConfiger::get_instance(); json::value info = basic_info_with_what("BattleFormation"); auto& details = info["details"]; diff --git a/src/MeoAssistant/BattleProcessTask.cpp b/src/MeoAssistant/BattleProcessTask.cpp index 66ef64119e..a82c4a55f2 100644 --- a/src/MeoAssistant/BattleProcessTask.cpp +++ b/src/MeoAssistant/BattleProcessTask.cpp @@ -8,16 +8,15 @@ #include "AsstRanges.hpp" #include "NoWarningCV.h" -#include "Controller.h" -#include "Logger.hpp" -#include "Resource.h" -#include "TaskData.h" - -#include "ProcessTask.h" - #include "BattleImageAnalyzer.h" +#include "Controller.h" +#include "CopilotConfiger.h" +#include "Logger.hpp" #include "MatchImageAnalyzer.h" #include "OcrWithPreprocessImageAnalyzer.h" +#include "ProcessTask.h" +#include "TaskData.h" +#include "TilePack.h" void asst::BattleProcessTask::set_stage_name(std::string name) { @@ -64,7 +63,7 @@ bool asst::BattleProcessTask::get_stage_info() { LogTraceFunction; - const auto& tile = Resrc.tile(); + const auto& tile = TilePack::get_instance(); m_normal_tile_info = tile.calc(m_stage_name, false); m_side_tile_info = tile.calc(m_stage_name, true); @@ -73,7 +72,7 @@ bool asst::BattleProcessTask::get_stage_info() return false; } - const auto& copilot = Resrc.copilot(); + const auto& copilot = CopilotConfiger::get_instance(); bool contains = copilot.contains_actions(m_stage_name); if (!contains) { return false; diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index 88cf9b070d..13cf74d02e 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -29,8 +29,8 @@ #endif #include "AsstTypes.h" +#include "GeneralConfiger.h" #include "Logger.hpp" -#include "Resource.h" #ifdef _WIN32 // for Windows socket @@ -479,7 +479,7 @@ asst::Point asst::Controller::rand_point_in_rect(const Rect& rect) void asst::Controller::random_delay() const { - auto& opt = Resrc.cfg().get_options(); + auto& opt = GeneralConfiger::get_instance().get_options(); if (opt.control_delay_upper != 0) { LogTraceFunction; static std::default_random_engine rand_engine(std::random_device {}()); @@ -785,7 +785,7 @@ std::optional asst::Controller::start_game(const std::string& client_type, if (client_type.empty()) { return std::nullopt; } - if (auto intent_name = Resrc.cfg().get_intent_name(client_type)) { + if (auto intent_name = GeneralConfiger::get_instance().get_intent_name(client_type)) { std::string cur_cmd = utils::string_replace_all(m_adb.start, "[Intent]", intent_name.value()); int id = push_cmd(cur_cmd); if (block) { @@ -882,7 +882,7 @@ int asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int int id = 0; // 额外的滑动:adb有bug,同样的参数,偶尔会划得非常远。额外做一个短程滑动,把之前的停下来 - const auto& opt = Resrc.cfg().get_options(); + const auto& opt = GeneralConfiger::get_instance().get_options(); if (extra_swipe && opt.adb_extra_swipe_duration > 0) { std::string extra_cmd = utils::string_replace_all_batch( m_adb.swipe, { @@ -938,7 +938,7 @@ bool asst::Controller::connect(const std::string& adb_path, const std::string& a }; }; - auto adb_ret = Resrc.cfg().get_adb_cfg(config); + auto adb_ret = GeneralConfiger::get_instance().get_adb_cfg(config); if (!adb_ret) { json::value info = get_info_json() | json::object { { "what", "ConnectFailed" }, diff --git a/src/MeoAssistant/CopilotConfiger.h b/src/MeoAssistant/CopilotConfiger.h index 2236f4d63b..16aae48656 100644 --- a/src/MeoAssistant/CopilotConfiger.h +++ b/src/MeoAssistant/CopilotConfiger.h @@ -4,7 +4,7 @@ namespace asst { - class CopilotConfiger : public AbstractConfiger + class CopilotConfiger : public SingletonHolder, public AbstractConfiger { public: virtual ~CopilotConfiger() override = default; diff --git a/src/MeoAssistant/CopilotTask.cpp b/src/MeoAssistant/CopilotTask.cpp index 8774a688ba..62f6f97488 100644 --- a/src/MeoAssistant/CopilotTask.cpp +++ b/src/MeoAssistant/CopilotTask.cpp @@ -2,8 +2,8 @@ #include "BattleFormationTask.h" #include "BattleProcessTask.h" +#include "CopilotConfiger.h" #include "ProcessTask.h" -#include "Resource.h" asst::CopilotTask::CopilotTask(const AsstCallback& callback, void* callback_arg) : PackageTask(callback, callback_arg, TaskType), @@ -44,5 +44,5 @@ bool asst::CopilotTask::set_params(const json::value& params) std::string filename = params.get("filename", std::string()); // 文件名为空时,不加载资源,直接返回 true - return filename.empty() || Resrc.copilot().load(filename); + return filename.empty() || CopilotConfiger::get_instance().load(filename); } diff --git a/src/MeoAssistant/CreditShopImageAnalyzer.cpp b/src/MeoAssistant/CreditShopImageAnalyzer.cpp index 3e8e52c4a5..ec53980abc 100644 --- a/src/MeoAssistant/CreditShopImageAnalyzer.cpp +++ b/src/MeoAssistant/CreditShopImageAnalyzer.cpp @@ -8,7 +8,7 @@ #include "MatchImageAnalyzer.h" #include "MultiMatchImageAnalyzer.h" #include "OcrImageAnalyzer.h" -#include "Resource.h" +#include "TaskData.h" void asst::CreditShopImageAnalyzer::set_black_list(std::vector black_list) { diff --git a/src/MeoAssistant/CreditShoppingTask.cpp b/src/MeoAssistant/CreditShoppingTask.cpp index 7b410f1879..c459ea25ac 100644 --- a/src/MeoAssistant/CreditShoppingTask.cpp +++ b/src/MeoAssistant/CreditShoppingTask.cpp @@ -7,7 +7,7 @@ #include "CreditShopImageAnalyzer.h" #include "MatchImageAnalyzer.h" #include "OcrImageAnalyzer.h" -#include "Resource.h" +#include "TaskData.h" void asst::CreditShoppingTask::set_black_list(std::vector black_list) { diff --git a/src/MeoAssistant/DepotImageAnalyzer.cpp b/src/MeoAssistant/DepotImageAnalyzer.cpp index 9b8d93096d..08b85d631f 100644 --- a/src/MeoAssistant/DepotImageAnalyzer.cpp +++ b/src/MeoAssistant/DepotImageAnalyzer.cpp @@ -3,10 +3,10 @@ #include "NoWarningCV.h" #include "AsstUtils.hpp" +#include "ItemConfiger.h" #include "Logger.hpp" #include "MatchImageAnalyzer.h" #include "OcrWithPreprocessImageAnalyzer.h" -#include "Resource.h" #include "TaskData.h" bool asst::DepotImageAnalyzer::analyze() @@ -113,7 +113,7 @@ bool asst::DepotImageAnalyzer::analyze_all_items() { LogTraceFunction; - auto& res_item = Resrc.item(); + auto& res_item = ItemConfiger::get_instance(); for (const Rect& roi : m_all_items_roi) { if (check_roi_empty(roi)) { // roi 是竖着有序的 @@ -157,7 +157,7 @@ size_t asst::DepotImageAnalyzer::match_item(const Rect& roi, /* out */ ItemInfo& { LogTraceFunction; - const auto& all_items = Resrc.item().get_ordered_material_item_id(); + const auto& all_items = ItemConfiger::get_instance().get_ordered_material_item_id(); MatchImageAnalyzer analyzer(m_image_resized); analyzer.set_task_info("DeoptMatchData"); diff --git a/src/MeoAssistant/DepotRecognitionTask.cpp b/src/MeoAssistant/DepotRecognitionTask.cpp index 3e2fb6fb4b..40a125173b 100644 --- a/src/MeoAssistant/DepotRecognitionTask.cpp +++ b/src/MeoAssistant/DepotRecognitionTask.cpp @@ -6,9 +6,9 @@ #include "Controller.h" #include "DepotImageAnalyzer.h" +#include "GeneralConfiger.h" #include "Logger.hpp" #include "ProcessTask.h" -#include "Resource.h" #include "TaskData.h" bool asst::DepotRecognitionTask::_run() @@ -56,7 +56,7 @@ void asst::DepotRecognitionTask::callback_analyze_result() { LogTraceFunction; - auto& templ = Resrc.cfg().get_options().depot_export_template; + auto& templ = GeneralConfiger::get_instance().get_options().depot_export_template; json::value info = basic_info_with_what("DepotInfo"); auto& details = info["details"]; diff --git a/src/MeoAssistant/GeneralConfiger.h b/src/MeoAssistant/GeneralConfiger.h index 32e497356d..6dffdc27ed 100644 --- a/src/MeoAssistant/GeneralConfiger.h +++ b/src/MeoAssistant/GeneralConfiger.h @@ -69,7 +69,7 @@ namespace asst std::string stop; }; - class GeneralConfiger : public AbstractConfiger + class GeneralConfiger final : public SingletonHolder, public AbstractConfiger { public: virtual ~GeneralConfiger() override = default; diff --git a/src/MeoAssistant/InfrastAbstractTask.cpp b/src/MeoAssistant/InfrastAbstractTask.cpp index 9c84ac7498..80be6d0052 100644 --- a/src/MeoAssistant/InfrastAbstractTask.cpp +++ b/src/MeoAssistant/InfrastAbstractTask.cpp @@ -10,7 +10,7 @@ #include "MatchImageAnalyzer.h" #include "OcrImageAnalyzer.h" #include "ProcessTask.h" -#include "Resource.h" +#include "TaskData.h" asst::InfrastAbstractTask::InfrastAbstractTask(AsstCallback callback, void* callback_arg, std::string task_chain) : AbstractTask(std::move(callback), callback_arg, std::move(task_chain)) diff --git a/src/MeoAssistant/InfrastClueImageAnalyzer.cpp b/src/MeoAssistant/InfrastClueImageAnalyzer.cpp index 93864402a4..e592d8a1ee 100644 --- a/src/MeoAssistant/InfrastClueImageAnalyzer.cpp +++ b/src/MeoAssistant/InfrastClueImageAnalyzer.cpp @@ -1,7 +1,6 @@ #include "InfrastClueImageAnalyzer.h" #include "MultiMatchImageAnalyzer.h" -#include "Resource.h" bool asst::InfrastClueImageAnalyzer::analyze() { diff --git a/src/MeoAssistant/InfrastClueVacancyImageAnalyzer.cpp b/src/MeoAssistant/InfrastClueVacancyImageAnalyzer.cpp index b7a365d483..3f260b3ba8 100644 --- a/src/MeoAssistant/InfrastClueVacancyImageAnalyzer.cpp +++ b/src/MeoAssistant/InfrastClueVacancyImageAnalyzer.cpp @@ -5,7 +5,6 @@ #include "AsstUtils.hpp" #include "Logger.hpp" #include "MatchImageAnalyzer.h" -#include "Resource.h" bool asst::InfrastClueVacancyImageAnalyzer::analyze() { diff --git a/src/MeoAssistant/InfrastConfiger.h b/src/MeoAssistant/InfrastConfiger.h index 2f5b44faaa..01747b563a 100644 --- a/src/MeoAssistant/InfrastConfiger.h +++ b/src/MeoAssistant/InfrastConfiger.h @@ -1,5 +1,5 @@ #pragma once -#include "AbstractConfiger.h" +#include "AbstractConfigerWithTempl.h" #include #include @@ -8,12 +8,15 @@ namespace asst { - class InfrastConfiger : public AbstractConfiger + class InfrastConfiger final : public SingletonHolder, public AbstractConfigerWithTempl { public: virtual ~InfrastConfiger() override = default; - auto get_templ_required() const noexcept -> const std::unordered_set& { return m_templ_required; } + virtual const std::unordered_set& get_templ_required() const noexcept override + { + return m_templ_required; + } auto get_skills(const std::string& facility_name) const -> const std::unordered_map& { diff --git a/src/MeoAssistant/InfrastDormTask.cpp b/src/MeoAssistant/InfrastDormTask.cpp index 5360230c47..c8e265689b 100644 --- a/src/MeoAssistant/InfrastDormTask.cpp +++ b/src/MeoAssistant/InfrastDormTask.cpp @@ -1,5 +1,7 @@ #include "InfrastDormTask.h" +#include + #include "Controller.h" #include "InfrastOperImageAnalyzer.h" #include "Logger.hpp" @@ -7,8 +9,7 @@ #include "OcrImageAnalyzer.h" #include "OcrWithPreprocessImageAnalyzer.h" #include "ProcessTask.h" -#include "Resource.h" -#include +#include "TaskData.h" asst::InfrastDormTask& asst::InfrastDormTask::set_notstationed_enabled(bool notstationed_enabled) noexcept { diff --git a/src/MeoAssistant/InfrastFacilityImageAnalyzer.cpp b/src/MeoAssistant/InfrastFacilityImageAnalyzer.cpp index 1958e337ef..e827a981b4 100644 --- a/src/MeoAssistant/InfrastFacilityImageAnalyzer.cpp +++ b/src/MeoAssistant/InfrastFacilityImageAnalyzer.cpp @@ -5,7 +5,7 @@ #include "AsstUtils.hpp" #include "Logger.hpp" #include "MultiMatchImageAnalyzer.h" -#include "Resource.h" +#include "TaskData.h" bool asst::InfrastFacilityImageAnalyzer::analyze() { diff --git a/src/MeoAssistant/InfrastInfoTask.cpp b/src/MeoAssistant/InfrastInfoTask.cpp index dbc93dcc43..346755c528 100644 --- a/src/MeoAssistant/InfrastInfoTask.cpp +++ b/src/MeoAssistant/InfrastInfoTask.cpp @@ -3,7 +3,6 @@ #include "Controller.h" #include "InfrastFacilityImageAnalyzer.h" #include "Logger.hpp" -#include "Resource.h" #include "RuntimeStatus.h" bool asst::InfrastInfoTask::_run() diff --git a/src/MeoAssistant/InfrastMfgTask.cpp b/src/MeoAssistant/InfrastMfgTask.cpp index f12cc14d3d..e695be9ccd 100644 --- a/src/MeoAssistant/InfrastMfgTask.cpp +++ b/src/MeoAssistant/InfrastMfgTask.cpp @@ -2,7 +2,6 @@ #include "Controller.h" #include "MatchImageAnalyzer.h" -#include "Resource.h" bool asst::InfrastMfgTask::_run() { diff --git a/src/MeoAssistant/InfrastOperImageAnalyzer.cpp b/src/MeoAssistant/InfrastOperImageAnalyzer.cpp index d18958913d..564a04024f 100644 --- a/src/MeoAssistant/InfrastOperImageAnalyzer.cpp +++ b/src/MeoAssistant/InfrastOperImageAnalyzer.cpp @@ -1,14 +1,14 @@ #include "InfrastOperImageAnalyzer.h" #include "AsstRanges.hpp" - #include "NoWarningCV.h" #include "HashImageAnalyzer.h" +#include "InfrastConfiger.h" #include "InfrastSmileyImageAnalyzer.h" #include "Logger.hpp" #include "MatchImageAnalyzer.h" -#include "Resource.h" +#include "TaskData.h" bool asst::InfrastOperImageAnalyzer::analyze() { @@ -273,7 +273,7 @@ void asst::InfrastOperImageAnalyzer::skill_analyze() std::vector> possible_skills; // 逐个该设施内所有可能的技能,取得分最高的 - for (const auto& skill : Resrc.infrast().get_skills(m_facility) | views::values) { + for (const auto& skill : InfrastConfiger::get_instance().get_skills(m_facility) | views::values) { skill_analyzer.set_templ_name(skill.templ_name); if (!skill_analyzer.analyze()) { diff --git a/src/MeoAssistant/InfrastProductionTask.cpp b/src/MeoAssistant/InfrastProductionTask.cpp index 69a1c95dd8..62a8b09680 100644 --- a/src/MeoAssistant/InfrastProductionTask.cpp +++ b/src/MeoAssistant/InfrastProductionTask.cpp @@ -7,14 +7,15 @@ #include "Controller.h" #include "HashImageAnalyzer.h" +#include "InfrastConfiger.h" #include "InfrastOperImageAnalyzer.h" #include "Logger.hpp" #include "MatchImageAnalyzer.h" #include "MultiMatchImageAnalyzer.h" #include "OcrWithPreprocessImageAnalyzer.h" #include "ProcessTask.h" -#include "Resource.h" #include "RuntimeStatus.h" +#include "TaskData.h" asst::InfrastProductionTask& asst::InfrastProductionTask::set_uses_of_drone(std::string uses_of_drones) noexcept { @@ -84,7 +85,7 @@ bool asst::InfrastProductionTask::shift_facility_list() /* 识别当前正在造什么 */ MatchImageAnalyzer product_analyzer(image); - auto& all_products = Resrc.infrast().get_facility_info(facility_name()).products; + auto& all_products = InfrastConfiger::get_instance().get_facility_info(facility_name()).products; std::string cur_product = all_products.at(0); double max_score = 0; for (const std::string& product : all_products) { @@ -228,7 +229,7 @@ size_t asst::InfrastProductionTask::opers_detect() bool asst::InfrastProductionTask::optimal_calc() { LogTraceFunction; - auto& facility_info = Resrc.infrast().get_facility_info(facility_name()); + auto& facility_info = InfrastConfiger::get_instance().get_facility_info(facility_name()); int cur_max_num_of_opers = facility_info.max_num_of_opers - m_cur_num_of_locked_opers; std::vector all_available_combs; @@ -299,7 +300,7 @@ bool asst::InfrastProductionTask::optimal_calc() } // 遍历所有组合,找到效率最高的 - auto& all_group = Resrc.infrast().get_skills_group(facility_name()); + auto& all_group = InfrastConfiger::get_instance().get_skills_group(facility_name()); for (const infrast::SkillsGroup& group : all_group) { Log.trace(group.desc); auto cur_available_opers = all_available_combs; @@ -442,7 +443,7 @@ bool asst::InfrastProductionTask::opers_choose() LogTraceFunction; bool has_error = false; - auto& facility_info = Resrc.infrast().get_facility_info(facility_name()); + auto& facility_info = InfrastConfiger::get_instance().get_facility_info(facility_name()); int cur_max_num_of_opers = facility_info.max_num_of_opers - m_cur_num_of_locked_opers; const int face_hash_thres = diff --git a/src/MeoAssistant/InfrastReceptionTask.cpp b/src/MeoAssistant/InfrastReceptionTask.cpp index 5c6460f29c..4baa893cc2 100644 --- a/src/MeoAssistant/InfrastReceptionTask.cpp +++ b/src/MeoAssistant/InfrastReceptionTask.cpp @@ -8,7 +8,7 @@ #include "Logger.hpp" #include "MatchImageAnalyzer.h" #include "ProcessTask.h" -#include "Resource.h" +#include "TaskData.h" bool asst::InfrastReceptionTask::_run() { diff --git a/src/MeoAssistant/InfrastSmileyImageAnalyzer.cpp b/src/MeoAssistant/InfrastSmileyImageAnalyzer.cpp index 56fcdc82ef..951cabf9e7 100644 --- a/src/MeoAssistant/InfrastSmileyImageAnalyzer.cpp +++ b/src/MeoAssistant/InfrastSmileyImageAnalyzer.cpp @@ -4,7 +4,6 @@ #include "AsstUtils.hpp" #include "MultiMatchImageAnalyzer.h" -#include "Resource.h" bool asst::InfrastSmileyImageAnalyzer::analyze() { diff --git a/src/MeoAssistant/ItemConfiger.h b/src/MeoAssistant/ItemConfiger.h index a028283210..774b62cbc4 100644 --- a/src/MeoAssistant/ItemConfiger.h +++ b/src/MeoAssistant/ItemConfiger.h @@ -1,13 +1,14 @@ #pragma once -#include "AbstractConfiger.h" +#include "AbstractConfigerWithTempl.h" #include #include #include + namespace asst { - class ItemConfiger : public AbstractConfiger + class ItemConfiger final : public SingletonHolder, public AbstractConfigerWithTempl { public: virtual ~ItemConfiger() override = default; @@ -27,6 +28,10 @@ namespace asst } } const auto& get_all_item_id() const noexcept { return m_all_item_id; } + virtual const std::unordered_set& get_templ_required() const noexcept override + { + return get_all_item_id(); + } const auto& get_ordered_material_item_id() const noexcept { return m_ordered_material_item_id; } protected: diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index 667cc30834..d46fcd6f13 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -11,32 +11,25 @@ #include "AsstRanges.hpp" #include "AsstUtils.hpp" +#include "SingletonHolder.hpp" #include "Version.h" namespace asst { - class Logger + class Logger : public SingletonHolder { public: - ~Logger() { flush(); } + virtual ~Logger() override { flush(); } - Logger(const Logger&) = delete; - Logger(Logger&&) = delete; - Logger& operator=(const Logger&) = delete; - Logger& operator=(Logger&&) = delete; - - static Logger& get_instance() + static bool set_directory(const std::filesystem::path& dir) { - static Logger _unique_instance; - return _unique_instance; - } - - static bool set_dirname(std::string dirname) noexcept - { - if (!std::filesystem::exists(dirname) || !std::filesystem::is_directory(dirname)) { + if (!std::filesystem::exists(dir) || !std::filesystem::is_directory(dir)) { return false; } - m_dirname = std::move(dirname); + m_directory = dir; + m_log_path = m_directory / "asst.log"; + m_log_bak_path = m_directory / "asst.bak.log"; + return true; } @@ -86,24 +79,24 @@ namespace asst } } - const std::string m_log_filename = m_dirname + "asst.log"; - const std::string m_log_bak_filename = m_dirname + "asst.bak.log"; + protected: + friend class SingletonHolder; - private: Logger() { check_filesize_and_remove(); log_init_info(); } + private: void check_filesize_and_remove() const { constexpr uintmax_t MaxLogSize = 4ULL * 1024 * 1024; try { - if (std::filesystem::exists(m_log_filename)) { - const uintmax_t log_size = std::filesystem::file_size(m_log_filename); + if (std::filesystem::exists(m_log_path)) { + const uintmax_t log_size = std::filesystem::file_size(m_log_path); if (log_size >= MaxLogSize) { - std::filesystem::rename(m_log_filename, m_log_bak_filename); + std::filesystem::rename(m_log_path, m_log_bak_path); } } } @@ -116,11 +109,7 @@ namespace asst trace("MeoAssistant Process Start"); trace("Version", asst::Version); trace("Built at", __DATE__, __TIME__); -#ifdef _WIN32 // 输出到日志的时候统一编码utf8 - trace("Working Path", asst::utils::ansi_to_utf8(m_dirname)); -#else - trace("Working Path", m_dirname); -#endif + trace("Working Path", m_directory); trace("-----------------------------"); } @@ -145,7 +134,7 @@ namespace asst #endif // END _WIN32 if (!m_ofs || !m_ofs.is_open()) { - m_ofs = std::ofstream(m_log_filename, std::ios::out | std::ios::app); + m_ofs = std::ofstream(m_log_path, std::ios::out | std::ios::app); } #ifdef ASST_DEBUG stream_put_line(m_ofs, buff, args...); @@ -195,7 +184,7 @@ namespace asst } else { ASST_STATIC_ASSERT_FALSE( - "\nunsupported type, one of the following expected\n" + "unsupported type, one of the following expected\n" "\t1. those can be converted to string;\n" "\t2. those can be inserted to stream with operator<< directly;\n" "\t3. container or nested container containing 1. 2. or 3. and iterable with range-based for", @@ -245,7 +234,10 @@ namespace asst return stream_put_line_impl::apply(s, std::forward(args)...); } - inline static std::string m_dirname; + inline static std::filesystem::path m_directory; + inline static std::filesystem::path m_log_path; + inline static std::filesystem::path m_log_bak_path; + std::mutex m_trace_mutex; std::ofstream m_ofs; }; diff --git a/src/MeoAssistant/MatchImageAnalyzer.cpp b/src/MeoAssistant/MatchImageAnalyzer.cpp index 744f37395d..47ea171e77 100644 --- a/src/MeoAssistant/MatchImageAnalyzer.cpp +++ b/src/MeoAssistant/MatchImageAnalyzer.cpp @@ -4,7 +4,8 @@ #include "AsstUtils.hpp" #include "Logger.hpp" -#include "Resource.h" +#include "TaskData.h" +#include "TemplResource.h" asst::MatchImageAnalyzer::MatchImageAnalyzer(const cv::Mat& image, const Rect& roi, std::string templ_name, double templ_thres) @@ -13,7 +14,7 @@ asst::MatchImageAnalyzer::MatchImageAnalyzer(const cv::Mat& image, const Rect& r bool asst::MatchImageAnalyzer::analyze() { - const cv::Mat templ = m_templ_name.empty() ? m_templ : Resrc.templ().get_templ(m_templ_name); + const cv::Mat templ = m_templ_name.empty() ? m_templ : TemplResource::get_instance().get_templ(m_templ_name); if (templ.empty()) { Log.error("templ is empty!"); return false; diff --git a/src/MeoAssistant/MeoAssistant.vcxproj b/src/MeoAssistant/MeoAssistant.vcxproj index a1ef17228f..7fb9598131 100644 --- a/src/MeoAssistant/MeoAssistant.vcxproj +++ b/src/MeoAssistant/MeoAssistant.vcxproj @@ -14,6 +14,7 @@ + @@ -81,7 +82,7 @@ - + @@ -99,6 +100,7 @@ + @@ -117,7 +119,6 @@ - @@ -172,7 +173,7 @@ - + diff --git a/src/MeoAssistant/MeoAssistant.vcxproj.filters b/src/MeoAssistant/MeoAssistant.vcxproj.filters index b674f502a9..01dff0d45f 100644 --- a/src/MeoAssistant/MeoAssistant.vcxproj.filters +++ b/src/MeoAssistant/MeoAssistant.vcxproj.filters @@ -132,7 +132,7 @@ 头文件\Resource - + 头文件 @@ -390,6 +390,12 @@ 头文件\Utils + + 头文件\Utils + + + 头文件\Resource + @@ -416,13 +422,10 @@ 源文件\Resource - - 源文件\Resource - 源文件\Resource - + 源文件 diff --git a/src/MeoAssistant/MultiMatchImageAnalyzer.cpp b/src/MeoAssistant/MultiMatchImageAnalyzer.cpp index 01fae54353..5251a021cc 100644 --- a/src/MeoAssistant/MultiMatchImageAnalyzer.cpp +++ b/src/MeoAssistant/MultiMatchImageAnalyzer.cpp @@ -6,8 +6,8 @@ #include "NoWarningCV.h" #include "Logger.hpp" -#include "Resource.h" #include "TaskData.h" +#include "TemplResource.h" asst::MultiMatchImageAnalyzer::MultiMatchImageAnalyzer(const cv::Mat& image, const Rect& roi, std::string templ_name, double templ_thres) @@ -19,7 +19,7 @@ bool asst::MultiMatchImageAnalyzer::analyze() Log.trace("MultiMatchImageAnalyzer::analyze | ", m_templ_name); m_result.clear(); - const cv::Mat templ = Resrc.templ().get_templ(m_templ_name); + const cv::Mat templ = TemplResource::get_instance().get_templ(m_templ_name); if (templ.empty()) { Log.error("templ is empty!"); return false; diff --git a/src/MeoAssistant/OcrImageAnalyzer.cpp b/src/MeoAssistant/OcrImageAnalyzer.cpp index 71ce59f054..a8a866070b 100644 --- a/src/MeoAssistant/OcrImageAnalyzer.cpp +++ b/src/MeoAssistant/OcrImageAnalyzer.cpp @@ -4,7 +4,8 @@ #include #include "Logger.hpp" -#include "Resource.h" +#include "OcrPack.h" +#include "TaskData.h" bool asst::OcrImageAnalyzer::analyze() { @@ -74,7 +75,7 @@ bool asst::OcrImageAnalyzer::analyze() m_roi.height = m_image.rows - m_roi.y; } - m_ocr_result = Resrc.ocr().recognize(m_image, m_roi, all_pred, m_without_det); + m_ocr_result = OcrPack::get_instance().recognize(m_image, m_roi, all_pred, m_without_det); // log.trace("ocr result", m_ocr_result); return !m_ocr_result.empty(); diff --git a/src/MeoAssistant/OcrPack.cpp b/src/MeoAssistant/OcrPack.cpp index 56093310a0..a383f25360 100644 --- a/src/MeoAssistant/OcrPack.cpp +++ b/src/MeoAssistant/OcrPack.cpp @@ -27,28 +27,22 @@ asst::OcrPack::~OcrPack() PaddleOcrDestroy(m_ocr); } -bool asst::OcrPack::load(const std::string& dir) +bool asst::OcrPack::load(const std::filesystem::path& path) { LogTraceFunction; - if (!std::filesystem::exists(dir)) { + if (!std::filesystem::exists(path)) { return false; } - constexpr static auto DetName = "/det"; - // constexpr static const char* ClsName = "/cls"; - constexpr static auto RecName = "/rec"; - constexpr static auto KeysName = "/ppocr_keys_v1.txt"; - - const std::string dst_filename = dir + DetName; - // const std::string cls_filename = dir + ClsName; - const std::string rec_filename = dir + RecName; - const std::string keys_filename = dir + KeysName; + const auto& dst_path = path / "det"; + const auto& rec_path = path / "rec"; + const auto& key_path = path / "ppocr_keys_v1.txt"; if (m_ocr != nullptr) { PaddleOcrDestroy(m_ocr); } - m_ocr = PaddleOcrCreate(dst_filename.c_str(), rec_filename.c_str(), keys_filename.c_str(), nullptr); + m_ocr = PaddleOcrCreate(dst_path.string().c_str(), rec_path.string().c_str(), key_path.string().c_str(), nullptr); return m_ocr != nullptr; } diff --git a/src/MeoAssistant/OcrPack.h b/src/MeoAssistant/OcrPack.h index 797fc296f7..19b8b78f77 100644 --- a/src/MeoAssistant/OcrPack.h +++ b/src/MeoAssistant/OcrPack.h @@ -13,16 +13,14 @@ struct paddle_ocr_t; namespace asst { - class OcrPack final : public AbstractResource + class OcrPack final : public SingletonHolder, public AbstractResource { constexpr static size_t MaxBoxSize = 128; public: - using AbstractResource::AbstractResource; - OcrPack(); virtual ~OcrPack() override; - virtual bool load(const std::string& dir) override; + virtual bool load(const std::filesystem::path& path) override; std::vector recognize(const cv::Mat& image, const TextRectProc& pred = nullptr, bool without_det = false); @@ -30,6 +28,9 @@ namespace asst bool without_det = false); private: + friend class SingletonHolder; + OcrPack(); + paddle_ocr_t* m_ocr = nullptr; // each box has 8 value ( 4 points, x and y ) diff --git a/src/MeoAssistant/PackageTask.cpp b/src/MeoAssistant/PackageTask.cpp index 05a837cc40..81bb7a53ea 100644 --- a/src/MeoAssistant/PackageTask.cpp +++ b/src/MeoAssistant/PackageTask.cpp @@ -1,7 +1,7 @@ #include "PackageTask.h" +#include "GeneralConfiger.h" #include "Logger.hpp" -#include "Resource.h" bool asst::PackageTask::run() { @@ -11,7 +11,7 @@ bool asst::PackageTask::run() } m_running = true; - const auto task_delay = Resrc.cfg().get_options().task_delay; + const auto task_delay = GeneralConfiger::get_instance().get_options().task_delay; for (size_t i = 0; i != m_subtasks.size(); ++i) { if (need_exit()) { diff --git a/src/MeoAssistant/ProcessTask.cpp b/src/MeoAssistant/ProcessTask.cpp index b2ec4586a3..8af0f1e40a 100644 --- a/src/MeoAssistant/ProcessTask.cpp +++ b/src/MeoAssistant/ProcessTask.cpp @@ -7,24 +7,25 @@ #include "AsstUtils.hpp" #include "Controller.h" +#include "GeneralConfiger.h" #include "Logger.hpp" #include "ProcessTaskImageAnalyzer.h" -#include "Resource.h" #include "RuntimeStatus.h" +#include "TaskData.h" using namespace asst; asst::ProcessTask::ProcessTask(const AbstractTask& abs, std::vector tasks_name) : AbstractTask(abs), m_raw_tasks_name(std::move(tasks_name)) { - m_task_delay = Resrc.cfg().get_options().task_delay; + m_task_delay = GeneralConfiger::get_instance().get_options().task_delay; m_basic_info_cache = json::value(); } asst::ProcessTask::ProcessTask(AbstractTask&& abs, std::vector tasks_name) noexcept : AbstractTask(std::move(abs)), m_raw_tasks_name(std::move(tasks_name)) { - m_task_delay = Resrc.cfg().get_options().task_delay; + m_task_delay = GeneralConfiger::get_instance().get_options().task_delay; m_basic_info_cache = json::value(); } @@ -35,7 +36,7 @@ bool asst::ProcessTask::run() return true; } if (m_task_delay == TaskDelayUnsetted) { - m_task_delay = Resrc.cfg().get_options().task_delay; + m_task_delay = GeneralConfiger::get_instance().get_options().task_delay; } m_cur_tasks_name = m_raw_tasks_name; diff --git a/src/MeoAssistant/ProcessTaskImageAnalyzer.cpp b/src/MeoAssistant/ProcessTaskImageAnalyzer.cpp index 3893cdf932..93d56dc58a 100644 --- a/src/MeoAssistant/ProcessTaskImageAnalyzer.cpp +++ b/src/MeoAssistant/ProcessTaskImageAnalyzer.cpp @@ -7,8 +7,8 @@ #include "Logger.hpp" #include "MatchImageAnalyzer.h" #include "OcrImageAnalyzer.h" -#include "Resource.h" #include "RuntimeStatus.h" +#include "TaskData.h" asst::ProcessTaskImageAnalyzer::ProcessTaskImageAnalyzer(const cv::Mat& image, std::vector tasks_name) : AbstractImageAnalyzer(image), m_tasks_name(std::move(tasks_name)) diff --git a/src/MeoAssistant/RecruitConfiger.h b/src/MeoAssistant/RecruitConfiger.h index c51ab29238..8213734bd7 100644 --- a/src/MeoAssistant/RecruitConfiger.h +++ b/src/MeoAssistant/RecruitConfiger.h @@ -83,7 +83,7 @@ namespace asst } }; - class RecruitConfiger : public AbstractConfiger + class RecruitConfiger final : public SingletonHolder, public AbstractConfiger { public: virtual ~RecruitConfiger() override = default; diff --git a/src/MeoAssistant/RecruitImageAnalyzer.cpp b/src/MeoAssistant/RecruitImageAnalyzer.cpp index 6ea9b0c957..dd746602ef 100644 --- a/src/MeoAssistant/RecruitImageAnalyzer.cpp +++ b/src/MeoAssistant/RecruitImageAnalyzer.cpp @@ -3,7 +3,8 @@ #include "MatchImageAnalyzer.h" #include "MultiMatchImageAnalyzer.h" #include "OcrImageAnalyzer.h" -#include "Resource.h" +#include "RecruitConfiger.h" +#include "TaskData.h" bool asst::RecruitImageAnalyzer::analyze() { @@ -23,7 +24,7 @@ bool asst::RecruitImageAnalyzer::tags_analyze() if (!analyzer_inited) { const auto tags_task_ptr = Task.get("RecruitTags"); tags_analyzer.set_roi(tags_task_ptr->roi); - auto& all_tags_set = Resrc.recruit().get_all_tags(); + auto& all_tags_set = RecruitConfiger::get_instance().get_all_tags(); std::vector all_tags_vec; all_tags_vec.assign(all_tags_set.begin(), all_tags_set.end()); // 把因为“资深干员”是“高级资深干员”的子串,把“高级资深干员”放到最前面,免得先被“资深干员”匹配上了 @@ -42,7 +43,7 @@ bool asst::RecruitImageAnalyzer::tags_analyze() if (tags_analyzer.analyze()) { m_tags_result = tags_analyzer.get_result(); return true; - // if (m_tags_result.size() == Resrc.recruit().CorrectNumberOfTags) { + // if (m_tags_result.size() == RecruitConfiger::get_instance().CorrectNumberOfTags) { // return true; // } } diff --git a/src/MeoAssistant/ReportDataTask.cpp b/src/MeoAssistant/ReportDataTask.cpp index 7a77455800..4fbbb11d66 100644 --- a/src/MeoAssistant/ReportDataTask.cpp +++ b/src/MeoAssistant/ReportDataTask.cpp @@ -1,8 +1,8 @@ #include "ReportDataTask.h" #include "AsstUtils.hpp" +#include "GeneralConfiger.h" #include "Logger.hpp" -#include "Resource.h" #include @@ -75,7 +75,8 @@ void asst::ReportDataTask::report_to_penguin() m_extra_param += " -H \"X-Penguin-Idempotency-Key: " + std::move(key) + "\""; - http::Response response = report("ReportToPenguinStats", Resrc.cfg().get_options().penguin_report.cmd_format); + http::Response response = + report("ReportToPenguinStats", GeneralConfiger::get_instance().get_options().penguin_report.cmd_format); if (response.success()) { if (auto penguinid_opt = response.find_header("x-penguin-set-penguinid")) [[unlikely]] { @@ -90,7 +91,7 @@ void asst::ReportDataTask::report_to_yituliu() { LogTraceFunction; - report("ReportToYituliu", Resrc.cfg().get_options().yituliu_report.cmd_format); + report("ReportToYituliu", GeneralConfiger::get_instance().get_options().yituliu_report.cmd_format); } asst::http::Response asst::ReportDataTask::report(const std::string& subtask, const std::string& format, diff --git a/src/MeoAssistant/Resource.cpp b/src/MeoAssistant/Resource.cpp deleted file mode 100644 index 32bc0fb43a..0000000000 --- a/src/MeoAssistant/Resource.cpp +++ /dev/null @@ -1,215 +0,0 @@ -#include "Resource.h" - -#include -#include -#include - -#include - -#include "AsstTypes.h" -#include "Logger.hpp" - -bool asst::Resource::load(const std::string& dir) -{ - LogTraceFunction; - - constexpr static auto TemplsFilename = "template"; - constexpr static auto GeneralCfgFilename = "config.json"; - constexpr static auto TaskDataFilename = "tasks.json"; - constexpr static auto RoguelikeRecruitCfgFilename = "roguelike_recruit.json"; - constexpr static auto RecruitCfgFilename = "recruit.json"; - constexpr static auto ItemCfgFilename = "item_index.json"; - constexpr static auto InfrastCfgFilename = "infrast.json"; - constexpr static auto InfrastTempls = "template/infrast"; - // constexpr static const char* CopilotCfgDirname = "copilot"; - constexpr static auto RoguelikeCfgDirname = "roguelike_copilot.json"; - constexpr static auto OcrResourceFilename = "PaddleOCR"; - constexpr static auto TilesCalcResourceFilename = "Arknights-Tile-Pos"; - constexpr static auto StageDropsCfgFilename = "stages.json"; - constexpr static auto StageDropsTempls = "template/items"; - constexpr static auto BattleDataCfgFilename = "battle_data.json"; - constexpr static auto RoguelikeShoppingCfgFilename = "roguelike_shopping.json"; - - bool overload = false; - - /* 加载各个Json配置文件 */ - if (!m_general_cfg_unique_ins.load(dir + GeneralCfgFilename)) { - if (!m_loaded) { - m_last_error = std::string(GeneralCfgFilename) + ": " + m_general_cfg_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - - if (!TaskData::get_instance().load(dir + TaskDataFilename)) { - if (!m_loaded) { - m_last_error = std::string(TaskDataFilename) + ": " + TaskData::get_instance().get_last_error(); - return false; - } - } - else { - overload = true; - } - - if (!m_recruit_cfg_unique_ins.load(dir + RecruitCfgFilename)) { - if (!m_loaded) { - m_last_error = std::string(RecruitCfgFilename) + ": " + m_recruit_cfg_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - - if (!m_roguelike_recruit_cfg_unique_ins.load(dir + RoguelikeRecruitCfgFilename)) { - if (!m_loaded) { - m_last_error = - std::string(RoguelikeRecruitCfgFilename) + ": " + m_roguelike_recruit_cfg_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - - if (!m_item_cfg_unique_ins.load(dir + ItemCfgFilename)) { - if (!m_loaded) { - m_last_error = std::string(ItemCfgFilename) + ": " + m_item_cfg_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - - // for (const auto& entry : std::filesystem::directory_iterator(dir + CopilotCfgDirname)) { - // if (entry.path().extension() != ".json") { - // continue; - // } - // if (!m_copilot_cfg_unique_ins.load(entry.path().string())) { - // m_last_error = entry.path().string() + " Load failed"; - // return false; - // } - // } - - if (!m_roguelike_cfg_unique_ins.load(dir + RoguelikeCfgDirname)) { - if (!m_loaded) { - m_last_error = std::string(RoguelikeCfgDirname) + ": " + m_roguelike_cfg_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - - if (!m_infrast_cfg_unique_ins.load(dir + InfrastCfgFilename)) { - if (!m_loaded) { - m_last_error = std::string(InfrastCfgFilename) + ": " + m_infrast_cfg_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - - if (!m_stage_drops_cfg_unique_ins.load(dir + StageDropsCfgFilename)) { - if (!m_loaded) { - m_last_error = std::string(StageDropsCfgFilename) + ": " + m_stage_drops_cfg_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - - /* 加载模板图片资源 */ - // task所需要的模板资源 - m_templ_resource_unique_ins.set_load_required(TaskData::get_instance().get_templ_required()); - if (!m_templ_resource_unique_ins.load(dir + TemplsFilename)) { - if (!m_loaded) { - m_last_error = std::string(TemplsFilename) + ": " + m_templ_resource_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - // 基建所需要的模板资源 - m_templ_resource_unique_ins.set_load_required(m_infrast_cfg_unique_ins.get_templ_required()); - if (!m_templ_resource_unique_ins.load(dir + InfrastTempls)) { - if (!m_loaded) { - m_last_error = std::string(InfrastTempls) + ": " + m_templ_resource_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - // 关卡掉落物品的模板资源 - m_templ_resource_unique_ins.set_load_required(m_item_cfg_unique_ins.get_all_item_id()); - if (!m_templ_resource_unique_ins.load(dir + StageDropsTempls)) { - if (!m_loaded) { - m_last_error = std::string(StageDropsTempls) + ": " + m_templ_resource_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - - // 加载战斗中需要的数据 - if (!m_battle_data_cfg_unique_ins.load(dir + BattleDataCfgFilename)) { - if (!m_loaded) { - m_last_error = std::string(BattleDataCfgFilename) + ": " + m_battle_data_cfg_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - - // 加载肉鸽购物需要的数据 - if (!m_roguelike_shopping_cfg_unique_ins.load(dir + RoguelikeShoppingCfgFilename)) { - if (!m_loaded) { - m_last_error = - std::string(RoguelikeShoppingCfgFilename) + ": " + m_roguelike_shopping_cfg_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - - /* 加载OCR库所需要的资源 */ - // m_ocr_pack_unique_ins.set_param(opt.ocr_gpu_index, opt.ocr_thread_number); - if (!m_ocr_pack_unique_ins.load(dir + OcrResourceFilename)) { - if (!m_loaded) { - m_last_error = std::string(OcrResourceFilename) + ": " + m_ocr_pack_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - - /* 加载地图格子识别库所需要的资源 */ - - if (!m_tile_pack_unique_ins.load(dir + TilesCalcResourceFilename)) { - if (!m_loaded) { - m_last_error = std::string(TilesCalcResourceFilename) + ": " + m_tile_pack_unique_ins.get_last_error(); - return false; - } - } - else { - overload = true; - } - - if (m_loaded) { - return overload; - } - - m_loaded = true; - return true; -} diff --git a/src/MeoAssistant/Resource.h b/src/MeoAssistant/Resource.h deleted file mode 100644 index 458e6a5200..0000000000 --- a/src/MeoAssistant/Resource.h +++ /dev/null @@ -1,97 +0,0 @@ -#pragma once - -#include "AbstractResource.h" - -#include -#include - -#include "BattleDataConfiger.h" -#include "CopilotConfiger.h" -#include "GeneralConfiger.h" -#include "InfrastConfiger.h" -#include "ItemConfiger.h" -#include "OcrPack.h" -#include "RecruitConfiger.h" -#include "RoguelikeCopilotConfiger.h" -#include "RoguelikeRecruitConfiger.h" -#include "RoguelikeShoppingConfiger.h" -#include "StageDropsConfiger.h" -#include "TaskData.h" -#include "TemplResource.h" -#include "TilePack.h" - -namespace asst -{ - class Resource : public AbstractResource - { - public: - virtual ~Resource() override = default; - - static Resource& get_instance() - { - static Resource unique_instance; - return unique_instance; - } - - virtual bool load(const std::string& dir) override; - - TemplResource& templ() noexcept { return m_templ_resource_unique_ins; } - GeneralConfiger& cfg() noexcept { return m_general_cfg_unique_ins; } - RecruitConfiger& recruit() noexcept { return m_recruit_cfg_unique_ins; } - RoguelikeRecruitConfiger& roguelike_recruit() noexcept { return m_roguelike_recruit_cfg_unique_ins; } - ItemConfiger& item() noexcept { return m_item_cfg_unique_ins; } - InfrastConfiger& infrast() noexcept { return m_infrast_cfg_unique_ins; } - CopilotConfiger& copilot() noexcept { return m_copilot_cfg_unique_ins; } - RoguelikeCopilotConfiger& roguelike() noexcept { return m_roguelike_cfg_unique_ins; } - OcrPack& ocr() noexcept { return m_ocr_pack_unique_ins; } - TilePack& tile() noexcept { return m_tile_pack_unique_ins; } - StageDropsConfiger& drops() noexcept { return m_stage_drops_cfg_unique_ins; } - BattleDataConfiger& battle_data() noexcept { return m_battle_data_cfg_unique_ins; } - RoguelikeShoppingConfiger& roguelike_shopping() noexcept { return m_roguelike_shopping_cfg_unique_ins; } - - const TemplResource& templ() const noexcept { return m_templ_resource_unique_ins; } - const GeneralConfiger& cfg() const noexcept { return m_general_cfg_unique_ins; } - const RecruitConfiger& recruit() const noexcept { return m_recruit_cfg_unique_ins; } - const RoguelikeRecruitConfiger& roguelike_recruit() const noexcept - { - return m_roguelike_recruit_cfg_unique_ins; - } - const ItemConfiger& item() const noexcept { return m_item_cfg_unique_ins; } - const InfrastConfiger& infrast() const noexcept { return m_infrast_cfg_unique_ins; } - const CopilotConfiger& copilot() const noexcept { return m_copilot_cfg_unique_ins; } - const RoguelikeCopilotConfiger& roguelike() const noexcept { return m_roguelike_cfg_unique_ins; } - const OcrPack& ocr() const noexcept { return m_ocr_pack_unique_ins; } - const TilePack& tile() const noexcept { return m_tile_pack_unique_ins; } - const StageDropsConfiger& drops() const noexcept { return m_stage_drops_cfg_unique_ins; } - const BattleDataConfiger& battle_data() const noexcept { return m_battle_data_cfg_unique_ins; } - const RoguelikeShoppingConfiger& roguelike_shopping() const noexcept - { - return m_roguelike_shopping_cfg_unique_ins; - } - - Resource& operator=(const Resource&) = delete; - Resource& operator=(Resource&&) noexcept = delete; - - private: - Resource() = default; - - TemplResource m_templ_resource_unique_ins; - GeneralConfiger m_general_cfg_unique_ins; - RecruitConfiger m_recruit_cfg_unique_ins; - RoguelikeRecruitConfiger m_roguelike_recruit_cfg_unique_ins; - CopilotConfiger m_copilot_cfg_unique_ins; - RoguelikeCopilotConfiger m_roguelike_cfg_unique_ins; - ItemConfiger m_item_cfg_unique_ins; - InfrastConfiger m_infrast_cfg_unique_ins; - OcrPack m_ocr_pack_unique_ins; - TilePack m_tile_pack_unique_ins; - StageDropsConfiger m_stage_drops_cfg_unique_ins; - BattleDataConfiger m_battle_data_cfg_unique_ins; - RoguelikeShoppingConfiger m_roguelike_shopping_cfg_unique_ins; - - bool m_loaded = false; - }; - - // static auto& resource = Resource::get_instance(); -#define Resrc Resource::get_instance() -} // namespace asst diff --git a/src/MeoAssistant/ResourceLoader.cpp b/src/MeoAssistant/ResourceLoader.cpp new file mode 100644 index 0000000000..7853d583ae --- /dev/null +++ b/src/MeoAssistant/ResourceLoader.cpp @@ -0,0 +1,71 @@ +#include "ResourceLoader.h" + +#include + +#include "Logger.hpp" + +#include "BattleDataConfiger.h" +#include "CopilotConfiger.h" +#include "GeneralConfiger.h" +#include "InfrastConfiger.h" +#include "ItemConfiger.h" +#include "OcrPack.h" +#include "RecruitConfiger.h" +#include "RoguelikeCopilotConfiger.h" +#include "RoguelikeRecruitConfiger.h" +#include "RoguelikeShoppingConfiger.h" +#include "StageDropsConfiger.h" +#include "TaskData.h" +#include "TemplResource.h" +#include "TilePack.h" + +bool asst::ResourceLoader::load(const std::filesystem::path& path) +{ +#define LoadResouceAndCheckRet(Configer, Filename) \ + { \ + auto full_path = path / Filename; \ + bool ret = load_resource(full_path); \ + if (!ret) { \ + Log.error("Load", full_path, "failed"); \ + return false; \ + } \ + } + +#define LoadResouceWithTemplAndCheckRet(Configer, Filename, TemplDir) \ + { \ + auto full_path = path / Filename; \ + auto full_templ_dir = path / TemplDir; \ + bool ret = load_resource_with_templ(full_path, full_templ_dir); \ + if (!ret) { \ + Log.error("Load", full_path, "with", full_templ_dir, "failed"); \ + return false; \ + } \ + } + + LogTraceFunction; + + /* load resource with json files*/ + LoadResouceAndCheckRet(GeneralConfiger, "config.json"); + LoadResouceAndCheckRet(RecruitConfiger, "recruit.json"); + LoadResouceAndCheckRet(StageDropsConfiger, "stages.json"); + LoadResouceAndCheckRet(RoguelikeCopilotConfiger, "roguelike_copilot.json"); + LoadResouceAndCheckRet(RoguelikeRecruitConfiger, "roguelike_recruit.json"); + LoadResouceAndCheckRet(RoguelikeShoppingConfiger, "roguelike_shopping.json"); + LoadResouceAndCheckRet(BattleDataConfiger, "battle_data.json"); + + /* load resource with json and template files*/ + LoadResouceWithTemplAndCheckRet(TaskData, "tasks.json", "template"); + LoadResouceWithTemplAndCheckRet(InfrastConfiger, "infrast.json", "template" / "infrast"); + LoadResouceWithTemplAndCheckRet(ItemConfiger, "item_index.json", "template" / "items"); + + /* load 3rd parties resource */ + LoadResouceAndCheckRet(TilePack, "Arknights-Tile-Pos" / "levels.json"); + LoadResouceAndCheckRet(OcrPack, "PaddleOCR"); + + m_loaded = true; + +#undef LoadTemplByConfigerAndCheckRet +#undef LoadResouceAndCheckRet + + return true; +} diff --git a/src/MeoAssistant/ResourceLoader.h b/src/MeoAssistant/ResourceLoader.h new file mode 100644 index 0000000000..e35f7e0dd9 --- /dev/null +++ b/src/MeoAssistant/ResourceLoader.h @@ -0,0 +1,48 @@ +#pragma once + +#include "AbstractResource.h" +#include "SingletonHolder.hpp" + +#include + +#include "AbstractConfigerWithTempl.h" +#include "TemplResource.h" + +namespace asst +{ + class ResourceLoader final : public SingletonHolder, public AbstractResource + { + public: + virtual ~ResourceLoader() override = default; + + virtual bool load(const std::filesystem::path& path) override; + + private: + template + requires std::is_base_of_v + bool load_resource(const std::filesystem::path& path) + { + if (!std::filesystem::exists(path)) { + return m_loaded; + } + return SingletonHolder::get_instance().load(path); + } + + template + requires std::is_base_of_v + bool load_resource_with_templ(const std::filesystem::path& path, const std::filesystem::path& templ_dir) + { + if (!load_resource(path)) { + return false; + } + static auto& templ_ins = SingletonHolder::get_instance(); + const auto& required = SingletonHolder::get_instance().get_templ_required(); + templ_ins.set_load_required(required); + + return load_resource(templ_dir); + } + + private: + bool m_loaded = false; + }; +} // namespace asst diff --git a/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp b/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp index ee59c56ab8..b8b845799d 100644 --- a/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp +++ b/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp @@ -6,15 +6,17 @@ #include "NoWarningCV.h" +#include "BattleDataConfiger.h" #include "BattleImageAnalyzer.h" #include "Controller.h" #include "Logger.hpp" #include "MatchImageAnalyzer.h" #include "OcrWithPreprocessImageAnalyzer.h" #include "ProcessTask.h" -#include "Resource.h" +#include "RoguelikeCopilotConfiger.h" #include "RuntimeStatus.h" #include "TaskData.h" +#include "TilePack.h" bool asst::RoguelikeBattleTaskPlugin::verify(AsstMsg msg, const json::value& details) const { @@ -95,7 +97,7 @@ bool asst::RoguelikeBattleTaskPlugin::get_stage_info() wait_for_start(); - const auto& tile = Resrc.tile(); + const auto& tile = TilePack::get_instance(); bool calced = false; if (m_stage_name.empty()) { @@ -135,7 +137,7 @@ bool asst::RoguelikeBattleTaskPlugin::get_stage_info() calced = true; } - auto opt = Resrc.roguelike().get_stage_data(m_stage_name); + auto opt = RoguelikeCopilotConfiger::get_instance().get_stage_data(m_stage_name); if (opt && !opt->replacement_home.empty()) { m_homes = opt->replacement_home; std::string log_str = "[ "; @@ -675,7 +677,7 @@ std::pair asst::RoguelikeBattleTaskPlugin::calc_best_direction int64_t elite = m_status->get_number(RuntimeStatus::RoguelikeCharElitePrefix + oper.name).value_or(0); // 按朝右算,后面根据方向做转换 - BattleAttackRange right_attack_range = Resrc.battle_data().get_range(oper.name, elite); + BattleAttackRange right_attack_range = BattleDataConfiger::get_instance().get_range(oper.name, elite); if (right_attack_range == BattleDataConfiger::EmptyRange) { switch (oper.role) { diff --git a/src/MeoAssistant/RoguelikeCopilotConfiger.h b/src/MeoAssistant/RoguelikeCopilotConfiger.h index f35e2cd4f0..33099358f9 100644 --- a/src/MeoAssistant/RoguelikeCopilotConfiger.h +++ b/src/MeoAssistant/RoguelikeCopilotConfiger.h @@ -7,7 +7,7 @@ namespace asst { - class RoguelikeCopilotConfiger : public AbstractConfiger + class RoguelikeCopilotConfiger final : public SingletonHolder, public AbstractConfiger { public: virtual ~RoguelikeCopilotConfiger() override = default; diff --git a/src/MeoAssistant/RoguelikeCustomStartTaskPlugin.cpp b/src/MeoAssistant/RoguelikeCustomStartTaskPlugin.cpp index 116aea6166..f898fd83c7 100644 --- a/src/MeoAssistant/RoguelikeCustomStartTaskPlugin.cpp +++ b/src/MeoAssistant/RoguelikeCustomStartTaskPlugin.cpp @@ -1,9 +1,10 @@ #include "RoguelikeCustomStartTaskPlugin.h" + +#include "BattleDataConfiger.h" #include "Controller.h" #include "Logger.hpp" #include "OcrImageAnalyzer.h" #include "ProcessTask.h" -#include "Resource.h" #include "RuntimeStatus.h" #include "TaskData.h" @@ -109,7 +110,7 @@ bool asst::RoguelikeCustomStartTaskPlugin::hijack_core_char() { BattleRole::Sniper, "狙击" }, { BattleRole::Special, "特种" }, { BattleRole::Support, "辅助" }, { BattleRole::Tank, "重装" }, { BattleRole::Warrior, "近卫" } }; - const auto& role = Resrc.battle_data().get_role(char_name); + const auto& role = BattleDataConfiger::get_instance().get_role(char_name); auto role_iter = RoleOcrNameMap.find(role); if (role_iter == RoleOcrNameMap.cend()) { Log.error("Unknown role", char_name, static_cast(role)); diff --git a/src/MeoAssistant/RoguelikeRecruitConfiger.h b/src/MeoAssistant/RoguelikeRecruitConfiger.h index 4e13677c6f..86e06d35d8 100644 --- a/src/MeoAssistant/RoguelikeRecruitConfiger.h +++ b/src/MeoAssistant/RoguelikeRecruitConfiger.h @@ -23,7 +23,7 @@ namespace asst BattleSkillUsage skill_usage = BattleSkillUsage::Possibly; BattleSkillUsage alternate_skill_usage = BattleSkillUsage::Possibly; }; - class RoguelikeRecruitConfiger : public AbstractConfiger + class RoguelikeRecruitConfiger final : public SingletonHolder, public AbstractConfiger { public: virtual ~RoguelikeRecruitConfiger() override = default; diff --git a/src/MeoAssistant/RoguelikeRecruitImageAnalyzer.cpp b/src/MeoAssistant/RoguelikeRecruitImageAnalyzer.cpp index 0f88ae6176..778bf5ce22 100644 --- a/src/MeoAssistant/RoguelikeRecruitImageAnalyzer.cpp +++ b/src/MeoAssistant/RoguelikeRecruitImageAnalyzer.cpp @@ -3,7 +3,6 @@ #include "Logger.hpp" #include "MatchImageAnalyzer.h" #include "OcrWithFlagTemplImageAnalyzer.h" -#include "Resource.h" #include "TaskData.h" bool asst::RoguelikeRecruitImageAnalyzer::analyze() diff --git a/src/MeoAssistant/RoguelikeRecruitTaskPlugin.cpp b/src/MeoAssistant/RoguelikeRecruitTaskPlugin.cpp index 8913530ec8..9065491f04 100644 --- a/src/MeoAssistant/RoguelikeRecruitTaskPlugin.cpp +++ b/src/MeoAssistant/RoguelikeRecruitTaskPlugin.cpp @@ -3,7 +3,7 @@ #include "Controller.h" #include "Logger.hpp" #include "ProcessTask.h" -#include "Resource.h" +#include "RoguelikeRecruitConfiger.h" #include "RoguelikeRecruitImageAnalyzer.h" #include "RuntimeStatus.h" #include "TaskData.h" @@ -37,7 +37,7 @@ bool asst::RoguelikeRecruitTaskPlugin::_run() recruited = true; }; - const auto& recruit_cfg = Resrc.roguelike_recruit(); + const auto& recruit_cfg = RoguelikeRecruitConfiger::get_instance(); // 编队信息 (已有角色) std::string str_chars_info = diff --git a/src/MeoAssistant/RoguelikeShoppingConfiger.h b/src/MeoAssistant/RoguelikeShoppingConfiger.h index 27f9938e05..5680f11881 100644 --- a/src/MeoAssistant/RoguelikeShoppingConfiger.h +++ b/src/MeoAssistant/RoguelikeShoppingConfiger.h @@ -18,7 +18,7 @@ namespace asst bool ignore_no_longer_buy = false; }; - class RoguelikeShoppingConfiger : public AbstractConfiger + class RoguelikeShoppingConfiger final : public SingletonHolder, public AbstractConfiger { public: virtual ~RoguelikeShoppingConfiger() override = default; diff --git a/src/MeoAssistant/RoguelikeShoppingTaskPlugin.cpp b/src/MeoAssistant/RoguelikeShoppingTaskPlugin.cpp index 1bcb57fb37..a56750de95 100644 --- a/src/MeoAssistant/RoguelikeShoppingTaskPlugin.cpp +++ b/src/MeoAssistant/RoguelikeShoppingTaskPlugin.cpp @@ -1,10 +1,11 @@ #include "RoguelikeShoppingTaskPlugin.h" +#include "BattleDataConfiger.h" #include "Controller.h" #include "Logger.hpp" #include "OcrWithFlagTemplImageAnalyzer.h" #include "ProcessTask.h" -#include "Resource.h" +#include "RoguelikeShoppingConfiger.h" #include "RuntimeStatus.h" bool asst::RoguelikeShoppingTaskPlugin::verify(AsstMsg msg, const json::value& details) const @@ -65,7 +66,7 @@ bool asst::RoguelikeShoppingTaskPlugin::_run() } } else { - auto& battle_data = Resrc.battle_data(); + auto& battle_data = BattleDataConfiger::get_instance(); BattleRole role = battle_data.get_role(name); map_roles_count[role] += 1; @@ -81,7 +82,7 @@ bool asst::RoguelikeShoppingTaskPlugin::_run() } const auto& result = analyzer.get_result(); - const auto& order_goods_list = Resrc.roguelike_shopping().get_goods(); + const auto& order_goods_list = RoguelikeShoppingConfiger::get_instance().get_goods(); bool bought = false; for (const auto& goods : order_goods_list) { if (need_exit()) { diff --git a/src/MeoAssistant/RoguelikeSkillSelectionImageAnalyzer.cpp b/src/MeoAssistant/RoguelikeSkillSelectionImageAnalyzer.cpp index a7c365a108..ee15fe9ef0 100644 --- a/src/MeoAssistant/RoguelikeSkillSelectionImageAnalyzer.cpp +++ b/src/MeoAssistant/RoguelikeSkillSelectionImageAnalyzer.cpp @@ -6,7 +6,7 @@ #include "Logger.hpp" #include "MultiMatchImageAnalyzer.h" #include "OcrWithPreprocessImageAnalyzer.h" -#include "Resource.h" +#include "RoguelikeRecruitConfiger.h" #include "TaskData.h" bool asst::RoguelikeSkillSelectionImageAnalyzer::analyze() @@ -50,7 +50,7 @@ std::string asst::RoguelikeSkillSelectionImageAnalyzer::name_analyze(const Rect& analyzer.set_task_info(name_task_ptr); analyzer.set_image(m_image); analyzer.set_roi(roi.move(name_task_ptr->roi)); - analyzer.set_required(Resrc.roguelike_recruit().get_oper_order()); + analyzer.set_required(RoguelikeRecruitConfiger::get_instance().get_oper_order()); analyzer.set_replace(std::dynamic_pointer_cast(Task.get("CharsNameOcrReplace"))->replace_map); if (!analyzer.analyze()) { diff --git a/src/MeoAssistant/RoguelikeSkillSelectionTaskPlugin.cpp b/src/MeoAssistant/RoguelikeSkillSelectionTaskPlugin.cpp index c8ab8f134b..59dd7813d7 100644 --- a/src/MeoAssistant/RoguelikeSkillSelectionTaskPlugin.cpp +++ b/src/MeoAssistant/RoguelikeSkillSelectionTaskPlugin.cpp @@ -1,7 +1,7 @@ #include "RoguelikeSkillSelectionTaskPlugin.h" #include "Controller.h" -#include "Resource.h" +#include "RoguelikeRecruitConfiger.h" #include "RoguelikeSkillSelectionImageAnalyzer.h" #include "RuntimeStatus.h" #include "TaskData.h" @@ -33,7 +33,7 @@ bool asst::RoguelikeSkillSelectionTaskPlugin::_run() return false; } - const auto& rg_src = Resrc.roguelike_recruit(); + const auto& rg_src = RoguelikeRecruitConfiger::get_instance(); for (const auto& [name, skill_vec] : analyzer.get_result()) { if (name.empty()) { continue; diff --git a/src/MeoAssistant/SingletonHolder.hpp b/src/MeoAssistant/SingletonHolder.hpp new file mode 100644 index 0000000000..321166fd15 --- /dev/null +++ b/src/MeoAssistant/SingletonHolder.hpp @@ -0,0 +1,26 @@ +#pragma once + +namespace asst +{ + template + class SingletonHolder + { + public: + static T& get_instance() + { + static T unique_instance; + return unique_instance; + } + virtual ~SingletonHolder() = default; + + public: + SingletonHolder(const SingletonHolder&) = delete; + SingletonHolder(SingletonHolder&&) = delete; + + SingletonHolder& operator=(const SingletonHolder&) = delete; + SingletonHolder& operator=(SingletonHolder&&) = delete; + + protected: + SingletonHolder() = default; + }; +} diff --git a/src/MeoAssistant/StageDropsConfiger.h b/src/MeoAssistant/StageDropsConfiger.h index 68900726a6..309fdd54ef 100644 --- a/src/MeoAssistant/StageDropsConfiger.h +++ b/src/MeoAssistant/StageDropsConfiger.h @@ -53,10 +53,9 @@ namespace asst int quantity = 0; }; - class StageDropsConfiger final : public AbstractConfiger + class StageDropsConfiger final : public SingletonHolder, public AbstractConfiger { public: - using AbstractConfiger::AbstractConfiger; virtual ~StageDropsConfiger() override = default; const auto& get_stage_info(const std::string& code, StageDifficulty difficulty) const diff --git a/src/MeoAssistant/StageDropsImageAnalyzer.cpp b/src/MeoAssistant/StageDropsImageAnalyzer.cpp index 516be3fbb7..ad7b451c3a 100644 --- a/src/MeoAssistant/StageDropsImageAnalyzer.cpp +++ b/src/MeoAssistant/StageDropsImageAnalyzer.cpp @@ -5,10 +5,11 @@ #include "NoWarningCV.h" #include "AsstUtils.hpp" +#include "ItemConfiger.h" #include "Logger.hpp" #include "MatchImageAnalyzer.h" #include "OcrWithPreprocessImageAnalyzer.h" -#include "Resource.h" +#include "StageDropsConfiger.h" #include "TaskData.h" #include @@ -56,11 +57,6 @@ bool asst::StageDropsImageAnalyzer::analyze_stage_code() OcrImageAnalyzer analyzer(m_image); analyzer.set_task_info("StageDrops-StageName"); - // const auto& stages = Resrc.drops().get_all_stage_code(); - // std::vector stages_req(stages.cbegin(), stages.cend()); - // // 名字长的放前面 - // ranges::sort(stages_req, std::greater {}, std::mem_fn(&std::string::size)); - // analyzer.set_required(std::move(stages_req)); if (!analyzer.analyze()) { return false; @@ -185,7 +181,6 @@ bool asst::StageDropsImageAnalyzer::analyze_drops() auto task_ptr = Task.get("StageDrops-Item"); - auto& ResrcItem = Resrc.item(); bool has_error = false; const auto& roi = task_ptr->roi; for (auto it = m_baseline.cbegin(); it != m_baseline.cend(); ++it) { @@ -227,7 +222,7 @@ bool asst::StageDropsImageAnalyzer::analyze_drops() info.item_id = std::move(item); info.quantity = quantity; - const std::string& name = ResrcItem.get_item_name(info.item_id); + const std::string& name = ItemConfiger::get_instance().get_item_name(info.item_id); info.item_name = name.empty() ? info.item_id : name; static const std::unordered_map DropTypeName = { @@ -454,7 +449,7 @@ std::string asst::StageDropsImageAnalyzer::match_item(const Rect& roi, StageDrop std::string result; if (!m_stage_code.empty()) { - auto& drops = Resrc.drops().get_stage_info(m_stage_code, m_difficulty).drops; + auto& drops = StageDropsConfiger::get_instance().get_stage_info(m_stage_code, m_difficulty).drops; if (auto find_iter = drops.find(type); find_iter != drops.end()) { result = match_item_with_templs(find_iter->second); } @@ -462,11 +457,11 @@ std::string asst::StageDropsImageAnalyzer::match_item(const Rect& roi, StageDrop // 没识别到的话就把全部材料都拿来跑一遍 if (result.empty()) { - auto items = Resrc.item().get_all_item_id(); + auto items = ItemConfiger::get_instance().get_all_item_id(); result = match_item_with_templs(std::vector(items.cbegin(), items.cend())); // 将这次识别到的加入该关卡的待识别列表 if (!result.empty() && !m_stage_code.empty()) { - Resrc.drops().append_drops(StageKey { m_stage_code, m_difficulty }, type, result); + StageDropsConfiger::get_instance().append_drops(StageKey { m_stage_code, m_difficulty }, type, result); } } diff --git a/src/MeoAssistant/StageDropsTaskPlugin.cpp b/src/MeoAssistant/StageDropsTaskPlugin.cpp index c41854de9c..59410b3e1e 100644 --- a/src/MeoAssistant/StageDropsTaskPlugin.cpp +++ b/src/MeoAssistant/StageDropsTaskPlugin.cpp @@ -6,11 +6,12 @@ #include "AsstUtils.hpp" #include "Controller.h" +#include "ItemConfiger.h" #include "Logger.hpp" #include "ProcessTask.h" #include "ReportDataTask.h" -#include "Resource.h" #include "RuntimeStatus.h" +#include "StageDropsConfiger.h" #include "StageDropsImageAnalyzer.h" #include "TaskData.h" #include "Version.h" @@ -147,7 +148,7 @@ void asst::StageDropsTaskPlugin::drop_info_callback() drops_vec.emplace_back(std::move(info)); } - auto& item = Resrc.item(); + auto& item = ItemConfiger::get_instance(); std::vector stats_vec; for (auto&& [id, count] : m_drop_stats) { @@ -172,7 +173,7 @@ void asst::StageDropsTaskPlugin::drop_info_callback() json::value& stage = details["stage"]; stage["stageCode"] = m_stage_code; if (!m_stage_code.empty()) { - stage["stageId"] = Resrc.drops().get_stage_info(m_stage_code, m_stage_difficulty).stage_id; + stage["stageId"] = StageDropsConfiger::get_instance().get_stage_info(m_stage_code, m_stage_difficulty).stage_id; } callback(AsstMsg::SubTaskExtraInfo, info); diff --git a/src/MeoAssistant/TaskData.cpp b/src/MeoAssistant/TaskData.cpp index 8702bea9c4..3a01766ede 100644 --- a/src/MeoAssistant/TaskData.cpp +++ b/src/MeoAssistant/TaskData.cpp @@ -37,7 +37,7 @@ bool asst::TaskData::parse(const json::value& json) algorithm = AlgorithmType::Hash; } else { - m_last_error = "algorithm error " + algorithm_str; + Log.error("Unknown algorithm", algorithm_str); return false; } @@ -124,7 +124,7 @@ bool asst::TaskData::parse(const json::value& json) task_info_ptr->action = ProcessTaskAction::SlowlySwipeToTheRight; } else { - m_last_error = "Task: " + name + " error: " + action; + Log.error("Unknown action:", action, ", Task:", name); return false; } @@ -154,7 +154,7 @@ bool asst::TaskData::parse(const json::value& json) int height = static_cast(roi_arr[3]); #ifdef ASST_DEBUG if (x + width > WindowWidthDefault || y + height > WindowHeightDefault) { - m_last_error = name + " roi is out of bounds"; + Log.error(name, "roi is out of bounds"); return false; } #endif @@ -201,7 +201,7 @@ bool asst::TaskData::parse(const json::value& json) 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"; + Log.error(name, "'s next", next, "is null"); return false; } } diff --git a/src/MeoAssistant/TaskData.h b/src/MeoAssistant/TaskData.h index bc37e9c0ea..3779a3ec2a 100644 --- a/src/MeoAssistant/TaskData.h +++ b/src/MeoAssistant/TaskData.h @@ -1,6 +1,6 @@ #pragma once -#include "AbstractConfiger.h" +#include "AbstractConfigerWithTempl.h" #include #include @@ -10,20 +10,11 @@ namespace asst { struct TaskInfo; - class TaskData : public AbstractConfiger + class TaskData final : public SingletonHolder, public AbstractConfigerWithTempl { public: - TaskData(const TaskData&) = delete; - TaskData(TaskData&&) = delete; - virtual ~TaskData() override = default; - - static TaskData& get_instance() noexcept - { - static TaskData unique_instance; - return unique_instance; - } - const std::unordered_set& get_templ_required() const noexcept; + virtual const std::unordered_set& get_templ_required() const noexcept override; template requires std::derived_from && @@ -51,13 +42,11 @@ namespace asst } protected: - TaskData() = default; - virtual bool parse(const json::value& json) override; std::unordered_map> m_all_tasks_info; std::unordered_set m_templ_required; }; - static auto& Task = TaskData::get_instance(); + inline static auto& Task = TaskData::get_instance(); } diff --git a/src/MeoAssistant/TemplResource.cpp b/src/MeoAssistant/TemplResource.cpp index a859c54772..eea577b136 100644 --- a/src/MeoAssistant/TemplResource.cpp +++ b/src/MeoAssistant/TemplResource.cpp @@ -13,12 +13,12 @@ void asst::TemplResource::set_load_required(std::unordered_set requ m_templs_filename = std::move(required); } -bool asst::TemplResource::load(const std::string& dir) +bool asst::TemplResource::load(const std::filesystem::path& path) { LogTraceFunction; for (const std::string& filename : m_templs_filename) { - std::filesystem::path filepath(dir + "/" + filename); + std::filesystem::path filepath(path / filename); if (!filepath.has_extension()) { filepath.replace_extension(".png"); } @@ -30,7 +30,7 @@ bool asst::TemplResource::load(const std::string& dir) continue; } else { - m_last_error = filepath.string() + " not exists"; + Log.error("Templ load failed, file not exists", filepath); return false; } } diff --git a/src/MeoAssistant/TemplResource.h b/src/MeoAssistant/TemplResource.h index 4181fb6631..8a9d8aff77 100644 --- a/src/MeoAssistant/TemplResource.h +++ b/src/MeoAssistant/TemplResource.h @@ -1,6 +1,7 @@ #pragma once #include "AbstractResource.h" +#include "SingletonHolder.hpp" #include #include @@ -9,13 +10,13 @@ namespace asst { - class TemplResource final : public AbstractResource + class TemplResource final : public SingletonHolder, public AbstractResource { public: virtual ~TemplResource() override = default; void set_load_required(std::unordered_set required) noexcept; - virtual bool load(const std::string& dir) override; + virtual bool load(const std::filesystem::path& path) override; bool exist_templ(const std::string& key) const noexcept; const cv::Mat get_templ(const std::string& key) const noexcept; diff --git a/src/MeoAssistant/TilePack.cpp b/src/MeoAssistant/TilePack.cpp index 8c533786d1..dee8509f4c 100644 --- a/src/MeoAssistant/TilePack.cpp +++ b/src/MeoAssistant/TilePack.cpp @@ -9,21 +9,19 @@ ASST_SUPPRESS_CV_WARNINGS_END asst::TilePack::~TilePack() = default; -bool asst::TilePack::load(const std::string& dir) +bool asst::TilePack::load(const std::filesystem::path& path) { LogTraceFunction; - if (!std::filesystem::exists(dir)) { + if (!std::filesystem::exists(path)) { return false; } - constexpr static auto filename = "/levels.json"; - try { - m_tile_calculator = std::make_unique(WindowWidthDefault, WindowHeightDefault, dir + filename); + m_tile_calculator = std::make_unique(WindowWidthDefault, WindowHeightDefault, path.string()); } catch (std::exception& e) { - m_last_error = e.what(); + Log.error("Tile create failed", e.what()); return false; } return m_tile_calculator != nullptr; diff --git a/src/MeoAssistant/TilePack.h b/src/MeoAssistant/TilePack.h index d51754be8c..1a7e6c0db0 100644 --- a/src/MeoAssistant/TilePack.h +++ b/src/MeoAssistant/TilePack.h @@ -12,7 +12,7 @@ namespace Map namespace asst { - class TilePack final : public AbstractResource + class TilePack final : public SingletonHolder, public AbstractResource { public: enum class BuildableType @@ -55,10 +55,9 @@ namespace asst }; public: - using AbstractResource::AbstractResource; virtual ~TilePack() override; - virtual bool load(const std::string& dir) override; + virtual bool load(const std::filesystem::path& path) override; std::unordered_map calc(const std::string& stage_code, bool side) const;