[nostalgia] Cleanup config IO

This commit is contained in:
Gary Talent 2024-06-01 20:14:29 -05:00
parent 6cbafc75bf
commit 3635702ede
3 changed files with 36 additions and 44 deletions

View File

@ -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<ox::String, TileSheetEditorConfig> configs;
};
oxModelBegin(TileSheetEditorConfigs)
oxModelField(configs)
oxModelEnd()
static ox::Vector<uint32_t> normalizePixelSizes(
ox::Vector<uint8_t> 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<TileSheetEditorConfig>(
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<TileSheetEditorConfig>(keelCtx(m_sctx), configName(itemPath()),
[&path](TileSheetEditorConfig *config) {
config->activeSubsheet = std::move(path);
studio::editConfig<TileSheetEditorConfig>(keelCtx(m_sctx), itemPath(),
[&path](TileSheetEditorConfig &config) {
config.activeSubsheet = std::move(path);
});
}

View File

@ -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<StudioConfig>(keelCtx(m_ctx), [&](StudioConfig *config) {
config->activeTabItemName = m_activeEditor->itemPath();
studio::editConfig<StudioConfig>(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<StudioConfig>(keelCtx(m_ctx), [&](StudioConfig *config) {
config->showProjectExplorer = m_showProjectExplorer;
studio::editConfig<StudioConfig>(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<StudioConfig>(keelCtx(m_ctx), [&](StudioConfig *config) {
config->projectPath = ox::String(m_project->projectPath());
config->openFiles.clear();
studio::editConfig<StudioConfig>(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<StudioConfig>(keelCtx(m_ctx), [&](StudioConfig *config) {
if (!config->openFiles.contains(path)) {
config->openFiles.emplace_back(path);
studio::editConfig<StudioConfig>(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<StudioConfig>(keelCtx(m_ctx), [&](StudioConfig *config) {
std::ignore = config->openFiles.erase(std::remove(config->openFiles.begin(), config->openFiles.end(), path));
studio::editConfig<StudioConfig>(keelCtx(m_ctx), [&](StudioConfig &config) {
std::ignore = config.openFiles.erase(std::remove(config.openFiles.begin(), config.openFiles.end(), path));
});
return {};
}

View File

@ -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<typename T>
ox::Result<T> 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<T> readConfig(keel::Context &ctx) noexcept {
}
template<typename T>
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<typename T>
ox::Error writeConfig(keel::Context &ctx, T *data) noexcept {
ox::Error writeConfig(keel::Context &ctx, T const&data) noexcept {
constexpr auto TypeName = ox::requireModelTypeName<T>();
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<T>(ctx, name);
oxLogError(err);
f(&c);
f(c);
}
template<typename T, typename Func>
@ -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<T>(ctx, name);
oxLogError(err);
f(&c);
oxLogError(writeConfig(ctx, name, &c));
f(c);
oxLogError(writeConfig(ctx, name, c));
}
template<typename T, typename Func>