1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-12-25 22:25: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()); VFILE *vf = vfs.GetFile(path.c_str());
if (!vf) if (!vf)
return NULL; return NULL;
fileSize = vf->size();
char *buffer = (char*)vf->getBuf(NULL, NULL); char *buffer = (char*)vf->getBuf(NULL, NULL);
vf->dropBuf(false); fileSize = vf->size();
vf->dropBuf(false); // unlink buffer from file
#else #else
FILE *f = fopen(path.c_str(), "rb"); FILE *f = fopen(path.c_str(), "rb");
if (!f) if (!f)
@ -1128,6 +1128,8 @@ char *readCompressedFile(std::string path, unsigned long *size_ret)
{ {
unsigned long size = 0; unsigned long size = 0;
char *buf = readFile(path, &size); char *buf = readFile(path, &size);
if(!buf)
return NULL;
ZlibCompressor z; // allocates with new[] by default ZlibCompressor z; // allocates with new[] by default
z.init(buf, size, ByteBuffer::TAKE_OVER); z.init(buf, size, ByteBuffer::TAKE_OVER);
z.Compressed(true); z.Compressed(true);

View file

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

View file

@ -73,7 +73,7 @@ public:
virtual const void *getBuf(allocator_func alloc = NULL, delete_func del = NULL); 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, /** 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); virtual void dropBuf(bool del);
/** Basic RTTI, for debugging purposes */ /** Basic RTTI, for debugging purposes */
@ -117,7 +117,6 @@ public:
protected: protected:
void *_fh; // FILE* void *_fh; // FILE*
vfspos _size;
void *_buf; void *_buf;
}; };

View file

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