nod/driver/main.cpp

266 lines
7.9 KiB
C++
Raw Normal View History

2015-06-29 23:46:19 -07:00
#include <stdio.h>
#include <string.h>
2016-03-04 15:04:30 -08:00
#include "logvisor/logvisor.hpp"
#include "nod/nod.hpp"
2015-06-29 23:46:19 -07:00
static void printHelp()
{
fprintf(stderr, "Usage:\n"
2016-01-20 22:30:37 -08:00
" nodtool extract [-f] <image-in> [<dir-out>]\n"
" nodtool makegcn <fsroot-in> [<image-out>]\n"
" nodtool makewii <fsroot-in> [<image-out>]\n"
" nodtool mergegcn <fsroot-in> <image-in> [<image-out>]\n"
" nodtool mergewii <fsroot-in> <image-in> [<image-out>]\n");
}
2015-07-02 13:37:07 -07:00
#if NOD_UCS2
#ifdef strcasecmp
#undef strcasecmp
#endif
#define strcasecmp _wcsicmp
2016-01-20 22:30:37 -08:00
#define PRISize "Iu"
2015-07-02 13:37:07 -07:00
int wmain(int argc, wchar_t* argv[])
#else
2016-01-20 22:30:37 -08:00
#define PRISize "zu"
2015-06-29 23:46:19 -07:00
int main(int argc, char* argv[])
2015-07-02 13:37:07 -07:00
#endif
2015-06-29 23:46:19 -07:00
{
2016-01-20 22:30:37 -08:00
if (argc < 3 ||
(!strcasecmp(argv[1], _S("makegcn")) && argc < 3) ||
(!strcasecmp(argv[1], _S("makewii")) && argc < 3) ||
(!strcasecmp(argv[1], _S("mergegcn")) && argc < 4) ||
(!strcasecmp(argv[1], _S("mergewii")) && argc < 4))
2015-06-29 23:46:19 -07:00
{
printHelp();
2016-01-25 12:21:10 -08:00
return 1;
2015-06-29 23:46:19 -07:00
}
/* Enable logging to console */
2016-09-07 23:16:22 -07:00
logvisor::RegisterStandardExceptions();
2016-03-04 15:04:30 -08:00
logvisor::RegisterConsoleLogger();
bool verbose = false;
nod::ExtractionContext ctx = {true,
2017-11-12 22:18:53 -08:00
[&](std::string_view str, float c) {
if (verbose)
2017-11-12 22:18:53 -08:00
fprintf(stderr, "Current node: %s, Extraction %g%% Complete\n", str.data(), c * 100.f);
}};
2016-03-04 15:04:30 -08:00
const nod::SystemChar* inDir = nullptr;
const nod::SystemChar* outDir = _S(".");
2015-09-27 18:55:59 -07:00
for (int a=2 ; a<argc ; ++a)
{
if (argv[a][0] == '-' && argv[a][1] == 'f')
2015-09-27 18:55:59 -07:00
ctx.force = true;
else if (argv[a][0] == '-' && argv[a][1] == 'v')
verbose = true;
2015-09-27 18:55:59 -07:00
else if (!inDir)
inDir = argv[a];
else
outDir = argv[a];
}
2015-06-29 23:46:19 -07:00
2017-11-12 23:21:29 -08:00
auto progFunc = [&](float prog, nod::SystemStringView name, size_t bytes)
{
2017-05-21 20:18:52 -07:00
nod::Printf(_S("\r "));
if (bytes != -1)
2017-11-12 23:21:29 -08:00
nod::Printf(_S("\r%g%% %s %" PRISize " B"), prog * 100.f, name.data(), bytes);
else
2017-11-12 23:21:29 -08:00
nod::Printf(_S("\r%g%% %s"), prog * 100.f, name.data());
fflush(stdout);
};
2015-07-02 13:37:07 -07:00
if (!strcasecmp(argv[1], _S("extract")))
{
2016-01-22 15:45:58 -08:00
bool isWii;
2016-03-04 15:04:30 -08:00
std::unique_ptr<nod::DiscBase> disc = nod::OpenDiscFromImage(inDir, isWii);
if (!disc)
2016-01-25 12:21:10 -08:00
return 1;
2016-03-04 15:04:30 -08:00
nod::Mkdir(outDir, 0755);
2016-01-22 15:45:58 -08:00
2016-03-04 15:04:30 -08:00
nod::Partition* dataPart = disc->getDataPartition();
if (!dataPart)
2016-01-25 12:21:10 -08:00
return 1;
2015-06-29 23:46:19 -07:00
2015-09-27 18:55:59 -07:00
if (!dataPart->extractToDirectory(outDir, ctx))
2016-01-25 12:21:10 -08:00
return 1;
}
2016-01-20 22:30:37 -08:00
else if (!strcasecmp(argv[1], _S("makegcn")))
2015-06-29 23:46:19 -07:00
{
/* Pre-validate path */
2016-03-04 15:04:30 -08:00
nod::Sstat theStat;
if (nod::Stat(argv[2], &theStat) || !S_ISDIR(theStat.st_mode))
{
nod::LogModule.report(logvisor::Error, _S("unable to stat %s as directory"), argv[2]);
return 1;
}
2016-03-04 15:04:30 -08:00
if (nod::DiscBuilderGCN::CalculateTotalSizeRequired(argv[2]) == -1)
return 1;
2016-01-20 22:30:37 -08:00
nod::EBuildResult ret;
2016-01-25 12:21:10 -08:00
if (argc < 4)
2016-01-20 22:30:37 -08:00
{
nod::SystemString outPath(argv[2]);
2016-01-20 22:30:37 -08:00
outPath.append(_S(".iso"));
2017-11-12 23:21:29 -08:00
nod::DiscBuilderGCN b(outPath, progFunc);
ret = b.buildFromDirectory(argv[2]);
2016-01-20 22:30:37 -08:00
}
else
{
nod::DiscBuilderGCN b(argv[3], progFunc);
ret = b.buildFromDirectory(argv[2]);
2016-01-20 22:30:37 -08:00
}
printf("\n");
if (ret != nod::EBuildResult::Success)
2016-01-25 12:21:10 -08:00
return 1;
}
else if (!strcasecmp(argv[1], _S("makewii")))
{
/* Pre-validate path */
2016-03-04 15:04:30 -08:00
nod::Sstat theStat;
if (nod::Stat(argv[2], &theStat) || !S_ISDIR(theStat.st_mode))
{
nod::LogModule.report(logvisor::Error, _S("unable to stat %s as directory"), argv[4]);
return 1;
}
bool dual = false;
if (nod::DiscBuilderWii::CalculateTotalSizeRequired(argv[2], dual) == -1)
return 1;
nod::EBuildResult ret;
2016-01-22 15:45:58 -08:00
if (argc < 4)
{
nod::SystemString outPath(argv[2]);
outPath.append(_S(".iso"));
nod::DiscBuilderWii b(outPath.c_str(), dual, progFunc);
ret = b.buildFromDirectory(argv[2]);
}
else
{
nod::DiscBuilderWii b(argv[3], dual, progFunc);
ret = b.buildFromDirectory(argv[2]);
}
printf("\n");
if (ret != nod::EBuildResult::Success)
2016-01-25 12:21:10 -08:00
return 1;
}
else if (!strcasecmp(argv[1], _S("mergegcn")))
{
/* Pre-validate paths */
nod::Sstat theStat;
if (nod::Stat(argv[2], &theStat) || !S_ISDIR(theStat.st_mode))
{
nod::LogModule.report(logvisor::Error, _S("unable to stat %s as directory"), argv[2]);
return 1;
}
if (nod::Stat(argv[3], &theStat) || !S_ISREG(theStat.st_mode))
{
nod::LogModule.report(logvisor::Error, _S("unable to stat %s as file"), argv[3]);
return 1;
}
bool isWii;
std::unique_ptr<nod::DiscBase> disc = nod::OpenDiscFromImage(argv[3], isWii);
if (!disc)
{
nod::LogModule.report(logvisor::Error, _S("unable to open image %s"), argv[3]);
return 1;
}
if (isWii)
{
nod::LogModule.report(logvisor::Error, _S("Wii images should be merged with 'mergewii'"));
return 1;
}
if (nod::DiscMergerGCN::CalculateTotalSizeRequired(static_cast<nod::DiscGCN&>(*disc), argv[2]) == -1)
return 1;
nod::EBuildResult ret;
if (argc < 5)
{
nod::SystemString outPath(argv[2]);
outPath.append(_S(".iso"));
nod::DiscMergerGCN b(outPath.c_str(), static_cast<nod::DiscGCN&>(*disc), progFunc);
ret = b.mergeFromDirectory(argv[2]);
}
else
{
nod::DiscMergerGCN b(argv[4], static_cast<nod::DiscGCN&>(*disc), progFunc);
ret = b.mergeFromDirectory(argv[2]);
}
printf("\n");
if (ret != nod::EBuildResult::Success)
return 1;
}
else if (!strcasecmp(argv[1], _S("mergewii")))
{
/* Pre-validate paths */
nod::Sstat theStat;
if (nod::Stat(argv[2], &theStat) || !S_ISDIR(theStat.st_mode))
{
nod::LogModule.report(logvisor::Error, _S("unable to stat %s as directory"), argv[2]);
return 1;
}
if (nod::Stat(argv[3], &theStat) || !S_ISREG(theStat.st_mode))
{
nod::LogModule.report(logvisor::Error, _S("unable to stat %s as file"), argv[3]);
return 1;
}
bool isWii;
std::unique_ptr<nod::DiscBase> disc = nod::OpenDiscFromImage(argv[3], isWii);
if (!disc)
{
nod::LogModule.report(logvisor::Error, _S("unable to open image %s"), argv[3]);
return 1;
}
if (!isWii)
{
nod::LogModule.report(logvisor::Error, _S("GameCube images should be merged with 'mergegcn'"));
return 1;
}
bool dual = false;
if (nod::DiscMergerWii::CalculateTotalSizeRequired(static_cast<nod::DiscWii&>(*disc), argv[2], dual) == -1)
return 1;
nod::EBuildResult ret;
if (argc < 5)
{
nod::SystemString outPath(argv[2]);
outPath.append(_S(".iso"));
nod::DiscMergerWii b(outPath.c_str(), static_cast<nod::DiscWii&>(*disc), dual, progFunc);
ret = b.mergeFromDirectory(argv[2]);
}
else
{
nod::DiscMergerWii b(argv[4], static_cast<nod::DiscWii&>(*disc), dual, progFunc);
ret = b.mergeFromDirectory(argv[2]);
}
printf("\n");
if (ret != nod::EBuildResult::Success)
return 1;
}
else
{
printHelp();
2016-01-25 12:21:10 -08:00
return 1;
2015-06-29 23:46:19 -07:00
}
nod::LogModule.report(logvisor::Info, _S("Success!"));
2015-06-29 23:46:19 -07:00
return 0;
}