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
207 template <typename T>
208 std::optional<T> getValueOrFail(std::string_view sSection, std::string_view sKey) const;
209
215 std::vector<std::string> getAllSections();
216
225 std::variant<std::vector<std::string>, Error> getAllKeysOfSection(std::string_view sSection) const;
226
276 template <typename T>
277 void
278 setValue(std::string_view sSection, std::string_view sKey, T value, std::string_view sComment = "");
279
304 [[nodiscard]] std::optional<Error> saveFile(ConfigCategory category, std::string_view sFileName);
305
321 [[nodiscard]] std::optional<Error>
322 saveFile(std::filesystem::path pathToConfigFile, bool bEnableBackup);
323
330 std::filesystem::path getFilePath() const;
331
332 private:
344 static std::variant<std::filesystem::path, Error>
345 constructFilePath(ConfigCategory category, std::string_view sFileName);
346
355 static std::string generateFreeFileName(
356 const std::set<std::string>& usedFileNames, const std::string& sFileNamePrefix = "");
357
359 toml::value tomlData;
360
362 std::filesystem::path filePath;
363
365 inline static const char* sBackupFileExtension = ".old";
366 };
367
368 template <typename T>
369 T ConfigManager::getValue(std::string_view sSection, std::string_view sKey, T defaultValue) const {
370 if (sSection.empty()) {
371 return toml::find_or(tomlData, sKey.data(), defaultValue);
372 } else {
373 return toml::find_or(tomlData, sSection.data(), sKey.data(), defaultValue);
374 }
375 }
376
377 template <typename T>
379 std::string_view sSection, std::string_view sKey, T value, std::string_view sComment) {
380 if (sSection.empty()) {
381 if (sComment.empty()) {
382 tomlData[sKey.data()] = toml::value(value);
383 } else {
384 tomlData[sKey.data()] = toml::value(value, {sComment.data()});
385 }
386 } else {
387 if (sComment.empty()) {
388 tomlData[sSection.data()][sKey.data()] = toml::value(value);
389 } else {
390 tomlData[sSection.data()][sKey.data()] = toml::value(value, {sComment.data()});
391 }
392 }
393 }
394
395 template <typename T>
396 std::optional<T> ConfigManager::getValueOrFail(std::string_view sSection, std::string_view sKey) const {
397 try {
398 if (sSection.empty()) {
399 return toml::find<T>(tomlData, sKey.data());
400 } else {
401 return toml::find<T>(tomlData, sSection.data(), sKey.data());
402 }
403 } catch (...) {
404 return {};
405 }
406 }
407} // 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:359
std::filesystem::path filePath
Definition: ConfigManager.h:362
static std::string getConfigFormatExtension()
Definition: ConfigManager.cpp:11
static const char * sBackupFileExtension
Definition: ConfigManager.h:365
void setValue(std::string_view sSection, std::string_view sKey, T value, std::string_view sComment="")
Definition: ConfigManager.h:378
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:369
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::optional< T > getValueOrFail(std::string_view sSection, std::string_view sKey) const
Definition: ConfigManager.h:396
std::variant< std::vector< std::string >, Error > getAllKeysOfSection(std::string_view sSection) const
Definition: ConfigManager.cpp:177
Definition: Error.h:27