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 Common {
37 
38 enum RecorderEventType {
39  kRecorderEventTypeNormal = 0,
40  kRecorderEventTypeTimer = 1,
41  kRecorderEventTypeTimeDate = 2,
42  kRecorderEventTypeScreenUpdate = 3,
43 };
44 
45 struct RecorderEvent : Event {
46  RecorderEventType recordedtype;
47  union {
48  uint32 time;
49  TimeDate timeDate;
50  };
51 
52  RecorderEvent() {
53  recordedtype = kRecorderEventTypeNormal;
54  time = 0;
55  timeDate.tm_sec = 0;
56  timeDate.tm_min = 0;
57  timeDate.tm_hour = 0;
58  timeDate.tm_mday = 0;
59  timeDate.tm_mon = 0;
60  timeDate.tm_year = 0;
61  timeDate.tm_wday = 0;
62  }
63 
64  RecorderEvent(const Event &e) : Event(e) {
65  recordedtype = kRecorderEventTypeNormal;
66  time = 0;
67  timeDate.tm_sec = 0;
68  timeDate.tm_min = 0;
69  timeDate.tm_hour = 0;
70  timeDate.tm_mday = 0;
71  timeDate.tm_mon = 0;
72  timeDate.tm_year = 0;
73  timeDate.tm_wday = 0;
74  }
75 };
76 
77 
78 
79 class PlaybackFile {
81  enum fileMode {
82  kRead = 0,
83  kWrite = 1,
84  kClosed = 2
85  };
86  enum PlaybackFileState {
87  kFileStateCheckFormat,
88  kFileStateCheckVersion,
89  kFileStateProcessHash,
90  kFileStateProcessHeader,
91  kFileStateProcessRandom,
92  kFileStateSelectSection,
93  kFileStateProcessSettings,
94  kFileStateProcessSave,
95  kFileStateDone,
96  kFileStateError
97  };
98  enum FileTag {
99  kFormatIdTag = MKTAG('P','B','C','K'),
100  kVersionTag = MKTAG('V','E','R','S'),
101  kHeaderSectionTag = MKTAG('H','E','A','D'),
102  kHashSectionTag = MKTAG('H','A','S','H'),
103  kRandomSectionTag = MKTAG('R','A','N','D'),
104  kEventTag = MKTAG('E','V','N','T'),
105  kScreenShotTag = MKTAG('T','H','M','B'),
106  kSettingsSectionTag = MKTAG('S','E','T','T'),
107  kAuthorTag = MKTAG('H','A','U','T'),
108  kCommentsTag = MKTAG('H','C','M','T'),
109  kNameTag = MKTAG('H','N','A','M'),
110  kHashRecordTag = MKTAG('H','R','C','D'),
111  kRandomRecordTag = MKTAG('R','R','C','D'),
112  kSettingsRecordTag = MKTAG('S','R','E','C'),
113  kSettingsRecordKeyTag = MKTAG('S','K','E','Y'),
114  kSettingsRecordValueTag = MKTAG('S','V','A','L'),
115  kSaveTag = MKTAG('S','A','V','E'),
116  kSaveRecordTag = MKTAG('R','S','A','V'),
117  kSaveRecordNameTag = MKTAG('S','N','A','M'),
118  kSaveRecordBufferTag = MKTAG('S','B','U','F'),
119  kMD5Tag = MKTAG('M','D','5',' ')
120  };
121  struct ChunkHeader {
122  FileTag id;
123  uint32 len;
124  };
125 public:
126  struct SaveFileBuffer {
127  byte *buffer;
128  uint32 size;
129  };
131  String fileName;
132  String author;
133  String name;
134  String notes;
135  String description;
136  StringMap hashRecords;
137  StringMap settingsRecords;
139  RandomSeedsDictionary randomSourceRecords;
140  };
141  PlaybackFile();
142  ~PlaybackFile();
143 
144  bool openWrite(const String &fileName);
145  bool openRead(const String &fileName);
146  void close();
147 
148  bool hasNextEvent() const;
149  RecorderEvent getNextEvent();
150  void writeEvent(const RecorderEvent &event);
151 
152  void saveScreenShot(Graphics::Surface &screen, byte md5[16]);
153  Graphics::Surface *getScreenShot(int number);
154  int getScreensCount();
155 
156  bool isEventsBufferEmpty() const;
157  PlaybackFileHeader &getHeader() {return _header;}
158  void updateHeader();
159  void addSaveFile(const String &fileName, InSaveFile *saveStream);
160 
161  uint32 getVersion() const {return _version;}
162 private:
163  Array<byte> _tmpBuffer;
164  WriteStream *_recordFile;
165  WriteStream *_writeStream;
166  WriteStream *_screenshotsFile;
167  MemoryReadStream _tmpPlaybackFile;
168  SeekableReadStream *_readStream;
169  SeekableMemoryWriteStream _tmpRecordFile;
170 
171  fileMode _mode;
172  bool _headerDumped;
173  int _recordCount;
174  uint32 _eventsSize;
175  PlaybackFileHeader _header;
176  PlaybackFileState _playbackParseState;
177  uint32 _version;
178 
179  void skipHeader();
180  bool parseHeader();
181  bool processChunk(ChunkHeader &nextChunk);
182  void returnToChunkHeader();
183 
184  bool readSaveRecord();
185  void checkRecordedMD5();
186  bool readChunkHeader(ChunkHeader &nextChunk);
187  void processRndSeedRecord(ChunkHeader chunk);
188  bool processSettingsRecord();
189 
190  bool checkPlaybackFileVersion();
191 
192  void dumpHeaderToFile();
193  void writeSaveFilesSection();
194  void writeGameSettings();
195  void writeHeaderSection();
196  void writeGameHash();
197  void writeRandomRecords();
198 
199  void dumpRecordsToFile();
200 
201  String readString(int len);
202  void readHashMap(ChunkHeader chunk);
203 
204  bool skipToNextScreenshot();
205  void readEvent(RecorderEvent& event);
206  void readEventsToBuffer(uint32 size);
207  bool grabScreenAndComputeMD5(Graphics::Surface &screen, uint8 md5[16]);
208 };
209 
210 } // End of namespace Common
211 
212 #endif
Definition: recorderfile.h:45
Definition: recorderfile.h:130
int tm_hour
Definition: system.h:108
Definition: system.h:105
Definition: str.h:59
Definition: surface.h:66
int tm_mday
Definition: system.h:109
Definition: stream.h:77
int tm_min
Definition: system.h:107
Definition: recorderfile.h:79
Definition: stream.h:745
Definition: recorderfile.h:126
int tm_wday
Definition: system.h:112
int tm_sec
Definition: system.h:106
Definition: events.h:198
Definition: algorithm.h:29
Definition: memstream.h:43
int tm_year
Definition: system.h:111
#define MKTAG(a0, a1, a2, a3)
Definition: endian.h:188
int tm_mon
Definition: system.h:110
Definition: memstream.h:155