From 8a6a8086da370021d1812cfc956cd082c8a41757 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Mon, 1 Jul 2019 00:07:44 +0100 Subject: [PATCH] Make in-memory buffers seekable to fix fog of war --- components/files/memorystream.hpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/components/files/memorystream.hpp b/components/files/memorystream.hpp index 9a3510044..b4c7b7675 100644 --- a/components/files/memorystream.hpp +++ b/components/files/memorystream.hpp @@ -9,11 +9,31 @@ namespace Files struct MemBuf : std::streambuf { MemBuf(char const* buffer, size_t size) - { // a streambuf isn't specific to istreams, so we need a non-const pointer :/ - char* nonconstBuffer = (const_cast(buffer)); - this->setg(nonconstBuffer, nonconstBuffer, nonconstBuffer + size); + : bufferStart(const_cast(buffer)) + , bufferEnd(bufferStart + size) + { + this->setg(bufferStart, bufferStart, bufferEnd); + } + + pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) override + { + if (dir == std::ios_base::cur) + gbump(off); + else + setg(bufferStart, (dir == std::ios_base::beg ? bufferStart : bufferEnd) + off, bufferEnd); + + return gptr() - bufferStart; } + + pos_type seekpos(pos_type pos, std::ios_base::openmode which) override + { + return seekoff(pos, std::ios_base::beg, which); + } + + protected: + char* bufferStart; + char* bufferEnd; }; /// @brief A variant of std::istream that reads from a constant in-memory buffer.