[nostalgia] Add UUID to file path mapping

This commit is contained in:
2023-02-12 22:13:32 -06:00
parent ba7ee92ad2
commit 5e43eff631
8 changed files with 105 additions and 42 deletions
+53 -24
View File
@@ -10,9 +10,9 @@
#include <ox/fs/fs.hpp>
#include <ox/model/metadata.hpp>
#include <nostalgia/foundation/context.hpp>
#include "asset.hpp"
#include "context.hpp"
#include "typeconv.hpp"
namespace nostalgia::foundation {
@@ -32,13 +32,14 @@ oxModelEnd()
ox::Result<std::size_t> getPreloadAddr(foundation::Context *ctx, const ox::FileAddress &file) noexcept;
ox::Result<std::size_t> getPreloadAddr(foundation::Context *ctx, ox::CRStringView file) noexcept;
template<typename T>
ox::Result<foundation::AssetRef<T>> readObj(
[[maybe_unused]] foundation::Context *ctx,
[[maybe_unused]] ox::CRStringView path,
[[maybe_unused]] bool forceLoad = false) noexcept {
#ifndef OX_BARE_METAL
const auto readConvert = [ctx](const ox::Buffer &buff) -> ox::Result<T> {
template<typename T>
ox::Result<foundation::AssetRef<T>> readObjFile(
foundation::Context *ctx,
ox::CRStringView assetId,
bool forceLoad) noexcept {
constexpr auto readConvert = [](Context *ctx, const ox::Buffer &buff) -> ox::Result<T> {
auto [obj, err] = readAsset<T>(buff);
if (err) {
if (err != ox::Error_ClawTypeVersionMismatch && err != ox::Error_ClawTypeMismatch) {
@@ -48,27 +49,55 @@ ox::Result<foundation::AssetRef<T>> readObj(
}
return std::move(obj);
};
if (forceLoad) {
oxRequire(buff, ctx->rom->read(path));
oxRequire(obj, readConvert(buff));
oxRequire(cached, ctx->assetManager.setAsset(path, obj));
return std::move(cached);
ox::StringView path;
if (ctx->useUuids) {
path = ctx->uuidToPath[assetId];
} else {
auto [cached, err] = ctx->assetManager.getAsset<T>(path);
if (err) {
oxRequire(buff, ctx->rom->read(path));
oxRequire(obj, readConvert(buff));
oxReturnError(ctx->assetManager.setAsset(path, obj).moveTo(&cached));
}
return std::move(cached);
path = assetId;
}
if (forceLoad) {
oxRequire(buff, ctx->rom->read(assetId));
oxRequire(obj, readConvert(ctx, buff));
oxRequire(cached, ctx->assetManager.setAsset(assetId, obj));
return cached;
} else {
auto [cached, err] = ctx->assetManager.getAsset<T>(assetId);
if (err) {
oxRequire(buff, ctx->rom->read(assetId));
oxRequire(obj, readConvert(ctx, buff));
oxReturnError(ctx->assetManager.setAsset(assetId, obj).moveTo(&cached));
}
return cached;
}
}
#else
template<typename T>
ox::Result<foundation::AssetRef<T>> readObjNoCache(
foundation::Context *ctx,
ox::CRStringView assetId) noexcept {
if constexpr(ox::preloadable<T>::value) {
oxRequire(addr, getPreloadAddr(ctx, path));
oxRequire(addr, getPreloadAddr(ctx, assetId));
return foundation::AssetRef<T>(reinterpret_cast<const T*>(addr));
} else {
return OxError(1);
}
}
#endif
ox::Error buildUuidMap(Context *ctx) noexcept;
template<typename T>
ox::Result<foundation::AssetRef<T>> readObj(
[[maybe_unused]] foundation::Context *ctx,
[[maybe_unused]] ox::CRStringView assetId,
[[maybe_unused]] bool forceLoad = false) noexcept {
#ifndef OX_BARE_METAL
return readObjFile<T>(ctx, assetId, forceLoad);
#else
return readObjNoCache<T>(ctx, assetId);
#endif
}
@@ -78,8 +107,8 @@ ox::Result<foundation::AssetRef<T>> readObj(
const ox::FileAddress &file,
[[maybe_unused]] bool forceLoad = false) noexcept {
#ifndef OX_BARE_METAL
oxRequire(path, file.getPath());
return readObj<T>(ctx, ox::StringView(path), forceLoad);
oxRequire(assetId, file.getPath());
return readObj<T>(ctx, ox::StringView(assetId), forceLoad);
#else
if constexpr(ox::preloadable<T>::value) {
oxRequire(addr, getPreloadAddr(ctx, file));
@@ -100,9 +129,9 @@ ox::Error writeObj(
return ctx->rom->write(file, objBuff.data(), objBuff.size());
}
ox::Result<ox::UniquePtr<ox::FileSystem>> loadRomFs(ox::CRStringView path) noexcept;
ox::Result<ox::UniquePtr<ox::FileSystem>> loadRomFs(ox::CRStringView assetId) noexcept;
ox::Result<char*> loadRom(ox::CRStringView path = "") noexcept;
ox::Result<char*> loadRom(ox::CRStringView assetId = "") noexcept;
void unloadRom(char*) noexcept;