Skip to content

Commit

Permalink
feat(assets): add identifying assets in code from their path
Browse files Browse the repository at this point in the history
  • Loading branch information
GalaxyCrush committed Oct 9, 2024
1 parent 92218d3 commit 51985bb
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 36 deletions.
21 changes: 20 additions & 1 deletion engine/include/cubos/engine/assets/asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

namespace cubos::engine
{
/// @brief Type of the asset identifier.
enum class IdType
{
UUID,
Path,
Invalid
};

template <typename T>
class Asset;

Expand Down Expand Up @@ -71,11 +79,22 @@ namespace cubos::engine
/// @return Asset version.
int getVersion() const;

/// @brief Gets the type of the asset identifier.
/// @return Asset identifier type.
IdType getIdType() const;

/// @brief Gets the Path or UUID of the asset.
/// @return Asset Path or UUID.
std::string getIdString() const;

/// @brief Gets the UUID of the asset if it is the case of having one.
/// @brief Gets the UUID of the asset if it has one.
///
/// There are two possible reasons for an asset handle to have no UUID:
/// - it being an invalid or null handle;
/// - it being created from an asset path.
///
/// To ensure an asset handle gets a UUID, you can use @ref Assets::load.
///
/// @return Asset UUID.
std::optional<uuids::uuid> getId() const;

Expand Down
5 changes: 0 additions & 5 deletions engine/include/cubos/engine/assets/assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,6 @@ namespace cubos::engine
/// @return Lock guard.
std::unique_lock<std::shared_mutex> lockWrite(const AnyAsset& handle) const;

/// @brief Checks if the given path is a valid path to an asset.
/// @param path Path to check.
/// @return A string that says if the given string is a path, uuid or not a valid format string
static std::string isPath(const std::string& path);

/// @brief Gets a pointer to the entry associated with the given handle.
/// @param handle Handle to get the entry for.
/// @return Entry for the given handle, or nullptr if there is no such entry.
Expand Down
2 changes: 1 addition & 1 deletion engine/include/cubos/engine/assets/meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace cubos::engine

/// @brief Gets the path of the asset.
/// @return Path of the asset.
std::string getPath() const;
std::optional<std::string> getPath() const;

/// @brief Sets a parameter on the asset's metadata.
/// @param key Key of the parameter.
Expand Down
17 changes: 15 additions & 2 deletions engine/src/assets/asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ AnyAsset::AnyAsset(std::string_view str)
pathOrId = str;

Check warning on line 43 in engine/src/assets/asset.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L43

Added line #L43 was not covered by tests
mId = id.value();
}
else if (str.find('/') != std::string_view::npos || str.find('\\') != std::string_view::npos)
else if (!str.empty() && str[0] == '/')

Check warning on line 46 in engine/src/assets/asset.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L46

Added line #L46 was not covered by tests
{
pathOrId = str;

Check warning on line 48 in engine/src/assets/asset.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L48

Added line #L48 was not covered by tests
}
else
{
CUBOS_ERROR("Could not create asset handle, invalid UUID: \"{}\"", str);
CUBOS_ERROR("Could not create asset handle, invalid path or UUID: \"{}\"", str);

Check warning on line 52 in engine/src/assets/asset.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L52

Added line #L52 was not covered by tests
}
}

Expand Down Expand Up @@ -113,6 +113,19 @@ int AnyAsset::getVersion() const
return getId().has_value() && getId().value() == mId ? mVersion : 0;

Check warning on line 113 in engine/src/assets/asset.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L113

Added line #L113 was not covered by tests
}

IdType AnyAsset::getIdType() const

Check warning on line 116 in engine/src/assets/asset.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L116

Added line #L116 was not covered by tests
{
if (!pathOrId.empty() && pathOrId[0] == '/')

Check warning on line 118 in engine/src/assets/asset.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L118

Added line #L118 was not covered by tests
{
return IdType::Path;

Check warning on line 120 in engine/src/assets/asset.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L120

Added line #L120 was not covered by tests
}
else if (auto id = uuids::uuid::from_string(pathOrId))

Check warning on line 122 in engine/src/assets/asset.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L122

Added line #L122 was not covered by tests
{
return IdType::UUID;

Check warning on line 124 in engine/src/assets/asset.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L124

Added line #L124 was not covered by tests
}
return IdType::Invalid;

Check warning on line 126 in engine/src/assets/asset.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L126

Added line #L126 was not covered by tests
}

std::string AnyAsset::getIdString() const

Check warning on line 129 in engine/src/assets/asset.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L129

Added line #L129 was not covered by tests
{
return pathOrId;

Check warning on line 131 in engine/src/assets/asset.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L131

Added line #L131 was not covered by tests
Expand Down
24 changes: 6 additions & 18 deletions engine/src/assets/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,18 +597,6 @@ std::unique_lock<std::shared_mutex> Assets::lockWrite(const AnyAsset& handle) co
abort();
}

std::string Assets::isPath(const std::string& path)
{
if (path.find('/') != std::string::npos || path.find('\\') != std::string::npos)
{
return "Path";
}
else if (auto id = uuids::uuid::from_string(path))
{
return "UUID";
}
return "Invalid format";
}
std::shared_ptr<Assets::Entry> Assets::entry(const AnyAsset& handle) const
{
// If the handle is null, we can't access the asset.
Expand All @@ -622,8 +610,9 @@ std::shared_ptr<Assets::Entry> Assets::entry(const AnyAsset& handle) const
auto sharedLock = std::shared_lock(mMutex);

auto sid = handle.getIdString();
auto type = handle.getIdType();

Check warning on line 613 in engine/src/assets/assets.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/assets.cpp#L612-L613

Added lines #L612 - L613 were not covered by tests

if (isPath(sid) == "Path")
if (type == IdType::Path)

Check warning on line 615 in engine/src/assets/assets.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/assets.cpp#L615

Added line #L615 was not covered by tests
{
for (const auto& [eid, entry] : mEntries)

Check warning on line 617 in engine/src/assets/assets.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/assets.cpp#L617

Added line #L617 was not covered by tests
{
Expand All @@ -635,7 +624,7 @@ std::shared_ptr<Assets::Entry> Assets::entry(const AnyAsset& handle) const
CUBOS_ERROR("No such asset {}", handle);
return nullptr;
}
if (isPath(sid) == "UUID")
if (type == IdType::UUID)

Check warning on line 627 in engine/src/assets/assets.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/assets.cpp#L627

Added line #L627 was not covered by tests
{
// Search for the entry in the map.
auto it = mEntries.find(handle.getId().value());
Expand Down Expand Up @@ -673,8 +662,8 @@ std::shared_ptr<Assets::Entry> Assets::entry(const AnyAsset& handle, bool create
}

auto sid = handle.getIdString();

if (isPath(sid) == "Path")
auto type = handle.getIdType();
if (type == IdType::Path)

Check warning on line 666 in engine/src/assets/assets.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/assets.cpp#L664-L666

Added lines #L664 - L666 were not covered by tests
{
for (const auto& [eid, entry] : mEntries)

Check warning on line 668 in engine/src/assets/assets.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/assets.cpp#L668

Added line #L668 was not covered by tests
{
Expand All @@ -697,9 +686,8 @@ std::shared_ptr<Assets::Entry> Assets::entry(const AnyAsset& handle, bool create
CUBOS_ERROR("No such asset {}", handle);
return nullptr;

Check warning on line 687 in engine/src/assets/assets.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/assets.cpp#L686-L687

Added lines #L686 - L687 were not covered by tests
}
if (isPath(sid) == "UUID")
else if (type == IdType::UUID)

Check warning on line 689 in engine/src/assets/assets.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/assets.cpp#L689

Added line #L689 was not covered by tests
{

// Search for an existing entry for the asset.
auto it = mEntries.find(handle.getId().value());
if (it == mEntries.end())

Check warning on line 693 in engine/src/assets/assets.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/assets.cpp#L692-L693

Added lines #L692 - L693 were not covered by tests
Expand Down
8 changes: 2 additions & 6 deletions engine/src/assets/meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ uuids::uuid AssetMeta::getId() const
return uuids::uuid::from_string(this->get("id").value()).value();

Check warning on line 27 in engine/src/assets/meta.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/meta.cpp#L27

Added line #L27 was not covered by tests
}

std::string AssetMeta::getPath() const
std::optional<std::string> AssetMeta::getPath() const

Check warning on line 30 in engine/src/assets/meta.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/meta.cpp#L30

Added line #L30 was not covered by tests
{
if (auto path = this->get("path"))
{
return path.value();
}
return {};
return this->get("path");

Check warning on line 32 in engine/src/assets/meta.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/meta.cpp#L32

Added line #L32 was not covered by tests
}
6 changes: 3 additions & 3 deletions engine/src/render/mesh/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ void cubos::engine::renderMeshPlugin(Cubos& cubos)

cubos.observer("update RenderMesh on LoadRenderVoxels")
.onAdd<LoadRenderVoxels>()
.call([](Commands cmds, State& state, Assets& assets, Query<Entity, const RenderVoxelGrid&> query) {
.call([](Commands cmds, State& state, Assets& assets, Query<Entity, RenderVoxelGrid&> query) {

Check warning on line 82 in engine/src/render/mesh/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/mesh/plugin.cpp#L82

Added line #L82 was not covered by tests
for (auto [ent, grid] : query)
{
if (grid.asset.isNull())
{
continue;
}

// If not loaded, the mId might be a nil UUID, so we need to load the asset first.
assets.load(grid.asset);
// The asset handle might not yet have a valid ID, and since we need getId, we load it first.
grid.asset = assets.load(grid.asset);

Check warning on line 91 in engine/src/render/mesh/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/mesh/plugin.cpp#L91

Added line #L91 was not covered by tests
// If the asset has never been loaded, or it has been updated since the last meshing, update the entry
// and queue a meshing task. Otherwise, just reuse the existing mesh.
auto& entry = state.entries[grid.asset.getId().value()];

Check warning on line 94 in engine/src/render/mesh/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/mesh/plugin.cpp#L94

Added line #L94 was not covered by tests
Expand Down

0 comments on commit 51985bb

Please sign in to comment.