Skip to content

Commit

Permalink
feat(engine): allow getting the type of an asset
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoMendonc-a authored and RiscadoA committed Sep 2, 2023
1 parent 5be4522 commit 8ea2bc0
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 3 deletions.
10 changes: 10 additions & 0 deletions engine/include/cubos/engine/assets/assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ namespace cubos::engine
/// @return Vector with all registered assets.
std::vector<AnyAsset> listAll() const;

/// @brief Gets the type of an asset.
///
/// If the asset is not loaded, its type is deduced from its bridge.
/// If there's also no associated bridge, aborts.
/// If the asset does not exist, aborts.
///
/// @param handle Handle to check the type for.
/// @return Asset type.
std::type_index type(const AnyAsset& handle) const;

private:
/// @brief Represents a known asset - may or may not be loaded.
struct Entry
Expand Down
21 changes: 20 additions & 1 deletion engine/include/cubos/engine/assets/bridge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#pragma once

#include <typeindex>

#include <cubos/engine/assets/asset.hpp>

namespace cubos::engine
Expand All @@ -26,7 +28,14 @@ namespace cubos::engine
class AssetBridge
{
public:
AssetBridge() = default;
/// @brief Constructs a bridge.
///
/// @param index Type of assets loaded by the bridge.
explicit AssetBridge(std::type_index index)
: mIndex(index)
{
}

virtual ~AssetBridge() = default;

/// @brief Loads an asset.
Expand All @@ -46,5 +55,15 @@ namespace cubos::engine
/// @param handle Handle of the asset being saved.
/// @return Whether the asset was successfully saved.
virtual bool save(const Assets& assets, const AnyAsset& handle);

/// @brief Gets the type of the assets the bridge loads.
/// @return Type of the asset.
inline std::type_index assetType() const
{
return mIndex;
}

private:
std::type_index mIndex; ///< Type of assets loaded by the bridge
};
} // namespace cubos::engine
3 changes: 2 additions & 1 deletion engine/include/cubos/engine/assets/bridges/binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace cubos::engine
/// @brief Constructs a bridge.
/// @param littleEndian Whether to use little endian byte order.
BinaryBridge(bool littleEndian = true)
: mLittleEndian{littleEndian}
: FileBridge(typeid(T))
, mLittleEndian{littleEndian}
{
}

Expand Down
8 changes: 8 additions & 0 deletions engine/include/cubos/engine/assets/bridges/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ namespace cubos::engine
class FileBridge : public AssetBridge
{
public:
/// @brief Constructs a bridge.
///
/// @param index Type of assets loaded by the bridge.
explicit FileBridge(std::type_index index)
: AssetBridge(index)
{
}

bool load(Assets& assets, const AnyAsset& handle) final;
bool save(const Assets& assets, const AnyAsset& handle) final;

Expand Down
3 changes: 2 additions & 1 deletion engine/include/cubos/engine/assets/bridges/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ namespace cubos::engine
///
/// @param indentation Indentation level to use when saving the JSON file.
JSONBridge(int indentation = 4)
: mIndentation{indentation}
: FileBridge(typeid(T))
, mIndentation{indentation}
{
}

Expand Down
8 changes: 8 additions & 0 deletions engine/include/cubos/engine/scene/bridge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <cubos/engine/assets/bridge.hpp>
#include <cubos/engine/scene/scene.hpp>

namespace cubos::engine
{
Expand Down Expand Up @@ -45,6 +46,13 @@ namespace cubos::engine
class SceneBridge : public AssetBridge
{
public:
/// @brief Constructs a bridge.
///
SceneBridge()
: AssetBridge(typeid(Scene))
{
}

bool load(Assets& assets, const AnyAsset& handle) override;
bool save(const Assets& assets, const AnyAsset& handle) override;
};
Expand Down
5 changes: 5 additions & 0 deletions engine/samples/assets/bridge/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ using namespace cubos::engine;
class TextBridge : public FileBridge
{
public:
TextBridge()
: FileBridge(typeid(std::string))
{
}

bool loadFromFile(Assets& assets, const AnyAsset& handle, Stream& stream) override
{
// Dump the file's contents into a string.
Expand Down
20 changes: 20 additions & 0 deletions engine/src/cubos/engine/assets/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,12 @@ void Assets::loader()
assetEntry->status = Assets::Status::Unloaded;
assetEntry->cond.notify_all();
}
else
{
auto assetEntry = this->entry(task.handle);
CUBOS_ASSERT(assetEntry != nullptr);
CUBOS_ASSERT(assetEntry->type == task.bridge->assetType());
}
}
}

Expand All @@ -600,4 +606,18 @@ std::vector<AnyAsset> Assets::listAll() const
out.emplace_back(entry);
}
return out;
}

std::type_index Assets::type(const AnyAsset& handle) const
{
auto assetEntry = this->entry(handle);
CUBOS_ASSERT(assetEntry != nullptr, "Could not find asset's type");
if (assetEntry->status == Status::Loaded)
{
return assetEntry->type;
}

auto bridge = this->bridge(handle);
CUBOS_ASSERT(bridge != nullptr, "Could not find asset's type");
return bridge->assetType();
}

0 comments on commit 8ea2bc0

Please sign in to comment.