5#include <entt/entt.hpp>
10#include <unordered_map>
49 template <
typename Key,
typename AssetClass>
52 template <
typename Key,
typename AssetClass>
55 template <
typename Key,
typename AssetClass>
65 template <
typename Key,
typename AssetClass>
83 std::unique_ptr<AssetClass>
asset;
92 template <
typename Key,
typename AssetClass>
103 const auto record =
record_.lock();
104 return record ? record->state :
UNLOADED;
112 [[nodiscard]] AssetClass*
get()
const
114 const auto record =
record_.lock();
115 return record && record->state ==
LOADED ? record->asset.get() :
nullptr;
120 const auto record =
record_.lock();
121 return record ? record->generation : 0;
126 static const std::string empty;
127 const auto record =
record_.lock();
128 return record ? record->lastError : empty;
133 const auto record =
record_.lock();
137 [[nodiscard]]
explicit operator bool()
const
145 explicit Ref(std::shared_ptr<RecordType> record) :
record_(
std::move(record)) {}
149 static std::shared_future<Result> future = []
151 std::promise<Result> promise;
153 result.
error =
"Asset record expired";
154 promise.set_value(std::move(result));
155 return promise.get_future().share();
170 explicit AssetBase(
bool initializeDefaultEntity =
false) :
249 template <
typename Key,
typename AssetClass>
268 template <
typename... Args>
278 void add(Key path, AssetClass* asset);
298 void resolve(
const Key& path, std::unique_ptr<AssetClass> asset);
303 void fail(
const Key& path, std::string error);
346 std::optional<AssetClass*>
getAsset(
const Key& path);
364 template<
typename Func>
366 for (
const auto& [key, record] :
records) {
367 if (record && record->asset !=
nullptr) {
368 func(key, record->asset.get());
375 [[nodiscard]] std::shared_ptr<RecordType>
getRecord(
const Key& path)
const;
376 void completeRecord(
const std::shared_ptr<RecordType>& record,
bool success, std::string error = {});
379 std::unordered_map<Key, std::shared_ptr<RecordType>>
records;
382 template<
typename Key,
typename AssetClass>
383 template<
typename... Args>
386 record->asset = std::make_unique<AssetClass>(std::forward<Args>(args)...);
387 ++record->generation;
388 record->resetFuture();
390 if (record->state ==
LOADED) {
395 template<
typename Key,
typename AssetClass>
398 assert(asset !=
nullptr);
400 record->asset.reset(asset);
401 ++record->generation;
402 record->resetFuture();
404 if (record->state ==
LOADED) {
409 template<
typename Key,
typename AssetClass>
414 template<
typename Key,
typename AssetClass>
419 template<
typename Key,
typename AssetClass>
426 ++record->generation;
427 record->resetFuture();
428 record->lastError.clear();
430 record->asset.reset();
434 record->state = state;
440 template<
typename Key,
typename AssetClass>
443 record->asset = std::move(asset);
445 record->lastError.clear();
449 template<
typename Key,
typename AssetClass>
452 record->asset.reset();
454 record->lastError = std::move(error);
458 template<
typename Key,
typename AssetClass>
462 record->asset.reset();
464 ++record->generation;
465 record->resetFuture();
466 record->lastError.clear();
469 template<
typename Key,
typename AssetClass>
472 return record && record->asset !=
nullptr;
475 template <
typename Key,
typename AssetClass>
481 template <
typename Key,
typename AssetClass>
486 return record->state;
489 template<
typename Key,
typename AssetClass>
493 if (!record || record->asset ==
nullptr)
return std::nullopt;
495 return record->asset.get();
498 template <
typename Key,
typename AssetClass>
502 for (
auto& [key, record] :
records)
506 record->asset.reset();
508 ++record->generation;
509 record->resetFuture();
510 record->lastError.clear();
516 template<
typename Key,
typename AssetClass>
523 auto record = std::make_shared<RecordType>(path);
528 template<
typename Key,
typename AssetClass>
530 const auto it =
records.find(path);
531 return it !=
records.end() ? it->second :
nullptr;
534 template<
typename Key,
typename AssetClass>
536 if (!record || record->readyPromiseResolved || !record->readyPromise)
return;
540 result.
asset = success ? record->asset.get() :
nullptr;
542 result.
error = std::move(error);
545 record->readyPromise->set_value(std::move(result));
546 record->readyPromiseResolved =
true;
549 template<
typename Key,
typename AssetClass>
551 if (!record || record->asset ==
nullptr)
return;
552 if (record->state ==
FAILED)
return;
554 const LoadState payloadState = record->asset->getLoadingState();
555 if (payloadState != record->state) {
556 record->state = payloadState;
558 if (record->state ==
LOADED) {
#define TRACY_ZONE_SCOPED_NAMED(name)
void requestLoad()
If this asset is in the UNLOADED state it will get added to the assets to load.
virtual void unload()
Base function for unloading the ecs data.
entt::entity getEntity() const
Getter for data.
AssetBase(bool initializeDefaultEntity=false)
virtual void updateLoadingState()
CpuLoadingState loadingState
CpuLoadingState getLoadingState()
Gets the loading state of this asset.
entt::registry & mainRegistry
A manager which is used to look up existing assets and their loading state.
void declare(Key path, Args &&... args)
Adds an asset to the list in its unloaded state.
std::shared_ptr< RecordType > getOrCreateRecord(const Key &path)
Record< Key, AssetClass > RecordType
LoadResult< Key, AssetClass > ResultType
CpuLoadingState getAssetLoadingState(const Key &path)
Gets a loading state for an asset. If the asset is not yet declared it will return that it is UNLOADE...
std::optional< AssetClass * > getAsset(const Key &path)
Try's to get an asset. If the asset does not exist (can be checked with exists) it will return a std:...
void syncRecordStateFromPayload(const std::shared_ptr< RecordType > &record) const
void setAssetLoadingState(const Key &path, LoadState state)
Sets the record loading state without requiring a payload object.
void fail(const Key &path, std::string error)
Marks the record as failed and resolves the ready future with an error.
std::shared_ptr< RecordType > getRecord(const Key &path) const
bool isDeclared(Key path)
Checks if an asset has been declared. If it exists it is also declared.
void forEachAsset(Func &&func) const
Iterates over all assets and calls the provided callback for each.
void completeRecord(const std::shared_ptr< RecordType > &record, bool success, std::string error={})
bool exists(Key path)
Checks if an asset already exists. This means it is declared and there is content.
virtual void clear()
Clears out all resources.
void add(Key path, AssetClass *asset)
Adds or overwrites a previously declared asset at a key.
RefType getRef(const Key &path) const
Gets a stable asset reference if a record exists, otherwise an expired ref.
virtual ~AssetManager()=default
Ref< Key, AssetClass > RefType
void resolve(const Key &path, std::unique_ptr< AssetClass > asset)
Stores a loaded payload and resolves the record's ready future.
std::unordered_map< Key, std::shared_ptr< RecordType > > records
void unloadAssetPayload(const Key &path)
Drops the loaded payload while preserving the stable record.
RefType getOrCreateRef(const Key &path)
Gets a stable asset reference, creating a record if necessary.
LoadState getState() const
Ref(std::shared_ptr< RecordType > record)
LoadResult< std::filesystem::path, Audio > Result
const std::string & getError() const
std::shared_future< Result > readyFuture() const
std::weak_ptr< RecordType > record_
static std::shared_future< Result > expiredFuture()
Record< std::filesystem::path, Audio > RecordType
uint64_t getGeneration() const
Classes which are related to asset loading are mostly stored in this namespace.
CpuLoadingState
State for assets in the asset loading process.
@ LOADED
All components of the asset have been loaded, and it is ready to be used.
@ REQUESTED_LOAD
An asset has been requested to load and is waiting for an asset pipeline to start working on it.
@ LOADING
While the asset is currently being processed in the asset pipeline.
@ UNLOADED
When the asset is not loaded yet but the frame is already existing for the heavy data to be loaded in...
@ FAILED
Asset loading finished unsuccessfully. Inspect the asset ref error for details.
CpuLoadingState LoadState
Data structs for the Entity Component System.
Ref< Key, AssetClass > ref
std::shared_future< Result > readyFuture
bool readyPromiseResolved
std::shared_ptr< std::promise< Result > > readyPromise
LoadResult< Key, AssetClass > Result
std::unique_ptr< AssetClass > asset