31 #include "glm/gtc/type_ptr.hpp" 51 const size_t pos = gff4.
pos();
55 type = gff4Endian.readUint32BE();
71 gff4.
seek(gff4Endian.pos());
153 e.
add(
"Failed reading GFF4 file");
174 if ((type != 0xFFFFFFFF) && (
_header.
type != type))
194 static const uint32 kStructTemplateSize = 16;
199 _stream->seek(structTemplateStart + i * kStructTemplateSize);
214 if (fieldOffset == 0xFFFFFFFF) {
216 throw Common::Exception(
"GFF4: fieldOffset NULL, but fieldCount %u", fieldCount);
225 strct.
fields.resize(fieldCount);
226 for (
uint32 j = 0; j < fieldCount; j++) {
232 field.
type = (typeAndFlags & 0x0000FFFF);
233 field.
flags = (typeAndFlags & 0xFFFF0000) >> 16;
275 std::pair<StructMap::iterator, bool> result;
277 result =
_structs.insert(std::make_pair(
id, strct));
287 StructMap::iterator s =
_structs.find(
id);
329 isList(false), isReference(false), isGeneric(false), structIndex(0) {
334 label(l), offset(o), isGeneric(g) {
336 isList = (f & 0x8000) != 0;
340 const bool isStruct = (f & 0x4000) != 0;
353 bool supportedConfig =
true;
357 supportedConfig =
false;
359 supportedConfig =
false;
361 supportedConfig =
false;
363 supportedConfig =
false;
365 if (!supportedConfig)
383 load(parent, offset, tmplt);
391 _parent(&parent), _label(0), _refCount(0), _fieldCount(0) {
399 load(parent, genericParent);
431 for (
size_t i = 0; i < tmplt.
fields.size(); i++) {
438 if ((offset == 0xFFFFFFFF) || (field.
offset == 0xFFFFFFFF))
439 fieldOffset = 0xFFFFFFFF;
449 throw Common::Exception(
"GFF4: TODO: ASCII string field in a file with shared strings");
456 if (field.
offset == 0xFFFFFFFF)
482 field.
structs.resize(structCount, 0);
483 for (
uint32 i = 0; i < structCount; i++) {
485 if (offset == 0xFFFFFFFF)
490 strct =
new GFF4Struct(parent, offset, tmplt);
500 if (field.
offset == 0xFFFFFFFF)
511 field.
structs.push_back(strct);
521 static const uint32 kGenericSize = 8;
528 for (
uint32 i = 0; i < genericCount; i++) {
529 data.
seek(genericStart + i * kGenericSize);
532 const uint16 fieldType = (typeAndFlags & 0x0000FFFF);
533 const uint16 fieldFlags = (typeAndFlags & 0xFFFF0000) >> 16;
537 if (fieldOffset == 0xFFFFFFFF)
550 throw Common::Exception(
"GFF4: TODO: ASCII string field in a file with shared strings");
564 return (((
uint64) offset) << 32) | (tmplt ? tmplt->
index : 0xFFFFFFFF);
611 FieldMap::const_iterator f =
_fields.find(field);
619 if (!isReference || (offset == 0xFFFFFFFF))
625 if (offset == 0xFFFFFFFF)
640 if (offset == 0xFFFFFFFF)
666 if (length < minLength)
667 throw Common::Exception(
"GFF4: Vector/Matrix type is too short (%d < %d)", length, minLength);
668 if (length > maxLength)
669 throw Common::Exception(
"GFF4: Vector/Matrix type is too long (%d > %d)", length, maxLength);
679 if (listOffset == 0xFFFFFFFF)
845 const size_t offset = data.
pos();
848 const size_t size = length * lengthMult;
880 if (offset == 0xFFFFFFFF)
886 return getString(data, encoding, offset);
922 return getUint(field, def) != 0;
985 if (offset != 0xFFFFFFFF) {
988 else if (offset != 0)
1083 for (
uint32 i = 0; i < length; i++)
1099 for (
uint32 i = 0; i < length; i++)
1110 m = glm::make_mat4(f);
1125 vectorMatrix.resize(length);
1126 for (
uint32 i = 0; i < length; i++)
1143 vectorMatrix.resize(length);
1144 for (
uint32 i = 0; i < length; i++)
1161 for (
uint32 i = 0; i < count; i++)
1176 for (
uint32 i = 0; i < count; i++)
1191 for (
uint32 i = 0; i < count; i++)
1206 for (
uint32 i = 0; i < count; i++)
1221 for (
uint32 i = 0; i < count; i++)
1228 std::vector<Common::UString> &list)
const {
1244 for (
uint32 i = 0; i < count; i++)
1245 list[i] =
getString(*data, *f, encoding);
1255 std::vector<uint32> &strRefs, std::vector<Common::UString> &strs)
const {
1268 strRefs.resize(count);
1271 std::vector<uint32> offsets;
1272 offsets.resize(count);
1274 for (
uint32 i = 0; i < count; i++) {
1279 if (offset != 0xFFFFFFFF) {
1282 else if (offset != 0)
1291 std::vector<uint32> &strRefs, std::vector<Common::UString> &strs)
const {
1306 for (
uint32 i = 0; i < count; i++) {
1308 list[i].resize(length);
1309 for (
uint32 j = 0; j < length; j++)
1326 for (
uint32 i = 0; i < count; i++) {
1328 list[i].resize(length);
1329 for (
uint32 j = 0; j < length; j++)
1346 for (
uint32 i = 0; i < count; i++) {
1349 for (
uint32 j = 0; j < length; j++)
1352 list[i] = glm::make_mat4(m);
1416 if ((size == 0) || (count == 0))
1419 const size_t dataSize = count * size;
1420 const size_t dataBegin = data->
pos();
1421 const size_t dataEnd = data->
pos() + dataSize;
1423 if ((dataBegin >= data->
size()) || ((data->
size() - dataBegin) < dataSize))
#define ResMan
Shortcut for accessing the sound manager.
#define MKTAG(a0, a1, a2, a3)
A wrapper macro used around four character constants, like 'DATA', to ensure portability.
bool getVector3(uint32 field, double &v1, double &v2, double &v3) const
void add(const char *s,...) GCC_PRINTF(2
uint32 getType() const
Return the GFF4's specific type.
This is a wrapper around SeekableSubReadStream, but it adds non-endian read methods whose endianness ...
#define TypeMan
Shortcut for accessing the file type manager.
Common::UString getString(uint32 field, Common::Encoding encoding, const Common::UString &def="") const
Return a field string, read from the given encoding.
A class holding an UTF-8 string.
uint32 getDataOffset() const
uint32 label
A numerical label of the field.
virtual size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)=0
Sets the stream position indicator for the stream.
void reset(PointerType o=0)
Resets the pointer with the new value.
int64 getSint(uint32 field, int64 def=0) const
FORCEINLINE int8 readSByte()
Read a signed byte from the stream and return it.
uint32 getListCount(Common::SeekableSubReadStreamEndian &data, const Field &field) const
uint32 getFieldSize(FieldType type) const
Utility functions to handle files used in BioWare's Aurora engine.
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...
bool isBigEndian() const
Is the GFF4's platform natively big endian?
UTF-16 LE (little endian).
size_t pos() const
Obtains the current value of the stream position indicator of the stream.
void unregisterStruct(uint64 id)
const GFF4Struct * getGeneric(uint32 field) const
A GFF (generic file format) V4.0/V4.1 file, found in Dragon Age: Origins, Dragon Age 2 and Sonic Chro...
bool hasSharedStrings() const
float getFloat(uint32 field, float def=0.0f) const
Utility templates and functions for working with strings and streams.
Exception that provides a stack of explanations.
A 32bit fixed-point value, found in Sonic.
size_t size() const
Obtains the total size of the stream, measured in bytes.
static void readHeader(Common::ReadStream &stream, uint32 &id, uint32 &version, bool &utf16le)
Read the header out of a stream.
uint16 structIndex
Index of the field's struct type (if kFieldTypeStruct).
Common::ScopedPtr< Common::SeekableReadStream > _origStream
std::vector< const GFF4Struct * > GFF4List
size_t getBytesPerCodepoint(Encoding encoding)
Return the number of bytes per codepoint in this encoding.
Handling version V4.0/V4.1 of BioWare's GFFs (generic file format).
Basic exceptions to throw.
Common::SeekableReadStream * getData(uint32 field) const
Return the raw data of the field as a Seekable(Sub)ReadStream.
const char * c_str() const
Return the (utf8 encoded) string data.
ASCII string, found in Sonic.
void load(GFF4File &parent, uint32 offset, const GFF4File::StructTemplate &tmplt)
static const uint32 kVersion41
2 unsigned 32bit integers, reference into the TLK table.
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)
Sets the stream position indicator for the stream.
double getDouble(uint32 field, double def=0.0) const
static const uint32 kVersion40
FieldType type
Type of the field.
uint64 getID() const
Return the struct's unique ID within the GFF4.
uint32 getDataOffset(bool isReference, uint32 offset) const
void loadStructs(GFF4File &parent, Field &field)
std::vector< Field > fields
bool isList
Is this field a singular item or a list?
FieldType
The type of a GFF4 field.
bool hasField(uint32 field) const
Does this specific field exist?
double readNintendoFixedPoint(uint32 value, bool sign, uint8 iBits, uint8 fBits)
Read a fixed-point value, in a format used by the Nintendo DS.
uint32 getPlatform() const
Return the platform this GFF4 is for.
uint32 getRefCount() const
Return the number of structs that refer to this struct.
Utility functions for working with differing string encodings.
3 IEEE floats, 3D vector.
const Field * getField(uint32 field) const
uint32 _id
The file's ID.
bool getFieldProperties(uint32 field, FieldType &type, uint32 &label, bool &isList) const
Collectively return all field properties in one go.
uint32 _version
The file's version.
static uint64 generateID(uint32 offset, const GFF4File::StructTemplate *tmplt=0)
const StructTemplate & getStructTemplate(uint32 i) const
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
Basic reading stream interfaces.
Common::UString getSharedString(uint32 i) const
virtual size_t pos() const =0
Obtains the current value of the stream position indicator of the stream.
uint32 readUint32BE()
Read an unsigned 32-bit word stored in big endian (MSB first) order from the stream and return it...
Common::SeekableSubReadStreamEndian & getStream(uint32 offset) const
StructMap _structs
All actual structs in this GFF4.
GFF4List structs
List of GFF4Struct (if kFieldTypeStruct).
Plain, unextended ASCII (7bit clean).
PointerType get() const
Returns the plain pointer value.
bool isReference
Is this field a reference (pointer) to another field?
void loadHeader(uint32 type)
bool getVectorMatrix(uint32 field, std::vector< double > &vectorMatrix) const
Return a field vector or a matrix type as a std::vector of doubles.
bool getMatrix4x4(uint32 field, double(&m)[16]) const
const GFF4Struct * getStruct(uint32 field) const
GFF4File(Common::SeekableReadStream *gff4, uint32 type=0xFFFFFFFF)
Take over this stream and read a GFF4 file out of it.
FieldType getFieldType(uint32 field) const
Return the type of this field, or kFieldTypeNone if it doesn't exist.
void loadGeneric(GFF4File &parent, Field &field)
uint32 getTypeVersion() const
Return the GFF4's specific type version.
UString debugTag(uint32 tag, bool trim)
Create an elaborate string from an integer tag, for debugging purposes.
16 IEEE floats, 4x4 matrix in row-major order.
64bit IEEE float (double).
SharedStrings _sharedStrings
The shared strings used in V4.1.
FileType
Various file types used by the Aurora engine and found in archives.
A template of a struct, used when loading a struct.
bool getBool(uint32 field, bool def=false) const
Common::Encoding getNativeEncoding() const
Return the native UTF-16 encoding according to the GFF4's platform.
UString readString(SeekableReadStream &stream, Encoding encoding)
Read a string with the given encoding of a stream.
StructTemplates _structTemplates
All struct templates in this GFF4.
4 IEEE floats, Quaternion rotation.
UString readStringFixed(SeekableReadStream &stream, Encoding encoding, size_t length)
Read length bytes as a string with the given encoding out of a stream.
A field in the GFF4 struct.
Common::ScopedPtr< Common::SeekableSubReadStreamEndian > _stream
A "generic" field, able to hold any other type.
4 IEEE floats, 4D vector.
const std::vector< uint32 > & getFieldLabels() const
Return a list of all field labels in this struct.
const GFF4List & getList(uint32 field) const
SeekableSubReadStream provides access to a SeekableReadStream restricted to the range [begin...
GFF4Struct * findStruct(uint64 id)
size_t getFieldCount() const
Return the number of fields in this struct.
uint64 getUint(uint32 field, uint64 def=0) const
void clear()
Clear the string's contents.
void registerStruct(uint64 id, GFF4Struct *strct)
const GFF4Struct & getTopLevel() const
Returns the top-level struct.
GFF4Struct * _topLevelStruct
The top-level struct.
GFF4Struct(GFF4File &parent, uint32 offset, const GFF4File::StructTemplate &tmplt)
Load a GFF4 struct.
Interface for a seekable & readable data stream.
4 IEEE floats, RGBA color.
byte readByte()
Read an unsigned byte from the stream and return it.
std::vector< uint32 > _fieldLabels
The labels of all fields in this struct.
bool getVector4(uint32 field, double &v1, double &v2, double &v3, double &v4) const
Header _header
This GFF4's header.
uint32 offset
Offset into the GFF4 data.
The global resource manager for Aurora resources.
static const uint32 kGFFID
uint32 getLabel() const
Return the struct's label.
uint32 getVectorMatrixLength(const Field &field, uint32 minLength, uint32 maxLength) const
bool isGeneric
Is this field found in a generic?