CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/ConfigSettings.cpp
Views: 1401
#include "Common/Data/Format/IniFile.h"1#include "Common/Net/URL.h"2#include "Common/Log.h"34#include "Core/ConfigSettings.h"5#include "Core/ConfigValues.h"6#include "Core/Config.h"78std::unordered_map<void*, ConfigSetting*>& ConfigSetting::getPtrLUT() {9static std::unordered_map<void*, ConfigSetting*> lut;10return lut;11}1213bool ConfigSetting::perGame(void *ptr) {14return g_Config.bGameSpecific && getPtrLUT().count(ptr) > 0 && getPtrLUT()[ptr]->PerGame();15}1617bool ConfigSetting::Get(const Section *section) const {18switch (type_) {19case TYPE_BOOL:20return section->Get(iniKey_, ptr_.b, cb_.b ? cb_.b() : default_.b);2122case TYPE_INT:23if (translateFrom_) {24std::string value;25if (section->Get(iniKey_, &value, nullptr)) {26*ptr_.i = translateFrom_(value);27return true;28}29}30return section->Get(iniKey_, ptr_.i, cb_.i ? cb_.i() : default_.i);31case TYPE_UINT32:32return section->Get(iniKey_, ptr_.u, cb_.u ? cb_.u() : default_.u);33case TYPE_UINT64:34return section->Get(iniKey_, ptr_.lu, cb_.lu ? cb_.lu() : default_.lu);35case TYPE_FLOAT:36return section->Get(iniKey_, ptr_.f, cb_.f ? cb_.f() : default_.f);37case TYPE_STRING:38return section->Get(iniKey_, ptr_.s, cb_.s ? cb_.s() : default_.s);39case TYPE_TOUCH_POS:40{41ConfigTouchPos defaultTouchPos = cb_.touchPos ? cb_.touchPos() : default_.touchPos;42section->Get(iniKey_, &ptr_.touchPos->x, defaultTouchPos.x);43section->Get(ini2_, &ptr_.touchPos->y, defaultTouchPos.y);44section->Get(ini3_, &ptr_.touchPos->scale, defaultTouchPos.scale);45if (ini4_) {46section->Get(ini4_, &ptr_.touchPos->show, defaultTouchPos.show);47} else {48ptr_.touchPos->show = defaultTouchPos.show;49}50return true;51}52case TYPE_PATH:53{54std::string tmp;55bool result = section->Get(iniKey_, &tmp, cb_.p ? cb_.p() : default_.p);56if (result) {57*ptr_.p = Path(tmp);58}59return result;60}61case TYPE_CUSTOM_BUTTON:62{63ConfigCustomButton defaultCustomButton = cb_.customButton ? cb_.customButton() : default_.customButton;64section->Get(iniKey_, &ptr_.customButton->key, defaultCustomButton.key);65section->Get(ini2_, &ptr_.customButton->image, defaultCustomButton.image);66section->Get(ini3_, &ptr_.customButton->shape, defaultCustomButton.shape);67section->Get(ini4_, &ptr_.customButton->toggle, defaultCustomButton.toggle);68section->Get(ini5_, &ptr_.customButton->repeat, defaultCustomButton.repeat);69return true;70}71default:72_dbg_assert_msg_(false, "Get(%s): Unexpected ini setting type: %d", iniKey_, (int)type_);73return false;74}75}7677void ConfigSetting::Set(Section *section) const {78if (!SaveSetting()) {79return;80}8182switch (type_) {83case TYPE_BOOL:84return section->Set(iniKey_, *ptr_.b);85case TYPE_INT:86if (translateTo_) {87std::string value = translateTo_(*ptr_.i);88return section->Set(iniKey_, value);89}90return section->Set(iniKey_, *ptr_.i);91case TYPE_UINT32:92return section->Set(iniKey_, *ptr_.u);93case TYPE_UINT64:94return section->Set(iniKey_, *ptr_.lu);95case TYPE_FLOAT:96return section->Set(iniKey_, *ptr_.f);97case TYPE_STRING:98return section->Set(iniKey_, *ptr_.s);99case TYPE_PATH:100return section->Set(iniKey_, ptr_.p->ToString());101case TYPE_TOUCH_POS:102section->Set(iniKey_, ptr_.touchPos->x);103section->Set(ini2_, ptr_.touchPos->y);104section->Set(ini3_, ptr_.touchPos->scale);105if (ini4_) {106section->Set(ini4_, ptr_.touchPos->show);107}108return;109case TYPE_CUSTOM_BUTTON:110section->Set(iniKey_, ptr_.customButton->key);111section->Set(ini2_, ptr_.customButton->image);112section->Set(ini3_, ptr_.customButton->shape);113section->Set(ini4_, ptr_.customButton->toggle);114section->Set(ini5_, ptr_.customButton->repeat);115return;116default:117_dbg_assert_msg_(false, "Set(%s): Unexpected ini setting type: %d", iniKey_, (int)type_);118return;119}120}121122void ConfigSetting::RestoreToDefault() const {123switch (type_) {124case TYPE_BOOL: *ptr_.b = cb_.b ? cb_.b() : default_.b; break;125case TYPE_INT: *ptr_.i = cb_.i ? cb_.i() : default_.i; break;126case TYPE_UINT32: *ptr_.u = cb_.u ? cb_.u() : default_.u; break;127case TYPE_UINT64: *ptr_.lu = cb_.lu ? cb_.lu() : default_.lu; break;128case TYPE_FLOAT: *ptr_.f = cb_.f ? cb_.f() : default_.f; break;129case TYPE_STRING: *ptr_.s = cb_.s ? cb_.s() : default_.s; break;130case TYPE_TOUCH_POS: *ptr_.touchPos = cb_.touchPos ? cb_.touchPos() : default_.touchPos; break;131case TYPE_PATH: *ptr_.p = Path(cb_.p ? cb_.p() : default_.p); break;132case TYPE_CUSTOM_BUTTON: *ptr_.customButton = cb_.customButton ? cb_.customButton() : default_.customButton; break;133default:134_dbg_assert_msg_(false, "RestoreToDefault(%s): Unexpected ini setting type: %d", iniKey_, (int)type_);135}136}137138void ConfigSetting::ReportSetting(UrlEncoder &data, const std::string &prefix) const {139if (!Report())140return;141142switch (type_) {143case TYPE_BOOL: return data.Add(prefix + iniKey_, *ptr_.b);144case TYPE_INT: return data.Add(prefix + iniKey_, *ptr_.i);145case TYPE_UINT32: return data.Add(prefix + iniKey_, *ptr_.u);146case TYPE_UINT64: return data.Add(prefix + iniKey_, *ptr_.lu);147case TYPE_FLOAT: return data.Add(prefix + iniKey_, *ptr_.f);148case TYPE_STRING: return data.Add(prefix + iniKey_, *ptr_.s);149case TYPE_PATH: return data.Add(prefix + iniKey_, ptr_.p->ToString());150case TYPE_TOUCH_POS: return; // Doesn't report.151case TYPE_CUSTOM_BUTTON: return; // Doesn't report.152default:153_dbg_assert_msg_(false, "Report(%s): Unexpected ini setting type: %d", iniKey_, (int)type_);154return;155}156}157158159