[nostalgia] Update Studio to handle tabs and open directory dialog on Mac, Update core::init

This commit is contained in:
2021-10-31 13:31:12 -05:00
parent e29f65f351
commit ad743565b2
28 changed files with 416 additions and 155 deletions
+37 -17
View File
@@ -19,18 +19,20 @@
#include <ox/std/trace.hpp>
#include <ox/std/string.hpp>
#include <nostalgia/core/context.hpp>
namespace nostalgia::studio {
constexpr auto ConfigDir = [] {
switch (ox::defines::OS) {
case ox::defines::OS::Darwin:
return "{}/Library/Preferences/NostalgiaStudio";
return "{}/Library/Preferences/{}";
case ox::defines::OS::DragonFlyBSD:
case ox::defines::OS::FreeBSD:
case ox::defines::OS::Linux:
case ox::defines::OS::NetBSD:
case ox::defines::OS::OpenBSD:
return "{}/.config/NostalgiaStudio";
return "{}/.config/{}";
case ox::defines::OS::BareMetal:
case ox::defines::OS::Windows:
return "";
@@ -38,13 +40,14 @@ constexpr auto ConfigDir = [] {
}();
template<typename T>
ox::Result<T> readConfig(const ox::String &name = ox::getModelTypeName<T>()) noexcept {
ox::Result<T> readConfig(core::Context *ctx, const ox::String &name = ox::getModelTypeName<T>()) noexcept {
oxAssert(name != "", "Config type has no TypeName");
const auto homeDir = std::getenv("HOME");
const auto configPath = ox::sfmt(ConfigDir, homeDir).toStdString();
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()) {
oxErrorf("Could not find config file: {}", path);
oxErrf("Could not find config file: {}\n", path);
return OxError(1, "Could not find config file");
}
try {
@@ -54,15 +57,16 @@ ox::Result<T> readConfig(const ox::String &name = ox::getModelTypeName<T>()) noe
file.read(buff.data(), size);
return ox::readOC<T>(buff);
} catch (const std::ios_base::failure &e) {
oxErrorf("Could not read config file: {}", e.what());
oxErrf("Could not read config file: {}\n", e.what());
return OxError(2, "Could not read config file");
}
}
template<typename T>
ox::Error writeConfig(const ox::String &name, T *data) noexcept {
ox::Error writeConfig(core::Context *ctx, const ox::String &name, T *data) noexcept {
oxAssert(name != "", "Config type has no TypeName");
const auto homeDir = std::getenv("HOME");
const auto configPath = ox::sfmt(ConfigDir, homeDir).toStdString();
const auto configPath = ox::sfmt(ConfigDir, homeDir, ctx->appName).toStdString();
const auto path = ox::sfmt("{}/{}.json", configPath, name).toStdString();
std::error_code ec;
std::filesystem::create_directory(configPath, ec);
@@ -71,7 +75,7 @@ ox::Error writeConfig(const ox::String &name, T *data) noexcept {
}
std::ofstream file(path, std::ios::binary | std::ios::ate);
if (!file.good()) {
oxErrorf("Could not find config file: {}", path);
oxErrf("Could not find config file: {}\n", path);
return OxError(1, "Could not find config file");
}
oxRequireM(buff, ox::writeOC(data));
@@ -80,26 +84,42 @@ ox::Error writeConfig(const ox::String &name, T *data) noexcept {
file.write(buff.data(), static_cast<ox::Signed<decltype(buff.size())>>(buff.size()));
return OxError(0);
} catch (const std::ios_base::failure &e) {
oxErrorf("Could not read config file: {}", e.what());
oxErrf("Could not read config file: {}\n", e.what());
return OxError(2, "Could not read config file");
}
}
template<typename T>
ox::Error writeConfig(T *data) noexcept {
return writeConfig(ox::getModelTypeName<T>(), data);
ox::Error writeConfig(core::Context *ctx, T *data) noexcept {
const auto TypeName = ox::getModelTypeName<T>();
return writeConfig(ctx, TypeName, data);
}
template<typename T, typename Func>
ox::Error editConfig(const ox::String &name, Func f) noexcept {
auto c = readConfig<T>(name);
void openConfig(core::Context *ctx, const ox::String &name, Func f) noexcept {
oxAssert(name != "", "Config type has no TypeName");
const auto c = readConfig<T>(ctx, name);
f(&c.value);
return writeConfig(name, &c.value);
}
template<typename T, typename Func>
ox::Error editConfig(Func f) noexcept {
return editConfig<T>(ox::getModelTypeName<T>(), f);
void openConfig(core::Context *ctx, Func f) noexcept {
const auto TypeName = ox::getModelTypeName<T>();
openConfig<T>(ctx, TypeName, f);
}
template<typename T, typename Func>
void editConfig(core::Context *ctx, const ox::String &name, Func f) noexcept {
oxAssert(name != "", "Config type has no TypeName");
auto c = readConfig<T>(ctx, name);
f(&c.value);
oxLogError(writeConfig(ctx, name, &c.value));
}
template<typename T, typename Func>
void editConfig(core::Context *ctx, Func f) noexcept {
const auto TypeName = ox::getModelTypeName<T>();
editConfig<T>(ctx, TypeName, f);
}
}