xoreos  0.0.5
biffile.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 /* See BioWare's own specs released for Neverwinter Nights modding
26  * (<https://github.com/xoreos/xoreos-docs/tree/master/specs/bioware>)
27  */
28 
29 #include <cassert>
30 
31 #include "src/common/util.h"
32 #include "src/common/strutil.h"
33 #include "src/common/error.h"
35 
36 #include "src/aurora/biffile.h"
37 #include "src/aurora/keyfile.h"
38 
39 static const uint32 kBIFID = MKTAG('B', 'I', 'F', 'F');
40 static const uint32 kVersion1 = MKTAG('V', '1', ' ', ' ');
41 static const uint32 kVersion11 = MKTAG('V', '1', '.', '1');
42 
43 namespace Aurora {
44 
46  assert(_bif);
47 
48  load(*_bif);
49 }
50 
52 }
53 
55  readHeader(bif);
56 
57  if (_id != kBIFID)
58  throw Common::Exception("Not a BIF file (%s)", Common::debugTag(_id).c_str());
59 
60  if ((_version != kVersion1) && (_version != kVersion11))
61  throw Common::Exception("Unsupported BIF file version %s", Common::debugTag(_version).c_str());
62 
63  uint32 varResCount = bif.readUint32LE();
64  uint32 fixResCount = bif.readUint32LE();
65 
66  if (fixResCount != 0)
67  throw Common::Exception("TODO: Fixed BIF resources");
68 
69  _iResources.resize(varResCount);
70 
71  uint32 offVarResTable = bif.readUint32LE();
72 
73  try {
74 
75  readVarResTable(bif, offVarResTable);
76 
77  } catch (Common::Exception &e) {
78  e.add("Failed reading BIF file");
79  throw;
80  }
81 
82 }
83 
85  bif.seek(offset);
86 
87  for (IResourceList::iterator res = _iResources.begin(); res != _iResources.end(); ++res) {
88  bif.skip(4); // ID
89 
90  if (_version == kVersion11)
91  bif.skip(4); // Flags
92 
93  res->offset = bif.readUint32LE();
94  res->size = bif.readUint32LE();
95  res->type = (FileType) bif.readUint32LE();
96  }
97 }
98 
99 void BIFFile::mergeKEY(const KEYFile &key, uint32 dataFileIndex) {
100  const KEYFile::ResourceList &keyResList = key.getResources();
101 
102  for (KEYFile::ResourceList::const_iterator keyRes = keyResList.begin(); keyRes != keyResList.end(); ++keyRes) {
103  if (keyRes->bifIndex != dataFileIndex)
104  continue;
105 
106  if (keyRes->resIndex >= _iResources.size()) {
107  warning("Resource index out of range (%d/%d)", keyRes->resIndex, (int) _iResources.size());
108  continue;
109  }
110 
111  if (keyRes->type != _iResources[keyRes->resIndex].type)
112  warning("KEY and BIF disagree on the type of the resource \"%s\" (%d, %d). Trusting the BIF",
113  keyRes->name.c_str(), keyRes->type, _iResources[keyRes->resIndex].type);
114 
115  Resource res;
116 
117  res.name = keyRes->name;
118  res.type = _iResources[keyRes->resIndex].type;
119  res.index = keyRes->resIndex;
120 
121  _resources.push_back(res);
122  }
123 
124 }
125 
127  return _resources;
128 }
129 
131  if (index >= _iResources.size())
132  throw Common::Exception("Resource index out of range (%u/%u)", index, (uint)_iResources.size());
133 
134  return _iResources[index];
135 }
136 
138  return getIResource(index).size;
139 }
140 
142  const IResource &res = getIResource(index);
143 
144  if (tryNoCopy)
145  return new Common::SeekableSubReadStream(_bif.get(), res.offset, res.offset + res.size);
146 
147  _bif->seek(res.offset);
148 
149  return _bif->readStream(res.size);
150 }
151 
152 } // End of namespace Aurora
void load(Common::SeekableReadStream &bif)
Definition: biffile.cpp:54
#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
void add(const char *s,...) GCC_PRINTF(2
Definition: error.cpp:58
uint32 readUint32LE()
Read an unsigned 32-bit word stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:133
virtual size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)=0
Sets the stream position indicator for the stream.
static const uint32 kVersion1
Definition: biffile.cpp:40
const ResourceList & getResources() const
Return a list of all containing resources.
Definition: keyfile.cpp:145
IResourceList _iResources
Internal list of resource offsets and sizes.
Definition: biffile.h:113
Implementing the reading stream interfaces for plain memory blocks.
Common::UString name
The resource&#39;s name.
Definition: archive.h:49
uint32 size
The resource&#39;s size.
Definition: biffile.h:102
static const uint32 kBIFID
Definition: biffile.cpp:39
static const uint32 kVersion11
Definition: biffile.cpp:41
Class to hold resource index information of a KEY file.
Definition: keyfile.h:70
Utility templates and functions for working with strings and streams.
Exception that provides a stack of explanations.
Definition: error.h:36
static void readHeader(Common::ReadStream &stream, uint32 &id, uint32 &version, bool &utf16le)
Read the header out of a stream.
Definition: aurorafile.cpp:53
Handling BioWare&#39;s KEYs (resource index files).
Basic exceptions to throw.
Handling BioWare&#39;s BIFs (resource data files).
Utility templates and functions.
void readVarResTable(Common::SeekableReadStream &bif, uint32 offset)
Definition: biffile.cpp:84
void mergeKEY(const KEYFile &key, uint32 dataFileIndex)
Merge information from the KEY into the data file.
Definition: biffile.cpp:99
std::list< Resource > ResourceList
Definition: archive.h:57
virtual size_t skip(ptrdiff_t offset)
Skip the specified number of bytes, adding that offset to the current position in the stream...
Definition: readstream.h:317
uint32 _id
The file&#39;s ID.
Definition: aurorafile.h:77
StackException Exception
Definition: error.h:59
A resource within the archive.
Definition: archive.h:48
uint32 _version
The file&#39;s version.
Definition: aurorafile.h:78
Internal resource information.
Definition: biffile.h:98
void warning(const char *s,...)
Definition: util.cpp:33
const IResource & getIResource(uint32 index) const
Definition: biffile.cpp:130
uint32 offset
The offset of the resource within the BIF.
Definition: biffile.h:101
const ResourceList & getResources() const
Return the list of resources.
Definition: biffile.cpp:126
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
BIFFile(Common::SeekableReadStream *bif)
Take over this stream and read a BIF file out of it.
Definition: biffile.cpp:45
ResourceList _resources
External list of resource names and types.
Definition: biffile.h:110
uint32_t uint32
Definition: types.h:204
UString debugTag(uint32 tag, bool trim)
Create an elaborate string from an integer tag, for debugging purposes.
Definition: strutil.cpp:117
Common::SeekableReadStream * getResource(uint32 index, bool tryNoCopy=false) const
Return a stream of the resource&#39;s contents.
Definition: biffile.cpp:141
FileType
Various file types used by the Aurora engine and found in archives.
Definition: types.h:56
SeekableSubReadStream provides access to a SeekableReadStream restricted to the range [begin...
Definition: readstream.h:359
uint32 getResourceSize(uint32 index) const
Return the size of a resource.
Definition: biffile.cpp:137
Interface for a seekable & readable data stream.
Definition: readstream.h:265
Common::ScopedPtr< Common::SeekableReadStream > _bif
Definition: biffile.h:107
std::vector< Resource > ResourceList
Definition: keyfile.h:81
unsigned int uint
Definition: types.h:211