1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
|
#include "configurationmanager.hpp"
#include <fstream>
#include <components/debug/debuglog.hpp>
#include <components/fallback/validate.hpp>
#include <components/files/configfileparser.hpp>
#include <components/misc/strings/conversion.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp>
/**
* \namespace Files
*/
namespace Files
{
namespace bpo = boost::program_options;
#if defined(_WIN32) || defined(__WINDOWS__)
static const char* const applicationName = "OpenMW";
#else
static const char* const applicationName = "openmw";
#endif
static constexpr auto localToken = u8"?local?";
static constexpr auto userConfigToken = u8"?userconfig?";
static constexpr auto userDataToken = u8"?userdata?";
static constexpr auto globalToken = u8"?global?";
ConfigurationManager::ConfigurationManager(bool silent)
: mFixedPath(applicationName)
, mSilent(silent)
{
setupTokensMapping();
// Initialize with fixed paths, will be overridden in `readConfiguration`.
mUserDataPath = mFixedPath.getUserDataPath();
mScreenshotPath = mFixedPath.getUserDataPath() / "screenshots";
}
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));
}
static bool hasReplaceConfig(const bpo::variables_map& variables)
{
if (variables["replace"].empty())
return false;
for (const std::string& var : variables["replace"].as<std::vector<std::string>>())
{
if (var == "config")
return true;
}
return false;
}
void ConfigurationManager::readConfiguration(
bpo::variables_map& variables, const bpo::options_description& description, bool quiet)
{
bool silent = mSilent;
mSilent = quiet;
// ensure defaults are present
bpo::store(bpo::parsed_options(&description), variables);
std::optional<bpo::variables_map> 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)
{
if (!quiet)
Log(Debug::Error) << "Neither local config nor global config are available.";
mSilent = silent;
return;
}
std::stack<std::filesystem::path> extraConfigDirs;
addExtraConfigDirs(extraConfigDirs, variables);
if (!hasReplaceConfig(variables))
addExtraConfigDirs(extraConfigDirs, *config);
std::vector<bpo::variables_map> parsedConfigs{ *std::move(config) };
std::set<std::filesystem::path>
alreadyParsedPaths; // needed to prevent infinite loop in case of a circular link
alreadyParsedPaths.insert(mActiveConfigPaths.front());
while (!extraConfigDirs.empty())
{
auto path = extraConfigDirs.top();
extraConfigDirs.pop();
if (alreadyParsedPaths.count(path) > 0)
{
if (!quiet)
Log(Debug::Warning) << "Repeated config dir: " << path;
continue;
}
alreadyParsedPaths.insert(path);
config = loadConfig(path, description);
if (config && hasReplaceConfig(*config) && parsedConfigs.size() > 1)
{
mActiveConfigPaths.resize(1);
parsedConfigs.resize(1);
Log(Debug::Info) << "Skipping previous configs except " << (mActiveConfigPaths.front() / "openmw.cfg")
<< " due to replace=config in " << (path / "openmw.cfg");
}
mActiveConfigPaths.emplace_back(std::move(path));
if (config)
{
addExtraConfigDirs(extraConfigDirs, *config);
parsedConfigs.push_back(*std::move(config));
}
}
for (auto it = parsedConfigs.rbegin(); it != parsedConfigs.rend(); ++it)
{
auto composingVariables = separateComposingVariables(variables, description);
for (auto& [k, v] : *it)
{
auto variable = variables.find(k);
if (variable == variables.end())
variables.insert({ k, v });
else if (variable->second.defaulted())
variable->second = v;
}
mergeComposingVariables(variables, composingVariables, description);
}
mUserDataPath = variables["user-data"]
.as<Files::MaybeQuotedPath>()
.u8string(); // This call to u8string is redundant, but required to build on MSVC 14.26 due
// to implementation bugs.
if (mUserDataPath.empty())
{
if (!quiet)
Log(Debug::Warning) << "Error: `user-data` is not specified";
mUserDataPath = mFixedPath.getUserDataPath();
}
mScreenshotPath = mUserDataPath / "screenshots";
std::filesystem::create_directories(getUserConfigPath());
std::filesystem::create_directories(mScreenshotPath);
// probably not necessary but validate the creation of the screenshots directory and fallback to the original
// behavior if it fails
if (!std::filesystem::is_directory(mScreenshotPath))
mScreenshotPath = mUserDataPath;
if (!quiet)
{
Log(Debug::Info) << "Logs dir: " << getUserConfigPath();
Log(Debug::Info) << "User data dir: " << mUserDataPath;
Log(Debug::Info) << "Screenshots dir: " << mScreenshotPath;
}
mSilent = silent;
}
void ConfigurationManager::addExtraConfigDirs(
std::stack<std::filesystem::path>& dirs, const bpo::variables_map& variables) const
{
auto configIt = variables.find("config");
if (configIt == variables.end())
return;
Files::PathContainer newDirs = asPathContainer(configIt->second.as<Files::MaybeQuotedPathContainer>());
for (auto it = newDirs.rbegin(); it != newDirs.rend(); ++it)
dirs.push(*it);
}
void ConfigurationManager::addCommonOptions(bpo::options_description& description)
{
auto addOption = description.add_options();
addOption("config",
bpo::value<Files::MaybeQuotedPathContainer>()
->default_value(Files::MaybeQuotedPathContainer(), "")
->multitoken()
->composing(),
"additional config directories");
addOption("replace",
bpo::value<std::vector<std::string>>()
->default_value(std::vector<std::string>(), "")
->multitoken()
->composing(),
"settings where the values from the current source should replace those from lower-priority sources "
"instead of being appended");
addOption("user-data", bpo::value<Files::MaybeQuotedPath>()->default_value(Files::MaybeQuotedPath(), ""),
"set user data directory (used for saves, screenshots, etc)");
addOption("resources",
boost::program_options::value<Files::MaybeQuotedPath>()->default_value(
Files::MaybeQuotedPath(), "resources"),
"set resources directory");
}
bpo::variables_map separateComposingVariables(
bpo::variables_map& variables, const bpo::options_description& description)
{
bpo::variables_map composingVariables;
for (auto itr = variables.begin(); itr != variables.end();)
{
if (description.find(itr->first, false).semantic()->is_composing())
{
composingVariables.emplace(*itr);
itr = variables.erase(itr);
}
else
++itr;
}
return composingVariables;
}
void mergeComposingVariables(
bpo::variables_map& first, bpo::variables_map& second, const bpo::options_description& description)
{
// There are a few places this assumes all variables are present in second, but it's never crashed in the wild,
// so it looks like that's guaranteed.
std::set<std::string> replacedVariables;
if (description.find_nothrow("replace", false))
{
auto replace = second["replace"];
if (!replace.defaulted() && !replace.empty())
{
std::vector<std::string> replaceVector = replace.as<std::vector<std::string>>();
replacedVariables.insert(replaceVector.begin(), replaceVector.end());
}
}
for (const auto& option : description.options())
{
if (option->semantic()->is_composing())
{
std::string name = option->canonical_display_name();
auto firstPosition = first.find(name);
if (firstPosition == first.end())
{
first.emplace(name, second[name]);
continue;
}
if (replacedVariables.count(name) || firstPosition->second.defaulted() || firstPosition->second.empty())
{
firstPosition->second = second[name];
continue;
}
if (second[name].defaulted() || second[name].empty())
continue;
boost::any& firstValue = firstPosition->second.value();
const boost::any& secondValue = second[name].value();
if (firstValue.type() == typeid(Files::MaybeQuotedPathContainer))
{
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>))
{
auto& firstVector = boost::any_cast<std::vector<std::string>&>(firstValue);
const auto& secondVector = boost::any_cast<const std::vector<std::string>&>(secondValue);
firstVector.insert(firstVector.end(), secondVector.begin(), secondVector.end());
}
else if (firstValue.type() == typeid(Fallback::FallbackMap))
{
auto& firstMap = boost::any_cast<Fallback::FallbackMap&>(firstValue);
const auto& secondMap = boost::any_cast<const Fallback::FallbackMap&>(secondValue);
std::map<std::string, std::string> tempMap(secondMap.mMap);
tempMap.merge(firstMap.mMap);
firstMap.mMap.swap(tempMap);
}
else
Log(Debug::Error)
<< "Unexpected composing variable type. Curse boost and their blasted arcane templates.";
}
}
}
void ConfigurationManager::processPath(std::filesystem::path& path, const std::filesystem::path& basePath) const
{
const auto str = path.u8string();
if (str.empty() || str[0] != u8'?')
{
if (!path.is_absolute())
path = basePath / path;
return;
}
const auto pos = str.find('?', 1);
if (pos != std::u8string::npos && pos != 0)
{
auto tokenIt = mTokensMapping.find(str.substr(0, pos + 1));
if (tokenIt != mTokensMapping.end())
{
auto tempPath(((mFixedPath).*(tokenIt->second))());
if (pos < str.length() - 1)
{
// There is something after the token, so we should
// append it to the path
tempPath /= str.substr(pos + 1, str.length() - pos);
}
path = std::move(tempPath);
}
else
{
if (!mSilent)
Log(Debug::Warning) << "Path starts with unknown token: " << path;
path.clear();
}
}
}
void ConfigurationManager::processPaths(Files::PathContainer& dataDirs, const std::filesystem::path& basePath) const
{
for (auto& path : dataDirs)
processPath(path, basePath);
}
void ConfigurationManager::processPaths(
boost::program_options::variables_map& variables, const std::filesystem::path& basePath) const
{
for (auto& [name, var] : variables)
{
if (var.defaulted())
continue;
if (var.value().type() == typeid(MaybeQuotedPathContainer))
{
auto& pathContainer = boost::any_cast<MaybeQuotedPathContainer&>(var.value());
for (MaybeQuotedPath& path : pathContainer)
processPath(path, basePath);
}
else if (var.value().type() == typeid(MaybeQuotedPath))
{
std::filesystem::path& path = boost::any_cast<Files::MaybeQuotedPath&>(var.value());
processPath(path, basePath);
}
}
}
void ConfigurationManager::filterOutNonExistingPaths(Files::PathContainer& dataDirs) const
{
dataDirs.erase(std::remove_if(dataDirs.begin(), dataDirs.end(),
[this](const std::filesystem::path& p) {
bool exists = std::filesystem::is_directory(p);
if (!exists && !mSilent)
Log(Debug::Warning) << "No such dir: " << p;
return !exists;
}),
dataDirs.end());
}
std::optional<bpo::variables_map> ConfigurationManager::loadConfig(
const std::filesystem::path& path, const bpo::options_description& description) const
{
std::filesystem::path cfgFile(path);
cfgFile /= openmwCfgFile;
if (std::filesystem::is_regular_file(cfgFile))
{
if (!mSilent)
Log(Debug::Info) << "Loading config file: " << cfgFile;
std::ifstream configFileStream(cfgFile);
if (configFileStream.is_open())
{
bpo::variables_map variables;
bpo::store(Files::parse_config_file(configFileStream, description, true), variables);
processPaths(variables, path);
return variables;
}
else if (!mSilent)
Log(Debug::Error) << "Loading failed.";
}
return std::nullopt;
}
const std::filesystem::path& ConfigurationManager::getGlobalPath() const
{
return mFixedPath.getGlobalConfigPath();
}
const std::filesystem::path& ConfigurationManager::getUserConfigPath() const
{
if (mActiveConfigPaths.empty())
return mFixedPath.getUserConfigPath();
else
return mActiveConfigPaths.back();
}
const std::filesystem::path& ConfigurationManager::getUserDataPath() const
{
return mUserDataPath;
}
const std::filesystem::path& ConfigurationManager::getLocalPath() const
{
return mFixedPath.getLocalPath();
}
const std::filesystem::path& ConfigurationManager::getCachePath() const
{
return mFixedPath.getCachePath();
}
const std::filesystem::path& ConfigurationManager::getInstallPath() const
{
return mFixedPath.getInstallPath();
}
const std::filesystem::path& ConfigurationManager::getScreenshotPath() const
{
return mScreenshotPath;
}
void parseArgs(
int argc, const char* const argv[], bpo::variables_map& variables, const bpo::options_description& description)
{
bpo::store(bpo::command_line_parser(argc, argv).options(description).allow_unregistered().run(), variables);
}
void parseConfig(std::istream& stream, bpo::variables_map& variables, const bpo::options_description& description)
{
bpo::store(Files::parse_config_file(stream, description, true), variables);
}
std::istream& operator>>(std::istream& istream, MaybeQuotedPath& MaybeQuotedPath)
{
// If the stream starts with a double quote, read from stream using boost::filesystem::path rules (which are not
// the same as std::filesystem::path rules), then discard anything remaining. This prevents
// boost::program_options getting upset that we've not consumed the whole stream. If it doesn't start with a
// double quote, read the whole thing verbatim.
// This function's implementation and comments have a history of being changed by well-meaning but incorrect
// OpenMW developers. Please be very careful you aren't joining them. If you're insufficiently apprehensive,
// here's a 737-word GitLab comment to scare you:
// https://gitlab.com/OpenMW/openmw/-/merge_requests/2979#note_1371082428.
if (istream.peek() == '"')
{
std::string intermediate;
// std::filesystem::path would use '"', '\' here.
// We use & because it's easier when copying and pasting Windows paths, which have many backslashes and few
// ampersands, and because it's backwards-compatible with the previous format, which used
// boost::filesystem::path's operator>>.
istream >> std::quoted(intermediate, '"', '&');
static_cast<std::filesystem::path&>(MaybeQuotedPath) = Misc::StringUtils::stringToU8String(intermediate);
if (istream && !istream.eof() && istream.peek() != EOF)
{
std::string remainder{ std::istreambuf_iterator(istream), {} };
Log(Debug::Warning) << "Trailing data in path setting. Used '" << MaybeQuotedPath << "' but '"
<< remainder << "' remained";
}
}
else
{
std::string intermediate{ std::istreambuf_iterator(istream), {} };
static_cast<std::filesystem::path&>(MaybeQuotedPath) = Misc::StringUtils::stringToU8String(intermediate);
}
return istream;
}
PathContainer asPathContainer(const MaybeQuotedPathContainer& MaybeQuotedPathContainer)
{
PathContainer res;
res.reserve(MaybeQuotedPathContainer.size());
for (const auto& maybeQuotedPath : MaybeQuotedPathContainer)
{
res.emplace_back(maybeQuotedPath.u8string()); // This call to u8string is redundant, but required to build
// on MSVC 14.26 due to implementation bugs.
}
return res;
}
} /* namespace Files */
|