ScummVM API documentation
archive.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 DIRECTOR_ARCHIVE_H
23 #define DIRECTOR_ARCHIVE_H
24 
25 #include "common/hash-str.h"
26 #include "common/file.h"
27 
28 namespace Common {
29 class MacResManager;
30 class SeekableMemoryWriteStream;
31 class SeekableReadStreamEndian;
32 class SeekableReadStream;
33 class Path;
34 }
35 
36 namespace Director {
37 
38 // Completely ripped off of Mohawk's Archive code
39 
40 struct Resource {
41  uint32 index;
42  int32 offset;
43  uint32 size;
44  uint32 uncompSize;
45  uint32 compressionType;
46  uint32 castId;
47  uint32 libResourceId;
48  uint32 tag;
49  uint16 flags;
50  uint16 unk1;
51  uint32 nextFreeResourceID;
52  Common::String name;
53  Common::Array<Resource> children;
54  bool accessed;
55 
56  Resource() = default;
57  Resource(Resource *original) {
58  index = original->index;
59  offset = original->offset;
60  size = original->size;
61  uncompSize = original->uncompSize;
62  compressionType = original->compressionType;
63  castId = original->castId;
64  libResourceId = original->libResourceId;
65  tag = original->tag;
66  flags = original->flags;
67  unk1 = original->unk1;
68  nextFreeResourceID = original->nextFreeResourceID;
69  name = Common::String(original->name);
70  children = Common::Array<Resource>(original->children);
71  accessed = original->accessed;
72  }
73 };
74 
75 class Archive {
76 public:
77  Archive();
78  virtual ~Archive();
79 
80  virtual bool openFile(const Common::Path &path);
81  virtual bool openStream(Common::SeekableReadStream *stream, uint32 offset = 0) = 0;
82  virtual bool writeToFile(Common::String filename, Movie *movie) {
83  // Saving Director movies was introduced in Director 4
84  // However, from DirectorEngine::createArchive, it is evident that after Director 4 only RIFX Archives were written
85  error("Archive::writeToFile was called on a non-RIFX Archive, which is not allowed");
86  return false;
87  }
88  virtual void close();
89 
90  /* Loading Functions for Cast */
91  bool loadConfig(Cast *cast);
92 
93  Common::Path getPathName() const { return _pathName; }
94  Common::String getFileName() const;
95  void setPathName(const Common::Path &name) { _pathName = name; }
96  virtual uint32 getFileSize();
97 
98  bool isOpen() const { return _stream != 0; }
99 
100  bool hasResource(uint32 tag, int id) const;
101  bool hasResource(uint32 tag, const Common::String &resName) const;
102  virtual Common::SeekableReadStreamEndian *getResource(uint32 tag, uint16 id);
103  virtual Common::SeekableReadStreamEndian *getFirstResource(uint32 tag);
104  virtual Common::SeekableReadStreamEndian *getFirstResource(uint32 tag, uint16 parentId);
105  virtual Resource getResourceDetail(uint32 tag, uint16 id);
106  uint32 getOffset(uint32 tag, uint16 id) const;
107  uint getResourceSize(uint32 tag, uint16 id) const;
108  uint16 findResourceID(uint32 tag, const Common::String &resName, bool ignoreCase = false) const;
109  Common::String getName(uint32 tag, uint16 id) const;
110  Common::SeekableReadStreamEndian *getMovieResourceIfPresent(uint32 tag);
111 
112  Common::Array<uint32> getResourceTypeList() const;
113  Common::Array<uint16> getResourceIDList(uint32 type) const;
114  bool _isBigEndian;
115  static uint32 convertTagToUppercase(uint32 tag);
116 
117  virtual Common::String formatArchiveInfo();
118 
119  void listUnaccessedChunks();
120 
121 protected:
122  void dumpChunk(Resource &res, Common::DumpFile &out);
127  TypeMap _types;
128  MovieChunkMap _movieChunks;
129 
130  Common::Path _pathName;
131 };
132 
133 class MacArchive : public Archive {
134 public:
135  MacArchive();
136  ~MacArchive() override;
137 
138  uint32 getFileSize() override;
139  void close() override;
140  bool openFile(const Common::Path &path) override;
141  bool openStream(Common::SeekableReadStream *stream, uint32 startOffset = 0) override;
142  Common::SeekableReadStreamEndian *getResource(uint32 tag, uint16 id) override;
143  Common::String formatArchiveInfo() override;
144 
145 private:
146  Common::MacResManager *_resFork;
147 
148  void readTags();
149 };
150 
151 class RIFFArchive : public Archive {
152 public:
153  RIFFArchive() : Archive() { _startOffset = 0; }
154  ~RIFFArchive() override {}
155 
156  bool openStream(Common::SeekableReadStream *stream, uint32 startOffset = 0) override;
157  Common::SeekableReadStreamEndian *getResource(uint32 tag, uint16 id) override;
158  Common::String formatArchiveInfo() override;
159 
160  uint32 _startOffset;
161 };
162 
163 class RIFXArchive : public Archive {
164 public:
165  RIFXArchive();
166  ~RIFXArchive() override;
167 
168  bool openStream(Common::SeekableReadStream *stream, uint32 startOffset = 0) override;
169  bool writeToFile(Common::String filename, Movie *movie) override;
170 
171  Common::SeekableReadStreamEndian *getFirstResource(uint32 tag) override;
172  virtual Common::SeekableReadStreamEndian *getFirstResource(uint32 tag, bool fileEndianness);
173  Common::SeekableReadStreamEndian *getFirstResource(uint32 tag, uint16 parentId) override;
174  Common::SeekableReadStreamEndian *getResource(uint32 tag, uint16 id) override;
175  virtual Common::SeekableReadStreamEndian *getResource(uint32 tag, uint16 id, bool fileEndianness);
176  Resource getResourceDetail(uint32 tag, uint16 id) override;
177  Common::String formatArchiveInfo() override;
178 
179 private:
180  /* archive-save.cpp */
181  /* These functions are for writing movies */
182  bool writeMemoryMap(Common::SeekableWriteStream *writeStream, Common::Array<Resource *> resource); // Parallel to readMemoryMap
183  bool writeAfterBurnerMap(Common::SeekableWriteStream *writeStreaa); // Parallel to readAfterBurnerMap
184  bool writeKeyTable(Common::SeekableWriteStream *writeStream, uint32 offset); // Parallel to readKeyTable
185  bool writeCast(Common::SeekableWriteStream *writeStream, uint32 offset, uint32 castLib); // Parallel to readCast
186 
187  uint32 getArchiveSize(Common::Array<Resource *> resources);
188  uint32 getMmapSize();
189  uint32 getImapSize();
190  uint32 getCASResourceSize(uint32 castLib);
191  uint32 getKeyTableResourceSize();
192  Common::Array<Resource *> rebuildResources(Movie *movie);
193 
194  bool readMemoryMap(Common::SeekableReadStreamEndian &stream, uint32 moreOffset, Common::SeekableMemoryWriteStream *dumpStream, uint32 movieStartOffset);
195  bool readAfterburnerMap(Common::SeekableReadStreamEndian &stream, uint32 moreOffset);
196  void readCast(Common::SeekableReadStreamEndian &casStream, uint16 libResourceId);
197  void readKeyTable(Common::SeekableReadStreamEndian &keyStream);
198 
199  uint32 findParentIndex(uint32 tag, uint16 index);
200 
201  /* Memory Map data to save the file */
202  uint32 _metaTag;
203  uint32 _mapversion;
204  uint32 _mmapHeaderSize;
205  uint32 _mmapEntrySize;
206  uint32 _totalCount;
207  uint32 _resCount;
208  uint32 _imapLength;
209  uint32 _version;
210  uint32 _size;
211 
212  /* Key Table data to save the file */
213  uint16 _keyTableEntrySize;
214  uint16 _keyTableEntrySize2;
215  uint32 _keyTableEntryCount;
216  uint32 _keyTableUsedCount;
217 
218  /* AfterBurner data to save the file */
219  uint32 _fverLength;
220  uint32 _afterBurnerVersion;
221  uint32 _fcdrLength;
222  uint32 _abmpLength;
223  uint32 _abmpEnd;
224  uint32 _abmpCompressionType;
225  uint32 _abmpUncompLength;
226  uint32 _abmpActualUncompLength;
227 
228 protected:
229  uint32 _rifxType;
230 
231  // Each resource is independent
232  Common::Array<Resource *> _resources;
234  uint32 _ilsBodyOffset;
237 
238  // In the RIFX container type, some resources are related of other resources in a child-parent relation
239  // If you want to check the relation between a 'BITD' resource and some other resource (e.g. 'CASt')
240  // Check if index of 'BITD' resource is present in _keyData['BITD'][index of 'CASt' resource]
242 };
243 
244 /*******************************************
245  *
246  * Projector Archive
247  *
248  *******************************************/
249 
251 public:
253  ~ProjectorArchive() override;
254 
255  bool hasFile(const Common::Path &path) const override;
256  int listMembers(Common::ArchiveMemberList &list) const override;
257  const Common::ArchiveMemberPtr getMember(const Common::Path &path) const override;
258  Common::SeekableReadStream *createReadStreamForMember(const Common::Path &path) const override;
259  bool isLoaded() { return _isLoaded; }
260 private:
261  Common::SeekableReadStream *createBufferedReadStream();
262  bool loadArchive(Common::SeekableReadStream *stream);
263 
264  struct Entry {
265  uint32 offset;
266  uint32 size;
267  };
269  FileMap _files;
270  Common::Path _path;
271 
272  bool _isLoaded;
273 };
274 
275 void dumpFile(Common::String filename, uint32 id, uint32 tag, byte *dumpData, uint32 dumpSize);
276 
278 public:
279  SavedArchive(Common::String target) ;
280  bool hasFile(const Common::Path &path) const override;
281  int listMembers(Common::ArchiveMemberList &list) const override;
282  const Common::ArchiveMemberPtr getMember(const Common::Path &path) const override;
283  Common::SeekableReadStream *createReadStreamForMember(const Common::Path &path) const override;
284 
285 private:
287  FileMap _files;
288 };
289 
290 }
291 #endif
Definition: archive.h:133
Definition: cast.h:87
Definition: macresman.h:126
Definition: str.h:59
Definition: archive.h:250
Definition: archive.h:163
Definition: list.h:44
Definition: path.h:52
Definition: movie.h:88
Definition: stream.h:745
Definition: file.h:145
Definition: archive.h:36
Definition: archive.h:141
Definition: archive.h:277
Definition: archive.h:75
Path
Definition: game.h:75
Definition: hashmap.h:85
Definition: algorithm.h:29
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: archive.h:40
Definition: stream.h:351
Definition: stream.h:944
Definition: archive.h:151
Definition: memstream.h:155