metaforce/hecl/driver/ToolExtract.hpp

195 lines
7.0 KiB
C++
Raw Normal View History

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