summaryrefslogtreecommitdiffstats
path: root/src/frontend_common/config.h
blob: 4798d64324bac5f3540fa778c1327f3c40171bf1 (plain) (blame)
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
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <memory>
#include <string>
#include "common/settings.h"

#define SI_NO_CONVERSION
#include <SimpleIni.h>
#include <boost/algorithm/string/replace.hpp>

// Workaround for conflicting definition in libloaderapi.h caused by SimpleIni
#undef LoadString
#undef CreateFile
#undef DeleteFile
#undef CopyFile
#undef CreateDirectory
#undef MoveFile

namespace Core {
class System;
}

class Config {
public:
    enum class ConfigType {
        GlobalConfig,
        PerGameConfig,
        InputProfile,
    };

    virtual ~Config() = default;

    void ClearControlPlayerValues() const;

    [[nodiscard]] const std::string& GetConfigFilePath() const;

    [[nodiscard]] bool Exists(const std::string& section, const std::string& key) const;

protected:
    explicit Config(ConfigType config_type = ConfigType::GlobalConfig);

    void Initialize(const std::string& config_name = "config");
    void Initialize(std::optional<std::string> config_path);

    void WriteToIni() const;

    void SetUpIni();
    [[nodiscard]] bool IsCustomConfig() const;

    void Reload();

    /**
     * Derived config classes must implement this so they can reload all platform-specific
     * values and global ones.
     */
    virtual void ReloadAllValues() = 0;

    /**
     * Derived config classes must implement this so they can save all platform-specific
     * and global values.
     */
    virtual void SaveAllValues() = 0;

    void ReadValues();
    void ReadPlayerValues(std::size_t player_index);

    void ReadTouchscreenValues();
    void ReadMotionTouchValues();

    // Read functions bases off the respective config section names.
    void ReadAudioValues();
    void ReadControlValues();
    void ReadCoreValues();
    void ReadDataStorageValues();
    void ReadDebuggingValues();
    void ReadServiceValues();
    void ReadDisabledAddOnValues();
    void ReadMiscellaneousValues();
    void ReadCpuValues();
    void ReadRendererValues();
    void ReadScreenshotValues();
    void ReadSystemValues();
    void ReadWebServiceValues();
    void ReadNetworkValues();

    // Read platform specific sections
    virtual void ReadHidbusValues() = 0;
    virtual void ReadDebugControlValues() = 0;
    virtual void ReadPathValues() = 0;
    virtual void ReadShortcutValues() = 0;
    virtual void ReadUIValues() = 0;
    virtual void ReadUIGamelistValues() = 0;
    virtual void ReadUILayoutValues() = 0;
    virtual void ReadMultiplayerValues() = 0;

    void SaveValues();
    void SavePlayerValues(std::size_t player_index);
    void SaveTouchscreenValues();
    void SaveMotionTouchValues();

    // Save functions based off the respective config section names.
    void SaveAudioValues();
    void SaveControlValues();
    void SaveCoreValues();
    void SaveDataStorageValues();
    void SaveDebuggingValues();
    void SaveNetworkValues();
    void SaveDisabledAddOnValues();
    void SaveMiscellaneousValues();
    void SaveCpuValues();
    void SaveRendererValues();
    void SaveScreenshotValues();
    void SaveSystemValues();
    void SaveWebServiceValues();

    // Save platform specific sections
    virtual void SaveHidbusValues() = 0;
    virtual void SaveDebugControlValues() = 0;
    virtual void SavePathValues() = 0;
    virtual void SaveShortcutValues() = 0;
    virtual void SaveUIValues() = 0;
    virtual void SaveUIGamelistValues() = 0;
    virtual void SaveUILayoutValues() = 0;
    virtual void SaveMultiplayerValues() = 0;

    virtual std::vector<Settings::BasicSetting*>& FindRelevantList(Settings::Category category) = 0;

    /**
     * Reads a setting from the qt_config.
     *
     * @param key The setting's identifier
     * @param default_value The value to use when the setting is not already present in the config
     */
    bool ReadBooleanSetting(const std::string& key,
                            std::optional<bool> default_value = std::nullopt);
    s64 ReadIntegerSetting(const std::string& key, std::optional<s64> default_value = std::nullopt);
    u64 ReadUnsignedIntegerSetting(const std::string& key,
                                   std::optional<u64> default_value = std::nullopt);
    double ReadDoubleSetting(const std::string& key,
                             std::optional<double> default_value = std::nullopt);
    std::string ReadStringSetting(const std::string& key,
                                  std::optional<std::string> default_value = std::nullopt);

    /**
     * Writes a setting to the qt_config.
     *
     * @param key The setting's idetentifier
     * @param value Value of the setting
     * @param default_value Default of the setting if not present in config
     * @param use_global Specifies if the custom or global config should be in use, for custom
     * configs
     */
    void WriteBooleanSetting(const std::string& key, const bool& value,
                             const std::optional<bool>& default_value = std::nullopt,
                             const std::optional<bool>& use_global = std::nullopt);
    void WriteDoubleSetting(const std::string& key, const double& value,
                            const std::optional<double>& default_value = std::nullopt,
                            const std::optional<bool>& use_global = std::nullopt);
    void WriteStringSetting(const std::string& key, const std::string& value,
                            const std::optional<std::string>& default_value = std::nullopt,
                            const std::optional<bool>& use_global = std::nullopt);
    template <typename T>
    std::enable_if_t<std::is_integral_v<T>> WriteIntegerSetting(
        const std::string& key, const T& value,
        const std::optional<T>& default_value = std::nullopt,
        const std::optional<bool>& use_global = std::nullopt) {
        std::optional<std::string> string_default = std::nullopt;
        if (default_value.has_value()) {
            string_default = std::make_optional(ToString(default_value.value()));
        }
        WritePreparedSetting(key, AdjustOutputString(ToString(value)), string_default, use_global);
    }

    void ReadCategory(Settings::Category category);
    void WriteCategory(Settings::Category category);
    void ReadSettingGeneric(Settings::BasicSetting* setting);
    void WriteSettingGeneric(const Settings::BasicSetting* setting);

    template <typename T>
    [[nodiscard]] std::string ToString(const T& value_) {
        if constexpr (std::is_same_v<T, std::string>) {
            return value_;
        } else if constexpr (std::is_same_v<T, std::optional<u32>>) {
            return value_.has_value() ? std::to_string(*value_) : "none";
        } else if constexpr (std::is_same_v<T, bool>) {
            return value_ ? "true" : "false";
        } else if constexpr (std::is_same_v<T, u64>) {
            return std::to_string(static_cast<u64>(value_));
        } else if constexpr (std::is_same_v<T, s64>) {
            return std::to_string(static_cast<s64>(value_));
        } else {
            return std::to_string(value_);
        }
    }

    void BeginGroup(const std::string& group);
    void EndGroup();
    std::string GetSection();
    [[nodiscard]] std::string GetGroup() const;
    static std::string AdjustKey(const std::string& key);
    static std::string AdjustOutputString(const std::string& string);
    std::string GetFullKey(const std::string& key, bool skipArrayIndex);
    int BeginArray(const std::string& array);
    void EndArray();
    void SetArrayIndex(int index);

    const ConfigType type;
    std::unique_ptr<CSimpleIniA> config;
    std::string config_loc;
    const bool global;

private:
    void WritePreparedSetting(const std::string& key, const std::string& adjusted_value,
                              const std::optional<std::string>& adjusted_default_value,
                              const std::optional<bool>& use_global);
    void WriteString(const std::string& key, const std::string& value);

    inline static std::array<char, 18> special_characters = {
        '!', '#', '$', '%', '^', '&', '*', '|', ';', '\'', '\"', ',', '<', '>', '?', '`', '~', '='};

    struct ConfigArray {
        std::string name;
        int size;
        int index;
    };
    std::vector<ConfigArray> array_stack;
    std::vector<std::string> key_stack;
};