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 "snapshot.h"
7
8#include <base/hash.h>
9
10#include <engine/demo.h>
11#include <engine/shared/protocol.h>
12
13#include <functional>
14#include <vector>
15
16typedef std::function<void()> TUpdateIntraTimesFunc;
17
18class CSnapshotDelta;
19class IStorage;
20
21class CDemoRecorder : public IDemoRecorder
22{
23 IStorage *m_pStorage;
24
25 IOHANDLE m_File;
26 char m_aCurrentFilename[IO_MAX_PATH_LENGTH];
27 int m_LastTickMarker;
28 int m_LastKeyFrame;
29 int m_FirstTick;
30
31 CSnapshotBuffer m_LastSnapshotData;
32 CSnapshotDelta *m_pSnapshotDelta;
33
34 int m_NumTimelineMarkers;
35 int m_aTimelineMarkers[MAX_TIMELINE_MARKERS];
36
37 bool m_NoMapData;
38
39 DEMOFUNC_FILTER m_pfnFilter;
40 void *m_pUser;
41
42 void WriteTickMarker(int Tick, bool Keyframe);
43 bool Write(int Type, const void *pData, int Size);
44
45public:
46 CDemoRecorder(CSnapshotDelta *pSnapshotDelta, bool NoMapData = false);
47 CDemoRecorder() = default;
48 ~CDemoRecorder() override;
49
50 int Start(IStorage *pStorage, 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);
51 int Stop(IDemoRecorder::EStopMode Mode, const char *pTargetFilename = "") override;
52
53 void AddDemoMarker();
54 void AddDemoMarker(int Tick);
55
56 void RecordSnapshot(int Tick, const void *pData, int Size);
57 void RecordMessage(const void *pData, int Size);
58
59 bool IsRecording() const override { return m_File != nullptr; }
60 const char *CurrentFilename() const override { return m_aCurrentFilename; }
61
62 bool HasMarkers() const override { return m_NumTimelineMarkers > 0; }
63 int Length() const override { return (m_LastTickMarker - m_FirstTick) / SERVER_TICK_SPEED; }
64};
65
66class CDemoPlayer : public IDemoPlayer
67{
68public:
69 class IListener
70 {
71 public:
72 virtual ~IListener() = default;
73 virtual void OnDemoPlayerSnapshot(void *pData, int Size) = 0;
74 virtual void OnDemoPlayerMessage(void *pData, int Size) = 0;
75 };
76
77 class CPlaybackInfo
78 {
79 public:
80 CDemoHeader m_Header;
81 CTimelineMarkers m_TimelineMarkers;
82
83 IDemoPlayer::CInfo m_Info;
84
85 int64_t m_LastUpdate;
86 int64_t m_LastScan;
87 int64_t m_CurrentTime;
88
89 int m_NextTick;
90 int m_PreviousTick;
91
92 float m_IntraTick;
93 float m_IntraTickSincePrev;
94 float m_TickTime;
95
96 bool m_LiveStateUpdating;
97 int m_LiveStateFailedCount;
98 int m_LiveStateUnchangedCount;
99 };
100
101private:
102 IListener *m_pListener;
103
104 TUpdateIntraTimesFunc m_UpdateIntraTimesFunc;
105
106 // Playback
107 class CKeyFrame
108 {
109 public:
110 int64_t m_Filepos;
111 int m_Tick;
112
113 CKeyFrame(int64_t Filepos, int Tick) :
114 m_Filepos(Filepos), m_Tick(Tick)
115 {
116 }
117 };
118
119 IOHANDLE m_File;
120 int64_t m_MapOffset;
121 char m_aFilename[IO_MAX_PATH_LENGTH];
122 char m_aErrorMessage[256];
123 std::vector<CKeyFrame> m_vKeyFrames;
124 CMapInfo m_MapInfo;
125 int m_SpeedIndex;
126
127 CPlaybackInfo m_Info;
128 unsigned char m_aCompressedSnapshotData[CSnapshot::MAX_SIZE];
129 unsigned char m_aDecompressedSnapshotData[CSnapshot::MAX_SIZE];
130
131 // Depending on the chunk header
132 // this is either a full CSnapshot or a CSnapshotDelta.
133 unsigned char m_aChunkData[CSnapshot::MAX_SIZE];
134 // Storage for the full snapshot
135 // where the delta gets unpacked into.
136 CSnapshotBuffer m_Snapshot;
137 CSnapshotBuffer m_LastSnapshotData;
138 int m_LastSnapshotDataSize;
139 CSnapshotDelta *m_pSnapshotDelta;
140 CSnapshotDelta *m_pSnapshotDeltaSixup;
141
142 bool m_UseVideo;
143#if defined(CONF_VIDEORECORDER)
144 bool m_WasRecording = false;
145#endif
146
147 enum EReadChunkHeaderResult
148 {
149 CHUNKHEADER_SUCCESS,
150 CHUNKHEADER_ERROR,
151 CHUNKHEADER_EOF,
152 };
153 EReadChunkHeaderResult ReadChunkHeader(int *pType, int *pSize, int *pTick);
154 void DoTick();
155 enum class EScanFileResult
156 {
157 SUCCESS,
158 ERROR_RECOVERABLE,
159 ERROR_UNRECOVERABLE,
160 };
161 EScanFileResult ScanFile();
162 void UpdateTimes();
163
164 int64_t Time();
165 bool m_Sixup;
166
167 CSnapshotDelta *SnapshotDelta();
168 void Construct(CSnapshotDelta *pSnapshotDelta, CSnapshotDelta *pSnapshotDeltaSixup, bool UseVideo);
169
170public:
171 CDemoPlayer(CSnapshotDelta *pSnapshotDelta, CSnapshotDelta *pSnapshotDeltaSixup, bool UseVideo);
172 CDemoPlayer(CSnapshotDelta *pSnapshotDelta, CSnapshotDelta *pSnapshotDeltaSixup, bool UseVideo, TUpdateIntraTimesFunc &&UpdateIntraTimesFunc);
173 ~CDemoPlayer() override;
174
175 void SetListener(IListener *pListener);
176
177 int Load(IStorage *pStorage, const char *pFilename, int StorageType);
178 unsigned char *GetMapData(IStorage *pStorage);
179 bool ExtractMap(IStorage *pStorage);
180 void Play();
181 void Pause() override;
182 void Unpause() override;
183 void Stop(const char *pErrorMessage = "");
184 void SetSpeed(float Speed) override;
185 void SetSpeedIndex(int SpeedIndex) override;
186 void AdjustSpeedIndex(int Offset) override;
187 bool SeekPercent(float Percent) override;
188 bool SeekTime(float Seconds) override;
189 bool SeekTick(ETickOffset TickOffset) override;
190 bool SetPos(int WantedTick) override;
191 const CInfo *BaseInfo() const override { return &m_Info.m_Info; }
192 void GetDemoName(char *pBuffer, size_t BufferSize) const override;
193 bool GetDemoInfo(IStorage *pStorage, const char *pFilename, int StorageType, CDemoHeader *pDemoHeader, CTimelineMarkers *pTimelineMarkers, CMapInfo *pMapInfo, IOHANDLE *pFile = nullptr, char *pErrorMessage = nullptr, size_t ErrorMessageSize = 0) const override;
194 const char *Filename() const { return m_aFilename; }
195 const char *ErrorMessage() const override { return m_aErrorMessage; }
196
197 void Update(bool RealTime = true);
198 bool IsSixup() const { return m_Sixup; }
199
200 const CPlaybackInfo *Info() const { return &m_Info; }
201 bool IsPlaying() const override { return m_File != nullptr; }
202 const CMapInfo *GetMapInfo() const { return &m_MapInfo; }
203};
204
205class CDemoEditor : public IDemoEditor
206{
207 IStorage *m_pStorage;
208 CSnapshotDelta *m_pSnapshotDelta;
209 CSnapshotDelta *m_pSnapshotDeltaSixup;
210
211public:
212 virtual void Init(CSnapshotDelta *pSnapshotDelta, CSnapshotDelta *pSnapshotDeltaSixup, IStorage *pStorage);
213 bool Slice(const char *pDemo, const char *pDst, int StartTick, int EndTick, DEMOFUNC_FILTER pfnFilter, void *pUser) override;
214};
215
216#endif
217