29 auto absolute_path = std::filesystem::absolute(filepath);
36 if (
auto* asset = &assets_.at(it->second)) {
49 .
name = filepath.stem().string(),
67 std::vector<AssetID> registered_assets;
70 if (!std::filesystem::exists(absolute_dir)) {
71 return registered_assets;
74 auto iterator = std::filesystem::recursive_directory_iterator(absolute_dir);
75 for (
const auto& entry : iterator) {
76 if (entry.is_regular_file()) {
80 registered_assets.push_back(id);
85 return registered_assets;
89 for (
auto &metadata: assets_ | std::views::values) {
90 auto absolute_path = to_absolute_path(metadata.filepath);
91 if (std::filesystem::exists(absolute_path)) {
92 metadata.last_modified = get_file_last_modified(absolute_path);
98 const auto it =
assets_.find(uuid);
115 std::vector<AssetMetadata> result;
116 for (
const auto &metadata: assets_ | std::views::values) {
117 if (metadata.type == type) {
118 result.push_back(metadata);
125 std::vector<AssetMetadata> result;
126 result.reserve(assets_.size());
127 for (
const auto &metadata: assets_ | std::views::values) {
128 result.push_back(metadata);
155 if (std::filesystem::exists(absolute_path)) {
157 return current_modified != it->second.last_modified;
164 std::vector<AssetID> modified;
165 for (
const auto &uuid: assets_ | std::views::keys) {
166 if (has_asset_changed(uuid)) {
167 modified.push_back(uuid);
175 j[
"version"] =
"1.0";
176 j[
"assets"] = nlohmann::json::array();
178 for (
const auto &metadata: assets_ | std::views::values) {
179 nlohmann::json asset_json;
180 asset_json[
"uuid"] = metadata.uuid;
181 asset_json[
"path"] = metadata.filepath.string();
182 asset_json[
"type"] = metadata.type;
183 asset_json[
"name"] = metadata.name;
184 asset_json[
"last_modified"] = metadata.last_modified;
185 j[
"assets"].push_back(asset_json);
202 if (j.contains(
"assets")) {
203 for (
const auto& asset_json : j[
"assets"]) {
204 AssetMetadata metadata{
205 .uuid = asset_json[
"uuid"].get<AssetID>(),
206 .filepath = asset_json[
"path"].get<std::string>(),
207 .type = asset_json[
"type"].get<AssetType>(),
208 .name = asset_json[
"name"].get<std::string>(),
209 .last_modified = asset_json[
"last_modified"].get<uint64_t>(),
212 assets_[metadata.uuid] = metadata;
213 path_to_uuid_[metadata.filepath] = metadata.uuid;
217 }
catch (
const std::exception& e) {
218 std::cerr <<
"ERROR::ASSETREGISTRY::LOAD:: " << e.what() << std::endl;
233 static const std::unordered_map<std::string, AssetType> extension_map = {
234 {
".png", AssetType::TEXTURE},
235 {
".jpg", AssetType::TEXTURE},
236 {
".jpeg", AssetType::TEXTURE},
237 {
".obj", AssetType::MODEL},
238 {
".gltf", AssetType::MODEL},
239 {
".glb", AssetType::MODEL},
240 {
".fbx", AssetType::MODEL},
241 {
".hfmodel", AssetType::MODEL},
242 {
".hfmat", AssetType::MATERIAL},
243 {
".hfmesh", AssetType::MESH},
244 {
".hfscene", AssetType::SCENE},
245 {
".frag", AssetType::SHADER},
246 {
".vert", AssetType::SHADER},
247 {
".glsl", AssetType::SHADER},
248 {
".shader", AssetType::SHADER}
251 auto extension = filepath.extension().string();
252 std::ranges::transform(extension, extension.begin(), tolower);
253 const auto it = extension_map.find(extension);
259 std::hash<std::string> hasher;
260 uint64_t id = hasher(filepath.string());
265 return std::filesystem::relative(absolute_path,
project_root_);
274 for (
const auto& [uuid, metadata] : assets_) {
275 path_to_uuid_[metadata.filepath] = uuid;
280 if (std::filesystem::exists(filepath)) {
281 const auto ftime = std::filesystem::last_write_time(filepath);
282 return ftime.time_since_epoch().count();
Registry for storing assets.
std::optional< AssetID > get_uuid_by_path(const std::filesystem::path &filepath)
AssetID generate_uuid(const std::filesystem::path &filepath)
void set_project_root(const std::filesystem::path &project_root)
std::filesystem::path get_absolute_path(AssetID uuid)
std::unordered_map< std::filesystem::path, AssetID > path_to_uuid_
std::filesystem::path registry_file_
AssetID register_asset(const std::filesystem::path &filepath, AssetType type)
std::unordered_map< AssetID, AssetMetadata > assets_
std::filesystem::path to_absolute_path(const std::filesystem::path &relative_path) const
static AssetType get_type_from_extension(const std::filesystem::path &filepath)
std::filesystem::path to_relative_path(const std::filesystem::path &absolute_path) const
AssetID register_asset(const std::filesystem::path &filepath)
std::vector< AssetID > register_directory(const std::filesystem::path &directory_path, bool recursive)
std::vector< AssetMetadata > get_assets_by_type(AssetType type)
std::filesystem::path get_relative_path(AssetID uuid)
bool asset_exists(AssetID uuid) const
std::vector< AssetID > get_modified_assets() const
AssetRegistry(const std::filesystem::path ®istry_file, const std::filesystem::path &project_root)
std::optional< AssetMetadata > get_asset(AssetID uuid) const
std::filesystem::path project_root_
std::vector< AssetMetadata > get_all_assets() const
uint64_t get_file_last_modified(const std::filesystem::path &filepath) const
constexpr AssetID INVALID_ASSET_ID