2021-10-07 00:34:29 +08:00
|
|
|
#include "ncmcrypt.h"
|
|
|
|
|
2024-01-05 18:34:42 +08:00
|
|
|
#include <iostream>
|
2021-10-07 00:34:29 +08:00
|
|
|
#include <stdexcept>
|
2024-01-05 18:34:42 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <filesystem>
|
2021-10-07 00:34:29 +08:00
|
|
|
|
2024-02-12 21:52:56 +08:00
|
|
|
#if defined(_WIN32)
|
2024-03-22 12:07:32 +08:00
|
|
|
#include "platform.h"
|
2024-01-06 00:20:17 +08:00
|
|
|
#endif
|
|
|
|
|
2024-01-06 10:19:33 +08:00
|
|
|
#include "color.h"
|
|
|
|
|
2024-01-05 18:34:42 +08:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
2024-03-21 14:04:16 +08:00
|
|
|
void displayHelp()
|
|
|
|
{
|
2024-01-05 18:34:42 +08:00
|
|
|
std::cout << "Usage: ncmdump [-d] [-h] file1 file2 ..." << std::endl;
|
|
|
|
std::cout << "Options:" << std::endl;
|
2024-01-06 00:20:17 +08:00
|
|
|
std::cout << " -d Process files in a folder (requires folder path)" << std::endl;
|
2024-01-05 18:34:42 +08:00
|
|
|
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-22 12:07:32 +08:00
|
|
|
std::cerr << BOLDRED << "Error: " << RESET << "file '" << filePath.u8string() << "' does not exist." << std::endl;
|
2024-01-05 18:34:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-21 14:04:16 +08:00
|
|
|
try
|
|
|
|
{
|
2024-03-22 12:07:32 +08:00
|
|
|
NeteaseCrypt crypt(filePath.u8string());
|
2024-01-05 18:34:42 +08:00
|
|
|
crypt.Dump();
|
|
|
|
crypt.FixMetadata();
|
|
|
|
|
2024-03-22 12:07:32 +08:00
|
|
|
std::cout << BOLDGREEN << "Done: " << RESET << "'" << crypt.dumpFilepath().u8string() << "'" << std::endl;
|
2024-03-21 14:04:16 +08:00
|
|
|
}
|
|
|
|
catch (const std::invalid_argument &e)
|
|
|
|
{
|
2024-03-22 12:07:32 +08:00
|
|
|
std::cerr << BOLDRED << "Exception: " << RESET << RED << e.what() << RESET << " '" << filePath.u8string() << "'" << std::endl;
|
2024-01-05 18:34:42 +08:00
|
|
|
}
|
2024-03-21 14:04:16 +08:00
|
|
|
catch (...)
|
|
|
|
{
|
2024-03-22 12:12:21 +08:00
|
|
|
std::cerr << BOLDRED << "Unexpected exception while processing file: " << RESET << filePath.u8string() << std::endl;
|
2024-01-05 18:34:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()))
|
|
|
|
{
|
2024-01-06 00:20:17 +08:00
|
|
|
processFile(entry.path());
|
2024-01-05 18:34:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 00:20:17 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2024-03-22 12:29:46 +08:00
|
|
|
#if defined(_WIN32)
|
2024-03-22 12:07:32 +08:00
|
|
|
win32_utf8argv(&argc, &argv);
|
2024-03-22 12:29:46 +08:00
|
|
|
#endif
|
2024-03-21 14:04:16 +08:00
|
|
|
if (argc <= 1)
|
|
|
|
{
|
2024-01-05 18:34:42 +08:00
|
|
|
displayHelp();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-01-06 00:20:17 +08:00
|
|
|
std::vector<fs::path> files;
|
2024-01-05 18:34:42 +08:00
|
|
|
bool processFolders = false;
|
2024-01-06 00:20:17 +08:00
|
|
|
|
2024-01-05 18:34:42 +08:00
|
|
|
bool folderProvided = false;
|
|
|
|
|
2024-02-08 10:26:53 +08:00
|
|
|
#define COMPARE_STR(s1, s2) (strcmp(s1, s2) == 0)
|
|
|
|
#define HELP_SHORT "-h"
|
|
|
|
#define HELP_LONG "--help"
|
2024-03-24 09:20:25 +08:00
|
|
|
#define PROCESS_FOLDER "-d"
|
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))
|
|
|
|
{
|
2024-01-05 18:34:42 +08:00
|
|
|
displayHelp();
|
|
|
|
return 0;
|
2024-03-21 14:04:16 +08:00
|
|
|
}
|
2024-03-24 09:20:25 +08:00
|
|
|
else if (COMPARE_STR(argv[i], PROCESS_FOLDER))
|
2024-03-21 14:04:16 +08:00
|
|
|
{
|
2024-01-05 18:34:42 +08:00
|
|
|
processFolders = true;
|
2024-03-21 14:04:16 +08:00
|
|
|
if (i + 1 < argc && argv[i + 1][0] != '-')
|
|
|
|
{
|
2024-01-05 18:34:42 +08:00
|
|
|
folderProvided = true;
|
|
|
|
processFilesInFolder(argv[i + 1]);
|
|
|
|
// Skip the folder name
|
|
|
|
++i;
|
2024-03-21 14:04:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-05 18:34:42 +08:00
|
|
|
std::cerr << "Error: -d option requires a folder path." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2024-03-21 14:04:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-22 12:07:32 +08:00
|
|
|
fs::path path = fs::u8path(argv[i]);
|
2024-01-06 00:20:17 +08:00
|
|
|
files.push_back(path);
|
2024-01-05 18:34:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-21 14:04:16 +08:00
|
|
|
for (const auto &file : files)
|
|
|
|
{
|
|
|
|
if (processFolders && fs::is_directory(file))
|
|
|
|
{
|
2024-01-05 18:34:42 +08:00
|
|
|
processFilesInFolder(file);
|
2024-03-21 14:04:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-05 18:34:42 +08:00
|
|
|
processFile(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|