[nostalgia/studio] Cleanup
This commit is contained in:
parent
fad8837ad1
commit
fea6a0764c
@ -4,6 +4,28 @@
|
|||||||
|
|
||||||
#include "configio.hpp"
|
#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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -4,9 +4,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <filesystem>
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
#include <ox/fs/fs.hpp>
|
#include <ox/fs/fs.hpp>
|
||||||
#include <ox/model/typenamecatcher.hpp>
|
#include <ox/model/typenamecatcher.hpp>
|
||||||
#include <ox/oc/oc.hpp>
|
#include <ox/oc/oc.hpp>
|
||||||
@ -20,44 +17,20 @@
|
|||||||
|
|
||||||
namespace nostalgia::studio {
|
namespace nostalgia::studio {
|
||||||
|
|
||||||
constexpr auto ConfigDir = [] {
|
[[nodiscard]]
|
||||||
switch (ox::defines::OS) {
|
ox::String configPath(const core::Context *ctx) noexcept;
|
||||||
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 "";
|
|
||||||
}
|
|
||||||
}();
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ox::Result<T> readConfig(core::Context *ctx, ox::CRStringView name) noexcept {
|
ox::Result<T> readConfig(core::Context *ctx, ox::CRStringView name) noexcept {
|
||||||
oxAssert(ox_strcmp(name, ""), "Config type has no TypeName");
|
oxAssert(ox_strcmp(name, ""), "Config type has no TypeName");
|
||||||
const auto homeDir = std::getenv(ox::defines::OS == ox::OS::Windows ? "USERPROFILE" : "HOME");
|
const auto path = ox::sfmt("/{}.json", name);
|
||||||
const auto configPath = ox::sfmt(ConfigDir, homeDir, ctx->appName).toStdString();
|
ox::PassThroughFS fs(configPath(ctx));
|
||||||
const auto path = ox::sfmt("{}/{}.json", configPath, name).toStdString();
|
const auto [buff, err] = fs.read(path);
|
||||||
std::ifstream file(path, std::ios::binary | std::ios::ate);
|
if (err) {
|
||||||
if (!file.good()) {
|
oxErrf("Could not read config file: {}\n", toStr(err));
|
||||||
oxErrf("Could not find config file: {}\n", path);
|
return err;
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
return ox::readOC<T>(buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -67,19 +40,17 @@ ox::Result<T> readConfig(core::Context *ctx) noexcept {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
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");
|
oxAssert(ox_strcmp(name, ""), "Config type has no TypeName");
|
||||||
const auto homeDir = std::getenv(ox::defines::OS == ox::OS::Windows ? "USERPROFILE" : "HOME");
|
const auto path = ox::sfmt("/{}.json", name);
|
||||||
const auto configPath = ox::sfmt(ConfigDir, homeDir, ctx->appName);
|
ox::PassThroughFS fs(configPath(ctx));
|
||||||
const auto path = ox::sfmt("{}.json", name);
|
if (const auto err = fs.mkdir("/", true)) {
|
||||||
ox::PassThroughFS fs(configPath);
|
|
||||||
if (auto err = fs.mkdir("/", true)) {
|
|
||||||
oxErrf("Could not create config directory: {}\n", toStr(err));
|
oxErrf("Could not create config directory: {}\n", toStr(err));
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
oxRequireM(buff, ox::writeOC(data));
|
oxRequireM(buff, ox::writeOC(data));
|
||||||
buff.back().value = '\n';
|
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));
|
oxErrf("Could not read config file: {}\n", toStr(err));
|
||||||
return OxError(2, "Could not read config file");
|
return OxError(2, "Could not read config file");
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ void BaseEditor::exportFile() {
|
|||||||
void BaseEditor::keyStateChanged(core::Key, bool) {
|
void BaseEditor::keyStateChanged(core::Key, bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseEditor::close() {
|
void BaseEditor::close() const {
|
||||||
this->closed.emit(itemName());
|
this->closed.emit(itemName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ class NOSTALGIASTUDIO_EXPORT BaseEditor: public Widget {
|
|||||||
|
|
||||||
virtual void keyStateChanged(core::Key key, bool down);
|
virtual void keyStateChanged(core::Key key, bool down);
|
||||||
|
|
||||||
void close();
|
void close() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save changes to item being edited.
|
* Save changes to item being edited.
|
||||||
|
@ -82,7 +82,7 @@ void StudioUI::handleKeyEvent(core::Key key, bool down) noexcept {
|
|||||||
m_newMenu.open();
|
m_newMenu.open();
|
||||||
break;
|
break;
|
||||||
case core::Key::Alpha_O:
|
case core::Key::Alpha_O:
|
||||||
m_taskRunner.add(new FileDialogManager(this, &StudioUI::openProject));
|
m_taskRunner.add(ox::make<FileDialogManager>(this, &StudioUI::openProject));
|
||||||
break;
|
break;
|
||||||
case core::Key::Alpha_Q:
|
case core::Key::Alpha_Q:
|
||||||
core::shutdown(m_ctx);
|
core::shutdown(m_ctx);
|
||||||
@ -134,7 +134,7 @@ void StudioUI::drawMenu() noexcept {
|
|||||||
m_newMenu.open();
|
m_newMenu.open();
|
||||||
}
|
}
|
||||||
if (ImGui::MenuItem("Open Project...", "Ctrl+O")) {
|
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())) {
|
if (ImGui::MenuItem("Save", "Ctrl+S", false, m_activeEditor && m_activeEditor->unsavedChanges())) {
|
||||||
m_activeEditor->save();
|
m_activeEditor->save();
|
||||||
@ -240,19 +240,19 @@ void StudioUI::loadEditorMaker(const studio::EditorMaker &editorMaker) noexcept
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void StudioUI::loadModule(studio::Module *module) noexcept {
|
void StudioUI::loadModule(studio::Module *mod) noexcept {
|
||||||
for (auto &editorMaker : module->editors(m_ctx)) {
|
for (const auto &editorMaker : mod->editors(m_ctx)) {
|
||||||
loadEditorMaker(editorMaker);
|
loadEditorMaker(editorMaker);
|
||||||
}
|
}
|
||||||
for (auto &im : module->itemMakers(m_ctx)) {
|
for (auto &im : mod->itemMakers(m_ctx)) {
|
||||||
m_newMenu.addItemMaker(std::move(im));
|
m_newMenu.addItemMaker(std::move(im));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void StudioUI::loadModules() noexcept {
|
void StudioUI::loadModules() noexcept {
|
||||||
for (auto &moduleMaker : BuiltinModules) {
|
for (auto &moduleMaker : BuiltinModules) {
|
||||||
const auto module = moduleMaker();
|
const auto mod = moduleMaker();
|
||||||
loadModule(module.get());
|
loadModule(mod.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -324,7 +324,7 @@ ox::Error StudioUI::openFileActiveTab(ox::CRStringView path, bool makeActiveTab)
|
|||||||
if (err) {
|
if (err) {
|
||||||
return OxError(1, "There is no editor for this file extension");
|
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 {
|
} else {
|
||||||
const auto err = m_editorMakers[ext](path).moveTo(&editor);
|
const auto err = m_editorMakers[ext](path).moveTo(&editor);
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -65,7 +65,7 @@ class StudioUI: public ox::SignalHandler {
|
|||||||
|
|
||||||
void loadEditorMaker(const studio::EditorMaker &editorMaker) noexcept;
|
void loadEditorMaker(const studio::EditorMaker &editorMaker) noexcept;
|
||||||
|
|
||||||
void loadModule(studio::Module *module) noexcept;
|
void loadModule(studio::Module *mod) noexcept;
|
||||||
|
|
||||||
void loadModules() noexcept;
|
void loadModules() noexcept;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user