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-01-06 00:20:17 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
|
2024-01-05 18:34:42 +08:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
|
|
|
void displayHelp() {
|
|
|
|
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-01-06 00:20:17 +08:00
|
|
|
void processFile(const fs::path& filePath) {
|
2024-01-05 18:34:42 +08:00
|
|
|
if (fs::exists(filePath) == false) {
|
2024-01-06 00:20:17 +08:00
|
|
|
std::cerr << "Error: file '" << filePath.string() << "' does not exist." << std::endl;
|
2024-01-05 18:34:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-01-06 00:20:17 +08:00
|
|
|
NeteaseCrypt crypt(filePath.string());
|
2024-01-05 18:34:42 +08:00
|
|
|
crypt.Dump();
|
|
|
|
crypt.FixMetadata();
|
|
|
|
|
2024-01-06 00:20:17 +08:00
|
|
|
std::cout << "Done: '" << crypt.dumpFilepath().string() << "'" << std::endl;
|
2024-01-05 18:34:42 +08:00
|
|
|
} catch (const std::invalid_argument& e) {
|
|
|
|
std::cout << "Exception: '" << filePath << "'" << e.what() << std::endl;
|
|
|
|
} catch (...) {
|
|
|
|
std::cout << "Unexpected exception while processing file: " << filePath << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 00:20:17 +08:00
|
|
|
void processFilesInFolder(const fs::path& folderPath) {
|
2024-01-05 18:34:42 +08:00
|
|
|
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
|
|
|
#ifdef _WIN32
|
|
|
|
int wmain(int argc, wchar_t* argv[])
|
|
|
|
#else
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetConsoleOutputCP(CP_UTF8);
|
|
|
|
#endif
|
|
|
|
|
2024-01-05 18:34:42 +08:00
|
|
|
if (argc <= 1) {
|
|
|
|
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-01-06 00:20:17 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#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-01-05 18:34:42 +08:00
|
|
|
|
2024-01-06 00:20:17 +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-01-06 00:20:17 +08:00
|
|
|
} else if (COMPARE_STR(argv[i], FOLDER)) {
|
2024-01-05 18:34:42 +08:00
|
|
|
processFolders = true;
|
|
|
|
if (i + 1 < argc && argv[i + 1][0] != '-') {
|
|
|
|
folderProvided = true;
|
|
|
|
processFilesInFolder(argv[i + 1]);
|
|
|
|
// Skip the folder name
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
std::cerr << "Error: -d option requires a folder path." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
2024-01-06 00:20:17 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
int multiByteStrSize = WideCharToMultiByte(CP_UTF8, 0, argv[1], -1, NULL, 0, NULL, NULL);
|
|
|
|
char *multiByteStr = new char[multiByteStrSize];
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, argv[i], -1, multiByteStr, multiByteStrSize, NULL, NULL);
|
|
|
|
fs::path path(multiByteStr);
|
|
|
|
#else
|
|
|
|
fs::path path(arg);
|
|
|
|
#endif
|
|
|
|
files.push_back(path);
|
2024-01-05 18:34:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& file : files) {
|
|
|
|
if (processFolders && fs::is_directory(file)) {
|
|
|
|
processFilesInFolder(file);
|
|
|
|
} else {
|
|
|
|
processFile(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|