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 #include "mm/shared/xeen/cc_archive.h"
34 #include "mm/shared/xeen/file.h"
35 
36 namespace MM {
37 namespace Xeen {
38 
39 using Shared::Xeen::BaseCCArchive;
40 using Shared::Xeen::CCArchive;
41 using Shared::Xeen::CCEntry;
42 using Shared::Xeen::File;
43 
44 class XeenEngine;
45 class SaveArchive;
46 class Party;
47 class OutFile;
48 class SavesManager;
49 
50 #define SYNC_AS(SUFFIX,STREAM,TYPE,SIZE) \
51  template<typename T> \
52  void syncAs ## SUFFIX(T &val, Version minVersion = 0, Version maxVersion = kLastVersion) { \
53  if (_version < minVersion || _version > maxVersion) \
54  return; \
55  if (_loadStream) \
56  val = static_cast<TYPE>(_loadStream->read ## STREAM()); \
57  else { \
58  TYPE tmp = (TYPE)val; \
59  _saveStream->write ## STREAM(tmp); \
60  } \
61  _bytesSynced += SIZE; \
62  }
63 
64 /*
65  * Main resource manager
66  */
67 class FileManager {
68 public:
69  int _ccNum = 0;
70  CCArchive *_xeenCc = nullptr, *_darkCc = nullptr,
71  *_introCc = nullptr;
72  SaveArchive *_xeenSave = nullptr, *_darkSave = nullptr;
73  BaseCCArchive *_currentArchive = nullptr;
74  SaveArchive *_currentSave = nullptr;
75 
76 public:
81 
85  ~FileManager();
86 
91  bool setup();
92 
97  void setGameCc(int ccMode);
98 
102  void load(Common::SeekableReadStream &stream);
103 
107  void save(Common::WriteStream &s);
108 };
109 
116 class SubWriteStream : virtual public Common::WriteStream {
117 protected:
118  Common::WriteStream *_parentStream;
119  uint32 _begin;
120 public:
121  SubWriteStream(Common::WriteStream *parentStream) :
122  _parentStream(parentStream), _begin(parentStream->pos()) {
123  }
124 
125  uint32 write(const void *dataPtr, uint32 dataSize) override {
126  return _parentStream->write(dataPtr, dataSize);
127  }
128  bool flush() override { return _parentStream->flush(); }
129  void finalize() override {}
130  int64 pos() const override { return _parentStream->pos() - _begin; }
131 };
132 
134 private:
136  int _filesize;
137 public:
139  Common::Serializer(in, out), _in(in), _filesize(-1) {}
140 
141  SYNC_AS(Sint8, Byte, int8, 1)
142 
143  bool finished() {
144  if (_in && _filesize == -1)
145  _filesize = _in->size();
146  return _in != nullptr && _in->pos() >= _filesize;
147  }
148 };
149 
150 class SaveArchive : public BaseCCArchive {
151  friend class OutFile;
152 private:
153  Party *_party;
154  byte *_data;
155  uint32 _dataSize;
157 public:
158  SaveArchive(Party *party);
159  ~SaveArchive() override;
160 
164  void reset(CCArchive *src);
165 
169  Common::SeekableReadStream *createReadStreamForMember(const Common::Path &path) const override;
170 
174  virtual Common::SeekableReadStream *createReadStreamForMember(uint16 id) const;
175 
179  void load(Common::SeekableReadStream &stream);
180 
184  void save(Common::WriteStream &s);
185 
189  void loadParty();
190 
194  void replaceEntry(uint16 id, const byte *data, size_t size);
195 };
196 
200 class OutFile : public Common::WriteStream {
201 private:
202  SaveArchive *_archive;
203  Common::Path _filename;
204  Common::MemoryWriteStreamDynamic _backingStream;
205 public:
206  OutFile(const Common::Path &filename);
207  OutFile(const Common::Path &filename, SaveArchive *archive);
208  OutFile(const Common::Path &filename, int ccMode);
209 
213  void finalize() override;
214 
218  uint32 write(const void *dataPtr, uint32 dataSize) override;
219 
223  int64 pos() const override;
224 };
225 
226 } // End of namespace Xeen
227 } // End of namespace MM
228 
229 #endif
virtual int64 size() const =0
virtual int64 pos() const =0
Definition: files.h:200
Definition: stream.h:77
void save(Common::WriteStream &s)
Definition: party.h:159
void setGameCc(int ccMode)
Definition: files.h:116
Definition: memstream.h:194
Definition: path.h:52
Definition: files.h:150
Definition: stream.h:745
virtual bool flush()
Definition: stream.h:103
Definition: serializer.h:79
bool flush() override
Definition: files.h:128
void finalize() override
Definition: files.h:129
Definition: xeen.h:93
virtual int64 pos() const =0
Definition: hashmap.h:85
Definition: files.h:133
void load(Common::SeekableReadStream &stream)
Definition: detection.h:27
uint32 write(const void *dataPtr, uint32 dataSize) override
Definition: files.h:125
Definition: cc_archive.h:51
int64 pos() const override
Definition: files.h:130
FileManager(XeenEngine *vm)
Definition: files.h:67
Definition: cc_archive.h:94
virtual uint32 write(const void *dataPtr, uint32 dataSize)=0