ScummVM API documentation
file.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 SCI_ENGINE_FILE_H
23 #define SCI_ENGINE_FILE_H
24 
25 #include "common/memstream.h"
26 #include "common/str-array.h"
27 #include "common/stream.h"
28 
29 namespace Sci {
30 
31 enum kFileOpenMode {
32  kFileOpenModeOpenOrCreate = 0,
33  kFileOpenModeOpenOrFail = 1,
34  kFileOpenModeCreate = 2
35 };
36 
37 enum {
40 };
41 
42 enum {
44  kNewGameId = 999,
45 
46  // SCI engine expects game IDs to start at 0, but slot 0 in ScummVM is
47  // reserved for autosave, so non-autosave games get their IDs shifted up
48  // when saving or restoring, and shifted down when enumerating save games.
49  // ScummVM slot 0 can't be shifted down as -1 is an illegal SCI save ID
50  // so it is instead wrapped around to 99 and then back to 0 when shifting up.
51  kSaveIdShift = 1,
52  kMaxShiftedSaveId = 99
53 };
54 
55 enum {
56  kVirtualFileHandleStart = 32000,
57  kVirtualFileHandleSci32Save = 32100,
58  kVirtualFileHandleSciAudio = 32300,
59  kVirtualFileHandleEnd = 32300
60 };
61 
62 struct SavegameDesc {
63  int16 id;
64  int date;
65  int time;
66  int version;
67  // name is a null-terminated 36 character array in SCI16,
68  // but in SCI32 the 36th character can be used for text.
69  // At least Phant2 makes use of this. We add an element
70  // so that this string is always terminated internally.
71  char name[kMaxSaveNameLength + 1];
72  Common::String gameVersion;
73  uint32 script0Size;
74  uint32 gameObjectOffset;
75 #ifdef ENABLE_SCI32
76  // Used by Shivers 1
77  uint16 lowScore;
78  uint16 highScore;
79  // Used by MGDX
80  uint8 avatarId;
81 #endif
82 };
83 
84 class FileHandle {
85 public:
86  Common::String _name;
88  Common::WriteStream *_out;
89 
90 public:
91  FileHandle();
92  ~FileHandle();
93 
94  void close();
95  bool isOpen() const;
96 };
97 
98 
99 class DirSeeker {
100 protected:
101  reg_t _outbuffer;
102  Common::StringArray _files;
103  Common::StringArray _virtualFiles;
105 
106 public:
107  DirSeeker() {
108  _outbuffer = NULL_REG;
109  _iter = _files.begin();
110  }
111 
112  reg_t firstFile(const Common::String &mask, reg_t buffer, SegManager *segMan);
113  reg_t nextFile(SegManager *segMan);
114 
115  Common::String getVirtualFilename(uint fileNumber);
116 
117 private:
118  void addAsVirtualFiles(Common::String title, Common::String fileMask);
119 };
120 
121 #ifdef ENABLE_SCI32
122 
126 class MemoryDynamicRWStream : public Common::MemoryWriteStreamDynamic, public Common::SeekableReadStream {
127 public:
128  MemoryDynamicRWStream(DisposeAfterUse::Flag disposeMemory = DisposeAfterUse::NO) : MemoryWriteStreamDynamic(disposeMemory), _eos(false) { }
129 
130  uint32 read(void *dataPtr, uint32 dataSize) override;
131 
132  bool eos() const override { return _eos; }
133  int64 pos() const override { return _pos; }
134  int64 size() const override { return _size; }
135  void clearErr() override { _eos = false; Common::MemoryWriteStreamDynamic::clearErr(); }
136  bool seek(int64 offs, int whence = SEEK_SET) override { return Common::MemoryWriteStreamDynamic::seek(offs, whence); }
137 
138 protected:
139  bool _eos;
140 };
141 
147 class SaveFileRewriteStream : public MemoryDynamicRWStream {
148 public:
149  SaveFileRewriteStream(const Common::String &fileName,
151  kFileOpenMode mode, bool compress);
152  ~SaveFileRewriteStream() override;
153 
154  uint32 write(const void *dataPtr, uint32 dataSize) override { _changed = true; return MemoryDynamicRWStream::write(dataPtr, dataSize); }
155 
156  void commit(); //< Save back to disk
157 
158 protected:
159  Common::String _fileName;
160  bool _compress;
161  bool _changed;
162 };
163 
164 #endif
165 
166 uint findFreeFileHandle(EngineState *s);
167 reg_t file_open(EngineState *s, const Common::String &filename, kFileOpenMode mode, bool unwrapFilename);
168 FileHandle *getFileFromHandle(EngineState *s, uint handle);
169 int fgets_wrapper(EngineState *s, char *dest, int maxsize, int handle);
170 void listSavegames(Common::Array<SavegameDesc> &saves);
171 int findSavegame(Common::Array<SavegameDesc> &saves, int16 savegameId);
172 bool fillSavegameDesc(const Common::String &filename, SavegameDesc &desc);
173 
174 #ifdef ENABLE_SCI32
175 
179 Common::MemoryReadStream *makeCatalogue(const uint maxNumSaves, const uint gameNameSize, const Common::String &fileNamePattern, const bool ramaFormat);
180 
181 int shiftSciToScummVMSaveId(int saveId);
182 int shiftScummVMToSciSaveId(int saveId);
183 #endif
184 
185 } // End of namespace Sci
186 
187 #endif // SCI_ENGINE_FILE_H
Definition: state.h:100
Definition: str.h:59
Definition: stream.h:77
virtual void clearErr()
Definition: stream.h:71
iterator begin()
Definition: array.h:374
Definition: file.h:99
Definition: memstream.h:194
Definition: stream.h:745
bool seek(int64 offs, int whence=SEEK_SET) override
Definition: memstream.h:254
The save game slot number for autosaves.
Definition: file.h:43
The save game slot number for a "new game" save.
Definition: file.h:44
Definition: console.h:28
Definition: memstream.h:43
Definition: seg_manager.h:48
Definition: file.h:84
Definition: file.h:62
Maximum number of savegames.
Definition: file.h:39
Maximum length of a savegame name (excluding terminator character)
Definition: file.h:38
Definition: vm_types.h:39