2012-01-21 00:14:35 +00:00
|
|
|
#include "configurationmanager.hpp"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2016-07-26 23:58:31 +00:00
|
|
|
#include <components/files/escape.hpp>
|
|
|
|
|
2012-02-23 22:01:42 +00:00
|
|
|
#include <boost/algorithm/string/erase.hpp>
|
2016-07-11 00:09:34 +00:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2014-01-19 07:34:54 +00:00
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2012-02-19 22:39:37 +00:00
|
|
|
/**
|
|
|
|
* \namespace Files
|
|
|
|
*/
|
2012-01-21 00:14:35 +00:00
|
|
|
namespace Files
|
|
|
|
{
|
|
|
|
|
|
|
|
static const char* const openmwCfgFile = "openmw.cfg";
|
|
|
|
|
2014-07-28 19:52:34 +00:00
|
|
|
#if defined(_WIN32) || defined(__WINDOWS__)
|
|
|
|
static const char* const applicationName = "OpenMW";
|
|
|
|
#else
|
|
|
|
static const char* const applicationName = "openmw";
|
|
|
|
#endif
|
|
|
|
|
2012-01-28 10:59:08 +00:00
|
|
|
const char* const localToken = "?local?";
|
2014-06-18 15:18:48 +00:00
|
|
|
const char* const userDataToken = "?userdata?";
|
2012-01-28 10:59:08 +00:00
|
|
|
const char* const globalToken = "?global?";
|
2012-01-21 00:14:35 +00:00
|
|
|
|
2015-01-30 21:11:26 +00:00
|
|
|
ConfigurationManager::ConfigurationManager(bool silent)
|
2014-07-28 19:52:34 +00:00
|
|
|
: mFixedPath(applicationName)
|
2015-01-30 21:11:26 +00:00
|
|
|
, mSilent(silent)
|
2012-01-21 00:14:35 +00:00
|
|
|
{
|
|
|
|
setupTokensMapping();
|
|
|
|
|
2013-12-25 23:28:19 +00:00
|
|
|
boost::filesystem::create_directories(mFixedPath.getUserConfigPath());
|
2013-12-26 00:24:43 +00:00
|
|
|
boost::filesystem::create_directories(mFixedPath.getUserDataPath());
|
2012-07-22 11:52:55 +00:00
|
|
|
|
2013-12-25 23:28:19 +00:00
|
|
|
mLogPath = mFixedPath.getUserConfigPath();
|
2012-01-21 00:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigurationManager::~ConfigurationManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigurationManager::setupTokensMapping()
|
|
|
|
{
|
2012-02-19 22:39:37 +00:00
|
|
|
mTokensMapping.insert(std::make_pair(localToken, &FixedPath<>::getLocalPath));
|
2014-06-18 15:18:48 +00:00
|
|
|
mTokensMapping.insert(std::make_pair(userDataToken, &FixedPath<>::getUserDataPath));
|
2012-01-28 10:59:08 +00:00
|
|
|
mTokensMapping.insert(std::make_pair(globalToken, &FixedPath<>::getGlobalDataPath));
|
2012-01-21 00:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigurationManager::readConfiguration(boost::program_options::variables_map& variables,
|
2015-06-16 02:48:45 +00:00
|
|
|
boost::program_options::options_description& description, bool quiet)
|
2012-01-21 00:14:35 +00:00
|
|
|
{
|
2015-06-16 02:48:45 +00:00
|
|
|
bool silent = mSilent;
|
|
|
|
mSilent = quiet;
|
|
|
|
|
2013-12-25 23:28:19 +00:00
|
|
|
loadConfig(mFixedPath.getUserConfigPath(), variables, description);
|
2012-01-21 00:14:35 +00:00
|
|
|
boost::program_options::notify(variables);
|
2012-01-25 22:55:43 +00:00
|
|
|
|
2015-12-02 18:21:10 +00:00
|
|
|
// read either local or global config depending on type of installation
|
|
|
|
bool loaded = loadConfig(mFixedPath.getLocalPath(), variables, description);
|
2012-01-21 00:14:35 +00:00
|
|
|
boost::program_options::notify(variables);
|
2015-12-02 18:21:10 +00:00
|
|
|
if (!loaded)
|
|
|
|
{
|
|
|
|
loadConfig(mFixedPath.getGlobalConfigPath(), variables, description);
|
|
|
|
boost::program_options::notify(variables);
|
|
|
|
}
|
2012-01-25 22:55:43 +00:00
|
|
|
|
2015-06-16 02:48:45 +00:00
|
|
|
mSilent = silent;
|
2012-01-25 22:55:43 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 12:31:20 +00:00
|
|
|
void ConfigurationManager::processPaths(Files::PathContainer& dataDirs, bool create)
|
2012-01-25 22:55:43 +00:00
|
|
|
{
|
2012-02-23 22:01:42 +00:00
|
|
|
std::string path;
|
2012-01-25 22:55:43 +00:00
|
|
|
for (Files::PathContainer::iterator it = dataDirs.begin(); it != dataDirs.end(); ++it)
|
|
|
|
{
|
2012-02-23 22:01:42 +00:00
|
|
|
path = it->string();
|
|
|
|
boost::erase_all(path, "\"");
|
2012-02-27 22:56:58 +00:00
|
|
|
*it = boost::filesystem::path(path);
|
2012-01-28 08:49:09 +00:00
|
|
|
|
|
|
|
// Check if path contains a token
|
2012-02-15 21:34:51 +00:00
|
|
|
if (!path.empty() && *path.begin() == '?')
|
2012-01-25 22:55:43 +00:00
|
|
|
{
|
2012-02-15 21:34:51 +00:00
|
|
|
std::string::size_type pos = path.find('?', 1);
|
|
|
|
if (pos != std::string::npos && pos != 0)
|
2012-01-25 22:55:43 +00:00
|
|
|
{
|
2012-02-15 21:34:51 +00:00
|
|
|
TokensMappingContainer::iterator tokenIt = mTokensMapping.find(path.substr(0, pos + 1));
|
|
|
|
if (tokenIt != mTokensMapping.end())
|
2012-01-28 08:49:09 +00:00
|
|
|
{
|
2012-02-15 21:34:51 +00:00
|
|
|
boost::filesystem::path tempPath(((mFixedPath).*(tokenIt->second))());
|
|
|
|
if (pos < path.length() - 1)
|
|
|
|
{
|
|
|
|
// There is something after the token, so we should
|
|
|
|
// append it to the path
|
|
|
|
tempPath /= path.substr(pos + 1, path.length() - pos);
|
|
|
|
}
|
|
|
|
|
2012-02-19 22:39:37 +00:00
|
|
|
*it = tempPath;
|
2012-01-28 08:49:09 +00:00
|
|
|
}
|
|
|
|
else
|
2012-01-25 22:55:43 +00:00
|
|
|
{
|
2012-02-15 21:34:51 +00:00
|
|
|
// Clean invalid / unknown token, it will be removed outside the loop
|
2012-01-28 08:49:09 +00:00
|
|
|
(*it).clear();
|
2012-01-25 22:55:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-19 22:39:37 +00:00
|
|
|
|
|
|
|
if (!boost::filesystem::is_directory(*it))
|
|
|
|
{
|
2013-09-08 12:31:20 +00:00
|
|
|
if (create)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
boost::filesystem::create_directories (*it);
|
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
|
|
|
|
if (boost::filesystem::is_directory(*it))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-02-19 22:39:37 +00:00
|
|
|
(*it).clear();
|
|
|
|
}
|
2012-01-25 22:55:43 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 22:39:37 +00:00
|
|
|
dataDirs.erase(std::remove_if(dataDirs.begin(), dataDirs.end(),
|
2017-05-06 13:12:06 +00:00
|
|
|
std::bind(&boost::filesystem::path::empty, std::placeholders::_1)), dataDirs.end());
|
2012-01-21 00:14:35 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 18:21:10 +00:00
|
|
|
bool ConfigurationManager::loadConfig(const boost::filesystem::path& path,
|
2012-01-21 00:14:35 +00:00
|
|
|
boost::program_options::variables_map& variables,
|
|
|
|
boost::program_options::options_description& description)
|
|
|
|
{
|
|
|
|
boost::filesystem::path cfgFile(path);
|
|
|
|
cfgFile /= std::string(openmwCfgFile);
|
|
|
|
if (boost::filesystem::is_regular_file(cfgFile))
|
|
|
|
{
|
2015-01-30 21:11:26 +00:00
|
|
|
if (!mSilent)
|
|
|
|
std::cout << "Loading config file: " << cfgFile.string() << "... ";
|
2012-01-21 00:14:35 +00:00
|
|
|
|
2016-07-11 00:09:34 +00:00
|
|
|
boost::filesystem::ifstream configFileStreamUnfiltered(cfgFile);
|
2016-07-12 00:06:57 +00:00
|
|
|
boost::iostreams::filtering_istream configFileStream;
|
|
|
|
configFileStream.push(escape_hash_filter());
|
|
|
|
configFileStream.push(configFileStreamUnfiltered);
|
2016-07-11 00:09:34 +00:00
|
|
|
if (configFileStreamUnfiltered.is_open())
|
2012-01-21 00:14:35 +00:00
|
|
|
{
|
|
|
|
boost::program_options::store(boost::program_options::parse_config_file(
|
2012-02-22 07:34:47 +00:00
|
|
|
configFileStream, description, true), variables);
|
2012-01-21 00:14:35 +00:00
|
|
|
|
2015-01-30 21:11:26 +00:00
|
|
|
if (!mSilent)
|
|
|
|
std::cout << "done." << std::endl;
|
2015-12-02 18:21:10 +00:00
|
|
|
return true;
|
2012-01-21 00:14:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-30 21:11:26 +00:00
|
|
|
if (!mSilent)
|
|
|
|
std::cout << "failed." << std::endl;
|
2015-12-02 18:21:10 +00:00
|
|
|
return false;
|
2012-01-21 00:14:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-02 18:21:10 +00:00
|
|
|
return false;
|
2012-01-21 00:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const boost::filesystem::path& ConfigurationManager::getGlobalPath() const
|
|
|
|
{
|
2013-12-25 23:28:19 +00:00
|
|
|
return mFixedPath.getGlobalConfigPath();
|
2012-01-21 00:14:35 +00:00
|
|
|
}
|
|
|
|
|
2013-12-26 00:24:43 +00:00
|
|
|
const boost::filesystem::path& ConfigurationManager::getUserConfigPath() const
|
2012-01-21 00:14:35 +00:00
|
|
|
{
|
2013-12-25 23:28:19 +00:00
|
|
|
return mFixedPath.getUserConfigPath();
|
2012-01-21 00:14:35 +00:00
|
|
|
}
|
|
|
|
|
2013-12-26 00:24:43 +00:00
|
|
|
const boost::filesystem::path& ConfigurationManager::getUserDataPath() const
|
|
|
|
{
|
|
|
|
return mFixedPath.getUserDataPath();
|
|
|
|
}
|
|
|
|
|
2012-01-21 00:14:35 +00:00
|
|
|
const boost::filesystem::path& ConfigurationManager::getLocalPath() const
|
|
|
|
{
|
|
|
|
return mFixedPath.getLocalPath();
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:55:43 +00:00
|
|
|
const boost::filesystem::path& ConfigurationManager::getGlobalDataPath() const
|
2012-01-21 00:14:35 +00:00
|
|
|
{
|
2012-01-25 22:55:43 +00:00
|
|
|
return mFixedPath.getGlobalDataPath();
|
|
|
|
}
|
|
|
|
|
2012-09-02 17:40:26 +00:00
|
|
|
const boost::filesystem::path& ConfigurationManager::getCachePath() const
|
|
|
|
{
|
|
|
|
return mFixedPath.getCachePath();
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:55:43 +00:00
|
|
|
const boost::filesystem::path& ConfigurationManager::getInstallPath() const
|
|
|
|
{
|
|
|
|
return mFixedPath.getInstallPath();
|
|
|
|
}
|
|
|
|
|
2012-01-21 00:14:35 +00:00
|
|
|
const boost::filesystem::path& ConfigurationManager::getLogPath() const
|
|
|
|
{
|
|
|
|
return mLogPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace Cfg */
|