From 71354fcbbc7a5d30291934efc8433768e234496a Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 13 Feb 2023 23:11:25 -0600 Subject: [PATCH] [nostalgia] Fix the initialization of UUIDs for new assets --- src/nostalgia/foundation/asset.hpp | 2 +- src/nostalgia/foundation/media.cpp | 12 +++++++++--- src/nostalgia/foundation/media.hpp | 4 +++- src/nostalgia/studio/lib/itemmaker.hpp | 8 ++++++-- src/nostalgia/studio/lib/project.cpp | 6 ++++-- src/nostalgia/studio/lib/project.hpp | 2 +- src/nostalgia/studio/newmenu.cpp | 5 +++-- 7 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/nostalgia/foundation/asset.hpp b/src/nostalgia/foundation/asset.hpp index 80631976..e5a7306d 100644 --- a/src/nostalgia/foundation/asset.hpp +++ b/src/nostalgia/foundation/asset.hpp @@ -19,7 +19,7 @@ ox::Result readUuidHeader(const ox::Buffer &buff) noexcept; ox::Result readUuidHeader(const char *buff, std::size_t buffLen) noexcept; ox::Error writeUuidHeader(ox::Writer_c auto *writer, const ox::UUID &uuid) noexcept { - const auto hdr = ox::sfmt>("N1{};", uuid.toString()); + const auto hdr = ox::sfmt>("N1;{};", uuid.toString()); return write(writer, hdr); } diff --git a/src/nostalgia/foundation/media.cpp b/src/nostalgia/foundation/media.cpp index b3907edf..c277a581 100644 --- a/src/nostalgia/foundation/media.cpp +++ b/src/nostalgia/foundation/media.cpp @@ -45,6 +45,11 @@ static void clearUuidMap(Context *ctx) noexcept { ctx->pathToUuid.clear(); } +void createUuidMapping(Context *ctx, const ox::String &filePath, const ox::UUID &uuid) noexcept { + ctx->pathToUuid[filePath] = uuid; + ctx->uuidToPath[uuid.toString()] = filePath; +} + static ox::Error buildUuidMap(Context *ctx, ox::CRStringView path) noexcept { oxRequire(files, ctx->rom->ls(path)); for (const auto &f : files) { @@ -52,9 +57,10 @@ static ox::Error buildUuidMap(Context *ctx, ox::CRStringView path) noexcept { oxRequire(stat, ctx->rom->stat(filePath)); if (stat.fileType == ox::FileType::NormalFile) { oxRequire(data, ctx->rom->read(filePath)); - oxRequire(hdr, readAssetHeader(data)); - ctx->pathToUuid[filePath] = hdr.uuid; - ctx->uuidToPath[hdr.uuid.toString()] = filePath; + const auto [hdr, err] = readAssetHeader(data); + if (!err) { + createUuidMapping(ctx, filePath, hdr.uuid); + } } else if (stat.fileType == ox::FileType::Directory) { if (!beginsWith(f, ".")) { oxReturnError(buildUuidMap(ctx, filePath)); diff --git a/src/nostalgia/foundation/media.hpp b/src/nostalgia/foundation/media.hpp index 5741a60c..1f0506ce 100644 --- a/src/nostalgia/foundation/media.hpp +++ b/src/nostalgia/foundation/media.hpp @@ -91,6 +91,8 @@ ox::Result> readObjNoCache( #endif +void createUuidMapping(Context *ctx, const ox::String &filePath, const ox::UUID &uuid) noexcept; + ox::Error buildUuidMap(Context *ctx) noexcept; template @@ -135,7 +137,7 @@ ox::Error writeObj( ox::Error setRomFs(Context *ctx, ox::UPtr fs) noexcept; -ox::Result> loadRomFs(ox::CRStringView assetId) noexcept; +ox::Result> loadRomFs(ox::CRStringView path) noexcept; ox::Result loadRom(ox::CRStringView assetId = "") noexcept; diff --git a/src/nostalgia/studio/lib/itemmaker.hpp b/src/nostalgia/studio/lib/itemmaker.hpp index 847c095d..0c56e9ff 100644 --- a/src/nostalgia/studio/lib/itemmaker.hpp +++ b/src/nostalgia/studio/lib/itemmaker.hpp @@ -1,13 +1,16 @@ /* - * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved. + * Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved. */ #pragma once #include +#include #include +#include "context.hpp" + namespace nostalgia::studio { class ItemMaker { @@ -45,8 +48,9 @@ class ItemMakerT: public ItemMaker { fmt(pFmt) { } ox::Error write(core::Context *ctx, ox::CRStringView pName) const noexcept override { - const auto path = ox::sfmt("{}/{}.{}", parentDir, pName, fileExt); + const auto path = ox::sfmt("/{}/{}.{}", parentDir, pName, fileExt); auto sctx = core::applicationData(ctx); + foundation::createUuidMapping(ctx, path, ox::UUID::generate().unwrap()); return sctx->project->writeObj(path, &item, fmt); } }; diff --git a/src/nostalgia/studio/lib/project.cpp b/src/nostalgia/studio/lib/project.cpp index 83188d00..3feee187 100644 --- a/src/nostalgia/studio/lib/project.cpp +++ b/src/nostalgia/studio/lib/project.cpp @@ -87,8 +87,10 @@ ox::Error Project::writeBuff(const ox::StringView &path, const ox::Buffer &buff) ox::Buffer outBuff; outBuff.reserve(buff.size() + HdrSz); ox::BufferWriter writer(&outBuff); - oxRequire(uuid, m_ctx->pathToUuid.at(path)); - oxReturnError(foundation::writeUuidHeader(&writer, *uuid)); + const auto [uuid, err] = m_ctx->pathToUuid.at(path); + if (!err) { + oxReturnError(foundation::writeUuidHeader(&writer, *uuid)); + } oxReturnError(writer.write(buff.data(), buff.size())); const auto newFile = m_fs->stat(path).error != 0; oxReturnError(m_fs->write(path, outBuff.data(), outBuff.size(), ox::FileType::NormalFile)); diff --git a/src/nostalgia/studio/lib/project.hpp b/src/nostalgia/studio/lib/project.hpp index c98317ff..c064c0b4 100644 --- a/src/nostalgia/studio/lib/project.hpp +++ b/src/nostalgia/studio/lib/project.hpp @@ -120,7 +120,7 @@ ox::Error Project::writeObj(const ox::String &path, const T *obj, ox::ClawFormat // replace garbage last character with new line typeOut.back().value = '\n'; // write to FS - const auto typePath = ox::sfmt("{}/{}", descPath, buildTypeId(*t)); + const auto typePath = ox::sfmt("/{}/{}", descPath, buildTypeId(*t)); oxReturnError(writeBuff(typePath, typeOut)); } fileUpdated.emit(path); diff --git a/src/nostalgia/studio/newmenu.cpp b/src/nostalgia/studio/newmenu.cpp index c7b140e0..96862be8 100644 --- a/src/nostalgia/studio/newmenu.cpp +++ b/src/nostalgia/studio/newmenu.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved. + * Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved. */ #include @@ -108,7 +108,8 @@ void NewMenu::drawLastPageButtons(core::Context *ctx) noexcept { } void NewMenu::finish(core::Context *ctx) noexcept { - const auto err = m_types[static_cast(m_selectedType)]->write(ctx, m_itemName); + const auto itemName = ox::String(m_itemName); + const auto err = m_types[static_cast(m_selectedType)]->write(ctx, itemName); if (err) { oxLogError(err); return;