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]...` 1. 多文件:命令行下使用 `ncmdump file1 file2...`
2. 直接 ncm 拖拽文件到二进制文件上 2. 文件夹批量处理:命令行下使用 `ncmdump -d folder`
3. 直接 ncm 拖拽文件到二进制文件上

View File

@ -1,27 +1,84 @@
#include "ncmcrypt.h" #include "ncmcrypt.h"
#include <iostream>
#include <stdexcept> #include <stdexcept>
#include <vector>
#include <filesystem>
int main(int argc, char *argv[]) { namespace fs = std::filesystem;
if (argc <= 1) { void displayHelp() {
std::cout << "Please input file path!" << std::endl; std::cout << "Usage: ncmdump [-d] [-h] file1 file2 ..." << std::endl;
return 1; 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 { try {
int i; NeteaseCrypt crypt(filePath);
for (i = 1; i < argc; i++) {
NeteaseCrypt crypt(argv[i]);
crypt.Dump(); crypt.Dump();
crypt.FixMetadata(); crypt.FixMetadata();
std::cout << crypt.dumpFilepath() << std::endl; std::cout << "Done: " << crypt.dumpFilepath() << std::endl;
} } catch (const std::invalid_argument& e) {
} catch (std::invalid_argument e) { std::cout << "Exception: '" << filePath << "'" << e.what() << std::endl;
std::cout << "Exception: " << e.what() << std::endl;
} catch (...) { } 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; return 0;

View File

@ -113,13 +113,13 @@ NeteaseMusicMetadata::NeteaseMusicMetadata(cJSON* raw) {
} }
bool NeteaseCrypt::openFile(std::string const& path) { bool NeteaseCrypt::openFile(std::string const& path) {
try {
mFile.open(path, std::ios::in | std::ios::binary); mFile.open(path, std::ios::in | std::ios::binary);
} catch (...) { if (!mFile.is_open()) {
return false; return false;
} } else {
return true; return true;
} }
}
bool NeteaseCrypt::isNcmFile() { bool NeteaseCrypt::isNcmFile() {
unsigned int header; unsigned int header;