diff --git a/src/tools/eupak/main.cpp b/src/tools/eupak/main.cpp index 9e237e4..00cf2eb 100644 --- a/src/tools/eupak/main.cpp +++ b/src/tools/eupak/main.cpp @@ -8,44 +8,16 @@ #include #include -#include -#include -#include - -#include "europa/structs/Pak.hpp" -#include "tasks/Task.hpp" +#include int main(int argc, char** argv) { - // FIXME: At some point we should just have task classes register their arguments - // and then they will deal with all that themselves. The only thing we will do here - // is point them to themselves. - // - // The current design is really really bad for introducing new stuff. - argparse::ArgumentParser parser("eupak", EUPAK_VERSION_STR); parser.add_description("Eupak (Europa Package Multi-Tool) v" EUPAK_VERSION_STR); - - argparse::ArgumentParser extractParser("extract", EUPAK_VERSION_STR, argparse::default_arguments::help); - extractParser.add_description("Extract a package file."); - extractParser.add_argument("-d", "--directory") - .default_value("") - .metavar("DIRECTORY") - .help("Directory to extract to."); - extractParser.add_argument("input") - .help("Input archive") - .metavar("ARCHIVE"); - - extractParser.add_argument("--verbose") - .help("Increase extraction output verbosity") - .default_value(false) - .implicit_value(true); - - parser.add_subparser(extractParser); - auto tasks = std::vector { eupak::tasks::TaskFactory::CreateNamed("create", parser), - eupak::tasks::TaskFactory::CreateNamed("info", parser) + eupak::tasks::TaskFactory::CreateNamed("info", parser), + eupak::tasks::TaskFactory::CreateNamed("extract", parser), }; try { @@ -77,47 +49,5 @@ int main(int argc, char** argv) { } } -#if 0 - - if(parser.is_subcommand_used("extract")) { - eupak::tasks::ExtractTask task; - eupak::tasks::ExtractTask::Arguments args; - - args.verbose = extractParser.get("--verbose"); - args.inputPath = eupak::fs::path(extractParser.get("input")); - - if(extractParser.is_used("--directory")) { - args.outputDirectory = eupak::fs::path(extractParser.get("--directory")); - } else { - // Default to the basename appended to current path - // as a "relatively sane" default path to extract to. - // Should be okay. - args.outputDirectory = eupak::fs::current_path() / args.inputPath.stem(); - } - - std::cout << "Input PAK/PMDL: " << args.inputPath << '\n'; - std::cout << "Output Directory: " << args.outputDirectory << '\n'; - - return task.Run(std::move(args)); - } - - if(parser.is_subcommand_used("info")) { - eupak::tasks::InfoTask task; - eupak::tasks::InfoTask::Arguments args; - - - return task.Run(std::move(args)); - } - - - if(parser.is_subcommand_used("create")) { - eupak::tasks::CreateTask task; - eupak::tasks::CreateTask::Arguments args; - - - return task.Run(std::move(args)); - } -#endif - return 0; } diff --git a/src/tools/eupak/tasks/ExtractTask.cpp b/src/tools/eupak/tasks/ExtractTask.cpp index 5e3f548..30337cc 100644 --- a/src/tools/eupak/tasks/ExtractTask.cpp +++ b/src/tools/eupak/tasks/ExtractTask.cpp @@ -6,6 +6,7 @@ // SPDX-License-Identifier: MIT // +#include #include #include #include @@ -14,9 +15,66 @@ #include #include -namespace eupak::tasks { +#include "tasks/Task.hpp" + +namespace eupak::tasks { + ExtractTask::ExtractTask() + : parser("extract", EUPAK_VERSION_STR, argparse::default_arguments::help) { + // clang-format off + parser + .add_description("Extract a package file."); + parser + .add_argument("-d", "--directory") + .default_value("") + .metavar("DIRECTORY") + .help("Directory to extract to."); + + parser + .add_argument("input") + .help("Input archive") + .metavar("ARCHIVE"); + + parser + .add_argument("--verbose") + .help("Increase extraction output verbosity") + .default_value(false) + .implicit_value(true); + // clang-format on + } + + void ExtractTask::Init(argparse::ArgumentParser& parentParser) { + parentParser.add_subparser(parser); + } + + bool ExtractTask::ShouldRun(argparse::ArgumentParser& parentParser) const { + return parentParser.is_subcommand_used("extract"); + }; + + int ExtractTask::Parse() { + eupak::tasks::ExtractTask task; + eupak::tasks::ExtractTask::Arguments args; + + args.verbose = parser.get("--verbose"); + args.inputPath = eupak::fs::path(parser.get("input")); + + if(parser.is_used("--directory")) { + args.outputDirectory = eupak::fs::path(parser.get("--directory")); + } else { + // Default to the basename appended to current path + // as a "relatively sane" default path to extract to. + // Should be okay. + args.outputDirectory = eupak::fs::current_path() / args.inputPath.stem(); + } + + return 0; + } + + int ExtractTask::Run() { + const auto& args = currentArgs; + + std::cout << "Input PAK/PMDL: " << args.inputPath << '\n'; + std::cout << "Output Directory: " << args.outputDirectory << '\n'; - int ExtractTask::Run(Arguments&& args) { std::ifstream ifs(args.inputPath.string(), std::ifstream::binary); if(!ifs) { @@ -98,4 +156,5 @@ namespace eupak::tasks { return 0; } + EUPAK_REGISTER_TASK("extract", ExtractTask); } // namespace eupak::tasks \ No newline at end of file diff --git a/src/tools/eupak/tasks/ExtractTask.hpp b/src/tools/eupak/tasks/ExtractTask.hpp index 9cea06c..4a9eebc 100644 --- a/src/tools/eupak/tasks/ExtractTask.hpp +++ b/src/tools/eupak/tasks/ExtractTask.hpp @@ -11,20 +11,32 @@ #include +#include "tasks/Task.hpp" + namespace eupak::tasks { - struct ExtractTask { + struct ExtractTask : ITask { + ExtractTask(); + void Init(argparse::ArgumentParser& parentParser) override; + + bool ShouldRun(argparse::ArgumentParser& parentParser) const override; + + int Parse() override; + + int Run() override; + + private: struct Arguments { fs::path inputPath; fs::path outputDirectory; bool verbose; }; - int Run(Arguments&& args); + argparse::ArgumentParser parser; + Arguments currentArgs; }; - -} +} // namespace eupak::tasks #endif // EUROPATOOLS_EXTRACTTASK_H