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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef GLK_QUETZAL
24 #define GLK_QUETZAL
25 
26 #include "common/array.h"
27 #include "common/endian.h"
28 #include "common/memstream.h"
29 #include "common/stream.h"
30 #include "engines/savestate.h"
31 #include "glk/blorb.h"
32 #include "glk/glk_types.h"
33 
34 namespace Glk {
35 
36 enum QueztalTag {
37 	ID_IFSF = MKTAG('I', 'F', 'S', 'F'),
38 	ID_IFZS = MKTAG('I', 'F', 'Z', 'S'),
39 	ID_IFhd = MKTAG('I', 'F', 'h', 'd'),
40 	ID_UMem = MKTAG('U', 'M', 'e', 'm'),
41 	ID_CMem = MKTAG('C', 'M', 'e', 'm'),
42 	ID_Stks = MKTAG('S', 't', 'k', 's'),
43 	ID_SCVM = MKTAG('S', 'C', 'V', 'M')
44 };
45 
46 extern const uint32 INTERPRETER_IDS[INTERPRETER_TADS3 + 1];
47 
48 /**
49  * Quetzal save file reader
50  */
51 class QuetzalReader {
52 	struct Chunk {
53 		uint32 _id;
54 		size_t _offset, _size;
55 	};
56 public:
57 	/**
58 	 * Iterator for the chunks list
59 	 */
60 	struct Iterator {
61 	private:
62 		Common::SeekableReadStream *_stream;
63 		Common::Array<Chunk> &_chunks;
64 		int _index;
65 	public:
66 		/**
67 		 * Constructor
68 		 */
IteratorIterator69 		Iterator(Common::SeekableReadStream *stream, Common::Array<Chunk> &chunks, int index) :
70 			_stream(stream), _chunks(chunks), _index(index) {}
71 
72 		/**
73 		 * Deference
74 		 */
75 		Chunk &operator*() const { return _chunks[_index]; }
76 
77 		/**
78 		 * Incrementer
79 		 */
80 		Iterator &operator++() {
81 			++_index;
82 			return *this;
83 		}
84 
85 		/**
86 		 * Decrementer
87 		 */
88 		Iterator &operator--() {
89 			--_index;
90 			return *this;
91 		}
92 
93 		/**
94 		 * Equality test
95 		 */
96 		bool operator==(const Iterator &rhs) { return _index == rhs._index; }
97 
98 		/**
99 		 * Inequality test
100 		 */
101 		bool operator!=(const Iterator &rhs) { return _index != rhs._index; }
102 
103 		/**
104 		 * Get a read stream for the contents of a chunk
105 		 */
getStreamIterator106 		Common::SeekableReadStream *getStream() {
107 			_stream->seek(_chunks[_index]._offset);
108 			return _stream->readStream(_chunks[_index]._size);
109 		}
110 	};
111 private:
112 	Common::SeekableReadStream *_stream;
113 	Common::Array<Chunk> _chunks;
114 public:
115 	/**
116 	 * Constructor
117 	 */
QuetzalReader()118 	QuetzalReader() : _stream(nullptr) {}
119 
120 	/**
121 	 * Clear
122 	 */
123 	void clear();
124 
125 	/**
126 	 * Opens a Quetzal file for access
127 	 */
128 	bool open(Common::SeekableReadStream *stream, uint32 formType = 0);
129 
130 	/**
131 	 * Return an iterator for the beginning of the chunks list
132 	 */
begin()133 	Iterator begin() { return Iterator(_stream, _chunks, 0); }
134 
135 	/**
136 	 * Return an iterator for the beginning of the chunks list
137 	 */
end()138 	Iterator end() { return Iterator(_stream, _chunks, _chunks.size()); }
139 
140 	/**
141 	 * Loads a Quetzal save and extracts it's description from an ANNO chunk
142 	 */
143 	static bool getSavegameDescription(Common::SeekableReadStream *rs, Common::String &saveName);
144 
145 	/**
146 	 * Loads a Quetzal save and extract's it's description and meta info
147 	 */
148 	static bool getSavegameMetaInfo(Common::SeekableReadStream *rs, SaveStateDescriptor &ssd);
149 
150 	/**
151 	 * Support method for reading a string from a stream
152 	 */
153 	static Common::String readString(Common::ReadStream *src);
154 };
155 
156 /**
157  * Quetzal save file writer
158  */
159 class QuetzalWriter {
160 	/**
161 	 * Chunk entry
162 	 */
163 	struct Chunk {
164 		uint32 _id;
165 		Common::MemoryWriteStreamDynamic _stream;
166 
167 		/**
168 		 * Constructor
169 		 */
ChunkChunk170 		Chunk() : _id(0), _stream(DisposeAfterUse::YES) {}
171 
172 		/**
173 		 * Constructor
174 		 */
ChunkChunk175 		Chunk(uint32 id) : _id(id), _stream(DisposeAfterUse::YES) {}
176 	};
177 private:
178 	Common::Array<Chunk> _chunks;
179 
180 	/**
181 	 * Add chunks common to all Glk savegames
182 	 */
183 	void addCommonChunks(const Common::String &saveName);
184 public:
185 	/**
186 	 * Clear
187 	 */
clear()188 	void clear() { _chunks.clear(); }
189 
190 	/**
191 	 * Add a chunk
192 	 */
193 	Common::WriteStream &add(uint32 chunkId);
194 
195 	/**
196 	 * Save the added chunks to file
197 	 */
198 	void save(Common::WriteStream *out, const Common::String &saveName, uint32 formType = ID_IFSF);
199 };
200 
201 } // End of namespace Glk
202 
203 #endif
204