#include "sgu_stdafx.h" #include "sgu_mode_npk_extract_files.h" bool extractNPKFiles(const char* szNPKFileName, const char* szOutputDir) { if (!szNPKFileName || !szOutputDir || szNPKFileName[0] == 0 || szOutputDir[0] == 0) { printf("Error: Invalid parameters. NPK file name and output directory must be specified.\n"); return false; // Invalid parameters } if (!createEmptyPath(szOutputDir)) { printf("Error: Failed to create output directory '%s'.\n", szOutputDir); return false; } NPK::INPKFile* npkFile = NPK::createNPKFile(); if (!npkFile) { return false; // Failed to create NPK file interface } if (!npkFile->openPakFile(szNPKFileName)) { printf("Failed to open NPK file: '%s', Error=%s\n", szNPKFileName, NPK::getLastErrorMessage()); NPK::closeNPKFile(npkFile); return false; // Failed to open NPK file } int32_t fileCount = npkFile->getFileCount(); for (int32_t i = 0; i < fileCount; ++i) { NPK::INPKFile::FileNode fileNode; if (npkFile->getFileNode(i, fileNode)) { std::string outputPathName = std::string(szOutputDir) + "/" + fileNode.fileName; char szOutputPath[MAX_PATH]; StringCbCopyA(szOutputPath, sizeof(szOutputPath), outputPathName.c_str()); getParentPath(szOutputPath);// Get the directory part of the path if (!forceCreatePath(szOutputPath)) { printf("Error: Failed to create output path '%s'.\n", szOutputPath); NPK::closeNPKFile(npkFile); return false; } NPK::IFileStream* fileStream = npkFile->openFileStream(fileNode.fileName.c_str()); if(fileStream == nullptr) { printf("Error: Failed to open file stream for '%s', Error=%s\n", fileNode.fileName.c_str(), NPK::getLastErrorMessage()); NPK::closeNPKFile(npkFile); return false; } FILE* fpOutput = fopen(outputPathName.c_str(), "wb"); if (!fpOutput) { printf("Error: Failed to open output file '%s' for writing.\n", outputPathName.c_str()); npkFile->closeFileStream(fileStream); NPK::closeNPKFile(npkFile); return false; // Failed to open output file } char buffer[4096] = { 0 }; uint32_t bytesRead = 0; while ((bytesRead = fileStream->read(buffer, sizeof(buffer))) > 0) { if (fwrite(buffer, 1, bytesRead, fpOutput) != bytesRead) { printf("Error: Failed to write to output file '%s'.\n", outputPathName.c_str()); fclose(fpOutput); npkFile->closeFileStream(fileStream); NPK::closeNPKFile(npkFile); return false; // Failed to write to output file } } fclose(fpOutput); npkFile->closeFileStream(fileStream); } } NPK::closeNPKFile(npkFile); return true; }