diff --git a/src/nostalgia/studio/lib/configio.cpp b/src/nostalgia/studio/lib/configio.cpp index b91aab8a..a64b70dd 100644 --- a/src/nostalgia/studio/lib/configio.cpp +++ b/src/nostalgia/studio/lib/configio.cpp @@ -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); +} +} diff --git a/src/nostalgia/studio/lib/configio.hpp b/src/nostalgia/studio/lib/configio.hpp index e836c30d..e28ab408 100644 --- a/src/nostalgia/studio/lib/configio.hpp +++ b/src/nostalgia/studio/lib/configio.hpp @@ -4,9 +4,6 @@ #pragma once -#include -#include - #include #include #include @@ -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 ox::Result 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(size)); - file.read(buff.data(), size); - return ox::readOC(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(buff); } template @@ -67,19 +40,17 @@ ox::Result readConfig(core::Context *ctx) noexcept { } template -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"); } diff --git a/src/nostalgia/studio/lib/editor.cpp b/src/nostalgia/studio/lib/editor.cpp index afba887e..b9160005 100644 --- a/src/nostalgia/studio/lib/editor.cpp +++ b/src/nostalgia/studio/lib/editor.cpp @@ -29,7 +29,7 @@ void BaseEditor::exportFile() { void BaseEditor::keyStateChanged(core::Key, bool) { } -void BaseEditor::close() { +void BaseEditor::close() const { this->closed.emit(itemName()); } diff --git a/src/nostalgia/studio/lib/editor.hpp b/src/nostalgia/studio/lib/editor.hpp index 85cb0ea0..5d350d68 100644 --- a/src/nostalgia/studio/lib/editor.hpp +++ b/src/nostalgia/studio/lib/editor.hpp @@ -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. diff --git a/src/nostalgia/studio/studioapp.cpp b/src/nostalgia/studio/studioapp.cpp index ba64310d..76b1831b 100644 --- a/src/nostalgia/studio/studioapp.cpp +++ b/src/nostalgia/studio/studioapp.cpp @@ -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(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(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(path, std::move(obj)); } else { const auto err = m_editorMakers[ext](path).moveTo(&editor); if (err) { diff --git a/src/nostalgia/studio/studioapp.hpp b/src/nostalgia/studio/studioapp.hpp index b5c79f7d..9bc45922 100644 --- a/src/nostalgia/studio/studioapp.hpp +++ b/src/nostalgia/studio/studioapp.hpp @@ -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;