mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-01 14:58:14 +00:00
Access config setting data by owner pointer and offset, instead of by direct pointer
This commit is contained in:
1 parent
0626d3266d
commit
c30df3084c
3 files changed
+152
-141
No files matched your search
+37
-33
@@ -224,6 +224,7 @@ static int DefaultScreenRotation() {
|
||||
#define SETTING(a, x) (const char *)&a, &a.x
|
||||
#define SETTING_IDX(a, x, i) (const char *)&a, &a.x[i]
|
||||
|
||||
// All relative to g_Config.
|
||||
static const ConfigSetting generalSettings[] = {
|
||||
ConfigSetting("FirstRun", SETTING(g_Config, bFirstRun), true, CfgFlag::DEFAULT),
|
||||
ConfigSetting("RunCount", SETTING(g_Config, iRunCount), 0, CfgFlag::DEFAULT),
|
||||
@@ -1056,35 +1057,37 @@ static const ConfigSetting vrSettings[] = {
|
||||
|
||||
// The first column says what structure the parameters are relative to.
|
||||
static const ConfigSectionSettings sectionDescs[] = {
|
||||
{(const char *)&g_Config, "General", generalSettings, ARRAY_SIZE(generalSettings)},
|
||||
{(const char *)&g_Config, "CPU", cpuSettings, ARRAY_SIZE(cpuSettings)},
|
||||
{(const char *)&g_Config, "Graphics", graphicsSettings, ARRAY_SIZE(graphicsSettings)},
|
||||
{(const char *)&g_Config, "Sound", soundSettings, ARRAY_SIZE(soundSettings)},
|
||||
{(const char *)&g_Config, "Control", controlSettings, ARRAY_SIZE(controlSettings)},
|
||||
{(const char *)&g_Config, "SystemParam", systemParamSettings, ARRAY_SIZE(systemParamSettings)},
|
||||
{(const char *)&g_Config, "Network", networkSettings, ARRAY_SIZE(networkSettings)},
|
||||
{(const char *)&g_Config, "Debugger", debuggerSettings, ARRAY_SIZE(debuggerSettings)},
|
||||
{(const char *)&g_Config, "JIT", jitSettings, ARRAY_SIZE(jitSettings)},
|
||||
{(const char *)&g_Config, "Theme", themeSettings, ARRAY_SIZE(themeSettings)},
|
||||
{(const char *)&g_Config, "VR", vrSettings, ARRAY_SIZE(vrSettings)},
|
||||
{(const char *)&g_Config, "Achievements", achievementSettings, ARRAY_SIZE(achievementSettings)},
|
||||
{(char *)&g_Config, "General", generalSettings, ARRAY_SIZE(generalSettings)},
|
||||
{(char *)&g_Config, "CPU", cpuSettings, ARRAY_SIZE(cpuSettings)},
|
||||
{(char *)&g_Config, "Graphics", graphicsSettings, ARRAY_SIZE(graphicsSettings)},
|
||||
{(char *)&g_Config, "Sound", soundSettings, ARRAY_SIZE(soundSettings)},
|
||||
{(char *)&g_Config, "Control", controlSettings, ARRAY_SIZE(controlSettings)},
|
||||
{(char *)&g_Config, "SystemParam", systemParamSettings, ARRAY_SIZE(systemParamSettings)},
|
||||
{(char *)&g_Config, "Network", networkSettings, ARRAY_SIZE(networkSettings)},
|
||||
{(char *)&g_Config, "Debugger", debuggerSettings, ARRAY_SIZE(debuggerSettings)},
|
||||
{(char *)&g_Config, "JIT", jitSettings, ARRAY_SIZE(jitSettings)},
|
||||
{(char *)&g_Config, "Theme", themeSettings, ARRAY_SIZE(themeSettings)},
|
||||
{(char *)&g_Config, "VR", vrSettings, ARRAY_SIZE(vrSettings)},
|
||||
{(char *)&g_Config, "Achievements", achievementSettings, ARRAY_SIZE(achievementSettings)},
|
||||
};
|
||||
|
||||
const size_t numSections = ARRAY_SIZE(sectionDescs);
|
||||
|
||||
static void IterateSettings(IniFile &iniFile, std::function<void(Section *section, const ConfigSetting &setting)> func) {
|
||||
static void IterateSettings(IniFile &iniFile, std::function<void(char *owner, Section *section, const ConfigSetting &setting)> func) {
|
||||
for (size_t i = 0; i < numSections; ++i) {
|
||||
Section *section = iniFile.GetOrCreateSection(sectionDescs[i].section);
|
||||
char *owner = sectionDescs[i].owner;
|
||||
for (size_t j = 0; j < sectionDescs[i].settingsCount; j++) {
|
||||
func(section, sectionDescs[i].settings[j]);
|
||||
func(owner, section, sectionDescs[i].settings[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void IterateSettings(std::function<void(const ConfigSetting &setting)> func) {
|
||||
static void IterateSettings(std::function<void(char *owner, const ConfigSetting &setting)> func) {
|
||||
for (size_t i = 0; i < numSections; ++i) {
|
||||
char *owner = sectionDescs[i].owner;
|
||||
for (size_t j = 0; j < sectionDescs[i].settingsCount; j++) {
|
||||
func(sectionDescs[i].settings[j]);
|
||||
func(owner, sectionDescs[i].settings[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1097,8 +1100,8 @@ std::map<const void *, const ConfigSetting *> &Config::getPtrLUT() {
|
||||
Config::Config() {
|
||||
// Initialize the pointer->setting lookup map.
|
||||
auto ref = getPtrLUT();
|
||||
IterateSettings([this, &ref](const ConfigSetting &setting) {
|
||||
const void *ptr = setting.GetVoidPtr();
|
||||
IterateSettings([this, &ref](const char *owner, const ConfigSetting &setting) {
|
||||
const void *ptr = setting.GetVoidPtr(owner);
|
||||
ref[ptr] = &setting;
|
||||
});
|
||||
}
|
||||
@@ -1176,9 +1179,9 @@ bool Config::LoadAppendedConfig() {
|
||||
return false;
|
||||
}
|
||||
|
||||
IterateSettings(iniFile, [&iniFile](Section *section, const ConfigSetting &setting) {
|
||||
IterateSettings(iniFile, [&iniFile](char *owner, Section *section, const ConfigSetting &setting) {
|
||||
if (iniFile.Exists(section->name().c_str(), setting.iniKey_)) {
|
||||
setting.ReadFromIniSection(section);
|
||||
setting.ReadFromIniSection(owner, section);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1223,8 +1226,8 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) {
|
||||
// Continue anyway to initialize the config.
|
||||
}
|
||||
|
||||
IterateSettings(iniFile, [](const Section *section, const ConfigSetting &setting) {
|
||||
setting.ReadFromIniSection(section);
|
||||
IterateSettings(iniFile, [](char *owner, const Section *section, const ConfigSetting &setting) {
|
||||
setting.ReadFromIniSection(owner, section);
|
||||
});
|
||||
|
||||
iRunCount++;
|
||||
@@ -1364,9 +1367,9 @@ bool Config::Save(const char *saveReason) {
|
||||
// Need to do this somewhere...
|
||||
bFirstRun = false;
|
||||
|
||||
IterateSettings(iniFile, [&](Section *section, const ConfigSetting &setting) {
|
||||
IterateSettings(iniFile, [&](const char *owner, Section *section, const ConfigSetting &setting) {
|
||||
if (!bGameSpecific || !setting.PerGame()) {
|
||||
setting.WriteToIniSection(section);
|
||||
setting.WriteToIniSection(owner, section);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1573,8 +1576,8 @@ void Config::RestoreDefaults(RestoreSettingsBits whatToRestore, bool log) {
|
||||
Load();
|
||||
} else {
|
||||
if (whatToRestore & RestoreSettingsBits::SETTINGS) {
|
||||
IterateSettings([log](const ConfigSetting &setting) {
|
||||
setting.RestoreToDefault(log);
|
||||
IterateSettings([log](char *owner, const ConfigSetting &setting) {
|
||||
setting.RestoreToDefault(owner, log);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1653,9 +1656,9 @@ bool Config::saveGameConfig(const std::string &pGameId, const std::string &title
|
||||
|
||||
PreSaveCleanup(true);
|
||||
|
||||
IterateSettings(iniFile, [](Section *section, const ConfigSetting &setting) {
|
||||
IterateSettings(iniFile, [](const char *owner, Section *section, const ConfigSetting &setting) {
|
||||
if (setting.PerGame()) {
|
||||
setting.WriteToIniSection(section);
|
||||
setting.WriteToIniSection(owner, section);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1712,9 +1715,9 @@ bool Config::loadGameConfig(const std::string &pGameId, const std::string &title
|
||||
vPostShaderNames.push_back(v);
|
||||
}
|
||||
|
||||
IterateSettings(iniFile, [](const Section *section, const ConfigSetting &setting) {
|
||||
IterateSettings(iniFile, [](char *owner, const Section *section, const ConfigSetting &setting) {
|
||||
if (setting.PerGame()) {
|
||||
setting.ReadFromIniSection(section);
|
||||
setting.ReadFromIniSection(owner, section);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1739,9 +1742,9 @@ void Config::unloadGameConfig() {
|
||||
iniFile.Load(iniFilename_);
|
||||
|
||||
// Reload game specific settings back to standard.
|
||||
IterateSettings(iniFile, [](const Section *section, const ConfigSetting &setting) {
|
||||
IterateSettings(iniFile, [](char *owner, const Section *section, const ConfigSetting &setting) {
|
||||
if (setting.PerGame()) {
|
||||
setting.ReadFromIniSection(section);
|
||||
setting.ReadFromIniSection(owner, section);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1801,8 +1804,9 @@ void Config::ResetControlLayout() {
|
||||
void Config::GetReportingInfo(UrlEncoder &data) const {
|
||||
for (size_t i = 0; i < numSections; ++i) {
|
||||
const std::string prefix = std::string("config.") + sectionDescs[i].section;
|
||||
const char *owner = (const char *)sectionDescs[i].owner;
|
||||
for (size_t j = 0; j < sectionDescs[i].settingsCount; j++) {
|
||||
sectionDescs[i].settings[j].ReportSetting(data, prefix);
|
||||
sectionDescs[i].settings[j].ReportSetting(owner, data, prefix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+104
-78
@@ -12,47 +12,50 @@ bool ConfigSetting::perGame(void *ptr) {
|
||||
return g_Config.bGameSpecific && g_Config.getPtrLUT().count(ptr) > 0 && g_Config.getPtrLUT()[ptr]->PerGame();
|
||||
}
|
||||
|
||||
bool ConfigSetting::ReadFromIniSection(const Section *section) const {
|
||||
bool ConfigSetting::ReadFromIniSection(char *owner, const Section *section) const {
|
||||
switch (type_) {
|
||||
case Type::TYPE_BOOL:
|
||||
return section->Get(iniKey_, ptr_.b, cb_.b ? cb_.b() : default_.b);
|
||||
return section->Get(iniKey_, (bool *)(owner + offset_), cb_.b ? cb_.b() : default_.b);
|
||||
|
||||
case Type::TYPE_INT:
|
||||
if (translateFrom_) {
|
||||
std::string value;
|
||||
if (section->Get(iniKey_, &value, nullptr)) {
|
||||
*ptr_.i = translateFrom_(value);
|
||||
*((int *)(owner + offset_)) = translateFrom_(value);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return section->Get(iniKey_, ptr_.i, cb_.i ? cb_.i() : default_.i);
|
||||
return section->Get(iniKey_, (int *)(owner + offset_), cb_.i ? cb_.i() : default_.i);
|
||||
case Type::TYPE_UINT32:
|
||||
return section->Get(iniKey_, ptr_.u, cb_.u ? cb_.u() : default_.u);
|
||||
return section->Get(iniKey_, (uint32_t *)(owner + offset_), cb_.u ? cb_.u() : default_.u);
|
||||
case Type::TYPE_UINT64:
|
||||
return section->Get(iniKey_, ptr_.lu, cb_.lu ? cb_.lu() : default_.lu);
|
||||
return section->Get(iniKey_, (uint64_t *)(owner + offset_), cb_.lu ? cb_.lu() : default_.lu);
|
||||
case Type::TYPE_FLOAT:
|
||||
return section->Get(iniKey_, ptr_.f, cb_.f ? cb_.f() : default_.f);
|
||||
return section->Get(iniKey_, (float *)(owner + offset_), cb_.f ? cb_.f() : default_.f);
|
||||
case Type::TYPE_STRING:
|
||||
return section->Get(iniKey_, ptr_.s, cb_.s ? cb_.s().c_str() : default_.s);
|
||||
return section->Get(iniKey_, (std::string *)(owner + offset_), cb_.s ? cb_.s().c_str() : default_.s);
|
||||
case Type::TYPE_STRING_VECTOR:
|
||||
{
|
||||
// No support for callbacks for these yet. that's not an issue.
|
||||
bool success = section->Get(iniKey_, ptr_.v, default_.v);
|
||||
std::vector<std::string> *ptr = (std::vector<std::string> *)(owner + offset_);
|
||||
bool success = section->Get(iniKey_, ptr, default_.v);
|
||||
if (success) {
|
||||
MakeUnique(*ptr_.v);
|
||||
MakeUnique(*ptr);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
case Type::TYPE_TOUCH_POS:
|
||||
{
|
||||
ConfigTouchPos defaultTouchPos = cb_.touchPos ? cb_.touchPos() : default_.touchPos;
|
||||
section->Get(iniKey_, &ptr_.touchPos->x, defaultTouchPos.x);
|
||||
section->Get(ini2_, &ptr_.touchPos->y, defaultTouchPos.y);
|
||||
section->Get(ini3_, &ptr_.touchPos->scale, defaultTouchPos.scale);
|
||||
|
||||
ConfigTouchPos *touchPos = ((ConfigTouchPos *)(owner + offset_));
|
||||
section->Get(iniKey_, &touchPos->x, defaultTouchPos.x);
|
||||
section->Get(ini2_, &touchPos->y, defaultTouchPos.y);
|
||||
section->Get(ini3_, &touchPos->scale, defaultTouchPos.scale);
|
||||
if (ini4_) {
|
||||
section->Get(ini4_, &ptr_.touchPos->show, defaultTouchPos.show);
|
||||
section->Get(ini4_, &touchPos->show, defaultTouchPos.show);
|
||||
} else {
|
||||
ptr_.touchPos->show = defaultTouchPos.show;
|
||||
touchPos->show = defaultTouchPos.show;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -61,18 +64,21 @@ bool ConfigSetting::ReadFromIniSection(const Section *section) const {
|
||||
std::string tmp;
|
||||
bool result = section->Get(iniKey_, &tmp, cb_.p ? cb_.p() : default_.p);
|
||||
if (result) {
|
||||
*ptr_.p = Path(tmp);
|
||||
Path *path = (Path *)(owner + offset_);
|
||||
*path = Path(tmp);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
case Type::TYPE_CUSTOM_BUTTON:
|
||||
{
|
||||
ConfigCustomButton defaultCustomButton = cb_.customButton ? cb_.customButton() : default_.customButton;
|
||||
section->Get(iniKey_, &ptr_.customButton->key, defaultCustomButton.key);
|
||||
section->Get(ini2_, &ptr_.customButton->image, defaultCustomButton.image);
|
||||
section->Get(ini3_, &ptr_.customButton->shape, defaultCustomButton.shape);
|
||||
section->Get(ini4_, &ptr_.customButton->toggle, defaultCustomButton.toggle);
|
||||
section->Get(ini5_, &ptr_.customButton->repeat, defaultCustomButton.repeat);
|
||||
|
||||
ConfigCustomButton *customButton = ((ConfigCustomButton *)(owner + offset_));
|
||||
section->Get(iniKey_, &customButton->key, defaultCustomButton.key);
|
||||
section->Get(ini2_, &customButton->image, defaultCustomButton.image);
|
||||
section->Get(ini3_, &customButton->shape, defaultCustomButton.shape);
|
||||
section->Get(ini4_, &customButton->toggle, defaultCustomButton.toggle);
|
||||
section->Get(ini5_, &customButton->repeat, defaultCustomButton.repeat);
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
@@ -81,64 +87,75 @@ bool ConfigSetting::ReadFromIniSection(const Section *section) const {
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigSetting::WriteToIniSection(Section *section) const {
|
||||
void ConfigSetting::WriteToIniSection(const char *owner, Section *section) const {
|
||||
if (!SaveSetting()) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (type_) {
|
||||
case Type::TYPE_BOOL:
|
||||
return section->Set(iniKey_, *ptr_.b);
|
||||
return section->Set(iniKey_, *(bool *)(owner + offset_));
|
||||
case Type::TYPE_INT:
|
||||
if (translateTo_) {
|
||||
std::string value = translateTo_(*ptr_.i);
|
||||
int *ptr_i = (int *)(owner + offset_);
|
||||
std::string value = translateTo_(*ptr_i);
|
||||
return section->Set(iniKey_, value);
|
||||
}
|
||||
return section->Set(iniKey_, *ptr_.i);
|
||||
return section->Set(iniKey_, *(int *)(owner + offset_));
|
||||
case Type::TYPE_UINT32:
|
||||
return section->Set(iniKey_, *ptr_.u);
|
||||
return section->Set(iniKey_, *(uint32_t *)(owner + offset_));
|
||||
case Type::TYPE_UINT64:
|
||||
return section->Set(iniKey_, *ptr_.lu);
|
||||
return section->Set(iniKey_, *(uint64_t *)(owner + offset_));
|
||||
case Type::TYPE_FLOAT:
|
||||
return section->Set(iniKey_, *ptr_.f);
|
||||
return section->Set(iniKey_, *(float *)(owner + offset_));
|
||||
case Type::TYPE_STRING:
|
||||
return section->Set(iniKey_, *ptr_.s);
|
||||
return section->Set(iniKey_, *(std::string *)(owner + offset_));
|
||||
case Type::TYPE_STRING_VECTOR:
|
||||
return section->Set(iniKey_, *ptr_.v);
|
||||
return section->Set(iniKey_, *(std::vector<std::string> *)(owner + offset_));
|
||||
case Type::TYPE_PATH:
|
||||
return section->Set(iniKey_, ptr_.p->ToString());
|
||||
{
|
||||
Path *path = (Path *)(owner + offset_);
|
||||
return section->Set(iniKey_, path->ToString());
|
||||
}
|
||||
case Type::TYPE_TOUCH_POS:
|
||||
section->Set(iniKey_, ptr_.touchPos->x);
|
||||
section->Set(ini2_, ptr_.touchPos->y);
|
||||
section->Set(ini3_, ptr_.touchPos->scale);
|
||||
{
|
||||
const ConfigTouchPos *touchPos = (const ConfigTouchPos *)(owner + offset_);
|
||||
section->Set(iniKey_, touchPos->x);
|
||||
section->Set(ini2_, touchPos->y);
|
||||
section->Set(ini3_, touchPos->scale);
|
||||
if (ini4_) {
|
||||
section->Set(ini4_, ptr_.touchPos->show);
|
||||
section->Set(ini4_, touchPos->show);
|
||||
}
|
||||
return;
|
||||
}
|
||||
case Type::TYPE_CUSTOM_BUTTON:
|
||||
section->Set(iniKey_, ptr_.customButton->key);
|
||||
section->Set(ini2_, ptr_.customButton->image);
|
||||
section->Set(ini3_, ptr_.customButton->shape);
|
||||
section->Set(ini4_, ptr_.customButton->toggle);
|
||||
section->Set(ini5_, ptr_.customButton->repeat);
|
||||
{
|
||||
const ConfigCustomButton *customButton = (const ConfigCustomButton *)(owner + offset_);
|
||||
section->Set(iniKey_, customButton->key);
|
||||
section->Set(ini2_, customButton->image);
|
||||
section->Set(ini3_, customButton->shape);
|
||||
section->Set(ini4_, customButton->toggle);
|
||||
section->Set(ini5_, customButton->repeat);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
_dbg_assert_msg_(false, "Set%.*s): Unexpected ini setting type: %d", STR_VIEW(iniKey_), (int)type_);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool ConfigSetting::RestoreToDefault(bool log) const {
|
||||
bool ConfigSetting::RestoreToDefault(const char *owner, bool log) const {
|
||||
switch (type_) {
|
||||
case Type::TYPE_BOOL:
|
||||
{
|
||||
const bool origValue = *ptr_.b;
|
||||
*ptr_.b = cb_.b ? cb_.b() : default_.b;
|
||||
if (*ptr_.b != origValue) {
|
||||
bool *ptr_b = (bool *)(owner + offset_);
|
||||
const bool origValue = *ptr_b;
|
||||
*ptr_b = cb_.b ? cb_.b() : default_.b;
|
||||
if (*ptr_b != origValue) {
|
||||
if (log) {
|
||||
INFO_LOG(Log::System, "Restored %.*s from %s to default %s", STR_VIEW(iniKey_),
|
||||
origValue ? "true" : "false",
|
||||
*ptr_.b ? "true" : "false");
|
||||
*ptr_b ? "true" : "false");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -146,12 +163,13 @@ bool ConfigSetting::RestoreToDefault(bool log) const {
|
||||
}
|
||||
case Type::TYPE_INT:
|
||||
{
|
||||
const int origValue = *ptr_.i;
|
||||
*ptr_.i = cb_.i ? cb_.i() : default_.i;
|
||||
if (*ptr_.i != origValue) {
|
||||
int *ptr_i = (int *)(owner + offset_);
|
||||
const int origValue = *ptr_i;
|
||||
*ptr_i = cb_.i ? cb_.i() : default_.i;
|
||||
if (*ptr_i != origValue) {
|
||||
if (log) {
|
||||
INFO_LOG(Log::System, "Restored %.*s from %d to default %d", STR_VIEW(iniKey_),
|
||||
origValue, *ptr_.i);
|
||||
origValue, *ptr_i);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -159,12 +177,13 @@ bool ConfigSetting::RestoreToDefault(bool log) const {
|
||||
}
|
||||
case Type::TYPE_UINT32:
|
||||
{
|
||||
const u32 origValue = *ptr_.u;
|
||||
*ptr_.u = cb_.u ? cb_.u() : default_.u;
|
||||
if (*ptr_.u != origValue) {
|
||||
uint32_t *ptr_u = (uint32_t *)(owner + offset_);
|
||||
const uint32_t origValue = *ptr_u;
|
||||
*ptr_u = cb_.u ? cb_.u() : default_.u;
|
||||
if (*ptr_u != origValue) {
|
||||
if (log) {
|
||||
INFO_LOG(Log::System, "Restored %.*s from %u to default %u", STR_VIEW(iniKey_),
|
||||
origValue, *ptr_.u);
|
||||
origValue, *ptr_u);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -172,12 +191,13 @@ bool ConfigSetting::RestoreToDefault(bool log) const {
|
||||
}
|
||||
case Type::TYPE_UINT64:
|
||||
{
|
||||
const u64 origValue = *ptr_.lu;
|
||||
*ptr_.lu = cb_.lu ? cb_.lu() : default_.lu;
|
||||
if (*ptr_.lu != origValue) {
|
||||
uint64_t *ptr_lu = (uint64_t *)(owner + offset_);
|
||||
const uint64_t origValue = *ptr_lu;
|
||||
*ptr_lu = cb_.lu ? cb_.lu() : default_.lu;
|
||||
if (*ptr_lu != origValue) {
|
||||
if (log) {
|
||||
INFO_LOG(Log::System, "Restored %.*s from %llu to default %llu", STR_VIEW(iniKey_),
|
||||
(unsigned long long)origValue, (unsigned long long)(*ptr_.lu));
|
||||
(unsigned long long)origValue, (unsigned long long)(*ptr_lu));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -185,12 +205,13 @@ bool ConfigSetting::RestoreToDefault(bool log) const {
|
||||
}
|
||||
case Type::TYPE_FLOAT:
|
||||
{
|
||||
const float origValue = *ptr_.f;
|
||||
*ptr_.f = cb_.f ? cb_.f() : default_.f;
|
||||
if (*ptr_.f != origValue) {
|
||||
float *ptr_f = (float *)(owner + offset_);
|
||||
const float origValue = *ptr_f;
|
||||
*ptr_f = cb_.f ? cb_.f() : default_.f;
|
||||
if (*ptr_f != origValue) {
|
||||
if (log) {
|
||||
INFO_LOG(Log::System, "Restored %.*s from %f to default %f", STR_VIEW(iniKey_),
|
||||
origValue, *ptr_.f);
|
||||
origValue, *ptr_f);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -198,12 +219,13 @@ bool ConfigSetting::RestoreToDefault(bool log) const {
|
||||
}
|
||||
case Type::TYPE_STRING:
|
||||
{
|
||||
const std::string origValue = *ptr_.s;
|
||||
*ptr_.s = cb_.s ? cb_.s() : default_.s;
|
||||
if (*ptr_.s != origValue) {
|
||||
std::string *ptr_s = (std::string *)(owner + offset_);
|
||||
const std::string origValue = *ptr_s;
|
||||
*ptr_s = cb_.s ? cb_.s() : default_.s;
|
||||
if (*ptr_s != origValue) {
|
||||
if (log) {
|
||||
INFO_LOG(Log::System, "Restored %.*s from \"%s\" to default \"%s\"", STR_VIEW(iniKey_),
|
||||
origValue.c_str(), ptr_.s->c_str());
|
||||
origValue.c_str(), ptr_s->c_str());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -211,22 +233,26 @@ bool ConfigSetting::RestoreToDefault(bool log) const {
|
||||
}
|
||||
case Type::TYPE_STRING_VECTOR:
|
||||
{
|
||||
*ptr_.v = *default_.v;
|
||||
std::vector<std::string> *ptr_vec = (std::vector<std::string> *)(owner + offset_);
|
||||
*ptr_vec = *default_.v;
|
||||
break;
|
||||
}
|
||||
case Type::TYPE_TOUCH_POS:
|
||||
{
|
||||
*ptr_.touchPos = cb_.touchPos ? cb_.touchPos() : default_.touchPos;
|
||||
ConfigTouchPos *ptr_touchPos = (ConfigTouchPos *)(owner + offset_);
|
||||
*ptr_touchPos = cb_.touchPos ? cb_.touchPos() : default_.touchPos;
|
||||
break;
|
||||
}
|
||||
case Type::TYPE_PATH:
|
||||
{
|
||||
*ptr_.p = Path(cb_.p ? cb_.p() : default_.p);
|
||||
Path *ptr_path = (Path *)(owner + offset_);
|
||||
*ptr_path = Path(cb_.p ? cb_.p() : default_.p);
|
||||
break;
|
||||
}
|
||||
case Type::TYPE_CUSTOM_BUTTON:
|
||||
{
|
||||
*ptr_.customButton = cb_.customButton ? cb_.customButton() : default_.customButton;
|
||||
ConfigCustomButton *ptr_customButton = (ConfigCustomButton *)(owner + offset_);
|
||||
*ptr_customButton = cb_.customButton ? cb_.customButton() : default_.customButton;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -236,21 +262,21 @@ bool ConfigSetting::RestoreToDefault(bool log) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void ConfigSetting::ReportSetting(UrlEncoder &data, const std::string &prefix) const {
|
||||
void ConfigSetting::ReportSetting(const char *owner, UrlEncoder &data, const std::string &prefix) const {
|
||||
if (!Report())
|
||||
return;
|
||||
|
||||
const std::string key = prefix + std::string(iniKey_);
|
||||
|
||||
switch (type_) {
|
||||
case Type::TYPE_BOOL: return data.Add(key, *ptr_.b);
|
||||
case Type::TYPE_INT: return data.Add(key, *ptr_.i);
|
||||
case Type::TYPE_UINT32: return data.Add(key, *ptr_.u);
|
||||
case Type::TYPE_UINT64: return data.Add(key, *ptr_.lu);
|
||||
case Type::TYPE_FLOAT: return data.Add(key, *ptr_.f);
|
||||
case Type::TYPE_STRING: return data.Add(key, *ptr_.s);
|
||||
case Type::TYPE_STRING_VECTOR: return data.Add(key, *ptr_.v);
|
||||
case Type::TYPE_PATH: return data.Add(key, ptr_.p->ToString());
|
||||
case Type::TYPE_BOOL: return data.Add(key, (const bool *)(owner + offset_));
|
||||
case Type::TYPE_INT: return data.Add(key, (const int *)(owner + offset_));
|
||||
case Type::TYPE_UINT32: return data.Add(key, (const u32 *)(owner + offset_));
|
||||
case Type::TYPE_UINT64: return data.Add(key, (const u64 *)(owner + offset_));
|
||||
case Type::TYPE_FLOAT: return data.Add(key, (const float *)(owner + offset_));
|
||||
case Type::TYPE_STRING: return data.Add(key, (const std::string *)(owner + offset_));
|
||||
case Type::TYPE_STRING_VECTOR: return data.Add(key, (const std::vector<std::string> *)(owner + offset_));
|
||||
case Type::TYPE_PATH: return data.Add(key, ((const Path *)(owner + offset_))->ToString());
|
||||
case Type::TYPE_TOUCH_POS: return; // Doesn't report.
|
||||
case Type::TYPE_CUSTOM_BUTTON: return; // Doesn't report.
|
||||
default:
|
||||
|
||||
+11
-30
@@ -57,13 +57,13 @@ struct ConfigSetting {
|
||||
|
||||
typedef bool (*BoolDefaultCallback)();
|
||||
typedef int (*IntDefaultCallback)();
|
||||
typedef uint32_t(*Uint32DefaultCallback)();
|
||||
typedef uint64_t(*Uint64DefaultCallback)();
|
||||
typedef uint32_t (*Uint32DefaultCallback)();
|
||||
typedef uint64_t (*Uint64DefaultCallback)();
|
||||
typedef float (*FloatDefaultCallback)();
|
||||
typedef std::string (*StringDefaultCallback)();
|
||||
typedef ConfigTouchPos(*TouchPosDefaultCallback)();
|
||||
typedef ConfigTouchPos (*TouchPosDefaultCallback)();
|
||||
typedef const char *(*PathDefaultCallback)();
|
||||
typedef ConfigCustomButton(*CustomButtonDefaultCallback)();
|
||||
typedef ConfigCustomButton (*CustomButtonDefaultCallback)();
|
||||
|
||||
union DefaultCallback {
|
||||
BoolDefaultCallback b;
|
||||
@@ -79,132 +79,114 @@ struct ConfigSetting {
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, bool *v, bool def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_BOOL), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.b = v;
|
||||
cb_.b = nullptr;
|
||||
default_.b = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, int *v, int def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_INT), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.i = v;
|
||||
cb_.i = nullptr;
|
||||
default_.i = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, int *v, int def, std::string (*transTo)(int), int (*transFrom)(const std::string &), CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_INT), flags_(flags), translateTo_(transTo), translateFrom_(transFrom), offset_((const char *)v - owner) {
|
||||
ptr_.i = v;
|
||||
cb_.i = nullptr;
|
||||
default_.i = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, uint32_t *v, uint32_t def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_UINT32), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.u = v;
|
||||
cb_.u = nullptr;
|
||||
default_.u = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, uint64_t *v, uint64_t def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_UINT64), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.lu = v;
|
||||
cb_.lu = nullptr;
|
||||
default_.lu = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, float *v, float def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_FLOAT), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.f = v;
|
||||
cb_.f = nullptr;
|
||||
default_.f = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, std::string *v, const char *def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_STRING), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.s = v;
|
||||
cb_.s = nullptr;
|
||||
default_.s = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, std::vector<std::string> *v, const std::vector<std::string> *def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_STRING_VECTOR), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.v = v;
|
||||
default_.v = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, Path *v, const char *def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_PATH), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.p = v;
|
||||
cb_.p = nullptr;
|
||||
default_.p = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(const char *iniX, const char *iniY, const char *iniScale, const char *iniShow, const char *owner, ConfigTouchPos *v, ConfigTouchPos def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(iniX), ini2_(iniY), ini3_(iniScale), ini4_(iniShow), type_(Type::TYPE_TOUCH_POS), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.touchPos = v;
|
||||
cb_.touchPos = nullptr;
|
||||
default_.touchPos = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(const char *iniKey, const char *iniImage, const char *iniShape, const char *iniToggle, const char *iniRepeat, const char *owner, ConfigCustomButton *v, ConfigCustomButton def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(iniKey), ini2_(iniImage), ini3_(iniShape), ini4_(iniToggle), ini5_(iniRepeat), type_(Type::TYPE_CUSTOM_BUTTON), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.customButton = v;
|
||||
cb_.customButton = nullptr;
|
||||
default_.customButton = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, bool *v, BoolDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_BOOL), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.b = v;
|
||||
cb_.b = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, int *v, IntDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_INT), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.i = v;
|
||||
cb_.i = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, int *v, IntDefaultCallback def, std::string(*transTo)(int), int(*transFrom)(const std::string &), CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_INT), flags_(flags), offset_((const char *)v - owner), translateTo_(transTo), translateFrom_(transFrom) {
|
||||
ptr_.i = v;
|
||||
cb_.i = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, uint32_t *v, Uint32DefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_UINT32), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.u = v;
|
||||
cb_.u = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, float *v, FloatDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_FLOAT), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.f = v;
|
||||
cb_.f = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view ini, const char *owner, std::string *v, StringDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(ini), type_(Type::TYPE_STRING), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.s = v;
|
||||
cb_.s = def;
|
||||
}
|
||||
|
||||
constexpr ConfigSetting(std::string_view iniX, const char *iniY, const char *iniScale, const char *iniShow, const char *owner, ConfigTouchPos *v, TouchPosDefaultCallback def, CfgFlag flags = CfgFlag::DEFAULT) noexcept
|
||||
: iniKey_(iniX), ini2_(iniY), ini3_(iniScale), ini4_(iniShow), type_(Type::TYPE_TOUCH_POS), flags_(flags), offset_((const char *)v - owner) {
|
||||
ptr_.touchPos = v;
|
||||
cb_.touchPos = def;
|
||||
}
|
||||
|
||||
bool ReadFromIniSection(const Section *section) const;
|
||||
bool ReadFromIniSection(char *owner, const Section *section) const;
|
||||
|
||||
// Yes, this can be const because what's modified is not the ConfigSetting struct, but the value which is stored elsewhere.
|
||||
// Should actually be called WriteToIni or something.
|
||||
void WriteToIniSection(Section *section) const;
|
||||
void WriteToIniSection(const char *owner, Section *section) const;
|
||||
|
||||
// If log is true, logs if the setting changed.
|
||||
bool RestoreToDefault(bool log) const;
|
||||
bool RestoreToDefault(const char *owner, bool log) const;
|
||||
|
||||
void ReportSetting(UrlEncoder &data, const std::string &prefix) const;
|
||||
void ReportSetting(const char *owner, UrlEncoder &data, const std::string &prefix) const;
|
||||
|
||||
// Easy flag accessors.
|
||||
bool PerGame() const { return flags_ & CfgFlag::PER_GAME; }
|
||||
@@ -222,14 +204,13 @@ struct ConfigSetting {
|
||||
// Returns false if per-game settings are not currently used
|
||||
static bool perGame(void *ptr);
|
||||
|
||||
const void *GetVoidPtr() const {
|
||||
const void *GetVoidPtr(const char *owner) const {
|
||||
// undefined behavior but in reality will work.
|
||||
return (const void *)ptr_.i;
|
||||
return (const void *)(owner + offset_);
|
||||
}
|
||||
|
||||
private:
|
||||
CfgFlag flags_;
|
||||
SettingPtr ptr_{};
|
||||
DefaultValue default_{};
|
||||
DefaultCallback cb_{};
|
||||
u32 offset_;
|
||||
@@ -240,7 +221,7 @@ private:
|
||||
};
|
||||
|
||||
struct ConfigSectionSettings {
|
||||
const char *base;
|
||||
char *owner;
|
||||
const char *section;
|
||||
const ConfigSetting *settings;
|
||||
size_t settingsCount;
|
||||
|
||||
Reference in new issue
Block a user