[nostalgia] Cleanup config IO
This commit is contained in:
@@ -17,13 +17,25 @@
|
||||
|
||||
namespace studio {
|
||||
|
||||
namespace detail {
|
||||
inline ox::String slashesToPct(ox::StringView str) noexcept {
|
||||
auto out = ox::String{str};
|
||||
for (auto&c: out) {
|
||||
if (c == '/' || c == '\\') {
|
||||
c = '%';
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
ox::String configPath(keel::Context const&ctx) noexcept;
|
||||
|
||||
template<typename T>
|
||||
ox::Result<T> readConfig(keel::Context &ctx, ox::CRStringView name) noexcept {
|
||||
oxAssert(name != "", "Config type has no TypeName");
|
||||
auto const path = ox::sfmt("/{}.json", name);
|
||||
auto const path = ox::sfmt("/{}.json", detail::slashesToPct(name));
|
||||
ox::PassThroughFS fs(configPath(ctx));
|
||||
auto const [buff, err] = fs.read(path);
|
||||
if (err) {
|
||||
@@ -40,15 +52,15 @@ ox::Result<T> readConfig(keel::Context &ctx) noexcept {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ox::Error writeConfig(keel::Context &ctx, ox::CRStringView name, T *data) noexcept {
|
||||
ox::Error writeConfig(keel::Context &ctx, ox::CRStringView name, T const&data) noexcept {
|
||||
oxAssert(name != "", "Config type has no TypeName");
|
||||
auto const path = ox::sfmt("/{}.json", name);
|
||||
auto const path = ox::sfmt("/{}.json", detail::slashesToPct(name));
|
||||
ox::PassThroughFS fs(configPath(ctx));
|
||||
if (auto const err = fs.mkdir("/", true)) {
|
||||
oxErrf("Could not create config directory: {}\n", toStr(err));
|
||||
return err;
|
||||
}
|
||||
oxRequireM(buff, ox::writeOC(*data));
|
||||
oxRequireM(buff, ox::writeOC(data));
|
||||
*buff.back().value = '\n';
|
||||
if (auto const err = fs.write(path, buff.data(), buff.size())) {
|
||||
oxErrf("Could not read config file: {}\n", toStr(err));
|
||||
@@ -58,7 +70,7 @@ ox::Error writeConfig(keel::Context &ctx, ox::CRStringView name, T *data) noexce
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ox::Error writeConfig(keel::Context &ctx, T *data) noexcept {
|
||||
ox::Error writeConfig(keel::Context &ctx, T const&data) noexcept {
|
||||
constexpr auto TypeName = ox::requireModelTypeName<T>();
|
||||
return writeConfig(ctx, TypeName, data);
|
||||
}
|
||||
@@ -68,7 +80,7 @@ void openConfig(keel::Context &ctx, ox::CRStringView name, Func f) noexcept {
|
||||
oxAssert(name != "", "Config type has no TypeName");
|
||||
auto const [c, err] = readConfig<T>(ctx, name);
|
||||
oxLogError(err);
|
||||
f(&c);
|
||||
f(c);
|
||||
}
|
||||
|
||||
template<typename T, typename Func>
|
||||
@@ -82,8 +94,8 @@ void editConfig(keel::Context &ctx, ox::CRStringView name, Func f) noexcept {
|
||||
oxAssert(name != "", "Config type has no TypeName");
|
||||
auto [c, err] = readConfig<T>(ctx, name);
|
||||
oxLogError(err);
|
||||
f(&c);
|
||||
oxLogError(writeConfig(ctx, name, &c));
|
||||
f(c);
|
||||
oxLogError(writeConfig(ctx, name, c));
|
||||
}
|
||||
|
||||
template<typename T, typename Func>
|
||||
|
Reference in New Issue
Block a user