switch to INI for settings (easier), make settings a singleton thingy, slightly simplify core pause/stop/reset. Drop support for remappable inputs for now
This commit is contained in:
31
src/utils/Options.cpp
Normal file
31
src/utils/Options.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <Options.hpp>
|
||||
|
||||
template <>
|
||||
void Options::SetValue<std::string>(const std::string &key, const std::string &field, const std::string &value) {
|
||||
structure[key][field] = value;
|
||||
}
|
||||
|
||||
template <>
|
||||
void Options::SetValue<float>(const std::string &key, const std::string &field, const float &value) {
|
||||
structure[key][field] = fmt::format("{:.2f}", value);
|
||||
}
|
||||
|
||||
template <>
|
||||
void Options::SetValue<bool>(const std::string &key, const std::string &field, const bool &value) {
|
||||
structure[key][field] = value ? "true" : "false";
|
||||
}
|
||||
|
||||
template<>
|
||||
std::string Options::GetValue<std::string>(const std::string &key, const std::string &field) {
|
||||
return structure[key][field];
|
||||
}
|
||||
|
||||
template<>
|
||||
float Options::GetValue<float>(const std::string &key, const std::string &field) {
|
||||
return std::stof(structure[key][field]);
|
||||
}
|
||||
|
||||
template<>
|
||||
bool Options::GetValue<bool>(const std::string &key, const std::string &field) {
|
||||
return structure[key][field] == "true" ? true : false;
|
||||
}
|
||||
@@ -2,48 +2,44 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <common.hpp>
|
||||
#include <mini/ini.h>
|
||||
#include <log.hpp>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
static FORCE_INLINE mINI::INIStructure JSONOpenOrCreate(const std::string &path) {
|
||||
auto fileExists = fs::exists(path);
|
||||
struct Options {
|
||||
Options() : file{"resources/options.ini"} {
|
||||
auto fileExists = fs::exists("resources/options.ini");
|
||||
if(fileExists) {
|
||||
file.read(structure);
|
||||
return;
|
||||
}
|
||||
|
||||
mINI::INIStructure options;
|
||||
mINI::INIFile file(path);
|
||||
|
||||
if (fileExists) {
|
||||
file.read(options);
|
||||
return options;
|
||||
}
|
||||
structure["general"]["savePath"] = "";
|
||||
structure["audio"]["volumeL"] = "0.5";
|
||||
structure["audio"]["volumeR"] = "0.5";
|
||||
structure["audio"]["lock"] = "true";
|
||||
structure["cpu"]["type"] = "interpreter";
|
||||
|
||||
options["general"]["savePath"] = "";
|
||||
options["audio"]["volumeL"] = 0.5;
|
||||
options["audio"]["volumeR"] = 0.5;
|
||||
options["audio"]["lock"] = true;
|
||||
options["cpu"]["type"] = "interpreter";
|
||||
if(!file.generate(structure))
|
||||
Util::panic("Couldn't generate settings' INI!");
|
||||
}
|
||||
|
||||
file.write(options);
|
||||
static Options &GetInstance() {
|
||||
static Options instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
template <typename T>
|
||||
void SetValue(const std::string &key, const std::string &field, const T &value);
|
||||
template <typename T>
|
||||
T GetValue(const std::string &key, const std::string &field);
|
||||
|
||||
template <typename T>
|
||||
static FORCE_INLINE void JSONSetField(mINI::INIStructure &options, const std::string &key, const std::string &field,
|
||||
const T &value) {
|
||||
options[key][field] = value;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static FORCE_INLINE T JSONGetField(mINI::INIStructure &options, const std::string &key, const std::string &field) {
|
||||
return options[key][field];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static FORCE_INLINE void JSONSetField(mINI::INIStructure &options, const std::string &key, const T &value) {
|
||||
options[key] = value;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static FORCE_INLINE T JSONGetField(mINI::INIStructure &options, const std::string &key) {
|
||||
return options[key];
|
||||
}
|
||||
void Apply() {
|
||||
if(!file.write(structure))
|
||||
Util::panic("Could not modify options on disk!");
|
||||
}
|
||||
private:
|
||||
mINI::INIFile file;
|
||||
mINI::INIStructure structure;
|
||||
};
|
||||
Reference in New Issue
Block a user