2025-11-19 22:20:27 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
2025-11-27 13:32:44 +01:00
|
|
|
#include <amethyst/id.hpp>
|
2025-11-19 22:20:27 +01:00
|
|
|
#include <amethyst/texture.hpp>
|
|
|
|
|
#include <amethyst/types.hpp>
|
|
|
|
|
|
2025-11-26 13:46:46 +01:00
|
|
|
namespace Amy {
|
2025-11-27 13:32:44 +01:00
|
|
|
class AssetMgr {
|
2025-11-19 22:20:27 +01:00
|
|
|
public:
|
2025-11-27 13:32:44 +01:00
|
|
|
AssetMgr() = default;
|
|
|
|
|
~AssetMgr() = default;
|
2025-11-19 22:20:27 +01:00
|
|
|
|
2025-11-27 13:32:44 +01:00
|
|
|
void AutoLoad(const ID& name, ksr path);
|
|
|
|
|
|
|
|
|
|
void Add(const ID& id, Asset* v) {
|
|
|
|
|
if (pAssets.count(id)) {
|
|
|
|
|
throw std::runtime_error("[amy]: assets: " + id.GetName() +
|
|
|
|
|
" already exists!");
|
|
|
|
|
}
|
|
|
|
|
pAssets[id] = v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Remove(const ID& id) {
|
2025-11-26 13:46:46 +01:00
|
|
|
if (pAssets.count(id)) {
|
|
|
|
|
pAssets.erase(id);
|
2025-11-19 22:20:27 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-27 13:32:44 +01:00
|
|
|
|
2025-11-19 22:20:27 +01:00
|
|
|
template <typename T>
|
2025-11-27 13:32:44 +01:00
|
|
|
T* Get(const ID& id) {
|
2025-11-26 13:46:46 +01:00
|
|
|
auto r = pAssets.find(id);
|
|
|
|
|
if (r == pAssets.end()) {
|
2025-11-27 13:32:44 +01:00
|
|
|
throw std::runtime_error("[amy] assets: unable to find " + id.GetName());
|
2025-11-19 22:20:27 +01:00
|
|
|
}
|
|
|
|
|
if (auto v = dynamic_cast<T*>(r->second)) {
|
|
|
|
|
return v;
|
|
|
|
|
} else {
|
2025-11-27 13:32:44 +01:00
|
|
|
throw std::runtime_error(id.GetName() + " is not of type " +
|
|
|
|
|
typeid(T).name());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
bool IsType(const ID& id) {
|
|
|
|
|
auto r = pAssets.find(id);
|
|
|
|
|
if (r == pAssets.end()) {
|
|
|
|
|
throw std::runtime_error("[amy] assets: unable to find " + id.GetName());
|
2025-11-19 22:20:27 +01:00
|
|
|
}
|
2025-11-27 13:32:44 +01:00
|
|
|
return dynamic_cast<T*>(r->second) != nullptr;
|
2025-11-19 22:20:27 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-30 21:57:01 +01:00
|
|
|
template <typename T>
|
|
|
|
|
size_t Count() const {
|
|
|
|
|
size_t ret = 0;
|
|
|
|
|
for (auto& it : pAssets) {
|
|
|
|
|
if (dynamic_cast<T*>(it.second)) {
|
|
|
|
|
ret++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-19 22:20:27 +01:00
|
|
|
private:
|
2025-11-27 13:32:44 +01:00
|
|
|
std::map<ID, Asset*> pAssets;
|
2025-11-19 22:20:27 +01:00
|
|
|
};
|
2025-11-26 13:46:46 +01:00
|
|
|
} // namespace Amy
|