Reuse the same code to load settings in apps/openmw, apps/launcher, apps/opencs

C++20
Petr Mikheev 3 years ago
parent 1bcc4a8bcc
commit a453e5c198

@ -414,57 +414,23 @@ bool Launcher::MainDialog::setupGameData()
bool Launcher::MainDialog::setupGraphicsSettings()
{
// This method is almost a copy of OMW::Engine::loadSettings(). They should definitely
// remain consistent, and possibly be merged into a shared component. At the very least
// the filenames should be in the CfgMgr component.
// Ensure to clear previous settings in case we had already loaded settings.
mEngineSettings.clear();
// Create the settings manager and load default settings file
const std::string localDefault = (mCfgMgr.getLocalPath() / "defaults.bin").string();
const std::string globalDefault = (mCfgMgr.getGlobalPath() / "defaults.bin").string();
std::string defaultPath;
// Prefer the defaults.bin in the current directory.
if (boost::filesystem::exists(localDefault))
defaultPath = localDefault;
else if (boost::filesystem::exists(globalDefault))
defaultPath = globalDefault;
// Something's very wrong if we can't find the file at all.
else {
cfgError(tr("Error reading OpenMW configuration file"),
tr("<br><b>Could not find defaults.bin</b><br><br> \
The problem may be due to an incomplete installation of OpenMW.<br> \
Reinstalling OpenMW may resolve the problem."));
return false;
}
// Load the default settings, report any parsing errors.
try {
mEngineSettings.loadDefault(defaultPath);
}
catch (std::exception& e) {
std::string msg = std::string("<br><b>Error reading defaults.bin</b><br><br>") + e.what();
cfgError(tr("Error reading OpenMW configuration file"), tr(msg.c_str()));
return false;
}
// Load user settings if they exist
const std::string userPath = (mCfgMgr.getUserConfigPath() / "settings.cfg").string();
// User settings are not required to exist, so if they don't we're done.
if (!boost::filesystem::exists(userPath)) return true;
try {
mEngineSettings.loadUser(userPath);
mEngineSettings.clear(); // Ensure to clear previous settings in case we had already loaded settings.
try
{
boost::program_options::variables_map variables;
boost::program_options::options_description desc;
mCfgMgr.addCommonOptions(desc);
mCfgMgr.readConfiguration(variables, desc, true);
mEngineSettings.load(mCfgMgr);
return true;
}
catch (std::exception& e) {
std::string msg = std::string("<br><b>Error reading settings.cfg</b><br><br>") + e.what();
cfgError(tr("Error reading OpenMW configuration file"), tr(msg.c_str()));
catch (std::exception& e)
{
cfgError(tr("Error reading OpenMW configuration files"),
tr("<br>The problem may be due to an incomplete installation of OpenMW.<br> \
Reinstalling OpenMW may resolve the problem.<br>") + e.what());
return false;
}
return true;
}
void Launcher::MainDialog::loadSettings()

@ -89,23 +89,6 @@ namespace NavMeshTool
return result;
}
void loadSettings(const Files::ConfigurationManager& config, Settings::Manager& settings)
{
const std::string localDefault = (config.getLocalPath() / "defaults.bin").string();
const std::string globalDefault = (config.getGlobalPath() / "defaults.bin").string();
if (boost::filesystem::exists(localDefault))
settings.loadDefault(localDefault);
else if (boost::filesystem::exists(globalDefault))
settings.loadDefault(globalDefault);
else
throw std::runtime_error("No default settings file found! Make sure the file \"defaults.bin\" was properly installed.");
const std::string settingsPath = (config.getUserConfigPath() / "settings.cfg").string();
if (boost::filesystem::exists(settingsPath))
settings.loadUser(settingsPath);
}
int runNavMeshTool(int argc, char *argv[])
{
bpo::options_description desc = makeOptionsDescription();
@ -166,7 +149,7 @@ namespace NavMeshTool
VFS::registerArchives(&vfs, fileCollections, archives, true);
Settings::Manager settings;
loadSettings(config, settings);
settings.load(config);
const osg::Vec3f agentHalfExtents = Settings::Manager::getVector3("default actor pathfind half extents", "Game");

@ -21,7 +21,7 @@
using namespace Fallback;
CS::Editor::Editor (int argc, char **argv)
: mSettingsState (mCfgMgr), mDocumentManager (mCfgMgr),
: mConfigVariables(readConfiguration()), mSettingsState (mCfgMgr), mDocumentManager (mCfgMgr),
mPid(""), mLock(), mMerge (mDocumentManager),
mIpcServerName ("org.openmw.OpenCS"), mServer(nullptr), mClientSocket(nullptr)
{
@ -83,7 +83,7 @@ CS::Editor::~Editor ()
remove(mPid.string().c_str())); // ignore any error
}
std::pair<Files::PathContainer, std::vector<std::string> > CS::Editor::readConfig(bool quiet)
boost::program_options::variables_map CS::Editor::readConfiguration()
{
boost::program_options::variables_map variables;
boost::program_options::options_description desc("Syntax: openmw-cs <options>\nAllowed options");
@ -109,6 +109,13 @@ std::pair<Files::PathContainer, std::vector<std::string> > CS::Editor::readConfi
mCfgMgr.readConfiguration(variables, desc, false);
setupLogging(mCfgMgr.getLogPath().string(), "OpenMW-CS");
return variables;
}
std::pair<Files::PathContainer, std::vector<std::string> > CS::Editor::readConfig(bool quiet)
{
boost::program_options::variables_map& variables = mConfigVariables;
Fallback::Map::init(variables["fallback"].as<FallbackMap>().mMap);
mEncodingName = variables["encoding"].as<std::string>();

@ -40,6 +40,7 @@ namespace CS
Q_OBJECT
Files::ConfigurationManager mCfgMgr;
boost::program_options::variables_map mConfigVariables;
CSMPrefs::State mSettingsState;
CSMDoc::DocumentManager mDocumentManager;
CSVDoc::StartupDialogue mStartup;
@ -58,6 +59,8 @@ namespace CS
Files::PathContainer mDataDirs;
std::string mEncodingName;
boost::program_options::variables_map readConfiguration();
///< Calls mCfgMgr.readConfiguration; should be used before initialization of mSettingsState as it depends on the configuration.
std::pair<Files::PathContainer, std::vector<std::string> > readConfig(bool quiet=false);
///< \return data paths

@ -16,22 +16,7 @@ CSMPrefs::State *CSMPrefs::State::sThis = nullptr;
void CSMPrefs::State::load()
{
// default settings file
boost::filesystem::path local = mConfigurationManager.getLocalPath() / mDefaultConfigFile;
boost::filesystem::path global = mConfigurationManager.getGlobalPath() / mDefaultConfigFile;
if (boost::filesystem::exists (local))
mSettings.loadDefault (local.string());
else if (boost::filesystem::exists (global))
mSettings.loadDefault (global.string());
else
throw std::runtime_error ("No default settings file found! Make sure the file \"" + mDefaultConfigFile + "\" was properly installed.");
// user settings file
boost::filesystem::path user = mConfigurationManager.getUserConfigPath() / mConfigFile;
if (boost::filesystem::exists (user))
mSettings.loadUser (user.string());
mSettings.load(mConfigurationManager);
}
void CSMPrefs::State::declare()

@ -518,34 +518,6 @@ void OMW::Engine::setSkipMenu (bool skipMenu, bool newGame)
mNewGame = newGame;
}
std::string OMW::Engine::loadSettings (Settings::Manager & settings)
{
const std::vector<boost::filesystem::path>& paths = mCfgMgr.getActiveConfigPaths();
if (paths.empty())
throw std::runtime_error("No config dirs! ConfigurationManager::readConfiguration must be called first.");
// Create the settings manager and load default settings file.
const std::string defaultsBin = (paths.front() / "defaults.bin").string();
if (!boost::filesystem::exists(defaultsBin))
throw std::runtime_error ("No default settings file found! Make sure the file \"defaults.bin\" was properly installed.");
settings.loadDefault(defaultsBin);
// Load "settings.cfg" from every config dir except the last one as additional default settings.
for (int i = 0; i < static_cast<int>(paths.size()) - 1; ++i)
{
const std::string additionalDefaults = (paths[i] / "settings.cfg").string();
if (boost::filesystem::exists(additionalDefaults))
settings.loadDefault(additionalDefaults, true);
}
// Load "settings.cfg" from the last config as user settings if they exist. This path will be used to save modified settings.
std::string settingspath = (paths.back() / "settings.cfg").string();
if (boost::filesystem::exists(settingspath))
settings.loadUser(settingspath);
return settingspath;
}
void OMW::Engine::createWindow(Settings::Manager& settings)
{
int screen = settings.getInt("screen", "Video");
@ -981,8 +953,7 @@ void OMW::Engine::go()
// Load settings
Settings::Manager settings;
std::string settingspath;
settingspath = loadSettings (settings);
std::string settingspath = settings.load(mCfgMgr);
MWClass::registerClasses();

@ -3,6 +3,7 @@
#include <sstream>
#include <components/files/configurationmanager.hpp>
#include <components/misc/stringops.hpp>
namespace Settings
@ -19,16 +20,33 @@ void Manager::clear()
mChangedSettings.clear();
}
void Manager::loadDefault(const std::string &file, bool overrideExisting)
std::string Manager::load(const Files::ConfigurationManager& cfgMgr)
{
SettingsFileParser parser;
parser.loadSettingsFile(file, mDefaultSettings, true, overrideExisting);
}
const std::vector<boost::filesystem::path>& paths = cfgMgr.getActiveConfigPaths();
if (paths.empty())
throw std::runtime_error("No config dirs! ConfigurationManager::readConfiguration must be called first.");
// Create the settings manager and load default settings file.
const std::string defaultsBin = (paths.front() / "defaults.bin").string();
if (!boost::filesystem::exists(defaultsBin))
throw std::runtime_error ("No default settings file found! Make sure the file \"defaults.bin\" was properly installed.");
parser.loadSettingsFile(defaultsBin, mDefaultSettings, true, false);
// Load "settings.cfg" from every config dir except the last one as additional default settings.
for (int i = 0; i < static_cast<int>(paths.size()) - 1; ++i)
{
const std::string additionalDefaults = (paths[i] / "settings.cfg").string();
if (boost::filesystem::exists(additionalDefaults))
parser.loadSettingsFile(additionalDefaults, mDefaultSettings, false, true);
}
void Manager::loadUser(const std::string &file)
{
SettingsFileParser parser;
parser.loadSettingsFile(file, mUserSettings);
// Load "settings.cfg" from the last config as user settings if they exist. This path will be used to save modified settings.
std::string settingspath = (paths.back() / "settings.cfg").string();
if (boost::filesystem::exists(settingspath))
parser.loadSettingsFile(settingspath, mUserSettings, false, false);
return settingspath;
}
void Manager::saveUser(const std::string &file)

@ -9,6 +9,11 @@
#include <osg/Vec2f>
#include <osg/Vec3f>
namespace Files
{
struct ConfigurationManager;
}
namespace Settings
{
///
@ -26,11 +31,8 @@ namespace Settings
void clear();
///< clears all settings and default settings
void loadDefault (const std::string& file, bool overrideExisting = false);
///< load file as the default settings (can be overridden by user settings)
void loadUser (const std::string& file);
///< load file as user settings
std::string load(const Files::ConfigurationManager& cfgMgr);
///< load settings from all active config dirs. Returns the path of the last loaded file.
void saveUser (const std::string& file);
///< save user settings to file

Loading…
Cancel
Save