diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2025-03-05 21:18:53 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-05 21:18:53 +1100 |
commit | b77101f031e7fc49abf6bfa269b1036d368b552c (patch) | |
tree | 52f7b44b44c19dd9cafaba5f463b82ee32d6c1df | |
parent | 149d9fb606cbbc0d316e9836945630708a6558bc (diff) | |
parent | 33bb286cfb2a99ee785827c0f92e02acd2ec0cfd (diff) |
Merge pull request #206 from Bottinator22/main
Allow persistently preloading assets
-rw-r--r-- | assets/opensb/preload.config | 6 | ||||
-rw-r--r-- | source/base/StarAssets.cpp | 26 | ||||
-rw-r--r-- | source/base/StarAssets.hpp | 25 |
3 files changed, 43 insertions, 14 deletions
diff --git a/assets/opensb/preload.config b/assets/opensb/preload.config new file mode 100644 index 0000000..f13d799 --- /dev/null +++ b/assets/opensb/preload.config @@ -0,0 +1,6 @@ +[ + { + "type": "json", + "path": "/player.config:inventoryFilters" + } +] diff --git a/source/base/StarAssets.cpp b/source/base/StarAssets.cpp index 8471a0c..5bb752e 100644 --- a/source/base/StarAssets.cpp +++ b/source/base/StarAssets.cpp @@ -23,7 +23,7 @@ #include "StarUtilityLuaBindings.hpp" namespace Star { - + static void validateBasePath(std::string_view const& basePath) { if (basePath.empty() || basePath[0] != '/') throw AssetException(strf("Path '{}' must be absolute", basePath)); @@ -353,6 +353,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, type == AssetType::Image); + + auto asset = getAsset(AssetId{type, std::move(components)}); + // make this asset never unload + asset->forcePersist = true; + } } Assets::~Assets() { @@ -626,23 +640,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) { diff --git a/source/base/StarAssets.hpp b/source/base/StarAssets.hpp index 6ffcb05..a8e4480 100644 --- a/source/base/StarAssets.hpp +++ b/source/base/StarAssets.hpp @@ -71,20 +71,20 @@ public: StringList digestIgnore; }; - enum class AssetType { - Json, - Image, - Audio, - Font, - Bytes - }; - enum class QueuePriority { None, Working, PostProcess, Load }; + + enum class AssetType { + Json, + Image, + Audio, + Font, + Bytes + }; struct AssetId { AssetType type; @@ -107,6 +107,7 @@ public: double time = 0.0; bool needsPostProcessing = false; + bool forcePersist = false; }; struct JsonData : AssetData { @@ -255,6 +256,14 @@ public: void cleanup(); private: + EnumMap<AssetType> 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<AssetId> const& assetIds) const; |