DDraceNetwork Docs
demo.h
Go to the documentation of this file.
1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_SHARED_DEMO_H
4#define ENGINE_SHARED_DEMO_H
5
6#include <base/hash.h>
7
8#include <engine/demo.h>
10
11#include <functional>
12#include <vector>
13
14#include "snapshot.h"
15
16typedef std::function<void()> TUpdateIntraTimesFunc;
17
19{
22
28
31
34
36
38 void *m_pUser;
39
40 void WriteTickMarker(int Tick, bool Keyframe);
41 void Write(int Type, const void *pData, int Size);
42
43public:
44 CDemoRecorder(class CSnapshotDelta *pSnapshotDelta, bool NoMapData = false);
46 ~CDemoRecorder() override;
47
48 int Start(class IStorage *pStorage, class IConsole *pConsole, const char *pFilename, const char *pNetversion, const char *pMap, const SHA256_DIGEST &Sha256, unsigned MapCrc, const char *pType, unsigned MapSize, unsigned char *pMapData, IOHANDLE MapFile, DEMOFUNC_FILTER pfnFilter, void *pUser);
49 int Stop(IDemoRecorder::EStopMode Mode, const char *pTargetFilename = "") override;
50
51 void AddDemoMarker();
52 void AddDemoMarker(int Tick);
53
54 void RecordSnapshot(int Tick, const void *pData, int Size);
55 void RecordMessage(const void *pData, int Size);
56
57 bool IsRecording() const override { return m_File != nullptr; }
58 const char *CurrentFilename() const override { return m_aCurrentFilename; }
59
60 int Length() const override { return (m_LastTickMarker - m_FirstTick) / SERVER_TICK_SPEED; }
61};
62
64{
65public:
67 {
68 public:
69 virtual ~IListener() {}
70 virtual void OnDemoPlayerSnapshot(void *pData, int Size) = 0;
71 virtual void OnDemoPlayerMessage(void *pData, int Size) = 0;
72 };
73
75 {
78
80
81 int64_t m_LastUpdate;
83
86
90 };
91
92private:
94
96
97 // Playback
98 struct SKeyFrame
99 {
100 int64_t m_Filepos;
102
103 SKeyFrame(int64_t Filepos, int Tick) :
104 m_Filepos(Filepos), m_Tick(Tick)
105 {
106 }
107 };
108
111 int64_t m_MapOffset;
114 std::vector<SKeyFrame> m_vKeyFrames;
117
121
122 // Depending on the chunk header
123 // this is either a full CSnapshot or a CSnapshotDelta.
125 // Storage for the full snapshot
126 // where the delta gets unpacked into.
131
133#if defined(CONF_VIDEORECORDER)
134 bool m_WasRecording = false;
135#endif
136
138 {
142 };
143 EReadChunkHeaderResult ReadChunkHeader(int *pType, int *pSize, int *pTick);
144 void DoTick();
145 bool ScanFile();
146
147 int64_t Time();
149
150public:
151 CDemoPlayer(class CSnapshotDelta *pSnapshotDelta, bool UseVideo);
152 CDemoPlayer(class CSnapshotDelta *pSnapshotDelta, bool UseVideo, TUpdateIntraTimesFunc &&UpdateIntraTimesFunc);
153 ~CDemoPlayer() override;
154
155 void Construct(class CSnapshotDelta *pSnapshotDelta, bool UseVideo);
156
157 void SetListener(IListener *pListener);
158
159 int Load(class IStorage *pStorage, class IConsole *pConsole, const char *pFilename, int StorageType);
160 unsigned char *GetMapData(class IStorage *pStorage);
161 bool ExtractMap(class IStorage *pStorage);
162 int Play();
163 void Pause() override;
164 void Unpause() override;
165 void Stop(const char *pErrorMessage = "");
166 void SetSpeed(float Speed) override;
167 void SetSpeedIndex(int SpeedIndex) override;
168 void AdjustSpeedIndex(int Offset) override;
169 int SeekPercent(float Percent) override;
170 int SeekTime(float Seconds) override;
171 int SeekTick(ETickOffset TickOffset) override;
172 int SetPos(int WantedTick) override;
173 const CInfo *BaseInfo() const override { return &m_Info.m_Info; }
174 void GetDemoName(char *pBuffer, size_t BufferSize) const override;
175 bool GetDemoInfo(class IStorage *pStorage, class IConsole *pConsole, const char *pFilename, int StorageType, CDemoHeader *pDemoHeader, CTimelineMarkers *pTimelineMarkers, CMapInfo *pMapInfo, IOHANDLE *pFile = nullptr, char *pErrorMessage = nullptr, size_t ErrorMessageSize = 0) const override;
176 const char *Filename() const { return m_aFilename; }
177 const char *ErrorMessage() const override { return m_aErrorMessage; }
178
179 int Update(bool RealTime = true);
180 bool IsSixup() const { return m_Sixup; }
181
182 const CPlaybackInfo *Info() const { return &m_Info; }
183 bool IsPlaying() const override { return m_File != nullptr; }
184 const CMapInfo *GetMapInfo() const { return &m_MapInfo; }
185};
186
188{
192
193public:
194 virtual void Init(class CSnapshotDelta *pSnapshotDelta, class IConsole *pConsole, class IStorage *pStorage);
195 bool Slice(const char *pDemo, const char *pDst, int StartTick, int EndTick, DEMOFUNC_FILTER pfnFilter, void *pUser) override;
196};
197
198#endif
Definition: demo.h:188
IConsole * m_pConsole
Definition: demo.h:189
class CSnapshotDelta * m_pSnapshotDelta
Definition: demo.h:191
bool Slice(const char *pDemo, const char *pDst, int StartTick, int EndTick, DEMOFUNC_FILTER pfnFilter, void *pUser) override
Definition: demo.cpp:1250
virtual void Init(class CSnapshotDelta *pSnapshotDelta, class IConsole *pConsole, class IStorage *pStorage)
Definition: demo.cpp:1243
IStorage * m_pStorage
Definition: demo.h:190
Definition: demo.h:67
virtual void OnDemoPlayerMessage(void *pData, int Size)=0
virtual ~IListener()
Definition: demo.h:69
virtual void OnDemoPlayerSnapshot(void *pData, int Size)=0
Definition: demo.h:64
bool ScanFile()
Definition: demo.cpp:571
const char * ErrorMessage() const override
Definition: demo.h:177
int m_LastSnapshotDataSize
Definition: demo.h:129
void SetListener(IListener *pListener)
Definition: demo.cpp:501
bool ExtractMap(class IStorage *pStorage)
Definition: demo.cpp:882
int Load(class IStorage *pStorage, class IConsole *pConsole, const char *pFilename, int StorageType)
Definition: demo.cpp:797
bool m_Sixup
Definition: demo.h:148
int m_SpeedIndex
Definition: demo.h:116
int SeekTick(ETickOffset TickOffset) override
Definition: demo.cpp:970
unsigned char m_aSnapshot[CSnapshot::MAX_SIZE]
Definition: demo.h:127
void Construct(class CSnapshotDelta *pSnapshotDelta, bool UseVideo)
Definition: demo.cpp:487
IListener * m_pListener
Definition: demo.h:93
const char * Filename() const
Definition: demo.h:176
IOHANDLE m_File
Definition: demo.h:110
bool IsPlaying() const override
Definition: demo.h:183
EReadChunkHeaderResult
Definition: demo.h:138
@ CHUNKHEADER_SUCCESS
Definition: demo.h:139
@ CHUNKHEADER_ERROR
Definition: demo.h:140
@ CHUNKHEADER_EOF
Definition: demo.h:141
~CDemoPlayer() override
Definition: demo.cpp:482
bool IsSixup() const
Definition: demo.h:180
void AdjustSpeedIndex(int Offset) override
Definition: demo.cpp:1043
bool m_UseVideo
Definition: demo.h:132
int Play()
Definition: demo.cpp:946
unsigned char * GetMapData(class IStorage *pStorage)
Definition: demo.cpp:864
CPlaybackInfo m_Info
Definition: demo.h:118
int SeekPercent(float Percent) override
Definition: demo.cpp:958
char m_aFilename[IO_MAX_PATH_LENGTH]
Definition: demo.h:112
class CSnapshotDelta * m_pSnapshotDelta
Definition: demo.h:130
int SeekTime(float Seconds) override
Definition: demo.cpp:964
unsigned char m_aCompressedSnapshotData[CSnapshot::MAX_SIZE]
Definition: demo.h:119
void SetSpeedIndex(int SpeedIndex) override
Definition: demo.cpp:1036
int Update(bool RealTime=true)
Definition: demo.cpp:1048
const CMapInfo * GetMapInfo() const
Definition: demo.h:184
const CInfo * BaseInfo() const override
Definition: demo.h:173
bool m_WasRecording
Definition: demo.h:134
const CPlaybackInfo * Info() const
Definition: demo.h:182
void SetSpeed(float Speed) override
Definition: demo.cpp:1031
class IConsole * m_pConsole
Definition: demo.h:109
std::vector< SKeyFrame > m_vKeyFrames
Definition: demo.h:114
void Unpause() override
Definition: demo.cpp:788
unsigned char m_aDecompressedSnapshotData[CSnapshot::MAX_SIZE]
Definition: demo.h:120
char m_aErrorMessage[256]
Definition: demo.h:113
EReadChunkHeaderResult ReadChunkHeader(int *pType, int *pSize, int *pTick)
Definition: demo.cpp:506
int64_t Time()
Definition: demo.cpp:919
int64_t m_MapOffset
Definition: demo.h:111
void DoTick()
Definition: demo.cpp:629
unsigned char m_aChunkData[CSnapshot::MAX_SIZE]
Definition: demo.h:124
unsigned char m_aLastSnapshotData[CSnapshot::MAX_SIZE]
Definition: demo.h:128
void Pause() override
Definition: demo.cpp:779
void Stop(const char *pErrorMessage="")
Definition: demo.cpp:1089
CMapInfo m_MapInfo
Definition: demo.h:115
CDemoPlayer(class CSnapshotDelta *pSnapshotDelta, bool UseVideo)
Definition: demo.cpp:477
int SetPos(int WantedTick) override
Definition: demo.cpp:995
void GetDemoName(char *pBuffer, size_t BufferSize) const override
Definition: demo.cpp:1116
TUpdateIntraTimesFunc m_UpdateIntraTimesFunc
Definition: demo.h:95
bool GetDemoInfo(class IStorage *pStorage, class IConsole *pConsole, const char *pFilename, int StorageType, CDemoHeader *pDemoHeader, CTimelineMarkers *pTimelineMarkers, CMapInfo *pMapInfo, IOHANDLE *pFile=nullptr, char *pErrorMessage=nullptr, size_t ErrorMessageSize=0) const override
Definition: demo.cpp:1121
Definition: demo.h:19
int m_LastKeyFrame
Definition: demo.h:26
bool IsRecording() const override
Definition: demo.h:57
class IConsole * m_pConsole
Definition: demo.h:20
~CDemoRecorder() override
Definition: demo.cpp:53
void Write(int Type, const void *pData, int Size)
Definition: demo.cpp:269
class CSnapshotDelta * m_pSnapshotDelta
Definition: demo.h:30
void WriteTickMarker(int Tick, bool Keyframe)
Definition: demo.cpp:244
char m_aCurrentFilename[IO_MAX_PATH_LENGTH]
Definition: demo.h:24
int Length() const override
Definition: demo.h:60
void RecordSnapshot(int Tick, const void *pData, int Size)
Definition: demo.cpp:319
class IStorage * m_pStorage
Definition: demo.h:21
bool m_NoMapData
Definition: demo.h:35
IOHANDLE m_File
Definition: demo.h:23
int m_LastTickMarker
Definition: demo.h:25
int Stop(IDemoRecorder::EStopMode Mode, const char *pTargetFilename="") override
Definition: demo.cpp:363
int m_NumTimelineMarkers
Definition: demo.h:32
int m_FirstTick
Definition: demo.h:27
void RecordMessage(const void *pData, int Size)
Definition: demo.cpp:351
void AddDemoMarker()
Definition: demo.cpp:429
CDemoRecorder()
Definition: demo.h:45
DEMOFUNC_FILTER m_pfnFilter
Definition: demo.h:37
int m_aTimelineMarkers[MAX_TIMELINE_MARKERS]
Definition: demo.h:33
const char * CurrentFilename() const override
Definition: demo.h:58
unsigned char m_aLastSnapshotData[CSnapshot::MAX_SIZE]
Definition: demo.h:29
void * m_pUser
Definition: demo.h:38
int Start(class IStorage *pStorage, class IConsole *pConsole, const char *pFilename, const char *pNetversion, const char *pMap, const SHA256_DIGEST &Sha256, unsigned MapCrc, const char *pType, unsigned MapSize, unsigned char *pMapData, IOHANDLE MapFile, DEMOFUNC_FILTER pfnFilter, void *pUser)
Definition: demo.cpp:59
Definition: snapshot.h:74
@ MAX_SIZE
Definition: snapshot.h:52
Definition: console.h:18
Definition: demo.h:119
Definition: demo.h:64
Definition: demo.h:60
ETickOffset
Definition: demo.h:78
Definition: demo.h:102
EStopMode
Definition: demo.h:106
Definition: storage.h:20
bool(* DEMOFUNC_FILTER)(const void *pData, int DataSize, void *pUser)
Definition: demo.h:23
@ MAX_TIMELINE_MARKERS
Definition: demo.h:15
@ SERVER_TICK_SPEED
Definition: protocol.h:80
void * IOHANDLE
Definition: logger.h:11
std::function< void()> TUpdateIntraTimesFunc
Definition: demo.h:16
Definition: demo.h:31
Definition: demo.h:75
CTimelineMarkers m_TimelineMarkers
Definition: demo.h:77
float m_IntraTickSincePrev
Definition: demo.h:88
CDemoHeader m_Header
Definition: demo.h:76
float m_IntraTick
Definition: demo.h:87
IDemoPlayer::CInfo m_Info
Definition: demo.h:79
int64_t m_CurrentTime
Definition: demo.h:82
int64_t m_LastUpdate
Definition: demo.h:81
int m_NextTick
Definition: demo.h:84
int m_PreviousTick
Definition: demo.h:85
float m_TickTime
Definition: demo.h:89
Definition: demo.h:99
int64_t m_Filepos
Definition: demo.h:100
SKeyFrame(int64_t Filepos, int Tick)
Definition: demo.h:103
int m_Tick
Definition: demo.h:101
Definition: demo.h:52
Definition: demo.h:46
Definition: hash.h:15
@ IO_MAX_PATH_LENGTH
Definition: types.h:43