fix: improve file addition to MPQ archives

This commit is contained in:
2025-02-27 00:51:47 -05:00
parent ff4ebff4a8
commit 454ba7ce66
6 changed files with 16 additions and 49 deletions

View File

@@ -17,10 +17,15 @@ fs.writeFileSync(path.join(customPath, 'test2.txt'), 'Another test file');
// Create an MPQ from our directory
const outputMpq = path.join(__dirname, 'test.mpq');
console.log('Creating new MPQ archive from directory...');
console.log('Creating new MPQ archive...');
const mpqPath = mpq.createArchive(testDir, 2, outputMpq);
console.log(`Created MPQ at: ${mpqPath}`);
// Add files to the archive
console.log('\nAdding files to archive...');
mpq.addFile(mpqPath, path.join(customPath, 'test1.txt'), 'custom/path/test1.txt');
mpq.addFile(mpqPath, path.join(customPath, 'test2.txt'), 'custom/path/test2.txt');
// List files in the archive to verify
console.log('\nFiles in the archive:');
const files = mpq.listFiles(mpqPath);

Binary file not shown.

View File

@@ -212,7 +212,7 @@ class MPQTool {
let cmd = `${this.mpqcliPath} add "${absoluteMpqPath}" "${absoluteSourcePath}"`;
if (targetPath) {
cmd += ` -p "${targetPath}"`;
cmd += ` "${targetPath}"`;
}
execSync(cmd, {
@@ -233,47 +233,7 @@ class MPQTool {
* @returns {boolean} Success status
*/
addFile(mpqFile, filePath, targetPath = null) {
try {
const fs = require('fs');
const absoluteMpqPath = path.resolve(mpqFile);
const absoluteFilePath = path.resolve(filePath);
// Create a temporary directory
const tempDir = path.join(require('os').tmpdir(), 'mpq-tool-' + Date.now());
fs.mkdirSync(tempDir, { recursive: true });
// Extract current MPQ contents
console.log('Extracting current MPQ contents...');
this.extractAll(absoluteMpqPath, tempDir);
// Copy new file to temp directory
// Normalize path separators for MPQ
const finalPath = (targetPath || path.basename(absoluteFilePath))
.split(/[/\\]/) // Split on both forward and back slashes
.join('\\'); // Join with backslashes
const targetFilePath = path.join(tempDir, finalPath);
// Create parent directories if needed
fs.mkdirSync(path.dirname(targetFilePath), { recursive: true });
// Copy the file
fs.copyFileSync(absoluteFilePath, targetFilePath);
// Create new MPQ with all files
console.log('Creating new MPQ with added file...');
const tempMpq = path.join(require('os').tmpdir(), 'temp.mpq');
this.createArchive(tempDir, 2, tempMpq);
// Replace original MPQ
fs.renameSync(tempMpq, absoluteMpqPath);
// Clean up
fs.rmSync(tempDir, { recursive: true, force: true });
return true;
} catch (error) {
throw new Error(`Failed to add file: ${error.message}`);
}
return this.addToArchive(mpqFile, filePath, targetPath || '');
}
}

View File

@@ -4,11 +4,11 @@
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cout << "Usage: mpqcli <command> <mpq_file> [file_path]" << std::endl;
std::cout << "Usage: mpqcli <command> <mpq_file> [file_path] [target_path]" << std::endl;
std::cout << "Commands:" << std::endl;
std::cout << " list - List contents of MPQ file" << std::endl;
std::cout << " extract - Extract file from MPQ" << std::endl;
std::cout << " add - Add file to MPQ" << std::endl;
std::cout << " add - Add file to MPQ (optional target_path)" << std::endl;
std::cout << " create - Create new MPQ archive (v1 or v2)" << std::endl;
return 1;
}
@@ -24,7 +24,8 @@ int main(int argc, char* argv[]) {
handler.extractFile(argv[3]);
}
else if (command == "add" && argc > 3) {
handler.addFile(argv[3]);
std::string targetPath = argc > 4 ? argv[4] : "";
handler.addFile(argv[3], targetPath);
}
else if (command == "create") {
int version = (argc > 3 && std::string(argv[3]) == "-v2") ? 2 : 1;

View File

@@ -63,14 +63,15 @@ void MPQHandler::extractFile(const std::string& filePath) {
SFileCloseArchive(hMpq);
}
void MPQHandler::addFile(const std::string& filePath) {
void MPQHandler::addFile(const std::string& filePath, const std::string& targetPath) {
HANDLE hMpq = NULL;
if (!SFileOpenArchive(mpqPath.c_str(), 0, 0, &hMpq)) {
std::cerr << "Failed to open MPQ file" << std::endl;
return;
}
if (!SFileAddFileEx(hMpq, filePath.c_str(), filePath.c_str(),
const char* archivePath = targetPath.empty() ? filePath.c_str() : targetPath.c_str();
if (!SFileAddFileEx(hMpq, filePath.c_str(), archivePath,
MPQ_FILE_COMPRESS | MPQ_FILE_REPLACEEXISTING,
MPQ_COMPRESSION_ZLIB, MPQ_COMPRESSION_NEXT_SAME)) {
std::cerr << "Failed to add file to MPQ" << std::endl;

View File

@@ -8,7 +8,7 @@ public:
void listFiles();
void extractFile(const std::string& filePath);
void addFile(const std::string& filePath);
void addFile(const std::string& filePath, const std::string& targetPath = "");
void createArchive(const std::string& path, int version);
private: