From 53229b05dac922e61de066065488ae1a93bf5eab Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 13 Feb 2023 21:37:14 -0600 Subject: [PATCH] [nostalgia] Fix to allow building UUID map later --- src/nostalgia/foundation/asset.hpp | 5 +++++ src/nostalgia/foundation/foundation.hpp | 3 +-- src/nostalgia/foundation/media.cpp | 14 ++++++++++++++ src/nostalgia/foundation/media.hpp | 2 ++ src/nostalgia/studio/lib/project.cpp | 17 ++++++++++++++--- src/nostalgia/studio/lib/project.hpp | 5 +++-- src/nostalgia/studio/main.cpp | 12 ++++-------- src/nostalgia/studio/studioapp.cpp | 6 +++--- 8 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/nostalgia/foundation/asset.hpp b/src/nostalgia/foundation/asset.hpp index 8013d328..80631976 100644 --- a/src/nostalgia/foundation/asset.hpp +++ b/src/nostalgia/foundation/asset.hpp @@ -18,6 +18,11 @@ 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()); + return write(writer, hdr); +} + template ox::Result readAsset(const ox::Buffer &buff) noexcept { std::size_t offset = 0; diff --git a/src/nostalgia/foundation/foundation.hpp b/src/nostalgia/foundation/foundation.hpp index a5e60787..bf6cdccf 100644 --- a/src/nostalgia/foundation/foundation.hpp +++ b/src/nostalgia/foundation/foundation.hpp @@ -15,9 +15,8 @@ namespace nostalgia::foundation { template ox::Result> init(ox::UPtr &&fs, ox::CRStringView appName) noexcept { auto ctx = ox::make_unique(); - ctx->rom = std::move(fs); ctx->appName = appName; - oxReturnError(buildUuidMap(ctx.get())); + oxIgnoreError(setRomFs(ctx.get(), std::move(fs))); auto mods = modules(); if (mods) { for (auto &mod : *mods) { diff --git a/src/nostalgia/foundation/media.cpp b/src/nostalgia/foundation/media.cpp index b16f4091..b3907edf 100644 --- a/src/nostalgia/foundation/media.cpp +++ b/src/nostalgia/foundation/media.cpp @@ -40,6 +40,11 @@ ox::Result findPreloadSection() noexcept { return OxError(1, "findPreloadSection is unsupported on this platform"); } +static void clearUuidMap(Context *ctx) noexcept { + ctx->uuidToPath.clear(); + ctx->pathToUuid.clear(); +} + static ox::Error buildUuidMap(Context *ctx, ox::CRStringView path) noexcept { oxRequire(files, ctx->rom->ls(path)); for (const auto &f : files) { @@ -76,6 +81,9 @@ ox::Error buildUuidMap(Context *ctx) noexcept { namespace nostalgia::foundation { +static void clearUuidMap(Context*) noexcept { +} + ox::Error buildUuidMap(Context*) noexcept { return {}; } @@ -122,6 +130,12 @@ ox::Result getPreloadAddr(foundation::Context *ctx, const ox::FileA namespace nostalgia::foundation { +ox::Error setRomFs(Context *ctx, ox::UPtr fs) noexcept { + ctx->rom = std::move(fs); + clearUuidMap(ctx); + return buildUuidMap(ctx); +} + ox::Result> loadRomFs(ox::CRStringView path) noexcept { const auto lastDot = ox_lastIndexOf(path, '.'); const auto fsExt = lastDot != -1 ? path.substr(static_cast(lastDot)) : ""; diff --git a/src/nostalgia/foundation/media.hpp b/src/nostalgia/foundation/media.hpp index 716517fe..5741a60c 100644 --- a/src/nostalgia/foundation/media.hpp +++ b/src/nostalgia/foundation/media.hpp @@ -133,6 +133,8 @@ ox::Error writeObj( return ctx->rom->write(file, objBuff.data(), objBuff.size()); } +ox::Error setRomFs(Context *ctx, ox::UPtr fs) noexcept; + ox::Result> loadRomFs(ox::CRStringView assetId) noexcept; ox::Result loadRom(ox::CRStringView assetId = "") noexcept; diff --git a/src/nostalgia/studio/lib/project.cpp b/src/nostalgia/studio/lib/project.cpp index 6873f88c..83188d00 100644 --- a/src/nostalgia/studio/lib/project.cpp +++ b/src/nostalgia/studio/lib/project.cpp @@ -21,7 +21,11 @@ static void generateTypes(ox::TypeStore *ts) noexcept { } } -Project::Project(ox::FileSystem *fs, ox::String path) noexcept: m_path(std::move(path)), m_typeStore(fs), m_fs(fs) { +Project::Project(foundation::Context *ctx, ox::String path) noexcept: + m_path(std::move(path)), + m_typeStore(ctx->rom.get()), + m_fs(ctx->rom.get()), + m_ctx(ctx) { oxTracef("nostalgia::studio", "Project: {}", m_path); generateTypes(&m_typeStore); buildFileIndex(); @@ -79,15 +83,22 @@ void Project::indexFile(ox::CRStringView path) noexcept { } ox::Error Project::writeBuff(const ox::StringView &path, const ox::Buffer &buff) noexcept { + constexpr auto HdrSz = 40; + ox::Buffer outBuff; + outBuff.reserve(buff.size() + HdrSz); + ox::BufferWriter writer(&outBuff); + oxRequire(uuid, m_ctx->pathToUuid.at(path)); + 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, buff.data(), buff.size(), ox::FileType::NormalFile)); + oxReturnError(m_fs->write(path, outBuff.data(), outBuff.size(), ox::FileType::NormalFile)); if (newFile) { fileAdded.emit(path); indexFile(path); } else { fileUpdated.emit(path); } - return OxError(0); + return {}; } ox::Result Project::loadBuff(const ox::String &path) const noexcept { diff --git a/src/nostalgia/studio/lib/project.hpp b/src/nostalgia/studio/lib/project.hpp index f6cd42dc..c98317ff 100644 --- a/src/nostalgia/studio/lib/project.hpp +++ b/src/nostalgia/studio/lib/project.hpp @@ -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. */ #pragma once @@ -43,10 +43,11 @@ class NOSTALGIASTUDIO_EXPORT Project { ox::String m_path; mutable core::TypeStore m_typeStore; mutable ox::FileSystem *m_fs = nullptr; + foundation::Context *m_ctx = nullptr; ox::HashMap> m_fileExtFileMap; public: - explicit Project(ox::FileSystem *fs, ox::String path) noexcept; + explicit Project(foundation::Context *ctx, ox::String path) noexcept; ox::Error create() noexcept; diff --git a/src/nostalgia/studio/main.cpp b/src/nostalgia/studio/main.cpp index b7dea528..4771145a 100644 --- a/src/nostalgia/studio/main.cpp +++ b/src/nostalgia/studio/main.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -54,16 +55,11 @@ static ox::Error run(ox::UniquePtr fs) noexcept { return core::run(ctx.get()); } -static ox::Error run(int argc, const char **argv) noexcept { +static ox::Error run(int, const char**) noexcept { ox::trace::init(); + ox::UUID::seedGenerator({}); loadModules(); - if (argc >= 2) { - const auto path = argv[1]; - oxRequireM(fs, foundation::loadRomFs(path)); - return run(std::move(fs)); - } else { - return run(ox::UniquePtr(nullptr)); - } + return run(ox::UniquePtr(nullptr)); } } diff --git a/src/nostalgia/studio/studioapp.cpp b/src/nostalgia/studio/studioapp.cpp index ecf3f43b..8d73b4c1 100644 --- a/src/nostalgia/studio/studioapp.cpp +++ b/src/nostalgia/studio/studioapp.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 @@ -286,9 +286,9 @@ void StudioUI::save() noexcept { ox::Error StudioUI::openProject(ox::CRStringView path) noexcept { oxRequireM(fs, foundation::loadRomFs(path)); - m_ctx->rom = std::move(fs); + oxReturnError(foundation::setRomFs(m_ctx, std::move(fs))); core::setWindowTitle(m_ctx, ox::sfmt("Nostalgia Studio - {}", path)); - m_project = ox::make_unique(m_ctx->rom.get(), path); + m_project = ox::make_unique(m_ctx, path); auto sctx = applicationData(m_ctx); sctx->project = m_project.get(); m_project->fileAdded.connect(m_projectExplorer.get(), &ProjectExplorer::refreshProjectTreeModel);