[nostalgia/studio] Cleanup

This commit is contained in:
Gary Talent 2023-02-01 22:42:07 -06:00
parent fad8837ad1
commit fea6a0764c
6 changed files with 47 additions and 54 deletions

View File

@ -4,6 +4,28 @@
#include "configio.hpp"
namespace nostalgia::studio {
constexpr auto ConfigDir = [] {
switch (ox::defines::OS) {
case ox::OS::Darwin:
return "{}/Library/Preferences/{}";
case ox::OS::DragonFlyBSD:
case ox::OS::FreeBSD:
case ox::OS::Linux:
case ox::OS::NetBSD:
case ox::OS::OpenBSD:
return "{}/.config/{}";
case ox::OS::Windows:
return R"({}/AppData/Local/{})";
case ox::OS::BareMetal:
return "";
}
}();
ox::String configPath(const core::Context *ctx) noexcept {
const auto homeDir = std::getenv(ox::defines::OS == ox::OS::Windows ? "USERPROFILE" : "HOME");
return ox::sfmt(ConfigDir, homeDir, ctx->appName);
}
}

View File

@ -4,9 +4,6 @@
#pragma once
#include <filesystem>
#include <fstream>
#include <ox/fs/fs.hpp>
#include <ox/model/typenamecatcher.hpp>
#include <ox/oc/oc.hpp>
@ -20,44 +17,20 @@
namespace nostalgia::studio {
constexpr auto ConfigDir = [] {
switch (ox::defines::OS) {
case ox::OS::Darwin:
return "{}/Library/Preferences/{}";
case ox::OS::DragonFlyBSD:
case ox::OS::FreeBSD:
case ox::OS::Linux:
case ox::OS::NetBSD:
case ox::OS::OpenBSD:
return "{}/.config/{}";
case ox::OS::Windows:
return R"({}/AppData/Local/{})";
case ox::OS::BareMetal:
return "";
}
}();
[[nodiscard]]
ox::String configPath(const core::Context *ctx) noexcept;
template<typename T>
ox::Result<T> readConfig(core::Context *ctx, ox::CRStringView name) noexcept {
oxAssert(ox_strcmp(name, ""), "Config type has no TypeName");
const auto homeDir = std::getenv(ox::defines::OS == ox::OS::Windows ? "USERPROFILE" : "HOME");
const auto configPath = ox::sfmt(ConfigDir, homeDir, ctx->appName).toStdString();
const auto path = ox::sfmt("{}/{}.json", configPath, name).toStdString();
std::ifstream file(path, std::ios::binary | std::ios::ate);
if (!file.good()) {
oxErrf("Could not find config file: {}\n", path);
return OxError(1, "Could not find config file");
}
try {
const auto size = file.tellg();
file.seekg(0, std::ios::beg);
ox::Buffer buff(static_cast<std::size_t>(size));
file.read(buff.data(), size);
return ox::readOC<T>(buff);
} catch (const std::ios_base::failure &e) {
oxErrf("Could not read config file: {}\n", e.what());
return OxError(2, "Could not read config file");
const auto path = ox::sfmt("/{}.json", name);
ox::PassThroughFS fs(configPath(ctx));
const auto [buff, err] = fs.read(path);
if (err) {
oxErrf("Could not read config file: {}\n", toStr(err));
return err;
}
return ox::readOC<T>(buff);
}
template<typename T>
@ -67,19 +40,17 @@ ox::Result<T> readConfig(core::Context *ctx) noexcept {
}
template<typename T>
ox::Error writeConfig(core::Context *ctx, const auto &name, T *data) noexcept {
ox::Error writeConfig(core::Context *ctx, ox::CRStringView name, T *data) noexcept {
oxAssert(ox_strcmp(name, ""), "Config type has no TypeName");
const auto homeDir = std::getenv(ox::defines::OS == ox::OS::Windows ? "USERPROFILE" : "HOME");
const auto configPath = ox::sfmt(ConfigDir, homeDir, ctx->appName);
const auto path = ox::sfmt("{}.json", name);
ox::PassThroughFS fs(configPath);
if (auto err = fs.mkdir("/", true)) {
const auto path = ox::sfmt("/{}.json", name);
ox::PassThroughFS fs(configPath(ctx));
if (const auto err = fs.mkdir("/", true)) {
oxErrf("Could not create config directory: {}\n", toStr(err));
return err;
}
oxRequireM(buff, ox::writeOC(data));
buff.back().value = '\n';
if (auto err = fs.write(path, buff.data(), buff.size())) {
if (const auto err = fs.write(path, buff.data(), buff.size())) {
oxErrf("Could not read config file: {}\n", toStr(err));
return OxError(2, "Could not read config file");
}

View File

@ -29,7 +29,7 @@ void BaseEditor::exportFile() {
void BaseEditor::keyStateChanged(core::Key, bool) {
}
void BaseEditor::close() {
void BaseEditor::close() const {
this->closed.emit(itemName());
}

View File

@ -50,7 +50,7 @@ class NOSTALGIASTUDIO_EXPORT BaseEditor: public Widget {
virtual void keyStateChanged(core::Key key, bool down);
void close();
void close() const;
/**
* Save changes to item being edited.

View File

@ -82,7 +82,7 @@ void StudioUI::handleKeyEvent(core::Key key, bool down) noexcept {
m_newMenu.open();
break;
case core::Key::Alpha_O:
m_taskRunner.add(new FileDialogManager(this, &StudioUI::openProject));
m_taskRunner.add(ox::make<FileDialogManager>(this, &StudioUI::openProject));
break;
case core::Key::Alpha_Q:
core::shutdown(m_ctx);
@ -134,7 +134,7 @@ void StudioUI::drawMenu() noexcept {
m_newMenu.open();
}
if (ImGui::MenuItem("Open Project...", "Ctrl+O")) {
m_taskRunner.add(new FileDialogManager(this, &StudioUI::openProject));
m_taskRunner.add(ox::make<FileDialogManager>(this, &StudioUI::openProject));
}
if (ImGui::MenuItem("Save", "Ctrl+S", false, m_activeEditor && m_activeEditor->unsavedChanges())) {
m_activeEditor->save();
@ -240,19 +240,19 @@ void StudioUI::loadEditorMaker(const studio::EditorMaker &editorMaker) noexcept
}
}
void StudioUI::loadModule(studio::Module *module) noexcept {
for (auto &editorMaker : module->editors(m_ctx)) {
void StudioUI::loadModule(studio::Module *mod) noexcept {
for (const auto &editorMaker : mod->editors(m_ctx)) {
loadEditorMaker(editorMaker);
}
for (auto &im : module->itemMakers(m_ctx)) {
for (auto &im : mod->itemMakers(m_ctx)) {
m_newMenu.addItemMaker(std::move(im));
}
}
void StudioUI::loadModules() noexcept {
for (auto &moduleMaker : BuiltinModules) {
const auto module = moduleMaker();
loadModule(module.get());
const auto mod = moduleMaker();
loadModule(mod.get());
}
}
@ -324,7 +324,7 @@ ox::Error StudioUI::openFileActiveTab(ox::CRStringView path, bool makeActiveTab)
if (err) {
return OxError(1, "There is no editor for this file extension");
}
editor = new ClawEditor(path, std::move(obj));
editor = ox::make<ClawEditor>(path, std::move(obj));
} else {
const auto err = m_editorMakers[ext](path).moveTo(&editor);
if (err) {

View File

@ -65,7 +65,7 @@ class StudioUI: public ox::SignalHandler {
void loadEditorMaker(const studio::EditorMaker &editorMaker) noexcept;
void loadModule(studio::Module *module) noexcept;
void loadModule(studio::Module *mod) noexcept;
void loadModules() noexcept;