ScummVM API documentation
files.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 XEEN_FILES_H
23 #define XEEN_FILES_H
24 
25 #include "common/scummsys.h"
26 #include "common/array.h"
27 #include "common/file.h"
28 #include "common/memstream.h"
29 #include "common/savefile.h"
30 #include "common/serializer.h"
31 #include "common/str-array.h"
32 #include "graphics/surface.h"
33 
34 namespace Xeen {
35 
36 class XeenEngine;
37 class CCArchive;
38 class BaseCCArchive;
39 class File;
40 class SaveArchive;
41 class Party;
42 class OutFile;
43 class SavesManager;
44 
45 #define SYNC_AS(SUFFIX,STREAM,TYPE,SIZE) \
46  template<typename T> \
47  void syncAs ## SUFFIX(T &val, Version minVersion = 0, Version maxVersion = kLastVersion) { \
48  if (_version < minVersion || _version > maxVersion) \
49  return; \
50  if (_loadStream) \
51  val = static_cast<TYPE>(_loadStream->read ## STREAM()); \
52  else { \
53  TYPE tmp = (TYPE)val; \
54  _saveStream->write ## STREAM(tmp); \
55  } \
56  _bytesSynced += SIZE; \
57  }
58 
62 struct CCEntry {
63  uint16 _id;
64  int _offset;
65  uint16 _size;
66  int _writeOffset;
67 
68  CCEntry() : _id(0), _offset(0), _size(0), _writeOffset(0) {}
69  CCEntry(uint16 id, uint32 offset, uint32 size)
70  : _id(id), _offset(offset), _size(size) {
71  }
72 };
73 
74 /*
75  * Main resource manager
76  */
77 class FileManager {
78 public:
79  int _ccNum;
80 public:
85 
89  ~FileManager();
90 
95  bool setup();
96 
101  void setGameCc(int ccMode);
102 
106  void load(Common::SeekableReadStream &stream);
107 
111  void save(Common::WriteStream &s);
112 };
113 
117 class File : public Common::File {
118  friend class FileManager;
119  friend class OutFile;
120  friend class SavesManager;
121  friend class Debugger;
122 private:
123  static CCArchive *_xeenCc, *_darkCc, *_introCc;
124  static SaveArchive *_xeenSave, *_darkSave;
125  static BaseCCArchive *_currentArchive;
126  static SaveArchive *_currentSave;
127 public:
131  static void setCurrentArchive(int ccMode);
132 
136  static void syncBitFlags(Common::Serializer &s, bool *startP, bool *endP);
137 public:
138  File() : Common::File() {}
139  File(const Common::String &filename);
140  File(const Common::String &filename, int ccMode);
141  File(const Common::String &filename, Common::Archive &archive);
142  ~File() override {}
143 
147  bool open(const Common::Path &filename) override;
148 
152  bool open(const Common::Path &filename, Common::Archive &archive) override;
153 
157  virtual bool open(const Common::String &filename, int ccMode);
158 
162  bool open(const Common::FSNode &node) override {
163  return Common::File::open(node);
164  }
165 
169  bool open(SeekableReadStream *stream, const Common::String &name) override {
170  return Common::File::open(stream, name);
171  }
172 
176  Common::String readString();
177 
184  static bool exists(const Common::String &filename);
185 
193  static bool exists(const Common::String &filename, int ccMode);
194 
202  static bool exists(const Common::String &filename, Common::Archive &archive);
203 };
204 
211 class SubWriteStream : virtual public Common::WriteStream {
212 protected:
213  Common::WriteStream *_parentStream;
214  uint32 _begin;
215 public:
216  SubWriteStream(Common::WriteStream *parentStream) :
217  _parentStream(parentStream), _begin(parentStream->pos()) {
218  }
219 
220  uint32 write(const void *dataPtr, uint32 dataSize) override {
221  return _parentStream->write(dataPtr, dataSize);
222  }
223  bool flush() override { return _parentStream->flush(); }
224  void finalize() override {}
225  int64 pos() const override { return _parentStream->pos() - _begin; }
226 };
227 
229 public:
230  StringArray() {}
231  StringArray(const Common::String &name) { load(name); }
232 
236  void load(const Common::String &name);
237 
241  void load(const Common::String &name, int ccMode);
242 };
243 
245 private:
247  int _filesize;
248 public:
250  Common::Serializer(in, out), _in(in), _filesize(-1) {}
251 
252  SYNC_AS(Sint8, Byte, int8, 1)
253 
254  bool finished() {
255  if (_in && _filesize == -1)
256  _filesize = _in->size();
257  return _in != nullptr && _in->pos() >= _filesize;
258  }
259 };
260 
265 protected:
266  Common::Array<CCEntry> _index;
267 
271  void loadIndex(Common::SeekableReadStream &stream);
272 
276  void saveIndex(Common::WriteStream &stream);
277 
282  virtual bool getHeaderEntry(const Common::String &resourceName, CCEntry &ccEntry) const;
283 
288  virtual bool getHeaderEntry(uint16 id, CCEntry &ccEntry) const;
289 public:
293  static uint16 convertNameToId(const Common::String &resourceName);
294 public:
295  BaseCCArchive() {}
296 
297  // Archive implementation
298  bool hasFile(const Common::Path &path) const override;
299  int listMembers(Common::ArchiveMemberList &list) const override;
300  const Common::ArchiveMemberPtr getMember(const Common::Path &path) const override;
301 };
302 
306 class CCArchive : public BaseCCArchive {
307 private:
308  Common::String _filename;
309  Common::String _prefix;
310  bool _encoded;
311 protected:
312  bool getHeaderEntry(const Common::String &resourceName, CCEntry &ccEntry) const override;
313 public:
314  CCArchive(const Common::String &filename, bool encoded);
315  CCArchive(const Common::String &filename, const Common::String &prefix, bool encoded);
316  ~CCArchive() override;
317 
318  // Archive implementation
319  Common::SeekableReadStream *createReadStreamForMember(const Common::Path &path) const override;
320 };
321 
322 class SaveArchive : public BaseCCArchive {
323  friend class OutFile;
324 private:
325  Party *_party;
326  byte *_data;
327  uint32 _dataSize;
329 public:
330  SaveArchive(Party *party);
331  ~SaveArchive() override;
332 
336  void reset(CCArchive *src);
337 
341  Common::SeekableReadStream *createReadStreamForMember(const Common::Path &path) const override;
342 
346  virtual Common::SeekableReadStream *createReadStreamForMember(uint16 id) const;
347 
351  void load(Common::SeekableReadStream &stream);
352 
356  void save(Common::WriteStream &s);
357 
361  void loadParty();
362 
366  void replaceEntry(uint16 id, const byte *data, size_t size);
367 };
368 
372 class OutFile : public Common::WriteStream {
373 private:
374  SaveArchive *_archive;
375  Common::String _filename;
376  Common::MemoryWriteStreamDynamic _backingStream;
377 public:
378  OutFile(const Common::String &filename);
379  OutFile(const Common::String &filename, SaveArchive *archive);
380  OutFile(const Common::String &filename, int ccMode);
381 
385  void finalize() override;
386 
390  uint32 write(const void *dataPtr, uint32 dataSize) override;
391 
395  int64 pos() const override;
396 };
397 
398 } // End of namespace Xeen
399 
400 #endif /* XEEN_FILES_H */
bool flush() override
Definition: files.h:223
Definition: files.h:211
virtual int64 size() const =0
int64 pos() const override
Definition: files.h:225
Definition: str.h:59
void finalize() override
Definition: files.h:224
virtual int64 pos() const =0
Definition: stream.h:75
uint32 write(const void *dataPtr, uint32 dataSize) override
Definition: files.h:220
virtual bool open(const Path &filename)
Definition: list.h:44
Definition: memstream.h:179
Definition: path.h:50
Definition: stream.h:652
virtual bool flush()
Definition: stream.h:101
Definition: files.h:62
Definition: files.h:228
Definition: debugger.h:32
Definition: serializer.h:79
Definition: archive.h:101
Definition: files.h:264
virtual int64 pos() const =0
Definition: files.h:244
Definition: hashmap.h:85
Definition: file.h:47
Definition: files.h:117
Definition: fs.h:68
Array< String > StringArray
Definition: str-array.h:43
Definition: saves.h:47
Definition: files.h:372
Definition: xeen.h:100
Definition: party.h:152
Definition: files.h:322
Definition: ptr.h:159
virtual uint32 write(const void *dataPtr, uint32 dataSize)=0
bool open(const Common::FSNode &node) override
Definition: files.h:162
Definition: files.h:306
Definition: character.h:33
bool open(SeekableReadStream *stream, const Common::String &name) override
Definition: files.h:169
Definition: files.h:77