diff --git a/examples/add-files.js b/examples/add-files.js index b741597..364e3ee 100644 --- a/examples/add-files.js +++ b/examples/add-files.js @@ -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); diff --git a/examples/test.mpq b/examples/test.mpq index 1ca0e8b..c2f9ddd 100644 Binary files a/examples/test.mpq and b/examples/test.mpq differ diff --git a/lib/index.js b/lib/index.js index cdccb7f..1077b9c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 || ''); } } diff --git a/mpqcli/src/main.cpp b/mpqcli/src/main.cpp index b7b5962..3722746 100644 --- a/mpqcli/src/main.cpp +++ b/mpqcli/src/main.cpp @@ -4,11 +4,11 @@ int main(int argc, char* argv[]) { if (argc < 3) { - std::cout << "Usage: mpqcli [file_path]" << std::endl; + std::cout << "Usage: mpqcli [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; diff --git a/mpqcli/src/mpq_handler.cpp b/mpqcli/src/mpq_handler.cpp index 4dbe2ff..8774d0f 100644 --- a/mpqcli/src/mpq_handler.cpp +++ b/mpqcli/src/mpq_handler.cpp @@ -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; diff --git a/mpqcli/src/mpq_handler.hpp b/mpqcli/src/mpq_handler.hpp index e7e7177..0b695ed 100644 --- a/mpqcli/src/mpq_handler.hpp +++ b/mpqcli/src/mpq_handler.hpp @@ -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: