From db3e9c5d93cbc351fdb95de12e9631cab63f4040 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 12 Jul 2022 02:07:43 -0500 Subject: [PATCH] [nostalgia/studio] Fix config path for Windows --- src/nostalgia/studio/lib/configio.hpp | 31 +++++++++++---------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/nostalgia/studio/lib/configio.hpp b/src/nostalgia/studio/lib/configio.hpp index 53774b15..8931f0bd 100644 --- a/src/nostalgia/studio/lib/configio.hpp +++ b/src/nostalgia/studio/lib/configio.hpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -29,8 +30,9 @@ constexpr auto ConfigDir = [] { case ox::OS::NetBSD: case ox::OS::OpenBSD: return "{}/.config/{}"; - case ox::OS::BareMetal: case ox::OS::Windows: + return "{}/AppData/Local/{}"; + case ox::OS::BareMetal: return ""; } }(); @@ -38,7 +40,7 @@ constexpr auto ConfigDir = [] { template ox::Result readConfig(core::Context *ctx, const char *name) noexcept { oxAssert(ox_strcmp(name, ""), "Config type has no TypeName"); - const auto homeDir = std::getenv("HOME"); + const auto homeDir = std::getenv(ox::defines::OS == ox::OS::Windows ? "USERPROFILE" : "HOME"); 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); @@ -67,28 +69,21 @@ ox::Result readConfig(core::Context *ctx) noexcept { template ox::Error writeConfig(core::Context *ctx, const auto &name, T *data) noexcept { oxAssert(ox_strcmp(name, ""), "Config type has no TypeName"); - const auto homeDir = std::getenv("HOME"); + const auto homeDir = std::getenv(ox::defines::OS == ox::OS::Windows ? "USERPROFILE" : "HOME"); 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); - if (ec) { - oxErrf("Could not create config directory: {}\n", ec.message()); - } - std::ofstream file(path, std::ios::binary | std::ios::ate); - if (!file.good()) { - oxErrf("Could not find config file: {}\n", path); - return OxError(1, "Could not find config file"); + const auto path = ox::sfmt("{}.json", name).toStdString(); + ox::PassThroughFS fs(configPath.c_str()); + if (auto err = fs.mkdir(configPath.c_str(), true)) { + oxErrf("Could not create config directory: {}\n", toStr(err)); + return err; } oxRequireM(buff, ox::writeOC(data)); buff.back().value = '\n'; - try { - file.write(buff.data(), static_cast>(buff.size())); - return OxError(0); - } catch (const std::ios_base::failure &e) { - oxErrf("Could not read config file: {}\n", e.what()); + if (auto err = fs.write(path.c_str(), 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