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"
|
2012-06-24 15:05:44 +02:00
|
|
|
#include "binarytree.h"
|
2012-07-14 03:10:24 +02:00
|
|
|
#include <framework/core/application.h>
|
2012-04-02 17:51:03 +02:00
|
|
|
|
|
|
|
#include <physfs.h>
|
|
|
|
|
2012-06-23 23:30:54 +02:00
|
|
|
FileStream::FileStream(const std::string& name, PHYSFS_File *fileHandle, bool writeable) :
|
|
|
|
m_name(name),
|
|
|
|
m_fileHandle(fileHandle),
|
|
|
|
m_pos(0),
|
|
|
|
m_writeable(writeable),
|
|
|
|
m_caching(false)
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FileStream::~FileStream()
|
|
|
|
{
|
2012-07-30 14:29:13 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if(g_app.isTerminated())
|
|
|
|
g_logger.warning("FileStream reference not released");
|
|
|
|
#endif
|
|
|
|
if(!g_app.isTerminated())
|
2012-04-02 17:51:03 +02:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2012-04-24 23:05:46 +02:00
|
|
|
void FileStream::cache()
|
|
|
|
{
|
2012-06-23 23:30:54 +02:00
|
|
|
m_caching = true;
|
2012-04-24 23:05:46 +02:00
|
|
|
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_writeable) {
|
|
|
|
if(!m_fileHandle)
|
|
|
|
return;
|
2012-04-24 23:05:46 +02:00
|
|
|
|
2012-06-23 23:30:54 +02:00
|
|
|
// cache entire file into data buffer
|
|
|
|
m_pos = PHYSFS_tell(m_fileHandle);
|
|
|
|
PHYSFS_seek(m_fileHandle, 0);
|
|
|
|
int size = PHYSFS_fileLength(m_fileHandle);
|
|
|
|
m_data.resize(size);
|
|
|
|
if(PHYSFS_read(m_fileHandle, m_data.data(), size, 1) == -1)
|
|
|
|
throwError("unable to read file data", true);
|
|
|
|
PHYSFS_close(m_fileHandle);
|
|
|
|
m_fileHandle = nullptr;
|
|
|
|
}
|
2012-04-24 23:05:46 +02:00
|
|
|
}
|
|
|
|
|
2012-06-22 01:58:18 +02:00
|
|
|
void FileStream::close()
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
2012-07-30 14:29:13 +02:00
|
|
|
if(m_fileHandle && PHYSFS_isInit()) {
|
2012-06-22 01:58:18 +02:00
|
|
|
if(!PHYSFS_close(m_fileHandle))
|
2012-06-23 16:26:18 +02:00
|
|
|
throwError("close failed", true);
|
2012-04-02 17:51:03 +02:00
|
|
|
m_fileHandle = nullptr;
|
|
|
|
}
|
2012-06-23 23:30:54 +02:00
|
|
|
|
|
|
|
m_data.clear();
|
|
|
|
m_pos = 0;
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2012-06-22 01:58:18 +02:00
|
|
|
void FileStream::flush()
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_writeable)
|
|
|
|
throwError("filestream is not writeable");
|
|
|
|
|
|
|
|
if(m_fileHandle) {
|
|
|
|
if(m_caching) {
|
|
|
|
if(!PHYSFS_seek(m_fileHandle, 0))
|
|
|
|
throwError("flush seek failed", true);
|
|
|
|
uint len = m_data.size();
|
|
|
|
if(PHYSFS_write(m_fileHandle, m_data.data(), 1, len) != len)
|
|
|
|
throwError("flush write failed", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(PHYSFS_flush(m_fileHandle) == 0)
|
|
|
|
throwError("flush failed", true);
|
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2012-06-22 01:58:18 +02:00
|
|
|
int FileStream::read(void *buffer, uint32 size, uint32 nmemb)
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_caching) {
|
2012-04-24 23:05:46 +02:00
|
|
|
int res = PHYSFS_read(m_fileHandle, buffer, size, nmemb);
|
2012-06-22 01:58:18 +02:00
|
|
|
if(res == -1)
|
2012-06-23 16:26:18 +02:00
|
|
|
throwError("read failed", true);
|
2012-04-24 23:05:46 +02:00
|
|
|
return res;
|
|
|
|
} else {
|
2012-06-22 01:58:18 +02:00
|
|
|
uint maxReadPos = m_data.size()-1;
|
2012-04-24 23:05:46 +02:00
|
|
|
int writePos = 0;
|
|
|
|
uint8 *outBuffer = (uint8*)buffer;
|
2012-06-22 01:58:18 +02:00
|
|
|
for(uint i=0;i<nmemb;++i) {
|
|
|
|
if(m_pos+size > maxReadPos)
|
2012-04-24 23:05:46 +02:00
|
|
|
return i;
|
|
|
|
|
2012-06-22 01:58:18 +02:00
|
|
|
for(uint j=0;j<size;++j)
|
|
|
|
outBuffer[writePos++] = m_data[m_pos++];
|
2012-04-24 23:05:46 +02:00
|
|
|
}
|
|
|
|
return nmemb;
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
void FileStream::write(const void *buffer, uint32 count)
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_caching) {
|
|
|
|
if(PHYSFS_write(m_fileHandle, buffer, 1, count) != count)
|
|
|
|
throwError("write failed", true);
|
|
|
|
} else {
|
|
|
|
m_data.grow(m_pos + count);
|
|
|
|
memcpy(&m_data[m_pos], buffer, count);
|
|
|
|
m_pos += count;
|
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2012-06-22 01:58:18 +02:00
|
|
|
void FileStream::seek(uint32 pos)
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_caching) {
|
2012-06-22 01:58:18 +02:00
|
|
|
if(!PHYSFS_seek(m_fileHandle, pos))
|
2012-06-23 16:26:18 +02:00
|
|
|
throwError("seek failed", true);
|
2012-04-24 23:05:46 +02:00
|
|
|
} else {
|
2012-06-22 01:58:18 +02:00
|
|
|
if(pos > m_data.size())
|
|
|
|
throwError("seek failed");
|
|
|
|
m_pos = pos;
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
2012-06-22 01:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileStream::skip(uint len)
|
|
|
|
{
|
|
|
|
seek(tell() + len);
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2012-07-15 15:23:13 +02:00
|
|
|
uint FileStream::size()
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_caching)
|
2012-04-24 23:05:46 +02:00
|
|
|
return PHYSFS_fileLength(m_fileHandle);
|
|
|
|
else
|
2012-06-22 01:58:18 +02:00
|
|
|
return m_data.size();
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2012-07-15 15:23:13 +02:00
|
|
|
uint FileStream::tell()
|
2012-04-13 21:54:08 +02:00
|
|
|
{
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_caching)
|
2012-04-24 23:05:46 +02:00
|
|
|
return PHYSFS_tell(m_fileHandle);
|
|
|
|
else
|
2012-06-22 01:58:18 +02:00
|
|
|
return m_pos;
|
2012-04-13 21:54:08 +02:00
|
|
|
}
|
|
|
|
|
2012-04-02 17:51:03 +02:00
|
|
|
uint8 FileStream::getU8()
|
|
|
|
{
|
|
|
|
uint8 v = 0;
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_caching) {
|
2012-04-24 23:05:46 +02:00
|
|
|
if(PHYSFS_read(m_fileHandle, &v, 1, 1) != 1)
|
2012-06-23 16:26:18 +02:00
|
|
|
throwError("read failed", true);
|
2012-04-24 23:05:46 +02:00
|
|
|
} else {
|
2012-06-22 01:58:18 +02:00
|
|
|
if(m_pos+1 > m_data.size())
|
|
|
|
throwError("read failed");
|
2012-04-24 23:05:46 +02:00
|
|
|
|
2012-06-22 01:58:18 +02:00
|
|
|
v = m_data[m_pos];
|
|
|
|
m_pos += 1;
|
2012-04-24 23:05:46 +02:00
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16 FileStream::getU16()
|
|
|
|
{
|
|
|
|
uint16 v = 0;
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_caching) {
|
2012-04-24 23:05:46 +02:00
|
|
|
if(PHYSFS_readULE16(m_fileHandle, &v) == 0)
|
2012-06-23 16:26:18 +02:00
|
|
|
throwError("read failed", true);
|
2012-04-24 23:05:46 +02:00
|
|
|
} else {
|
2012-06-22 01:58:18 +02:00
|
|
|
if(m_pos+2 > m_data.size())
|
|
|
|
throwError("read failed");
|
2012-04-24 23:05:46 +02:00
|
|
|
|
2012-06-22 01:58:18 +02:00
|
|
|
v = stdext::readLE16(&m_data[m_pos]);
|
|
|
|
m_pos += 2;
|
2012-04-24 23:05:46 +02:00
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 FileStream::getU32()
|
|
|
|
{
|
|
|
|
uint32 v = 0;
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_caching) {
|
2012-04-24 23:05:46 +02:00
|
|
|
if(PHYSFS_readULE32(m_fileHandle, &v) == 0)
|
2012-06-23 16:26:18 +02:00
|
|
|
throwError("read failed", true);
|
2012-04-24 23:05:46 +02:00
|
|
|
} else {
|
2012-06-22 01:58:18 +02:00
|
|
|
if(m_pos+4 > m_data.size())
|
|
|
|
throwError("read failed");
|
2012-04-24 23:05:46 +02:00
|
|
|
|
2012-06-22 01:58:18 +02:00
|
|
|
v = stdext::readLE32(&m_data[m_pos]);
|
|
|
|
m_pos += 4;
|
2012-04-24 23:05:46 +02:00
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 FileStream::getU64()
|
|
|
|
{
|
|
|
|
uint64 v = 0;
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_caching) {
|
2012-04-24 23:05:46 +02:00
|
|
|
if(PHYSFS_readULE64(m_fileHandle, (PHYSFS_uint64*)&v) == 0)
|
2012-06-23 16:26:18 +02:00
|
|
|
throwError("read failed", true);
|
2012-04-24 23:05:46 +02:00
|
|
|
} else {
|
2012-06-22 01:58:18 +02:00
|
|
|
if(m_pos+8 > m_data.size())
|
|
|
|
throwError("read failed");
|
|
|
|
v = stdext::readLE64(&m_data[m_pos]);
|
|
|
|
m_pos += 8;
|
2012-04-24 23:05:46 +02:00
|
|
|
}
|
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;
|
2012-06-23 16:26:18 +02:00
|
|
|
uint16 len = getU16();
|
2012-05-12 06:52:16 +02:00
|
|
|
if(len > 0 && len < 8192) {
|
|
|
|
char buffer[8192];
|
|
|
|
if(m_fileHandle) {
|
|
|
|
if(PHYSFS_read(m_fileHandle, buffer, 1, len) == 0)
|
2012-06-23 16:26:18 +02:00
|
|
|
throwError("read failed", true);
|
2012-05-12 06:52:16 +02:00
|
|
|
else
|
|
|
|
str = std::string(buffer, len);
|
|
|
|
} else {
|
2012-06-22 01:58:18 +02:00
|
|
|
if(m_pos+len > m_data.size()) {
|
2012-06-22 21:04:03 +02:00
|
|
|
throwError("read failed");
|
2012-05-12 06:52:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-22 01:58:18 +02:00
|
|
|
str = std::string((char*)&m_data[m_pos], len);
|
|
|
|
m_pos += len;
|
2012-05-12 06:52:16 +02:00
|
|
|
}
|
2012-06-23 16:26:18 +02:00
|
|
|
} else if(len != 0)
|
|
|
|
throwError("read failed because string is too big");
|
2012-05-12 06:52:16 +02:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2012-06-24 15:05:44 +02:00
|
|
|
BinaryTreePtr FileStream::getBinaryTree()
|
|
|
|
{
|
|
|
|
uint8 byte = getU8();
|
2012-07-15 15:23:13 +02:00
|
|
|
if(byte != BINARYTREE_NODE_START)
|
2012-07-09 08:46:11 +02:00
|
|
|
stdext::throw_exception(stdext::format("failed to read node start (getBinaryTree): %d", byte));
|
2012-07-15 15:23:13 +02:00
|
|
|
|
|
|
|
return BinaryTreePtr(new BinaryTree(asFileStream()));
|
2012-06-24 15:05:44 +02:00
|
|
|
}
|
|
|
|
|
2012-07-20 20:56:08 +02:00
|
|
|
BinaryTreePtr FileStream::makeTree()
|
|
|
|
{
|
|
|
|
BinaryTreePtr root(new BinaryTree(asFileStream()));
|
|
|
|
root->setType(0);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2012-04-02 17:51:03 +02:00
|
|
|
void FileStream::addU8(uint8 v)
|
|
|
|
{
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_caching) {
|
|
|
|
if(PHYSFS_write(m_fileHandle, &v, 1, 1) != 1)
|
|
|
|
throwError("write failed", true);
|
|
|
|
} else {
|
|
|
|
m_data.add(v);
|
|
|
|
m_pos++;
|
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
void FileStream::addU16(uint16 v)
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_caching) {
|
|
|
|
if(PHYSFS_writeULE16(m_fileHandle, v) == 0)
|
|
|
|
throwError("write failed", true);
|
|
|
|
} else {
|
|
|
|
m_data.grow(m_pos + 2);
|
|
|
|
stdext::writeLE16(&m_data[m_pos], v);
|
|
|
|
m_pos += 2;
|
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
void FileStream::addU32(uint32 v)
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_caching) {
|
|
|
|
if(PHYSFS_writeULE32(m_fileHandle, v) == 0)
|
|
|
|
throwError("write failed", true);
|
|
|
|
} else {
|
|
|
|
m_data.grow(m_pos + 4);
|
|
|
|
stdext::writeLE32(&m_data[m_pos], v);
|
|
|
|
m_pos += 4;
|
|
|
|
}
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
void FileStream::addU64(uint64 v)
|
2012-04-02 17:51:03 +02:00
|
|
|
{
|
2012-06-23 23:30:54 +02:00
|
|
|
if(!m_caching) {
|
|
|
|
if(PHYSFS_writeULE64(m_fileHandle, v) == 0)
|
|
|
|
throwError("write failed", true);
|
|
|
|
} else {
|
|
|
|
m_data.grow(m_pos + 8);
|
|
|
|
stdext::writeLE64(&m_data[m_pos], v);
|
|
|
|
m_pos += 8;
|
|
|
|
}
|
2012-06-22 01:58:18 +02:00
|
|
|
}
|
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
void FileStream::addString(const std::string& v)
|
|
|
|
{
|
|
|
|
addU16(v.length());
|
|
|
|
write(v.c_str(), v.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileStream::throwError(const std::string& message, bool physfsError)
|
2012-06-22 01:58:18 +02:00
|
|
|
{
|
|
|
|
std::string completeMessage = stdext::format("in file '%s': %s", m_name, message);
|
2012-06-23 16:26:18 +02:00
|
|
|
if(physfsError)
|
|
|
|
completeMessage += std::string(": ") + PHYSFS_getLastError();
|
2012-06-22 01:58:18 +02:00
|
|
|
stdext::throw_exception(completeMessage);
|
2012-04-02 17:51:03 +02:00
|
|
|
}
|