forked from jewalky/srvmgr
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver_state.cpp
More file actions
149 lines (121 loc) · 4.7 KB
/
server_state.cpp
File metadata and controls
149 lines (121 loc) · 4.7 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "server_state.h"
#include <fstream>
#include <mutex>
#include "a2types.h"
#include "lib/utils.hpp"
#include "player_settings.h"
ServerState server_state;
const char* server_state_filename = "server_state.dat";
ServerState::ServerState() : map_index(0) {
}
bool ServerState::Save() {
std::ofstream f(server_state_filename, std::ios::out | std::ios::trunc);
if (!f) {
Printf("[server_state] save: failed to open '%s' for writing: %s", server_state_filename, std::strerror(errno));
return false;
}
f << this->map_index << '\n';
// Save map order directly from the game's map array.
const auto* a2_map_files = (A2Array<const char*>*)0x006d15f0;
f << "map_order " << a2_map_files->size;
for (uint32_t i = 0; i < a2_map_files->size; ++i) {
f << ' ' << a2_map_files->data[i];
}
f << '\n';
// Save player settings. We store only autobuff masks and default diplomacy for now.
settings::ForEachReadonly([&f](const std::string& player_name, settings::PlayerSettings* ps) {
if (ps->autobuff_mask != 0) {
f << "player_settings_autobuff_mask " << player_name << ' ' << ps->last_modified << ' ' << ps->autobuff_mask << '\n';
}
if (ps->default_diplomacy != 0) {
f << "player_settings_default_diplomacy " << player_name << ' ' << ps->last_modified << ' ' << ps->default_diplomacy << '\n';
}
});
f.close();
if (!f) {
Printf("[server_state] save: failed to write to '%s': %s", server_state_filename, std::strerror(errno));
return false;
}
return true;
}
std::mutex throttled_save_mutex;
time_t last_save_time = 0;
bool ServerState::ThrottledSave() {
std::lock_guard<std::mutex> lock(throttled_save_mutex);
if (time(NULL) - last_save_time < 60) {
return false;
}
last_save_time = time(NULL);
return Save();
}
bool ServerState::Load() {
std::ifstream f(server_state_filename, std::ios::in);
if (!f) {
Printf("[server_state] load: failed to open '%s' for reading: %s", server_state_filename, std::strerror(errno));
return false;
}
f >> this->map_index;
std::string section;
while (true) {
f >> section;
if (!f) {
break;
}
if (section == "map_order") {
int count;
f >> count;
if (!f) {
Printf("[server_state] load: failed to read map count from '%s': %s", server_state_filename, std::strerror(errno));
return false;
}
if (count < 0 || count > 1000) {
Printf("[server_state] load: invalid map count %d in '%s'", count, server_state_filename);
return false;
}
this->map_order.resize(count);
for (int i = 0; i < count; ++i) {
f >> this->map_order[i];
}
if (!f) {
Printf("[server_state] load: failed to read map order from '%s': %s", server_state_filename, std::strerror(errno));
return false;
}
} else if (section == "player_settings_autobuff_mask") {
std::string player_name;
time_t last_modified;
uint32_t mask;
f >> player_name >> last_modified >> mask;
if (!f) {
Printf("[server_state] load: failed to read player_settings_autobuff_mask from '%s': %s", server_state_filename, std::strerror(errno));
return false;
}
// Skip timestamps older than 1 week.
if (time(NULL) - last_modified > 7 * 24 * 3600) {
continue;
}
auto* ps = settings::FindOrCreate(player_name.c_str());
ps->autobuff_mask = mask;
ps->last_modified = last_modified;
} else if (section == "player_settings_default_diplomacy") {
std::string player_name;
time_t last_modified;
uint32_t mask;
f >> player_name >> last_modified >> mask;
if (!f) {
Printf("[server_state] load: failed to read player_settings_default_diplomacy from '%s': %s", server_state_filename, std::strerror(errno));
return false;
}
// Skip timestamps older than 1 week.
if (time(NULL) - last_modified > 7 * 24 * 3600) {
continue;
}
auto* ps = settings::FindOrCreate(player_name.c_str());
ps->default_diplomacy = mask;
ps->last_modified = last_modified;
} else {
Printf("[server_state] load: unknown section '%s' in '%s'", section.c_str(), server_state_filename);
return false;
}
}
return true;
}