Files
cursebreaker-parser/include/assets/asset_base.hpp
cdemeyer-teachx 998313be3c initial commit
2025-11-12 06:29:59 +09:00

86 lines
1.9 KiB
C++

#pragma once
#include <cstdint>
#include <unordered_map>
#include <array>
#include <string_view>
#include <span>
#include <ryml.hpp>
#include <ryml_std.hpp>
#include <c4/format.hpp>
struct AssetBase
{
public:
int64_t ID;
};
struct AssetGUID
{
AssetGUID() = default;
AssetGUID(const char* data) : AssetGUID(std::string_view(data, 32)) {};
AssetGUID(std::string_view data)
{
for (size_t i = 0; i < 16; ++i) Data[0] = (Data[0] << 4) | hexToVal(data[i]);
for (size_t i = 16; i < 32; ++i) Data[1] = (Data[1] << 4) | hexToVal(data[i]);
}
static uint64_t hexToVal(char c)
{
return (c >= '0' && c <= '9') ? (c - '0') : (c - 'a' + 10);
}
bool operator==(const AssetGUID& other) const
{
return Data[0] == other.Data[0] && Data[1] == other.Data[1];
}
static constexpr uint64_t DefaultVal = 0xe000000000000000;
std::array<uint64_t, 2> Data{ 0, DefaultVal };
};
namespace std
{
template <>
struct hash<AssetGUID> {
size_t operator()(const AssetGUID& v) const noexcept
{
auto h1 = std::hash<uint64_t>{}(v.Data[0]);
auto h2 = std::hash<uint64_t>{}(v.Data[1]);
return h1 ^ (h2 << 1);
}
};
}
template <typename T>
T* ParseAssetRef(const ryml::ConstNodeRef& node)
{
T* asset;
node["fileID"] >> asset;
return asset;
}
template <typename T>
bool LinkAssetRef(const std::unordered_map<int64_t, AssetBase*>& assetsMap, T*& asset)
{
auto it = assetsMap.find(reinterpret_cast<int64_t>(asset));
if (it != assetsMap.end())
{
asset = reinterpret_cast<T*>(it->second);
return true;
}
asset = nullptr;
return false;
}
inline AssetGUID ParseAssetGUID(const ryml::ConstNodeRef& node)
{
auto guidStringSpan2 = node["guid"].val();
assert(guidStringSpan2.size() == 32);
return AssetGUID{std::string_view(guidStringSpan2.data(), guidStringSpan2.size())};
}