From e543131f0d6c57990837f8447a2433595800fa9f Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 30 Nov 2023 00:57:47 -0600 Subject: [PATCH] [nostalgia,studio] Cleanup Studio --- .../core/include/nostalgia/core/consts.hpp | 6 ++-- .../core/src/studio/paletteeditor-imgui.cpp | 2 +- .../modules/core/src/studio/studiomodule.cpp | 14 ++------ .../core/src/studio/tilesheeteditormodel.cpp | 2 +- .../scene/src/studio/sceneeditor-imgui.cpp | 2 +- .../modlib/include/studio/itemmaker.hpp | 36 +++++++++++++------ src/studio/modlib/include/studio/module.hpp | 22 ++++++++++++ src/studio/modlib/include/studio/project.hpp | 26 +++++++------- src/studio/modlib/src/project.cpp | 12 +++---- 9 files changed, 75 insertions(+), 47 deletions(-) diff --git a/src/nostalgia/modules/core/include/nostalgia/core/consts.hpp b/src/nostalgia/modules/core/include/nostalgia/core/consts.hpp index db390f7f..eb884755 100644 --- a/src/nostalgia/modules/core/include/nostalgia/core/consts.hpp +++ b/src/nostalgia/modules/core/include/nostalgia/core/consts.hpp @@ -4,13 +4,15 @@ #pragma once +#include + namespace nostalgia::core { constexpr auto TileWidth = 8; constexpr auto TileHeight = 8; constexpr auto PixelsPerTile = TileWidth * TileHeight; -constexpr auto FileExt_ng = "ng"; -constexpr auto FileExt_npal = "npal"; +constexpr ox::StringLiteral FileExt_ng("ng"); +constexpr ox::StringLiteral FileExt_npal("npal"); } diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor-imgui.cpp b/src/nostalgia/modules/core/src/studio/paletteeditor-imgui.cpp index 39255ba8..7489d357 100644 --- a/src/nostalgia/modules/core/src/studio/paletteeditor-imgui.cpp +++ b/src/nostalgia/modules/core/src/studio/paletteeditor-imgui.cpp @@ -140,7 +140,7 @@ void PaletteEditorImGui::draw(turbine::Context*) noexcept { ox::Error PaletteEditorImGui::saveItem() noexcept { const auto sctx = applicationData(*m_ctx); - return sctx->project->writeObj(m_itemPath, &m_pal); + return sctx->project->writeObj(m_itemPath, m_pal); } } diff --git a/src/nostalgia/modules/core/src/studio/studiomodule.cpp b/src/nostalgia/modules/core/src/studio/studiomodule.cpp index d4fd7e91..7d261b5c 100644 --- a/src/nostalgia/modules/core/src/studio/studiomodule.cpp +++ b/src/nostalgia/modules/core/src/studio/studiomodule.cpp @@ -14,18 +14,8 @@ namespace nostalgia::core { class StudioModule: public studio::Module { ox::Vector editors(turbine::Context *ctx) const noexcept final { return { - { - {FileExt_ng}, - [ctx](ox::CRStringView path) -> ox::Result { - return ox::makeCatch(ctx, ox::String(path)); - } - }, - { - {FileExt_npal}, - [ctx](ox::CRStringView path) -> ox::Result { - return ox::makeCatch(ctx, ox::String(path)); - } - } + studio::editorMaker(ctx, FileExt_ng), + studio::editorMaker(ctx, FileExt_npal), }; } diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditormodel.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditormodel.cpp index 51ebf04d..ea1c1d4e 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditormodel.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditormodel.cpp @@ -773,7 +773,7 @@ void TileSheetEditorModel::ackUpdate() noexcept { ox::Error TileSheetEditorModel::saveFile() noexcept { const auto sctx = applicationData(*m_ctx); - return sctx->project->writeObj(m_path, &m_img); + return sctx->project->writeObj(m_path, m_img); } bool TileSheetEditorModel::pixelSelected(std::size_t idx) const noexcept { diff --git a/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.cpp b/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.cpp index 3e7e4c51..2d72964c 100644 --- a/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.cpp +++ b/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.cpp @@ -49,7 +49,7 @@ void SceneEditorImGui::onActivated() noexcept { ox::Error SceneEditorImGui::saveItem() noexcept { const auto sctx = applicationData(m_ctx); - oxReturnError(sctx->project->writeObj(m_itemPath, &m_editor.scene())); + oxReturnError(sctx->project->writeObj(m_itemPath, m_editor.scene())); oxReturnError(m_ctx.keelCtx.assetManager.setAsset(m_itemPath, m_editor.scene())); return {}; } diff --git a/src/studio/modlib/include/studio/itemmaker.hpp b/src/studio/modlib/include/studio/itemmaker.hpp index 92231454..9bed54c0 100644 --- a/src/studio/modlib/include/studio/itemmaker.hpp +++ b/src/studio/modlib/include/studio/itemmaker.hpp @@ -18,10 +18,10 @@ class ItemMaker { const ox::String name; const ox::String parentDir; const ox::String fileExt; - constexpr explicit ItemMaker(ox::String pName, ox::String pParentDir, ox::String pFileExt) noexcept: + constexpr explicit ItemMaker(ox::String pName, ox::String pParentDir, ox::CRStringView pFileExt) noexcept: name(std::move(pName)), parentDir(std::move(pParentDir)), - fileExt(std::move(pFileExt)) { + fileExt(pFileExt) { } virtual ~ItemMaker() noexcept = default; virtual ox::Error write(turbine::Context *ctx, ox::CRStringView pName) const noexcept = 0; @@ -33,25 +33,39 @@ class ItemMakerT: public ItemMaker { const T item; const ox::ClawFormat fmt; public: - constexpr explicit ItemMakerT(ox::String pDisplayName, ox::String pParentDir, ox::String fileExt, ox::ClawFormat pFmt = ox::ClawFormat::Metal) noexcept: - ItemMaker(std::move(pDisplayName), std::move(pParentDir), std::move(fileExt)), + constexpr explicit ItemMakerT( + ox::String pDisplayName, + ox::String pParentDir, + ox::CRStringView fileExt, + ox::ClawFormat pFmt = ox::ClawFormat::Metal) noexcept: + ItemMaker(std::move(pDisplayName), std::move(pParentDir), fileExt), fmt(pFmt) { } - constexpr ItemMakerT(ox::String pDisplayName, ox::String pParentDir, ox::String fileExt, const T &pItem, ox::ClawFormat pFmt) noexcept: - ItemMaker(std::move(pDisplayName), std::move(pParentDir), std::move(fileExt)), + constexpr ItemMakerT( + ox::String pDisplayName, + ox::String pParentDir, + ox::CRStringView fileExt, + T pItem, + ox::ClawFormat pFmt) noexcept: + ItemMaker(std::move(pDisplayName), std::move(pParentDir), fileExt), item(pItem), fmt(pFmt) { } - constexpr ItemMakerT(ox::String pDisplayName, ox::String pParentDir, T &&pItem, ox::ClawFormat pFmt) noexcept: - ItemMaker(std::move(pDisplayName), std::move(pParentDir), std::move(fileExt)), - item(std::forward(pItem)), - fmt(pFmt) { + constexpr ItemMakerT( + ox::String pDisplayName, + ox::String pParentDir, + ox::CRStringView fileExt, + T &&pItem, + ox::ClawFormat pFmt) noexcept: + ItemMaker(std::move(pDisplayName), std::move(pParentDir), fileExt), + item(std::move(pItem)), + fmt(pFmt) { } ox::Error write(turbine::Context *ctx, ox::CRStringView pName) const noexcept override { const auto path = ox::sfmt("/{}/{}.{}", parentDir, pName, fileExt); auto sctx = turbine::applicationData(*ctx); keel::createUuidMapping(&ctx->keelCtx, path, ox::UUID::generate().unwrap()); - return sctx->project->writeObj(path, &item, fmt); + return sctx->project->writeObj(path, item, fmt); } }; diff --git a/src/studio/modlib/include/studio/module.hpp b/src/studio/modlib/include/studio/module.hpp index 68723ce9..95dd8dfb 100644 --- a/src/studio/modlib/include/studio/module.hpp +++ b/src/studio/modlib/include/studio/module.hpp @@ -33,4 +33,26 @@ class Module { }; +template +[[nodiscard]] +studio::EditorMaker editorMaker(turbine::Context *ctx, ox::CRStringView ext) noexcept { + return { + {ox::String(ext)}, + [ctx](ox::CRStringView path) -> ox::Result { + return ox::makeCatch(ctx, ox::String(path)); + } + }; +} + +template +[[nodiscard]] +studio::EditorMaker editorMaker(turbine::Context *ctx, ox::Vector exts) noexcept { + return { + std::move(exts), + [ctx](ox::CRStringView path) -> ox::Result { + return ox::makeCatch(ctx, ox::String(path)); + } + }; +} + } diff --git a/src/studio/modlib/include/studio/project.hpp b/src/studio/modlib/include/studio/project.hpp index 397478a7..ccd3ecf5 100644 --- a/src/studio/modlib/include/studio/project.hpp +++ b/src/studio/modlib/include/studio/project.hpp @@ -38,7 +38,7 @@ constexpr ox::Result fileExt(ox::CRStringView path) noexcept { class Project { private: - keel::Context *m_ctx = nullptr; + keel::Context &m_ctx; ox::String m_path; ox::String m_projectDataDir; mutable keel::TypeStore m_typeStore; @@ -60,12 +60,12 @@ class Project { */ template ox::Error writeObj( - const ox::StringView &path, - const T *obj, + ox::CRStringView path, + T const&obj, ox::ClawFormat fmt = ox::ClawFormat::Metal) noexcept; template - ox::Result loadObj(const ox::StringView &path) const noexcept; + ox::Result loadObj(ox::CRStringView path) const noexcept; ox::Result stat(ox::CRStringView path) const noexcept; @@ -83,9 +83,9 @@ class Project { void indexFile(ox::CRStringView path) noexcept; - ox::Error writeBuff(const ox::StringView &path, const ox::Buffer &buff) noexcept; + ox::Error writeBuff(ox::CRStringView path, ox::Buffer const&buff) noexcept; - ox::Result loadBuff(const ox::StringView &path) const noexcept; + ox::Result loadBuff(ox::CRStringView path) const noexcept; ox::Error lsProcDir(ox::Vector *paths, ox::CRStringView path) const noexcept; @@ -105,14 +105,14 @@ class Project { }; template -ox::Error Project::writeObj(const ox::StringView &path, const T *obj, ox::ClawFormat fmt) noexcept { +ox::Error Project::writeObj(ox::CRStringView path, T const&obj, ox::ClawFormat fmt) noexcept { // write MetalClaw - oxRequireM(buff, ox::writeClaw(*obj, fmt)); + oxRequireM(buff, ox::writeClaw(obj, fmt)); // write to FS oxReturnError(writeBuff(path, buff)); // write type descriptor if (m_typeStore.get().error) { - oxReturnError(ox::buildTypeDef(&m_typeStore, obj)); + oxReturnError(ox::buildTypeDef(&m_typeStore, &obj)); } // write out type store const auto descPath = ox::sfmt("/.{}/type_descriptors", m_projectDataDir); @@ -125,13 +125,13 @@ ox::Error Project::writeObj(const ox::StringView &path, const T *obj, ox::ClawFo const auto typePath = ox::sfmt("/{}/{}", descPath, buildTypeId(*t)); oxReturnError(writeBuff(typePath, typeOut)); } - oxReturnError(keel::setAsset(m_ctx, path, *obj)); + oxReturnError(keel::setAsset(&m_ctx, path, obj)); fileUpdated.emit(path); return {}; } template -ox::Result Project::loadObj(const ox::StringView &path) const noexcept { +ox::Result Project::loadObj(ox::CRStringView path) const noexcept { oxRequire(buff, loadBuff(path)); if constexpr (ox::is_same_v) { return keel::readAsset(&m_typeStore, buff); @@ -151,7 +151,7 @@ ox::Error Project::subscribe(ProjectEvent e, ox::SignalHandler *tgt, Functor &&s case ProjectEvent::FileRecognized: { oxRequire(files, listFiles()); - for (const auto &f : files) { + for (auto const&f : files) { slot(f); } connect(this, &Project::fileRecognized, tgt, slot); @@ -164,7 +164,7 @@ ox::Error Project::subscribe(ProjectEvent e, ox::SignalHandler *tgt, Functor &&s connect(this, &Project::fileUpdated, tgt, slot); break; } - return OxError(0); + return {}; } } diff --git a/src/studio/modlib/src/project.cpp b/src/studio/modlib/src/project.cpp index d9230b60..90f44a95 100644 --- a/src/studio/modlib/src/project.cpp +++ b/src/studio/modlib/src/project.cpp @@ -22,7 +22,7 @@ static void generateTypes(ox::TypeStore *ts) noexcept { } Project::Project(keel::Context *ctx, ox::String path, ox::CRStringView projectDataDir) noexcept: - m_ctx(ctx), + m_ctx(*ctx), m_path(std::move(path)), m_projectDataDir(projectDataDir), m_typeStore(ctx->rom.get(), ox::sfmt("/.{}/type_descriptors", projectDataDir)), @@ -45,7 +45,7 @@ ox::FileSystem *Project::romFs() noexcept { ox::Error Project::mkdir(ox::CRStringView path) const noexcept { oxReturnError(m_fs->mkdir(path, true)); fileUpdated.emit(path); - return OxError(0); + return {}; } ox::Result Project::stat(ox::CRStringView path) const noexcept { @@ -83,12 +83,12 @@ void Project::indexFile(ox::CRStringView path) noexcept { m_fileExtFileMap[ext].emplace_back(path); } -ox::Error Project::writeBuff(const ox::StringView &path, const ox::Buffer &buff) noexcept { +ox::Error Project::writeBuff(ox::CRStringView path, ox::Buffer const&buff) noexcept { constexpr auto HdrSz = 40; ox::Buffer outBuff; outBuff.reserve(buff.size() + HdrSz); ox::BufferWriter writer(&outBuff); - const auto [uuid, err] = m_ctx->pathToUuid.at(path); + const auto [uuid, err] = m_ctx.pathToUuid.at(path); if (!err) { oxReturnError(keel::writeUuidHeader(writer, *uuid)); } @@ -104,7 +104,7 @@ ox::Error Project::writeBuff(const ox::StringView &path, const ox::Buffer &buff) return {}; } -ox::Result Project::loadBuff(const ox::StringView &path) const noexcept { +ox::Result Project::loadBuff(ox::CRStringView path) const noexcept { return m_fs->read(path); } @@ -124,7 +124,7 @@ ox::Error Project::lsProcDir(ox::Vector *paths, ox::CRStringView pat break; } } - return OxError(0); + return {}; } ox::Result> Project::listFiles(ox::CRStringView path) const noexcept {