metaforce/hecl/driver/ToolExtract.hpp

167 lines
5.9 KiB
C++
Raw Normal View History

2018-10-07 03:38:44 +00:00
#pragma once
2015-06-12 09:08:49 +00:00
#include "ToolBase.hpp"
2017-12-29 07:56:31 +00:00
#include <cstdio>
2015-09-01 19:26:14 +00:00
#if _WIN32
2015-09-01 19:17:21 +00:00
#include <conio.h>
2015-09-01 19:26:14 +00:00
#else
#include <termios.h>
#endif
2015-06-12 09:08:49 +00:00
2018-03-23 21:40:12 +00:00
#include "hecl/MultiProgressPrinter.hpp"
2018-12-08 05:18:42 +00:00
class ToolExtract final : public ToolBase {
hecl::Database::IDataSpec::ExtractPassInfo m_einfo;
struct SpecExtractPass {
const hecl::Database::DataSpecEntry* m_entry;
std::unique_ptr<hecl::Database::IDataSpec> m_instance;
SpecExtractPass(const hecl::Database::DataSpecEntry* entry, std::unique_ptr<hecl::Database::IDataSpec>&& instance)
: m_entry(entry), m_instance(std::move(instance)) {}
SpecExtractPass(const SpecExtractPass& other) = delete;
SpecExtractPass(SpecExtractPass&& other) = default;
};
std::vector<SpecExtractPass> m_specPasses;
std::vector<hecl::Database::IDataSpec::ExtractReport> m_reps;
std::unique_ptr<hecl::Database::Project> m_fallbackProj;
hecl::Database::Project* m_useProj = nullptr;
2015-06-12 09:08:49 +00:00
public:
2018-12-08 05:18:42 +00:00
ToolExtract(const ToolPassInfo& info) : ToolBase(info) {
if (!m_info.args.size())
2019-07-20 04:22:58 +00:00
LogModule.report(logvisor::Fatal, fmt("hecl extract needs a source path as its first argument"));
2018-12-08 05:18:42 +00:00
if (!info.project) {
hecl::SystemString rootDir;
if (info.output.empty()) {
/* Get name from input file and init project there */
hecl::SystemString baseFile = info.args.front();
size_t slashPos = baseFile.rfind(_SYS_STR('/'));
if (slashPos == hecl::SystemString::npos)
slashPos = baseFile.rfind(_SYS_STR('\\'));
if (slashPos != hecl::SystemString::npos)
baseFile.assign(baseFile.begin() + slashPos + 1, baseFile.end());
size_t dotPos = baseFile.rfind(_SYS_STR('.'));
if (dotPos != hecl::SystemString::npos)
baseFile.assign(baseFile.begin(), baseFile.begin() + dotPos);
if (baseFile.empty())
2019-07-20 04:22:58 +00:00
LogModule.report(logvisor::Fatal, fmt("hecl extract must be ran within a project directory"));
2018-12-08 05:18:42 +00:00
rootDir = info.cwd + baseFile;
} else {
if (hecl::PathRelative(info.output.c_str()))
rootDir = info.cwd + info.output;
2015-07-28 23:54:54 +00:00
else
2018-12-08 05:18:42 +00:00
rootDir = info.output;
}
size_t ErrorRef = logvisor::ErrorCount;
hecl::ProjectRootPath newProjRoot(rootDir);
newProjRoot.makeDir();
m_fallbackProj.reset(new hecl::Database::Project(newProjRoot));
if (logvisor::ErrorCount > ErrorRef)
2019-07-28 01:19:48 +00:00
LogModule.report(logvisor::Fatal, fmt(_SYS_STR("unable to init project at '{}'")), rootDir);
2019-07-20 04:22:58 +00:00
LogModule.report(logvisor::Info, fmt(_SYS_STR("initialized project at '{}/.hecl'")), rootDir);
2018-12-08 05:18:42 +00:00
m_useProj = m_fallbackProj.get();
} else
m_useProj = info.project;
m_einfo.srcpath = m_info.args.front();
m_einfo.force = info.force;
m_einfo.extractArgs.reserve(info.args.size());
auto it = info.args.cbegin();
++it;
for (; it != info.args.cend(); ++it)
m_einfo.extractArgs.push_back(*it);
m_specPasses.reserve(hecl::Database::DATA_SPEC_REGISTRY.size());
for (const hecl::Database::DataSpecEntry* entry : hecl::Database::DATA_SPEC_REGISTRY) {
if (entry->m_factory) {
auto ds = entry->m_factory(*m_useProj, hecl::Database::DataSpecTool::Extract);
if (ds && ds->canExtract(m_einfo, m_reps))
m_specPasses.emplace_back(entry, std::move(ds));
}
2015-06-12 09:08:49 +00:00
}
2018-12-08 05:18:42 +00:00
}
static void Help(HelpOutput& help) {
help.secHead(_SYS_STR("NAME"));
help.beginWrap();
help.wrap(_SYS_STR("hecl-extract - Extract objects from supported package/image formats\n"));
help.endWrap();
help.secHead(_SYS_STR("SYNOPSIS"));
help.beginWrap();
help.wrap(_SYS_STR("hecl extract <packagefile> [<subnode>...]\n"));
help.endWrap();
help.secHead(_SYS_STR("DESCRIPTION"));
help.beginWrap();
help.wrap(_SYS_STR("This command recursively extracts all or part of a dataspec-supported ")
2018-10-14 20:09:15 +00:00
_SYS_STR("package format. Each object is decoded to a working format and added to the project.\n\n"));
2018-12-08 05:18:42 +00:00
help.endWrap();
2015-06-12 09:08:49 +00:00
2018-12-08 05:18:42 +00:00
help.secHead(_SYS_STR("OPTIONS"));
help.optionHead(_SYS_STR("<packagefile>[/<subnode>...]"), _SYS_STR("input file"));
help.beginWrap();
help.wrap(_SYS_STR("Specifies the package file or disc image to source data from. ")
2018-10-14 20:09:15 +00:00
_SYS_STR("An optional subnode specifies a named hierarchical-node specific ")
2018-12-08 05:18:42 +00:00
_SYS_STR("to the game architecture (levels/areas)."));
help.endWrap();
}
hecl::SystemString toolName() const override { return _SYS_STR("extract"); }
2018-12-08 05:18:42 +00:00
static void _recursivePrint(int level, hecl::Database::IDataSpec::ExtractReport& rep) {
for (int l = 0; l < level; ++l)
2019-07-20 04:22:58 +00:00
fmt::print(fmt(_SYS_STR(" ")));
2018-12-08 05:18:42 +00:00
if (XTERM_COLOR)
2019-07-20 04:22:58 +00:00
fmt::print(fmt(_SYS_STR("" BOLD "{}" NORMAL "")), rep.name);
2018-12-08 05:18:42 +00:00
else
2019-07-20 04:22:58 +00:00
fmt::print(fmt(_SYS_STR("{}")), rep.name);
2018-12-08 05:18:42 +00:00
if (rep.desc.size())
2019-07-20 04:22:58 +00:00
fmt::print(fmt(_SYS_STR(" [{}]")), rep.desc);
fmt::print(fmt(_SYS_STR("\n")));
2018-12-08 05:18:42 +00:00
for (hecl::Database::IDataSpec::ExtractReport& child : rep.childOpts)
_recursivePrint(level + 1, child);
}
int run() override {
2018-12-08 05:18:42 +00:00
if (m_specPasses.empty()) {
if (XTERM_COLOR)
2019-07-20 04:22:58 +00:00
fmt::print(fmt(_SYS_STR("" RED BOLD "NOTHING TO EXTRACT" NORMAL "\n")));
2018-12-08 05:18:42 +00:00
else
2019-07-20 04:22:58 +00:00
fmt::print(fmt(_SYS_STR("NOTHING TO EXTRACT\n")));
2018-12-08 05:18:42 +00:00
return 1;
2015-06-12 09:08:49 +00:00
}
2018-12-08 05:18:42 +00:00
if (XTERM_COLOR)
2019-07-20 04:22:58 +00:00
fmt::print(fmt(_SYS_STR("" GREEN BOLD "ABOUT TO EXTRACT:" NORMAL "\n")));
2018-12-08 05:18:42 +00:00
else
2019-07-20 04:22:58 +00:00
fmt::print(fmt(_SYS_STR("ABOUT TO EXTRACT:\n")));
2016-03-24 02:51:57 +00:00
2018-12-08 05:18:42 +00:00
for (hecl::Database::IDataSpec::ExtractReport& rep : m_reps) {
_recursivePrint(0, rep);
2019-07-20 04:22:58 +00:00
fmt::print(fmt(_SYS_STR("\n")));
2015-07-10 05:28:33 +00:00
}
2018-12-08 05:18:42 +00:00
fflush(stdout);
2015-07-10 05:28:33 +00:00
2018-12-08 05:18:42 +00:00
if (continuePrompt()) {
for (SpecExtractPass& ds : m_specPasses) {
2015-07-10 05:28:33 +00:00
if (XTERM_COLOR)
2019-07-20 04:22:58 +00:00
fmt::print(fmt(_SYS_STR("" MAGENTA BOLD "Using DataSpec {}:" NORMAL "\n")), ds.m_entry->m_name);
2015-07-10 05:28:33 +00:00
else
2019-07-20 04:22:58 +00:00
fmt::print(fmt(_SYS_STR("Using DataSpec {}:\n")), ds.m_entry->m_name);
2018-12-08 05:18:42 +00:00
ds.m_instance->doExtract(m_einfo, {true});
2019-07-20 04:22:58 +00:00
fmt::print(fmt(_SYS_STR("\n\n")));
2018-12-08 05:18:42 +00:00
}
2015-06-12 09:08:49 +00:00
}
2018-12-08 05:18:42 +00:00
return 0;
}
};