65 lines
1.9 KiB
C++
65 lines
1.9 KiB
C++
#ifndef DESTRUM_ASSETDATABASE_H
|
|
#define DESTRUM_ASSETDATABASE_H
|
|
|
|
#include <compare>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <unordered_map>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <destrum/Assets/AssetReference.h>
|
|
#include <destrum/Singleton.h>
|
|
|
|
struct AssetGuid {
|
|
std::uint64_t high{0};
|
|
std::uint64_t low{0};
|
|
|
|
[[nodiscard]] static AssetGuid Generate();
|
|
[[nodiscard]] static std::optional<AssetGuid> Parse(std::string_view text);
|
|
[[nodiscard]] std::string ToString() const;
|
|
[[nodiscard]] bool IsValid() const { return high != 0 || low != 0; }
|
|
|
|
auto operator<=>(const AssetGuid&) const = default;
|
|
};
|
|
|
|
template <>
|
|
struct std::hash<AssetGuid> {
|
|
std::size_t operator()(const AssetGuid& guid) const noexcept
|
|
{
|
|
return std::hash<std::uint64_t>{}(guid.high ^ (guid.low * 0x9E3779B97F4A7C15ull));
|
|
}
|
|
};
|
|
|
|
class AssetDatabase final : public Singleton<AssetDatabase> {
|
|
public:
|
|
class MetaFileScope {
|
|
public:
|
|
MetaFileScope();
|
|
~MetaFileScope();
|
|
MetaFileScope(const MetaFileScope&) = delete;
|
|
MetaFileScope& operator=(const MetaFileScope&) = delete;
|
|
};
|
|
|
|
void Refresh();
|
|
void Clear();
|
|
void Register(std::string_view path, const AssetGuid& guid);
|
|
|
|
[[nodiscard]] std::optional<AssetGuid> FindGuid(std::string_view path) const;
|
|
[[nodiscard]] std::optional<std::string> FindPath(const AssetGuid& guid) const;
|
|
std::optional<AssetGuid> GuidForSave(std::string_view path);
|
|
|
|
[[nodiscard]] nlohmann::json WriteReference(std::string_view path, std::string_view subresource = {});
|
|
[[nodiscard]] std::optional<AssetReference> ReadReference(const nlohmann::json& reference) const;
|
|
|
|
private:
|
|
std::unordered_map<std::string, AssetGuid> m_Guids;
|
|
std::unordered_map<AssetGuid, std::string> m_Paths;
|
|
int m_MetaFileScopes{0};
|
|
};
|
|
|
|
#endif // DESTRUM_ASSETDATABASE_H
|