[nostalgia/studio] Start on ImGui version of Studio

This commit is contained in:
2021-07-26 01:39:56 -05:00
parent 6160335af3
commit ddd63bc45f
46 changed files with 1005 additions and 2269 deletions
+105
View File
@@ -0,0 +1,105 @@
/*
* Copyright 2016 - 2021 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <filesystem>
#include <fstream>
#include <ox/model/typenamecatcher.hpp>
#include <ox/oc/oc.hpp>
#include <ox/std/buffer.hpp>
#include <ox/std/defines.hpp>
#include <ox/std/fmt.hpp>
#include <ox/std/trace.hpp>
#include <ox/std/string.hpp>
namespace nostalgia::studio {
constexpr auto ConfigDir = [] {
switch (ox::defines::OS) {
case ox::defines::OS::Darwin:
return "{}/Library/Preferences/NostalgiaStudio";
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";
case ox::defines::OS::BareMetal:
case ox::defines::OS::Windows:
return "";
}
}();
template<typename T>
ox::Result<T> readConfig(const ox::String &name = ox::getModelTypeName<T>()) noexcept {
const auto homeDir = std::getenv("HOME");
const auto configPath = ox::sfmt(ConfigDir, homeDir).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);
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) {
oxErrorf("Could not read config file: {}", e.what());
return OxError(2, "Could not read config file");
}
}
template<typename T>
ox::Error writeConfig(const ox::String &name, T *data) noexcept {
const auto homeDir = std::getenv("HOME");
const auto configPath = ox::sfmt(ConfigDir, homeDir).toStdString();
const auto path = ox::sfmt("{}/{}.json", configPath, name).toStdString();
std::error_code ec;
std::filesystem::create_directory(configPath, ec);
if (ec) {
oxErrf("Could not create config directory: {}\n", ec.message());
}
std::ofstream file(path, std::ios::binary | std::ios::ate);
if (!file.good()) {
oxErrorf("Could not find config file: {}", path);
return OxError(1, "Could not find config file");
}
oxRequireM(buff, ox::writeOC(data));
buff.back().value = '\n';
try {
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());
return OxError(2, "Could not read config file");
}
}
template<typename T>
ox::Error writeConfig(T *data) noexcept {
return writeConfig(ox::getModelTypeName<T>(), data);
}
template<typename T, typename Func>
ox::Error editConfig(const ox::String &name, Func f) noexcept {
auto c = readConfig<T>(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);
}
}