diff --git a/src/MaaCore/Task/AbstractTask.cpp b/src/MaaCore/Task/AbstractTask.cpp index 1f8409e37c..5a37c5f389 100644 --- a/src/MaaCore/Task/AbstractTask.cpp +++ b/src/MaaCore/Task/AbstractTask.cpp @@ -158,7 +158,64 @@ bool asst::AbstractTask::save_img(const std::filesystem::path& relative_dir) return false; } std::string stem = utils::get_time_filestem(); + + // 第一次或每执行100次后执行清理 + if (m_save_file_cnt[relative_dir] == 0) { + std::thread t([=]() { + filenum_ctrl(relative_dir, 1000); + }); + t.detach(); + + m_save_file_cnt[relative_dir] = 0; + } + m_save_file_cnt[relative_dir] = (m_save_file_cnt[relative_dir] + 1) % 100; + auto relative_path = relative_dir / (stem + "_raw.png"); Log.trace("Save image", relative_path); return asst::imwrite(relative_path, image); -} \ No newline at end of file +} + +size_t asst::AbstractTask::filenum_ctrl(const std::filesystem::path& relative_dir, size_t max_files) +{ + std::filesystem::path absolute_path; + if (relative_dir.is_relative()) [[likely]] { + const auto& user_dir = UserDir.get(); + absolute_path = user_dir / relative_dir; + } + else { + absolute_path = relative_dir; + } + if (!std::filesystem::exists(absolute_path)) { + return 0; + } + + size_t file_nums = 0; + std::vector> filepaths; + std::filesystem::directory_iterator iter(absolute_path); + for (auto& file : iter) { + if (file.is_regular_file()) { + ++file_nums; + filepaths.emplace_back(last_write_time(file.path()), file.path()); + } + } + + std::sort(filepaths.begin(), filepaths.end(), + [](const std::pair& a, + const std::pair& b) { + if (a.first != b.first) return a.first < b.first; + return a.second < b.second; + }); + + long long to_del = file_nums - max_files; + size_t deleted = 0; + + for (int i = 0; i < to_del; ++i) { + auto path = filepaths[i].second; + std::cout << filepaths[i].first.time_since_epoch().count() << path << std::endl; + if (std::filesystem::remove(path)) { + deleted++; + } + } + + return deleted; +} diff --git a/src/MaaCore/Task/AbstractTask.h b/src/MaaCore/Task/AbstractTask.h index 58d6fb1523..5dc49753f2 100644 --- a/src/MaaCore/Task/AbstractTask.h +++ b/src/MaaCore/Task/AbstractTask.h @@ -63,6 +63,7 @@ namespace asst virtual void callback(AsstMsg msg, const json::value& detail); virtual void click_return_button(); bool save_img(const std::filesystem::path& relative_dir = utils::path("debug")); + size_t filenum_ctrl(const std::filesystem::path& relative_dir, size_t max_files = 1000); json::value basic_info_with_what(std::string what) const; @@ -76,5 +77,6 @@ namespace asst mutable json::value m_basic_info_cache; int m_task_id = 0; std::set m_plugins; + std::map m_save_file_cnt; }; } // namespace asst