GekkoNet 1.0
C/C++ Peer to Peer Game Networking SDK
Loading...
Searching...
No Matches
replay.cpp
Go to the documentation of this file.
1#include "replay.h"
2
3#include <algorithm>
4#include <cstdio>
5#include <cstring>
6
7#include "compression.h"
8#include "zpp/zpp_bits.h"
9
10bool Gekko::ReplaySystem::StartRecording(GekkoConfig config, Frame frame, bool save_state, bool disable_compression)
11{
12 Reset();
13
14 _replay.config = config;
15
16 _no_compression = disable_compression;
17
18 if (InputSize() == 0) {
19 Reset();
20 return false;
21 }
22
23 _start_frame = frame;
24 _last_recorded_frame = frame - 1;
25 _last_checksum_frame = frame - 1;
26
27 _needs_state = save_state && config.state_size > 0;
28
29 if (_needs_state) {
30 _state.state = std::make_unique<u8[]>(config.state_size);
31 }
32
34
35 return true;
36}
37
39{
40 length = 0;
41
42 if (_mode != Recording) return nullptr;
43
44 const u8* data = Serialize(length);
45
46 _mode = None;
47
48 return data;
49}
50
52{
53 length = 0;
54
55 if (_mode != Recording) return nullptr;
56
57 return Serialize(length);
58}
59
61{
62 length = 0;
63
65
66 const u32 block = InputSize();
67
68 // a checksum can only be verified for a frame the replay holds the inputs of,
69 // keep later ones aside while serializing.
70 const u64 recorded_frames = _replay.inputs.size() / block;
71 size_t kept = _replay.checksums.size();
72 while (kept > 0 && (u64)_replay.checksums[kept - 1].frame >= recorded_frames) {
73 kept--;
74 }
75 std::vector<ReplayChecksum> later(_replay.checksums.begin() + kept, _replay.checksums.end());
76 _replay.checksums.resize(kept);
77
78 std::vector<u8> packed;
79 if (!_no_compression && block > 0 && !_replay.inputs.empty()) {
80 auto delta = Compression::DeltaEncode(_replay.inputs.data(), (u32)_replay.inputs.size(), block);
81 packed = Compression::RLEEncode(delta.data(), (u32)delta.size());
82 }
83
84 const bool use_packed = !packed.empty() && packed.size() < _replay.inputs.size();
85 if (use_packed) {
86 _replay.inputs.swap(packed);
87 _replay.compressed = true;
88 }
89
90 _bin_buffer.clear();
91 zpp::bits::out out(_bin_buffer);
92 const bool bad = failure(out(
93 _replay.header,
94 _replay.version,
95 _replay.compressed,
96 _replay.config,
97 _replay.inputs,
98 _replay.initial_state,
99 _replay.checksum_interval,
100 _replay.checksums,
101 _replay.user_data
102 ));
103
104 if (use_packed) {
105 _replay.inputs.swap(packed);
106 _replay.compressed = false;
107 }
108
109 _replay.checksums.insert(_replay.checksums.end(), later.begin(), later.end());
110
111 if (bad) {
112 printf("failed to serialize replay data\n");
113 return nullptr;
114 }
115
116 length = (u32)_bin_buffer.size();
117
118 return _bin_buffer.data();
119}
120
122{
123 if (_mode != Recording || _needs_state) return;
124
125 const Frame incorrect = sync.GetMinIncorrectFrame();
126
127 if (incorrect != GameInput::NULL_FRAME) {
128 _last_recorded_frame = std::min(_last_recorded_frame, incorrect - 1);
129 }
130
131 const Frame received = sync.GetMinReceivedFrame();
132
133 std::unique_ptr<u8[]> inputs;
134 for (Frame frame = _last_recorded_frame + 1; frame <= received; frame++) {
135 if (!sync.GetSpectatorInputs(inputs, frame)) {
136 break;
137 }
138
139 RecordInput(frame, inputs.get());
140
141 _last_recorded_frame = frame;
142 }
143}
144
146{
147 if (_mode != Recording || !input) return;
148
149 const u32 size = InputSize();
150 if (size == 0) return;
151
152 if (frame < _start_frame) return;
153
154 const u64 index = (u64)(frame - _start_frame);
155 const u64 needed = (index + 1) * size;
156 if (_replay.inputs.size() < needed) {
157 _replay.inputs.resize(needed);
158 }
159
160 std::memcpy(_replay.inputs.data() + index * size, input, size);
161}
162
163void Gekko::ReplaySystem::RecordState(const u8* state, u32 length, Frame frame)
164{
165 if (_mode != Recording || !state || length == 0) return;
166
167 if (length > _replay.config.state_size) return;
168
169 _replay.initial_state.assign(state, state + length);
170
171 _replay.initial_state.resize(_replay.config.state_size);
172
173 _start_frame = frame + 1;
174 _last_recorded_frame = frame;
175 _last_checksum_frame = frame;
176
177 _needs_state = false;
178}
179
181{
182 if (_mode != Recording) return false;
183
184 _replay.checksum_interval = interval;
185
186 return true;
187}
188
189bool Gekko::ReplaySystem::SetUserData(const u8* data, u32 length)
190{
191 if (_mode != Recording || (!data && length > 0)) return false;
192
193 if (length == 0) {
194 _replay.user_data.clear();
195 }
196 else {
197 _replay.user_data.assign(data, data + length);
198 }
199
200 return true;
201}
202
204{
205 if (_mode != Recording || _needs_state || _replay.checksum_interval == 0) return false;
206
207 if (frame < 0 || frame < _start_frame || frame <= _last_checksum_frame) return false;
208
209 return frame % (Frame)_replay.checksum_interval == 0;
210}
211
213{
214 if (_mode != Recording || _needs_state || _replay.checksum_interval == 0) return;
215
216 for (Frame frame = std::max(_last_checksum_frame + 1, _start_frame); frame <= up_to; frame++) {
217 // frames the session did not save, or whose save got overwritten, stay without one.
218 auto state = storage.GetState(frame);
219 if (state->frame == frame) {
220 RecordChecksum(frame, state->checksum);
221 }
222 }
223
225}
226
228{
229 if (!WantsChecksum(frame)) return;
230
231 _replay.checksums.push_back({ frame - _start_frame, checksum });
232
233 _last_checksum_frame = frame;
234}
235
242
249
251{
252 if (!_pending_state) return;
253
254 _pending_state = false;
255
256 RecordState(_state.state.get(), _state.state_len, _state.frame);
257}
258
259bool Gekko::ReplaySystem::LoadReplay(const u8* replay_data, u32 length)
260{
261 Reset();
262
263 if (!replay_data || length == 0) return false;
264
265 _bin_buffer.assign(replay_data, replay_data + length);
266
267 bool bad = true;
268 try {
269 zpp::bits::in in(_bin_buffer);
270 bad = failure(in(_replay.header, _replay.version));
271
272 if (!bad && _replay.header == ReplayBlob::MAGIC) {
273 if (_replay.version == ReplayBlob::FORMAT_VERSION_1) {
274 bad = failure(in(
275 _replay.compressed,
276 _replay.config,
277 _replay.inputs,
278 _replay.initial_state
279 ));
280 }
281 else if (_replay.version == ReplayBlob::FORMAT_VERSION) {
282 bad = failure(in(
283 _replay.compressed,
284 _replay.config,
285 _replay.inputs,
286 _replay.initial_state,
287 _replay.checksum_interval,
288 _replay.checksums,
289 _replay.user_data
290 ));
291 }
292 }
293 }
294 catch (...) {
295 bad = true;
296 }
297
298 if (bad) {
299 printf("failed to deserialize replay data\n");
300 Reset();
301 return false;
302 }
303
304 if (_replay.header != ReplayBlob::MAGIC) {
305 printf("invalid replay header\n");
306 Reset();
307 return false;
308 }
309
310 if (_replay.version != ReplayBlob::FORMAT_VERSION &&
312 printf("unsupported replay version %u\n", _replay.version);
313 Reset();
314 return false;
315 }
316
317 if (InputSize() == 0) {
318 printf("invalid replay config\n");
319 Reset();
320 return false;
321 }
322
323 if (!_replay.initial_state.empty() &&
324 _replay.initial_state.size() != _replay.config.state_size) {
325 printf("invalid replay state size\n");
326 Reset();
327 return false;
328 }
329
330 if (_replay.compressed) {
331 if (_replay.inputs.size() % 2 != 0) {
332 printf("invalid replay input data\n");
333 Reset();
334 return false;
335 }
336
337 auto delta = Compression::RLEDecode(_replay.inputs.data(), (u32)_replay.inputs.size());
338 _replay.inputs = Compression::DeltaDecode(delta.data(), (u32)delta.size(), InputSize());
339 _replay.compressed = false;
340 }
341
342 if (!ValidChecksums()) {
343 printf("invalid replay checksums\n");
344 Reset();
345 return false;
346 }
347
348 _current_frame = 0;
349 _next_checksum = 0;
351
352 return true;
353}
354
356{
357 if (_replay.initial_state.empty()) return nullptr;
358
359 return _replay.initial_state.data();
360}
361
363{
364 if (_mode != Replaying || !input) return false;
365
366 const u32 size = InputSize();
367 if (size == 0) return false;
368
369 const u64 offset = (u64)_current_frame * size;
370 if (offset + size > _replay.inputs.size()) return false;
371
372 std::memcpy(input, _replay.inputs.data() + offset, size);
374
375 return true;
376}
377
379{
380 if (_mode != Replaying) return false;
381
382 const auto& checksums = _replay.checksums;
383 while (_next_checksum < checksums.size() && checksums[_next_checksum].frame < frame) {
385 }
386
387 if (_next_checksum >= checksums.size() || checksums[_next_checksum].frame != frame) {
388 return false;
389 }
390
391 checksum = checksums[_next_checksum].checksum;
392
393 return true;
394}
395
397{
398 length = 0;
399
400 if (_mode != Replaying || _replay.user_data.empty()) return nullptr;
401
402 length = (u32)_replay.user_data.size();
403
404 return _replay.user_data.data();
405}
406
408{
409 return _replay.config;
410}
411
413{
414 return _mode == Recording;
415}
416
418{
419 return _mode == Replaying;
420}
421
423{
424 _mode = None;
425 _start_frame = 0;
426 _current_frame = 0;
429 _next_checksum = 0;
430
431 _needs_state = false;
432 _pending_state = false;
433 _no_compression = false;
434
435 _bin_buffer.clear();
436
437 _state = StateEntry();
438
439 _replay = {};
440}
441
443{
444 return _replay.config.input_size * _replay.config.num_players;
445}
446
448{
449 if (_replay.checksums.empty()) return true;
450
451 if (_replay.checksum_interval == 0) return false;
452
453 // checksums belong to recorded frames and are stored in frame order.
454 const u64 frames = _replay.inputs.size() / InputSize();
455
456 Frame previous = GameInput::NULL_FRAME;
457 for (auto& entry : _replay.checksums) {
458 if (entry.frame <= previous || (u64)entry.frame >= frames) {
459 return false;
460 }
461 previous = entry.frame;
462 }
463
464 return true;
465}
StateEntry * GetState(Frame frame)
Definition storage.cpp:30
bool GetSpectatorInputs(std::unique_ptr< u8[]> &inputs, Frame frame)
Definition sync.cpp:63
Frame GetMinIncorrectFrame()
Definition sync.cpp:138
Frame GetMinReceivedFrame()
Definition sync.cpp:151
uint64_t u64
Definition gekko_types.h:8
int32_t Frame
Definition gekko_types.h:18
uint8_t u8
Definition gekko_types.h:5
uint32_t u32
Definition gekko_types.h:7
unsigned int state_size
Definition gekkonet.h:71
static std::vector< uint8_t > DeltaDecode(const uint8_t *data, u32 length, u32 stride)
Definition compression.h:67
static std::vector< uint8_t > RLEEncode(const uint8_t *data, u32 length)
Definition compression.h:19
static std::vector< uint8_t > DeltaEncode(const uint8_t *data, u32 length, u32 stride)
Definition compression.h:59
static std::vector< uint8_t > RLEDecode(const uint8_t *data, u32 length)
Definition compression.h:38
static const i32 NULL_FRAME
Definition input.h:21
static constexpr u32 MAGIC
Definition replay.h:18
static constexpr u32 FORMAT_VERSION_1
Definition replay.h:21
static constexpr u32 FORMAT_VERSION
Definition replay.h:19
bool NextReplayInput(u8 *input)
Definition replay.cpp:362
std::vector< u8 > _bin_buffer
Definition replay.h:99
void RecordState(const u8 *state, u32 length, Frame frame)
Definition replay.cpp:163
Frame _last_checksum_frame
Definition replay.h:95
const u8 * UserData(u32 &length) const
Definition replay.cpp:396
bool IsReplaying() const
Definition replay.cpp:417
bool SetUserData(const u8 *data, u32 length)
Definition replay.cpp:189
GekkoConfig Config()
Definition replay.cpp:407
StateEntry * PendingState()
Definition replay.cpp:243
bool WantsChecksum(Frame frame) const
Definition replay.cpp:203
bool RecordedChecksum(Frame frame, u32 &checksum)
Definition replay.cpp:378
const u8 * Serialize(u32 &length)
Definition replay.cpp:60
StateEntry _state
Definition replay.h:101
bool ValidChecksums() const
Definition replay.cpp:447
void RecordInputs(SyncSystem &sync)
Definition replay.cpp:121
ReplayBlob _replay
Definition replay.h:103
void RecordPendingState()
Definition replay.cpp:250
Frame _last_recorded_frame
Definition replay.h:94
const u8 * ReplayState()
Definition replay.cpp:355
enum Gekko::ReplaySystem::Mode _mode
void RecordChecksum(Frame frame, u32 checksum)
Definition replay.cpp:227
bool SetChecksumInterval(u32 interval)
Definition replay.cpp:180
bool StartRecording(GekkoConfig config, Frame frame, bool save_state, bool disable_compression)
Definition replay.cpp:10
bool IsRecording() const
Definition replay.cpp:412
bool LoadReplay(const u8 *replay_data, u32 length)
Definition replay.cpp:259
const u8 * PeekRecording(u32 &length)
Definition replay.cpp:51
const u8 * StopRecording(u32 &length)
Definition replay.cpp:38
u32 InputSize() const
Definition replay.cpp:442
void RecordInput(Frame frame, const u8 *input)
Definition replay.cpp:145
Frame _current_frame
Definition replay.h:93
void RecordChecksums(StateStorage &storage, Frame up_to)
Definition replay.cpp:212