Nameless Engine
Loading...
Searching...
No Matches
ConfigManager.h
1#pragma once
2
3// Standard.
4#include <variant>
5#include <optional>
6#include <set>
7#include <filesystem>
8
9// Custom.
10#include "misc/Error.h"
11
12// External.
13#define TOML11_PRESERVE_COMMENTS_BY_DEFAULT
14#include "toml11/toml.hpp"
15
16namespace ne {
20 enum class ConfigCategory {
21 PROGRESS, // used to store player's game progress (uses backup files under the hood)
22 SETTINGS // used to store player's game specific settings (no backup files)
23 };
24
29 public:
34 ConfigManager() = default;
35
36 ConfigManager(const ConfigManager&) = delete;
37 ConfigManager& operator=(const ConfigManager&) = delete;
38
44 static std::string getConfigFormatExtension();
45
51 static std::string getBackupFileExtension();
52
70 static std::set<std::string> getAllFileNames(ConfigCategory category);
71
82 static std::string getFreeProgressProfileName();
83
92 static std::filesystem::path getCategoryDirectory(ConfigCategory category);
93
108 [[nodiscard]] static std::optional<Error>
109 removeFile(ConfigCategory category, std::string_view sFileName);
110
119 static void removeFile(std::filesystem::path pathToConfigFile);
120
137 [[nodiscard]] std::optional<Error> loadFile(ConfigCategory category, std::string_view sFileName);
138
155 [[nodiscard]] std::optional<Error> loadFile(std::filesystem::path pathToConfigFile);
156
196 template <typename T>
197 T getValue(std::string_view sSection, std::string_view sKey, T defaultValue) const;
198
204 std::vector<std::string> getAllSections();
205
214 std::variant<std::vector<std::string>, Error> getAllKeysOfSection(std::string_view sSection) const;
215
265 template <typename T>
266 void
267 setValue(std::string_view sSection, std::string_view sKey, T value, std::string_view sComment = "");
268
293 [[nodiscard]] std::optional<Error> saveFile(ConfigCategory category, std::string_view sFileName);
294
310 [[nodiscard]] std::optional<Error>
311 saveFile(std::filesystem::path pathToConfigFile, bool bEnableBackup);
312
319 std::filesystem::path getFilePath() const;
320
321 private:
333 static std::variant<std::filesystem::path, Error>
334 constructFilePath(ConfigCategory category, std::string_view sFileName);
335
344 static std::string generateFreeFileName(
345 const std::set<std::string>& usedFileNames, const std::string& sFileNamePrefix = "");
346
348 toml::value tomlData;
349
351 std::filesystem::path filePath;
352
354 inline static const char* sBackupFileExtension = ".old";
355 };
356
357 template <typename T>
358 T ConfigManager::getValue(std::string_view sSection, std::string_view sKey, T defaultValue) const {
359 if (sSection.empty()) {
360 return toml::find_or(tomlData, sKey.data(), defaultValue);
361 } else {
362 return toml::find_or(tomlData, sSection.data(), sKey.data(), defaultValue);
363 }
364 }
365
366 template <typename T>
368 std::string_view sSection, std::string_view sKey, T value, std::string_view sComment) {
369 if (sSection.empty()) {
370 if (sComment.empty()) {
371 tomlData[sKey.data()] = toml::value(value);
372 } else {
373 tomlData[sKey.data()] = toml::value(value, {sComment.data()});
374 }
375 } else {
376 if (sComment.empty()) {
377 tomlData[sSection.data()][sKey.data()] = toml::value(value);
378 } else {
379 tomlData[sSection.data()][sKey.data()] = toml::value(value, {sComment.data()});
380 }
381 }
382 }
383} // namespace ne
Definition: ConfigManager.h:28
static std::set< std::string > getAllFileNames(ConfigCategory category)
Definition: ConfigManager.cpp:15
static std::string getBackupFileExtension()
Definition: ConfigManager.cpp:13
static std::variant< std::filesystem::path, Error > constructFilePath(ConfigCategory category, std::string_view sFileName)
Definition: ConfigManager.cpp:283
std::filesystem::path getFilePath() const
Definition: ConfigManager.cpp:280
std::optional< Error > loadFile(ConfigCategory category, std::string_view sFileName)
Definition: ConfigManager.cpp:116
static std::filesystem::path getCategoryDirectory(ConfigCategory category)
Definition: ConfigManager.cpp:56
static std::string getFreeProgressProfileName()
Definition: ConfigManager.cpp:51
toml::value tomlData
Definition: ConfigManager.h:348
std::filesystem::path filePath
Definition: ConfigManager.h:351
static std::string getConfigFormatExtension()
Definition: ConfigManager.cpp:11
static const char * sBackupFileExtension
Definition: ConfigManager.h:354
void setValue(std::string_view sSection, std::string_view sKey, T value, std::string_view sComment="")
Definition: ConfigManager.h:367
std::vector< std::string > getAllSections()
Definition: ConfigManager.cpp:163
std::optional< Error > saveFile(ConfigCategory category, std::string_view sFileName)
Definition: ConfigManager.cpp:199
ConfigManager()=default
T getValue(std::string_view sSection, std::string_view sKey, T defaultValue) const
Definition: ConfigManager.h:358
static std::string generateFreeFileName(const std::set< std::string > &usedFileNames, const std::string &sFileNamePrefix="")
Definition: ConfigManager.cpp:318
static std::optional< Error > removeFile(ConfigCategory category, std::string_view sFileName)
Definition: ConfigManager.cpp:75
std::variant< std::vector< std::string >, Error > getAllKeysOfSection(std::string_view sSection) const
Definition: ConfigManager.cpp:177
Definition: Error.h:27