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() bool Launcher::MainDialog::setupGraphicsSettings()
{ {
// This method is almost a copy of OMW::Engine::loadSettings(). They should definitely mEngineSettings.clear(); // Ensure to clear previous settings in case we had already loaded settings.
// remain consistent, and possibly be merged into a shared component. At the very least try
// the filenames should be in the CfgMgr component. {
boost::program_options::variables_map variables;
// Ensure to clear previous settings in case we had already loaded settings. boost::program_options::options_description desc;
mEngineSettings.clear(); mCfgMgr.addCommonOptions(desc);
mCfgMgr.readConfiguration(variables, desc, true);
// Create the settings manager and load default settings file mEngineSettings.load(mCfgMgr);
const std::string localDefault = (mCfgMgr.getLocalPath() / "defaults.bin").string(); return true;
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);
} }
catch (std::exception& e) { 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())); 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 false;
} }
return true;
} }
void Launcher::MainDialog::loadSettings() void Launcher::MainDialog::loadSettings()

@ -89,23 +89,6 @@ namespace NavMeshTool
return result; 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[]) int runNavMeshTool(int argc, char *argv[])
{ {
bpo::options_description desc = makeOptionsDescription(); bpo::options_description desc = makeOptionsDescription();
@ -166,7 +149,7 @@ namespace NavMeshTool
VFS::registerArchives(&vfs, fileCollections, archives, true); VFS::registerArchives(&vfs, fileCollections, archives, true);
Settings::Manager settings; Settings::Manager settings;
loadSettings(config, settings); settings.load(config);
const osg::Vec3f agentHalfExtents = Settings::Manager::getVector3("default actor pathfind half extents", "Game"); const osg::Vec3f agentHalfExtents = Settings::Manager::getVector3("default actor pathfind half extents", "Game");

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

@ -40,6 +40,7 @@ namespace CS
Q_OBJECT Q_OBJECT
Files::ConfigurationManager mCfgMgr; Files::ConfigurationManager mCfgMgr;
boost::program_options::variables_map mConfigVariables;
CSMPrefs::State mSettingsState; CSMPrefs::State mSettingsState;
CSMDoc::DocumentManager mDocumentManager; CSMDoc::DocumentManager mDocumentManager;
CSVDoc::StartupDialogue mStartup; CSVDoc::StartupDialogue mStartup;
@ -58,6 +59,8 @@ namespace CS
Files::PathContainer mDataDirs; Files::PathContainer mDataDirs;
std::string mEncodingName; 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); std::pair<Files::PathContainer, std::vector<std::string> > readConfig(bool quiet=false);
///< \return data paths ///< \return data paths

@ -16,22 +16,7 @@ CSMPrefs::State *CSMPrefs::State::sThis = nullptr;
void CSMPrefs::State::load() void CSMPrefs::State::load()
{ {
// default settings file mSettings.load(mConfigurationManager);
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());
} }
void CSMPrefs::State::declare() void CSMPrefs::State::declare()

@ -518,34 +518,6 @@ void OMW::Engine::setSkipMenu (bool skipMenu, bool newGame)
mNewGame = 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) void OMW::Engine::createWindow(Settings::Manager& settings)
{ {
int screen = settings.getInt("screen", "Video"); int screen = settings.getInt("screen", "Video");
@ -981,8 +953,7 @@ void OMW::Engine::go()
// Load settings // Load settings
Settings::Manager settings; Settings::Manager settings;
std::string settingspath; std::string settingspath = settings.load(mCfgMgr);
settingspath = loadSettings (settings);
MWClass::registerClasses(); MWClass::registerClasses();

@ -3,6 +3,7 @@
#include <sstream> #include <sstream>
#include <components/files/configurationmanager.hpp>
#include <components/misc/stringops.hpp> #include <components/misc/stringops.hpp>
namespace Settings namespace Settings
@ -19,16 +20,33 @@ void Manager::clear()
mChangedSettings.clear(); mChangedSettings.clear();
} }
void Manager::loadDefault(const std::string &file, bool overrideExisting) std::string Manager::load(const Files::ConfigurationManager& cfgMgr)
{ {
SettingsFileParser parser; 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) // 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();
SettingsFileParser parser; if (boost::filesystem::exists(settingspath))
parser.loadSettingsFile(file, mUserSettings); parser.loadSettingsFile(settingspath, mUserSettings, false, false);
return settingspath;
} }
void Manager::saveUser(const std::string &file) void Manager::saveUser(const std::string &file)

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

Loading…
Cancel
Save