mirror of
https://github.com/edubart/otclient.git
synced 2025-12-03 00:16:50 +01:00
Optimize minimap save/load
This commit is contained in:
@@ -116,11 +116,11 @@ void Application::init(const std::string& compactName, const std::vector<std::st
|
||||
|
||||
void Application::deinit()
|
||||
{
|
||||
g_lua.callGlobalField("g_app", "onTerminate");
|
||||
|
||||
// hide the window because there is no render anymore
|
||||
g_window.hide();
|
||||
|
||||
g_lua.callGlobalField("g_app", "onTerminate");
|
||||
|
||||
// run modules unload events
|
||||
g_modules.unloadModules();
|
||||
g_modules.clear();
|
||||
|
||||
@@ -25,11 +25,13 @@
|
||||
|
||||
#include <physfs.h>
|
||||
|
||||
FileStream::FileStream(const std::string& name, PHYSFS_File *fileHandle)
|
||||
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)
|
||||
{
|
||||
m_name = name;
|
||||
m_pos = 0;
|
||||
m_fileHandle = fileHandle;
|
||||
}
|
||||
|
||||
FileStream::~FileStream()
|
||||
@@ -40,18 +42,22 @@ FileStream::~FileStream()
|
||||
|
||||
void FileStream::cache()
|
||||
{
|
||||
if(!m_fileHandle)
|
||||
return;
|
||||
m_caching = true;
|
||||
|
||||
// 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[0], size, 1) == -1)
|
||||
throwError("unable to read file data", true);
|
||||
if(!m_writeable) {
|
||||
if(!m_fileHandle)
|
||||
return;
|
||||
|
||||
close();
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
void FileStream::close()
|
||||
@@ -60,21 +66,34 @@ void FileStream::close()
|
||||
if(!PHYSFS_close(m_fileHandle))
|
||||
throwError("close failed", true);
|
||||
m_fileHandle = nullptr;
|
||||
} else {
|
||||
m_data.clear();
|
||||
m_pos = 0;
|
||||
}
|
||||
|
||||
m_data.clear();
|
||||
m_pos = 0;
|
||||
}
|
||||
|
||||
void FileStream::flush()
|
||||
{
|
||||
if(m_fileHandle && PHYSFS_flush(m_fileHandle) == 0)
|
||||
throwError("flush failed", true);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
int FileStream::read(void *buffer, uint32 size, uint32 nmemb)
|
||||
{
|
||||
if(m_fileHandle) {
|
||||
if(!m_caching) {
|
||||
int res = PHYSFS_read(m_fileHandle, buffer, size, nmemb);
|
||||
if(res == -1)
|
||||
throwError("read failed", true);
|
||||
@@ -96,13 +115,19 @@ int FileStream::read(void *buffer, uint32 size, uint32 nmemb)
|
||||
|
||||
void FileStream::write(const void *buffer, uint32 count)
|
||||
{
|
||||
if(PHYSFS_write(m_fileHandle, buffer, 1, count) != count)
|
||||
throwError("write failed", true);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void FileStream::seek(uint32 pos)
|
||||
{
|
||||
if(m_fileHandle) {
|
||||
if(!m_caching) {
|
||||
if(!PHYSFS_seek(m_fileHandle, pos))
|
||||
throwError("seek failed", true);
|
||||
} else {
|
||||
@@ -119,7 +144,7 @@ void FileStream::skip(uint len)
|
||||
|
||||
int FileStream::size()
|
||||
{
|
||||
if(m_fileHandle)
|
||||
if(!m_caching)
|
||||
return PHYSFS_fileLength(m_fileHandle);
|
||||
else
|
||||
return m_data.size();
|
||||
@@ -127,7 +152,7 @@ int FileStream::size()
|
||||
|
||||
int FileStream::tell()
|
||||
{
|
||||
if(m_fileHandle)
|
||||
if(!m_caching)
|
||||
return PHYSFS_tell(m_fileHandle);
|
||||
else
|
||||
return m_pos;
|
||||
@@ -136,7 +161,7 @@ int FileStream::tell()
|
||||
uint8 FileStream::getU8()
|
||||
{
|
||||
uint8 v = 0;
|
||||
if(m_fileHandle) {
|
||||
if(!m_caching) {
|
||||
if(PHYSFS_read(m_fileHandle, &v, 1, 1) != 1)
|
||||
throwError("read failed", true);
|
||||
} else {
|
||||
@@ -152,7 +177,7 @@ uint8 FileStream::getU8()
|
||||
uint16 FileStream::getU16()
|
||||
{
|
||||
uint16 v = 0;
|
||||
if(m_fileHandle) {
|
||||
if(!m_caching) {
|
||||
if(PHYSFS_readULE16(m_fileHandle, &v) == 0)
|
||||
throwError("read failed", true);
|
||||
} else {
|
||||
@@ -168,7 +193,7 @@ uint16 FileStream::getU16()
|
||||
uint32 FileStream::getU32()
|
||||
{
|
||||
uint32 v = 0;
|
||||
if(m_fileHandle) {
|
||||
if(!m_caching) {
|
||||
if(PHYSFS_readULE32(m_fileHandle, &v) == 0)
|
||||
throwError("read failed", true);
|
||||
} else {
|
||||
@@ -184,7 +209,7 @@ uint32 FileStream::getU32()
|
||||
uint64 FileStream::getU64()
|
||||
{
|
||||
uint64 v = 0;
|
||||
if(m_fileHandle) {
|
||||
if(!m_caching) {
|
||||
if(PHYSFS_readULE64(m_fileHandle, (PHYSFS_uint64*)&v) == 0)
|
||||
throwError("read failed", true);
|
||||
} else {
|
||||
@@ -223,26 +248,49 @@ std::string FileStream::getString()
|
||||
|
||||
void FileStream::addU8(uint8 v)
|
||||
{
|
||||
if(PHYSFS_write(m_fileHandle, &v, 1, 1) != 1)
|
||||
throwError("write failed", true);
|
||||
if(!m_caching) {
|
||||
if(PHYSFS_write(m_fileHandle, &v, 1, 1) != 1)
|
||||
throwError("write failed", true);
|
||||
} else {
|
||||
m_data.add(v);
|
||||
m_pos++;
|
||||
}
|
||||
}
|
||||
|
||||
void FileStream::addU16(uint16 v)
|
||||
{
|
||||
if(PHYSFS_writeULE16(m_fileHandle, v) == 0)
|
||||
throwError("write failed", true);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void FileStream::addU32(uint32 v)
|
||||
{
|
||||
if(PHYSFS_writeULE32(m_fileHandle, v) == 0)
|
||||
throwError("write failed", true);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void FileStream::addU64(uint64 v)
|
||||
{
|
||||
if(PHYSFS_writeULE64(m_fileHandle, v) == 0)
|
||||
throwError("write failed", true);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void FileStream::addString(const std::string& v)
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
#include "declarations.h"
|
||||
#include <framework/luascript/luaobject.h>
|
||||
#include <framework/util/databuffer.h>
|
||||
|
||||
struct PHYSFS_File;
|
||||
|
||||
@@ -32,7 +33,7 @@ struct PHYSFS_File;
|
||||
class FileStream : public LuaObject
|
||||
{
|
||||
public:
|
||||
FileStream(const std::string& name, PHYSFS_File *fileHandle);
|
||||
FileStream(const std::string& name, PHYSFS_File *fileHandle, bool writeable);
|
||||
~FileStream();
|
||||
|
||||
void cache();
|
||||
@@ -64,9 +65,11 @@ private:
|
||||
|
||||
std::string m_name;
|
||||
PHYSFS_File *m_fileHandle;
|
||||
|
||||
std::vector<uint8_t> m_data;
|
||||
uint m_pos;
|
||||
bool m_writeable;
|
||||
bool m_caching;
|
||||
|
||||
DataBuffer<uint8_t> m_data;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -209,7 +209,7 @@ FileStreamPtr ResourceManager::openFile(const std::string& fileName)
|
||||
g_logger.error(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getLastError()));
|
||||
return nullptr;
|
||||
}
|
||||
return FileStreamPtr(new FileStream(fullPath, file));
|
||||
return FileStreamPtr(new FileStream(fullPath, file, false));
|
||||
}
|
||||
|
||||
FileStreamPtr ResourceManager::appendFile(const std::string& fileName)
|
||||
@@ -219,7 +219,7 @@ FileStreamPtr ResourceManager::appendFile(const std::string& fileName)
|
||||
g_logger.error(stdext::format("failed to append file '%s': %s", fileName, PHYSFS_getLastError()));
|
||||
return nullptr;
|
||||
}
|
||||
return FileStreamPtr(new FileStream(fileName, file));
|
||||
return FileStreamPtr(new FileStream(fileName, file, true));
|
||||
}
|
||||
|
||||
FileStreamPtr ResourceManager::createFile(const std::string& fileName)
|
||||
@@ -229,7 +229,7 @@ FileStreamPtr ResourceManager::createFile(const std::string& fileName)
|
||||
g_logger.error(stdext::format("failed to create file '%s': %s", fileName, PHYSFS_getLastError()));
|
||||
return nullptr;
|
||||
}
|
||||
return FileStreamPtr(new FileStream(fileName, file));
|
||||
return FileStreamPtr(new FileStream(fileName, file, true));
|
||||
}
|
||||
|
||||
bool ResourceManager::deleteFile(const std::string& fileName)
|
||||
|
||||
@@ -25,7 +25,8 @@
|
||||
|
||||
#include <framework/application.h>
|
||||
|
||||
LuaObject::LuaObject() : m_fieldsTableRef(-1)
|
||||
LuaObject::LuaObject() :
|
||||
m_fieldsTableRef(-1)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -27,42 +27,75 @@ template<class T>
|
||||
class DataBuffer
|
||||
{
|
||||
public:
|
||||
DataBuffer(int res = 64) {
|
||||
m_capacity = res;
|
||||
m_buffer = new T[m_capacity];
|
||||
m_size = 0;
|
||||
DataBuffer(uint res = 64) :
|
||||
m_size(0),
|
||||
m_capacity(res),
|
||||
m_buffer(new T[m_capacity]) { }
|
||||
~DataBuffer() {
|
||||
if(m_buffer)
|
||||
delete[] m_buffer;
|
||||
}
|
||||
~DataBuffer() { delete[] m_buffer; }
|
||||
|
||||
inline void reset() { m_size = 0; }
|
||||
inline bool isEmpty() const { return m_size == 0; }
|
||||
inline void clear() {
|
||||
m_size = 0;
|
||||
m_capacity = 0;
|
||||
delete[] m_buffer;
|
||||
m_buffer = nullptr;
|
||||
}
|
||||
|
||||
inline int size() const { return m_size; }
|
||||
inline bool empty() const { return m_size == 0; }
|
||||
inline uint size() const { return m_size; }
|
||||
inline T *data() const { return m_buffer; }
|
||||
|
||||
inline const T& at(int i) const { return m_buffer[i]; }
|
||||
inline const T& at(uint i) const { return m_buffer[i]; }
|
||||
inline const T& last() const { return m_buffer[m_size-1]; }
|
||||
inline const T& first() const { return m_buffer[0]; }
|
||||
inline const T& operator[](int i) const { return m_buffer[i]; }
|
||||
inline T& operator[](int i) { return m_buffer[i]; }
|
||||
inline const T& operator[](uint i) const { return m_buffer[i]; }
|
||||
inline T& operator[](uint i) { return m_buffer[i]; }
|
||||
|
||||
inline void add(const T &t) {
|
||||
if(m_size >= m_capacity) {
|
||||
m_capacity *= 2;
|
||||
T *buffer = new T[m_capacity];
|
||||
for(int i=0;i<m_size;++i)
|
||||
inline void reserve(uint n) {
|
||||
if(n > m_capacity) {
|
||||
T *buffer = new T[n];
|
||||
for(uint i=0;i<m_size;++i)
|
||||
buffer[i] = m_buffer[i];
|
||||
delete[] m_buffer;
|
||||
if(m_buffer)
|
||||
delete[] m_buffer;
|
||||
m_buffer = buffer;
|
||||
m_capacity = n;
|
||||
}
|
||||
m_buffer[m_size++] = t;
|
||||
}
|
||||
|
||||
inline void resize(uint n, T def = T()) {
|
||||
if(n == m_size)
|
||||
return;
|
||||
reserve(n);
|
||||
for(uint i=m_size;i<n;++i)
|
||||
m_buffer[i] = def;
|
||||
m_size = n;
|
||||
}
|
||||
|
||||
inline void grow(uint n) {
|
||||
if(n <= m_size)
|
||||
return;
|
||||
if(n > m_capacity) {
|
||||
uint newcapacity = m_capacity;
|
||||
do { newcapacity *= 2; } while(newcapacity < n);
|
||||
reserve(newcapacity);
|
||||
}
|
||||
m_size = n;
|
||||
}
|
||||
|
||||
inline void add(const T& v) {
|
||||
grow(m_size + 1);
|
||||
m_buffer[m_size-1] = v;
|
||||
}
|
||||
|
||||
inline DataBuffer &operator<<(const T &t) { add(t); return *this; }
|
||||
|
||||
private:
|
||||
int m_size;
|
||||
int m_capacity;
|
||||
uint m_size;
|
||||
uint m_capacity;
|
||||
T *m_buffer;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user