mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-04-30 05:47:57 +03:00
Imported Upstream version 0.26.0
This commit is contained in:
commit
9a2b6c69b6
1398 changed files with 212217 additions and 0 deletions
169
components/settings/settings.cpp
Normal file
169
components/settings/settings.cpp
Normal file
|
@ -0,0 +1,169 @@
|
|||
#include "settings.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <OgreResourceGroupManager.h>
|
||||
#include <OgreStringConverter.h>
|
||||
|
||||
using namespace Settings;
|
||||
|
||||
Ogre::ConfigFile Manager::mFile = Ogre::ConfigFile();
|
||||
Ogre::ConfigFile Manager::mDefaultFile = Ogre::ConfigFile();
|
||||
CategorySettingVector Manager::mChangedSettings = CategorySettingVector();
|
||||
CategorySettingValueMap Manager::mNewSettings = CategorySettingValueMap();
|
||||
|
||||
void Manager::loadUser (const std::string& file)
|
||||
{
|
||||
mFile.load(file);
|
||||
}
|
||||
|
||||
void Manager::loadDefault (const std::string& file)
|
||||
{
|
||||
mDefaultFile.load(file);
|
||||
}
|
||||
|
||||
void Manager::saveUser(const std::string& file)
|
||||
{
|
||||
std::fstream fout(file.c_str(), std::ios::out);
|
||||
|
||||
Ogre::ConfigFile::SectionIterator seci = mFile.getSectionIterator();
|
||||
|
||||
while (seci.hasMoreElements())
|
||||
{
|
||||
Ogre::String sectionName = seci.peekNextKey();
|
||||
|
||||
if (sectionName.length() > 0)
|
||||
fout << '\n' << '[' << seci.peekNextKey() << ']' << '\n';
|
||||
|
||||
Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
|
||||
Ogre::ConfigFile::SettingsMultiMap::iterator i;
|
||||
for (i = settings->begin(); i != settings->end(); ++i)
|
||||
{
|
||||
fout << i->first.c_str() << " = " << i->second.c_str() << '\n';
|
||||
}
|
||||
|
||||
CategorySettingValueMap::iterator it = mNewSettings.begin();
|
||||
while (it != mNewSettings.end())
|
||||
{
|
||||
if (it->first.first == sectionName)
|
||||
{
|
||||
fout << it->first.second << " = " << it->second << '\n';
|
||||
mNewSettings.erase(it++);
|
||||
}
|
||||
else
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
std::string category = "";
|
||||
for (CategorySettingValueMap::iterator it = mNewSettings.begin();
|
||||
it != mNewSettings.end(); ++it)
|
||||
{
|
||||
if (category != it->first.first)
|
||||
{
|
||||
category = it->first.first;
|
||||
fout << '\n' << '[' << category << ']' << '\n';
|
||||
}
|
||||
fout << it->first.second << " = " << it->second << '\n';
|
||||
}
|
||||
|
||||
fout.close();
|
||||
}
|
||||
|
||||
const std::string Manager::getString (const std::string& setting, const std::string& category)
|
||||
{
|
||||
if (mNewSettings.find(std::make_pair(category, setting)) != mNewSettings.end())
|
||||
return mNewSettings[std::make_pair(category, setting)];
|
||||
|
||||
std::string defaultval = mDefaultFile.getSetting(setting, category, "NOTFOUND");
|
||||
std::string val = mFile.getSetting(setting, category, defaultval);
|
||||
|
||||
if (val == "NOTFOUND")
|
||||
throw std::runtime_error("Trying to retrieve a non-existing setting: " + setting + " Make sure the settings-default.cfg file was properly installed.");
|
||||
return val;
|
||||
}
|
||||
|
||||
const float Manager::getFloat (const std::string& setting, const std::string& category)
|
||||
{
|
||||
return Ogre::StringConverter::parseReal( getString(setting, category) );
|
||||
}
|
||||
|
||||
const int Manager::getInt (const std::string& setting, const std::string& category)
|
||||
{
|
||||
return Ogre::StringConverter::parseInt( getString(setting, category) );
|
||||
}
|
||||
|
||||
const bool Manager::getBool (const std::string& setting, const std::string& category)
|
||||
{
|
||||
return Ogre::StringConverter::parseBool( getString(setting, category) );
|
||||
}
|
||||
|
||||
void Manager::setString (const std::string& setting, const std::string& category, const std::string& value)
|
||||
{
|
||||
CategorySetting s = std::make_pair(category, setting);
|
||||
|
||||
bool found=false;
|
||||
try
|
||||
{
|
||||
Ogre::ConfigFile::SettingsIterator it = mFile.getSettingsIterator(category);
|
||||
while (it.hasMoreElements())
|
||||
{
|
||||
Ogre::ConfigFile::SettingsMultiMap::iterator i = it.current();
|
||||
|
||||
if ((*i).first == setting)
|
||||
{
|
||||
if ((*i).second != value)
|
||||
{
|
||||
mChangedSettings.push_back(std::make_pair(category, setting));
|
||||
(*i).second = value;
|
||||
}
|
||||
found = true;
|
||||
}
|
||||
|
||||
it.getNext();
|
||||
}
|
||||
}
|
||||
catch (Ogre::Exception&)
|
||||
{}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
if (mNewSettings.find(s) != mNewSettings.end())
|
||||
{
|
||||
if (mNewSettings[s] != value)
|
||||
{
|
||||
mChangedSettings.push_back(std::make_pair(category, setting));
|
||||
mNewSettings[s] = value;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mDefaultFile.getSetting(setting, category) != value)
|
||||
mChangedSettings.push_back(std::make_pair(category, setting));
|
||||
mNewSettings[s] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Manager::setInt (const std::string& setting, const std::string& category, const int value)
|
||||
{
|
||||
setString(setting, category, Ogre::StringConverter::toString(value));
|
||||
}
|
||||
|
||||
void Manager::setFloat (const std::string& setting, const std::string& category, const float value)
|
||||
{
|
||||
setString(setting, category, Ogre::StringConverter::toString(value));
|
||||
}
|
||||
|
||||
void Manager::setBool (const std::string& setting, const std::string& category, const bool value)
|
||||
{
|
||||
setString(setting, category, Ogre::StringConverter::toString(value));
|
||||
}
|
||||
|
||||
const CategorySettingVector Manager::apply()
|
||||
{
|
||||
CategorySettingVector vec = mChangedSettings;
|
||||
mChangedSettings.clear();
|
||||
return vec;
|
||||
}
|
52
components/settings/settings.hpp
Normal file
52
components/settings/settings.hpp
Normal file
|
@ -0,0 +1,52 @@
|
|||
#ifndef _COMPONENTS_SETTINGS_H
|
||||
#define _COMPONENTS_SETTINGS_H
|
||||
|
||||
#include <OgreConfigFile.h>
|
||||
|
||||
namespace Settings
|
||||
{
|
||||
typedef std::pair < std::string, std::string > CategorySetting;
|
||||
typedef std::vector< std::pair<std::string, std::string> > CategorySettingVector;
|
||||
typedef std::map < CategorySetting, std::string > CategorySettingValueMap;
|
||||
|
||||
///
|
||||
/// \brief Settings management (can change during runtime)
|
||||
///
|
||||
class Manager
|
||||
{
|
||||
public:
|
||||
static Ogre::ConfigFile mFile;
|
||||
static Ogre::ConfigFile mDefaultFile;
|
||||
|
||||
static CategorySettingVector mChangedSettings;
|
||||
///< tracks all the settings that were changed since the last apply() call
|
||||
|
||||
static CategorySettingValueMap mNewSettings;
|
||||
///< tracks all the settings that are in the default file, but not in user file yet
|
||||
|
||||
void loadDefault (const std::string& file);
|
||||
///< load file as the default settings (can be overridden by user settings)
|
||||
|
||||
void loadUser (const std::string& file);
|
||||
///< load file as user settings
|
||||
|
||||
void saveUser (const std::string& file);
|
||||
///< save user settings to file
|
||||
|
||||
static const CategorySettingVector apply();
|
||||
///< returns the list of changed settings and then clears it
|
||||
|
||||
static const int getInt (const std::string& setting, const std::string& category);
|
||||
static const float getFloat (const std::string& setting, const std::string& category);
|
||||
static const std::string getString (const std::string& setting, const std::string& category);
|
||||
static const bool getBool (const std::string& setting, const std::string& category);
|
||||
|
||||
static void setInt (const std::string& setting, const std::string& category, const int value);
|
||||
static void setFloat (const std::string& setting, const std::string& category, const float value);
|
||||
static void setString (const std::string& setting, const std::string& category, const std::string& value);
|
||||
static void setBool (const std::string& setting, const std::string& category, const bool value);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // _COMPONENTS_SETTINGS_H
|
Loading…
Add table
Add a link
Reference in a new issue