Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/base/StarMemoryAssetSource.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/base/StarMemoryAssetSource.cpp')
-rw-r--r--source/base/StarMemoryAssetSource.cpp59
1 files changed, 46 insertions, 13 deletions
diff --git a/source/base/StarMemoryAssetSource.cpp b/source/base/StarMemoryAssetSource.cpp
index 17e3749..a0c220d 100644
--- a/source/base/StarMemoryAssetSource.cpp
+++ b/source/base/StarMemoryAssetSource.cpp
@@ -1,7 +1,7 @@
#include "StarMemoryAssetSource.hpp"
#include "StarDataStreamDevices.hpp"
#include "StarDataStreamExtra.hpp"
-#include "StarSha256.hpp"
+#include "StarImage.hpp"
namespace Star {
@@ -21,11 +21,17 @@ StringList MemoryAssetSource::assetPaths() const {
IODevicePtr MemoryAssetSource::open(String const& path) {
struct AssetReader : public IODevice {
- AssetReader(ByteArrayPtr assetData, String name) : assetData(assetData), name(name) { setMode(IOMode::Read); }
+ AssetReader(char* assetData, size_t assetSize, String name) {
+ this->assetData = assetData;
+ this->assetSize = assetSize;
+ this->name = std::move(name);
+ setMode(IOMode::Read);
+ }
size_t read(char* data, size_t len) override {
- len = min<StreamOffset>(len, StreamOffset(assetData->size()) - assetPos);
- assetData->copyTo(data, len);
+ len = min<StreamOffset>(len, StreamOffset(assetSize) - assetPos);
+ memcpy(data, assetData + assetPos, len);
+ assetPos += len;
return len;
}
@@ -33,25 +39,26 @@ IODevicePtr MemoryAssetSource::open(String const& path) {
throw IOException("Assets IODevices are read-only");
}
- StreamOffset size() override { return assetData->size(); }
+ StreamOffset size() override { return assetSize; }
StreamOffset pos() override { return assetPos; }
String deviceName() const override { return name; }
bool atEnd() override {
- return assetPos >= assetData->size();
+ return assetPos >= assetSize;
}
void seek(StreamOffset p, IOSeek mode) override {
if (mode == IOSeek::Absolute)
assetPos = p;
else if (mode == IOSeek::Relative)
- assetPos = clamp<StreamOffset>(assetPos + p, 0, assetData->size());
+ assetPos = clamp<StreamOffset>(assetPos + p, 0, assetSize);
else
- assetPos = clamp<StreamOffset>(assetPos - p, 0, assetData->size());
+ assetPos = clamp<StreamOffset>(assetPos - p, 0, assetSize);
}
- ByteArrayPtr assetData;
+ char* assetData;
+ size_t assetSize;
StreamOffset assetPos = 0;
String name;
};
@@ -59,8 +66,12 @@ IODevicePtr MemoryAssetSource::open(String const& path) {
auto p = m_files.ptr(path);
if (!p)
throw AssetSourceException::format("Requested file '{}' does not exist in memory", path);
-
- return make_shared<AssetReader>(*p, path);
+ else if (auto byteArray = p->ptr<ByteArray>())
+ return make_shared<AssetReader>(byteArray->ptr(), byteArray->size(), path);
+ else {
+ auto image = p->get<ImagePtr>().get();
+ return make_shared<AssetReader>((char*)image->data(), image->width() * image->height() * image->bytesPerPixel(), path);
+ }
}
bool MemoryAssetSource::empty() const {
@@ -76,15 +87,37 @@ bool MemoryAssetSource::erase(String const& path) {
}
void MemoryAssetSource::set(String const& path, ByteArray data) {
- m_files[path] = make_shared<ByteArray>(std::move(data));
+ m_files[path] = std::move(data);
+}
+
+void MemoryAssetSource::set(String const& path, Image const& image) {
+ m_files[path] = make_shared<Image>(image);
+}
+
+void MemoryAssetSource::set(String const& path, Image&& image) {
+ m_files[path] = make_shared<Image>(std::move(image));
}
ByteArray MemoryAssetSource::read(String const& path) {
auto p = m_files.ptr(path);
if (!p)
throw AssetSourceException::format("Requested file '{}' does not exist in memory", path);
+ else if (auto bytes = p->ptr<ByteArray>())
+ return *bytes;
+ else {
+ Image const* image = p->get<ImagePtr>().get();
+ return ByteArray((char const*)image->data(), image->width() * image->height() * image->bytesPerPixel());
+ }
+}
+
+ImageConstPtr MemoryAssetSource::image(String const& path) {
+ auto p = m_files.ptr(path);
+ if (!p)
+ throw AssetSourceException::format("Requested file '{}' does not exist in memory", path);
+ else if (auto imagePtr = p->ptr<ImagePtr>())
+ return *imagePtr;
else
- return *p->get(); // this is a double indirection, and that freaking sucks!!
+ return nullptr;
}
}