From bff478df23179fe609c0b253408a7c126e44296b Mon Sep 17 00:00:00 2001 From: MistEO Date: Tue, 6 Feb 2024 19:36:42 +0800 Subject: [PATCH 01/12] feat: init of PiCLI --- sample/interface.json | 6 +- source/CMakeLists.txt | 3 +- source/MaaProjectInterfaceCli/CMakeLists.txt | 13 ++++ .../MaaProjectInterfaceCli/configurator.cpp | 59 +++++++++++++++++++ source/MaaProjectInterfaceCli/configurator.h | 28 +++++++++ source/MaaProjectInterfaceCli/main.cpp | 15 +++++ source/ProjectInterface/Parser/Parser.cpp | 15 ++++- source/include/ProjectInterface/Parser.h | 3 +- source/include/ProjectInterface/Types.h | 32 +++++++++- 9 files changed, 166 insertions(+), 8 deletions(-) create mode 100644 source/MaaProjectInterfaceCli/CMakeLists.txt create mode 100644 source/MaaProjectInterfaceCli/configurator.cpp create mode 100644 source/MaaProjectInterfaceCli/configurator.h create mode 100644 source/MaaProjectInterfaceCli/main.cpp diff --git a/sample/interface.json b/sample/interface.json index 93194ea99..8473915d0 100644 --- a/sample/interface.json +++ b/sample/interface.json @@ -16,13 +16,13 @@ ], "entry": [ { - "task": "StartUp" + "name": "StartUp" }, { - "task": "Awards" + "name": "Awards" }, { - "task": "Combat", + "name": "Combat", "option": [ "combat_stage", "combat_times" diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index c50fab202..e61ab53b4 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -14,9 +14,10 @@ if (WITH_DBG_CONTROLLER) endif() add_subdirectory(LibraryHolder) -add_subdirectory(ProjectInterface) add_subdirectory(MaaFramework) add_subdirectory(MaaToolkit) +add_subdirectory(ProjectInterface) +add_subdirectory(MaaProjectInterfaceCli) if(WITH_GRPC) add_subdirectory(MaaRpc) diff --git a/source/MaaProjectInterfaceCli/CMakeLists.txt b/source/MaaProjectInterfaceCli/CMakeLists.txt new file mode 100644 index 000000000..78382d78b --- /dev/null +++ b/source/MaaProjectInterfaceCli/CMakeLists.txt @@ -0,0 +1,13 @@ +file(GLOB_RECURSE maa_pi_cli_src *.h *.hpp *.cpp) +file(GLOB_RECURSE maa_pi_cli_header ../include/MaaPiCli/*) + +add_executable(MaaPiCli ${maa_pi_cli_src} ${maa_pi_cli_header}) + +target_include_directories(MaaPiCli + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/../../include) + +target_link_libraries(MaaPiCli MaaUtils HeaderOnlyLibraries Boost::system) + +add_dependencies(MaaPiCli ProjectInterface MaaUtils MaaFramework MaaToolkit) + +source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${maa_pi_cli_src}) diff --git a/source/MaaProjectInterfaceCli/configurator.cpp b/source/MaaProjectInterfaceCli/configurator.cpp new file mode 100644 index 000000000..231bec474 --- /dev/null +++ b/source/MaaProjectInterfaceCli/configurator.cpp @@ -0,0 +1,59 @@ +#include "configurator.h" + +#include + +#include "ProjectInterface/Parser.h" + +using namespace MAA_PROJECT_INTERFACE_NS; + +bool Configurator::load(const std::filesystem::path& project_dir) +{ + auto data_opt = Parser::parse_interface(project_dir / kInterfaceFilename); + if (!data_opt) { + return false; + } + data_ = *data_opt; + + Configuration config; + auto cfg_json_opt = json::open(project_dir / kConfigFilename); + if (cfg_json_opt) { + if (auto cfg_opt = Parser::parse_config(*cfg_json_opt)) { + config = *cfg_opt; + } + } + + project_dir_ = project_dir; + return true; +} + +void Configurator::save() +{ + std::ofstream(project_dir_ / kInterfaceFilename) << config_.to_json(); +} + +void Configurator::check_config() +{ + auto resource_iter = + std::ranges::find_if(data_.resource, [&](const auto& resource) { return resource.name == config_.resource; }); + + if (resource_iter == data_.resource.end()) { + select_resource(); + } + else { + runtime_.resource_path = resource_iter->path; + } + + for (const auto& config_task : config_.task) {} + + runtime_.executor = data_.executor; +} + +void Configurator::select_resource() +{ + Resource res; + + // TODO + + config_.resource = res.name; + runtime_.resource_path = res.path; +} diff --git a/source/MaaProjectInterfaceCli/configurator.h b/source/MaaProjectInterfaceCli/configurator.h new file mode 100644 index 000000000..ba484831f --- /dev/null +++ b/source/MaaProjectInterfaceCli/configurator.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include + +#include "ProjectInterface/Types.h" + +class Configurator +{ + static constexpr std::string_view kInterfaceFilename = "interface.json"; + static constexpr std::string_view kConfigFilename = "config/config.json"; + +public: + bool load(const std::filesystem::path& project_dir); + void save(); + + void check_config(); + void select_resource(); + + const auto& get_runtime() const { return runtime_; } + +private: + std::filesystem::path project_dir_; + + MAA_PROJECT_INTERFACE_NS::InterfaceData data_; + MAA_PROJECT_INTERFACE_NS::Configuration config_; + MAA_PROJECT_INTERFACE_NS::RuntimeParam runtime_; +}; diff --git a/source/MaaProjectInterfaceCli/main.cpp b/source/MaaProjectInterfaceCli/main.cpp new file mode 100644 index 000000000..ccff0da05 --- /dev/null +++ b/source/MaaProjectInterfaceCli/main.cpp @@ -0,0 +1,15 @@ +#include + +#include "Configurator.h" +#include "Utils/Platform.h" + +int main(int argc, char** argv) +{ + auto app_path = MaaNS::path(argv[0]); + auto project_dir = app_path.parent_path(); + + Configurator config; + config.load(project_dir); + + return 0; +} diff --git a/source/ProjectInterface/Parser/Parser.cpp b/source/ProjectInterface/Parser/Parser.cpp index bda4b97e9..4fdd5e907 100644 --- a/source/ProjectInterface/Parser/Parser.cpp +++ b/source/ProjectInterface/Parser/Parser.cpp @@ -4,7 +4,7 @@ MAA_PROJECT_INTERFACE_NS_BEGIN -std::optional Parser::parse(const std::filesystem::path& path) +std::optional Parser::parse_interface(const std::filesystem::path& path) { LogFunc << VAR(path); @@ -24,4 +24,17 @@ std::optional Parser::parse(const std::filesystem::path& path) return root.as(); } +std::optional Parser::parse_config(const json::value& json) +{ + LogFunc << VAR(json); + + std::string error_key; + if (!Configuration().check_json(json, error_key)) { + LogError << "json is not a Configuration" << VAR(error_key) << VAR(json); + return std::nullopt; + } + + return json.as(); +} + MAA_PROJECT_INTERFACE_NS_END diff --git a/source/include/ProjectInterface/Parser.h b/source/include/ProjectInterface/Parser.h index 3cc71c7e5..2a081c235 100644 --- a/source/include/ProjectInterface/Parser.h +++ b/source/include/ProjectInterface/Parser.h @@ -12,7 +12,8 @@ MAA_PROJECT_INTERFACE_NS_BEGIN class Parser { public: - static std::optional parse(const std::filesystem::path& path); + static std::optional parse_interface(const std::filesystem::path& path); + static std::optional parse_config(const json::value& json); }; MAA_PROJECT_INTERFACE_NS_END diff --git a/source/include/ProjectInterface/Types.h b/source/include/ProjectInterface/Types.h index 86f5276d1..d841aa713 100644 --- a/source/include/ProjectInterface/Types.h +++ b/source/include/ProjectInterface/Types.h @@ -2,6 +2,7 @@ #include #include +#include #include @@ -37,11 +38,10 @@ struct Option struct Entry { std::string name; - std::string task; json::value param; std::vector