diff --git a/simgear/io/iostreams/sgstream.cxx b/simgear/io/iostreams/sgstream.cxx old mode 100644 new mode 100755 index fc20e73a..70e1e793 --- a/simgear/io/iostreams/sgstream.cxx +++ b/simgear/io/iostreams/sgstream.cxx @@ -227,6 +227,20 @@ void sg_ifstream::open( const SGPath& name, ios_openmode io_mode ) std::ifstream::open(ps.c_str(), io_mode); } +std::string sg_ifstream::read_all() +{ + this->seekg(0, std::ios::end); // seek to end + const auto pos = this->tellg(); + + std::string result; + result.resize(pos); + + this->seekg(0, std::ios::beg); + this->read(const_cast(result.data()), pos); + return result; +} + + sg_ofstream::sg_ofstream(const SGPath& path, ios_openmode io_mode) { #if defined(SG_WINDOWS) diff --git a/simgear/io/iostreams/sgstream.hxx b/simgear/io/iostreams/sgstream.hxx index 948d14ee..067a3830 100644 --- a/simgear/io/iostreams/sgstream.hxx +++ b/simgear/io/iostreams/sgstream.hxx @@ -196,6 +196,10 @@ public: void open( const SGPath& name, ios_openmode io_mode = ios_in|ios_binary ); + + /// read the entire stream into a buffer. Use on files, etc - not recommended on streams + /// which never EOF, will bvlock forever. + std::string read_all(); }; class sg_ofstream : public std::ofstream diff --git a/simgear/package/Root.cxx b/simgear/package/Root.cxx old mode 100644 new mode 100755 index be6254c7..d064751f --- a/simgear/package/Root.cxx +++ b/simgear/package/Root.cxx @@ -178,8 +178,7 @@ public: SG_LOG(SG_IO, SG_WARN, "Download failed for: " << u << ", will use old cached data"); cachePath.touch(); // touch the file so we don't repeat this danxce // kick a load from the cache - - queueLoadFromPersistentCache(u, cachePath); + loadFromPersistentCache(u, cachePath); } } @@ -202,6 +201,7 @@ public: std::string u = pendingThumbnails.front(); pendingThumbnails.pop_front(); + thumbnailDownloadRequest = new Root::ThumbnailDownloader(this, u); if (http) { @@ -260,11 +260,11 @@ public: return false; } - queueLoadFromPersistentCache(url, cachePath); + loadFromPersistentCache(url, cachePath); return true; } - void queueLoadFromPersistentCache(const std::string& url, const SGPath& path) + void loadFromPersistentCache(const std::string& url, const SGPath& path) { assert(path.exists()); @@ -277,17 +277,9 @@ public: assert(it->second.pathOnDisk.isNull() || (it->second.pathOnDisk == path)); } - if (it->second.requestPending) { - return; // all done - } - - it->second.requestPending = true; - auto dl = new Root::ThumbnailDownloader(this, path.fileUrl(), url); - if (http) { - http->makeRequest(dl); - } else { - httpPendingRequests.push_back(dl); - } + sg_ifstream thumbnailStream(path, std::ios::in | std::ios::binary); + string bytes = thumbnailStream.read_all(); + fireDataForThumbnail(url, reinterpret_cast(bytes.data()), bytes.size()); } DelegateVec delegates; @@ -813,7 +805,7 @@ void Root::requestThumbnailData(const std::string& aUrl) } } else { if (!it->second.requestPending && it->second.pathOnDisk.exists()) { - d->queueLoadFromPersistentCache(aUrl, it->second.pathOnDisk); + d->loadFromPersistentCache(aUrl, it->second.pathOnDisk); } } } diff --git a/simgear/scene/material/Effect.cxx b/simgear/scene/material/Effect.cxx index 57f6ac29..63a4b22e 100644 --- a/simgear/scene/material/Effect.cxx +++ b/simgear/scene/material/Effect.cxx @@ -107,14 +107,7 @@ bool loadShaderFromUTF8File(osg::Shader* shader, const std::string& fileName) if (!inStream.is_open()) return false; - string shaderSource; - while (!inStream.eof()) { - char bytes[8192]; - inStream.read(bytes, 8192); - shaderSource.append(bytes, inStream.gcount()); - } - inStream.close(); - shader->setShaderSource(shaderSource); + shader->setShaderSource(inStream.read_all()); return true; }