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 
109  uint32 _id = 0;
110  uint32 _length = 0;
111 
112  // ReadStream implementation
113  virtual bool eos() const { return _parentStream->eos(); };
114  virtual bool err() const { return _parentStream->err(); };
115  virtual void clearErr() { _parentStream->clearErr(); };
116  virtual uint32 read(void *dataPtr, uint32 dataSize);
117  virtual int64 pos() const { return _parentStream->pos(); };
118  virtual int64 size() const { return _parentStream->size(); };
119  virtual bool skip(uint32 offset) { return seek(offset, SEEK_CUR); };
120  virtual bool seek(int64 offset, int whence = SEEK_SET);
121 
122 private:
123  Common::SeekableReadStream *_parentStream = nullptr;
124  uint32 _dataStartOffset = 0;
125  uint32 _dataEndOffset = 0;
126 };
127 
128 class Subfile {
129 public:
130  Subfile() = default;
132 
133  Chunk nextChunk();
134  bool atEnd();
135 
136  Chunk _currentChunk;
137  uint32 _rate;
138 
139 private:
140  Common::SeekableReadStream *_stream = nullptr;
141  Chunk _rootChunk;
142 };
143 
144 // The stream loading class hierarchy presented below is a bit complex for reading directly
145 // from streams, like we can do on modern computers, without needing to worry about
146 // buffering from CD-ROM. But we are staying close to the original logic and class
147 // hierarchy where possible, so some of that original architecture is reflected here.
148 typedef uint32 ChannelIdent;
149 
150 class CdRomStream : public Common::File {
151 public:
152  CdRomStream() {};
153  void openStream(uint streamId);
154  void closeStream() { close(); }
155 
156  Subfile getNextSubfile();
157 
158 private:
159  uint _fileId = 0;
160 };
161 
163 public:
164  virtual ~ChannelClient() {};
165 
166  void setChannelIdent(ChannelIdent channelIdent) { _channelIdent = channelIdent; }
167  ChannelIdent channelIdent() const { return _channelIdent; }
168 
169  virtual void readChunk(Chunk &chunk) {};
170 
171  void registerWithStreamManager();
172  void unregisterWithStreamManager();
173 
174 protected:
175  ChannelIdent _channelIdent = 0;
176 };
177 
178 class StreamFeed {
179 public:
180  StreamFeed(uint streamId) : _id(streamId) {};
181  virtual ~StreamFeed() {};
182 
183  virtual void openFeed(uint streamId, uint startOffset) = 0;
184 
185  // The original also has forceCloseFeed, which doesn't do some other cleanup
186  // that the regular closeFeed does. However, since we are not doing caching and
187  // some other functionality in the original, we don't need this.
188  virtual void closeFeed() = 0;
189  virtual void stopFeed() = 0;
190  virtual void readData() = 0;
191 
192  uint _id = 0;
193 
194 protected:
195  CdRomStream *_stream = nullptr;
196 };
197 
198 class ImtStreamFeed : public StreamFeed {
199 public:
200  ImtStreamFeed(uint streamId);
201  ~ImtStreamFeed();
202 
203  virtual void openFeed(uint streamId, uint startOffset) override;
204  virtual void closeFeed() override;
205  // This implementation is currently empty because all this has to do with read timing.
206  virtual void stopFeed() override {};
207  virtual void readData() override;
208 };
209 
211 public:
213 
214  void registerChannelClient(ChannelClient *client);
215  void unregisterChannelClient(ChannelClient *client);
216  ChannelClient *channelClientForChannel(uint clientId) { return _channelClients.getValOrDefault(clientId); }
217 
218  ImtStreamFeed *openStreamFeed(uint streamId, uint offsetInStream = 0, uint maxBytesToRead = 0);
219  void closeStreamFeed(StreamFeed *streamFeed);
220 
221 private:
224 };
225 
226 } // End of namespace MediaStation
227 
228 #endif
virtual void clearErr()
Definition: datafile.h:115
virtual int64 size() const
Definition: datafile.h:118
Definition: datafile.h:35
Definition: str.h:59
virtual bool eos() const
Definition: datafile.h:113
Definition: actor.h:33
Definition: datafile.h:198
Definition: datafile.h:102
Definition: datafile.h:162
virtual int64 pos() const
Definition: datafile.h:117
Definition: rect.h:524
Definition: datafile.h:150
Definition: stream.h:745
Definition: datafile.h:210
Definition: datafile.h:178
Definition: hashmap.h:85
Definition: file.h:47
virtual bool skip(uint32 offset)
Definition: datafile.h:119
Definition: rect.h:144
Definition: datafile.h:128
virtual bool err() const
Definition: datafile.h:114
Definition: datafile.h:73