Skip to content

Commit 7e1ab67

Browse files
committed
fix: temp file error
1 parent 0eab5ba commit 7e1ab67

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

source/MaaFramework/Task/Component/Actuator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ bool Actuator::command(
244244
.image = controller()->cached_image(),
245245
.box = box,
246246
};
247-
return CommandAction::get_instance().run(param, rt);
247+
return CommandAction().run(param, rt);
248248
}
249249

250250
bool Actuator::custom_action(const MAA_RES_NS::Action::CustomParam& param, const cv::Rect& box, MaaRecoId reco_id, const std::string& name)

source/MaaFramework/Task/Component/CommandAction.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
MAA_TASK_NS_BEGIN
1414

15-
CommandAction::~CommandAction()
15+
TempFileHolder::~TempFileHolder()
1616
{
1717
LogFunc;
1818

19-
for (const auto& p : cached_images_) {
19+
for (const auto& p : files_) {
2020
if (!std::filesystem::exists(p)) {
2121
continue;
2222
}
@@ -25,6 +25,12 @@ CommandAction::~CommandAction()
2525
}
2626
}
2727

28+
void TempFileHolder::emplace(const std::filesystem::path& p)
29+
{
30+
LogTrace << p;
31+
files_.emplace_back(p);
32+
}
33+
2834
bool CommandAction::run(const MAA_RES_NS::Action::CommandParam& command, const Runtime& runtime)
2935
{
3036
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
4450
if (src.find(key) == std::string::npos) {
4551
continue;
4652
}
47-
string_replace_all(dst, key, func(runtime));
53+
dst = string_replace_all(dst, key, func(runtime));
4854
}
4955
return dst;
5056
};
@@ -101,7 +107,7 @@ std::string CommandAction::get_image_path(const Runtime& runtime)
101107
}
102108

103109
auto dst_path = std::filesystem::temp_directory_path() / (format_now_for_filename() + ".png");
104-
cached_images_.emplace_back(dst_path);
110+
TempFileHolder::get_instance().emplace(dst_path);
105111
imwrite(dst_path, runtime.image);
106112
image_path_ = path_to_utf8_string(dst_path);
107113
return image_path_;

source/MaaFramework/Task/Component/CommandAction.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@
77
#include "Conf/Conf.h"
88
#include "Resource/PipelineTypes.h"
99
#include "Utils/Platform.h"
10+
#include "Utils/SingletonHolder.hpp"
1011

1112
MAA_TASK_NS_BEGIN
1213

14+
class TempFileHolder : public SingletonHolder<TempFileHolder>
15+
{
16+
public:
17+
virtual ~TempFileHolder() override;
18+
19+
void emplace(const std::filesystem::path& p);
20+
21+
private:
22+
std::vector<std::filesystem::path> files_;
23+
};
24+
1325
class CommandAction
1426
{
1527
public:
@@ -24,8 +36,6 @@ class CommandAction
2436
};
2537

2638
public:
27-
~CommandAction();
28-
2939
bool run(const MAA_RES_NS::Action::CommandParam& command, const Runtime& runtime);
3040

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

3949
private:
40-
static std::vector<std::filesystem::path> cached_images_;
41-
4250
std::string image_path_;
4351
};
4452

0 commit comments

Comments
 (0)