2024-03-23 18:41:26 -07:00
|
|
|
#include "thirdparty/toml.hpp"
|
|
|
|
#include "util.hpp"
|
|
|
|
#include <filesystem>
|
2024-05-01 09:07:08 -07:00
|
|
|
#include <sstream>
|
2024-04-25 11:23:38 -07:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
#include <SDL.h>
|
|
|
|
#endif
|
2024-05-02 14:52:11 -07:00
|
|
|
#include <optional>
|
2024-05-01 09:07:08 -07:00
|
|
|
#include "web_functions.hpp"
|
2024-03-23 18:41:26 -07:00
|
|
|
using namespace std::filesystem;
|
2024-05-02 14:52:11 -07:00
|
|
|
|
2024-03-23 18:41:26 -07:00
|
|
|
namespace Looper::Options {
|
2024-05-02 14:52:11 -07:00
|
|
|
std::optional<const char *> options_path_override;
|
|
|
|
bool options_enabled() {
|
|
|
|
return !(options_path_override.has_value() && options_path_override.value() == nullptr);
|
|
|
|
};
|
2024-03-23 18:41:26 -07:00
|
|
|
toml::table *options;
|
|
|
|
toml::table opts_value;
|
|
|
|
std::string get_options_path() {
|
2024-05-02 14:52:11 -07:00
|
|
|
if (options_path_override.has_value()) {
|
|
|
|
if (options_path_override.value() == nullptr) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return options_path_override.value();
|
|
|
|
}
|
|
|
|
}
|
2024-05-01 09:07:08 -07:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
return "config_toml";
|
|
|
|
#else
|
2024-03-23 18:41:26 -07:00
|
|
|
path prefs_path(get_prefs_path());
|
|
|
|
prefs_path /= path("looper");
|
|
|
|
create_directories(prefs_path);
|
|
|
|
path config_file = prefs_path / path("config.toml");
|
|
|
|
return config_file.string();
|
2024-05-01 09:07:08 -07:00
|
|
|
#endif
|
2024-03-23 18:41:26 -07:00
|
|
|
}
|
|
|
|
void load_options() {
|
2024-05-02 14:52:11 -07:00
|
|
|
if (!options_enabled()) {
|
|
|
|
options = new toml::table();
|
|
|
|
return;
|
|
|
|
}
|
2024-03-23 18:41:26 -07:00
|
|
|
std::string config_path = get_options_path();
|
2024-05-01 09:07:08 -07:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
const char *value = nullptr;
|
|
|
|
read_storage(config_path.c_str(), &value, nullptr);
|
|
|
|
if (value != nullptr) {
|
|
|
|
auto res = toml::parse(value);
|
|
|
|
opts_value = res;
|
|
|
|
options = &opts_value;
|
|
|
|
if (options == nullptr) {
|
|
|
|
options = new toml::table();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
options = new toml::table();
|
|
|
|
}
|
|
|
|
#else
|
2024-03-23 18:41:26 -07:00
|
|
|
if (exists(config_path)) {
|
|
|
|
auto res = toml::parse_file(config_path);
|
|
|
|
opts_value = res;
|
|
|
|
options = &opts_value;
|
|
|
|
if (options == nullptr) {
|
|
|
|
options = new toml::table();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
options = new toml::table();
|
|
|
|
}
|
2024-05-01 09:07:08 -07:00
|
|
|
#endif
|
2024-03-23 18:41:26 -07:00
|
|
|
}
|
|
|
|
void save_options() {
|
2024-05-02 14:52:11 -07:00
|
|
|
if (!options_enabled()) {
|
|
|
|
return;
|
|
|
|
}
|
2024-05-01 09:07:08 -07:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
std::ostringstream output;
|
|
|
|
#else
|
2024-03-23 18:41:26 -07:00
|
|
|
std::ofstream output(get_options_path());
|
2024-05-01 09:07:08 -07:00
|
|
|
#endif
|
2024-03-23 18:41:26 -07:00
|
|
|
output << *options;
|
2024-05-01 09:07:08 -07:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
std::string str = output.str();
|
|
|
|
write_storage(get_options_path().c_str(), str.c_str(), str.length() + 1);
|
|
|
|
#else
|
2024-03-23 18:41:26 -07:00
|
|
|
output.close();
|
2024-05-01 09:07:08 -07:00
|
|
|
#endif
|
2024-03-23 18:41:26 -07:00
|
|
|
}
|
2024-12-08 09:55:27 -08:00
|
|
|
}
|