tools/eupak: Refactor ExtractTask to ITask

Completing this refactor.. Nice.
This commit is contained in:
Lily Tsuru 2025-01-15 19:34:31 -05:00
parent addf2071f4
commit d9dae6e6ac
3 changed files with 80 additions and 79 deletions

View file

@ -8,44 +8,16 @@
#include <argparse/argparse.hpp>
#include <EupakConfig.hpp>
#include <tasks/CreateTask.hpp>
#include <tasks/ExtractTask.hpp>
#include <tasks/InfoTask.hpp>
#include "europa/structs/Pak.hpp"
#include "tasks/Task.hpp"
#include <tasks/Task.hpp>
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<bool>("--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;
}

View file

@ -6,6 +6,7 @@
// SPDX-License-Identifier: MIT
//
#include <EupakConfig.hpp>
#include <europa/io/PakReader.hpp>
#include <fstream>
#include <indicators/cursor_control.hpp>
@ -14,9 +15,66 @@
#include <stdexcept>
#include <tasks/ExtractTask.hpp>
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<bool>("--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

View file

@ -11,20 +11,32 @@
#include <CommonDefs.hpp>
#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