增加Lua函数

This commit is contained in:
2025-09-02 22:45:41 +08:00
parent 6788128cc3
commit 25e7cff75d
14 changed files with 260 additions and 22 deletions
+42
View File
@@ -84,4 +84,46 @@ uint32_t CFileStream::crc32(void)
return m_crc32;
}
bool CFileStream::writeToDiskFile(const wchar_t* diskPathFile)
{
HANDLE hFile = ::CreateFileW(diskPathFile,
FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
CREATE_ALWAYS,
FILE_ATTRIBUTE_ARCHIVE,
0);
if (hFile == INVALID_HANDLE_VALUE)
{
setLastError(NPK_ERR_FILE_CREATE, "Failed to open output file for writing, LastError=%d.", ::GetLastError());
return false; // Failed to open output file
}
//store current read offset
uint32_t currentOffset = m_currentOffset;
m_currentOffset = 0;
//read loop
char buffer[4096] = { 0 };
uint32_t bytesRead = 0;
while ((bytesRead = this->read(buffer, sizeof(buffer))) > 0)
{
DWORD dwWriteBytes = 0;
if (!WriteFile(hFile, buffer, bytesRead, &dwWriteBytes, 0) ||
dwWriteBytes != bytesRead)
{
setLastError(NPK_ERR_FILE_WRITE, "Failed to write to output file, LastError=%d", ::GetLastError());
CloseHandle(hFile);
m_currentOffset = currentOffset;
return false; // Failed to write to output file
}
}
CloseHandle(hFile);
m_currentOffset = currentOffset;
return true;
}
}
+4 -2
View File
@@ -31,6 +31,8 @@ public:
return m_currentOffset >= m_fileSize;
}
virtual bool writeToDiskFile(const wchar_t* diskPathFile);
protected:
//Pack file
CNPKFile* m_pNPKFile;
@@ -38,9 +40,9 @@ protected:
HANDLE m_hFileandle;
std::string m_fileName;
//offset of the file in the pak file
uint32_t m_fileOffset = 0;
const uint32_t m_fileOffset = 0;
//size of the file
uint32_t m_fileSize = 0;
const uint32_t m_fileSize = 0;
//current read offset in the file
uint32_t m_currentOffset = 0;
//CRC32 checksum of the file
+22 -14
View File
@@ -258,34 +258,42 @@ bool createEmptyPath(const char* szPath)
return true;
}
void getParentPath(char* szPath)
void getParentPath(wchar_t* szPath)
{
if (!szPath || szPath[0] == 0) return;
size_t length = strlen(szPath);
std::replace(szPath, szPath+length, '/', '\\');
size_t length = wcslen(szPath);
std::replace(szPath, szPath+length, L'/', L'\\');
::PathRemoveFileSpecA(szPath);
::PathRemoveFileSpecW(szPath);
}
bool forceCreatePath(const char* szPath)
bool forceCreatePath(const wchar_t* szPath)
{
char szCurrentPath[MAX_PATH] = { 0 };
StringCchCopyA(szCurrentPath, MAX_PATH, szPath);
size_t length = strlen(szPath);
std::replace(szCurrentPath, szCurrentPath + length, '/', '\\');
wchar_t szCurrentPath[MAX_PATH] = { 0 };
StringCchCopyW(szCurrentPath, MAX_PATH, szPath);
size_t length = wcslen(szPath);
std::replace(szCurrentPath, szCurrentPath + length, L'/', L'\\');
//目录已经存在
if (::PathFileExists(szCurrentPath)) return true;
if (::PathFileExistsW(szCurrentPath)) return true;
//能够直接创建
if (::CreateDirectory(szCurrentPath, 0)) return true;
if (::CreateDirectoryW(szCurrentPath, 0)) return true;
//取得上一级目录
char szParentPath[MAX_PATH] = { 0 };
StringCchCopyA(szParentPath, MAX_PATH, szPath);
wchar_t szParentPath[MAX_PATH] = { 0 };
StringCchCopyW(szParentPath, MAX_PATH, szPath);
getParentPath(szParentPath);
//创建上一级目录
if (!forceCreatePath(szParentPath)) return false;
return TRUE == ::CreateDirectory(szCurrentPath, 0);
return TRUE == ::CreateDirectoryW(szCurrentPath, 0);
}
bool forceCreatePathForFile(const wchar_t* szFilePath)
{
wchar_t szPathForFile[MAX_PATH] = { 0 };
StringCchCopyW(szPathForFile, MAX_PATH, szFilePath);
getParentPath(szPathForFile);
return forceCreatePath(szPathForFile);
}