From 2ef2fc92c57c2836d654cc016fb6a61a0e6782e0 Mon Sep 17 00:00:00 2001 From: MistEO Date: Tue, 6 Feb 2024 00:13:27 +0800 Subject: [PATCH] refactor: ProjectInterface Parser --- source/CMakeLists.txt | 2 +- source/ProjectInterface/CMakeLists.txt | 13 ++ source/ProjectInterface/Parser/Parser.cpp | 27 ++++ source/TaskAgent/CMakeLists.txt | 13 -- source/TaskAgent/TaskAgentParser.cpp | 151 ------------------ source/include/Conf/Conf.h | 8 +- source/include/ProjectInterface/Parser.h | 18 +++ .../Types.h} | 31 ++-- source/include/TaskAgent/TaskAgentParser.h | 26 --- 9 files changed, 85 insertions(+), 204 deletions(-) create mode 100644 source/ProjectInterface/CMakeLists.txt create mode 100644 source/ProjectInterface/Parser/Parser.cpp delete mode 100644 source/TaskAgent/CMakeLists.txt delete mode 100644 source/TaskAgent/TaskAgentParser.cpp create mode 100644 source/include/ProjectInterface/Parser.h rename source/include/{TaskAgent/TaskAgentTypes.h => ProjectInterface/Types.h} (57%) delete mode 100644 source/include/TaskAgent/TaskAgentParser.h diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 2501bccfc..c50fab202 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -14,7 +14,7 @@ if (WITH_DBG_CONTROLLER) endif() add_subdirectory(LibraryHolder) -add_subdirectory(TaskAgent) +add_subdirectory(ProjectInterface) add_subdirectory(MaaFramework) add_subdirectory(MaaToolkit) diff --git a/source/ProjectInterface/CMakeLists.txt b/source/ProjectInterface/CMakeLists.txt new file mode 100644 index 000000000..eb59f9733 --- /dev/null +++ b/source/ProjectInterface/CMakeLists.txt @@ -0,0 +1,13 @@ +file(GLOB_RECURSE project_interface_src *.h *.hpp *.cpp) +file(GLOB_RECURSE project_interface_header ../include/ProjectInterface/*) + +add_library(ProjectInterface STATIC ${project_interface_src} ${project_interface_header}) + +target_include_directories(ProjectInterface + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/../../include) + +target_link_libraries(ProjectInterface MaaUtils HeaderOnlyLibraries Boost::system) + +add_dependencies(ProjectInterface MaaUtils) + +source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${project_interface_src}) diff --git a/source/ProjectInterface/Parser/Parser.cpp b/source/ProjectInterface/Parser/Parser.cpp new file mode 100644 index 000000000..bda4b97e9 --- /dev/null +++ b/source/ProjectInterface/Parser/Parser.cpp @@ -0,0 +1,27 @@ +#include "ProjectInterface/Parser.h" + +#include "Utils/Logger.h" + +MAA_PROJECT_INTERFACE_NS_BEGIN + +std::optional Parser::parse(const std::filesystem::path& path) +{ + LogFunc << VAR(path); + + auto root_opt = json::open(path); + if (!root_opt) { + LogError << "failed to parse" << path; + return std::nullopt; + } + const json::value& root = *root_opt; + + std::string error_key; + if (!InterfaceData().check_json(root, error_key)) { + LogError << "json is not an InterfaceData" << VAR(error_key) << VAR(root); + return std::nullopt; + } + + return root.as(); +} + +MAA_PROJECT_INTERFACE_NS_END diff --git a/source/TaskAgent/CMakeLists.txt b/source/TaskAgent/CMakeLists.txt deleted file mode 100644 index c4643050b..000000000 --- a/source/TaskAgent/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -file(GLOB_RECURSE task_agent_src *.h *.hpp *.cpp) -file(GLOB_RECURSE task_agent_header ../include/TaskAgent/*) - -add_library(TaskAgent STATIC ${task_agent_src} ${task_agent_header}) - -target_include_directories(TaskAgent - PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/../../include) - -target_link_libraries(TaskAgent MaaUtils HeaderOnlyLibraries) - -add_dependencies(TaskAgent MaaUtils) - -source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${task_agent_src}) diff --git a/source/TaskAgent/TaskAgentParser.cpp b/source/TaskAgent/TaskAgentParser.cpp deleted file mode 100644 index 670bdf8be..000000000 --- a/source/TaskAgent/TaskAgentParser.cpp +++ /dev/null @@ -1,151 +0,0 @@ -#include "TaskAgent/TaskAgentParser.h" - -#include "Utils/Logger.h" - -MAA_TASKAGENT_NS_BEGIN - -std::optional Parser::parse(const std::filesystem::path& path) -{ - LogFunc << VAR(path); - - auto root_opt = json::open(path); - if (!root_opt) { - LogError << "failed to parse" << path; - return std::nullopt; - } - const json::value& root = *root_opt; - - AgentData result; - - if (auto resource_list_opt = root.find("resource")) { - for (auto& jresource : *resource_list_opt) { - auto resource_opt = parse_resource(jresource); - if (!resource_opt) { - LogError << "failed to parse" << VAR(jresource); - return std::nullopt; - } - result.resource.emplace_back(*std::move(resource_opt)); - } - } - - std::unordered_map options; - - if (auto option_list_opt = root.find("option")) { - for (auto& joption : *option_list_opt) { - auto option_opt = parse_option(joption); - if (!option_opt) { - LogError << "failed to parse" << VAR(joption); - return std::nullopt; - } - std::string name = option_opt->name; - options.insert_or_assign(std::move(name), *std::move(option_opt)); - } - } - - if (auto entry_list_opt = root.find("entry")) { - for (auto& jentry : *entry_list_opt) { - auto entry_opt = parse_entry(jentry, options); - if (!entry_opt) { - LogError << "failed to parse" << VAR(jentry); - return std::nullopt; - } - result.entry.emplace_back(*std::move(entry_opt)); - } - } - - if (auto executor_list_opt = root.find("executor")) { - for (auto& jexecutor : *executor_list_opt) { - auto executor_opt = parse_executor(jexecutor); - if (!executor_opt) { - LogError << "failed to parse" << VAR(jexecutor); - return std::nullopt; - } - result.executor.emplace_back(*std::move(executor_opt)); - } - } - - return result; -} - -std::optional Parser::parse_resource(const json::value& input) -{ - Resource result; - - auto name_opt = input.find("name"); - if (!name_opt) { - LogError << "\"name\" not found or not a string" << VAR(input); - return std::nullopt; - } - result.name = *name_opt; - - auto path_opt = input.find("path"); - if (!path_opt) { - LogError << "\"path\" not found or not an array" << VAR(input); - return std::nullopt; - } - if (!path_opt->all()) { - LogError << "\"path\" not all string" << VAR(input); - return std::nullopt; - } - result.path = path_opt->to_vector(); - - return result; -} - -std::optional