From 560ae08424956bb495bc2453973467d138029c7c Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Mon, 25 Mar 2024 03:46:21 +1100 Subject: Add support for directly setting image assets and processing Image userdata --- source/base/StarAssets.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'source/base/StarAssets.cpp') diff --git a/source/base/StarAssets.cpp b/source/base/StarAssets.cpp index d4b7228..9718ec8 100644 --- a/source/base/StarAssets.cpp +++ b/source/base/StarAssets.cpp @@ -145,7 +145,10 @@ Assets::Assets(Settings settings, StringList assetSources) { ByteArray bytes; if (auto str = engine.luaMaybeTo(data)) bytes = ByteArray(str->utf8Ptr(), str->utf8Size()); - else { + else if (auto image = engine.luaMaybeTo(data)) { + newFiles->set(path, std::move(*image)); + return; + } else { auto json = engine.luaTo(data).repr(); bytes = ByteArray(json.utf8Ptr(), json.utf8Size()); } @@ -855,6 +858,19 @@ ByteArray Assets::read(String const& path) const { throw AssetException(strf("No such asset '{}'", path)); } +ImageConstPtr Assets::readImage(String const& path) const { + if (auto p = m_files.ptr(path)) { + ImageConstPtr image; + if (auto memorySource = as(p->source)) + image = memorySource->image(p->sourceName); + if (!image) + image = make_shared(Image::readPng(p->source->open(p->sourceName))); + return image; + } + throw AssetException(strf("No such asset '{}'", path)); +} + + Json Assets::checkPatchArray(String const& path, AssetSourcePtr const& source, Json const result, JsonArray const patchData, Maybe const external) const { auto externalRef = external.value(); auto newResult = result; @@ -1140,7 +1156,7 @@ shared_ptr Assets::loadImage(AssetPath const& path) const { } else { auto imageData = make_shared(); imageData->image = unlockDuring([&]() { - return make_shared(Image::readPng(open(path.basePath))); + return readImage(path.basePath); }); imageData->frames = bestFramesSpecification(path.basePath); -- cgit v1.2.3