diff --git a/source/MaaFramework/Task/Component/Actuator.cpp b/source/MaaFramework/Task/Component/Actuator.cpp index 5fb0c6ab3..ab510832c 100644 --- a/source/MaaFramework/Task/Component/Actuator.cpp +++ b/source/MaaFramework/Task/Component/Actuator.cpp @@ -244,7 +244,7 @@ bool Actuator::command( .image = controller()->cached_image(), .box = box, }; - return CommandAction::get_instance().run(param, rt); + return CommandAction().run(param, rt); } bool Actuator::custom_action(const MAA_RES_NS::Action::CustomParam& param, const cv::Rect& box, MaaRecoId reco_id, const std::string& name) diff --git a/source/MaaFramework/Task/Component/CommandAction.cpp b/source/MaaFramework/Task/Component/CommandAction.cpp index 6544e8ab5..62a34af2f 100644 --- a/source/MaaFramework/Task/Component/CommandAction.cpp +++ b/source/MaaFramework/Task/Component/CommandAction.cpp @@ -12,11 +12,11 @@ MAA_TASK_NS_BEGIN -CommandAction::~CommandAction() +TempFileHolder::~TempFileHolder() { LogFunc; - for (const auto& p : cached_images_) { + for (const auto& p : files_) { if (!std::filesystem::exists(p)) { continue; } @@ -25,6 +25,12 @@ CommandAction::~CommandAction() } } +void TempFileHolder::emplace(const std::filesystem::path& p) +{ + LogTrace << p; + files_.emplace_back(p); +} + bool CommandAction::run(const MAA_RES_NS::Action::CommandParam& command, const Runtime& runtime) { LogFunc << VAR(command.exec) << VAR(command.args) << VAR(command.detach); @@ -44,7 +50,7 @@ bool CommandAction::run(const MAA_RES_NS::Action::CommandParam& command, const R if (src.find(key) == std::string::npos) { continue; } - string_replace_all(dst, key, func(runtime)); + dst = string_replace_all(dst, key, func(runtime)); } return dst; }; @@ -101,7 +107,7 @@ std::string CommandAction::get_image_path(const Runtime& runtime) } auto dst_path = std::filesystem::temp_directory_path() / (format_now_for_filename() + ".png"); - cached_images_.emplace_back(dst_path); + TempFileHolder::get_instance().emplace(dst_path); imwrite(dst_path, runtime.image); image_path_ = path_to_utf8_string(dst_path); return image_path_; diff --git a/source/MaaFramework/Task/Component/CommandAction.h b/source/MaaFramework/Task/Component/CommandAction.h index d5c22ce94..0884d228f 100644 --- a/source/MaaFramework/Task/Component/CommandAction.h +++ b/source/MaaFramework/Task/Component/CommandAction.h @@ -7,9 +7,21 @@ #include "Conf/Conf.h" #include "Resource/PipelineTypes.h" #include "Utils/Platform.h" +#include "Utils/SingletonHolder.hpp" MAA_TASK_NS_BEGIN +class TempFileHolder : public SingletonHolder +{ +public: + virtual ~TempFileHolder() override; + + void emplace(const std::filesystem::path& p); + +private: + std::vector files_; +}; + class CommandAction { public: @@ -24,8 +36,6 @@ class CommandAction }; public: - ~CommandAction(); - bool run(const MAA_RES_NS::Action::CommandParam& command, const Runtime& runtime); private: @@ -37,8 +47,6 @@ class CommandAction std::string get_resource_dir(const Runtime& runtime); private: - static std::vector cached_images_; - std::string image_path_; };