diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index 7ad9856a77..7334d893db 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -2,6 +2,7 @@ set(GAME main.cpp engine.cpp + options.cpp ${CMAKE_SOURCE_DIR}/files/windows/openmw.rc ${CMAKE_SOURCE_DIR}/files/windows/openmw.exe.manifest diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp index de0fb0df03..d1f9fd1787 100644 --- a/apps/openmw/main.cpp +++ b/apps/openmw/main.cpp @@ -7,6 +7,7 @@ #include #include "engine.hpp" +#include "options.hpp" #if defined(_WIN32) #ifndef WIN32_LEAN_AND_MEAN @@ -39,108 +40,11 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat namespace bpo = boost::program_options; typedef std::vector StringsVector; - bpo::options_description desc("Syntax: openmw \nAllowed options"); - - desc.add_options() - ("help", "print help message") - ("version", "print version information and quit") - - ("replace", bpo::value()->default_value(Files::EscapeStringVector(), "") - ->multitoken()->composing(), "settings where the values from the current source should replace those from lower-priority sources instead of being appended") - - ("data", bpo::value()->default_value(Files::EscapePathContainer(), "data") - ->multitoken()->composing(), "set data directories (later directories have higher priority)") - - ("data-local", bpo::value()->default_value(Files::EscapePath(), ""), - "set local data directory (highest priority)") - - ("fallback-archive", bpo::value()->default_value(Files::EscapeStringVector(), "fallback-archive") - ->multitoken()->composing(), "set fallback BSA archives (later archives have higher priority)") - - ("resources", bpo::value()->default_value(Files::EscapePath(), "resources"), - "set resources directory") - - ("start", bpo::value()->default_value(""), - "set initial cell") - - ("content", bpo::value()->default_value(Files::EscapeStringVector(), "") - ->multitoken()->composing(), "content file(s): esm/esp, or omwgame/omwaddon") - - ("groundcover", bpo::value()->default_value(Files::EscapeStringVector(), "") - ->multitoken()->composing(), "groundcover content file(s): esm/esp, or omwgame/omwaddon") - - ("lua-scripts", bpo::value()->default_value(Files::EscapeStringVector(), "") - ->multitoken()->composing(), "file(s) with a list of global Lua scripts: omwscripts") - - ("no-sound", bpo::value()->implicit_value(true) - ->default_value(false), "disable all sounds") - - ("script-all", bpo::value()->implicit_value(true) - ->default_value(false), "compile all scripts (excluding dialogue scripts) at startup") - - ("script-all-dialogue", bpo::value()->implicit_value(true) - ->default_value(false), "compile all dialogue scripts at startup") - - ("script-console", bpo::value()->implicit_value(true) - ->default_value(false), "enable console-only script functionality") - - ("script-run", bpo::value()->default_value(""), - "select a file containing a list of console commands that is executed on startup") - - ("script-warn", bpo::value()->implicit_value (1) - ->default_value (1), - "handling of warnings when compiling scripts\n" - "\t0 - ignore warning\n" - "\t1 - show warning but consider script as correctly compiled anyway\n" - "\t2 - treat warnings as errors") - - ("script-blacklist", bpo::value()->default_value(Files::EscapeStringVector(), "") - ->multitoken()->composing(), "ignore the specified script (if the use of the blacklist is enabled)") - - ("script-blacklist-use", bpo::value()->implicit_value(true) - ->default_value(true), "enable script blacklisting") - - ("load-savegame", bpo::value()->default_value(Files::EscapePath(), ""), - "load a save game file on game startup (specify an absolute filename or a filename relative to the current working directory)") - - ("skip-menu", bpo::value()->implicit_value(true) - ->default_value(false), "skip main menu on game startup") - - ("new-game", bpo::value()->implicit_value(true) - ->default_value(false), "run new game sequence (ignored if skip-menu=0)") - - ("fs-strict", bpo::value()->implicit_value(true) - ->default_value(false), "strict file system handling (no case folding)") - - ("encoding", bpo::value()-> - default_value("win1252"), - "Character encoding used in OpenMW game messages:\n" - "\n\twin1250 - Central and Eastern European such as Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian, Serbian (Latin script), Romanian and Albanian languages\n" - "\n\twin1251 - Cyrillic alphabet such as Russian, Bulgarian, Serbian Cyrillic and other languages\n" - "\n\twin1252 - Western European (Latin) alphabet, used by default") - - ("fallback", bpo::value()->default_value(FallbackMap(), "") - ->multitoken()->composing(), "fallback values") - - ("no-grab", bpo::value()->implicit_value(true)->default_value(false), "Don't grab mouse cursor") - - ("export-fonts", bpo::value()->implicit_value(true) - ->default_value(false), "Export Morrowind .fnt fonts to PNG image and XML file in current directory") - - ("activate-dist", bpo::value ()->default_value (-1), "activation distance override") - - ("random-seed", bpo::value () - ->default_value(Misc::Rng::generateDefaultSeed()), - "seed value for random number generator") - ; - - bpo::parsed_options valid_opts = bpo::command_line_parser(argc, argv) - .options(desc).allow_unregistered().run(); + bpo::options_description desc = OpenMW::makeOptionsDescription(); bpo::variables_map variables; - // Runtime options override settings from all configs - bpo::store(valid_opts, variables); + Files::parseArgs(argc, argv, variables, desc); bpo::notify(variables); if (variables.count ("help")) @@ -158,9 +62,9 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat return false; } - bpo::variables_map composingVariables = cfgMgr.separateComposingVariables(variables, desc); + bpo::variables_map composingVariables = Files::separateComposingVariables(variables, desc); cfgMgr.readConfiguration(variables, desc); - cfgMgr.mergeComposingVariables(variables, composingVariables, desc); + Files::mergeComposingVariables(variables, composingVariables, desc); Version::Version v = Version::getOpenmwVersion(variables["resources"].as().mPath.string()); Log(Debug::Info) << v.describe(); diff --git a/apps/openmw/options.cpp b/apps/openmw/options.cpp new file mode 100644 index 0000000000..62ea0910dd --- /dev/null +++ b/apps/openmw/options.cpp @@ -0,0 +1,115 @@ +#include "options.hpp" + +#include +#include +#include + +#include + +namespace +{ + namespace bpo = boost::program_options; +} + +namespace OpenMW +{ + bpo::options_description makeOptionsDescription() + { + bpo::options_description desc("Syntax: openmw \nAllowed options"); + + desc.add_options() + ("help", "print help message") + ("version", "print version information and quit") + + ("replace", bpo::value()->default_value(Files::EscapeStringVector(), "") + ->multitoken()->composing(), "settings where the values from the current source should replace those from lower-priority sources instead of being appended") + + ("data", bpo::value()->default_value(Files::EscapePathContainer(), "data") + ->multitoken()->composing(), "set data directories (later directories have higher priority)") + + ("data-local", bpo::value()->default_value(Files::EscapePath(), ""), + "set local data directory (highest priority)") + + ("fallback-archive", bpo::value()->default_value(Files::EscapeStringVector(), "fallback-archive") + ->multitoken()->composing(), "set fallback BSA archives (later archives have higher priority)") + + ("resources", bpo::value()->default_value(Files::EscapePath(), "resources"), + "set resources directory") + + ("start", bpo::value()->default_value(""), + "set initial cell") + + ("content", bpo::value()->default_value(Files::EscapeStringVector(), "") + ->multitoken()->composing(), "content file(s): esm/esp, or omwgame/omwaddon") + + ("groundcover", bpo::value()->default_value(Files::EscapeStringVector(), "") + ->multitoken()->composing(), "groundcover content file(s): esm/esp, or omwgame/omwaddon") + + ("lua-scripts", bpo::value()->default_value(Files::EscapeStringVector(), "") + ->multitoken()->composing(), "file(s) with a list of global Lua scripts: omwscripts") + + ("no-sound", bpo::value()->implicit_value(true) + ->default_value(false), "disable all sounds") + + ("script-all", bpo::value()->implicit_value(true) + ->default_value(false), "compile all scripts (excluding dialogue scripts) at startup") + + ("script-all-dialogue", bpo::value()->implicit_value(true) + ->default_value(false), "compile all dialogue scripts at startup") + + ("script-console", bpo::value()->implicit_value(true) + ->default_value(false), "enable console-only script functionality") + + ("script-run", bpo::value()->default_value(""), + "select a file containing a list of console commands that is executed on startup") + + ("script-warn", bpo::value()->implicit_value (1) + ->default_value (1), + "handling of warnings when compiling scripts\n" + "\t0 - ignore warning\n" + "\t1 - show warning but consider script as correctly compiled anyway\n" + "\t2 - treat warnings as errors") + + ("script-blacklist", bpo::value()->default_value(Files::EscapeStringVector(), "") + ->multitoken()->composing(), "ignore the specified script (if the use of the blacklist is enabled)") + + ("script-blacklist-use", bpo::value()->implicit_value(true) + ->default_value(true), "enable script blacklisting") + + ("load-savegame", bpo::value()->default_value(Files::EscapePath(), ""), + "load a save game file on game startup (specify an absolute filename or a filename relative to the current working directory)") + + ("skip-menu", bpo::value()->implicit_value(true) + ->default_value(false), "skip main menu on game startup") + + ("new-game", bpo::value()->implicit_value(true) + ->default_value(false), "run new game sequence (ignored if skip-menu=0)") + + ("fs-strict", bpo::value()->implicit_value(true) + ->default_value(false), "strict file system handling (no case folding)") + + ("encoding", bpo::value()-> + default_value("win1252"), + "Character encoding used in OpenMW game messages:\n" + "\n\twin1250 - Central and Eastern European such as Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian, Serbian (Latin script), Romanian and Albanian languages\n" + "\n\twin1251 - Cyrillic alphabet such as Russian, Bulgarian, Serbian Cyrillic and other languages\n" + "\n\twin1252 - Western European (Latin) alphabet, used by default") + + ("fallback", bpo::value()->default_value(Fallback::FallbackMap(), "") + ->multitoken()->composing(), "fallback values") + + ("no-grab", bpo::value()->implicit_value(true)->default_value(false), "Don't grab mouse cursor") + + ("export-fonts", bpo::value()->implicit_value(true) + ->default_value(false), "Export Morrowind .fnt fonts to PNG image and XML file in current directory") + + ("activate-dist", bpo::value ()->default_value (-1), "activation distance override") + + ("random-seed", bpo::value () + ->default_value(Misc::Rng::generateDefaultSeed()), + "seed value for random number generator") + ; + + return desc; + } +} diff --git a/apps/openmw/options.hpp b/apps/openmw/options.hpp new file mode 100644 index 0000000000..246999eb19 --- /dev/null +++ b/apps/openmw/options.hpp @@ -0,0 +1,11 @@ +#ifndef APPS_OPENMW_OPTIONS_H +#define APPS_OPENMW_OPTIONS_H + +#include + +namespace OpenMW +{ + boost::program_options::options_description makeOptionsDescription(); +} + +#endif diff --git a/apps/openmw_test_suite/CMakeLists.txt b/apps/openmw_test_suite/CMakeLists.txt index 7800efac3d..5665f59508 100644 --- a/apps/openmw_test_suite/CMakeLists.txt +++ b/apps/openmw_test_suite/CMakeLists.txt @@ -40,6 +40,9 @@ if (GTEST_FOUND AND GMOCK_FOUND) shader/parsedefines.cpp shader/parsefors.cpp shader/shadermanager.cpp + + ../openmw/options.cpp + openmw/options.cpp ) source_group(apps\\openmw_test_suite FILES openmw_test_suite.cpp ${UNITTEST_SRC_FILES}) diff --git a/apps/openmw_test_suite/openmw/options.cpp b/apps/openmw_test_suite/openmw/options.cpp new file mode 100644 index 0000000000..ebb956b10c --- /dev/null +++ b/apps/openmw_test_suite/openmw/options.cpp @@ -0,0 +1,379 @@ +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +namespace +{ + using namespace testing; + using namespace OpenMW; + + namespace bpo = boost::program_options; + + std::vector generateSupportedCharacters(std::vector&& base = {}) + { + std::vector result = std::move(base); + for (int i = 1; i <= std::numeric_limits::max(); ++i) + if (i != '&' && i != '"' && i != ' ' && i != '@' && i != '\n') + result.push_back(std::string(1, i)); + return result; + } + + constexpr std::array supportedAtSignEscapings { + std::pair {'a', '@'}, + std::pair {'h', '#'}, + }; + + MATCHER_P(IsEscapePath, v, "") { return arg.mPath.string() == v; } + + template + void parseArgs(const T& arguments, bpo::variables_map& variables, bpo::options_description& description) + { + Files::parseArgs(static_cast(arguments.size()), arguments.data(), variables, description); + } + + TEST(OpenMWOptionsFromArguments, should_support_equality_to_separate_flag_and_value) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame=save.omwsave"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "save.omwsave"); + } + + TEST(OpenMWOptionsFromArguments, should_support_single_word_load_savegame_path) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame", "save.omwsave"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "save.omwsave"); + } + + TEST(OpenMWOptionsFromArguments, should_support_multi_component_load_savegame_path) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame", "/home/user/openmw/save.omwsave"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "/home/user/openmw/save.omwsave"); + } + + TEST(OpenMWOptionsFromArguments, should_support_windows_multi_component_load_savegame_path) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame", R"(C:\OpenMW\save.omwsave)"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), R"(C:\OpenMW\save.omwsave)"); + } + + TEST(OpenMWOptionsFromArguments, should_support_load_savegame_path_with_spaces) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame", "my save.omwsave"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "my"); +// EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "my save.omwsave"); + } + + TEST(OpenMWOptionsFromArguments, should_support_load_savegame_path_with_number_sign) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame", "my#save.omwsave"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "my#save.omwsave"); + } + + TEST(OpenMWOptionsFromArguments, should_support_load_savegame_path_with_at_sign) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame", "my@save.omwsave"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "my?ave.omwsave"); +// EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "my@save.omwsave"); + } + + TEST(OpenMWOptionsFromArguments, should_support_load_savegame_path_with_quote) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame", R"(my"save.omwsave)"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), R"(my"save.omwsave)"); + } + + TEST(OpenMWOptionsFromArguments, should_support_quted_load_savegame_path) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame", R"("save".omwsave)"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), R"(save)"); +// EXPECT_EQ(variables["load-savegame"].as().mPath.string(), R"("save".omwsave)"); + } + + TEST(OpenMWOptionsFromArguments, should_support_quoted_load_savegame_path_with_escaped_quote_by_ampersand) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame", R"("save&".omwsave")"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), R"(save".omwsave)"); + } + + TEST(OpenMWOptionsFromArguments, should_support_quoted_load_savegame_path_with_escaped_ampersand) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame", R"("save.omwsave&&")"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "save.omwsave&"); + } + + TEST(OpenMWOptionsFromArguments, should_support_load_savegame_path_with_ampersand) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame", "save&.omwsave"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "save&.omwsave"); + } + + TEST(OpenMWOptionsFromArguments, should_support_load_savegame_path_with_multiple_quotes) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame", R"(my"save".omwsave)"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), R"(my"save".omwsave)"); + } + + TEST(OpenMWOptionsFromArguments, should_compose_data) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--data", "1", "--data", "2"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_THAT(variables["data"].as(), ElementsAre(IsEscapePath("1"), IsEscapePath("2"))); + } + + TEST(OpenMWOptionsFromArguments, should_compose_data_from_single_flag) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--data", "1", "2"}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_THAT(variables["data"].as(), ElementsAre(IsEscapePath("1"), IsEscapePath("2"))); + } + + TEST(OpenMWOptionsFromArguments, should_throw_on_multiple_load_savegame) + { + bpo::options_description description = makeOptionsDescription(); + const std::array arguments {"openmw", "--load-savegame", "1.omwsave", "--load-savegame", "2.omwsave"}; + bpo::variables_map variables; + EXPECT_THROW(parseArgs(arguments, variables, description), std::exception); + } + + struct OpenMWOptionsFromArgumentsStrings : TestWithParam {}; + + TEST_P(OpenMWOptionsFromArgumentsStrings, should_support_paths_with_certain_characters_in_load_savegame_path) + { + bpo::options_description description = makeOptionsDescription(); + const std::string path = "save_" + std::string(GetParam()) + ".omwsave"; + const std::string pathArgument = "\"" + path + "\""; + const std::array arguments {"openmw", "--load-savegame", pathArgument.c_str()}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), path); + } + + INSTANTIATE_TEST_SUITE_P( + SupportedCharacters, + OpenMWOptionsFromArgumentsStrings, + ValuesIn(generateSupportedCharacters({u8"👍", u8"Ъ", u8"Ǽ", "\n"})) + ); + + struct OpenMWOptionsFromArgumentsEscapings : TestWithParam> {}; + + TEST_P(OpenMWOptionsFromArgumentsEscapings, should_support_escaping_with_at_sign_in_load_savegame_path) + { + bpo::options_description description = makeOptionsDescription(); + const std::string path = "save_@" + std::string(1, GetParam().first) + ".omwsave"; + const std::array arguments {"openmw", "--load-savegame", path.c_str()}; + bpo::variables_map variables; + parseArgs(arguments, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), + "save_" + std::string(1, GetParam().second) + ".omwsave"); + } + + INSTANTIATE_TEST_SUITE_P( + SupportedEscapingsWithAtSign, + OpenMWOptionsFromArgumentsEscapings, + ValuesIn(supportedAtSignEscapings) + ); + + TEST(OpenMWOptionsFromConfig, should_support_single_word_load_savegame_path) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream("load-savegame=save.omwsave"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "save.omwsave"); + } + + TEST(OpenMWOptionsFromConfig, should_strip_quotes_from_load_savegame_path) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream(R"(load-savegame="save.omwsave")"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "save.omwsave"); + } + + TEST(OpenMWOptionsFromConfig, should_strip_outer_quotes_from_load_savegame_path) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream(R"(load-savegame=""save".omwsave")"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), ""); +// EXPECT_EQ(variables["load-savegame"].as().mPath.string(), R"(""save".omwsave")"); + } + + TEST(OpenMWOptionsFromConfig, should_strip_quotes_from_load_savegame_path_with_space) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream(R"(load-savegame="my save.omwsave")"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "my save.omwsave"); + } + + TEST(OpenMWOptionsFromConfig, should_support_quoted_load_savegame_path_with_number_sign) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream("load-savegame=save#.omwsave"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "save#.omwsave"); + } + + TEST(OpenMWOptionsFromConfig, should_support_quoted_load_savegame_path_with_at_sign) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream("load-savegame=save@.omwsave"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "save@.omwsave"); + } + + TEST(OpenMWOptionsFromConfig, should_support_quoted_load_savegame_path_with_quote) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream(R"(load-savegame=save".omwsave)"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), R"(save".omwsave)"); + } + + TEST(OpenMWOptionsFromConfig, should_ignore_commented_option) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream("#load-savegame=save.omwsave"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), ""); + } + + TEST(OpenMWOptionsFromConfig, should_throw_on_multiple_load_savegame) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream("load-savegame=1.omwsave\nload-savegame=2.omwsave"); + bpo::variables_map variables; + EXPECT_THROW(Files::parseConfig(stream, variables, description), std::exception); + } + + TEST(OpenMWOptionsFromConfig, should_support_multi_component_load_savegame_path) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream("load-savegame=/home/user/openmw/save.omwsave"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "/home/user/openmw/save.omwsave"); + } + + TEST(OpenMWOptionsFromConfig, should_support_windows_multi_component_load_savegame_path) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream(R"(load-savegame=C:\OpenMW\save.omwsave)"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), R"(C:\OpenMW\save.omwsave)"); + } + + TEST(OpenMWOptionsFromConfig, should_compose_data) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream("data=1\ndata=2"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_THAT(variables["data"].as(), ElementsAre(IsEscapePath("1"), IsEscapePath("2"))); + } + + TEST(OpenMWOptionsFromConfig, should_support_quoted_load_savegame_path_with_escaped_quote_by_ampersand) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream(R"(load-savegame="save&".omwsave")"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), R"(save".omwsave)"); + } + + TEST(OpenMWOptionsFromConfig, should_support_quoted_load_savegame_path_with_escaped_ampersand) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream(R"(load-savegame="save.omwsave&&")"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "save.omwsave&"); + } + + TEST(OpenMWOptionsFromConfig, should_support_load_savegame_path_with_ampersand) + { + bpo::options_description description = makeOptionsDescription(); + std::istringstream stream("load-savegame=save&.omwsave"); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), "save&.omwsave"); + } + + struct OpenMWOptionsFromConfigStrings : TestWithParam {}; + + TEST_P(OpenMWOptionsFromConfigStrings, should_support_paths_with_certain_characters_in_load_savegame_path) + { + bpo::options_description description = makeOptionsDescription(); + const std::string path = "save_" + std::string(GetParam()) + ".omwsave"; + std::istringstream stream("load-savegame=\"" + path + "\""); + bpo::variables_map variables; + Files::parseConfig(stream, variables, description); + EXPECT_EQ(variables["load-savegame"].as().mPath.string(), path); + } + + INSTANTIATE_TEST_SUITE_P( + SupportedCharacters, + OpenMWOptionsFromConfigStrings, + ValuesIn(generateSupportedCharacters({u8"👍", u8"Ъ", u8"Ǽ"})) + ); +} diff --git a/components/files/configurationmanager.cpp b/components/files/configurationmanager.cpp index 35679ef293..b5d8b415f5 100644 --- a/components/files/configurationmanager.cpp +++ b/components/files/configurationmanager.cpp @@ -82,7 +82,8 @@ void ConfigurationManager::readConfiguration(boost::program_options::variables_m mSilent = silent; } -boost::program_options::variables_map ConfigurationManager::separateComposingVariables(boost::program_options::variables_map & variables, boost::program_options::options_description& description) +boost::program_options::variables_map separateComposingVariables(boost::program_options::variables_map & variables, + boost::program_options::options_description& description) { boost::program_options::variables_map composingVariables; for (auto itr = variables.begin(); itr != variables.end();) @@ -98,7 +99,8 @@ boost::program_options::variables_map ConfigurationManager::separateComposingVar return composingVariables; } -void ConfigurationManager::mergeComposingVariables(boost::program_options::variables_map & first, boost::program_options::variables_map & second, boost::program_options::options_description& description) +void mergeComposingVariables(boost::program_options::variables_map& first, boost::program_options::variables_map& second, + boost::program_options::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 replacedVariables; @@ -234,13 +236,10 @@ bool ConfigurationManager::loadConfig(const boost::filesystem::path& path, Log(Debug::Info) << "Loading config file: " << cfgFile.string(); boost::filesystem::ifstream configFileStreamUnfiltered(cfgFile); - boost::iostreams::filtering_istream configFileStream; - configFileStream.push(escape_hash_filter()); - configFileStream.push(configFileStreamUnfiltered); + if (configFileStreamUnfiltered.is_open()) { - boost::program_options::store(boost::program_options::parse_config_file( - configFileStream, description, true), variables); + parseConfig(configFileStreamUnfiltered, variables, description); return true; } @@ -300,4 +299,26 @@ const boost::filesystem::path& ConfigurationManager::getScreenshotPath() const return mScreenshotPath; } +void parseArgs(int argc, const char* const argv[], boost::program_options::variables_map& variables, + boost::program_options::options_description& description) +{ + boost::program_options::store( + boost::program_options::command_line_parser(argc, argv).options(description).allow_unregistered().run(), + variables + ); +} + +void parseConfig(std::istream& stream, boost::program_options::variables_map& variables, + boost::program_options::options_description& description) +{ + boost::iostreams::filtering_istream configFileStream; + configFileStream.push(escape_hash_filter()); + configFileStream.push(stream); + + boost::program_options::store( + boost::program_options::parse_config_file(configFileStream, description, true), + variables + ); +} + } /* namespace Cfg */ diff --git a/components/files/configurationmanager.hpp b/components/files/configurationmanager.hpp index 0ec0a1f67d..99e65a7658 100644 --- a/components/files/configurationmanager.hpp +++ b/components/files/configurationmanager.hpp @@ -25,10 +25,6 @@ struct ConfigurationManager void readConfiguration(boost::program_options::variables_map& variables, boost::program_options::options_description& description, bool quiet=false); - boost::program_options::variables_map separateComposingVariables(boost::program_options::variables_map& variables, boost::program_options::options_description& description); - - void mergeComposingVariables(boost::program_options::variables_map& first, boost::program_options::variables_map& second, boost::program_options::options_description& description); - void processPaths(Files::PathContainer& dataDirs, bool create = false); ///< \param create Try creating the directory, if it does not exist. @@ -68,6 +64,19 @@ struct ConfigurationManager bool mSilent; }; + +boost::program_options::variables_map separateComposingVariables(boost::program_options::variables_map& variables, + boost::program_options::options_description& description); + +void mergeComposingVariables(boost::program_options::variables_map& first, boost::program_options::variables_map& second, + boost::program_options::options_description& description); + +void parseArgs(int argc, const char* const argv[], boost::program_options::variables_map& variables, + boost::program_options::options_description& description); + +void parseConfig(std::istream& stream, boost::program_options::variables_map& variables, + boost::program_options::options_description& description); + } /* namespace Cfg */ #endif /* COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP */