New option "config" for specifying additional config directories.

C++20
Petr Mikheev 3 years ago
parent dd5ba5c57b
commit 5ca56a4f8a

@ -25,6 +25,7 @@ int main(int argc, char** argv)
("encoding", boost::program_options::value<std::string>()->default_value("win1252"), "encoding of the save file") ("encoding", boost::program_options::value<std::string>()->default_value("win1252"), "encoding of the save file")
; ;
p_desc.add("mwsave", 1).add("output", 1); p_desc.add("mwsave", 1).add("output", 1);
Files::ConfigurationManager::addCommonOptions(desc);
bpo::variables_map variables; bpo::variables_map variables;

@ -84,6 +84,7 @@ namespace NavMeshTool
("process-interior-cells", bpo::value<bool>()->implicit_value(true) ("process-interior-cells", bpo::value<bool>()->implicit_value(true)
->default_value(false), "build navmesh for interior cells") ->default_value(false), "build navmesh for interior cells")
; ;
Files::ConfigurationManager::addCommonOptions(result);
return result; return result;
} }

@ -102,6 +102,7 @@ std::pair<Files::PathContainer, std::vector<std::string> > CS::Editor::readConfi
->multitoken(), "exclude specified script from the verifier (if the use of the blacklist is enabled)") ->multitoken(), "exclude specified script from the verifier (if the use of the blacklist is enabled)")
("script-blacklist-use", boost::program_options::value<bool>()->implicit_value(true) ("script-blacklist-use", boost::program_options::value<bool>()->implicit_value(true)
->default_value(true), "enable script blacklisting"); ->default_value(true), "enable script blacklisting");
Files::ConfigurationManager::addCommonOptions(desc);
boost::program_options::notify(variables); boost::program_options::notify(variables);

@ -40,6 +40,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
typedef std::vector<std::string> StringsVector; typedef std::vector<std::string> StringsVector;
bpo::options_description desc = OpenMW::makeOptionsDescription(); bpo::options_description desc = OpenMW::makeOptionsDescription();
Files::ConfigurationManager::addCommonOptions(desc);
bpo::variables_map variables; bpo::variables_map variables;
@ -61,9 +62,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
return false; return false;
} }
bpo::variables_map composingVariables = Files::separateComposingVariables(variables, desc);
cfgMgr.readConfiguration(variables, desc); cfgMgr.readConfiguration(variables, desc);
Files::mergeComposingVariables(variables, composingVariables, desc);
setupLogging(cfgMgr.getLogPath().string(), "OpenMW"); setupLogging(cfgMgr.getLogPath().string(), "OpenMW");

@ -58,6 +58,7 @@ struct ContentFileTest : public ::testing::Test
("content", boost::program_options::value<std::vector<std::string>>()->default_value(std::vector<std::string>(), "") ("content", boost::program_options::value<std::vector<std::string>>()->default_value(std::vector<std::string>(), "")
->multitoken()->composing(), "content file(s): esm/esp, or omwgame/omwaddon") ->multitoken()->composing(), "content file(s): esm/esp, or omwgame/omwaddon")
("data-local", boost::program_options::value<Files::MaybeQuotedPathContainer::value_type>()->default_value(Files::MaybeQuotedPathContainer::value_type(), "")); ("data-local", boost::program_options::value<Files::MaybeQuotedPathContainer::value_type>()->default_value(Files::MaybeQuotedPathContainer::value_type(), ""));
Files::ConfigurationManager::addCommonOptions(desc);
boost::program_options::notify(variables); boost::program_options::notify(variables);

@ -20,6 +20,7 @@ static const char* const applicationName = "openmw";
#endif #endif
const char* const localToken = "?local?"; const char* const localToken = "?local?";
const char* const userConfigToken = "?userconfig?";
const char* const userDataToken = "?userdata?"; const char* const userDataToken = "?userdata?";
const char* const globalToken = "?global?"; const char* const globalToken = "?global?";
@ -50,6 +51,7 @@ ConfigurationManager::~ConfigurationManager()
void ConfigurationManager::setupTokensMapping() void ConfigurationManager::setupTokensMapping()
{ {
mTokensMapping.insert(std::make_pair(localToken, &FixedPath<>::getLocalPath)); 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(userDataToken, &FixedPath<>::getUserDataPath));
mTokensMapping.insert(std::make_pair(globalToken, &FixedPath<>::getGlobalDataPath)); mTokensMapping.insert(std::make_pair(globalToken, &FixedPath<>::getGlobalDataPath));
} }
@ -57,31 +59,92 @@ void ConfigurationManager::setupTokensMapping()
void ConfigurationManager::readConfiguration(boost::program_options::variables_map& variables, void ConfigurationManager::readConfiguration(boost::program_options::variables_map& variables,
boost::program_options::options_description& description, bool quiet) boost::program_options::options_description& description, bool quiet)
{ {
using ParsedConfigFile = bpo::basic_parsed_options<char>;
bool silent = mSilent; bool silent = mSilent;
mSilent = quiet; 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 std::optional<ParsedConfigFile> config = loadConfig(mFixedPath.getLocalPath(), description);
composingVariables = separateComposingVariables(variables, description); if (config)
bool loaded = loadConfig(mFixedPath.getLocalPath(), variables, description); mActiveConfigPaths.push_back(mFixedPath.getLocalPath());
mergeComposingVariables(variables, composingVariables, description); else
boost::program_options::notify(variables); {
if (!loaded) mActiveConfigPaths.push_back(mFixedPath.getGlobalConfigPath());
config = loadConfig(mFixedPath.getGlobalConfigPath(), description);
}
if (!config)
{ {
composingVariables = separateComposingVariables(variables, description); if (!quiet)
loadConfig(mFixedPath.getGlobalConfigPath(), variables, description); 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); mergeComposingVariables(variables, composingVariables, description);
boost::program_options::notify(variables);
} }
mLogPath = mActiveConfigPaths.back();
mSilent = silent; 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::variables_map separateComposingVariables(boost::program_options::variables_map & variables,
boost::program_options::options_description& description) 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)) if (description.find_nothrow("replace", false))
{ {
auto replace = second["replace"]; auto replace = second["replace"];
if (!replace.defaulted() && !replace.empty()) if (!replace.empty())
{ {
std::vector<std::string> replaceVector = replace.as<std::vector<std::string>>(); std::vector<std::string> replaceVector = replace.as<std::vector<std::string>>();
replacedVariables.insert(replaceVector.begin(), replaceVector.end()); replacedVariables.insert(replaceVector.begin(), replaceVector.end());
@ -132,7 +195,7 @@ void mergeComposingVariables(boost::program_options::variables_map& first, boost
continue; continue;
} }
if (second[name].defaulted() || second[name].empty()) if (second[name].empty())
continue; continue;
boost::any& firstValue = firstPosition->second.value(); 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); auto& firstPathContainer = boost::any_cast<Files::MaybeQuotedPathContainer&>(firstValue);
const auto& secondPathContainer = boost::any_cast<const Files::MaybeQuotedPathContainer&>(secondValue); const auto& secondPathContainer = boost::any_cast<const Files::MaybeQuotedPathContainer&>(secondValue);
firstPathContainer.insert(firstPathContainer.end(), secondPathContainer.begin(), secondPathContainer.end()); firstPathContainer.insert(firstPathContainer.end(), secondPathContainer.begin(), secondPathContainer.end());
} }
else if (firstValue.type() == typeid(std::vector<std::string>)) 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."; 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; std::string path;
for (Files::PathContainer::iterator it = dataDirs.begin(); it != dataDirs.end(); ++it) 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); std::string::size_type pos = path.find('?', 1);
if (pos != std::string::npos && pos != 0) 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()) if (tokenIt != mTokensMapping.end())
{ {
boost::filesystem::path tempPath(((mFixedPath).*(tokenIt->second))()); 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()); std::bind(&boost::filesystem::path::empty, std::placeholders::_1)), dataDirs.end());
} }
bool ConfigurationManager::loadConfig(const boost::filesystem::path& path, std::optional<bpo::basic_parsed_options<char>> ConfigurationManager::loadConfig(
boost::program_options::variables_map& variables, const boost::filesystem::path& path,
boost::program_options::options_description& description) boost::program_options::options_description& description)
{ {
boost::filesystem::path cfgFile(path); boost::filesystem::path cfgFile(path);
@ -238,20 +300,11 @@ bool ConfigurationManager::loadConfig(const boost::filesystem::path& path,
boost::filesystem::ifstream configFileStream(cfgFile); boost::filesystem::ifstream configFileStream(cfgFile);
if (configFileStream.is_open()) if (configFileStream.is_open())
{ return Files::parse_config_file(configFileStream, description, true);
parseConfig(configFileStream, variables, description); else if (!mSilent)
Log(Debug::Error) << "Loading failed.";
return true;
}
else
{
if (!mSilent)
Log(Debug::Error) << "Loading failed.";
return false;
}
} }
return false; return std::nullopt;
} }
const boost::filesystem::path& ConfigurationManager::getGlobalPath() const const boost::filesystem::path& ConfigurationManager::getGlobalPath() const
@ -344,4 +397,4 @@ PathContainer asPathContainer(const MaybeQuotedPathContainer& MaybeQuotedPathCon
return PathContainer(MaybeQuotedPathContainer.begin(), MaybeQuotedPathContainer.end()); return PathContainer(MaybeQuotedPathContainer.begin(), MaybeQuotedPathContainer.end());
} }
} /* namespace Cfg */ } /* namespace Files */

@ -2,6 +2,8 @@
#define COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP #define COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP
#include <map> #include <map>
#include <optional>
#include <stack>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
@ -25,7 +27,7 @@ struct ConfigurationManager
void readConfiguration(boost::program_options::variables_map& variables, void readConfiguration(boost::program_options::variables_map& variables,
boost::program_options::options_description& description, bool quiet=false); boost::program_options::options_description& description, bool quiet=false);
void processPaths(Files::PathContainer& dataDirs, bool create = false); void processPaths(Files::PathContainer& dataDirs, bool create = false) const;
///< \param create Try creating the directory, if it does not exist. ///< \param create Try creating the directory, if it does not exist.
/**< Fixed paths */ /**< Fixed paths */
@ -37,24 +39,34 @@ struct ConfigurationManager
const boost::filesystem::path& getUserDataPath() const; const boost::filesystem::path& getUserDataPath() const;
const boost::filesystem::path& getLocalDataPath() const; const boost::filesystem::path& getLocalDataPath() const;
const boost::filesystem::path& getInstallPath() const; const boost::filesystem::path& getInstallPath() const;
const std::vector<boost::filesystem::path>& getActiveConfigPaths() const { return mActiveConfigPaths; }
const boost::filesystem::path& getCachePath() const; const boost::filesystem::path& getCachePath() const;
const boost::filesystem::path& getLogPath() const; const boost::filesystem::path& getLogPath() const;
const boost::filesystem::path& getScreenshotPath() const; const boost::filesystem::path& getScreenshotPath() const;
static void addCommonOptions(boost::program_options::options_description& description);
private: private:
typedef Files::FixedPath<> FixedPathType; typedef Files::FixedPath<> FixedPathType;
typedef const boost::filesystem::path& (FixedPathType::*path_type_f)() const; typedef const boost::filesystem::path& (FixedPathType::*path_type_f)() const;
typedef std::map<std::string, path_type_f> TokensMappingContainer; typedef std::map<std::string, path_type_f> TokensMappingContainer;
bool loadConfig(const boost::filesystem::path& path, std::optional<boost::program_options::basic_parsed_options<char>> loadConfig(
boost::program_options::variables_map& variables, const boost::filesystem::path& path,
boost::program_options::options_description& description); boost::program_options::options_description& description);
void addExtraConfigDirs(std::stack<boost::filesystem::path>& dirs,
const boost::program_options::variables_map& variables) const;
void addExtraConfigDirs(std::stack<boost::filesystem::path>& dirs,
const boost::program_options::basic_parsed_options<char>& options) const;
void setupTokensMapping(); void setupTokensMapping();
std::vector<boost::filesystem::path> mActiveConfigPaths;
FixedPathType mFixedPath; FixedPathType mFixedPath;
boost::filesystem::path mLogPath; boost::filesystem::path mLogPath;

@ -4,6 +4,7 @@
content=builtin.omwscripts content=builtin.omwscripts
data-local="?userdata?data" data-local="?userdata?data"
config="?userconfig?"
resources=${OPENMW_RESOURCE_FILES} resources=${OPENMW_RESOURCE_FILES}
script-blacklist=Museum script-blacklist=Museum
script-blacklist=MockChangeScript script-blacklist=MockChangeScript

@ -4,6 +4,7 @@
content=builtin.omwscripts content=builtin.omwscripts
data-local="?userdata?data" data-local="?userdata?data"
config="?userconfig?"
resources=./resources resources=./resources
script-blacklist=Museum script-blacklist=Museum
script-blacklist=MockChangeScript script-blacklist=MockChangeScript

Loading…
Cancel
Save