[keel] Ensure consistent asset IDs in AssetManager

This commit is contained in:
Gary Talent 2024-05-27 00:45:41 -05:00
parent af634bd4e5
commit e62426b085
2 changed files with 82 additions and 25 deletions

View File

@ -32,6 +32,33 @@ oxModelEnd()
ox::Result<std::size_t> getPreloadAddr(keel::Context &ctx, ox::FileAddress const&file) noexcept; ox::Result<std::size_t> getPreloadAddr(keel::Context &ctx, ox::FileAddress const&file) noexcept;
ox::Result<std::size_t> getPreloadAddr(keel::Context &ctx, ox::CRStringView file) noexcept; ox::Result<std::size_t> getPreloadAddr(keel::Context &ctx, ox::CRStringView file) noexcept;
void createUuidMapping(Context &ctx, ox::StringView filePath, ox::UUID const&uuid) noexcept;
ox::Error buildUuidMap(Context &ctx) noexcept;
ox::Result<ox::UUID> pathToUuid(Context &ctx, ox::CRStringView path) noexcept;
ox::Result<ox::UUID> getUuid(Context &ctx, ox::FileAddress const&fileAddr) noexcept;
ox::Result<ox::UUID> getUuid(Context &ctx, ox::StringView path) noexcept;
ox::Result<ox::CStringView> getPath(Context &ctx, ox::FileAddress const&fileAddr) noexcept;
ox::Result<ox::CStringView> getPath(Context &ctx, ox::CStringView fileId) noexcept;
constexpr ox::Result<ox::UUID> uuidUrlToUuid(ox::StringView uuidUrl) noexcept {
return ox::UUID::fromString(substr(uuidUrl, 7));
}
ox::Result<ox::CStringView> uuidUrlToPath(Context &ctx, ox::StringView uuid) noexcept;
ox::Result<ox::CStringView> uuidToPath(Context &ctx, ox::CRStringView uuid) noexcept;
ox::Result<ox::CStringView> uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept;
ox::Error performPackTransforms(Context &ctx, ox::Buffer &clawData) noexcept;
#ifndef OX_BARE_METAL #ifndef OX_BARE_METAL
template<typename T> template<typename T>
@ -39,7 +66,8 @@ ox::Result<keel::AssetRef<T>> readObjFile(
keel::Context &ctx, keel::Context &ctx,
ox::StringView assetId, ox::StringView assetId,
bool forceLoad) noexcept { bool forceLoad) noexcept {
constexpr auto readConvert = [](Context &ctx, const ox::Buffer &buff) -> ox::Result<T> { constexpr auto readConvert = [](Context &ctx, const ox::Buffer &buff)
-> ox::Result<T> {
auto [obj, err] = readAsset<T>(buff); auto [obj, err] = readAsset<T>(buff);
if (err) { if (err) {
if (err != ox::Error_ClawTypeVersionMismatch && err != ox::Error_ClawTypeMismatch) { if (err != ox::Error_ClawTypeVersionMismatch && err != ox::Error_ClawTypeMismatch) {
@ -50,12 +78,18 @@ ox::Result<keel::AssetRef<T>> readObjFile(
return std::move(obj); return std::move(obj);
}; };
ox::StringView path; ox::StringView path;
ox::UUIDStr uuidStr;
if (beginsWith(assetId, "uuid://")) { if (beginsWith(assetId, "uuid://")) {
assetId = substr(assetId, 7); assetId = substr(assetId, 7);
oxRequire(p, ctx.uuidToPath.at(assetId)); oxRequire(p, ctx.uuidToPath.at(assetId));
path = *p; path = *p;
} else { } else {
path = assetId; path = assetId;
auto const [uuid, uuidErr] = getUuid(ctx, assetId);
if (!uuidErr) {
uuidStr = uuid.toString();
assetId = uuidStr;
}
} }
if (forceLoad) { if (forceLoad) {
oxRequire(buff, ctx.rom->read(path)); oxRequire(buff, ctx.rom->read(path));
@ -88,37 +122,17 @@ ox::Result<keel::AssetRef<T>> readObjNoCache(
} }
#endif #endif
void createUuidMapping(Context &ctx, ox::StringView filePath, ox::UUID const&uuid) noexcept;
ox::Error buildUuidMap(Context &ctx) noexcept;
ox::Result<ox::UUID> pathToUuid(Context &ctx, ox::CRStringView path) noexcept;
constexpr ox::Result<ox::UUID> uuidUrlToUuid(ox::StringView uuidUrl) noexcept {
return ox::UUID::fromString(substr(uuidUrl, 7));
}
ox::Result<ox::CStringView> uuidUrlToPath(Context &ctx, ox::StringView uuid) noexcept;
ox::Result<ox::CStringView> uuidToPath(Context &ctx, ox::CRStringView uuid) noexcept;
ox::Result<ox::CStringView> uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept;
ox::Error performPackTransforms(Context &ctx, ox::Buffer &clawData) noexcept;
template<typename T> template<typename T>
ox::Result<AssetRef<T>> setAsset(keel::Context &ctx, ox::StringView assetId, T const&asset) noexcept { ox::Result<AssetRef<T>> setAsset(keel::Context &ctx, ox::StringView assetId, T const&asset) noexcept {
#ifndef OX_BARE_METAL #ifndef OX_BARE_METAL
if (assetId.len() == 0) { if (assetId.len() == 0) {
return OxError(1, "Invalid asset ID"); return OxError(1, "Invalid asset ID");
} }
ox::UUIDStr idStr; ox::UUIDStr uuidStr;
if (assetId[0] == '/') { if (assetId[0] == '/') {
auto const [id, err] = ctx.pathToUuid.at(assetId); oxRequire(id, ctx.pathToUuid.at(assetId));
oxReturnError(err); uuidStr = id->toString();
idStr = id->toString(); assetId = uuidStr;
assetId = idStr;
} }
return ctx.assetManager.setAsset(assetId, asset); return ctx.assetManager.setAsset(assetId, asset);
#else #else

View File

@ -83,6 +83,49 @@ ox::Result<ox::UUID> pathToUuid(Context &ctx, ox::CRStringView path) noexcept {
#endif #endif
} }
ox::Result<ox::UUID> getUuid(Context &ctx, ox::FileAddress const&fileAddr) noexcept {
oxRequire(path, fileAddr.getPath());
return getUuid(ctx, path);
}
ox::Result<ox::UUID> getUuid(Context &ctx, ox::StringView path) noexcept {
if (beginsWith(path, "uuid://")) {
auto const uuid = substr(path, 7);
return ox::UUID::fromString(uuid);
} else {
return pathToUuid(ctx, path);
}
}
ox::Result<ox::CStringView> getPath(Context &ctx, ox::FileAddress const&fileAddr) noexcept {
oxRequire(path, fileAddr.getPath());
if (beginsWith(path, "uuid://")) {
auto const uuid = substr(path, 7);
#ifndef OX_BARE_METAL
oxRequireM(out, ctx.uuidToPath.at(uuid));
return ox::CStringView{*out};
#else
return OxError(1, "UUID to path conversion not supported on this platform");
#endif
} else {
return ox::CStringView{path};
}
}
ox::Result<ox::CStringView> getPath(Context &ctx, ox::CStringView fileId) noexcept {
if (beginsWith(fileId, "uuid://")) {
auto const uuid = substr(fileId, 7);
#ifndef OX_BARE_METAL
oxRequireM(out, ctx.uuidToPath.at(uuid));
return ox::CStringView{*out};
#else
return OxError(1, "UUID to path conversion not supported on this platform");
#endif
} else {
return ox::CStringView{fileId};
}
}
ox::Result<ox::CStringView> uuidUrlToPath(Context &ctx, ox::StringView uuid) noexcept { ox::Result<ox::CStringView> uuidUrlToPath(Context &ctx, ox::StringView uuid) noexcept {
uuid = substr(uuid, 7); uuid = substr(uuid, 7);
#ifndef OX_BARE_METAL #ifndef OX_BARE_METAL