Toast Engine
Loading...
Searching...
No Matches
asset_manager.hpp
Go to the documentation of this file.
1
15
16#pragma once
17
18#include "assets.hpp"
19#include "pack.hpp"
20#include "types.hpp"
21
22#include <filesystem>
23#include <memory>
24#include <mutex>
25#include <optional>
26#include <string>
27#include <toast/events/listener.hpp>
28#include <unordered_map>
29#include <unordered_set>
30#include <vector>
31
32namespace assets {
33
34struct AssetInfo {
35 std::string path;
36 std::string type;
37};
38
39struct Paths {
40 std::filesystem::path project;
41 std::filesystem::path artworks;
42 std::filesystem::path cache;
43 std::filesystem::path saved;
44 std::filesystem::path core;
45};
46
67class AssetManager {
68public:
69 AssetManager();
70 ~AssetManager() = default;
71
72 static auto get() noexcept -> AssetManager&;
73
74 [[nodiscard]]
75 static auto isInitialized() noexcept -> bool {
76 return instance != nullptr;
77 }
78
85 auto load(toast::UID uid) -> Asset*;
86
93 auto load(std::string_view uri) -> Asset*;
94
100 auto save(toast::UID uid) -> bool;
101
107 auto save(std::string_view uri) -> bool;
108
116 auto saveBytes(std::string_view uri, const std::vector<uint8_t>& data) -> bool;
117 void replacePrefab(toast::UID uid, const Prefab& prefab);
118
124 auto loadBytes(std::string_view uri) -> std::optional<std::vector<uint8_t>>;
125
129 auto tryLoadBytes(std::string_view uri) -> std::optional<std::vector<uint8_t>>;
130
135 void reloadManifest();
136
141 void clearUnusedAssets();
142
148 static void setPaths(Paths&& paths);
149
154 static void registerDatabase(std::string_view name, std::filesystem::path root);
155
160 static void clearDatabases();
161
165 static auto projectRoot() -> const std::filesystem::path&;
166
172 static auto resolveURI(std::string_view uri) -> std::optional<toast::UID>;
173
179 static auto getURI(toast::UID uid) -> std::string;
180
186 auto search(std::string_view query) -> std::vector<Handle<Asset>>;
187
188 auto listByType(std::string_view type) -> std::vector<toast::UID>;
189
194 static auto typeOf(toast::UID uid) -> std::string;
195
197 toast::UID uid;
198 std::string type;
199 bool replaced = false;
200 std::shared_ptr<const Prefab> prefab;
201 };
202
206 void pollModifiedAssets();
207
208 [[nodiscard]]
209 auto hasPendingReloads() const -> bool;
210
214 auto applyPendingReloads() -> std::vector<ReloadedAsset>;
215
216 [[nodiscard]]
217 auto getCachePath() const -> const std::filesystem::path&;
218
222 static void setLoadMode(SaveMode mode);
223
227 static auto getLoadMode() -> SaveMode;
228
236 static void mountPack(std::string_view scheme, const std::filesystem::path& pak_path);
237
238private:
239 static inline AssetManager* instance = nullptr;
240
241 static inline SaveMode load_mode = SaveMode::editor;
242
243 static inline std::unordered_map<std::string, std::unique_ptr<PackArchive>> mounts;
244
245 event::Listener listener;
246 mutable std::mutex mutex;
247 std::unordered_map<uint64_t, AssetInfo> manifest;
248 std::unordered_map<uint64_t, std::unique_ptr<Asset>> cache;
249 std::unordered_map<uint64_t, std::filesystem::file_time_type> asset_mtimes;
250 std::unordered_set<uint64_t> pending_reloads;
251 std::vector<std::unique_ptr<Asset>> retired_assets;
252
254 auto createAsset(const AssetInfo& info, std::vector<uint8_t>& raw_data) -> std::unique_ptr<Asset>;
255
256 static inline std::unordered_map<std::string, std::filesystem::path> roots;
257
258 auto resolveVirtualPath(std::string_view virtual_path) -> std::optional<std::filesystem::path>;
259 auto readVirtualPath(std::string_view virtual_path) -> std::optional<std::vector<uint8_t>>;
260 auto openFile(const std::filesystem::path& path) -> std::optional<std::vector<uint8_t>>;
261 auto saveFile(const std::filesystem::path& path, const std::vector<uint8_t>& data) -> bool;
262};
263
264}
Public API for the asset management system.
auto save(toast::UID uid) -> bool
Serializes the cached asset at the given UID back to its backing file.
Definition asset_manager.cpp:231
static void mountPack(std::string_view scheme, const std::filesystem::path &pak_path)
Mounts a .pak archive at a URI scheme.
Definition asset_manager.cpp:50
auto tryLoadBytes(std::string_view uri) -> std::optional< std::vector< uint8_t > >
Like loadBytes() but silent on misses.
Definition asset_manager.cpp:303
static auto typeOf(toast::UID uid) -> std::string
Looks up an asset's type string in the manifest without loading it.
Definition asset_manager.cpp:576
static auto getURI(toast::UID uid) -> std::string
Definition asset_manager.cpp:524
static auto projectRoot() -> const std::filesystem::path &
Returns the project root path (project:// root).
Definition asset_manager.cpp:423
void reloadManifest()
Re-reads the project manifest from disk.
Definition asset_manager.cpp:324
static void clearDatabases()
Removes all content database schemes (non-fixed), leaving only special roots intact.
Definition asset_manager.cpp:415
auto search(std::string_view query) -> std::vector< Handle< Asset > >
Searches the manifest for entries whose path contains the given query string.
Definition asset_manager.cpp:535
auto applyPendingReloads() -> std::vector< ReloadedAsset >
Reloads the assets pollModifiedAssets() changed.
Definition asset_manager.cpp:654
void clearUnusedAssets()
Evicts all assets whose ref count is zero from the cache.
Definition asset_manager.cpp:389
auto saveBytes(std::string_view uri, const std::vector< uint8_t > &data) -> bool
Writes raw bytes to the path resolved by a virtual URI, bypassing the manifest.
Definition asset_manager.cpp:282
void pollModifiedAssets()
Searches what assets have been modified.
Definition asset_manager.cpp:617
static void registerDatabase(std::string_view name, std::filesystem::path root)
Registers a content database scheme.
Definition asset_manager.cpp:411
static auto resolveURI(std::string_view uri) -> std::optional< toast::UID >
Maps a virtual URI to its manifest UID.
Definition asset_manager.cpp:515
static auto getLoadMode() -> SaveMode
Definition asset_manager.cpp:46
static void setLoadMode(SaveMode mode)
Selects the asset load mode.
Definition asset_manager.cpp:41
static void setPaths(Paths &&paths)
Configures the five fixed special URI roots; must be called before Engine::init().
Definition asset_manager.cpp:403
auto load(toast::UID uid) -> Asset *
Loads an asset by UID, returning the cached copy if already resident.
Definition asset_manager.cpp:170
auto loadBytes(std::string_view uri) -> std::optional< std::vector< uint8_t > >
Reads raw bytes from the URI, bypassing the manifest.
Definition asset_manager.cpp:292
Base class for all engine assets.
Definition core_types.hpp:45
Definition pack.hpp:33
Definition prefab.hpp:54
SaveMode
Definition core_types.hpp:25
Definition App.axaml.cs:9
Read-side parser for the PACK v2 archive format produced by tools/asset_packer.
Definition asset_manager.hpp:34
Definition asset_manager.hpp:196
Definition asset_manager.hpp:39
std::filesystem::path project
project root folder; special databases are derived from here
Definition asset_manager.hpp:40
Definition uid.hpp:20
Concrete asset types for the Toast engine.