zengine-old/engine/3rdparty/zasset/include/asset_manager.h
2024-03-03 23:33:13 +08:00

55 lines
1.6 KiB
C++

#pragma once
#include <filesystem>
#include <fstream>
#include <functional>
#include <sstream>
#include <string>
#include <map>
#include "asset.h"
namespace engineapi
{
class AssetManager
{
public:
struct AssetWrap {
Asset& asset;
uint32_t count;
AssetWrap(Asset& asset): asset(asset),count(0) {
}
};
private:
std::map<const std::string, AssetWrap> mAssetMap;
public:
template<typename TAsset>
TAsset* LoadAsset(const std::string& asset_url,uint32_t flags)
{
std::filesystem::path asset_path = getFullPath(asset_url);
auto it = mAssetMap.find(asset_path.string());
if (it != mAssetMap.end()) {
if (it->second.asset.IsShared()) {
return &(it->second.asset);
}
if (it->second.asset.IsCopyed()) {
TAsset* asset = new TAsset(asset_path.string(), flags);
*asset = it->second.asset;
return asset;
}
}
TAsset* asset = new TAsset(asset_path.string(), flags);
if (asset->IsShared() || asset->IsCopyed()) {
mAssetMap.emplace(asset_path.string(), *asset);
}
if (asset->IsAsync()) {
//todo: AsyncLoad
asset->SyncLoad();
}
else {
asset->SyncLoad();
}
return asset;
}
std::filesystem::path getFullPath(const std::string& relative_path) const;
};
}