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#include "toml11/single_include/toml.hpp"
14
15namespace ne {
19 enum class ConfigCategory {
20 PROGRESS, // used to store player's game progress (uses backup files under the hood)
21 SETTINGS // used to store player's game specific settings (no backup files)
22 };
23
28 public:
33 ConfigManager() = default;
34
35 ConfigManager(const ConfigManager&) = delete;
36 ConfigManager& operator=(const ConfigManager&) = delete;
37
43 static std::string getConfigFormatExtension();
44
50 static std::string getBackupFileExtension();
51
69 static std::set<std::string> getAllFileNames(ConfigCategory category);
70
81 static std::string getFreeProgressProfileName();
82
91 static std::filesystem::path getCategoryDirectory(ConfigCategory category);
92
107 [[nodiscard]] static std::optional<Error>
108 removeFile(ConfigCategory category, std::string_view sFileName);
109
118 static void removeFile(std::filesystem::path pathToConfigFile);
119
136 [[nodiscard]] std::optional<Error> loadFile(ConfigCategory category, std::string_view sFileName);
137
154 [[nodiscard]] std::optional<Error> loadFile(std::filesystem::path pathToConfigFile);
155
195 template <typename T>
196 T getValue(std::string_view sSection, std::string_view sKey, T defaultValue) const;
197
203 std::vector<std::string> getAllSections();
204
213 std::variant<std::vector<std::string>, Error> getAllKeysOfSection(std::string_view sSection) const;
214
264 template <typename T>
265 void
266 setValue(std::string_view sSection, std::string_view sKey, T value, std::string_view sComment = "");
267
292 [[nodiscard]] std::optional<Error> saveFile(ConfigCategory category, std::string_view sFileName);
293
309 [[nodiscard]] std::optional<Error>
310 saveFile(std::filesystem::path pathToConfigFile, bool bEnableBackup);
311
318 std::filesystem::path getFilePath() const;
319
320 private:
332 static std::variant<std::filesystem::path, Error>
333 constructFilePath(ConfigCategory category, std::string_view sFileName);
334
343 static std::string generateFreeFileName(
344 const std::set<std::string>& usedFileNames, const std::string& sFileNamePrefix = "");
345
347 toml::value tomlData;
348
350 std::filesystem::path filePath;
351
353 inline static const char* sBackupFileExtension = ".old";
354 };
355
356 template <typename T>
357 T ConfigManager::getValue(std::string_view sSection, std::string_view sKey, T defaultValue) const {
358 if (sSection.empty()) {
359 return toml::find_or(tomlData, sKey.data(), defaultValue);
360 } else {
361 return toml::find_or(tomlData, sSection.data(), sKey.data(), defaultValue);
362 }
363 }
364
365 template <typename T>
367 std::string_view sSection, std::string_view sKey, T value, std::string_view sComment) {
368 if (sSection.empty()) {
369 if (sComment.empty()) {
370 tomlData[sKey.data()] = toml::value(value);
371 } else {
372 tomlData[sKey.data()] = toml::value(value, {sComment.data()});
373 }
374 } else {
375 if (sComment.empty()) {
376 tomlData[sSection.data()][sKey.data()] = toml::value(value);
377 } else {
378 tomlData[sSection.data()][sKey.data()] = toml::value(value, {sComment.data()});
379 }
380 }
381 }
382} // namespace ne
Definition: ConfigManager.h:27
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:347
std::filesystem::path filePath
Definition: ConfigManager.h:350
static std::string getConfigFormatExtension()
Definition: ConfigManager.cpp:11
static const char * sBackupFileExtension
Definition: ConfigManager.h:353
void setValue(std::string_view sSection, std::string_view sKey, T value, std::string_view sComment="")
Definition: ConfigManager.h:366
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:357
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