-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEventRecorder.h
More file actions
117 lines (98 loc) · 3.33 KB
/
EventRecorder.h
File metadata and controls
117 lines (98 loc) · 3.33 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
#pragma once
#ifndef EVENTRECORDER_H
#define EVENTRECORDER_H
/*
Singleton event recorder class whose job is to:
- keep the last 30M events in a buffer
- store the state of RAM before the recording
- stop if the recording reaches max
- provide an ImGui interface to:
- turn on-off recording
- save and load recordings
- replay recordings
*/
#include "common.h"
#include "SDHRNetworking.h" // for SDHREvent
#include <vector>
#include <string>
#include <thread>
#define RECORDER_TOTALMEMSIZE 128 * 1024 // 128k memory snapshot
#define RECORDER_MEM_SNAPSHOT_CYCLES 1'000'000 // snapshot memory every x cycles
enum class EventRecorderStates_e
{
DISABLED = 0,
RECORDING,
STOPPED, // Anything at this state or later means the recorder is in REPLAY mode
PAUSED,
PLAYING,
TOTAL_COUNT
};
class EventRecorder
{
public:
void RecordEvent(SDHREvent* sdhr_event);
void DisplayImGuiWindow(bool* p_open);
void SetPAL(bool isPal); // Sets PAL (true) or NTSC (false)
inline const EventRecorderStates_e GetState() { return m_state; };
inline const bool IsRecording() { return (m_state == EventRecorderStates_e::RECORDING); };
inline const bool IsInReplayMode() { return (m_state >= EventRecorderStates_e::STOPPED); };
// public singleton code
static EventRecorder* GetInstance()
{
if (NULL == s_instance)
s_instance = new EventRecorder();
return s_instance;
}
~EventRecorder();
// This method reads a binary recording file previously saved using SaveRecording()
void ReadRecordingFile(std::ifstream& file);
// This method reads a text event file, generally used for debugging
void ReadTextEventsFromFile(std::ifstream& file);
// This method reads a PaintWorks Animations file, also for debugging
void ReadPaintWorksAnimationsFile(std::ifstream& file);
void StopReplay();
void StartReplay();
private:
void Initialize();
// recording
void StartRecording();
void StopRecording();
void ClearRecording();
void SaveRecording();
void LoadRecording();
void LoadTextEventsFromFile();
// replay
void PauseReplay(bool pause);
void RewindReplay();
// de/serialization
void MakeRAMSnapshot(size_t cycle);
void ApplyRAMSnapshot(size_t snapshot_index);
void WriteRecordingFile(std::ofstream& file);
void WriteEvent(const SDHREvent& event, std::ofstream& file);
void ReadEvent(std::ifstream& file);
bool bIsPAL = false; // Is the machine PAL?
bool bHasRecording = false;
EventRecorderStates_e m_state = EventRecorderStates_e::DISABLED;
void SetState(EventRecorderStates_e _state);
std::vector<ByteBuffer> v_memSnapshots; // memory snapshots at regular intervals
std::vector<SDHREvent> v_events;
// Replay thread control
std::thread thread_replay;
int replay_events_thread(bool* shouldPauseReplay, bool* shouldStopReplay);
int slowdownMultiplier = 1; // How much to slow the replay down by
bool bShouldPauseReplay = false;
bool bShouldStopReplay = false;
size_t currentReplayEvent; // index of the event ready to replay in the vector
bool bUserMovedEventSlider; // user moved the slider for events
bool bImGuiOpenModal = false;
std::string m_lastErrorString;
//////////////////////////////////////////////////////////////////////////
// Singleton pattern
//////////////////////////////////////////////////////////////////////////
static EventRecorder* s_instance;
EventRecorder()
{
Initialize();
}
};
#endif // EVENTRECORDER_H