Use Qt ini file implementation.

This commit is contained in:
BearOso 2023-06-28 13:59:37 -05:00
parent 4000503584
commit 4aaf009dbb
2 changed files with 29 additions and 11931 deletions

View File

@ -1,15 +1,13 @@
#include <cstdio> #include <cstdio>
#include <string> #include <string>
#define TOML_LARGE_FILES 1
#define TOML_IMPLEMENTATION 1
#include <fstream> #include <fstream>
#include "toml.hpp"
#include <filesystem> #include <filesystem>
namespace fs = std::filesystem; namespace fs = std::filesystem;
#include "EmuConfig.hpp" #include "EmuConfig.hpp"
#include "EmuBinding.hpp" #include "EmuBinding.hpp"
#include <functional> #include <functional>
#include <QSettings>
static const char *shortcut_names[] = static const char *shortcut_names[] =
{ {
@ -307,9 +305,7 @@ void EmuConfig::setDefaults(int section)
void EmuConfig::config(std::string filename, bool write) void EmuConfig::config(std::string filename, bool write)
{ {
toml::table root; QSettings settings(QString::fromStdString(filename), QSettings::IniFormat);
toml::table *table = nullptr;
std::string section;
std::function<void(std::string, bool &)> Bool; std::function<void(std::string, bool &)> Bool;
std::function<void(std::string, int &)> Int; std::function<void(std::string, int &)> Int;
@ -323,76 +319,63 @@ void EmuConfig::config(std::string filename, bool write)
if (write) if (write)
{ {
Bool = [&](std::string key, bool &value) { Bool = [&](std::string key, bool &value) {
table->insert_or_assign(key, value); settings.setValue(key, value);
}; };
Int = [&](std::string key, int &value) { Int = [&](std::string key, int &value) {
table->insert_or_assign(key, value); settings.setValue(key, value);
}; };
String = [&](std::string key, std::string &value) { String = [&](std::string key, std::string &value) {
table->insert_or_assign(key, value); settings.setValue(key, QString::fromStdString(value));
}; };
Enum = [&](std::string key, int &value, std::vector<const char *> map) { Enum = [&](std::string key, int &value, std::vector<const char *> map) {
table->insert_or_assign(key, map[value]); settings.setValue(key, map[value]);
}; };
Double = [&](std::string key, double &value) { Double = [&](std::string key, double &value) {
table->insert_or_assign(key, value); settings.setValue(key, value);
}; };
Binding = [&](std::string key, EmuBinding &binding) { Binding = [&](std::string key, EmuBinding &binding) {
table->insert_or_assign(key, binding.to_config_string()); settings.setValue(key, QString::fromStdString(binding.to_config_string()));
}; };
BeginSection = [&](std::string str) { BeginSection = [&](std::string str) {
section = str; settings.beginGroup(str);
table = new toml::table;
}; };
EndSection = [&]() { EndSection = [&]() {
root.insert_or_assign(section, *table); settings.endGroup();
delete table;
}; };
root.clear();
} }
else else
{ {
Bool = [&](std::string key, bool &value) { Bool = [&](std::string key, bool &value) {
if (table && table->contains(key) && table->get(key)->is_boolean()) if (settings.contains(key))
value = table->get(key)->as_boolean()->get(); value = settings.value(key).toBool();
}; };
Int = [&](std::string key, int &value) { Int = [&](std::string key, int &value) {
if (table && table->contains(key) && table->get(key)->is_integer()) if (settings.contains(key))
value = table->get(key)->as_integer()->get(); value = settings.value(key).toInt();
}; };
String = [&](std::string key, std::string &value) { String = [&](std::string key, std::string &value) {
if (table && table->contains(key) && table->get(key)->is_string()) if (settings.contains(key))
value = table->get(key)->as_string()->get(); value = settings.value(key).toString().toStdString();
}; };
Binding = [&](std::string key, EmuBinding &binding) { Binding = [&](std::string key, EmuBinding &binding) {
if (table && table->contains(key) && table->get(key)->is_string()) if (settings.contains(key))
binding = EmuBinding::from_config_string(table->get(key)->as_string()->get()); binding = EmuBinding::from_config_string(settings.value(key).toString().toStdString());
}; };
Double = [&](std::string key, double &value) { Double = [&](std::string key, double &value) {
if (table && table->contains(key) && table->get(key)->is_floating_point()) if (settings.contains(key))
value = table->get(key)->as_floating_point()->get(); value = settings.value(key).toDouble();
}; };
Enum = [&](std::string key, int &value, std::vector<const char *> map) { Enum = [&](std::string key, int &value, std::vector<const char *> map) {
std::string entry; QString entry;
if (table && table->contains(key) && table->get(key)->is_string()) if (settings.contains(key))
entry = table->get(key)->as_string()->get(); entry = settings.value(key).toString().toLower();
else else
return; return;
auto tolower = [](std::string str) -> std::string {
for (auto &c : str)
if (c >= 'A' && c <= 'Z')
c += ('a' - 'A');
return str;
};
entry = tolower(entry);
for (size_t i = 0; i < map.size(); i++) for (size_t i = 0; i < map.size(); i++)
{ {
if (tolower(map[i]) == entry) if (QString(map[i]).toLower() == entry)
{ {
value = i; value = i;
return; return;
@ -400,20 +383,11 @@ void EmuConfig::config(std::string filename, bool write)
} }
}; };
BeginSection = [&](std::string str) { BeginSection = [&](std::string str) {
section = str; settings.beginGroup(QString::fromStdString(str));
auto root_section = root.get(section);
if (root_section)
table = root_section->as_table();
else
table = nullptr;
}; };
EndSection = [&]() { EndSection = [&]() {
settings.endGroup();
}; };
auto parse_result = toml::parse_file(filename);
if (parse_result.failed())
return;
root = std::move(parse_result.table());
} }
BeginSection("Operational"); BeginSection("Operational");
@ -494,10 +468,10 @@ void EmuConfig::config(std::string filename, bool write)
Enum("SoundFilter", sound_filter, { "Gaussian", "Nearest", "Linear", "Cubic", "Sinc" }); Enum("SoundFilter", sound_filter, { "Gaussian", "Nearest", "Linear", "Cubic", "Sinc" });
EndSection(); EndSection();
const char *names[] = { "Up", "Down", "Left", "Right", "A", "B", "X", "Y", "L", "R", "Start", "Select", "Turbo A", "Turbo B", "Turbo X", "Turbo Y", "Turbo L", "Turbo R" }; const char *names[] = { "Up", "Down", "Left", "Right", "A", "B", "X", "Y", "L", "R", "Start", "Select", "Turbo_A", "Turbo_B", "Turbo_X", "Turbo_Y", "Turbo_L", "Turbo_R" };
for (int c = 0; c < 5; c++) for (int c = 0; c < 5; c++)
{ {
BeginSection("Controller " + std::to_string(c)); BeginSection("Controller_" + std::to_string(c));
for (int y = 0; y < num_controller_bindings; y++) for (int y = 0; y < num_controller_bindings; y++)
for (int x = 0; x < allowed_bindings; x++) for (int x = 0; x < allowed_bindings; x++)
@ -534,13 +508,6 @@ void EmuConfig::config(std::string filename, bool write)
Int("SRAMSaveInterval", sram_save_interval); Int("SRAMSaveInterval", sram_save_interval);
EndSection(); EndSection();
if (write)
{
std::ofstream ofs(filename);
ofs << root;
ofs.close();
}
} }
void EmuConfig::setVRRConfig(bool enable) void EmuConfig::setVRRConfig(bool enable)

File diff suppressed because it is too large Load Diff