aboutsummaryrefslogtreecommitdiff
path: root/common/recorderfile.h
blob: b1001628b84372336cf53209d723ae003f79b1c9 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef COMMON_RECORDERFILE_H
#define COMMON_RECORDERFILE_H

#include "common/scummsys.h"
#include "common/events.h"
#include "common/mutex.h"
#include "common/memstream.h"
#include "common/config-manager.h"
#include "common/savefile.h"

//capacity of records buffer
#define kMaxBufferedRecords 10000
#define kRecordBuffSize sizeof(RecorderEvent) * kMaxBufferedRecords

namespace Common {

enum RecorderEventType {
	kRecorderEventTypeNormal = 0,
	kRecorderEventTypeTimer = 1
};

struct RecorderEvent : Event {
	RecorderEventType recordedtype;
	uint32 time;

	RecorderEvent() {
		recordedtype = kRecorderEventTypeNormal;
		time = 0;
	}

	RecorderEvent(const Event &e) : Event(e) {
		recordedtype = kRecorderEventTypeNormal;
		time = 0;
	}
};



class PlaybackFile {
	typedef HashMap<String, uint32, IgnoreCase_Hash, IgnoreCase_EqualTo> RandomSeedsDictionary;
	enum fileMode {
		kRead = 0,
		kWrite = 1,
		kClosed = 2
	};
	enum PlaybackFileState {
		kFileStateCheckFormat,
		kFileStateCheckVersion,
		kFileStateProcessHash,
		kFileStateProcessHeader,
		kFileStateProcessRandom,
		kFileStateSelectSection,
		kFileStateProcessSettings,
		kFileStateProcessSave,
		kFileStateDone,
		kFileStateError
	};
	enum FileTag {
		kFormatIdTag = MKTAG('P','B','C','K'),
		kVersionTag = MKTAG('V','E','R','S'),
		kHeaderSectionTag = MKTAG('H','E','A','D'),
		kHashSectionTag = MKTAG('H','A','S','H'),
		kRandomSectionTag = MKTAG('R','A','N','D'),
		kEventTag = MKTAG('E','V','N','T'),
		kScreenShotTag = MKTAG('B','M','H','T'),
		kSettingsSectionTag = MKTAG('S','E','T','T'),
		kAuthorTag = MKTAG('H','A','U','T'),
		kCommentsTag = MKTAG('H','C','M','T'),
		kNameTag = MKTAG('H','N','A','M'),
		kHashRecordTag = MKTAG('H','R','C','D'),
		kRandomRecordTag = MKTAG('R','R','C','D'),
		kSettingsRecordTag = MKTAG('S','R','E','C'),
		kSettingsRecordKeyTag = MKTAG('S','K','E','Y'),
		kSettingsRecordValueTag = MKTAG('S','V','A','L'),
		kSaveTag = MKTAG('S','A','V','E'),
		kSaveRecordTag = MKTAG('R','S','A','V'),
		kSaveRecordNameTag = MKTAG('S','N','A','M'),
		kSaveRecordBufferTag = MKTAG('S','B','U','F'),
		kMD5Tag = MKTAG('M','D','5',' ')
	};
	struct ChunkHeader {
		FileTag id;
		uint32 len;
	};
public:
	struct SaveFileBuffer {
		byte *buffer;
		uint32 size;
	};
	struct PlaybackFileHeader {
		String fileName;
		String author;
		String name;
		String notes;
		String description;
		StringMap hashRecords;
		StringMap settingsRecords;
		HashMap<String, SaveFileBuffer> saveFiles;
		RandomSeedsDictionary randomSourceRecords;
	};
	PlaybackFile();
	~PlaybackFile();

	bool openWrite(const String &fileName);
	bool openRead(const String &fileName);
	void close();

	RecorderEvent getNextEvent();
	void writeEvent(const RecorderEvent &event);

	void saveScreenShot(Graphics::Surface &screen, byte md5[16]);
	Graphics::Surface *getScreenShot(int number);
	int getScreensCount();

	bool isEventsBufferEmpty();
	PlaybackFileHeader &getHeader() {return _header;}
	void updateHeader();
	void addSaveFile(const String &fileName, InSaveFile *saveStream);
private:
	WriteStream *_recordFile;
	WriteStream *_writeStream;
	WriteStream *_screenshotsFile;
	MemoryReadStream _tmpPlaybackFile;
	SeekableReadStream *_readStream;
	SeekableMemoryWriteStream _tmpRecordFile;

	fileMode _mode;
	bool _headerDumped;
	int _recordCount;
	uint32 _eventsSize;
	byte _tmpBuffer[kRecordBuffSize];
	PlaybackFileHeader _header;
	PlaybackFileState _playbackParseState;

	void skipHeader();
	bool parseHeader();
	bool processChunk(ChunkHeader &nextChunk);
	void returnToChunkHeader();

	bool readSaveRecord();
	void checkRecordedMD5();
	bool readChunkHeader(ChunkHeader &nextChunk);
	void processRndSeedRecord(ChunkHeader chunk);
	bool processSettingsRecord();

	bool checkPlaybackFileVersion();

	void dumpHeaderToFile();
	void writeSaveFilesSection();
	void writeGameSettings();
	void writeHeaderSection();
	void writeGameHash();
	void writeRandomRecords();

	void dumpRecordsToFile();

	String readString(int len);
	void readHashMap(ChunkHeader chunk);

	bool skipToNextScreenshot();
	void readEvent(RecorderEvent& event);
	void readEventsToBuffer(uint32 size);
	bool grabScreenAndComputeMD5(Graphics::Surface &screen, uint8 md5[16]);
};

} // End of namespace Common

#endif