|
|
|
@ -20,6 +20,7 @@ static const char* const applicationName = "openmw";
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
const char* const localToken = "?local?";
|
|
|
|
|
const char* const userConfigToken = "?userconfig?";
|
|
|
|
|
const char* const userDataToken = "?userdata?";
|
|
|
|
|
const char* const globalToken = "?global?";
|
|
|
|
|
|
|
|
|
@ -50,6 +51,7 @@ ConfigurationManager::~ConfigurationManager()
|
|
|
|
|
void ConfigurationManager::setupTokensMapping()
|
|
|
|
|
{
|
|
|
|
|
mTokensMapping.insert(std::make_pair(localToken, &FixedPath<>::getLocalPath));
|
|
|
|
|
mTokensMapping.insert(std::make_pair(userConfigToken, &FixedPath<>::getUserConfigPath));
|
|
|
|
|
mTokensMapping.insert(std::make_pair(userDataToken, &FixedPath<>::getUserDataPath));
|
|
|
|
|
mTokensMapping.insert(std::make_pair(globalToken, &FixedPath<>::getGlobalDataPath));
|
|
|
|
|
}
|
|
|
|
@ -57,31 +59,92 @@ void ConfigurationManager::setupTokensMapping()
|
|
|
|
|
void ConfigurationManager::readConfiguration(boost::program_options::variables_map& variables,
|
|
|
|
|
boost::program_options::options_description& description, bool quiet)
|
|
|
|
|
{
|
|
|
|
|
using ParsedConfigFile = bpo::basic_parsed_options<char>;
|
|
|
|
|
bool silent = mSilent;
|
|
|
|
|
mSilent = quiet;
|
|
|
|
|
|
|
|
|
|
// User config has the highest priority.
|
|
|
|
|
auto composingVariables = separateComposingVariables(variables, description);
|
|
|
|
|
loadConfig(mFixedPath.getUserConfigPath(), variables, description);
|
|
|
|
|
mergeComposingVariables(variables, composingVariables, description);
|
|
|
|
|
boost::program_options::notify(variables);
|
|
|
|
|
|
|
|
|
|
// read either local or global config depending on type of installation
|
|
|
|
|
composingVariables = separateComposingVariables(variables, description);
|
|
|
|
|
bool loaded = loadConfig(mFixedPath.getLocalPath(), variables, description);
|
|
|
|
|
mergeComposingVariables(variables, composingVariables, description);
|
|
|
|
|
boost::program_options::notify(variables);
|
|
|
|
|
if (!loaded)
|
|
|
|
|
std::optional<ParsedConfigFile> config = loadConfig(mFixedPath.getLocalPath(), description);
|
|
|
|
|
if (config)
|
|
|
|
|
mActiveConfigPaths.push_back(mFixedPath.getLocalPath());
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mActiveConfigPaths.push_back(mFixedPath.getGlobalConfigPath());
|
|
|
|
|
config = loadConfig(mFixedPath.getGlobalConfigPath(), description);
|
|
|
|
|
}
|
|
|
|
|
if (!config)
|
|
|
|
|
{
|
|
|
|
|
composingVariables = separateComposingVariables(variables, description);
|
|
|
|
|
loadConfig(mFixedPath.getGlobalConfigPath(), variables, description);
|
|
|
|
|
if (!quiet)
|
|
|
|
|
Log(Debug::Error) << "Neither local config nor global config are available.";
|
|
|
|
|
mSilent = silent;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::stack<boost::filesystem::path> extraConfigDirs;
|
|
|
|
|
addExtraConfigDirs(extraConfigDirs, variables);
|
|
|
|
|
addExtraConfigDirs(extraConfigDirs, *config);
|
|
|
|
|
|
|
|
|
|
std::vector<ParsedConfigFile> parsedOptions{*std::move(config)};
|
|
|
|
|
std::set<boost::filesystem::path> alreadyParsedPaths; // needed to prevent infinite loop in case of a circular link
|
|
|
|
|
alreadyParsedPaths.insert(boost::filesystem::path(mActiveConfigPaths.front()));
|
|
|
|
|
|
|
|
|
|
while (!extraConfigDirs.empty())
|
|
|
|
|
{
|
|
|
|
|
boost::filesystem::path path = extraConfigDirs.top();
|
|
|
|
|
extraConfigDirs.pop();
|
|
|
|
|
if (alreadyParsedPaths.count(path) > 0)
|
|
|
|
|
{
|
|
|
|
|
if (!quiet)
|
|
|
|
|
Log(Debug::Warning) << "Repeated config dir: " << path;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
alreadyParsedPaths.insert(path);
|
|
|
|
|
mActiveConfigPaths.push_back(path);
|
|
|
|
|
config = loadConfig(path, description);
|
|
|
|
|
if (!config)
|
|
|
|
|
continue;
|
|
|
|
|
addExtraConfigDirs(extraConfigDirs, *config);
|
|
|
|
|
parsedOptions.push_back(*std::move(config));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto it = parsedOptions.rbegin(); it != parsedOptions.rend(); ++it)
|
|
|
|
|
{
|
|
|
|
|
auto composingVariables = separateComposingVariables(variables, description);
|
|
|
|
|
boost::program_options::store(std::move(*it), variables);
|
|
|
|
|
mergeComposingVariables(variables, composingVariables, description);
|
|
|
|
|
boost::program_options::notify(variables);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLogPath = mActiveConfigPaths.back();
|
|
|
|
|
mSilent = silent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurationManager::addExtraConfigDirs(std::stack<boost::filesystem::path>& dirs,
|
|
|
|
|
const bpo::basic_parsed_options<char>& options) const
|
|
|
|
|
{
|
|
|
|
|
boost::program_options::variables_map variables;
|
|
|
|
|
boost::program_options::store(options, variables);
|
|
|
|
|
boost::program_options::notify(variables);
|
|
|
|
|
addExtraConfigDirs(dirs, variables);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurationManager::addExtraConfigDirs(std::stack<boost::filesystem::path>& dirs,
|
|
|
|
|
const boost::program_options::variables_map& variables) const
|
|
|
|
|
{
|
|
|
|
|
auto configIt = variables.find("config");
|
|
|
|
|
if (configIt == variables.end())
|
|
|
|
|
return;
|
|
|
|
|
Files::PathContainer newDirs = asPathContainer(configIt->second.as<Files::MaybeQuotedPathContainer>());
|
|
|
|
|
processPaths(newDirs);
|
|
|
|
|
for (auto it = newDirs.rbegin(); it != newDirs.rend(); ++it)
|
|
|
|
|
dirs.push(*it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurationManager::addCommonOptions(boost::program_options::options_description& description)
|
|
|
|
|
{
|
|
|
|
|
description.add_options()
|
|
|
|
|
("config", bpo::value<Files::MaybeQuotedPathContainer>()->default_value(Files::MaybeQuotedPathContainer(), "config")
|
|
|
|
|
->multitoken()->composing(), "additional config directories");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boost::program_options::variables_map separateComposingVariables(boost::program_options::variables_map & variables,
|
|
|
|
|
boost::program_options::options_description& description)
|
|
|
|
|
{
|
|
|
|
@ -107,7 +170,7 @@ void mergeComposingVariables(boost::program_options::variables_map& first, boost
|
|
|
|
|
if (description.find_nothrow("replace", false))
|
|
|
|
|
{
|
|
|
|
|
auto replace = second["replace"];
|
|
|
|
|
if (!replace.defaulted() && !replace.empty())
|
|
|
|
|
if (!replace.empty())
|
|
|
|
|
{
|
|
|
|
|
std::vector<std::string> replaceVector = replace.as<std::vector<std::string>>();
|
|
|
|
|
replacedVariables.insert(replaceVector.begin(), replaceVector.end());
|
|
|
|
@ -132,7 +195,7 @@ void mergeComposingVariables(boost::program_options::variables_map& first, boost
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (second[name].defaulted() || second[name].empty())
|
|
|
|
|
if (second[name].empty())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
boost::any& firstValue = firstPosition->second.value();
|
|
|
|
@ -142,7 +205,6 @@ void mergeComposingVariables(boost::program_options::variables_map& first, boost
|
|
|
|
|
{
|
|
|
|
|
auto& firstPathContainer = boost::any_cast<Files::MaybeQuotedPathContainer&>(firstValue);
|
|
|
|
|
const auto& secondPathContainer = boost::any_cast<const Files::MaybeQuotedPathContainer&>(secondValue);
|
|
|
|
|
|
|
|
|
|
firstPathContainer.insert(firstPathContainer.end(), secondPathContainer.begin(), secondPathContainer.end());
|
|
|
|
|
}
|
|
|
|
|
else if (firstValue.type() == typeid(std::vector<std::string>))
|
|
|
|
@ -165,10 +227,10 @@ void mergeComposingVariables(boost::program_options::variables_map& first, boost
|
|
|
|
|
Log(Debug::Error) << "Unexpected composing variable type. Curse boost and their blasted arcane templates.";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boost::program_options::notify(first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurationManager::processPaths(Files::PathContainer& dataDirs, bool create)
|
|
|
|
|
void ConfigurationManager::processPaths(Files::PathContainer& dataDirs, bool create) const
|
|
|
|
|
{
|
|
|
|
|
std::string path;
|
|
|
|
|
for (Files::PathContainer::iterator it = dataDirs.begin(); it != dataDirs.end(); ++it)
|
|
|
|
@ -181,7 +243,7 @@ void ConfigurationManager::processPaths(Files::PathContainer& dataDirs, bool cre
|
|
|
|
|
std::string::size_type pos = path.find('?', 1);
|
|
|
|
|
if (pos != std::string::npos && pos != 0)
|
|
|
|
|
{
|
|
|
|
|
TokensMappingContainer::iterator tokenIt = mTokensMapping.find(path.substr(0, pos + 1));
|
|
|
|
|
auto tokenIt = mTokensMapping.find(path.substr(0, pos + 1));
|
|
|
|
|
if (tokenIt != mTokensMapping.end())
|
|
|
|
|
{
|
|
|
|
|
boost::filesystem::path tempPath(((mFixedPath).*(tokenIt->second))());
|
|
|
|
@ -224,8 +286,8 @@ void ConfigurationManager::processPaths(Files::PathContainer& dataDirs, bool cre
|
|
|
|
|
std::bind(&boost::filesystem::path::empty, std::placeholders::_1)), dataDirs.end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConfigurationManager::loadConfig(const boost::filesystem::path& path,
|
|
|
|
|
boost::program_options::variables_map& variables,
|
|
|
|
|
std::optional<bpo::basic_parsed_options<char>> ConfigurationManager::loadConfig(
|
|
|
|
|
const boost::filesystem::path& path,
|
|
|
|
|
boost::program_options::options_description& description)
|
|
|
|
|
{
|
|
|
|
|
boost::filesystem::path cfgFile(path);
|
|
|
|
@ -238,20 +300,11 @@ bool ConfigurationManager::loadConfig(const boost::filesystem::path& path,
|
|
|
|
|
boost::filesystem::ifstream configFileStream(cfgFile);
|
|
|
|
|
|
|
|
|
|
if (configFileStream.is_open())
|
|
|
|
|
{
|
|
|
|
|
parseConfig(configFileStream, variables, description);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!mSilent)
|
|
|
|
|
Log(Debug::Error) << "Loading failed.";
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return Files::parse_config_file(configFileStream, description, true);
|
|
|
|
|
else if (!mSilent)
|
|
|
|
|
Log(Debug::Error) << "Loading failed.";
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const boost::filesystem::path& ConfigurationManager::getGlobalPath() const
|
|
|
|
@ -344,4 +397,4 @@ PathContainer asPathContainer(const MaybeQuotedPathContainer& MaybeQuotedPathCon
|
|
|
|
|
return PathContainer(MaybeQuotedPathContainer.begin(), MaybeQuotedPathContainer.end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} /* namespace Cfg */
|
|
|
|
|
} /* namespace Files */
|
|
|
|
|