From 76a64738d521d69f214654638615387fdc1423c0 Mon Sep 17 00:00:00 2001 From: Bottinator22 Date: Sun, 2 Mar 2025 15:29:52 -0800 Subject: allow persistently preloading assets --- source/base/StarAssets.cpp | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'source/base/StarAssets.cpp') diff --git a/source/base/StarAssets.cpp b/source/base/StarAssets.cpp index 8471a0c..7fad6be 100644 --- a/source/base/StarAssets.cpp +++ b/source/base/StarAssets.cpp @@ -24,6 +24,14 @@ namespace Star { +EnumMap const AssetTypeNames{ + {AssetType::Json, "json"}, + {AssetType::Image, "image"}, + {AssetType::Audio, "audio"}, + {AssetType::Font, "font"}, + {AssetType::Bytes, "bytes"} +}; + static void validateBasePath(std::string_view const& basePath) { if (basePath.empty() || basePath[0] != '/') throw AssetException(strf("Path '{}' must be absolute", basePath)); @@ -353,6 +361,20 @@ Assets::Assets(Settings settings, StringList assetSources) { int workerPoolSize = m_settings.workerPoolSize; for (int i = 0; i < workerPoolSize; i++) m_workerThreads.append(Thread::invoke("Assets::workerMain", mem_fn(&Assets::workerMain), this)); + + // preload.config contains an array of files which will be loaded and then told to persist + Json preload = json("/preload.config"); + Logger::info("Preloading assets"); + for (auto script : preload.iterateArray()) { + auto type = AssetTypeNames.getLeft(script.getString("type")); + auto path = script.getString("path"); + auto components = AssetPath::split(path); + validatePath(components, type == AssetType::Json, type == AssetType::Image); + + auto asset = getAsset(AssetId{type, std::move(components)}); + // make this asset never unload + asset->forcePersist = true; + } } Assets::~Assets() { @@ -626,23 +648,23 @@ size_t Assets::AssetIdHash::operator()(AssetId const& id) const { } bool Assets::JsonData::shouldPersist() const { - return !json.unique(); + return forcePersist || !json.unique(); } bool Assets::ImageData::shouldPersist() const { - return !alias && !image.unique(); + return forcePersist || !alias && !image.unique(); } bool Assets::AudioData::shouldPersist() const { - return !audio.unique(); + return forcePersist || !audio.unique(); } bool Assets::FontData::shouldPersist() const { - return !font.unique(); + return forcePersist || !font.unique(); } bool Assets::BytesData::shouldPersist() const { - return !bytes.unique(); + return forcePersist || !bytes.unique(); } FramesSpecification Assets::parseFramesSpecification(Json const& frameConfig, String path) { -- cgit v1.2.3 From 1af60db5b25143a8dd937b512c3ddfd2e275a4ab Mon Sep 17 00:00:00 2001 From: Bottinator22 Date: Sun, 2 Mar 2025 20:18:31 -0800 Subject: minor improvements --- source/base/StarAssets.cpp | 2 +- source/base/StarAssets.hpp | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'source/base/StarAssets.cpp') diff --git a/source/base/StarAssets.cpp b/source/base/StarAssets.cpp index 7fad6be..8622b87 100644 --- a/source/base/StarAssets.cpp +++ b/source/base/StarAssets.cpp @@ -369,7 +369,7 @@ Assets::Assets(Settings settings, StringList assetSources) { auto type = AssetTypeNames.getLeft(script.getString("type")); auto path = script.getString("path"); auto components = AssetPath::split(path); - validatePath(components, type == AssetType::Json, type == AssetType::Image); + validatePath(components, type == AssetType::Json || type == AssetType::Image, type == AssetType::Image); auto asset = getAsset(AssetId{type, std::move(components)}); // make this asset never unload diff --git a/source/base/StarAssets.hpp b/source/base/StarAssets.hpp index e848410..203214a 100644 --- a/source/base/StarAssets.hpp +++ b/source/base/StarAssets.hpp @@ -45,7 +45,6 @@ enum class AssetType { Font, Bytes }; -extern EnumMap const AssetTypeNames; // The assets system can load image, font, json, and data assets from a set of // sources. Each source is either a directory on the filesystem or a single -- cgit v1.2.3 From 33bb286cfb2a99ee785827c0f92e02acd2ec0cfd Mon Sep 17 00:00:00 2001 From: Bottinator22 Date: Sun, 2 Mar 2025 20:25:08 -0800 Subject: improvement --- source/base/StarAssets.cpp | 8 -------- source/base/StarAssets.hpp | 24 ++++++++++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) (limited to 'source/base/StarAssets.cpp') diff --git a/source/base/StarAssets.cpp b/source/base/StarAssets.cpp index 8622b87..5bb752e 100644 --- a/source/base/StarAssets.cpp +++ b/source/base/StarAssets.cpp @@ -23,14 +23,6 @@ #include "StarUtilityLuaBindings.hpp" namespace Star { - -EnumMap const AssetTypeNames{ - {AssetType::Json, "json"}, - {AssetType::Image, "image"}, - {AssetType::Audio, "audio"}, - {AssetType::Font, "font"}, - {AssetType::Bytes, "bytes"} -}; static void validateBasePath(std::string_view const& basePath) { if (basePath.empty() || basePath[0] != '/') diff --git a/source/base/StarAssets.hpp b/source/base/StarAssets.hpp index 203214a..a8e4480 100644 --- a/source/base/StarAssets.hpp +++ b/source/base/StarAssets.hpp @@ -38,14 +38,6 @@ struct FramesSpecification { StringMap aliases; }; -enum class AssetType { - Json, - Image, - Audio, - Font, - Bytes -}; - // The assets system can load image, font, json, and data assets from a set of // sources. Each source is either a directory on the filesystem or a single // packed asset file. @@ -85,6 +77,14 @@ public: PostProcess, Load }; + + enum class AssetType { + Json, + Image, + Audio, + Font, + Bytes + }; struct AssetId { AssetType type; @@ -256,6 +256,14 @@ public: void cleanup(); private: + EnumMap const AssetTypeNames{ + {AssetType::Json, "json"}, + {AssetType::Image, "image"}, + {AssetType::Audio, "audio"}, + {AssetType::Font, "font"}, + {AssetType::Bytes, "bytes"} + }; + static FramesSpecification parseFramesSpecification(Json const& frameConfig, String path); void queueAssets(List const& assetIds) const; -- cgit v1.2.3