From 8f2279e28a0b35013af4ed45ceffc4135dc0d1f5 Mon Sep 17 00:00:00 2001 From: fgenesis Date: Thu, 14 Jun 2012 17:54:40 +0200 Subject: [PATCH] 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. --- BBGE/Base.cpp | 6 ++++-- ExternalLibs/ttvfs/VFSFile.cpp | 18 +++--------------- ExternalLibs/ttvfs/VFSFile.h | 3 +-- ExternalLibs/ttvfs/VFSTools.cpp | 29 +++++++++++++++++++---------- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/BBGE/Base.cpp b/BBGE/Base.cpp index 888a960..7d2d4e6 100644 --- a/BBGE/Base.cpp +++ b/BBGE/Base.cpp @@ -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); diff --git a/ExternalLibs/ttvfs/VFSFile.cpp b/ExternalLibs/ttvfs/VFSFile.cpp index 74a328f..275fa75 100644 --- a/ExternalLibs/ttvfs/VFSFile.cpp +++ b/ExternalLibs/ttvfs/VFSFile.cpp @@ -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 ----------------------- diff --git a/ExternalLibs/ttvfs/VFSFile.h b/ExternalLibs/ttvfs/VFSFile.h index 2ed3273..5aef550 100644 --- a/ExternalLibs/ttvfs/VFSFile.h +++ b/ExternalLibs/ttvfs/VFSFile.h @@ -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; }; diff --git a/ExternalLibs/ttvfs/VFSTools.cpp b/ExternalLibs/ttvfs/VFSTools.cpp index 11165c1..5bb5e03 100644 --- a/ExternalLibs/ttvfs/VFSTools.cpp +++ b/ExternalLibs/ttvfs/VFSTools.cpp @@ -18,11 +18,12 @@ # include #else # include -# include -# include # include #endif +#include +#include + 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)