增加Lua函数
This commit is contained in:
+3
-1
@@ -29,7 +29,7 @@ enum ErrorCode
|
||||
NPK_ERR_PARAM,
|
||||
NPK_ERR_INVALID_STATE,
|
||||
NPK_ERR_NEW_FAILED,
|
||||
|
||||
NPK_ERR_FILE_CREATE,
|
||||
//Other errors
|
||||
NPK_ERR_UNKNOWN
|
||||
};
|
||||
@@ -98,6 +98,8 @@ public:
|
||||
virtual uint32_t tell(void) const = 0;
|
||||
virtual uint32_t crc32(void) = 0;
|
||||
virtual bool isEOF(void) const = 0;
|
||||
|
||||
virtual bool writeToDiskFile(const wchar_t* diskPathFile) = 0;
|
||||
};
|
||||
|
||||
class IFileList
|
||||
|
||||
@@ -33,7 +33,10 @@ bool clearPath(const char* szPath, bool recursion, bool removeSelf);
|
||||
bool createEmptyPath(const char* szPath);
|
||||
|
||||
// Force create a directory path, ensuring all parent directories are created as needed.
|
||||
bool forceCreatePath(const char* szPath);
|
||||
bool forceCreatePath(const wchar_t* szPath);
|
||||
|
||||
// Force create a directory path for the file
|
||||
bool forceCreatePathForFile(const wchar_t* szFilePath);
|
||||
|
||||
// Get the parent directory path of the specified path.
|
||||
void getParentPath(char* szPath);
|
||||
void getParentPath(wchar_t* szPath);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user