[nostalgia] Fix the initialization of UUIDs for new assets

This commit is contained in:
Gary Talent 2023-02-13 23:11:25 -06:00
parent 044a87b1c4
commit 71354fcbbc
7 changed files with 27 additions and 12 deletions

View File

@ -19,7 +19,7 @@ ox::Result<ox::UUID> readUuidHeader(const ox::Buffer &buff) noexcept;
ox::Result<ox::UUID> 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<ox::BString<40>>("N1{};", uuid.toString());
const auto hdr = ox::sfmt<ox::BString<40>>("N1;{};", uuid.toString());
return write(writer, hdr);
}

View File

@ -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));

View File

@ -91,6 +91,8 @@ ox::Result<foundation::AssetRef<T>> readObjNoCache(
#endif
void createUuidMapping(Context *ctx, const ox::String &filePath, const ox::UUID &uuid) noexcept;
ox::Error buildUuidMap(Context *ctx) noexcept;
template<typename T>
@ -135,7 +137,7 @@ ox::Error writeObj(
ox::Error setRomFs(Context *ctx, ox::UPtr<ox::FileSystem> fs) noexcept;
ox::Result<ox::UniquePtr<ox::FileSystem>> loadRomFs(ox::CRStringView assetId) noexcept;
ox::Result<ox::UniquePtr<ox::FileSystem>> loadRomFs(ox::CRStringView path) noexcept;
ox::Result<char*> loadRom(ox::CRStringView assetId = "") noexcept;

View File

@ -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 <ox/claw/claw.hpp>
#include <nostalgia/foundation/media.hpp>
#include <nostalgia/core/context.hpp>
#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<studio::StudioContext>(ctx);
foundation::createUuidMapping(ctx, path, ox::UUID::generate().unwrap());
return sctx->project->writeObj(path, &item, fmt);
}
};

View File

@ -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));
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));

View File

@ -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);

View File

@ -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 <imgui.h>
@ -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<std::size_t>(m_selectedType)]->write(ctx, m_itemName);
const auto itemName = ox::String(m_itemName);
const auto err = m_types[static_cast<std::size_t>(m_selectedType)]->write(ctx, itemName);
if (err) {
oxLogError(err);
return;