ScummVM API documentation
datafile.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 MEDIASTATION_DATAFILE_H
23 #define MEDIASTATION_DATAFILE_H
24 
25 #include "common/file.h"
26 #include "common/stream.h"
27 #include "common/path.h"
28 #include "common/rect.h"
29 #include "common/str.h"
30 
31 namespace MediaStation {
32 
33 // The version number of this engine,
34 // in the form 4.0r8 (major . minor r revision).
35 struct VersionInfo {
36  uint16 major = 0;
37  uint16 minor = 0;
38  uint16 patch = 0;
39 };
40 
42 
43 // A Media Station datafile consists of one or more RIFF-style "subfiles". Aside
44 // from some oddness at the start of the subfile, each subfile is basically
45 // standard sequence of chunks inside a LIST chunk, like you'd see in any RIFF
46 // file. These chunks have special IDs:
47 // - igod: Indicates a chunk that contains metadata about actor(s) in metadata sections.
48 // - a000, where 000 is a string that represents a 3-digit hexadecimal number.
49 // Indicates a chunk that contains actor data (sounds and bitmaps).
50 
51 enum DatumType {
52  kDatumTypeEmpty = 0x00,
53  kDatumTypeUint8 = 0x02,
54  kDatumTypeUint16 = 0x03,
55  kDatumTypeUint32 = 0x04,
56  kDatumTypeInt8 = 0x05,
57  kDatumTypeInt16 = 0x06,
58  kDatumTypeInt32 = 0x07,
59  kDatumTypeFloat = 0x08,
60  kDatumTypeDouble = 0x09,
61  kDatumTypeFilename = 0x0a,
62  kDatumTypeRect = 0x0d,
63  kDatumTypePoint = 0x0e,
64  kDatumTypeGraphicSize = 0x0f,
65  kDatumTypeGraphicUnit = 0x10,
66  kDatumTypeTime = 0x11,
67  kDatumTypeString = 0x12,
68  kDatumTypeVersion = 0x13,
69  kDatumTypeChannelIdent = 0x1b,
70  kDatumTypePolygon = 0x1d,
71 };
72 
74 public:
75  // Data files are internally little-endian, even on game versions targeting
76  // big-endian systems. The original engine has code for swapping byte order
77  // at runtime when needed. All of these internally assume the data files are
78  // stored little-endian on disk.
79  byte readTypedByte();
80  uint16 readTypedUint16();
81  uint32 readTypedUint32();
82  int8 readTypedSByte();
83  int16 readTypedSint16();
84  int32 readTypedSint32();
85  float readTypedFloat();
86  double readTypedDouble();
87  Common::String readTypedFilename();
88  Common::Rect readTypedRect();
89  Common::Point readTypedPoint();
90  Common::Point readTypedGraphicSize();
91  int16 readTypedGraphicUnit();
92  double readTypedTime();
93  Common::String readTypedString();
94  VersionInfo readTypedVersion();
95  uint32 readTypedChannelIdent();
96  Polygon readTypedPolygon();
97 
98 private:
99  void readAndVerifyType(DatumType type);
100 };
101 
102 class Chunk : public ParameterReadStream {
103 public:
104  Chunk() = default;
106 
107  uint32 bytesRemaining();
108  uint32 startPos() const { return _dataStartOffset; }
109 
110  uint32 _id = 0;
111  uint32 _length = 0;
112 
113  // ReadStream implementation
114  virtual bool eos() const { return _parentStream->eos(); };
115  virtual bool err() const { return _parentStream->err(); };
116  virtual void clearErr() { _parentStream->clearErr(); };
117  virtual uint32 read(void *dataPtr, uint32 dataSize);
118  virtual int64 pos() const { return _parentStream->pos(); };
119  virtual int64 size() const { return _parentStream->size(); };
120  virtual bool skip(uint32 offset) { return seek(offset, SEEK_CUR); };
121  virtual bool seek(int64 offset, int whence = SEEK_SET);
122 
123 private:
124  Common::SeekableReadStream *_parentStream = nullptr;
125  uint32 _dataStartOffset = 0;
126  uint32 _dataEndOffset = 0;
127 };
128 
129 class Subfile {
130 public:
131  Subfile() = default;
133 
134  Chunk nextChunk();
135  bool atEnd();
136 
137  Chunk _currentChunk;
138  uint32 _rate;
139 
140 private:
141  Common::SeekableReadStream *_stream = nullptr;
142  Chunk _rootChunk;
143 };
144 
145 // The stream loading class hierarchy presented below is a bit complex for reading directly
146 // from streams, like we can do on modern computers, without needing to worry about
147 // buffering from CD-ROM. But we are staying close to the original logic and class
148 // hierarchy where possible, so some of that original architecture is reflected here.
149 typedef uint32 ChannelIdent;
150 
151 class CdRomStream : public Common::File {
152 public:
153  CdRomStream() {};
154  void openStream(uint streamId);
155  void closeStream() { close(); }
156 
157  Subfile getNextSubfile();
158 
159 private:
160  uint _fileId = 0;
161 };
162 
164 public:
165  virtual ~ChannelClient() {};
166 
167  void setChannelIdent(ChannelIdent channelIdent) { _channelIdent = channelIdent; }
168  ChannelIdent channelIdent() const { return _channelIdent; }
169 
170  virtual void readChunk(Chunk &chunk) {};
171 
172  void registerWithStreamManager();
173  void unregisterWithStreamManager();
174 
175 protected:
176  ChannelIdent _channelIdent = 0;
177 };
178 
179 class StreamFeed {
180 public:
181  StreamFeed(uint streamId) : _id(streamId) {};
182  virtual ~StreamFeed() {};
183 
184  virtual void openFeed(uint streamId, uint startOffset) = 0;
185 
186  // The original also has forceCloseFeed, which doesn't do some other cleanup
187  // that the regular closeFeed does. However, since we are not doing caching and
188  // some other functionality in the original, we don't need this.
189  virtual void closeFeed() = 0;
190  virtual void stopFeed() = 0;
191  virtual void readData() = 0;
192 
193  uint _id = 0;
194 
195 protected:
196  CdRomStream *_stream = nullptr;
197 };
198 
199 class ImtStreamFeed : public StreamFeed {
200 public:
201  ImtStreamFeed(uint streamId);
202  ~ImtStreamFeed();
203 
204  virtual void openFeed(uint streamId, uint startOffset) override;
205  virtual void closeFeed() override;
206  // This implementation is currently empty because all this has to do with read timing.
207  virtual void stopFeed() override {};
208  virtual void readData() override;
209 };
210 
212 public:
214 
215  void registerChannelClient(ChannelClient *client);
216  void unregisterChannelClient(ChannelClient *client);
217  ChannelClient *channelClientForChannel(uint clientId) { return _channelClients.getValOrDefault(clientId); }
218 
219  ImtStreamFeed *openStreamFeed(uint streamId, uint offsetInStream = 0, uint maxBytesToRead = 0);
220  void closeStreamFeed(StreamFeed *streamFeed);
221 
222 private:
225 };
226 
227 } // End of namespace MediaStation
228 
229 #endif
virtual void clearErr()
Definition: datafile.h:116
virtual int64 size() const
Definition: datafile.h:119
Definition: datafile.h:35
Definition: str.h:59
virtual bool eos() const
Definition: datafile.h:114
Definition: actor.h:33
Definition: datafile.h:199
Definition: datafile.h:102
Definition: datafile.h:163
virtual int64 pos() const
Definition: datafile.h:118
Definition: rect.h:524
Definition: datafile.h:151
Definition: stream.h:745
Definition: datafile.h:211
Definition: datafile.h:179
Definition: hashmap.h:85
Definition: file.h:47
virtual bool skip(uint32 offset)
Definition: datafile.h:120
Definition: rect.h:144
Definition: datafile.h:129
virtual bool err() const
Definition: datafile.h:115
Definition: datafile.h:73