增加Lua导出和FileList

This commit is contained in:
2025-09-02 20:39:30 +08:00
parent 1fb319f0e8
commit 6788128cc3
18 changed files with 682 additions and 56 deletions
+72 -16
View File
@@ -3,6 +3,7 @@
#include "libNPK_Errors.h"
#include "libEncrypt_SHA256.h"
#include "libNPK_FileStream.h"
#include "libNPK_Utils.h"
#include <assert.h>
@@ -20,13 +21,20 @@ CNPKFile::CNPKFile()
CNPKFile::~CNPKFile()
{
if (m_hPakFile != NULL)
{
::CloseHandle(m_hPakFile);
m_hPakFile = NULL;
}
m_nFileCounts = 0;
m_fileList.clear();
m_hashFileList.clear();
}
bool CNPKFile::openPakFile(const char* szPackFileName)
bool CNPKFile::openPakFile(const wchar_t* wszNpkPathName)
{
assert(szPackFileName);
if (!szPackFileName || szPackFileName[0] == 0)
assert(wszNpkPathName);
if (!wszNpkPathName || wszNpkPathName[0] == 0)
{
setLastError(NPK_ERR_PARAM);
return false;
@@ -35,10 +43,15 @@ bool CNPKFile::openPakFile(const char* szPackFileName)
//close current pak file if it is open
closePakFile();
m_strPackFileName = szPackFileName;
wchar_t wszNpkBaseName[1024] = { 0 };
StringCchCopyW(wszNpkBaseName, 1024, wszNpkPathName);
PathRemoveFileSpecW(wszNpkBaseName);
m_strPackBaseName = convertWideStringToUtf8(wszNpkBaseName);
m_strPackFileName = convertWideStringToUtf8(::PathFindFileNameW(wszNpkPathName));
//open the pak file
m_hPakFile = ::CreateFile(szPackFileName,
m_hPakFile = ::CreateFileW(wszNpkPathName,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
@@ -48,7 +61,8 @@ bool CNPKFile::openPakFile(const char* szPackFileName)
if (m_hPakFile == INVALID_HANDLE_VALUE)
{
setLastError(NPK_ERR_FILE_ACCESS, "File=%s, WinErr=%d", szPackFileName, ::GetLastError());
setLastError(NPK_ERR_FILE_ACCESS, "File=%s, WinErr=%d",
convertWideStringToUtf8(wszNpkPathName).c_str(), ::GetLastError());
return false;
}
@@ -67,7 +81,8 @@ bool CNPKFile::openPakFile(const char* szPackFileName)
//Check NPK Magic Header
if(memcmp(fileHeader.szFileHeadMagic, kNPKFileHeaderMagic, kNPKFileHeaderMagicSize) != 0)
{
setLastError(NPK_ERR_INVALID_FILE_FORMAT, "File=%s, Magic=%s", szPackFileName, fileHeader.szFileHeadMagic);
setLastError(NPK_ERR_INVALID_FILE_FORMAT, "File=%s, Magic=%s",
convertWideStringToUtf8(wszNpkPathName).c_str(), fileHeader.szFileHeadMagic);
return false;
}
@@ -94,7 +109,7 @@ bool CNPKFile::openPakFile(const char* szPackFileName)
fileInfo.szFileName[j] ^= kNPKFileNameXorCode[j];
}
m_fileList[i].fileName = fileInfo.szFileName;
m_fileList[i].keyName = fileInfo.szFileName;
m_fileList[i].fileOffset = fileInfo.fileOffset;
m_fileList[i].fileSize = fileInfo.fileSize;
}
@@ -177,8 +192,8 @@ void CNPKFile::buildHashFileList()
m_hashFileList.clear();
for (int32_t i = 0; i < m_nFileCounts; i++)
{
const FileNode& fileNode = m_fileList[i];
m_hashFileList[fileNode.fileName] = i;
const FileItemBaseInfo& baseInfo = m_fileList[i];
m_hashFileList[baseInfo.keyName] = i;
}
}
@@ -194,13 +209,13 @@ bool CNPKFile::closePakFile(void)
return true;
}
bool CNPKFile::getFileNode(int32_t index, INPKFile::FileNode& fileNode) const
bool CNPKFile::getFileItemBaseInfo(int32_t index, FileItemBaseInfo* baseInfo) const
{
if (index < 0 || index >= getFileCount())
if (index < 0 || index >= getFileCounts())
{
return false;
}
fileNode = m_fileList[index];
*baseInfo = m_fileList[index];
return true;
}
@@ -231,13 +246,13 @@ IFileStream* CNPKFile::openFileStream(const char* szFileName)
fileIndex = it->second;
}
if (fileIndex < 0 || fileIndex >= getFileCount())
if (fileIndex < 0 || fileIndex >= getFileCounts())
{
setLastError(NPK_ERR_FILE_NOT_FOUND, "File=%s, FileIndex=%d, not found in NPK file", szFileName, fileIndex);
return nullptr;
}
FileNode& fileNode = m_fileList[fileIndex];
FileItemBaseInfo& fileNode = m_fileList[fileIndex];
CFileStream* pFileStream = new CFileStream(this, m_hPakFile, fileNode);
return pFileStream;
@@ -251,6 +266,47 @@ void CNPKFile::closeFileStream(IFileStream* fileStream)
}
}
IFileList* CNPKFile::generateFullInfoList(void)
{
if (m_hPakFile == NULL)
{
setLastError(NPK_ERR_INVALID_STATE, "NPK file is not opened");
return nullptr;
}
IFileList* pFileList = createEmptyFileList();
if (!pFileList)
{
setLastError(NPK_ERR_NEW_FAILED, "Failed to create empty file list");
return nullptr;
}
for (int32_t i = 0; i < m_nFileCounts; i++)
{
const FileItemBaseInfo& baseInfo = m_fileList[i];
FileItemFullInfo fullInfo;
fullInfo.keyName = baseInfo.keyName;
fullInfo.npkName = m_strPackFileName;
fullInfo.npkBase = m_strPackBaseName;
fullInfo.fileSize = baseInfo.fileSize;
fullInfo.fileOffset = baseInfo.fileOffset;
IFileStream* pFileStream = openFileStream(baseInfo.keyName.c_str());
if (pFileStream)
{
fullInfo.fileCRC = pFileStream->crc32();
closeFileStream(pFileStream);
pFileStream = nullptr;
}
else
{
fullInfo.fileCRC = 0;
}
pFileList->addFileNode(fullInfo); // Add file item to the list
}
return pFileList;
}
INPKFile* createNPKFile()
{
return new CNPKFile();