aboutsummaryrefslogtreecommitdiff
path: root/src/client/settings.cc
blob: 3253f609619e30529f787cdb7d84aeaa0c399a60 (plain)
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
#include "client/settings.hh"

namespace {
const std::string home_dir = getenv("HOME");
const char* const config_dir = "/.config/blockgame_linux/";
const char* const config_name = "blockgame_linux.conf";
} // namespace

namespace client {
namespace settings {

static std::string get_settings_file() {
    std::filesystem::create_directories(home_dir + ::config_dir);
    std::ifstream in{home_dir + ::config_dir + config_name,
                     std::fstream::in | std::fstream::app};

    // this must end with a newline, IT IS NOT UP FOR NEGOTIATION
    const std::string file{std::istreambuf_iterator<char>(in),
                           std::istreambuf_iterator<char>()};
    return file.ends_with('\n') ? file : file + '\n';
}

static void set_settings_file(const std::string_view contents) {
    std::filesystem::create_directories(home_dir + ::config_dir);
    std::ofstream out{home_dir + ::config_dir + ::config_name,
                      std::fstream::out | std::fstream::trunc};
    out << contents;
}

// Settings maps subkeys to std::string values (ie, "fov" -> "100.0"f).
// Heading maps setting to headings (ie, "camera -> {map with fov, 100.0f}).
using value_map = std::unordered_map<std::string, std::string>;
using setting_map = std::unordered_map<std::string, value_map>;
static setting_map& get_settings() {
    static setting_map ret = []() -> setting_map {
        setting_map settings{};

        std::stringstream ss{get_settings_file()};
        std::string heading = "default";
        for (std::string line; std::getline(ss, line);) {
            if (line.empty()) {
                continue;
            }

            if (line.starts_with('[') && line.ends_with(']')) {
                heading = std::string{std::begin(line) + 1, std::end(line) - 1};
                continue;
            }

            const size_t split_pos = line.find_first_of(':');

            if (split_pos == std::string::npos) {
                shared::print::warn
                    << "client: failed to parse line in settings file, "
                       "consider manual intervention\n";
                continue;
            }

            const std::string key{std::begin(line),
                                  std::begin(line) +
                                      static_cast<long>(split_pos)};
            const std::string value{std::begin(line) +
                                        static_cast<long>(split_pos) + 1,
                                    std::end(line)};

            value_map& values = settings[heading];

            values.emplace(key, value);
        }
        return settings;
    }();

    return ret;
}

std::optional<std::string> maybe_get_setting_str(const setting_pair_t& loc) {
    const auto& [heading, name] = loc;

    setting_map& settings = get_settings();
    value_map& values = settings[heading];

    const auto it = values.find(name);
    if (it == std::end(values)) {
        return std::nullopt;
    }
    return it->second;
}

std::string get_setting_str(const setting_pair_t& loc,
                            const std::string default_value) {
    const auto& [heading, name] = loc;

    setting_map& settings = get_settings();
    value_map& values = settings[heading];

    const auto find_it = values.find(name);
    if (find_it == std::end(values)) {
        values.emplace(name, default_value);
        return default_value;
    }
    return find_it->second;
}

void set_setting_str(const setting_pair_t& loc, const std::string& value) {
    const auto& [heading, name] = loc;

    setting_map& settings = get_settings();
    value_map& values = settings[heading];

    const auto find_it = values.find(name);
    if (find_it == std::end(values)) {
        values.emplace(name, value);
        return;
    }
    find_it->second = value;
}

void save() {
    std::string contents;

    const setting_map& settings = get_settings();

    // We alphabetically order the headings, and the key:values between them.
    std::vector<std::string> headings;
    std::ranges::transform(std::begin(settings), std::end(settings),
                           std::back_inserter(headings),
                           [](const auto& map) { return map.first; });
    std::ranges::sort(headings);

    for (const auto& heading : headings) {
        contents.append('[' + heading + "]\n");

        const value_map& values = settings.find(heading)->second;
        std::vector<std::string> keys;
        std::ranges::transform(
            std::begin(values), std::end(values), std::back_inserter(keys),
            [](const auto& key_val) { return key_val.first; });
        std::ranges::sort(keys);

        for (const auto& key : keys) {
            contents.append(key + ':' + values.find(key)->second + '\n');
        }
    }

    set_settings_file(contents);
}

} // namespace settings
} // namespace client