forked from albertvaka/nbgf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsavestate.h
80 lines (65 loc) · 1.96 KB
/
savestate.h
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
#pragma once
#include <unordered_map>
#include <string>
#include <sstream>
struct SaveStream;
#ifdef _DEBUG
// Savefiles will be stored in the working dir instead of the user's config folder
#define SAVESTATE_SAVE_IN_LOCAL_DIR
#endif
struct SaveState
{
// Open creates a SaveState and Loads its contents from disk
[[nodiscard]] static SaveState Open(const std::string& gaemName, int stateNum = 0) { return SaveState(gaemName, stateNum); }
[[nodiscard]] bool HasData() { return !state.empty(); }
SaveState& Clear() {
state.clear();
return *this;
}
[[nodiscard]] bool Has(const std::string& id) const { return state.find(id) != state.cend(); }
[[nodiscard]] std::string Get(const std::string& id) const {
auto data = state.find(id);
if (data == state.cend()) {
return "";
}
return data->second;
}
SaveState& Put(const std::string& id, const std::string& data) {
state[id] = data;
return *this;
}
[[nodiscard]] SaveStream StreamPut(const std::string& id);
[[nodiscard]] std::istringstream StreamGet(const std::string& id) const { return std::istringstream(Get(id)); }
void Save();
private:
SaveState(const std::string& gaemName, int stateNum = 0) : gaemName(gaemName), stateNum(stateNum) {
Load();
}
void Load();
std::string GetSaveFilePath();
SaveState& operator=(const SaveState&) = delete;
SaveState(const SaveState&) = delete;
std::unordered_map<std::string, std::string> state;
std::string gaemName;
int stateNum;
};
struct SaveStream
{
SaveState* state;
std::string id;
std::ostringstream stream;
SaveStream(SaveState* state, const std::string& id) : state(state), id(id) { }
~SaveStream() {
state->Put(id, stream.str());
}
template <typename T>
SaveStream& operator<<(T&& val) {
stream << " " << std::forward<T>(val);
return *this;
}
SaveStream& operator=(const SaveStream&) = delete;
SaveStream(const SaveStream&) = delete;
};
inline SaveStream SaveState::StreamPut(const std::string& id) {
return SaveStream(this, id);
}