From e62426b085a24a187d3bc7bef4a09a9295103bcf Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 27 May 2024 00:45:41 -0500 Subject: [PATCH] [keel] Ensure consistent asset IDs in AssetManager --- src/olympic/keel/include/keel/media.hpp | 64 +++++++++++++++---------- src/olympic/keel/src/media.cpp | 43 +++++++++++++++++ 2 files changed, 82 insertions(+), 25 deletions(-) diff --git a/src/olympic/keel/include/keel/media.hpp b/src/olympic/keel/include/keel/media.hpp index e7253750..d13f5acd 100644 --- a/src/olympic/keel/include/keel/media.hpp +++ b/src/olympic/keel/include/keel/media.hpp @@ -32,6 +32,33 @@ oxModelEnd() ox::Result getPreloadAddr(keel::Context &ctx, ox::FileAddress const&file) noexcept; ox::Result 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 pathToUuid(Context &ctx, ox::CRStringView path) noexcept; + +ox::Result getUuid(Context &ctx, ox::FileAddress const&fileAddr) noexcept; + +ox::Result getUuid(Context &ctx, ox::StringView path) noexcept; + +ox::Result getPath(Context &ctx, ox::FileAddress const&fileAddr) noexcept; + +ox::Result getPath(Context &ctx, ox::CStringView fileId) noexcept; + +constexpr ox::Result uuidUrlToUuid(ox::StringView uuidUrl) noexcept { + return ox::UUID::fromString(substr(uuidUrl, 7)); +} + +ox::Result uuidUrlToPath(Context &ctx, ox::StringView uuid) noexcept; + +ox::Result uuidToPath(Context &ctx, ox::CRStringView uuid) noexcept; + +ox::Result uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept; + +ox::Error performPackTransforms(Context &ctx, ox::Buffer &clawData) noexcept; + #ifndef OX_BARE_METAL template @@ -39,7 +66,8 @@ ox::Result> readObjFile( keel::Context &ctx, ox::StringView assetId, bool forceLoad) noexcept { - constexpr auto readConvert = [](Context &ctx, const ox::Buffer &buff) -> ox::Result { + constexpr auto readConvert = [](Context &ctx, const ox::Buffer &buff) + -> ox::Result { auto [obj, err] = readAsset(buff); if (err) { if (err != ox::Error_ClawTypeVersionMismatch && err != ox::Error_ClawTypeMismatch) { @@ -50,12 +78,18 @@ ox::Result> readObjFile( return std::move(obj); }; ox::StringView path; + ox::UUIDStr uuidStr; if (beginsWith(assetId, "uuid://")) { assetId = substr(assetId, 7); oxRequire(p, ctx.uuidToPath.at(assetId)); path = *p; } else { path = assetId; + auto const [uuid, uuidErr] = getUuid(ctx, assetId); + if (!uuidErr) { + uuidStr = uuid.toString(); + assetId = uuidStr; + } } if (forceLoad) { oxRequire(buff, ctx.rom->read(path)); @@ -88,37 +122,17 @@ ox::Result> readObjNoCache( } #endif - -void createUuidMapping(Context &ctx, ox::StringView filePath, ox::UUID const&uuid) noexcept; - -ox::Error buildUuidMap(Context &ctx) noexcept; - -ox::Result pathToUuid(Context &ctx, ox::CRStringView path) noexcept; - -constexpr ox::Result uuidUrlToUuid(ox::StringView uuidUrl) noexcept { - return ox::UUID::fromString(substr(uuidUrl, 7)); -} - -ox::Result uuidUrlToPath(Context &ctx, ox::StringView uuid) noexcept; - -ox::Result uuidToPath(Context &ctx, ox::CRStringView uuid) noexcept; - -ox::Result uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept; - -ox::Error performPackTransforms(Context &ctx, ox::Buffer &clawData) noexcept; - template ox::Result> setAsset(keel::Context &ctx, ox::StringView assetId, T const&asset) noexcept { #ifndef OX_BARE_METAL if (assetId.len() == 0) { return OxError(1, "Invalid asset ID"); } - ox::UUIDStr idStr; + ox::UUIDStr uuidStr; if (assetId[0] == '/') { - auto const [id, err] = ctx.pathToUuid.at(assetId); - oxReturnError(err); - idStr = id->toString(); - assetId = idStr; + oxRequire(id, ctx.pathToUuid.at(assetId)); + uuidStr = id->toString(); + assetId = uuidStr; } return ctx.assetManager.setAsset(assetId, asset); #else diff --git a/src/olympic/keel/src/media.cpp b/src/olympic/keel/src/media.cpp index 679484fa..0f98ca1f 100644 --- a/src/olympic/keel/src/media.cpp +++ b/src/olympic/keel/src/media.cpp @@ -83,6 +83,49 @@ ox::Result pathToUuid(Context &ctx, ox::CRStringView path) noexcept { #endif } +ox::Result getUuid(Context &ctx, ox::FileAddress const&fileAddr) noexcept { + oxRequire(path, fileAddr.getPath()); + return getUuid(ctx, path); +} + +ox::Result 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 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 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 uuidUrlToPath(Context &ctx, ox::StringView uuid) noexcept { uuid = substr(uuid, 7); #ifndef OX_BARE_METAL