Skip to content

Commit

Permalink
fix: temp file error
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Dec 18, 2024
1 parent 0eab5ba commit 7e1ab67
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion source/MaaFramework/Task/Component/Actuator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions source/MaaFramework/Task/Component/CommandAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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;
};
Expand Down Expand Up @@ -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_;
Expand Down
16 changes: 12 additions & 4 deletions source/MaaFramework/Task/Component/CommandAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<TempFileHolder>
{
public:
virtual ~TempFileHolder() override;

void emplace(const std::filesystem::path& p);

private:
std::vector<std::filesystem::path> files_;
};

class CommandAction
{
public:
Expand All @@ -24,8 +36,6 @@ class CommandAction
};

public:
~CommandAction();

bool run(const MAA_RES_NS::Action::CommandParam& command, const Runtime& runtime);

private:
Expand All @@ -37,8 +47,6 @@ class CommandAction
std::string get_resource_dir(const Runtime& runtime);

private:
static std::vector<std::filesystem::path> cached_images_;

std::string image_path_;
};

Expand Down

0 comments on commit 7e1ab67

Please sign in to comment.