Skip to content

Commit

Permalink
fix: 单次 Load 不再允许同名 task
Browse files Browse the repository at this point in the history
fix #63
  • Loading branch information
MistEO committed Oct 10, 2023
1 parent d4e532a commit 495c7fb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
44 changes: 35 additions & 9 deletions source/MaaFramework/Resource/PipelineResMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ bool PipelineResMgr::load(const std::filesystem::path& path, bool is_base)
if (is_base) {
clear();
}

paths_.emplace_back(path);

bool loaded = load_all_json(path);
loaded &= check_all_next_list();
if (!load_all_json(path)) {
LogError << "load_all_json failed" << VAR(path);
return false;
}

return loaded;
if (!check_all_next_list()) {
LogError << "check_all_next_list failed" << VAR(path);
return false;
}

return true;
}

void PipelineResMgr::clear()
Expand Down Expand Up @@ -50,36 +58,45 @@ bool PipelineResMgr::load_all_json(const std::filesystem::path& path)
return false;
}

bool loaded = false;
if (!std::filesystem::is_directory(path)) {
LogError << "path is not directory" << VAR(path);
return false;
}

bool loaded = false;

std::set<std::string> existing_keys;
for (auto& entry : std::filesystem::recursive_directory_iterator(path)) {
auto& entry_path = entry.path();
if (entry.is_directory()) {
LogDebug << "entry is directory" << VAR(entry_path);
continue;
}
if (!entry.is_regular_file()) {
LogWarn << "entry is not regular file, skip" << VAR(entry_path);
continue;
}

auto ext = path_to_utf8_string(entry_path.extension());
tolowers_(ext);
if (ext != ".json") {
LogWarn << "entry is not *.json, skip" << VAR(entry_path) << VAR(ext);
continue;
}

loaded &= open_and_parse_file(entry_path);
if (!loaded) {
bool parsed = open_and_parse_file(entry_path, existing_keys);
if (!parsed) {
LogError << "open_and_parse_file failed" << VAR(entry_path);
return false;
}

loaded = true;
}

return loaded;
}

bool PipelineResMgr::open_and_parse_file(const std::filesystem::path& path)
bool PipelineResMgr::open_and_parse_file(const std::filesystem::path& path, std::set<std::string>& existing_keys)
{
LogFunc << VAR(path);

Expand All @@ -88,9 +105,11 @@ bool PipelineResMgr::open_and_parse_file(const std::filesystem::path& path)
LogError << "json::open failed" << VAR(path);
return false;
}
const auto& json = *json_opt;

TaskDataMap cur_data_map;
if (!parse_config(*json_opt, cur_data_map, task_data_map_)) {
if (!parse_config(json, cur_data_map, existing_keys, task_data_map_)) {
LogError << "parse_config failed" << VAR(path) << VAR(json);
return false;
}

Expand Down Expand Up @@ -126,7 +145,8 @@ bool PipelineResMgr::check_next_list(const TaskData::NextList& next_list) const
return true;
}

bool PipelineResMgr::parse_config(const json::value& input, TaskDataMap& output, const TaskDataMap& default_value)
bool PipelineResMgr::parse_config(const json::value& input, TaskDataMap& output, std::set<std::string>& existing_keys,
const TaskDataMap& default_value)
{
if (!input.is_object()) {
LogError << "json is not object";
Expand All @@ -136,6 +156,11 @@ bool PipelineResMgr::parse_config(const json::value& input, TaskDataMap& output,
TaskDataMap data_map;

for (const auto& [key, value] : input.as_object()) {
if (existing_keys.contains(key)) {
LogError << "key already exists" << VAR(key);
return false;
}

TaskData task_data;
const auto& default_task_data = default_value.contains(key) ? default_value.at(key) : TaskData {};
bool ret = parse_task(key, value, task_data, default_task_data);
Expand All @@ -144,6 +169,7 @@ bool PipelineResMgr::parse_config(const json::value& input, TaskDataMap& output,
return false;
}
data_map.insert_or_assign(key, task_data);
existing_keys.emplace(key);
}

output = std::move(data_map);
Expand Down
6 changes: 4 additions & 2 deletions source/MaaFramework/Resource/PipelineResMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Utils/NonCopyable.hpp"

#include <filesystem>
#include <set>
#include <unordered_map>

#include <meojson/json.hpp>
Expand All @@ -27,7 +28,8 @@ class PipelineResMgr : public NonCopyable
const TaskDataMap& get_task_data_map() const { return task_data_map_; }

public:
static bool parse_config(const json::value& input, TaskDataMap& output, const TaskDataMap& default_value);
static bool parse_config(const json::value& input, TaskDataMap& output, std::set<std::string>& existing_keys,
const TaskDataMap& default_value);
static bool parse_task(const std::string& name, const json::value& input, TaskData& output,
const TaskData& default_value);

Expand Down Expand Up @@ -73,7 +75,7 @@ class PipelineResMgr : public NonCopyable

private:
bool load_all_json(const std::filesystem::path& path);
bool open_and_parse_file(const std::filesystem::path& path);
bool open_and_parse_file(const std::filesystem::path& path, std::set<std::string>& existing_keys);
bool check_all_next_list() const;
bool check_next_list(const TaskData::NextList& next_list) const;

Expand Down
3 changes: 2 additions & 1 deletion source/MaaFramework/Task/TaskDataMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ bool TaskDataMgr::set_diff_task(const json::value& input)

MAA_RES_NS::PipelineResMgr::TaskDataMap task_data_map;
auto& raw_data_map = resource()->pipeline_res().get_task_data_map();
bool parsed = MAA_RES_NS::PipelineResMgr::parse_config(input, task_data_map, raw_data_map);
std::set<std::string> existing_keys;
bool parsed = MAA_RES_NS::PipelineResMgr::parse_config(input, task_data_map, existing_keys, raw_data_map);
if (!parsed) {
LogError << "Parse json failed";
return false;
Expand Down

0 comments on commit 495c7fb

Please sign in to comment.