From 3635702edefdf89ea440c58916dac02f1a872717 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 1 Jun 2024 20:14:29 -0500 Subject: [PATCH] [nostalgia] Cleanup config IO --- .../tilesheeteditor/tilesheeteditor-imgui.cpp | 28 +++---------------- src/olympic/studio/applib/src/studioapp.cpp | 24 ++++++++-------- .../studio/modlib/include/studio/configio.hpp | 28 +++++++++++++------ 3 files changed, 36 insertions(+), 44 deletions(-) diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp index da406f06..f14c4451 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp @@ -15,16 +15,6 @@ namespace nostalgia::core { namespace ig = studio::ig; -static ox::String configName(ox::StringView str) noexcept { - auto out = ox::String{str}; - for (auto &c : out) { - if (c == '/' || c == '\\') { - c = '%'; - } - } - return out; -} - struct TileSheetEditorConfig { static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.studio.TileSheetEditorConfig"; static constexpr auto TypeVersion = 1; @@ -35,16 +25,6 @@ oxModelBegin(TileSheetEditorConfig) oxModelFieldRename(activeSubsheet, active_subsheet) oxModelEnd() -struct TileSheetEditorConfigs { - static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.studio.TileSheetEditorConfigs"; - static constexpr auto TypeVersion = 1; - ox::HashMap configs; -}; - -oxModelBegin(TileSheetEditorConfigs) - oxModelField(configs) -oxModelEnd() - static ox::Vector normalizePixelSizes( ox::Vector const&inPixels, int const bpp) noexcept { @@ -120,7 +100,7 @@ TileSheetEditorImGui::TileSheetEditorImGui(studio::StudioContext &sctx, ox::Stri m_model.paletteChanged.connect(this, &TileSheetEditorImGui::setPaletteSelection); // load config auto const&config = studio::readConfig( - keelCtx(m_sctx), configName(itemPath())); + keelCtx(m_sctx), itemPath()); if (config.ok()) { m_model.setActiveSubsheet(validateSubSheetIdx(m_model.img(), config.value.activeSubsheet)); } @@ -537,9 +517,9 @@ ox::Error TileSheetEditorImGui::setPaletteSelection() noexcept { void TileSheetEditorImGui::setActiveSubsheet(TileSheet::SubSheetIdx path) noexcept { m_model.setActiveSubsheet(path); - studio::editConfig(keelCtx(m_sctx), configName(itemPath()), - [&path](TileSheetEditorConfig *config) { - config->activeSubsheet = std::move(path); + studio::editConfig(keelCtx(m_sctx), itemPath(), + [&path](TileSheetEditorConfig &config) { + config.activeSubsheet = std::move(path); }); } diff --git a/src/olympic/studio/applib/src/studioapp.cpp b/src/olympic/studio/applib/src/studioapp.cpp index 1c43eed2..f6a6b298 100644 --- a/src/olympic/studio/applib/src/studioapp.cpp +++ b/src/olympic/studio/applib/src/studioapp.cpp @@ -203,8 +203,8 @@ void StudioUI::drawTabs() noexcept { if (ImGui::BeginTabItem(e->itemDisplayName().c_str(), &open, flags)) { if (m_activeEditor != e.get()) { m_activeEditor = e.get(); - studio::editConfig(keelCtx(m_ctx), [&](StudioConfig *config) { - config->activeTabItemName = m_activeEditor->itemPath(); + studio::editConfig(keelCtx(m_ctx), [&](StudioConfig &config) { + config.activeTabItemName = m_activeEditor->itemPath(); }); } if (m_activeEditorUpdatePending == e.get()) { @@ -258,8 +258,8 @@ void StudioUI::loadModules() noexcept { void StudioUI::toggleProjectExplorer() noexcept { m_showProjectExplorer = !m_showProjectExplorer; - studio::editConfig(keelCtx(m_ctx), [&](StudioConfig *config) { - config->showProjectExplorer = m_showProjectExplorer; + studio::editConfig(keelCtx(m_ctx), [&](StudioConfig &config) { + config.showProjectExplorer = m_showProjectExplorer; }); } @@ -342,9 +342,9 @@ ox::Error StudioUI::openProjectPath(ox::CRStringView path) noexcept { m_project->fileDeleted.connect(&m_projectExplorer, &ProjectExplorer::refreshProjectTreeModel); m_openFiles.clear(); m_editors.clear(); - studio::editConfig(keelCtx(m_ctx), [&](StudioConfig *config) { - config->projectPath = ox::String(m_project->projectPath()); - config->openFiles.clear(); + studio::editConfig(keelCtx(m_ctx), [&](StudioConfig &config) { + config.projectPath = ox::String(m_project->projectPath()); + config.openFiles.clear(); }); return m_projectExplorer.refreshProjectTreeModel(); } @@ -395,9 +395,9 @@ ox::Error StudioUI::openFileActiveTab(ox::CRStringView path, bool makeActiveTab) m_activeEditorUpdatePending = editor; } // save to config - studio::editConfig(keelCtx(m_ctx), [&](StudioConfig *config) { - if (!config->openFiles.contains(path)) { - config->openFiles.emplace_back(path); + studio::editConfig(keelCtx(m_ctx), [&](StudioConfig &config) { + if (!config.openFiles.contains(path)) { + config.openFiles.emplace_back(path); } }); return {}; @@ -409,8 +409,8 @@ ox::Error StudioUI::closeFile(ox::CRStringView path) noexcept { } std::ignore = m_openFiles.erase(std::remove(m_openFiles.begin(), m_openFiles.end(), path)); // save to config - studio::editConfig(keelCtx(m_ctx), [&](StudioConfig *config) { - std::ignore = config->openFiles.erase(std::remove(config->openFiles.begin(), config->openFiles.end(), path)); + studio::editConfig(keelCtx(m_ctx), [&](StudioConfig &config) { + std::ignore = config.openFiles.erase(std::remove(config.openFiles.begin(), config.openFiles.end(), path)); }); return {}; } diff --git a/src/olympic/studio/modlib/include/studio/configio.hpp b/src/olympic/studio/modlib/include/studio/configio.hpp index 14d476ed..6798c546 100644 --- a/src/olympic/studio/modlib/include/studio/configio.hpp +++ b/src/olympic/studio/modlib/include/studio/configio.hpp @@ -17,13 +17,25 @@ namespace studio { +namespace detail { +inline ox::String slashesToPct(ox::StringView str) noexcept { + auto out = ox::String{str}; + for (auto&c: out) { + if (c == '/' || c == '\\') { + c = '%'; + } + } + return out; +} +} + [[nodiscard]] ox::String configPath(keel::Context const&ctx) noexcept; template ox::Result readConfig(keel::Context &ctx, ox::CRStringView name) noexcept { oxAssert(name != "", "Config type has no TypeName"); - auto const path = ox::sfmt("/{}.json", name); + auto const path = ox::sfmt("/{}.json", detail::slashesToPct(name)); ox::PassThroughFS fs(configPath(ctx)); auto const [buff, err] = fs.read(path); if (err) { @@ -40,15 +52,15 @@ ox::Result readConfig(keel::Context &ctx) noexcept { } template -ox::Error writeConfig(keel::Context &ctx, ox::CRStringView name, T *data) noexcept { +ox::Error writeConfig(keel::Context &ctx, ox::CRStringView name, T const&data) noexcept { oxAssert(name != "", "Config type has no TypeName"); - auto const path = ox::sfmt("/{}.json", name); + auto const path = ox::sfmt("/{}.json", detail::slashesToPct(name)); ox::PassThroughFS fs(configPath(ctx)); if (auto const err = fs.mkdir("/", true)) { oxErrf("Could not create config directory: {}\n", toStr(err)); return err; } - oxRequireM(buff, ox::writeOC(*data)); + oxRequireM(buff, ox::writeOC(data)); *buff.back().value = '\n'; if (auto const err = fs.write(path, buff.data(), buff.size())) { oxErrf("Could not read config file: {}\n", toStr(err)); @@ -58,7 +70,7 @@ ox::Error writeConfig(keel::Context &ctx, ox::CRStringView name, T *data) noexce } template -ox::Error writeConfig(keel::Context &ctx, T *data) noexcept { +ox::Error writeConfig(keel::Context &ctx, T const&data) noexcept { constexpr auto TypeName = ox::requireModelTypeName(); return writeConfig(ctx, TypeName, data); } @@ -68,7 +80,7 @@ void openConfig(keel::Context &ctx, ox::CRStringView name, Func f) noexcept { oxAssert(name != "", "Config type has no TypeName"); auto const [c, err] = readConfig(ctx, name); oxLogError(err); - f(&c); + f(c); } template @@ -82,8 +94,8 @@ void editConfig(keel::Context &ctx, ox::CRStringView name, Func f) noexcept { oxAssert(name != "", "Config type has no TypeName"); auto [c, err] = readConfig(ctx, name); oxLogError(err); - f(&c); - oxLogError(writeConfig(ctx, name, &c)); + f(c); + oxLogError(writeConfig(ctx, name, c)); } template