nod/driver/main.cpp

204 lines
6.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 <gameid> <game-title> <fsroot-in> <dol-in> <apploader-in> [<image-out>]\n"
2016-01-22 15:45:58 -08:00
" nodtool makewii(sl|dl) <gameid> <game-title> <fsroot-in> <dol-in> <apploader-in> <parthead-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 < 7) ||
2016-01-22 15:45:58 -08:00
(!strcasecmp(argv[1], _S("makewiisl")) && argc < 8) ||
(!strcasecmp(argv[1], _S("makewiidl")) && argc < 8))
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();
2016-03-04 15:04:30 -08:00
nod::ExtractionContext ctx = { true, true, [&](const std::string& str){
2015-09-27 18:55:59 -07:00
fprintf(stderr, "%s\n", str.c_str());
}};
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')
ctx.verbose = true;
else if (!inDir)
inDir = argv[a];
else
outDir = argv[a];
}
2015-06-29 23:46:19 -07:00
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
if (isWii)
2016-03-04 15:04:30 -08:00
static_cast<nod::DiscWii&>(*disc).writeOutDataPartitionHeader(
(nod::SystemString(outDir) + _S("/partition_head.bin")).c_str());
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
{
2016-01-20 22:30:37 -08:00
#if NOD_UCS2
2016-01-23 21:06:54 -08:00
if (wcslen(argv[2]) < 6)
2016-03-04 15:04:30 -08:00
nod::LogModule.report(logvisor::Fatal, _S("game-id is not at least 6 characters"));
2016-01-20 22:30:37 -08:00
#else
if (strlen(argv[2]) < 6)
2016-03-04 15:04:30 -08:00
nod::LogModule.report(logvisor::Fatal, _S("game-id is not at least 6 characters"));
2016-01-20 22:30:37 -08:00
#endif
/* Pre-validate paths */
2016-03-04 15:04:30 -08:00
nod::Sstat theStat;
if (nod::Stat(argv[4], &theStat) || !S_ISDIR(theStat.st_mode))
nod::LogModule.report(logvisor::Fatal, _S("unable to stat %s as directory"), argv[4]);
if (nod::Stat(argv[5], &theStat) || !S_ISREG(theStat.st_mode))
nod::LogModule.report(logvisor::Fatal, _S("unable to stat %s as file"), argv[5]);
if (nod::Stat(argv[6], &theStat) || !S_ISREG(theStat.st_mode))
nod::LogModule.report(logvisor::Fatal, "unable to stat %s as file", argv[6]);
nod::SystemString gameIdSys(argv[2]);
nod::SystemUTF8View gameId(gameIdSys);
nod::SystemString gameTitleSys(argv[3]);
nod::SystemUTF8View gameTitle(gameTitleSys);
2016-01-20 22:30:37 -08:00
size_t lastIdx = -1;
2016-03-04 15:04:30 -08:00
auto progFunc = [&](size_t idx, const nod::SystemString& name, size_t bytes)
2016-01-20 22:30:37 -08:00
{
if (idx != lastIdx)
{
lastIdx = idx;
printf("\n");
}
2016-01-21 12:46:07 -08:00
if (bytes != -1)
2016-03-04 15:04:30 -08:00
nod::Printf(_S("\r%s %" PRISize " B"), name.c_str(), bytes);
2016-01-21 12:46:07 -08:00
else
2016-03-04 15:04:30 -08:00
nod::Printf(_S("\r%s"), name.c_str());
2016-01-20 22:30:37 -08:00
fflush(stdout);
};
2016-01-25 12:21:10 -08:00
bool ret;
2016-01-20 22:30:37 -08:00
if (argc < 8)
{
2016-03-04 15:04:30 -08:00
nod::SystemString outPath(argv[4]);
2016-01-20 22:30:37 -08:00
outPath.append(_S(".iso"));
2016-03-04 15:04:30 -08:00
nod::DiscBuilderGCN b(outPath.c_str(), gameId.utf8_str().c_str(), gameTitle.utf8_str().c_str(), 0x0003EB60, progFunc);
2016-01-25 12:21:10 -08:00
ret = b.buildFromDirectory(argv[4], argv[5], argv[6]);
2016-01-20 22:30:37 -08:00
}
else
{
2016-03-04 15:04:30 -08:00
nod::DiscBuilderGCN b(argv[7], gameId.utf8_str().c_str(), gameTitle.utf8_str().c_str(), 0x0003EB60, progFunc);
2016-01-25 12:21:10 -08:00
ret = b.buildFromDirectory(argv[4], argv[5], argv[6]);
2016-01-20 22:30:37 -08:00
}
printf("\n");
2016-01-25 12:21:10 -08:00
if (!ret)
return 1;
}
2016-01-22 15:45:58 -08:00
else if (!strcasecmp(argv[1], _S("makewiisl")) || !strcasecmp(argv[1], _S("makewiidl")))
{
#if NOD_UCS2
2016-01-23 21:06:54 -08:00
if (wcslen(argv[2]) < 6)
2016-03-04 15:04:30 -08:00
nod::LogModule.report(logvisor::Fatal, _S("game-id is not at least 6 characters"));
#else
if (strlen(argv[2]) < 6)
2016-03-04 15:04:30 -08:00
nod::LogModule.report(logvisor::Fatal, _S("game-id is not at least 6 characters"));
#endif
/* Pre-validate paths */
2016-03-04 15:04:30 -08:00
nod::Sstat theStat;
if (nod::Stat(argv[4], &theStat) || !S_ISDIR(theStat.st_mode))
nod::LogModule.report(logvisor::Fatal, _S("unable to stat %s as directory"), argv[4]);
if (nod::Stat(argv[5], &theStat) || !S_ISREG(theStat.st_mode))
nod::LogModule.report(logvisor::Fatal, _S("unable to stat %s as file"), argv[5]);
if (nod::Stat(argv[6], &theStat) || !S_ISREG(theStat.st_mode))
nod::LogModule.report(logvisor::Fatal, _S("unable to stat %s as file"), argv[6]);
if (nod::Stat(argv[7], &theStat) || !S_ISREG(theStat.st_mode))
nod::LogModule.report(logvisor::Fatal, _S("unable to stat %s as file"), argv[7]);
nod::SystemString gameIdSys(argv[2]);
nod::SystemUTF8View gameId(gameIdSys);
nod::SystemString gameTitleSys(argv[3]);
nod::SystemUTF8View gameTitle(gameTitleSys);
size_t lastIdx = -1;
2016-03-04 15:04:30 -08:00
auto progFunc = [&](size_t idx, const nod::SystemString& name, size_t bytes)
{
if (idx != lastIdx)
{
lastIdx = idx;
printf("\n");
}
if (bytes != -1)
2016-03-04 15:04:30 -08:00
nod::Printf(_S("\r%s %" PRISize " B"), name.c_str(), bytes);
else
2016-03-04 15:04:30 -08:00
nod::Printf(_S("\r%s"), name.c_str());
fflush(stdout);
};
2016-01-22 15:45:58 -08:00
bool dual = (argv[1][7] == _S('d') || argv[1][7] == _S('D'));
2016-01-25 12:21:10 -08:00
bool ret;
2016-01-22 15:45:58 -08:00
if (argc < 9)
{
2016-03-04 15:04:30 -08:00
nod::SystemString outPath(argv[4]);
outPath.append(_S(".iso"));
2016-03-04 15:04:30 -08:00
nod::DiscBuilderWii b(outPath.c_str(), gameId.utf8_str().c_str(), gameTitle.utf8_str().c_str(), dual, progFunc);
2016-01-25 12:21:10 -08:00
ret = b.buildFromDirectory(argv[4], argv[5], argv[6], argv[7]);
}
else
{
2016-03-04 15:04:30 -08:00
nod::DiscBuilderWii b(argv[8], gameId.utf8_str().c_str(), gameTitle.utf8_str().c_str(), dual, progFunc);
2016-01-25 12:21:10 -08:00
ret = b.buildFromDirectory(argv[4], argv[5], argv[6], argv[7]);
}
printf("\n");
2016-01-25 12:21:10 -08:00
if (!ret)
return 1;
}
else
{
printHelp();
2016-01-25 12:21:10 -08:00
return 1;
2015-06-29 23:46:19 -07:00
}
return 0;
}