forked from xenobyte-dev/InstantSuite
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPersistentManagedCVarStorage.cpp
More file actions
87 lines (74 loc) · 3.17 KB
/
PersistentManagedCVarStorage.cpp
File metadata and controls
87 lines (74 loc) · 3.17 KB
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
#include "PersistentManagedCVarStorage.hpp"
#include <fstream>
// #include "CVarManager.hpp" // UNABLE TO WORK RIGHT NOW
#include "Logger.hpp"
namespace {
namespace log = LOGGER;
}; // namespace
PersistentManagedCVarStorage::PersistentManagedCVarStorage(
BakkesMod::Plugin::BakkesModPlugin * plugin,
const std::string & storage_file_name,
const bool auto_write,
bool auto_load) :
_cv(plugin->cvarManager),
_storage_file(GetStorageFilePath(plugin->gameWrapper, storage_file_name)),
_auto_write(auto_write)
{
log::log_info("PersistentStorage: created and will store the data in {}", _storage_file.string());
if (auto_load) {
plugin->gameWrapper->SetTimeout([this](...) { Load(); }, 0.1f);
}
}
PersistentManagedCVarStorage::CVarCacheItem::CVarCacheItem(CVarWrapper cvar) :
value(cvar.getStringValue()), description(cvar.getDescription()) {
}
std::filesystem::path PersistentManagedCVarStorage::GetStorageFilePath(
const std::shared_ptr<GameWrapper> & gw,
std::string file_name) {
return gw->GetBakkesModPath() / "cfg" / file_name.append(".cfg");
}
void PersistentManagedCVarStorage::OnPersistentCVarChanged(const std::string & old, CVarWrapper changed_cvar) {
const auto cvar_name = changed_cvar.getCVarName();
if (auto it = _cvar_cache.find(cvar_name); it != _cvar_cache.end()) {
it->second = CVarCacheItem {changed_cvar};
}
// If you write to file before the file has been loaded,
// you will lose the data there.
if (_auto_write && _loaded) {
WritePersistentStorage();
}
}
void PersistentManagedCVarStorage::WritePersistentStorage() {
std::ofstream out(_storage_file);
log::log_info("PersistentStorage: Writing to file");
for (const auto & [cvar, cvar_cache_item] : _cvar_cache) {
out << std::format("{} \"{}\" //{}\n", cvar, cvar_cache_item.value, cvar_cache_item.description);
}
}
void PersistentManagedCVarStorage::Load() {
if (!std::filesystem::exists(_storage_file)) {
log::log_info("PersistentStorage: {} does not exist yet.", _storage_file.string());
}
log::LOG(log::LOGLEVEL::INFO, "PersistentStorage: Loading the persistent storage cfg.");
_cv->loadCfg(_storage_file.string());
_loaded = true;
}
void PersistentManagedCVarStorage::AddCVar(const std::string & s) {
if (auto cvar = _cv->getCvar(s)) {
_cvar_cache.insert_or_assign(s, CVarCacheItem {cvar});
cvar.addOnValueChanged([this](auto old, auto new_cvar) { OnPersistentCVarChanged(old, new_cvar); });
} else {
log::log_warning(
"PersistentStorage: the cvar {} should be registered before "
"adding it to persistent storage",
s);
}
}
void PersistentManagedCVarStorage::AddCVars(const std::initializer_list<std::string> cvars) {
for (const auto & cvar_name : cvars) {
AddCVar(cvar_name);
}
}
PersistentManagedCVarStorage::~PersistentManagedCVarStorage() {
WritePersistentStorage();
}