94 lines
2.7 KiB
C++
94 lines
2.7 KiB
C++
/*
|
|
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <ox/fs/fs.hpp>
|
|
#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>
|
|
|
|
#include <nostalgia/core/context.hpp>
|
|
|
|
namespace nostalgia::studio {
|
|
|
|
[[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 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>
|
|
ox::Result<T> readConfig(core::Context *ctx) noexcept {
|
|
constexpr auto TypeName = ox::requireModelTypeName<T>();
|
|
return readConfig<T>(ctx, TypeName);
|
|
}
|
|
|
|
template<typename T>
|
|
ox::Error writeConfig(core::Context *ctx, ox::CRStringView name, T *data) noexcept {
|
|
oxAssert(ox_strcmp(name, ""), "Config type has no TypeName");
|
|
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 (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");
|
|
}
|
|
return OxError(0);
|
|
}
|
|
|
|
template<typename T>
|
|
ox::Error writeConfig(core::Context *ctx, T *data) noexcept {
|
|
constexpr auto TypeName = ox::requireModelTypeName<T>();
|
|
return writeConfig(ctx, TypeName, data);
|
|
}
|
|
|
|
template<typename T, typename Func>
|
|
void openConfig(core::Context *ctx, const auto &name, Func f) noexcept {
|
|
oxAssert(name != "", "Config type has no TypeName");
|
|
const auto c = readConfig<T>(ctx, name);
|
|
f(&c.value);
|
|
}
|
|
|
|
template<typename T, typename Func>
|
|
void openConfig(core::Context *ctx, Func f) noexcept {
|
|
constexpr auto TypeName = ox::requireModelTypeName<T>();
|
|
openConfig<T>(ctx, TypeName, f);
|
|
}
|
|
|
|
template<typename T, typename Func>
|
|
void editConfig(core::Context *ctx, const auto &name, Func f) noexcept {
|
|
oxAssert(ox_strcmp(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 {
|
|
constexpr auto TypeName = ox::requireModelTypeName<T>();
|
|
editConfig<T>(ctx, TypeName, f);
|
|
}
|
|
|
|
}
|