-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersistentManagedCVarStorage.hpp
More file actions
93 lines (78 loc) · 3.2 KB
/
Copy pathPersistentManagedCVarStorage.hpp
File metadata and controls
93 lines (78 loc) · 3.2 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
88
89
90
91
92
93
#ifndef __PERSISTENTMANAGEDCVARSTORAGE_HPP__
#define __PERSISTENTMANAGEDCVARSTORAGE_HPP__
#include <filesystem>
#include <map>
#include "bakkesmod/wrappers/cvarmanagerwrapper.h"
#include "bakkesmod/wrappers/cvarwrapper.h"
#include "bakkesmod/wrappers/gamewrapper.h"
class PersistentManagedCVarStorage {
private:
std::shared_ptr<CVarManagerWrapper> _cv;
std::string _prefix;
std::filesystem::path _storage_file {""};
bool _auto_write = false;
bool _loaded = false;
struct CVarCacheItem {
std::string value;
std::string description;
explicit CVarCacheItem(CVarWrapper cvar);
};
std::map<std::string, CVarCacheItem> _cvar_cache;
static std::filesystem::path GetStorageFilePath(const std::shared_ptr<GameWrapper> & gw, std::string file_name);
void OnPersistentCVarChanged(const std::string & old, CVarWrapper changed_cvar);
public:
/**
* \brief
* PersistentStorage is a convenient class for making sure your cvar values are
* always loaded, and never lost if the main config is rewritten while your plugin
* is unloaded.
*
* \param plugin Pointer to the main plugin
* \param storage_file_name The file of the config file excluding the file extension
* \param auto_write Automatically write cvar value to persistent storage once changed
* \param auto_load Automatically load the cfg after 0.1s
*/
explicit PersistentManagedCVarStorage(
BakkesMod::Plugin::BakkesModPlugin * plugin,
const std::string & storage_file_name,
bool auto_write = false,
bool auto_load = false);
PersistentManagedCVarStorage(const PersistentManagedCVarStorage & other) = delete;
PersistentManagedCVarStorage(PersistentManagedCVarStorage && other) noexcept = delete;
PersistentManagedCVarStorage & operator=(const PersistentManagedCVarStorage & other) = delete;
PersistentManagedCVarStorage & operator=(PersistentManagedCVarStorage && other) noexcept = delete;
~PersistentManagedCVarStorage();
/**
* \brief
* Writes the cvar values to disk
*/
void WritePersistentStorage();
/**
* \brief
* Loads the cvar values from disk
*/
void Load();
/**
* \brief
* Add a single cvar to be persistent
* \param s The cvar name
*/
void AddCVar(const std::string & s);
/**
* \brief
* Adds cvars to be persistent. The cvars have to be registered first.
* \param cvars List of cvar names
*/
void AddCVars(std::initializer_list<std::string> cvars);
/**
* \brief
* Adds cvars to be persistent. The cvars have to be registered first.
* \param cvars List of cvar names
*/
template <class Iterable> void AddCVars(const Iterable cvars) {
for (const auto & cvar_name : cvars) {
AddCVar(cvar_name);
}
}
};
#endif