feat: parse directory with -d option (#4)

This commit is contained in:
TaurusXin 2024-01-05 18:34:42 +08:00
parent e08b922be3
commit cce512bbf3
3 changed files with 91 additions and 33 deletions

View File

@ -74,5 +74,6 @@ make win32
## 使用
1. 命令行下使用 `ncmdump [files]...`
2. 直接 ncm 拖拽文件到二进制文件上
1. 多文件:命令行下使用 `ncmdump file1 file2...`
2. 文件夹批量处理:命令行下使用 `ncmdump -d folder`
3. 直接 ncm 拖拽文件到二进制文件上

View File

@ -1,27 +1,84 @@
#include "ncmcrypt.h"
#include <iostream>
#include <stdexcept>
#include <vector>
#include <filesystem>
int main(int argc, char *argv[]) {
namespace fs = std::filesystem;
if (argc <= 1) {
std::cout << "Please input file path!" << std::endl;
return 1;
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;
}
void processFile(const std::string& filePath) {
if (fs::exists(filePath) == false) {
std::cerr << "Error: file '" << filePath << "' does not exist." << std::endl;
return;
}
try {
int i;
for (i = 1; i < argc; i++) {
NeteaseCrypt crypt(argv[i]);
NeteaseCrypt crypt(filePath);
crypt.Dump();
crypt.FixMetadata();
std::cout << crypt.dumpFilepath() << std::endl;
}
} catch (std::invalid_argument e) {
std::cout << "Exception: " << e.what() << std::endl;
std::cout << "Done: " << crypt.dumpFilepath() << std::endl;
} catch (const std::invalid_argument& e) {
std::cout << "Exception: '" << filePath << "'" << e.what() << std::endl;
} catch (...) {
std::cout << "Unexcept exception!" << std::endl;
std::cout << "Unexpected exception while processing file: " << filePath << std::endl;
}
}
void processFilesInFolder(const std::string& folderPath) {
for (const auto& entry : fs::directory_iterator(folderPath)) {
if (fs::is_regular_file(entry.status())) {
processFile(entry.path().string());
}
}
}
int main(int argc, char* argv[]) {
if (argc <= 1) {
displayHelp();
return 1;
}
std::vector<std::string> files;
bool processFolders = false;
bool folderProvided = false;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "-h" || arg == "--help") {
displayHelp();
return 0;
} else if (arg == "-d") {
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 {
files.push_back(arg);
}
}
for (const auto& file : files) {
if (processFolders && fs::is_directory(file)) {
processFilesInFolder(file);
} else {
processFile(file);
}
}
return 0;

View File

@ -113,12 +113,12 @@ NeteaseMusicMetadata::NeteaseMusicMetadata(cJSON* raw) {
}
bool NeteaseCrypt::openFile(std::string const& path) {
try {
mFile.open(path, std::ios::in | std::ios::binary);
} catch (...) {
if (!mFile.is_open()) {
return false;
}
} else {
return true;
}
}
bool NeteaseCrypt::isNcmFile() {
@ -143,7 +143,7 @@ int NeteaseCrypt::read(char *s, std::streamsize n) {
int gcount = mFile.gcount();
if (gcount <= 0) {
throw std::invalid_argument("can't read file");
throw std::invalid_argument(" can't read file");
}
return gcount;
@ -292,15 +292,15 @@ NeteaseCrypt::~NeteaseCrypt() {
NeteaseCrypt::NeteaseCrypt(std::string const& path) {
if (!openFile(path)) {
throw std::invalid_argument("can't open file");
throw std::invalid_argument(" can't open file");
}
if (!isNcmFile()) {
throw std::invalid_argument("not netease protected file");
throw std::invalid_argument(" not netease protected file");
}
if (!mFile.seekg(2, mFile.cur)) {
throw std::invalid_argument("can't seek file");
throw std::invalid_argument(" can't seek file");
}
mFilepath = path;