ScummVM API documentation
recorderfile.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef COMMON_RECORDERFILE_H
23 #define COMMON_RECORDERFILE_H
24 
25 #include "common/scummsys.h"
26 #include "common/events.h"
27 #include "common/mutex.h"
28 #include "common/memstream.h"
29 #include "common/config-manager.h"
30 #include "common/savefile.h"
31 
32 //capacity of records buffer
33 #define kMaxBufferedRecords 10000
34 #define kRecordBuffSize sizeof(RecorderEvent) * kMaxBufferedRecords
35 
36 namespace Graphics {
37 class ManagedSurface;
38 }
39 
40 namespace Common {
41 
42 enum RecorderEventType {
43  kRecorderEventTypeNormal = 0,
44  kRecorderEventTypeTimer = 1,
45  kRecorderEventTypeTimeDate = 2,
46  kRecorderEventTypeScreenUpdate = 3,
47 };
48 
49 struct RecorderEvent : Event {
50  RecorderEventType recordedtype;
51  union {
52  uint32 time;
53  TimeDate timeDate;
54  };
55 
56  RecorderEvent() {
57  recordedtype = kRecorderEventTypeNormal;
58  time = 0;
59  timeDate.tm_sec = 0;
60  timeDate.tm_min = 0;
61  timeDate.tm_hour = 0;
62  timeDate.tm_mday = 0;
63  timeDate.tm_mon = 0;
64  timeDate.tm_year = 0;
65  timeDate.tm_wday = 0;
66  }
67 
68  RecorderEvent(const Event &e) : Event(e) {
69  recordedtype = kRecorderEventTypeNormal;
70  time = 0;
71  timeDate.tm_sec = 0;
72  timeDate.tm_min = 0;
73  timeDate.tm_hour = 0;
74  timeDate.tm_mday = 0;
75  timeDate.tm_mon = 0;
76  timeDate.tm_year = 0;
77  timeDate.tm_wday = 0;
78  }
79 };
80 
81 
82 
83 class PlaybackFile {
85  enum fileMode {
86  kRead = 0,
87  kWrite = 1,
88  kClosed = 2
89  };
90  enum PlaybackFileState {
91  kFileStateCheckFormat,
92  kFileStateCheckVersion,
93  kFileStateProcessHash,
94  kFileStateProcessHeader,
95  kFileStateProcessRandom,
96  kFileStateSelectSection,
97  kFileStateProcessSettings,
98  kFileStateProcessSave,
99  kFileStateDone,
100  kFileStateError
101  };
102  enum FileTag {
103  kFormatIdTag = MKTAG('P','B','C','K'),
104  kVersionTag = MKTAG('V','E','R','S'),
105  kHeaderSectionTag = MKTAG('H','E','A','D'),
106  kHashSectionTag = MKTAG('H','A','S','H'),
107  kRandomSectionTag = MKTAG('R','A','N','D'),
108  kEventTag = MKTAG('E','V','N','T'),
109  kScreenShotTag = MKTAG('T','H','M','B'),
110  kSettingsSectionTag = MKTAG('S','E','T','T'),
111  kAuthorTag = MKTAG('H','A','U','T'),
112  kCommentsTag = MKTAG('H','C','M','T'),
113  kNameTag = MKTAG('H','N','A','M'),
114  kHashRecordTag = MKTAG('H','R','C','D'),
115  kRandomRecordTag = MKTAG('R','R','C','D'),
116  kSettingsRecordTag = MKTAG('S','R','E','C'),
117  kSettingsRecordKeyTag = MKTAG('S','K','E','Y'),
118  kSettingsRecordValueTag = MKTAG('S','V','A','L'),
119  kSaveTag = MKTAG('S','A','V','E'),
120  kSaveRecordTag = MKTAG('R','S','A','V'),
121  kSaveRecordNameTag = MKTAG('S','N','A','M'),
122  kSaveRecordBufferTag = MKTAG('S','B','U','F'),
123  kMD5Tag = MKTAG('M','D','5',' ')
124  };
125  struct ChunkHeader {
126  FileTag id;
127  uint32 len;
128  };
129 public:
130  struct SaveFileBuffer {
131  byte *buffer;
132  uint32 size;
133  };
135  String fileName;
136  String author;
137  String name;
138  String notes;
139  String description;
140  StringMap hashRecords;
141  StringMap settingsRecords;
143  RandomSeedsDictionary randomSourceRecords;
144  };
145  PlaybackFile();
146  ~PlaybackFile();
147 
148  bool openWrite(const String &fileName);
149  bool openRead(const String &fileName);
150  void close();
151 
152  bool hasNextEvent() const;
153  RecorderEvent getNextEvent();
154  void writeEvent(const RecorderEvent &event);
155 
156  void saveScreenShot(const Graphics::ManagedSurface &screen, const byte md5[16]);
157  void saveScreenShot(const Graphics::Surface &screen, const byte md5[16]);
158  Graphics::ManagedSurface *getScreenShot(int number);
159  int getScreensCount();
160 
161  bool isEventsBufferEmpty() const;
162  PlaybackFileHeader &getHeader() {return _header;}
163  void updateHeader();
164  void addSaveFile(const String &fileName, InSaveFile *saveStream);
165 
166  uint32 getVersion() const {return _version;}
167 private:
168  Array<byte> _tmpBuffer;
169  WriteStream *_recordFile;
170  WriteStream *_writeStream;
171  WriteStream *_screenshotsFile;
172  MemoryReadStream _tmpPlaybackFile;
173  SeekableReadStream *_readStream;
174  SeekableMemoryWriteStream _tmpRecordFile;
175 
176  fileMode _mode;
177  bool _headerDumped;
178  int _recordCount;
179  uint32 _eventsSize;
180  PlaybackFileHeader _header;
181  PlaybackFileState _playbackParseState;
182  uint32 _version;
183 
184  void skipHeader();
185  bool parseHeader();
186  bool processChunk(ChunkHeader &nextChunk);
187  void returnToChunkHeader();
188 
189  bool readSaveRecord();
190  void checkRecordedMD5();
191  bool readChunkHeader(ChunkHeader &nextChunk);
192  void processRndSeedRecord(ChunkHeader chunk);
193  bool processSettingsRecord();
194 
195  bool checkPlaybackFileVersion();
196 
197  void dumpHeaderToFile();
198  void writeSaveFilesSection();
199  void writeGameSettings();
200  void writeHeaderSection();
201  void writeGameHash();
202  void writeRandomRecords();
203 
204  void dumpRecordsToFile();
205 
206  String readString(int len);
207  void readHashMap(ChunkHeader chunk);
208 
209  bool skipToNextScreenshot();
210  void readEvent(RecorderEvent& event);
211  void readEventsToBuffer(uint32 size);
212 };
213 
214 } // End of namespace Common
215 
216 #endif
Definition: recorderfile.h:49
Definition: managed_surface.h:51
Definition: recorderfile.h:134
Definition: system.h:109
Definition: str.h:59
Definition: surface.h:67
Definition: stream.h:77
Definition: recorderfile.h:83
Definition: stream.h:745
Definition: recorderfile.h:130
Definition: events.h:210
Definition: algorithm.h:29
Definition: formatinfo.h:28
Definition: memstream.h:43
#define MKTAG(a0, a1, a2, a3)
Definition: endian.h:188
Definition: memstream.h:155