From 6ac45ddba12a98d2f321c9f072168789d6cebfb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 5 Apr 2023 11:44:11 +0200 Subject: [PATCH] Extract the ConfigSetting implementation into its own file. --- Core/Config.cpp | 349 +------------------------------------- Core/Config.h | 16 -- Core/ConfigSettings.cpp | 144 ++++++++++++++++ Core/ConfigSettings.h | 222 ++++++++++++++++++++++++ Core/ConfigValues.h | 16 ++ Core/Core.vcxproj | 2 + Core/Core.vcxproj.filters | 6 + 7 files changed, 391 insertions(+), 364 deletions(-) diff --git a/Core/Config.cpp b/Core/Config.cpp index 9bdac17310..1984aa7490 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -48,6 +48,7 @@ #include "Common/GPU/Vulkan/VulkanLoader.h" #include "Common/VR/PPSSPPVR.h" #include "Core/Config.h" +#include "Core/ConfigSettings.h" #include "Core/ConfigValues.h" #include "Core/Loaders.h" #include "Core/KeyMap.h" @@ -79,354 +80,6 @@ static const char *logSectionName = "LogDebug"; static const char *logSectionName = "Log"; #endif -struct ConfigSetting { - enum Type { - TYPE_TERMINATOR, - TYPE_BOOL, - TYPE_INT, - TYPE_UINT32, - TYPE_UINT64, - TYPE_FLOAT, - TYPE_STRING, - TYPE_TOUCH_POS, - TYPE_PATH, - TYPE_CUSTOM_BUTTON - }; - union DefaultValue { - bool b; - int i; - uint32_t u; - uint64_t lu; - float f; - const char *s; - const char *p; // not sure how much point.. - ConfigTouchPos touchPos; - ConfigCustomButton customButton; - }; - union SettingPtr { - bool *b; - int *i; - uint32_t *u; - uint64_t *lu; - float *f; - std::string *s; - Path *p; - ConfigTouchPos *touchPos; - ConfigCustomButton *customButton; - }; - - typedef bool (*BoolDefaultCallback)(); - typedef int (*IntDefaultCallback)(); - typedef uint32_t (*Uint32DefaultCallback)(); - typedef uint64_t (*Uint64DefaultCallback)(); - typedef float (*FloatDefaultCallback)(); - typedef const char *(*StringDefaultCallback)(); - typedef ConfigTouchPos (*TouchPosDefaultCallback)(); - typedef const char *(*PathDefaultCallback)(); - typedef ConfigCustomButton (*CustomButtonDefaultCallback)(); - - union DefaultCallback { - BoolDefaultCallback b; - IntDefaultCallback i; - Uint32DefaultCallback u; - Uint64DefaultCallback lu; - FloatDefaultCallback f; - StringDefaultCallback s; - PathDefaultCallback p; - TouchPosDefaultCallback touchPos; - CustomButtonDefaultCallback customButton; - }; - - ConfigSetting(const char *ini, bool *v, bool def, bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_BOOL), report_(false), save_(save), perGame_(perGame) { - ptr_.b = v; - cb_.b = nullptr; - default_.b = def; - } - - ConfigSetting(const char *ini, int *v, int def, bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_INT), report_(false), save_(save), perGame_(perGame) { - ptr_.i = v; - cb_.i = nullptr; - default_.i = def; - } - - ConfigSetting(const char *ini, int *v, int def, std::string (*transTo)(int), int (*transFrom)(const std::string &), bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_INT), report_(false), save_(save), perGame_(perGame), translateTo_(transTo), translateFrom_(transFrom) { - ptr_.i = v; - cb_.i = nullptr; - default_.i = def; - } - - ConfigSetting(const char *ini, uint32_t *v, uint32_t def, bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_UINT32), report_(false), save_(save), perGame_(perGame) { - ptr_.u = v; - cb_.u = nullptr; - default_.u = def; - } - - ConfigSetting(const char *ini, uint64_t *v, uint64_t def, bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_UINT64), report_(false), save_(save), perGame_(perGame) { - ptr_.lu = v; - cb_.lu = nullptr; - default_.lu = def; - } - - ConfigSetting(const char *ini, float *v, float def, bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_FLOAT), report_(false), save_(save), perGame_(perGame) { - ptr_.f = v; - cb_.f = nullptr; - default_.f = def; - } - - ConfigSetting(const char *ini, std::string *v, const char *def, bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_STRING), report_(false), save_(save), perGame_(perGame) { - ptr_.s = v; - cb_.s = nullptr; - default_.s = def; - } - - ConfigSetting(const char *ini, Path *p, const char *def, bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_PATH), report_(false), save_(save), perGame_(perGame) { - ptr_.p = p; - cb_.p = nullptr; - default_.p = def; - } - - ConfigSetting(const char *iniX, const char *iniY, const char *iniScale, const char *iniShow, ConfigTouchPos *v, ConfigTouchPos def, bool save = true, bool perGame = false) - : iniKey_(iniX), ini2_(iniY), ini3_(iniScale), ini4_(iniShow), type_(TYPE_TOUCH_POS), report_(false), save_(save), perGame_(perGame) { - ptr_.touchPos = v; - cb_.touchPos = nullptr; - default_.touchPos = def; - } - - ConfigSetting(const char *iniKey, const char *iniImage, const char *iniShape, const char *iniToggle, const char *iniRepeat, ConfigCustomButton *v, ConfigCustomButton def, bool save = true, bool perGame = false) - : iniKey_(iniKey), ini2_(iniImage), ini3_(iniShape), ini4_(iniToggle), ini5_(iniRepeat), type_(TYPE_CUSTOM_BUTTON), report_(false), save_(save), perGame_(perGame) { - ptr_.customButton = v; - cb_.customButton = nullptr; - default_.customButton = def; - } - - ConfigSetting(const char *ini, bool *v, BoolDefaultCallback def, bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_BOOL), report_(false), save_(save), perGame_(perGame) { - ptr_.b = v; - cb_.b = def; - } - - ConfigSetting(const char *ini, int *v, IntDefaultCallback def, bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_INT), report_(false), save_(save), perGame_(perGame) { - ptr_ .i = v; - cb_.i = def; - } - - ConfigSetting(const char *ini, int *v, IntDefaultCallback def, std::string(*transTo)(int), int(*transFrom)(const std::string &), bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_INT), report_(false), save_(save), perGame_(perGame), translateTo_(transTo), translateFrom_(transFrom) { - ptr_.i = v; - cb_.i = def; - } - - ConfigSetting(const char *ini, uint32_t *v, Uint32DefaultCallback def, bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_UINT32), report_(false), save_(save), perGame_(perGame) { - ptr_ .u = v; - cb_.u = def; - } - - ConfigSetting(const char *ini, float *v, FloatDefaultCallback def, bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_FLOAT), report_(false), save_(save), perGame_(perGame) { - ptr_.f = v; - cb_.f = def; - } - - ConfigSetting(const char *ini, std::string *v, StringDefaultCallback def, bool save = true, bool perGame = false) - : iniKey_(ini), type_(TYPE_STRING), report_(false), save_(save), perGame_(perGame) { - ptr_.s = v; - cb_.s = def; - } - - ConfigSetting(const char *iniX, const char *iniY, const char *iniScale, const char *iniShow, ConfigTouchPos *v, TouchPosDefaultCallback def, bool save = true, bool perGame = false) - : iniKey_(iniX), ini2_(iniY), ini3_(iniScale), ini4_(iniShow), type_(TYPE_TOUCH_POS), report_(false), save_(save), perGame_(perGame) { - ptr_.touchPos = v; - cb_.touchPos = def; - } - - // Should actually be called ReadFromIni or something. - bool Get(const Section *section) const { - switch (type_) { - case TYPE_BOOL: - return section->Get(iniKey_, ptr_.b, cb_.b ? cb_.b() : default_.b); - - case TYPE_INT: - if (translateFrom_) { - std::string value; - if (section->Get(iniKey_, &value, nullptr)) { - *ptr_.i = translateFrom_(value); - return true; - } - } - return section->Get(iniKey_, ptr_.i, cb_.i ? cb_.i() : default_.i); - case TYPE_UINT32: - return section->Get(iniKey_, ptr_.u, cb_.u ? cb_.u() : default_.u); - case TYPE_UINT64: - return section->Get(iniKey_, ptr_.lu, cb_.lu ? cb_.lu() : default_.lu); - case TYPE_FLOAT: - return section->Get(iniKey_, ptr_.f, cb_.f ? cb_.f() : default_.f); - case TYPE_STRING: - return section->Get(iniKey_, ptr_.s, cb_.s ? cb_.s() : default_.s); - case 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); - if (ini4_) { - section->Get(ini4_, &ptr_.touchPos->show, defaultTouchPos.show); - } else { - ptr_.touchPos->show = defaultTouchPos.show; - } - return true; - } - case TYPE_PATH: - { - std::string tmp; - bool result = section->Get(iniKey_, &tmp, cb_.p ? cb_.p() : default_.p); - if (result) { - *ptr_.p = Path(tmp); - } - return result; - } - case 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); - return true; - } - default: - _dbg_assert_msg_(false, "Get(%s): Unexpected ini setting type: %d", iniKey_, (int)type_); - return false; - } - } - - // 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 Set(Section *section) const { - if (!save_) { - return; - } - - switch (type_) { - case TYPE_BOOL: - return section->Set(iniKey_, *ptr_.b); - case TYPE_INT: - if (translateTo_) { - std::string value = translateTo_(*ptr_.i); - return section->Set(iniKey_, value); - } - return section->Set(iniKey_, *ptr_.i); - case TYPE_UINT32: - return section->Set(iniKey_, *ptr_.u); - case TYPE_UINT64: - return section->Set(iniKey_, *ptr_.lu); - case TYPE_FLOAT: - return section->Set(iniKey_, *ptr_.f); - case TYPE_STRING: - return section->Set(iniKey_, *ptr_.s); - case TYPE_PATH: - return section->Set(iniKey_, ptr_.p->ToString()); - case TYPE_TOUCH_POS: - section->Set(iniKey_, ptr_.touchPos->x); - section->Set(ini2_, ptr_.touchPos->y); - section->Set(ini3_, ptr_.touchPos->scale); - if (ini4_) { - section->Set(ini4_, ptr_.touchPos->show); - } - return; - case 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); - return; - default: - _dbg_assert_msg_(false, "Set(%s): Unexpected ini setting type: %d", iniKey_, (int)type_); - return; - } - } - - void RestoreToDefault() const { - switch (type_) { - case TYPE_BOOL: *ptr_.b = cb_.b ? cb_.b() : default_.b; break; - case TYPE_INT: *ptr_.i = cb_.i ? cb_.i() : default_.i; break; - case TYPE_UINT32: *ptr_.u = cb_.u ? cb_.u() : default_.u; break; - case TYPE_UINT64: *ptr_.lu = cb_.lu ? cb_.lu() : default_.lu; break; - case TYPE_FLOAT: *ptr_.f = cb_.f ? cb_.f() : default_.f; break; - case TYPE_STRING: *ptr_.s = cb_.s ? cb_.s() : default_.s; break; - case TYPE_TOUCH_POS: *ptr_.touchPos = cb_.touchPos ? cb_.touchPos() : default_.touchPos; break; - case TYPE_PATH: *ptr_.p = Path(cb_.p ? cb_.p() : default_.p); break; - case TYPE_CUSTOM_BUTTON: *ptr_.customButton = cb_.customButton ? cb_.customButton() : default_.customButton; break; - default: - _dbg_assert_msg_(false, "RestoreToDefault(%s): Unexpected ini setting type: %d", iniKey_, (int)type_); - } - } - - void Report(UrlEncoder &data, const std::string &prefix) const { - if (!report_) - return; - - switch (type_) { - case TYPE_BOOL: return data.Add(prefix + iniKey_, *ptr_.b); - case TYPE_INT: return data.Add(prefix + iniKey_, *ptr_.i); - case TYPE_UINT32: return data.Add(prefix + iniKey_, *ptr_.u); - case TYPE_UINT64: return data.Add(prefix + iniKey_, *ptr_.lu); - case TYPE_FLOAT: return data.Add(prefix + iniKey_, *ptr_.f); - case TYPE_STRING: return data.Add(prefix + iniKey_, *ptr_.s); - case TYPE_PATH: return data.Add(prefix + iniKey_, ptr_.p->ToString()); - case TYPE_TOUCH_POS: return; // Doesn't report. - case TYPE_CUSTOM_BUTTON: return; // Doesn't report. - default: - _dbg_assert_msg_(false, "Report(%s): Unexpected ini setting type: %d", iniKey_, (int)type_); - return; - } - } - - const char *iniKey_ = nullptr; - const char *ini2_ = nullptr; - const char *ini3_ = nullptr; - const char *ini4_ = nullptr; - const char *ini5_ = nullptr; - - const Type type_; - - bool report_; - const bool save_; - const bool perGame_; - SettingPtr ptr_; - DefaultValue default_{}; - DefaultCallback cb_; - - // We only support transform for ints. - std::string (*translateTo_)(int) = nullptr; - int(*translateFrom_)(const std::string &) = nullptr; -}; - -struct ReportedConfigSetting : public ConfigSetting { - template - ReportedConfigSetting(const char *ini, T1 *v, T2 def, bool save = true, bool perGame = false) - : ConfigSetting(ini, v, def, save, perGame) { - report_ = true; - } - - template - ReportedConfigSetting(const char *ini, T1 *v, T2 def, std::string(*transTo)(int), int(*transFrom)(const std::string &), bool save = true, bool perGame = false) - : ConfigSetting(ini, v, def, transTo, transFrom, save, perGame) { - report_ = true; - } -}; - const char *DefaultLangRegion() { // Unfortunate default. There's no need to use bFirstRun, since this is only a default. static std::string defaultLangRegion = "en_US"; diff --git a/Core/Config.h b/Core/Config.h index 7094e0a034..9b0e690bba 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -48,22 +48,6 @@ namespace http { struct UrlEncoder; struct ConfigPrivate; -struct ConfigTouchPos { - float x; - float y; - float scale; - // Note: Show is not used for all settings. - bool show; -}; - -struct ConfigCustomButton { - uint64_t key; - int image; - int shape; - bool toggle; - bool repeat; -}; - struct Config { public: Config(); diff --git a/Core/ConfigSettings.cpp b/Core/ConfigSettings.cpp index 9410dbfd9d..c9812d286f 100644 --- a/Core/ConfigSettings.cpp +++ b/Core/ConfigSettings.cpp @@ -1,3 +1,147 @@ +#include "Common/Data/Format/IniFile.h" +#include "Common/Net/URL.h" +#include "Common/Log.h" + #include "Core/ConfigSettings.h" #include "Core/ConfigValues.h" +bool ConfigSetting::Get(const Section *section) const { + switch (type_) { + case TYPE_BOOL: + return section->Get(iniKey_, ptr_.b, cb_.b ? cb_.b() : default_.b); + + case TYPE_INT: + if (translateFrom_) { + std::string value; + if (section->Get(iniKey_, &value, nullptr)) { + *ptr_.i = translateFrom_(value); + return true; + } + } + return section->Get(iniKey_, ptr_.i, cb_.i ? cb_.i() : default_.i); + case TYPE_UINT32: + return section->Get(iniKey_, ptr_.u, cb_.u ? cb_.u() : default_.u); + case TYPE_UINT64: + return section->Get(iniKey_, ptr_.lu, cb_.lu ? cb_.lu() : default_.lu); + case TYPE_FLOAT: + return section->Get(iniKey_, ptr_.f, cb_.f ? cb_.f() : default_.f); + case TYPE_STRING: + return section->Get(iniKey_, ptr_.s, cb_.s ? cb_.s() : default_.s); + case 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); + if (ini4_) { + section->Get(ini4_, &ptr_.touchPos->show, defaultTouchPos.show); + } else { + ptr_.touchPos->show = defaultTouchPos.show; + } + return true; + } + case TYPE_PATH: + { + std::string tmp; + bool result = section->Get(iniKey_, &tmp, cb_.p ? cb_.p() : default_.p); + if (result) { + *ptr_.p = Path(tmp); + } + return result; + } + case 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); + return true; + } + default: + _dbg_assert_msg_(false, "Get(%s): Unexpected ini setting type: %d", iniKey_, (int)type_); + return false; + } +} + +void ConfigSetting::Set(Section *section) const { + if (!save_) { + return; + } + + switch (type_) { + case TYPE_BOOL: + return section->Set(iniKey_, *ptr_.b); + case TYPE_INT: + if (translateTo_) { + std::string value = translateTo_(*ptr_.i); + return section->Set(iniKey_, value); + } + return section->Set(iniKey_, *ptr_.i); + case TYPE_UINT32: + return section->Set(iniKey_, *ptr_.u); + case TYPE_UINT64: + return section->Set(iniKey_, *ptr_.lu); + case TYPE_FLOAT: + return section->Set(iniKey_, *ptr_.f); + case TYPE_STRING: + return section->Set(iniKey_, *ptr_.s); + case TYPE_PATH: + return section->Set(iniKey_, ptr_.p->ToString()); + case TYPE_TOUCH_POS: + section->Set(iniKey_, ptr_.touchPos->x); + section->Set(ini2_, ptr_.touchPos->y); + section->Set(ini3_, ptr_.touchPos->scale); + if (ini4_) { + section->Set(ini4_, ptr_.touchPos->show); + } + return; + case 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); + return; + default: + _dbg_assert_msg_(false, "Set(%s): Unexpected ini setting type: %d", iniKey_, (int)type_); + return; + } +} + +void ConfigSetting::RestoreToDefault() const { + switch (type_) { + case TYPE_BOOL: *ptr_.b = cb_.b ? cb_.b() : default_.b; break; + case TYPE_INT: *ptr_.i = cb_.i ? cb_.i() : default_.i; break; + case TYPE_UINT32: *ptr_.u = cb_.u ? cb_.u() : default_.u; break; + case TYPE_UINT64: *ptr_.lu = cb_.lu ? cb_.lu() : default_.lu; break; + case TYPE_FLOAT: *ptr_.f = cb_.f ? cb_.f() : default_.f; break; + case TYPE_STRING: *ptr_.s = cb_.s ? cb_.s() : default_.s; break; + case TYPE_TOUCH_POS: *ptr_.touchPos = cb_.touchPos ? cb_.touchPos() : default_.touchPos; break; + case TYPE_PATH: *ptr_.p = Path(cb_.p ? cb_.p() : default_.p); break; + case TYPE_CUSTOM_BUTTON: *ptr_.customButton = cb_.customButton ? cb_.customButton() : default_.customButton; break; + default: + _dbg_assert_msg_(false, "RestoreToDefault(%s): Unexpected ini setting type: %d", iniKey_, (int)type_); + } +} + +void ConfigSetting::Report(UrlEncoder &data, const std::string &prefix) const { + if (!report_) + return; + + switch (type_) { + case TYPE_BOOL: return data.Add(prefix + iniKey_, *ptr_.b); + case TYPE_INT: return data.Add(prefix + iniKey_, *ptr_.i); + case TYPE_UINT32: return data.Add(prefix + iniKey_, *ptr_.u); + case TYPE_UINT64: return data.Add(prefix + iniKey_, *ptr_.lu); + case TYPE_FLOAT: return data.Add(prefix + iniKey_, *ptr_.f); + case TYPE_STRING: return data.Add(prefix + iniKey_, *ptr_.s); + case TYPE_PATH: return data.Add(prefix + iniKey_, ptr_.p->ToString()); + case TYPE_TOUCH_POS: return; // Doesn't report. + case TYPE_CUSTOM_BUTTON: return; // Doesn't report. + default: + _dbg_assert_msg_(false, "Report(%s): Unexpected ini setting type: %d", iniKey_, (int)type_); + return; + } +} diff --git a/Core/ConfigSettings.h b/Core/ConfigSettings.h index 3f59c932d3..6f5aa33f7d 100644 --- a/Core/ConfigSettings.h +++ b/Core/ConfigSettings.h @@ -1,2 +1,224 @@ #pragma once +#include + +#include "Core/ConfigValues.h" + +class Path; +class Section; // ini file section +struct UrlEncoder; + +struct ConfigSetting { + enum Type { + TYPE_TERMINATOR, + TYPE_BOOL, + TYPE_INT, + TYPE_UINT32, + TYPE_UINT64, + TYPE_FLOAT, + TYPE_STRING, + TYPE_TOUCH_POS, + TYPE_PATH, + TYPE_CUSTOM_BUTTON + }; + union DefaultValue { + bool b; + int i; + uint32_t u; + uint64_t lu; + float f; + const char *s; + const char *p; // not sure how much point.. + ConfigTouchPos touchPos; + ConfigCustomButton customButton; + }; + union SettingPtr { + bool *b; + int *i; + uint32_t *u; + uint64_t *lu; + float *f; + std::string *s; + Path *p; + ConfigTouchPos *touchPos; + ConfigCustomButton *customButton; + }; + + typedef bool (*BoolDefaultCallback)(); + typedef int (*IntDefaultCallback)(); + typedef uint32_t(*Uint32DefaultCallback)(); + typedef uint64_t(*Uint64DefaultCallback)(); + typedef float (*FloatDefaultCallback)(); + typedef const char *(*StringDefaultCallback)(); + typedef ConfigTouchPos(*TouchPosDefaultCallback)(); + typedef const char *(*PathDefaultCallback)(); + typedef ConfigCustomButton(*CustomButtonDefaultCallback)(); + + union DefaultCallback { + BoolDefaultCallback b; + IntDefaultCallback i; + Uint32DefaultCallback u; + Uint64DefaultCallback lu; + FloatDefaultCallback f; + StringDefaultCallback s; + PathDefaultCallback p; + TouchPosDefaultCallback touchPos; + CustomButtonDefaultCallback customButton; + }; + + ConfigSetting(const char *ini, bool *v, bool def, bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_BOOL), report_(false), save_(save), perGame_(perGame) { + ptr_.b = v; + cb_.b = nullptr; + default_.b = def; + } + + ConfigSetting(const char *ini, int *v, int def, bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_INT), report_(false), save_(save), perGame_(perGame) { + ptr_.i = v; + cb_.i = nullptr; + default_.i = def; + } + + ConfigSetting(const char *ini, int *v, int def, std::string(*transTo)(int), int (*transFrom)(const std::string &), bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_INT), report_(false), save_(save), perGame_(perGame), translateTo_(transTo), translateFrom_(transFrom) { + ptr_.i = v; + cb_.i = nullptr; + default_.i = def; + } + + ConfigSetting(const char *ini, uint32_t *v, uint32_t def, bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_UINT32), report_(false), save_(save), perGame_(perGame) { + ptr_.u = v; + cb_.u = nullptr; + default_.u = def; + } + + ConfigSetting(const char *ini, uint64_t *v, uint64_t def, bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_UINT64), report_(false), save_(save), perGame_(perGame) { + ptr_.lu = v; + cb_.lu = nullptr; + default_.lu = def; + } + + ConfigSetting(const char *ini, float *v, float def, bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_FLOAT), report_(false), save_(save), perGame_(perGame) { + ptr_.f = v; + cb_.f = nullptr; + default_.f = def; + } + + ConfigSetting(const char *ini, std::string *v, const char *def, bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_STRING), report_(false), save_(save), perGame_(perGame) { + ptr_.s = v; + cb_.s = nullptr; + default_.s = def; + } + + ConfigSetting(const char *ini, Path *p, const char *def, bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_PATH), report_(false), save_(save), perGame_(perGame) { + ptr_.p = p; + cb_.p = nullptr; + default_.p = def; + } + + ConfigSetting(const char *iniX, const char *iniY, const char *iniScale, const char *iniShow, ConfigTouchPos *v, ConfigTouchPos def, bool save = true, bool perGame = false) + : iniKey_(iniX), ini2_(iniY), ini3_(iniScale), ini4_(iniShow), type_(TYPE_TOUCH_POS), report_(false), save_(save), perGame_(perGame) { + ptr_.touchPos = v; + cb_.touchPos = nullptr; + default_.touchPos = def; + } + + ConfigSetting(const char *iniKey, const char *iniImage, const char *iniShape, const char *iniToggle, const char *iniRepeat, ConfigCustomButton *v, ConfigCustomButton def, bool save = true, bool perGame = false) + : iniKey_(iniKey), ini2_(iniImage), ini3_(iniShape), ini4_(iniToggle), ini5_(iniRepeat), type_(TYPE_CUSTOM_BUTTON), report_(false), save_(save), perGame_(perGame) { + ptr_.customButton = v; + cb_.customButton = nullptr; + default_.customButton = def; + } + + ConfigSetting(const char *ini, bool *v, BoolDefaultCallback def, bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_BOOL), report_(false), save_(save), perGame_(perGame) { + ptr_.b = v; + cb_.b = def; + } + + ConfigSetting(const char *ini, int *v, IntDefaultCallback def, bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_INT), report_(false), save_(save), perGame_(perGame) { + ptr_.i = v; + cb_.i = def; + } + + ConfigSetting(const char *ini, int *v, IntDefaultCallback def, std::string(*transTo)(int), int(*transFrom)(const std::string &), bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_INT), report_(false), save_(save), perGame_(perGame), translateTo_(transTo), translateFrom_(transFrom) { + ptr_.i = v; + cb_.i = def; + } + + ConfigSetting(const char *ini, uint32_t *v, Uint32DefaultCallback def, bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_UINT32), report_(false), save_(save), perGame_(perGame) { + ptr_.u = v; + cb_.u = def; + } + + ConfigSetting(const char *ini, float *v, FloatDefaultCallback def, bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_FLOAT), report_(false), save_(save), perGame_(perGame) { + ptr_.f = v; + cb_.f = def; + } + + ConfigSetting(const char *ini, std::string *v, StringDefaultCallback def, bool save = true, bool perGame = false) + : iniKey_(ini), type_(TYPE_STRING), report_(false), save_(save), perGame_(perGame) { + ptr_.s = v; + cb_.s = def; + } + + ConfigSetting(const char *iniX, const char *iniY, const char *iniScale, const char *iniShow, ConfigTouchPos *v, TouchPosDefaultCallback def, bool save = true, bool perGame = false) + : iniKey_(iniX), ini2_(iniY), ini3_(iniScale), ini4_(iniShow), type_(TYPE_TOUCH_POS), report_(false), save_(save), perGame_(perGame) { + ptr_.touchPos = v; + cb_.touchPos = def; + } + + // Should actually be called ReadFromIni or something. + bool Get(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 Set(Section *section) const; + + void RestoreToDefault() const; + + void Report(UrlEncoder &data, const std::string &prefix) const; + + const char *iniKey_ = nullptr; + const char *ini2_ = nullptr; + const char *ini3_ = nullptr; + const char *ini4_ = nullptr; + const char *ini5_ = nullptr; + + const Type type_; + + bool report_; + const bool save_; + const bool perGame_; + SettingPtr ptr_; + DefaultValue default_{}; + DefaultCallback cb_; + + // We only support transform for ints. + std::string(*translateTo_)(int) = nullptr; + int(*translateFrom_)(const std::string &) = nullptr; +}; + +struct ReportedConfigSetting : public ConfigSetting { + template + ReportedConfigSetting(const char *ini, T1 *v, T2 def, bool save = true, bool perGame = false) + : ConfigSetting(ini, v, def, save, perGame) { + report_ = true; + } + + template + ReportedConfigSetting(const char *ini, T1 *v, T2 def, std::string(*transTo)(int), int(*transFrom)(const std::string &), bool save = true, bool perGame = false) + : ConfigSetting(ini, v, def, transTo, transFrom, save, perGame) { + report_ = true; + } +}; diff --git a/Core/ConfigValues.h b/Core/ConfigValues.h index 96690ecc5d..300b183e5f 100644 --- a/Core/ConfigValues.h +++ b/Core/ConfigValues.h @@ -31,6 +31,22 @@ const int PSP_DEFAULT_FIRMWARE = 660; static const int8_t VOLUME_OFF = 0; static const int8_t VOLUME_FULL = 10; +struct ConfigTouchPos { + float x; + float y; + float scale; + // Note: Show is not used for all settings. + bool show; +}; + +struct ConfigCustomButton { + uint64_t key; + int image; + int shape; + bool toggle; + bool repeat; +}; + enum class CPUCore { INTERPRETER = 0, JIT = 1, diff --git a/Core/Core.vcxproj b/Core/Core.vcxproj index 9e534809f2..89454fd9e7 100644 --- a/Core/Core.vcxproj +++ b/Core/Core.vcxproj @@ -540,6 +540,7 @@ + @@ -1110,6 +1111,7 @@ + diff --git a/Core/Core.vcxproj.filters b/Core/Core.vcxproj.filters index 0789b4cdba..cefdfb01cb 100644 --- a/Core/Core.vcxproj.filters +++ b/Core/Core.vcxproj.filters @@ -1195,6 +1195,9 @@ MIPS + + Core + @@ -1932,6 +1935,9 @@ MIPS + + Core +