[nostalgia/studio] Fix config path for Windows
This commit is contained in:
parent
dac33e63fd
commit
db3e9c5d93
@ -7,6 +7,7 @@
|
|||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
|
#include <ox/fs/fs.hpp>
|
||||||
#include <ox/model/typenamecatcher.hpp>
|
#include <ox/model/typenamecatcher.hpp>
|
||||||
#include <ox/oc/oc.hpp>
|
#include <ox/oc/oc.hpp>
|
||||||
#include <ox/std/buffer.hpp>
|
#include <ox/std/buffer.hpp>
|
||||||
@ -29,8 +30,9 @@ constexpr auto ConfigDir = [] {
|
|||||||
case ox::OS::NetBSD:
|
case ox::OS::NetBSD:
|
||||||
case ox::OS::OpenBSD:
|
case ox::OS::OpenBSD:
|
||||||
return "{}/.config/{}";
|
return "{}/.config/{}";
|
||||||
case ox::OS::BareMetal:
|
|
||||||
case ox::OS::Windows:
|
case ox::OS::Windows:
|
||||||
|
return "{}/AppData/Local/{}";
|
||||||
|
case ox::OS::BareMetal:
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}();
|
}();
|
||||||
@ -38,7 +40,7 @@ constexpr auto ConfigDir = [] {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
ox::Result<T> readConfig(core::Context *ctx, const char *name) noexcept {
|
ox::Result<T> readConfig(core::Context *ctx, const char *name) noexcept {
|
||||||
oxAssert(ox_strcmp(name, ""), "Config type has no TypeName");
|
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 configPath = ox::sfmt(ConfigDir, homeDir, ctx->appName).toStdString();
|
||||||
const auto path = ox::sfmt("{}/{}.json", configPath, name).toStdString();
|
const auto path = ox::sfmt("{}/{}.json", configPath, name).toStdString();
|
||||||
std::ifstream file(path, std::ios::binary | std::ios::ate);
|
std::ifstream file(path, std::ios::binary | std::ios::ate);
|
||||||
@ -67,28 +69,21 @@ ox::Result<T> readConfig(core::Context *ctx) noexcept {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
ox::Error writeConfig(core::Context *ctx, const auto &name, T *data) noexcept {
|
ox::Error writeConfig(core::Context *ctx, const auto &name, T *data) noexcept {
|
||||||
oxAssert(ox_strcmp(name, ""), "Config type has no TypeName");
|
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 configPath = ox::sfmt(ConfigDir, homeDir, ctx->appName).toStdString();
|
||||||
const auto path = ox::sfmt("{}/{}.json", configPath, name).toStdString();
|
const auto path = ox::sfmt("{}.json", name).toStdString();
|
||||||
std::error_code ec;
|
ox::PassThroughFS fs(configPath.c_str());
|
||||||
std::filesystem::create_directory(configPath, ec);
|
if (auto err = fs.mkdir(configPath.c_str(), true)) {
|
||||||
if (ec) {
|
oxErrf("Could not create config directory: {}\n", toStr(err));
|
||||||
oxErrf("Could not create config directory: {}\n", ec.message());
|
return err;
|
||||||
}
|
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
oxRequireM(buff, ox::writeOC(data));
|
oxRequireM(buff, ox::writeOC(data));
|
||||||
buff.back().value = '\n';
|
buff.back().value = '\n';
|
||||||
try {
|
if (auto err = fs.write(path.c_str(), buff.data(), buff.size())) {
|
||||||
file.write(buff.data(), static_cast<ox::Signed<decltype(buff.size())>>(buff.size()));
|
oxErrf("Could not read config file: {}\n", toStr(err));
|
||||||
return OxError(0);
|
|
||||||
} catch (const std::ios_base::failure &e) {
|
|
||||||
oxErrf("Could not read config file: {}\n", e.what());
|
|
||||||
return OxError(2, "Could not read config file");
|
return OxError(2, "Could not read config file");
|
||||||
}
|
}
|
||||||
|
return OxError(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
Loading…
Reference in New Issue
Block a user