2013-06-17 19:01:03 +00:00
|
|
|
#include "Settings.h"
|
|
|
|
#include "Log.h"
|
2013-06-19 21:02:42 +00:00
|
|
|
#include "pugiXML/pugixml.hpp"
|
|
|
|
#include "platform.h"
|
|
|
|
#include <boost/filesystem.hpp>
|
2013-09-19 23:41:14 +00:00
|
|
|
#include "scrapers/GamesDBScraper.h"
|
2013-06-17 19:01:03 +00:00
|
|
|
|
|
|
|
Settings* Settings::sInstance = NULL;
|
|
|
|
|
2013-10-06 02:56:06 +00:00
|
|
|
Settings::Settings()
|
2013-06-17 19:01:03 +00:00
|
|
|
{
|
|
|
|
setDefaults();
|
2013-06-19 21:02:42 +00:00
|
|
|
loadFile();
|
2013-06-17 19:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Settings* Settings::getInstance()
|
|
|
|
{
|
|
|
|
if(sInstance == NULL)
|
|
|
|
sInstance = new Settings();
|
|
|
|
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::setDefaults()
|
|
|
|
{
|
|
|
|
mBoolMap.clear();
|
|
|
|
mIntMap.clear();
|
|
|
|
|
2014-03-08 19:00:18 +00:00
|
|
|
mBoolMap["ParseGamelistOnly"] = false;
|
|
|
|
mBoolMap["DrawFramerate"] = false;
|
|
|
|
mBoolMap["ShowExit"] = true;
|
|
|
|
mBoolMap["Debug"] = false;
|
|
|
|
mBoolMap["Windowed"] = false;
|
|
|
|
mBoolMap["EnableSounds"] = true;
|
|
|
|
mBoolMap["ShowHelpPrompts"] = true;
|
2013-10-16 22:05:02 +00:00
|
|
|
mBoolMap["ScrapeRatings"] = true;
|
2013-06-17 19:01:03 +00:00
|
|
|
|
2014-03-08 19:00:18 +00:00
|
|
|
mIntMap["DimTime"] = 120*1000;
|
2013-10-16 22:05:02 +00:00
|
|
|
mIntMap["ScraperResizeWidth"] = 400;
|
2013-10-05 20:28:59 +00:00
|
|
|
mIntMap["ScraperResizeHeight"] = 0;
|
2013-07-02 21:14:33 +00:00
|
|
|
|
2013-09-17 21:50:49 +00:00
|
|
|
mIntMap["GameListSortIndex"] = 0;
|
|
|
|
|
2014-03-22 16:44:57 +00:00
|
|
|
mStringMap["TransitionStyle"] = "fade";
|
|
|
|
|
2013-10-06 02:56:06 +00:00
|
|
|
mScraper = std::shared_ptr<Scraper>(new GamesDBScraper());
|
2013-06-17 19:01:03 +00:00
|
|
|
}
|
|
|
|
|
2013-06-19 21:02:42 +00:00
|
|
|
template <typename K, typename V>
|
|
|
|
void saveMap(pugi::xml_document& doc, std::map<K, V>& map, const char* type)
|
|
|
|
{
|
|
|
|
for(auto iter = map.begin(); iter != map.end(); iter++)
|
|
|
|
{
|
|
|
|
pugi::xml_node node = doc.append_child(type);
|
|
|
|
node.append_attribute("name").set_value(iter->first.c_str());
|
|
|
|
node.append_attribute("value").set_value(iter->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::saveFile()
|
|
|
|
{
|
|
|
|
const std::string path = getHomePath() + "/.emulationstation/es_settings.cfg";
|
|
|
|
|
|
|
|
pugi::xml_document doc;
|
|
|
|
|
|
|
|
saveMap<std::string, bool>(doc, mBoolMap, "bool");
|
|
|
|
saveMap<std::string, int>(doc, mIntMap, "int");
|
|
|
|
saveMap<std::string, float>(doc, mFloatMap, "float");
|
|
|
|
|
2014-03-22 16:44:57 +00:00
|
|
|
//saveMap<std::string, std::string>(doc, mStringMap, "string");
|
|
|
|
for(auto iter = mStringMap.begin(); iter != mStringMap.end(); iter++)
|
|
|
|
{
|
|
|
|
pugi::xml_node node = doc.append_child("string");
|
|
|
|
node.append_attribute("name").set_value(iter->first.c_str());
|
|
|
|
node.append_attribute("value").set_value(iter->second.c_str());
|
|
|
|
}
|
|
|
|
|
2013-10-06 02:56:06 +00:00
|
|
|
pugi::xml_node scraperNode = doc.append_child("scraper");
|
|
|
|
scraperNode.append_attribute("value").set_value(mScraper->getName());
|
|
|
|
|
2013-06-19 21:02:42 +00:00
|
|
|
doc.save_file(path.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::loadFile()
|
|
|
|
{
|
|
|
|
const std::string path = getHomePath() + "/.emulationstation/es_settings.cfg";
|
|
|
|
|
|
|
|
if(!boost::filesystem::exists(path))
|
|
|
|
return;
|
|
|
|
|
|
|
|
pugi::xml_document doc;
|
|
|
|
pugi::xml_parse_result result = doc.load_file(path.c_str());
|
|
|
|
if(!result)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Could not parse Settings file!\n " << result.description();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-16 23:20:21 +00:00
|
|
|
for(pugi::xml_node node = doc.child("bool"); node; node = node.next_sibling("bool"))
|
2013-06-19 21:02:42 +00:00
|
|
|
setBool(node.attribute("name").as_string(), node.attribute("value").as_bool());
|
2013-10-16 23:20:21 +00:00
|
|
|
for(pugi::xml_node node = doc.child("int"); node; node = node.next_sibling("int"))
|
2013-06-19 21:02:42 +00:00
|
|
|
setInt(node.attribute("name").as_string(), node.attribute("value").as_int());
|
2013-10-16 23:20:21 +00:00
|
|
|
for(pugi::xml_node node = doc.child("float"); node; node = node.next_sibling("float"))
|
2013-06-19 21:02:42 +00:00
|
|
|
setFloat(node.attribute("name").as_string(), node.attribute("value").as_float());
|
2014-03-22 16:44:57 +00:00
|
|
|
for(pugi::xml_node node = doc.child("string"); node; node = node.next_sibling("string"))
|
|
|
|
setString(node.attribute("name").as_string(), node.attribute("value").as_string());
|
2013-10-06 02:56:06 +00:00
|
|
|
|
|
|
|
if(doc.child("scraper"))
|
|
|
|
{
|
|
|
|
std::shared_ptr<Scraper> scr = createScraperByName(doc.child("scraper").attribute("value").as_string());
|
|
|
|
if(scr)
|
|
|
|
mScraper = scr;
|
|
|
|
}
|
2013-06-19 21:02:42 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 02:56:06 +00:00
|
|
|
std::shared_ptr<Scraper> Settings::getScraper()
|
2013-09-17 21:50:49 +00:00
|
|
|
{
|
|
|
|
return mScraper;
|
|
|
|
}
|
|
|
|
|
2013-10-06 02:56:06 +00:00
|
|
|
void Settings::setScraper(std::shared_ptr<Scraper> scraper)
|
|
|
|
{
|
|
|
|
mScraper = scraper;
|
|
|
|
}
|
|
|
|
|
2013-06-17 19:01:03 +00:00
|
|
|
//Print a warning message if the setting we're trying to get doesn't already exist in the map, then return the value in the map.
|
2013-06-19 23:31:59 +00:00
|
|
|
#define SETTINGS_GETSET(type, mapName, getMethodName, setMethodName) type Settings::getMethodName(const std::string& name) \
|
2013-06-17 19:01:03 +00:00
|
|
|
{ \
|
|
|
|
if(mapName.find(name) == mapName.end()) \
|
|
|
|
{ \
|
|
|
|
LOG(LogError) << "Tried to use unset setting " << name << "!"; \
|
|
|
|
} \
|
|
|
|
return mapName[name]; \
|
2013-06-19 21:02:42 +00:00
|
|
|
} \
|
2013-06-19 23:31:59 +00:00
|
|
|
void Settings::setMethodName(const std::string& name, type value) \
|
2013-06-19 21:02:42 +00:00
|
|
|
{ \
|
|
|
|
mapName[name] = value; \
|
2013-06-17 19:01:03 +00:00
|
|
|
}
|
|
|
|
|
2013-06-19 21:02:42 +00:00
|
|
|
SETTINGS_GETSET(bool, mBoolMap, getBool, setBool);
|
|
|
|
SETTINGS_GETSET(int, mIntMap, getInt, setInt);
|
|
|
|
SETTINGS_GETSET(float, mFloatMap, getFloat, setFloat);
|
2014-03-22 16:44:57 +00:00
|
|
|
SETTINGS_GETSET(const std::string&, mStringMap, getString, setString);
|