xoreos  0.0.5
thewitchersavewriter.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos 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 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/encoding.h"
26 
27 #include "src/aurora/util.h"
29 
30 namespace Aurora {
31 
32 static const uint32 kRGMHID = MKTAG('R', 'G', 'M', 'H');
33 
35  _stream(stream), _finished(false) {
36  // Write the magic id
37  stream.writeUint32BE(kRGMHID);
38 
39  // Write the version 1
40  stream.writeUint32LE(1);
41 
42  // Write the constant header length
43  stream.writeUint64LE(8232);
44 
45  // Write some zeros
46  stream.writeZeros(8);
47 
48  // Write some values, which are always the same
49  stream.writeUint32LE(0xEE7C4A60);
50  stream.writeUint32LE(0x459E4568);
51  stream.writeUint32LE(0x10D3DBBD);
52  stream.writeUint32LE(0x1CBCF20B);
53 
54  // Write Lightning Storm
55  Common::writeStringFixed(stream, "Lightning Storm", Common::kEncodingUTF16LE, 2048);
56 
57  // Write the current area name two times
58  Common::writeStringFixed(stream, areaName, Common::kEncodingUTF16LE, 2048);
59  Common::writeStringFixed(stream, areaName, Common::kEncodingUTF16LE, 2048);
60 
61  // Write 2048 zeros
62  stream.writeZeros(2048);
63 }
64 
65 void TheWitcherSaveWriter::add(const Common::UString &resRef, const Aurora::FileType fileType, Common::ReadStream &stream) {
66  if (_finished)
67  throw Common::Exception("TheWitcherSave::add() Archive is already finished");
68 
69  Resource resource;
70  resource.name = TypeMan.setFileType(resRef, fileType);
71  resource.offset = _stream.pos();
72  resource.size = _stream.writeStream(stream);
73 
74  _resources.push_back(resource);
75 }
76 
78  if (_finished)
79  throw Common::Exception("TheWitcherSave::finish() Archive is already finished");
80 
81  const size_t resourceTableOffset = _stream.pos();
82 
83  for (size_t i = 0; i < _resources.size(); ++i) {
84  const Resource &r = _resources[i];
89  }
90 
91  _stream.writeUint32LE(resourceTableOffset);
93 
94  _finished = true;
95 }
96 
97 } // End of namespace Aurora
#define MKTAG(a0, a1, a2, a3)
A wrapper macro used around four character constants, like &#39;DATA&#39;, to ensure portability.
Definition: endianness.h:140
Generic interface for a readable data stream.
Definition: readstream.h:64
#define TypeMan
Shortcut for accessing the file type manager.
Definition: util.h:85
A class holding an UTF-8 string.
Definition: ustring.h:48
Writer for writing TheWitcherSave files.
size_t writeString(WriteStream &stream, const Common::UString &str, Encoding encoding, bool terminate)
Write a string into a stream with a given encoding.
Definition: encoding.cpp:339
Utility functions to handle files used in BioWare&#39;s Aurora engine.
UTF-16 LE (little endian).
Definition: encoding.h:44
void writeUint64LE(uint64 value)
Definition: writestream.h:110
virtual size_t pos() const =0
Obtains the current value of the stream position indicator of the stream.
std::vector< Resource > _resources
Utility functions for working with differing string encodings.
StackException Exception
Definition: error.h:59
Plain, unextended ASCII (7bit clean).
Definition: encoding.h:40
void add(const Common::UString &resRef, const Aurora::FileType fileType, Common::ReadStream &stream)
Add a file to this TheWitcherSave archive.
size_t size() const
Return the size of the string, in characters.
Definition: ustring.cpp:241
static const uint32 kRGMHID
uint32_t uint32
Definition: types.h:204
size_t writeStream(ReadStream &stream, size_t n)
Copy n bytes of the given stream into the stream.
Definition: writestream.cpp:72
FileType
Various file types used by the Aurora engine and found in archives.
Definition: types.h:56
void writeUint32BE(uint32 value)
Definition: writestream.h:122
FORCEINLINE void writeZeros(size_t n)
Write n zeros to the stream.
Definition: writestream.h:143
TheWitcherSaveWriter(const Common::UString &areaName, Common::SeekableWriteStream &stream)
Create a new TheWitcherSave writer.
void finish()
Finish the stream and write the file table at the end of the stream, and set the finished flag to pre...
Common::SeekableWriteStream & _stream
void writeUint32LE(uint32 value)
Definition: writestream.h:104
void writeStringFixed(WriteStream &stream, const Common::UString &str, Encoding encoding, size_t length)
Write a string into a stream with a given encoding and fixed length in bytes.
Definition: encoding.cpp:347