ncmdump/main.cpp

145 lines
3.5 KiB
C++
Raw Normal View History

2021-10-07 00:34:29 +08:00
#include "ncmcrypt.h"
#include <iostream>
2021-10-07 00:34:29 +08:00
#include <stdexcept>
#include <vector>
#include <filesystem>
2021-10-07 00:34:29 +08:00
#if defined(_WIN32)
#include <Windows.h>
#endif
2024-01-06 10:19:33 +08:00
#include "color.h"
namespace fs = std::filesystem;
2024-03-21 14:04:16 +08:00
void displayHelp()
{
std::cout << "Usage: ncmdump [-d] [-h] file1 file2 ..." << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " -d Process files in a folder (requires folder path)" << std::endl;
std::cout << " -h, --help Display this help message" << std::endl;
}
2024-03-21 14:04:16 +08:00
void processFile(const fs::path &filePath)
{
if (fs::exists(filePath) == false)
{
2024-03-20 11:53:21 +08:00
std::cerr << BOLDRED << "Error: " << RESET << "file '" << filePath.string() << "' does not exist." << std::endl;
return;
}
2024-03-21 14:04:16 +08:00
try
{
NeteaseCrypt crypt(filePath.string());
crypt.Dump();
crypt.FixMetadata();
2024-01-06 10:19:33 +08:00
std::cout << BOLDGREEN << "Done: '" << RESET << crypt.dumpFilepath().string() << "'" << std::endl;
2024-03-21 14:04:16 +08:00
}
catch (const std::invalid_argument &e)
{
2024-01-06 10:19:33 +08:00
std::cerr << BOLDRED << "Exception: " << RESET << RED << e.what() << RESET << " '" << filePath.string() << "'" << std::endl;
2024-03-21 14:04:16 +08:00
}
catch (...)
{
2024-01-06 10:19:33 +08:00
std::cerr << BOLDRED << "Unexpected exception while processing file: " << RESET << filePath.string() << std::endl;
}
}
2024-03-21 14:04:16 +08:00
void processFilesInFolder(const fs::path &folderPath)
{
for (const auto &entry : fs::directory_iterator(folderPath))
{
if (fs::is_regular_file(entry.status()))
{
processFile(entry.path());
}
}
}
#if defined(_WIN32) && defined(__MINGW64__)
int wmain(int argc, wchar_t *argv[])
#else
int main(int argc, char **argv)
#endif
{
2024-03-20 12:04:55 +08:00
#if defined(_WIN32)
SetConsoleOutputCP(CP_UTF8);
2024-03-20 12:04:55 +08:00
#endif
2024-03-21 14:04:16 +08:00
if (argc <= 1)
{
displayHelp();
return 1;
}
std::vector<fs::path> files;
bool processFolders = false;
bool folderProvided = false;
#if defined(_WIN32) && defined(__MINGW64__)
#define COMPARE_STR(s1, s2) (wcscmp(s1, s2) == 0)
#define HELP_SHORT L"-h"
#define HELP_LONG L"--help"
#define FOLDER L"-d"
#else
#define COMPARE_STR(s1, s2) (strcmp(s1, s2) == 0)
#define HELP_SHORT "-h"
#define HELP_LONG "--help"
#define FOLDER "-d"
#endif
2024-03-21 14:04:16 +08:00
for (int i = 1; i < argc; ++i)
{
if (COMPARE_STR(argv[i], HELP_SHORT) || COMPARE_STR(argv[i], HELP_LONG))
{
displayHelp();
return 0;
2024-03-21 14:04:16 +08:00
}
else if (COMPARE_STR(argv[i], FOLDER))
{
processFolders = true;
2024-03-21 14:04:16 +08:00
if (i + 1 < argc && argv[i + 1][0] != '-')
{
folderProvided = true;
processFilesInFolder(argv[i + 1]);
// Skip the folder name
++i;
2024-03-21 14:04:16 +08:00
}
else
{
std::cerr << "Error: -d option requires a folder path." << std::endl;
return 1;
}
2024-03-21 14:04:16 +08:00
}
else
{
#if defined(_WIN32) && defined(__MINGW64__)
int utf8_size = WideCharToMultiByte(CP_UTF8, 0, argv[i], -1, NULL, 0, NULL, NULL);
char* str = new char[utf8_size];
WideCharToMultiByte(CP_UTF8, 0, argv[i], -1, str, utf8_size, NULL, NULL);
fs::path path(str);
#else
2024-03-20 11:53:21 +08:00
fs::path path(argv[i]);
#endif
files.push_back(path);
}
}
2024-03-21 14:04:16 +08:00
for (const auto &file : files)
{
if (processFolders && fs::is_directory(file))
{
processFilesInFolder(file);
2024-03-21 14:04:16 +08:00
}
else
{
processFile(file);
}
}
return 0;
}