chore: Auto update by pre-commit hooks [skip changelog]

This commit is contained in:
pre-commit-ci[bot]
2024-10-22 08:40:20 +00:00
parent 26d7349722
commit ce02c80498
261 changed files with 7746 additions and 7012 deletions

View File

@@ -21,6 +21,7 @@ const std::unordered_set<std::string>& asst::TaskData::get_templ_required() cons
{
return m_templ_required;
}
asst::TaskDerivedConstPtr asst::TaskData::get_raw(std::string_view name)
{
if (!generate_raw_task_and_base(name, true)) [[unlikely]] {
@@ -127,40 +128,42 @@ bool asst::TaskData::lazy_parse(const json::value& json)
validity = false;
continue;
}
auto check_tasklist = [&](const TaskList& task_list, std::string_view list_type,
bool enable_justreturn_check = false) {
std::unordered_set<std::string_view> tasks_set {};
std::string justreturn_task_name = "";
for (const std::string& task_name : task_list) {
if (tasks_set.contains(task_name)) [[unlikely]] {
continue;
}
// 检查是否有 JustReturn 任务不是最后一个任务
if (enable_justreturn_check && !justreturn_task_name.empty()) [[unlikely]] {
Log.error((std::string(name) += "->") += list_type,
"has a not-final JustReturn task:", justreturn_task_name);
justreturn_task_name = "";
validity = false;
auto check_tasklist =
[&](const TaskList& task_list, std::string_view list_type, bool enable_justreturn_check = false) {
std::unordered_set<std::string_view> tasks_set {};
std::string justreturn_task_name = "";
for (const std::string& task_name : task_list) {
if (tasks_set.contains(task_name)) [[unlikely]] {
continue;
}
// 检查是否有 JustReturn 任务不是最后一个任务
if (enable_justreturn_check && !justreturn_task_name.empty()) [[unlikely]] {
Log.error(
(std::string(name) += "->") += list_type,
"has a not-final JustReturn task:",
justreturn_task_name);
justreturn_task_name = "";
validity = false;
}
if (auto ptr = get(task_name); ptr == nullptr) [[unlikely]] {
Log.error(task_name, "in", (std::string(name) += "->") += list_type, "is null");
validity = false;
continue;
}
else if (ptr->algorithm == AlgorithmType::JustReturn) {
justreturn_task_name = ptr->name;
}
if (!checking_task_set.contains(task_name)) {
task_queue.emplace(task_name_view(task_name));
checking_task_set.emplace(task_name_view(task_name));
}
tasks_set.emplace(task_name_view(task_name));
}
if (auto ptr = get(task_name); ptr == nullptr) [[unlikely]] {
Log.error(task_name, "in", (std::string(name) += "->") += list_type, "is null");
validity = false;
continue;
}
else if (ptr->algorithm == AlgorithmType::JustReturn) {
justreturn_task_name = ptr->name;
}
if (!checking_task_set.contains(task_name)) {
task_queue.emplace(task_name_view(task_name));
checking_task_set.emplace(task_name_view(task_name));
}
tasks_set.emplace(task_name_view(task_name));
}
return true;
};
return true;
};
bool enable_justreturn_check = true;
if (name.ends_with("LoadingText") || name.ends_with("LoadingIcon")) {
// 放宽对 Loading 类任务的限制,不然 JustReturn 的任务如果本身有 JR 的 next就不能 @Loading 了
@@ -191,7 +194,9 @@ bool asst::TaskData::lazy_parse(const json::value& json)
Log.trace(checking_task_set.size(), "tasks checked.");
}
clear_tasks();
if (!validity) return false;
if (!validity) {
return false;
}
}
#endif
@@ -202,7 +207,9 @@ bool asst::TaskData::parse(const json::value& json)
{
LogTraceFunction;
if (!lazy_parse(json)) return false;
if (!lazy_parse(json)) {
return false;
}
// 本来重构之后完全支持惰性加载,但是发现模板图片不支持(
for (std::string_view name : m_json_all_tasks_info | views::keys) {
@@ -229,8 +236,12 @@ void asst::TaskData::set_task_base(const std::string_view task_name, std::string
clear_tasks();
}
bool asst::TaskData::generate_raw_task_info(std::string_view name, std::string_view prefix, std::string_view base,
const json::value& json, TaskDerivedType type)
bool asst::TaskData::generate_raw_task_info(
std::string_view name,
std::string_view prefix,
std::string_view base,
const json::value& json,
TaskDerivedType type)
{
TaskPipelineConstPtr base_task = base.empty() ? nullptr : get_raw(base);
if (base_task == nullptr) {
@@ -243,16 +254,21 @@ bool asst::TaskData::generate_raw_task_info(std::string_view name, std::string_v
task->type = type;
task->base = base;
task->prefix = prefix;
utils::get_and_check_value_or(name, json, "next", task->next,
[&]() { return append_prefix(base_task->next, prefix); });
utils::get_and_check_value_or(name, json, "sub", task->sub,
[&]() { return append_prefix(base_task->sub, prefix); });
utils::get_and_check_value_or(name, json, "exceededNext", task->exceeded_next,
[&]() { return append_prefix(base_task->exceeded_next, prefix); });
utils::get_and_check_value_or(name, json, "onErrorNext", task->on_error_next,
[&]() { return append_prefix(base_task->on_error_next, prefix); });
utils::get_and_check_value_or(name, json, "reduceOtherTimes", task->reduce_other_times,
[&]() { return append_prefix(base_task->reduce_other_times, prefix); });
utils::get_and_check_value_or(name, json, "next", task->next, [&]() {
return append_prefix(base_task->next, prefix);
});
utils::get_and_check_value_or(name, json, "sub", task->sub, [&]() {
return append_prefix(base_task->sub, prefix);
});
utils::get_and_check_value_or(name, json, "exceededNext", task->exceeded_next, [&]() {
return append_prefix(base_task->exceeded_next, prefix);
});
utils::get_and_check_value_or(name, json, "onErrorNext", task->on_error_next, [&]() {
return append_prefix(base_task->on_error_next, prefix);
});
utils::get_and_check_value_or(name, json, "reduceOtherTimes", task->reduce_other_times, [&]() {
return append_prefix(base_task->reduce_other_times, prefix);
});
m_task_status[task_name_view(name)] = NotToBeGenerate;
// 保存虚任务未展开的任务
@@ -431,8 +447,8 @@ asst::TaskPtr asst::TaskData::generate_task_info(std::string_view name)
&& algorithm != asst::AlgorithmType::JustReturn // 非 JustReturn
&& !task->next.empty() // 有 next
&& (algorithm != asst::AlgorithmType::MatchTemplate // template 不是 empty
|| std::dynamic_pointer_cast<const MatchTaskInfo>(task)->templ_names
!= std::vector<std::string> { "empty.png" })) {
|| std::dynamic_pointer_cast<const MatchTaskInfo>(task)->templ_names !=
std::vector<std::string> { "empty.png" })) {
// 符合上述条件时,我们认为此时的隐式全屏 roi 不是期望行为,给个警告
Log.warn("Task", name, "has implicit fullscreen roi.");
}
@@ -448,8 +464,11 @@ asst::TaskPtr asst::TaskData::generate_task_info(std::string_view name)
return task;
}
asst::TaskPtr asst::TaskData::generate_match_task_info(std::string_view name, const json::value& task_json,
MatchTaskConstPtr default_ptr, TaskDerivedType derived_type)
asst::TaskPtr asst::TaskData::generate_match_task_info(
std::string_view name,
const json::value& task_json,
MatchTaskConstPtr default_ptr,
TaskDerivedType derived_type)
{
if (default_ptr == nullptr) {
default_ptr = default_match_task_info_ptr;
@@ -470,17 +489,20 @@ asst::TaskPtr asst::TaskData::generate_match_task_info(std::string_view name, co
auto threshold_opt = task_json.find("templThreshold");
if (!threshold_opt) {
match_task_info_ptr->templ_thresholds = default_ptr->templ_thresholds;
match_task_info_ptr->templ_thresholds.resize(match_task_info_ptr->templ_names.size(),
default_ptr->templ_thresholds.back());
match_task_info_ptr->templ_thresholds.resize(
match_task_info_ptr->templ_names.size(),
default_ptr->templ_thresholds.back());
}
else if (threshold_opt->is_number()) {
// 单个数值时,所有模板都使用这个阈值
match_task_info_ptr->templ_thresholds.resize(match_task_info_ptr->templ_names.size(),
threshold_opt->as_double());
match_task_info_ptr->templ_thresholds.resize(
match_task_info_ptr->templ_names.size(),
threshold_opt->as_double());
}
else if (threshold_opt->is_array()) {
ranges::copy(threshold_opt->as_array() | views::transform(&json::value::as_double),
std::back_inserter(match_task_info_ptr->templ_thresholds));
ranges::copy(
threshold_opt->as_array() | views::transform(&json::value::as_double),
std::back_inserter(match_task_info_ptr->templ_thresholds));
}
else {
Log.error("Invalid templThreshold type in task", name);
@@ -500,19 +522,18 @@ asst::TaskPtr asst::TaskData::generate_match_task_info(std::string_view name, co
auto method_opt = task_json.find("method");
if (!method_opt) {
match_task_info_ptr->methods = default_ptr->methods;
match_task_info_ptr->methods.resize(match_task_info_ptr->templ_names.size(),
default_ptr->methods.back());
match_task_info_ptr->methods.resize(match_task_info_ptr->templ_names.size(), default_ptr->methods.back());
}
else if (method_opt->is_string()) {
// 单个数值时,所有模板都使用这个阈值
match_task_info_ptr->methods.resize(match_task_info_ptr->templ_names.size(),
get_match_method(method_opt->as_string()));
match_task_info_ptr->methods.resize(
match_task_info_ptr->templ_names.size(),
get_match_method(method_opt->as_string()));
}
else if (method_opt->is_array()) {
ranges::copy(method_opt->as_array() |
views::transform(&json::value::as_string) |
views::transform(&get_match_method),
std::back_inserter(match_task_info_ptr->methods));
ranges::copy(
method_opt->as_array() | views::transform(&json::value::as_string) | views::transform(&get_match_method),
std::back_inserter(match_task_info_ptr->methods));
}
else {
Log.error("Invalid method type in task", name);
@@ -580,7 +601,10 @@ asst::TaskPtr asst::TaskData::generate_match_task_info(std::string_view name, co
}
const auto& color_range = color_array_item.as_array();
if (color_range.size() != 2) { // lower & upper, 2 elements
Log.error("Invalid color_range in task", name, ", should have 2 elements (lower & upper) in each array");
Log.error(
"Invalid color_range in task",
name,
", should have 2 elements (lower & upper) in each array");
return nullptr;
}
@@ -639,8 +663,10 @@ asst::TaskPtr asst::TaskData::generate_match_task_info(std::string_view name, co
return match_task_info_ptr;
}
asst::TaskPtr asst::TaskData::generate_ocr_task_info(std::string_view name, const json::value& task_json,
OcrTaskConstPtr default_ptr)
asst::TaskPtr asst::TaskData::generate_ocr_task_info(
std::string_view name,
const json::value& task_json,
OcrTaskConstPtr default_ptr)
{
if (default_ptr == nullptr) {
default_ptr = default_ocr_task_info_ptr;
@@ -657,14 +683,31 @@ asst::TaskPtr asst::TaskData::generate_ocr_task_info(std::string_view name, cons
#endif
utils::get_and_check_value_or(name, task_json, "fullMatch", ocr_task_info_ptr->full_match, default_ptr->full_match);
utils::get_and_check_value_or(name, task_json, "isAscii", ocr_task_info_ptr->is_ascii, default_ptr->is_ascii);
utils::get_and_check_value_or(name, task_json, "withoutDet", ocr_task_info_ptr->without_det, default_ptr->without_det);
utils::get_and_check_value_or(name, task_json, "replaceFull", ocr_task_info_ptr->replace_full, default_ptr->replace_full);
utils::get_and_check_value_or(name, task_json, "ocrReplace", ocr_task_info_ptr->replace_map, default_ptr->replace_map);
utils::get_and_check_value_or(
name,
task_json,
"withoutDet",
ocr_task_info_ptr->without_det,
default_ptr->without_det);
utils::get_and_check_value_or(
name,
task_json,
"replaceFull",
ocr_task_info_ptr->replace_full,
default_ptr->replace_full);
utils::get_and_check_value_or(
name,
task_json,
"ocrReplace",
ocr_task_info_ptr->replace_map,
default_ptr->replace_map);
return ocr_task_info_ptr;
}
asst::ResultOrError<asst::TaskData::RawCompileResult> asst::TaskData::compile_raw_tasklist(
const TaskList& raw_tasks, std::string_view self_name, std::function<TaskDerivedConstPtr(std::string_view)> get_raw,
const TaskList& raw_tasks,
std::string_view self_name,
std::function<TaskDerivedConstPtr(std::string_view)> get_raw,
bool allow_duplicate)
{
RawCompileResult ret { .task_changed = false, .symbols = {} };
@@ -687,7 +730,10 @@ asst::ResultOrError<asst::TaskData::RawCompileResult> asst::TaskData::compile_ra
auto opt = symbol_stream.decode(
[&](const TaskDataSymbol& symbol, const TaskDataSymbol& prefix) -> TaskDataSymbol::SymbolsOrError {
return TaskDataSymbol::append_prefix(
symbol, prefix, self_name, get_raw,
symbol,
prefix,
self_name,
get_raw,
[&](const TaskList& raw_or_empty) -> TaskDataSymbol::SymbolsOrError {
if (auto opt = compile_raw_tasklist(raw_or_empty, self_name, get_raw, allow_duplicate)) {
return opt.value().symbols;
@@ -716,14 +762,16 @@ asst::ResultOrError<asst::TaskData::RawCompileResult> asst::TaskData::compile_ra
return ret;
}
asst::ResultOrError<asst::TaskData::CompileResult> asst::TaskData::compile_tasklist(const TaskList& raw_tasks,
std::string_view self_name,
bool allow_duplicate)
asst::ResultOrError<asst::TaskData::CompileResult>
asst::TaskData::compile_tasklist(const TaskList& raw_tasks, std::string_view self_name, bool allow_duplicate)
{
CompileResult ret { .task_changed = false, .tasks = {} };
std::vector<TaskDataSymbol> new_symbols;
if (auto opt = compile_raw_tasklist(
raw_tasks, self_name, [&](std::string_view name) { return get_raw(name); }, allow_duplicate);
raw_tasks,
self_name,
[&](std::string_view name) { return get_raw(name); },
allow_duplicate);
!opt) {
return { std::nullopt, std::move(opt.error()) };
}