xoreos  0.0.5
gff4file.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_GFF4FILE_H
26 #define AURORA_GFF4FILE_H
27 
28 #include <vector>
29 #include <map>
30 
31 #include "glm/mat4x4.hpp"
32 
33 #include <boost/noncopyable.hpp>
34 
35 #include "src/common/types.h"
36 #include "src/common/scopedptr.h"
37 #include "src/common/ustring.h"
38 #include "src/common/encoding.h"
39 
40 #include "src/aurora/types.h"
41 #include "src/aurora/aurorafile.h"
42 #include "src/aurora/gff4fields.h"
43 
44 namespace Common {
45  class SeekableReadStream;
46  class SeekableSubReadStreamEndian;
47 }
48 
49 namespace Aurora {
50 
51 class GFF4Struct;
52 
93 class GFF4File : boost::noncopyable, public AuroraFile {
94 public:
96  GFF4File(Common::SeekableReadStream *gff4, uint32 type = 0xFFFFFFFF);
98  GFF4File(const Common::UString &gff4, FileType fileType, uint32 type = 0xFFFFFFFF);
99  ~GFF4File();
100 
102  uint32 getType() const;
104  uint32 getTypeVersion() const;
106  uint32 getPlatform() const;
107 
109  bool isBigEndian() const;
112 
114  const GFF4Struct &getTopLevel() const;
115 
116 
117 private:
118  enum Platform {
119  kPlatformPC = MKTAG('P', 'C', ' ', ' '),
120  kPlatformPS3 = MKTAG('P', 'S', '3', ' '),
121  kPlatformXbox360 = MKTAG('X', '3', '6', '0')
122  };
123 
125  struct Header {
133 
135 
136  void read(Common::SeekableReadStream &gff4, uint32 version);
137  bool isBigEndian() const;
138  };
139 
141  struct StructTemplate {
142  struct Field {
147  };
148 
152 
153  std::vector<Field> fields;
154  };
155 
156  typedef std::vector<StructTemplate> StructTemplates;
157  typedef std::vector<Common::UString> SharedStrings;
158  typedef std::map<uint64, GFF4Struct *> StructMap;
159 
160 
161 
164 
169 
172 
177 
178 
179  // .--- Loading helpers
180  void load(uint32 type);
181  void loadHeader(uint32 type);
182  void loadStructs();
183  void loadStrings();
184 
185  void clear();
186  // '---
187 
188  // .--- Helper methods called by GFF4Struct
189  void registerStruct(uint64 id, GFF4Struct *strct);
190  void unregisterStruct(uint64 id);
192 
194  const StructTemplate &getStructTemplate(uint32 i) const;
195  uint32 getDataOffset() const;
196 
197  bool hasSharedStrings() const;
199  // '---
200 
201  friend class GFF4Struct;
202 };
203 
204 class GFF4Struct {
205 public:
207  enum FieldType {
230  };
231 
233  uint64 getID() const;
235  uint32 getRefCount() const;
236 
250  uint32 getLabel() const;
251 
253  size_t getFieldCount() const;
255  bool hasField(uint32 field) const;
256 
258  const std::vector<uint32> &getFieldLabels() const;
259 
261  FieldType getFieldType(uint32 field) const;
263  FieldType getFieldType(uint32 field, bool &isList) const;
264 
269  bool getFieldProperties(uint32 field, FieldType &type, uint32 &label, bool &isList) const;
270 
271 
272  // .--- Single values
273  uint64 getUint(uint32 field, uint64 def = 0 ) const;
274  int64 getSint(uint32 field, int64 def = 0 ) const;
275  bool getBool(uint32 field, bool def = false) const;
276 
277  double getDouble(uint32 field, double def = 0.0 ) const;
278  float getFloat (uint32 field, float def = 0.0f) const;
279 
281  Common::UString getString(uint32 field, Common::Encoding encoding, const Common::UString &def = "") const;
282 
284  Common::UString getString(uint32 field, const Common::UString &def = "") const;
285 
287  bool getTalkString(uint32 field, Common::Encoding encoding, uint32 &strRef, Common::UString &str) const;
288 
290  bool getTalkString(uint32 field, uint32 &strRef, Common::UString &str) const;
291 
292  bool getVector3(uint32 field, double &v1, double &v2, double &v3) const;
293  bool getVector4(uint32 field, double &v1, double &v2, double &v3, double &v4) const;
294 
295  bool getMatrix4x4(uint32 field, double (&m)[16]) const;
296 
297  bool getVector3(uint32 field, float &v1, float &v2, float &v3) const;
298  bool getVector4(uint32 field, float &v1, float &v2, float &v3, float &v4) const;
299 
300  bool getMatrix4x4(uint32 field, float (&m)[16]) const;
301 
303  bool getVectorMatrix(uint32 field, std::vector<double> &vectorMatrix) const;
305  bool getVectorMatrix(uint32 field, std::vector<float > &vectorMatrix) const;
306 
307  bool getMatrix4x4(uint32 field, glm::mat4 &m) const;
308  // '---
309 
310  // .--- Lists of values
311  bool getUint(uint32 field, std::vector<uint64> &list) const;
312  bool getSint(uint32 field, std::vector< int64> &list) const;
313  bool getBool(uint32 field, std::vector<bool > &list) const;
314 
315  bool getDouble(uint32 field, std::vector<double> &list) const;
316  bool getFloat (uint32 field, std::vector<float > &list) const;
317 
319  bool getString(uint32 field, Common::Encoding encoding, std::vector<Common::UString> &list) const;
320 
322  bool getString(uint32 field, std::vector<Common::UString> &list) const;
323 
325  bool getTalkString(uint32 field, Common::Encoding encoding,
326  std::vector<uint32> &strRefs, std::vector<Common::UString> &strs) const;
327 
329  bool getTalkString(uint32 field,
330  std::vector<uint32> &strRefs, std::vector<Common::UString> &strs) const;
331 
333  bool getVectorMatrix(uint32 field, std::vector< std::vector<double> > &list) const;
335  bool getVectorMatrix(uint32 field, std::vector< std::vector<float > > &list) const;
336 
337  bool getMatrix4x4(uint32 field, std::vector<glm::mat4> &list) const;
338  // '---
339 
340  // .--- Structs and lists of structs
341  const GFF4Struct *getStruct (uint32 field) const;
342  const GFF4Struct *getGeneric(uint32 field) const;
343  const GFF4List &getList (uint32 field) const;
344  // '---
345 
346  // .--- Raw data
349  // '---
350 
351 private:
353  struct Field {
357 
358  bool isList;
359  bool isReference;
360  bool isGeneric;
361 
364 
365  Field();
366  Field(uint32 l, uint16 t, uint16 f, uint32 o, bool g = false);
367  ~Field();
368  };
369 
370  typedef std::map<uint32, Field> FieldMap;
371 
372 
374 
376 
379 
380  size_t _fieldCount;
381 
383 
385  std::vector<uint32> _fieldLabels;
386 
387 
388  // .--- Loader
390  GFF4Struct(GFF4File &parent, uint32 offset, const GFF4File::StructTemplate &tmplt);
392  GFF4Struct(GFF4File &parent, const Field &genericParent);
393  ~GFF4Struct();
394 
395  void load(GFF4File &parent, uint32 offset, const GFF4File::StructTemplate &tmplt);
396  void loadStructs(GFF4File &parent, Field &field);
397  void loadGeneric(GFF4File &parent, Field &field);
398 
399  void load(GFF4File &parent, const Field &genericParent);
400 
401  static uint64 generateID(uint32 offset, const GFF4File::StructTemplate *tmplt = 0);
402  // '---
403 
404  // .--- Field and field data accessors
405  const Field *getField(uint32 field) const;
406 
407  uint32 getDataOffset(bool isReference, uint32 offset) const;
408  uint32 getDataOffset(const Field &field) const;
409 
411  Common::SeekableSubReadStreamEndian *getField(uint32 fieldID, const Field *&field) const;
412  // '---
413 
414  // .--- Field reader helpers
416  uint32 getFieldSize(FieldType type) const;
417 
420 
421  double getDouble(Common::SeekableSubReadStreamEndian &data, FieldType type) const;
422  float getFloat (Common::SeekableSubReadStreamEndian &data, FieldType type) const;
423 
426  uint32 offset) const;
428  Common::Encoding encoding) const;
429 
430  uint32 getVectorMatrixLength(const Field &field, uint32 minLength, uint32 maxLength) const;
431  // '---
432 
433 
434  friend class GFF4File;
435 };
436 
437 } // End of namespace Aurora
438 
439 #endif // AURORA_GFF4FILE_H
#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
uint32 stringCount
Number of shared strings (V4.1 only).
Definition: gff4file.h:130
bool getVector3(uint32 field, double &v1, double &v2, double &v3) const
Definition: gff4file.cpp:999
Signed 8bit integer.
Definition: gff4file.h:210
std::map< uint32, Field > FieldMap
Definition: gff4file.h:370
A GFF4 header.
Definition: gff4file.h:125
size_t _fieldCount
Definition: gff4file.h:380
uint32 getType() const
Return the GFF4&#39;s specific type.
Definition: gff4file.cpp:112
This is a wrapper around SeekableSubReadStream, but it adds non-endian read methods whose endianness ...
Definition: readstream.h:383
Definition: 2dafile.h:39
Common::UString getString(uint32 field, Common::Encoding encoding, const Common::UString &def="") const
Return a field string, read from the given encoding.
Definition: gff4file.cpp:949
A class holding an UTF-8 string.
Definition: ustring.h:48
uint32 getDataOffset() const
Definition: gff4file.cpp:300
All currently known fields in V4.0/V4.1 GFFs.
uint32 label
A numerical label of the field.
Definition: gff4file.h:354
std::vector< StructTemplate > StructTemplates
Definition: gff4file.h:156
int64 getSint(uint32 field, int64 def=0) const
Definition: gff4file.cpp:909
uint64_t uint64
Definition: types.h:206
uint32 getListCount(Common::SeekableSubReadStreamEndian &data, const Field &field) const
Definition: gff4file.cpp:674
uint32 getFieldSize(FieldType type) const
Definition: gff4file.cpp:687
Signed 64bit integer.
Definition: gff4file.h:216
bool getTalkString(uint32 field, Common::Encoding encoding, uint32 &strRef, Common::UString &str) const
Return a talk string, which is a reference into the TalkTable and an optional direct string...
Definition: gff4file.cpp:967
bool isBigEndian() const
Is the GFF4&#39;s platform natively big endian?
Definition: gff4file.cpp:124
void unregisterStruct(uint64 id)
Definition: gff4file.cpp:282
const GFF4Struct * getGeneric(uint32 field) const
Definition: gff4file.cpp:1378
A GFF (generic file format) V4.0/V4.1 file, found in Dragon Age: Origins, Dragon Age 2 and Sonic Chro...
Definition: gff4file.h:93
bool hasSharedStrings() const
Definition: gff4file.cpp:312
float getFloat(uint32 field, float def=0.0f) const
Definition: gff4file.cpp:937
A 32bit fixed-point value, found in Sonic.
Definition: gff4file.h:226
uint16 structIndex
Index of the field&#39;s struct type (if kFieldTypeStruct).
Definition: gff4file.h:362
void loadStrings()
Definition: gff4file.cpp:245
A simple scoped smart pointer template.
Signed 32bit integer.
Definition: gff4file.h:214
Common::ScopedPtr< Common::SeekableReadStream > _origStream
Definition: gff4file.h:162
std::vector< const GFF4Struct * > GFF4List
Definition: types.h:453
uint32 structCount
Number of struct templates in this GFF4.
Definition: gff4file.h:129
Unsigned 32bit integer.
Definition: gff4file.h:213
Unsigned 64bit integer.
Definition: gff4file.h:215
Common::SeekableReadStream * getData(uint32 field) const
Return the raw data of the field as a Seekable(Sub)ReadStream.
Definition: gff4file.cpp:1407
ASCII string, found in Sonic.
Definition: gff4file.h:227
void load(GFF4File &parent, uint32 offset, const GFF4File::StructTemplate &tmplt)
Definition: gff4file.cpp:423
uint32 dataOffset
Offset to the data portion.
Definition: gff4file.h:132
2 unsigned 32bit integers, reference into the TLK table.
Definition: gff4file.h:225
double getDouble(uint32 field, double def=0.0) const
Definition: gff4file.cpp:925
uint16_t uint16
Definition: types.h:202
uint32 stringOffset
Offset to shared strings (V4.1 only).
Definition: gff4file.h:131
uint32 platformID
ID of the platform this GFF4 is for.
Definition: gff4file.h:126
FieldType type
Type of the field.
Definition: gff4file.h:355
uint64 getID() const
Return the struct&#39;s unique ID within the GFF4.
Definition: gff4file.cpp:409
Base for BioWare&#39;s Aurora engine files.
uint32 getDataOffset(bool isReference, uint32 offset) const
Definition: gff4file.cpp:618
void loadStructs(GFF4File &parent, Field &field)
Definition: gff4file.cpp:455
void load(uint32 type)
Definition: gff4file.cpp:143
std::vector< Field > fields
Definition: gff4file.h:153
const GFF4File * _parent
Definition: gff4file.h:373
bool isList
Is this field a singular item or a list?
Definition: gff4file.h:358
FieldType
The type of a GFF4 field.
Definition: gff4file.h:207
bool hasField(uint32 field) const
Does this specific field exist?
Definition: gff4file.cpp:573
uint32 getPlatform() const
Return the platform this GFF4 is for.
Definition: gff4file.cpp:120
uint32 getRefCount() const
Return the number of structs that refer to this struct.
Definition: gff4file.cpp:413
Utility functions for working with differing string encodings.
Encoding
Definition: encoding.h:37
Low-level type definitions to handle fixed width types portably.
3 IEEE floats, 3D vector.
Definition: gff4file.h:219
const Field * getField(uint32 field) const
Definition: gff4file.cpp:610
bool getFieldProperties(uint32 field, FieldType &type, uint32 &label, bool &isList) const
Collectively return all field properties in one go.
Definition: gff4file.cpp:596
std::map< uint64, GFF4Struct * > StructMap
Definition: gff4file.h:158
static uint64 generateID(uint32 offset, const GFF4File::StructTemplate *tmplt=0)
Definition: gff4file.cpp:556
const StructTemplate & getStructTemplate(uint32 i) const
Definition: gff4file.cpp:304
bool isBigEndian() const
Definition: gff4file.cpp:74
Common::UString getSharedString(uint32 i) const
Definition: gff4file.cpp:316
FieldMap _fields
Definition: gff4file.h:382
uint32 type
The specific type this GFF4 describes.
Definition: gff4file.h:127
int64_t int64
Definition: types.h:205
void loadStructs()
Definition: gff4file.cpp:182
Common::SeekableSubReadStreamEndian & getStream(uint32 offset) const
Definition: gff4file.cpp:294
Unicode string handling.
StructMap _structs
All actual structs in this GFF4.
Definition: gff4file.h:174
Basic type definitions to handle files used in BioWare&#39;s Aurora engine.
GFF4List structs
List of GFF4Struct (if kFieldTypeStruct).
Definition: gff4file.h:363
bool isReference
Is this field a reference (pointer) to another field?
Definition: gff4file.h:359
void loadHeader(uint32 type)
Definition: gff4file.cpp:158
bool getVectorMatrix(uint32 field, std::vector< double > &vectorMatrix) const
Return a field vector or a matrix type as a std::vector of doubles.
Definition: gff4file.cpp:1114
bool getMatrix4x4(uint32 field, double(&m)[16]) const
Definition: gff4file.cpp:1073
const GFF4Struct * getStruct(uint32 field) const
Definition: gff4file.cpp:1360
GFF4File(Common::SeekableReadStream *gff4, uint32 type=0xFFFFFFFF)
Take over this stream and read a GFF4 file out of it.
Definition: gff4file.cpp:79
FieldType getFieldType(uint32 field) const
Return the type of this field, or kFieldTypeNone if it doesn&#39;t exist.
Definition: gff4file.cpp:581
void loadGeneric(GFF4File &parent, Field &field)
Definition: gff4file.cpp:498
uint32 getTypeVersion() const
Return the GFF4&#39;s specific type version.
Definition: gff4file.cpp:116
uint32_t uint32
Definition: types.h:204
16 IEEE floats, 4x4 matrix in row-major order.
Definition: gff4file.h:224
64bit IEEE float (double).
Definition: gff4file.h:218
SharedStrings _sharedStrings
The shared strings used in V4.1.
Definition: gff4file.h:171
Unsigned 16bit integer.
Definition: gff4file.h:211
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
A template of a struct, used when loading a struct.
Definition: gff4file.h:141
bool getBool(uint32 field, bool def=false) const
Definition: gff4file.cpp:921
Common::Encoding getNativeEncoding() const
Return the native UTF-16 encoding according to the GFF4&#39;s platform.
Definition: gff4file.cpp:128
StructTemplates _structTemplates
All struct templates in this GFF4.
Definition: gff4file.h:168
4 IEEE floats, Quaternion rotation.
Definition: gff4file.h:221
A field in the GFF4 struct.
Definition: gff4file.h:353
Common::ScopedPtr< Common::SeekableSubReadStreamEndian > _stream
Definition: gff4file.h:163
A "generic" field, able to hold any other type.
Definition: gff4file.h:229
4 IEEE floats, 4D vector.
Definition: gff4file.h:220
const std::vector< uint32 > & getFieldLabels() const
Return a list of all field labels in this struct.
Definition: gff4file.cpp:577
const GFF4List & getList(uint32 field) const
Definition: gff4file.cpp:1394
Unsigned 8bit integer.
Definition: gff4file.h:209
Signed 16bit integer.
Definition: gff4file.h:212
GFF4Struct * findStruct(uint64 id)
Definition: gff4file.cpp:286
size_t getFieldCount() const
Return the number of fields in this struct.
Definition: gff4file.cpp:569
uint64 getUint(uint32 field, uint64 def=0) const
Definition: gff4file.cpp:897
void registerStruct(uint64 id, GFF4Struct *strct)
Definition: gff4file.cpp:264
const GFF4Struct & getTopLevel() const
Returns the top-level struct.
Definition: gff4file.cpp:135
GFF4Struct * _topLevelStruct
The top-level struct.
Definition: gff4file.h:176
GFF4Struct(GFF4File &parent, uint32 offset, const GFF4File::StructTemplate &tmplt)
Load a GFF4 struct.
Definition: gff4file.cpp:374
void read(Common::SeekableReadStream &gff4, uint32 version)
Definition: gff4file.cpp:48
Interface for a seekable & readable data stream.
Definition: readstream.h:265
uint32 typeVersion
The version of the specific type this GFF4 describes.
Definition: gff4file.h:128
4 IEEE floats, RGBA color.
Definition: gff4file.h:223
std::vector< uint32 > _fieldLabels
The labels of all fields in this struct.
Definition: gff4file.h:385
bool getVector4(uint32 field, double &v1, double &v2, double &v3, double &v4) const
Definition: gff4file.cpp:1035
Header _header
This GFF4&#39;s header.
Definition: gff4file.h:166
uint32 offset
Offset into the GFF4 data.
Definition: gff4file.h:356
std::vector< Common::UString > SharedStrings
Definition: gff4file.h:157
uint32 getLabel() const
Return the struct&#39;s label.
Definition: gff4file.cpp:417
uint32 getVectorMatrixLength(const Field &field, uint32 minLength, uint32 maxLength) const
Definition: gff4file.cpp:653
bool isGeneric
Is this field found in a generic?
Definition: gff4file.h:360