Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1177 allow identifying assets in code from their path #1337

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions engine/include/cubos/engine/assets/asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

/// @brief Avoid using this field, use @ref getId() instead.
/// @todo This was added as a dirty fix for #692, should be removed once the issue is fixed.
uuids::uuid reflectedId;
std::string idOrPath;

~AnyAsset();

Expand Down Expand Up @@ -73,7 +73,11 @@

/// @brief Gets the UUID of the asset.
/// @return Asset UUID.
uuids::uuid getId() const;
std::string getId() const;

/// @brief Gets the path of the asset.
/// @return Asset path.
std::string getPath() const;

/// @brief Checks if the handle is null.
/// @return Whether the handle is null.
Expand Down Expand Up @@ -111,9 +115,10 @@
/// @brief Decrements the reference count of the asset.
void decRef() const;

uuids::uuid mId; ///< UUID of the asset.
void* mRefCount; ///< Void pointer to avoid including `<atomic>` in the header.
int mVersion; ///< Last known version of the asset.
uuids::uuid mId; ///< UUID of the asset.
void* mRefCount; ///< Void pointer to avoid including `<atomic>` in the header.
int mVersion; ///< Last known version of the asset.
std::string path; ///< Path of the asset.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-identifier-naming ⚠️
invalid case style for private member path

Suggested change
std::string path; ///< Path of the asset.
std::string mPath; ///< Path of the asset.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-identifier-naming ⚠️
invalid case style for private member path

Suggested change
std::string path; ///< Path of the asset.
std::string mPath; ///< Path of the asset.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-identifier-naming ⚠️
invalid case style for private member path

Suggested change
std::string path; ///< Path of the asset.
std::string mPath; ///< Path of the asset.

};

/// @brief Handle to an asset of a specific type.
Expand Down Expand Up @@ -160,10 +165,11 @@
inline AnyAsset::operator Asset<T>() const
{
Asset<T> asset;
asset.reflectedId = reflectedId;
asset.idOrPath = idOrPath;

Check warning on line 168 in engine/include/cubos/engine/assets/asset.hpp

View check run for this annotation

Codecov / codecov/patch

engine/include/cubos/engine/assets/asset.hpp#L168

Added line #L168 was not covered by tests
asset.mId = mId;
asset.mRefCount = mRefCount;
asset.mVersion = mVersion;
asset.path = path;

Check warning on line 172 in engine/include/cubos/engine/assets/asset.hpp

View check run for this annotation

Codecov / codecov/patch

engine/include/cubos/engine/assets/asset.hpp#L172

Added line #L172 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-identifier-naming ⚠️
invalid case style for private member path

Suggested change
asset.path = path;
asset.path = mPath;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-identifier-naming ⚠️
invalid case style for private member path

Suggested change
asset.path = path;
asset.mPath = mPath;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-identifier-naming ⚠️
invalid case style for private member path

Suggested change
asset.path = path;
asset.path = mPath;

asset.incRef();
return asset;
}
Expand Down
6 changes: 5 additions & 1 deletion engine/include/cubos/engine/assets/assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ namespace cubos::engine
struct Entry
{
Entry();

Status status{Status::Unloaded}; ///< The status of the asset.
AssetMeta meta; ///< The metadata associated with the asset.

Expand Down Expand Up @@ -308,6 +307,11 @@ namespace cubos::engine
/// @return Lock guard.
std::unique_lock<std::shared_mutex> lockWrite(const AnyAsset& handle) const;

/// @brief Checks if the given ID is of the correct type.
/// @param id ID to check.
/// @return The ID as a string.
std::string checkIdType(const std::string& id) const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-convert-member-functions-to-static ⚠️
method checkIdType can be made static

Suggested change
std::string checkIdType(const std::string& id) const;
static std::string checkIdType(const std::string& id) ;


/// @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
1 change: 1 addition & 0 deletions engine/samples/games/cubosurfers/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using namespace cubos::engine;

// static const Asset<Scene> SceneAsset = AnyAsset("/assets/scenes/main.cubos");
static const Asset<Scene> SceneAsset = AnyAsset("ee5bb451-05b7-430f-a641-a746f7009eef");
static const Asset<VoxelPalette> PaletteAsset = AnyAsset("101da567-3d23-46ae-a391-c10ec00e8718");
static const Asset<InputBindings> InputBindingsAsset = AnyAsset("b20900a4-20ee-4caa-8830-14585050bead");
Expand Down
48 changes: 33 additions & 15 deletions engine/src/assets/asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}

AnyAsset::AnyAsset(uuids::uuid id)
: reflectedId(id)
: idOrPath(uuids::to_string(id))

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L30

Added line #L30 was not covered by tests
, mId(id)
, mRefCount(nullptr)
, mVersion(-1)
Expand All @@ -38,54 +38,67 @@
: mRefCount(nullptr)
, mVersion(-1)
{

if (auto id = uuids::uuid::from_string(str))
{
reflectedId = id.value();
idOrPath = str;

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L44

Added line #L44 was not covered by tests
mId = id.value();
}
else
{
CUBOS_ERROR("Could not create asset handle, invalid UUID: \"{}\"", str);
if (str.find('/') != std::string::npos || str.find('\\') != std::string::npos)

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L49

Added line #L49 was not covered by tests
{
idOrPath = str;
path = 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#L51-L52

Added lines #L51 - L52 were not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-identifier-naming ⚠️
invalid case style for private member path

Suggested change
path = str;
mPath = str;

}
else
{
CUBOS_ERROR("Could not create asset handle, invalid UUID or Path: \"{}\"", str);

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L56

Added line #L56 was not covered by tests
}
}
}

AnyAsset::AnyAsset(const AnyAsset& other)
: reflectedId(other.reflectedId)
: idOrPath(other.idOrPath)

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L62

Added line #L62 was not covered by tests
, mId(other.mId)
, mRefCount(other.mRefCount)
, mVersion(other.mVersion)
, path(other.path)

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L66

Added line #L66 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-identifier-naming ⚠️
invalid case style for private member path

Suggested change
, path(other.path)
, mPath(other.mPath)

{
this->incRef();
}

AnyAsset::AnyAsset(AnyAsset&& other) noexcept
: reflectedId(other.reflectedId)
: idOrPath(other.idOrPath)

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L72

Added line #L72 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ performance-move-constructor-init ⚠️
move constructor initializes class member by calling a copy constructor

, mId(other.mId)
, mRefCount(other.mRefCount)
, mVersion(other.mVersion)
, path(other.path)

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L76

Added line #L76 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-identifier-naming ⚠️
invalid case style for private member path

Suggested change
, path(other.path)
, mPath(other.mPath)

{
other.mRefCount = nullptr;
}

AnyAsset& AnyAsset::operator=(const AnyAsset& other)
{
this->decRef();
reflectedId = other.reflectedId;
idOrPath = other.idOrPath;
mId = other.mId;
mRefCount = other.mRefCount;
mVersion = other.mVersion;
path = other.path;

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L88

Added line #L88 was not covered by tests
this->incRef();
return *this;
}

AnyAsset& AnyAsset::operator=(AnyAsset&& other) noexcept
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-identifier-naming ⚠️
invalid case style for private member path

Suggested change
AnyAsset& AnyAsset::operator=(AnyAsset&& other) noexcept
mPath = other.mPath;

{
this->decRef();
reflectedId = other.reflectedId;
idOrPath = other.idOrPath;
mId = other.mId;
mRefCount = other.mRefCount;
mVersion = other.mVersion;
other.mRefCount = nullptr;
path = other.path;
return *this;
}

Expand All @@ -96,22 +109,27 @@

int AnyAsset::getVersion() const
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-identifier-naming ⚠️
invalid case style for private member path

Suggested change
{
mPath = other.mPath;

return reflectedId == mId ? mVersion : 0;
return uuids::uuid::from_string(idOrPath) == mId ? mVersion : 0;

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L112

Added line #L112 was not covered by tests
}

std::string AnyAsset::getId() const

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L115

Added line #L115 was not covered by tests
{
return idOrPath;

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L117

Added line #L117 was not covered by tests
}

uuids::uuid AnyAsset::getId() const
std::string AnyAsset::getPath() const

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
{
return reflectedId;
return path;

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
}

bool AnyAsset::isNull() const
{
return reflectedId.is_nil();
return idOrPath.empty();

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L127

Added line #L127 was not covered by tests
}

bool AnyAsset::isStrong() const
{
return reflectedId == mId && mRefCount != nullptr;
return uuids::uuid::from_string(idOrPath) == mId && mRefCount != nullptr;

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L132

Added line #L132 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-identifier-naming ⚠️
invalid case style for private member path

Suggested change
return uuids::uuid::from_string(idOrPath) == mId && mRefCount != nullptr;
return mPath;

}

void AnyAsset::makeWeak()
Expand All @@ -126,20 +144,20 @@

return Type::create(std::move(name))
.with(ConstructibleTrait::typed<AnyAsset>().withBasicConstructors().build())
.with(FieldsTrait().withField("id", &AnyAsset::reflectedId));
.with(FieldsTrait().withField("id", &AnyAsset::idOrPath));

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L147

Added line #L147 was not covered by tests
}

void AnyAsset::incRef() const
{
if (reflectedId == mId && mRefCount != nullptr)
if (uuids::uuid::from_string(idOrPath) == mId && mRefCount != nullptr)
{
static_cast<std::atomic<int>*>(mRefCount)->fetch_add(1);
}
}

void AnyAsset::decRef() const
{
if (reflectedId == mId && mRefCount != nullptr)
if (uuids::uuid::from_string(idOrPath) == mId && mRefCount != nullptr)

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

View check run for this annotation

Codecov / codecov/patch

engine/src/assets/asset.cpp#L160

Added line #L160 was not covered by tests
{
static_cast<std::atomic<int>*>(mRefCount)->fetch_sub(1);
}
Expand Down
Loading
Loading