File: configurationmanager.hpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (110 lines) | stat: -rw-r--r-- 4,179 bytes parent folder | download
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
#ifndef COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP
#define COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP

#include <map>
#include <optional>
#include <stack>
#include <string_view>

#include <components/files/collections.hpp>
#include <components/files/fixedpath.hpp>

namespace boost::program_options
{
    class options_description;
    class variables_map;
}

/**
 * \namespace Files
 */
namespace Files
{
    inline constexpr std::string_view openmwCfgFile = "openmw.cfg";

    /**
     * \struct ConfigurationManager
     */
    struct ConfigurationManager
    {
        ConfigurationManager(bool silent = false); /// @param silent Emit log messages to cout?
        virtual ~ConfigurationManager();

        void readConfiguration(boost::program_options::variables_map& variables,
            const boost::program_options::options_description& description, bool quiet = false);

        void filterOutNonExistingPaths(Files::PathContainer& dataDirs) const;

        // Replaces tokens (`?local?`, `?global?`, etc.) in paths. Adds `basePath` prefix for relative paths.
        void processPath(std::filesystem::path& path, const std::filesystem::path& basePath) const;
        void processPaths(Files::PathContainer& dataDirs, const std::filesystem::path& basePath) const;
        void processPaths(
            boost::program_options::variables_map& variables, const std::filesystem::path& basePath) const;

        /**< Fixed paths */
        const std::filesystem::path& getGlobalPath() const;
        const std::filesystem::path& getLocalPath() const;

        const std::filesystem::path& getUserConfigPath() const;
        const std::filesystem::path& getUserDataPath() const;
        const std::filesystem::path& getInstallPath() const;
        const std::vector<std::filesystem::path>& getActiveConfigPaths() const { return mActiveConfigPaths; }

        const std::filesystem::path& getCachePath() const;

        const std::filesystem::path& getLogPath() const { return getUserConfigPath(); }
        const std::filesystem::path& getScreenshotPath() const;

        static void addCommonOptions(boost::program_options::options_description& description);

    private:
        typedef Files::FixedPath<> FixedPathType;

        typedef const std::filesystem::path& (FixedPathType::*path_type_f)() const;
        typedef std::map<std::u8string, path_type_f> TokensMappingContainer;

        std::optional<boost::program_options::variables_map> loadConfig(
            const std::filesystem::path& path, const boost::program_options::options_description& description) const;

        void addExtraConfigDirs(
            std::stack<std::filesystem::path>& dirs, const boost::program_options::variables_map& variables) const;

        void setupTokensMapping();

        std::vector<std::filesystem::path> mActiveConfigPaths;

        FixedPathType mFixedPath;

        std::filesystem::path mUserDataPath;
        std::filesystem::path mScreenshotPath;

        TokensMappingContainer mTokensMapping;

        bool mSilent;
    };

    boost::program_options::variables_map separateComposingVariables(boost::program_options::variables_map& variables,
        const boost::program_options::options_description& description);

    void mergeComposingVariables(boost::program_options::variables_map& first,
        boost::program_options::variables_map& second, const boost::program_options::options_description& description);

    void parseArgs(int argc, const char* const argv[], boost::program_options::variables_map& variables,
        const boost::program_options::options_description& description);

    void parseConfig(std::istream& stream, boost::program_options::variables_map& variables,
        const boost::program_options::options_description& description);

    class MaybeQuotedPath : public std::filesystem::path
    {
    };

    std::istream& operator>>(std::istream& istream, MaybeQuotedPath& MaybeQuotedPath);

    typedef std::vector<MaybeQuotedPath> MaybeQuotedPathContainer;

    PathContainer asPathContainer(const MaybeQuotedPathContainer& MaybeQuotedPathContainer);

} /* namespace Files */

#endif /* COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP */