2012-04-02 17:51:03 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "filestream.h"
|
|
|
|
|
|
|
|
#include <physfs.h>
|
|
|
|
|
|
|
|
FileStream::FileStream(const std::string& name, PHYSFS_File *fileHandle)
|
|
|
|
{
|
|
|
|
m_name = name;
|
|
|
|
m_fileHandle = fileHandle;
|
2012-04-24 23:05:46 +02:00
|
|
|
m_cacheReadPos = 0;
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FileStream::~FileStream()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2012-04-24 23:05:46 +02:00
|
|
|
void FileStream::cache()
|
|
|
|
{
|
|
|
|
if(!m_fileHandle)
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError("no file handle to cache");
|
2012-04-24 23:05:46 +02:00
|
|
|
|
|
|
|
// cache entire file into cache buffer
|
|
|
|
m_cacheReadPos = PHYSFS_tell(m_fileHandle);
|
|
|
|
PHYSFS_seek(m_fileHandle, 0);
|
|
|
|
int size = PHYSFS_fileLength(m_fileHandle);
|
|
|
|
m_cacheBuffer.resize(size);
|
|
|
|
int res = PHYSFS_read(m_fileHandle, &m_cacheBuffer[0], size, 1);
|
|
|
|
if(res == -1)
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-24 23:05:46 +02:00
|
|
|
|
|
|
|
PHYSFS_close(m_fileHandle);
|
|
|
|
m_fileHandle = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
bool FileStream::close()
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
|
|
|
if(m_fileHandle) {
|
|
|
|
if(PHYSFS_isInit() && PHYSFS_close(m_fileHandle) == 0)
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-02 17:51:03 +02:00
|
|
|
|
|
|
|
m_fileHandle = nullptr;
|
2012-04-13 21:54:08 +02:00
|
|
|
return true;
|
2012-04-24 23:05:46 +02:00
|
|
|
} else {
|
|
|
|
m_cacheBuffer.clear();
|
|
|
|
m_cacheReadPos = 0;
|
|
|
|
return true;
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
bool FileStream::flush()
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
2012-04-24 23:05:46 +02:00
|
|
|
if(!m_fileHandle)
|
|
|
|
return false;
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
if(PHYSFS_flush(m_fileHandle) == 0) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-13 21:54:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
int FileStream::read(void *buffer, int size, int nmemb)
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
2012-04-24 23:05:46 +02:00
|
|
|
if(m_fileHandle) {
|
|
|
|
int res = PHYSFS_read(m_fileHandle, buffer, size, nmemb);
|
|
|
|
if(res == -1) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-24 23:05:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
} else {
|
|
|
|
uint maxReadPos = m_cacheBuffer.size()-1;
|
|
|
|
int writePos = 0;
|
|
|
|
uint8 *outBuffer = (uint8*)buffer;
|
|
|
|
for(int i=0;i<nmemb;++i) {
|
|
|
|
if(m_cacheReadPos+size > maxReadPos)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
for(int j=0;j<size;++j)
|
|
|
|
outBuffer[writePos++] = m_cacheBuffer[m_cacheReadPos++];
|
|
|
|
}
|
|
|
|
return nmemb;
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
bool FileStream::write(void *buffer, int count)
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
2012-04-24 23:05:46 +02:00
|
|
|
if(!m_fileHandle)
|
|
|
|
return false;
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
if(PHYSFS_write(m_fileHandle, buffer, 1, count) != count) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-13 21:54:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-24 23:05:46 +02:00
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
return true;
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
bool FileStream::seek(int pos)
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
2012-04-24 23:05:46 +02:00
|
|
|
if(m_fileHandle) {
|
|
|
|
if(PHYSFS_seek(m_fileHandle, pos) == 0) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-24 23:05:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(pos > (int)m_cacheBuffer.size() || pos < 0) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': seek pos cannot be greater than file length", m_name));
|
2012-04-24 23:05:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_cacheReadPos = pos;
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
|
|
|
return true;
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int FileStream::size()
|
|
|
|
{
|
2012-04-24 23:05:46 +02:00
|
|
|
if(m_fileHandle)
|
|
|
|
return PHYSFS_fileLength(m_fileHandle);
|
|
|
|
else
|
|
|
|
return m_cacheBuffer.size();
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
int FileStream::tell()
|
|
|
|
{
|
2012-04-24 23:05:46 +02:00
|
|
|
if(m_fileHandle)
|
|
|
|
return PHYSFS_tell(m_fileHandle);
|
|
|
|
else
|
|
|
|
return m_cacheReadPos;
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
|
|
|
|
2012-04-02 17:51:03 +02:00
|
|
|
uint8 FileStream::getU8()
|
|
|
|
{
|
|
|
|
uint8 v = 0;
|
2012-04-24 23:05:46 +02:00
|
|
|
if(m_fileHandle) {
|
|
|
|
if(PHYSFS_read(m_fileHandle, &v, 1, 1) != 1)
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-24 23:05:46 +02:00
|
|
|
} else {
|
2012-04-28 00:17:51 +02:00
|
|
|
if(m_cacheReadPos+1 > m_cacheBuffer.size()) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name));
|
2012-04-24 23:05:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
v = m_cacheBuffer[m_cacheReadPos];
|
|
|
|
m_cacheReadPos += 1;
|
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16 FileStream::getU16()
|
|
|
|
{
|
|
|
|
uint16 v = 0;
|
2012-04-24 23:05:46 +02:00
|
|
|
if(m_fileHandle) {
|
|
|
|
if(PHYSFS_readULE16(m_fileHandle, &v) == 0)
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-24 23:05:46 +02:00
|
|
|
} else {
|
2012-04-28 00:17:51 +02:00
|
|
|
if(m_cacheReadPos+2 > m_cacheBuffer.size()) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name));
|
2012-04-24 23:05:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-28 15:06:26 +02:00
|
|
|
v = stdext::readLE16(&m_cacheBuffer[m_cacheReadPos]);
|
2012-04-24 23:05:46 +02:00
|
|
|
m_cacheReadPos += 2;
|
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 FileStream::getU32()
|
|
|
|
{
|
|
|
|
uint32 v = 0;
|
2012-04-24 23:05:46 +02:00
|
|
|
if(m_fileHandle) {
|
|
|
|
if(PHYSFS_readULE32(m_fileHandle, &v) == 0)
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-24 23:05:46 +02:00
|
|
|
} else {
|
2012-04-28 00:17:51 +02:00
|
|
|
if(m_cacheReadPos+4 > m_cacheBuffer.size()) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name));
|
2012-04-24 23:05:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-28 15:06:26 +02:00
|
|
|
v = stdext::readLE32(&m_cacheBuffer[m_cacheReadPos]);
|
2012-04-24 23:05:46 +02:00
|
|
|
m_cacheReadPos += 4;
|
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 FileStream::getU64()
|
|
|
|
{
|
|
|
|
uint64 v = 0;
|
2012-04-24 23:05:46 +02:00
|
|
|
if(m_fileHandle) {
|
|
|
|
if(PHYSFS_readULE64(m_fileHandle, (PHYSFS_uint64*)&v) == 0)
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-24 23:05:46 +02:00
|
|
|
} else {
|
2012-04-28 00:17:51 +02:00
|
|
|
if(m_cacheReadPos+8 > m_cacheBuffer.size()) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name));
|
2012-04-24 23:05:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-28 15:06:26 +02:00
|
|
|
v = stdext::readLE64(&m_cacheBuffer[m_cacheReadPos]);
|
2012-04-24 23:05:46 +02:00
|
|
|
m_cacheReadPos += 8;
|
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2012-05-12 06:52:16 +02:00
|
|
|
std::string FileStream::getString()
|
|
|
|
{
|
|
|
|
std::string str;
|
|
|
|
int len = getU16();
|
|
|
|
if(len > 0 && len < 8192) {
|
|
|
|
char buffer[8192];
|
|
|
|
if(m_fileHandle) {
|
|
|
|
if(PHYSFS_read(m_fileHandle, buffer, 1, len) == 0)
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-05-12 06:52:16 +02:00
|
|
|
else
|
|
|
|
str = std::string(buffer, len);
|
|
|
|
} else {
|
|
|
|
if(m_cacheReadPos+len > m_cacheBuffer.size()) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name));
|
2012-05-12 06:52:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
str = std::string((char*)&m_cacheBuffer[m_cacheReadPos], len);
|
|
|
|
m_cacheReadPos += len;
|
|
|
|
}
|
|
|
|
} else {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-05-12 06:52:16 +02:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2012-06-21 04:31:29 +02:00
|
|
|
uint8 FileStream::readNode(uint8 &oldNode, uint32 &type)
|
|
|
|
{
|
2012-06-21 05:05:44 +02:00
|
|
|
if (!oldNode) {
|
|
|
|
if ((oldNode = getU8()) == 0xFE) {
|
|
|
|
type = getU32();
|
|
|
|
return oldNode;
|
|
|
|
} else {
|
|
|
|
dump << "Failed to read new node.";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(getU8() == 0xFF);
|
|
|
|
if ((oldNode = getU8()) == 0xFE) {
|
|
|
|
type = getU32();
|
|
|
|
return oldNode;
|
|
|
|
} else {
|
|
|
|
dump << "Failed to read node with old type: " << type;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-06-21 04:31:29 +02:00
|
|
|
}
|
|
|
|
|
2012-04-02 17:51:03 +02:00
|
|
|
void FileStream::addU8(uint8 v)
|
|
|
|
{
|
|
|
|
if(PHYSFS_write(m_fileHandle, &v, 1, 1) != 1)
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileStream::addU16(uint8 v)
|
|
|
|
{
|
|
|
|
if(PHYSFS_writeULE16(m_fileHandle, v) == 0)
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileStream::addU32(uint8 v)
|
|
|
|
{
|
|
|
|
if(PHYSFS_writeULE32(m_fileHandle, v) == 0)
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileStream::addU64(uint8 v)
|
|
|
|
{
|
|
|
|
if(PHYSFS_writeULE64(m_fileHandle, v) == 0)
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|