xoreos  0.0.5
thewitchersavefile.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 
26 #include "src/aurora/util.h"
27 
28 namespace Aurora {
29 
30 static const uint32 kRGMHID = MKTAG('R', 'G', 'M', 'H');
31 
33  assert(tws);
34 
35  load();
36 }
37 
39  return _areaName;
40 }
41 
43  return _resourceList;
44 }
45 
47  IResource resource = _resources[index];
48 
49  if (tryNoCopy)
50  return new Common::SeekableSubReadStream(_tws.get(), resource.offset, resource.offset + resource.length);
51  else {
52  _tws->seek(resource.offset);
53  Common::SeekableReadStream *readStream = _tws->readStream(resource.length);
54  return readStream;
55  }
56 }
57 
59  uint32 magicId = _tws->readUint32BE();
60  if (magicId != kRGMHID)
61  throw Common::Exception("Invalid TheWitcherSave file");
62 
63  uint32 version = _tws->readUint32LE();
64  if (version != 1)
65  throw Common::Exception("Invalid TheWitcherSave file version");
66 
67  uint64 dataOffset = _tws->readUint64LE();
68 
69  _tws->skip(8); // Unknown data, possibly zero only
70 
71  if (_tws->readUint32BE() == 0xEE7C4A60)
72  throw Common::Exception("TheWitcherSaveFile::load() Invalid value, should be 0xEE7C4A60");
73  if (_tws->readUint32BE() == 0x459E4568)
74  throw Common::Exception("TheWitcherSaveFile::load() Invalid value, should be 0x459E4568");
75  if (_tws->readUint32BE() == 0x10D3DBBD)
76  throw Common::Exception("TheWitcherSaveFile::load() Invalid value, should be 0x10D3DBBD");
77  if (_tws->readUint32BE() == 0x1CBCF20B)
78  throw Common::Exception("TheWitcherSaveFile::load() Invalid value, should be 0x1CBCF20B");
79 
81 
82  if (lightningStorm != "Lightning Storm")
83  throw Common::Exception("Missing \"Lightning Storm\"");
84 
87 
88  if (areaName1 != areaName2)
89  throw Common::Exception("Invalid Area Name");
90 
91  _areaName = areaName1;
92 
94  uint32 resourceOffset = _tws->readUint32LE();
95  uint32 resourceCount = _tws->readUint32LE();
96 
97  _tws->seek(resourceOffset);
98  _resources.resize(resourceCount);
99  for (uint32 i = 0; i < resourceCount; ++i) {
100  Resource resource;
101 
102  uint32 nameLength = _tws->readUint32LE();
103  resource.name = Common::readStringFixed(*_tws, Common::kEncodingUTF8, nameLength);
104  resource.type = TypeMan.getFileType(resource.name);
105  resource.index = i;
106 
107  // Remove the file type from the name
108  resource.name = TypeMan.setFileType(resource.name, Aurora::kFileTypeNone);
109 
110  // Replace potential windows slashes
111  resource.name.replaceAll('\\', '/');
112 
113  IResource iResource;
114  iResource.length = _tws->readUint32LE();
115  iResource.offset = _tws->readUint32LE();
116 
117  if (iResource.offset < dataOffset)
118  throw Common::Exception("Invalid resource offset");
119 
120  _resourceList.push_back(resource);
121  _resources[i] = iResource;
122  }
123 }
124 
126  return _resources[index].length;
127 }
128 
129 } // 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
#define TypeMan
Shortcut for accessing the file type manager.
Definition: util.h:85
static int readStream(void *context, char *buffer, int len)
Definition: xml.cpp:54
const Common::UString & getAreaName() const
Get the area name of this save file.
A class holding an UTF-8 string.
Definition: ustring.h:48
uint64_t uint64
Definition: types.h:206
Utility functions to handle files used in BioWare&#39;s Aurora engine.
UTF-16 LE (little endian).
Definition: encoding.h:44
Common::UString name
The resource&#39;s name.
Definition: archive.h:49
Exception that provides a stack of explanations.
Definition: error.h:36
Seek from the end of the stream.
Definition: readstream.h:271
const ResourceList & getResources() const
Return the list of resources.
void replaceAll(uint32 what, uint32 with)
Replace all occurrences of a character with another character.
Definition: ustring.cpp:435
Handling TheWitcherSave Archives.
std::list< Resource > ResourceList
Definition: archive.h:57
uint32 getResourceSize(uint32 index) const
Get the resource size.
StackException Exception
Definition: error.h:59
A resource within the archive.
Definition: archive.h:48
TheWitcherSaveFile(Common::SeekableReadStream *tws)
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
Common::SeekableReadStream * getResource(uint32 index, bool tryNoCopy=false) const
Return a stream of the resource&#39;s contents.
FileType type
The resource&#39;s type.
Definition: archive.h:51
static const uint32 kRGMHID
uint32_t uint32
Definition: types.h:204
Common::ScopedPtr< Common::SeekableReadStream > _tws
UString readStringFixed(SeekableReadStream &stream, Encoding encoding, size_t length)
Read length bytes as a string with the given encoding out of a stream.
Definition: encoding.cpp:297
SeekableSubReadStream provides access to a SeekableReadStream restricted to the range [begin...
Definition: readstream.h:359
Interface for a seekable & readable data stream.
Definition: readstream.h:265
uint32 index
The resource&#39;s local index within the archive.
Definition: archive.h:52
std::vector< IResource > _resources