xoreos  0.0.5
gff3file.h
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 #ifndef AURORA_GFF3FILE_H
26 #define AURORA_GFF3FILE_H
27 
28 #include <vector>
29 #include <map>
30 
31 #include <boost/noncopyable.hpp>
32 
33 #include "src/common/types.h"
34 #include "src/common/scopedptr.h"
35 #include "src/common/ptrvector.h"
36 #include "src/common/ustring.h"
37 
38 #include "src/aurora/types.h"
39 #include "src/aurora/aurorafile.h"
40 
41 namespace Common {
42  class SeekableReadStream;
43 }
44 
45 namespace Aurora {
46 
47 class LocString;
48 class GFF3Struct;
49 
85 class GFF3File : boost::noncopyable, public AuroraFile {
86 public:
88  GFF3File(Common::SeekableReadStream *gff3, uint32 id = 0xFFFFFFFF, bool repairNWNPremium = false);
90  GFF3File(const Common::UString &gff3, FileType type, uint32 id = 0xFFFFFFFF, bool repairNWNPremium = false);
91  virtual ~GFF3File();
92 
94  uint32 getType() const;
95 
97  const GFF3Struct &getTopLevel() const;
98 
99 
100 private:
102  struct Header {
115 
116  Header();
117 
118  void read(Common::SeekableReadStream &gff3);
119  };
120 
122  typedef std::vector<GFF3List> ListArray;
123 
124 
126 
128 
133 
136 
138  std::vector<uint32> _listOffsetToIndex;
139 
140 
141  // .--- Loading helpers
142  void load(uint32 id);
143  void loadHeader(uint32 id);
144  void loadStructs();
145  void loadLists();
146  // '---
147 
148  // .--- Helper methods called by GFF3Struct
153 
155  const GFF3Struct &getStruct(uint32 i) const;
157  const GFF3List &getList (uint32 i) const;
158  // '---
159 
160  friend class GFF3Struct;
161 };
162 
164 class GFF3Struct {
165 public:
167  enum FieldType {
188  };
189 
199  uint32 getID() const;
200 
202  size_t getFieldCount() const;
204  bool hasField(const Common::UString &field) const;
205 
207  const std::vector<Common::UString> &getFieldNames() const;
208 
210  FieldType getFieldType(const Common::UString &field) const;
211 
212 
213  // .--- Read field values
214  char getChar(const Common::UString &field, char def = '\0' ) const;
215  uint64 getUint(const Common::UString &field, uint64 def = 0 ) const;
216  int64 getSint(const Common::UString &field, int64 def = 0 ) const;
217  bool getBool(const Common::UString &field, bool def = false) const;
218 
219  double getDouble(const Common::UString &field, double def = 0.0) const;
220 
222  const Common::UString &def = "") const;
223 
224  bool getLocString(const Common::UString &field, LocString &str) const;
225 
226  void getVector (const Common::UString &field,
227  float &x, float &y, float &z ) const;
228  void getOrientation(const Common::UString &field,
229  float &a, float &b, float &c, float &d) const;
230 
231  void getVector (const Common::UString &field,
232  double &x, double &y, double &z ) const;
233  void getOrientation(const Common::UString &field,
234  double &a, double &b, double &c, double &d) const;
235 
237  // '---
238 
239  // .--- Structs and lists of structs
240  const GFF3Struct &getStruct(const Common::UString &field) const;
241  const GFF3List &getList (const Common::UString &field) const;
242  // '---
243 
244 private:
246  struct Field {
249  bool extended;
250 
251  Field();
252  Field(FieldType t, uint32 d);
253  };
254 
255  typedef std::map<Common::UString, Field> FieldMap;
256 
257 
258  const GFF3File *_parent;
259 
263 
265 
267  std::vector<Common::UString> _fieldNames;
268 
269 
270  // .--- Loader
271  GFF3Struct(const GFF3File &parent, uint32 offset);
272  ~GFF3Struct();
273 
274  void load(uint32 offset);
275 
276  void readField (Common::SeekableReadStream &data, uint32 index);
277  void readFields (Common::SeekableReadStream &data, uint32 index, uint32 count);
279  std::vector<uint32> &indices, uint32 count) const;
280 
282  // '---
283 
284  // .--- Field and field data accessors
286  const Field *getField(const Common::UString &name) const;
288  Common::SeekableReadStream &getData(const Field &field) const;
289  // '---
290 
291  friend class GFF3File;
292 
293  template<typename T>
294  friend void Common::DeallocatorDefault::destroy(T *);
295 };
296 
297 } // End of namespace Aurora
298 
299 #endif // AURORA_GFF3FILE_H
Header _header
The GFF3&#39;s header.
Definition: gff3file.h:127
int64 getSint(const Common::UString &field, int64 def=0) const
Definition: gff3file.cpp:473
std::vector< GFF3List > ListArray
Definition: gff3file.h:122
A single character.
Definition: gff3file.h:170
A GFF3 header.
Definition: gff3file.h:102
bool _repairNWNPremium
Should we try to read GFF3 files found in Neverwinter Nights premium modules?
Definition: gff3file.h:130
std::vector< uint32 > _listOffsetToIndex
To convert list offsets found in GFF3 to real indices.
Definition: gff3file.h:138
Definition: 2dafile.h:39
const GFF3List & getList(uint32 i) const
Return a list within the GFF3.
Definition: gff3file.cpp:260
bool getBool(const Common::UString &field, bool def=false) const
Definition: gff3file.cpp:510
uint32 fieldOffset
Offset to the field definitions.
Definition: gff3file.h:105
bool getLocString(const Common::UString &field, LocString &str) const
Definition: gff3file.cpp:614
void readFields(Common::SeekableReadStream &data, uint32 index, uint32 count)
Definition: gff3file.cpp:354
A class holding an UTF-8 string.
Definition: ustring.h:48
void loadHeader(uint32 id)
Definition: gff3file.cpp:110
A localized string.
Definition: locstring.h:43
const GFF3Struct & getStruct(uint32 i) const
Return a struct within the GFF3.
Definition: gff3file.cpp:253
uint32 _fieldIndex
Field / Field indices index.
Definition: gff3file.h:261
uint64_t uint64
Definition: types.h:206
uint32 structOffset
Offset to the struct definitions.
Definition: gff3file.h:103
uint32 labelOffset
Offset to the field labels.
Definition: gff3file.h:107
uint32 getID() const
Return the struct&#39;s ID.
Definition: gff3file.cpp:311
const GFF3File * _parent
The parent GFF3.
Definition: gff3file.h:258
Unsigned 16bit integer.
Definition: gff3file.h:171
bool hasField(const Common::UString &field) const
Does this specific field exist?
Definition: gff3file.cpp:400
String reference, index into a talk table.
Definition: gff3file.h:187
void getOrientation(const Common::UString &field, float &a, float &b, float &c, float &d) const
Definition: gff3file.cpp:676
StructArray _structs
Our structs.
Definition: gff3file.h:134
char getChar(const Common::UString &field, char def='\0') const
Definition: gff3file.cpp:426
const GFF3Struct & getTopLevel() const
Returns the top-level struct.
Definition: gff3file.cpp:91
std::map< Common::UString, Field > FieldMap
Definition: gff3file.h:255
void read(Common::SeekableReadStream &gff3)
Definition: gff3file.cpp:50
uint64 getUint(const Common::UString &field, uint64 def=0) const
Definition: gff3file.cpp:436
FieldType getFieldType(const Common::UString &field) const
Return the type of this field, or kFieldTypeNone if such a field doesn&#39;t exist.
Definition: gff3file.cpp:408
uint32 structCount
Number of structs.
Definition: gff3file.h:104
FieldType type
Type of the field.
Definition: gff3file.h:247
FieldMap _fields
The fields, indexed by their label.
Definition: gff3file.h:264
uint32 data
Data of the field.
Definition: gff3file.h:248
void load(uint32 id)
Definition: gff3file.cpp:97
FieldType
The type of a GFF3 field.
Definition: gff3file.h:167
A simple scoped smart pointer template.
Common::SeekableReadStream & getStream(uint32 offset) const
Return the GFF3 stream.
Definition: gff3file.cpp:275
A vector of 3 floats.
Definition: gff3file.h:186
uint32 fieldCount
Number of fields.
Definition: gff3file.h:106
uint32 listIndicesOffset
Offset to the list indices.
Definition: gff3file.h:113
void getVector(const Common::UString &field, float &x, float &y, float &z) const
Definition: gff3file.cpp:660
Unsigned 64bit integer.
Definition: gff3file.h:175
Random data of variable length.
Definition: gff3file.h:182
double getDouble(const Common::UString &field, double def=0.0) const
Definition: gff3file.cpp:514
Base for BioWare&#39;s Aurora engine files.
static void destroy(T *x)
Definition: deallocator.h:40
A GFF (generic file format) V3.2/V3.3 file, found in all Aurora games except Sonic Chronicles: The Da...
Definition: gff3file.h:85
Signed 16bit integer.
Definition: gff3file.h:172
Signed 64bit integer.
Definition: gff3file.h:176
uint32 fieldDataCount
Number of field data fields.
Definition: gff3file.h:110
Low-level type definitions to handle fixed width types portably.
uint32 fieldIndicesCount
Number of field indices.
Definition: gff3file.h:112
A vector storing pointer to objects, with automatic deletion.
uint32 _fieldCount
Field count.
Definition: gff3file.h:262
uint32 fieldDataOffset
Offset to the field data.
Definition: gff3file.h:109
uint32 getType() const
Return the GFF3&#39;s specific type.
Definition: gff3file.cpp:87
std::vector< const GFF3Struct * > GFF3List
Definition: types.h:449
uint32 _id
The struct&#39;s ID.
Definition: gff3file.h:260
uint32 _offsetCorrection
The correctional value for offsets to repair Neverwinter Nights premium modules.
Definition: gff3file.h:132
int64_t int64
Definition: types.h:205
Unicode string handling.
Basic type definitions to handle files used in BioWare&#39;s Aurora engine.
ListArray _lists
Our lists.
Definition: gff3file.h:135
const GFF3List & getList(const Common::UString &field) const
Definition: gff3file.cpp:741
Common::PtrVector< GFF3Struct > StructArray
Definition: gff3file.h:121
Unsigned 32bit integer.
Definition: gff3file.h:173
Common::SeekableReadStream & getFieldData() const
Return the GFF3 stream seeked to the start of the field data.
Definition: gff3file.cpp:281
Signed 32bit integer.
Definition: gff3file.h:174
virtual ~GFF3File()
Definition: gff3file.cpp:84
A field in the GFF3 struct.
Definition: gff3file.h:246
std::vector< Common::UString > _fieldNames
The names of all fields in this struct.
Definition: gff3file.h:267
A struct within a GFF3.
Definition: gff3file.h:164
Resource reference, string.
Definition: gff3file.h:180
uint32_t uint32
Definition: types.h:204
const GFF3Struct & getStruct(const Common::UString &field) const
Definition: gff3file.cpp:728
size_t getFieldCount() const
Return the number of fields in this struct.
Definition: gff3file.cpp:396
uint32 listIndicesCount
Number of list indices.
Definition: gff3file.h:114
Base class for most files found in games using BioWare&#39;s Aurora engine.
Definition: aurorafile.h:52
FileType
Various file types used by the Aurora engine and found in archives.
Definition: types.h:56
Common::SeekableReadStream * getData(const Common::UString &field) const
Definition: gff3file.cpp:638
GFF3Struct(const GFF3File &parent, uint32 offset)
Definition: gff3file.cpp:304
Common::ScopedPtr< Common::SeekableReadStream > _stream
Definition: gff3file.h:125
List containing a number of structs.
Definition: gff3file.h:184
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
uint32 labelCount
Number of labels.
Definition: gff3file.h:108
Struct containing a number of fields.
Definition: gff3file.h:183
void loadStructs()
Definition: gff3file.cpp:183
void readIndices(Common::SeekableReadStream &data, std::vector< uint32 > &indices, uint32 count) const
Definition: gff3file.cpp:372
GFF3File(Common::SeekableReadStream *gff3, uint32 id=0xFFFFFFFF, bool repairNWNPremium=false)
Take over this stream and read a GFF3 file out of it.
Definition: gff3file.cpp:66
void readField(Common::SeekableReadStream &data, uint32 index)
Definition: gff3file.cpp:331
bool extended
Does this field need extended data?
Definition: gff3file.h:249
Interface for a seekable & readable data stream.
Definition: readstream.h:265
const Field * getField(const Common::UString &name) const
Returns the field with this tag.
Definition: gff3file.cpp:418
void load(uint32 offset)
Definition: gff3file.cpp:317
const std::vector< Common::UString > & getFieldNames() const
Return a list of all field names in this struct.
Definition: gff3file.cpp:404
uint32 fieldIndicesOffset
Offset to the field indices.
Definition: gff3file.h:111
Common::UString readLabel(Common::SeekableReadStream &data, uint32 index) const
Definition: gff3file.cpp:379