1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-12-25 14:15:46 +00:00

Fix problems with loading saves after saving over them.

This was caused by the VFS caching file sizes,
and not noticing that the file size had changed,
which confused zlib because the data stream ended too early.
This commit is contained in:
fgenesis 2012-06-14 17:54:40 +02:00
parent 6d92cd24c8
commit 8f2279e28a
4 changed files with 27 additions and 29 deletions

View file

@ -458,9 +458,9 @@ char *readFile(const std::string& path, unsigned long *size_ret)
VFILE *vf = vfs.GetFile(path.c_str());
if (!vf)
return NULL;
fileSize = vf->size();
char *buffer = (char*)vf->getBuf(NULL, NULL);
vf->dropBuf(false);
fileSize = vf->size();
vf->dropBuf(false); // unlink buffer from file
#else
FILE *f = fopen(path.c_str(), "rb");
if (!f)
@ -1128,6 +1128,8 @@ char *readCompressedFile(std::string path, unsigned long *size_ret)
{
unsigned long size = 0;
char *buf = readFile(path, &size);
if(!buf)
return NULL;
ZlibCompressor z; // allocates with new[] by default
z.init(buf, size, ByteBuffer::TAKE_OVER);
z.Compressed(true);

View file

@ -76,7 +76,7 @@ void VFSFile::dropBuf(bool del)
}
VFSFileReal::VFSFileReal(const char *name /* = NULL */)
: VFSFile(name), _fh(NULL), _size(npos), _buf(NULL)
: VFSFile(name), _fh(NULL), _buf(NULL)
{
}
@ -94,14 +94,8 @@ bool VFSFileReal::open(const char *mode /* = NULL */)
dropBuf(true);
_fh = real_fopen(fullname(), mode ? mode : "rb");
if(!_fh)
return false;
real_fseek((FILE*)_fh, 0, SEEK_END);
_size = getpos();
real_fseek((FILE*)_fh, 0, SEEK_SET);
return true;
return !!_fh;
}
bool VFSFileReal::isopen(void) const
@ -177,13 +171,7 @@ unsigned int VFSFileReal::write(const void *src, unsigned int bytes)
vfspos VFSFileReal::size(void)
{
VFS_GUARD_OPT(this);
if(_size != npos)
return _size;
open();
close();
// now size is known.
return _size;
return GetFileSize(fullname());
}
// ------------- VFSFileMem -----------------------

View file

@ -73,7 +73,7 @@ public:
virtual const void *getBuf(allocator_func alloc = NULL, delete_func del = NULL);
/** If del is true, delete internal buffer. If false, unregister internal buffer from the file,
but do not delete. Use free() or an appropriate deletion function later. */
but do not delete. Use delete[] or an appropriate deletion function later. */
virtual void dropBuf(bool del);
/** Basic RTTI, for debugging purposes */
@ -117,7 +117,6 @@ public:
protected:
void *_fh; // FILE*
vfspos _size;
void *_buf;
};

View file

@ -18,11 +18,12 @@
# include <windows.h>
#else
# include <sys/dir.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
VFS_NAMESPACE_START
std::string stringToLower(std::string s)
@ -259,16 +260,24 @@ bool CreateDirRec(const char *dir)
vfspos GetFileSize(const char* fn)
{
if(!fn || !*fn)
#ifdef VFS_LARGEFILE_SUPPORT
# ifdef _MSC_VER
struct _stat64 st;
if(_stat64(fn, &st))
return 0;
void *fp = real_fopen(fn, "rb");
if(!fp)
return st.st_size;
# else // _MSC_VER
struct stat64 st;
if(stat64(fn, &st))
return 0;
real_fseek(fp, 0, SEEK_END);
vfspos s = real_ftell(fp);
real_fclose(fp);
return s == npos ? 0 : s;
return st.st_size;
# endif
#else // VFS_LARGEFILE_SUPPORT
struct stat st;
if(stat(fn, &st))
return 0;
return st.st_size;
#endif
}
std::string FixSlashes(const std::string& s)