Skip to content

Commit

Permalink
AURORA: Add a writer for ERF archives
Browse files Browse the repository at this point in the history
  • Loading branch information
Nostritius authored and DrMcCoy committed Jun 24, 2018
1 parent 80073d1 commit b100c48
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/aurora/erfwriter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* xoreos is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with xoreos. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* Writing BioWare's ERFs (encapsulated resource file).
*/

#include <ctime>

#include "src/aurora/erfwriter.h"

namespace Aurora {

static const uint32 kVersion10 = MKTAG('V', '1', '.', '0');

ERFWriter::ERFWriter(uint32 id, uint32 fileCount, Common::SeekableWriteStream &stream, Version version,
LocString description) : _stream(stream), _currentFileCount(0), _fileCount(fileCount) {
if (version != kERFVersion10)
throw Common::Exception("Unsupported ERF version");

stream.writeUint32BE(id);
stream.writeUint32BE(kVersion10);

// Write Header
stream.writeUint32LE(description.getNumStrings()); // Language count
stream.writeUint32LE(description.getWrittenSize()); // Localized string size

stream.writeUint32LE(_fileCount); // Entry Count

// The size of the ERF header, which is immediately followed by the LocString table
static const uint32 kLocStringTableOffset = 160;

_keyTableOffset = kLocStringTableOffset + description.getWrittenSize();
_resourceTableOffset = _keyTableOffset + _fileCount * 24;

stream.writeUint32LE(kLocStringTableOffset); // LocString offset
stream.writeUint32LE(_keyTableOffset); // Key List offset
stream.writeUint32LE(_resourceTableOffset); // Resource offset

// Write the creation time of the file
std::time_t now = std::time(0);
std::tm *timepoint = std::localtime(&now);
stream.writeUint32LE(timepoint->tm_year);
stream.writeUint32LE(timepoint->tm_yday);

// Write the description string reference
if (description.getNumStrings())
stream.writeUint32LE(description.getID());
else
stream.writeUint32LE(0);

// Write 116 bytes of reserved header data
stream.writeZeros(116);

// Write the Localized string table
description.writeLocString(stream);

// Write the empty key list
stream.writeZeros(_fileCount * 24);

// The offset to the resource table plus the size of the source table
_offsetToResourceData = _resourceTableOffset + 8 * _fileCount;

// Write the empty resource list
stream.writeZeros(8 * _fileCount);
}

ERFWriter::~ERFWriter() {
}

void ERFWriter::add(const Common::UString &resRef, FileType resType, Common::ReadStream &stream) {
if (_currentFileCount == _fileCount)
throw Common::Exception("More files added than expected");

// Write the key table entry
_stream.seek(_keyTableOffset + _currentFileCount * 24);

// TODO: Handle file type aliases and "virtual" file types

_stream.write(resRef.c_str(), MIN<size_t>(resRef.size(), 16));
_stream.writeZeros(16 - MIN<size_t>(resRef.size(), 16));
_stream.writeUint32LE(_currentFileCount);
_stream.writeUint16LE(resType);
_stream.writeUint16LE(0); // Unused

// Write the actual resource data
_stream.seek(_offsetToResourceData);
const size_t size = _stream.writeStream(stream);

// Write the resource table entry
_stream.seek(_resourceTableOffset + _currentFileCount * 8);

_stream.writeUint32LE(_offsetToResourceData);
_stream.writeUint32LE(size);

// Advance data offset and file count
_offsetToResourceData += size;
_currentFileCount += 1;
}

} // End of namespace Aurora
71 changes: 71 additions & 0 deletions src/aurora/erfwriter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* xoreos is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with xoreos. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* Writing BioWare's ERFs (encapsulated resource file).
*/

#ifndef AURORA_ERFWRITER_H
#define AURORA_ERFWRITER_H

#include "src/common/writestream.h"
#include "src/common/readstream.h"

#include "src/aurora/locstring.h"

namespace Aurora {

class ERFWriter {
public:
enum Version {
kERFVersion10

// TODO: Add other versions
};

/** Create an ERF writer by writing the header to the stream and reserve fileCount
* places in the key and resource table.
*
* @param id The FourCC for the archive.
* @param fileCount The number of files which should be contained in the archive.
* @param stream The write stream in which the archive should be written.
* @param version The ERF version to write
* @param description The LocString, that should be used for the description.
*/
ERFWriter(uint32 id, uint32 fileCount, Common::SeekableWriteStream &stream,
Version version = kERFVersion10, LocString description = LocString());
~ERFWriter();

/** Add a new stream to this archive to be packed. */
void add(const Common::UString &resRef, FileType resType, Common::ReadStream &stream);

private:
Common::SeekableWriteStream &_stream;

uint32 _currentFileCount;
uint32 _fileCount;
uint32 _offsetToResourceData;
uint32 _keyTableOffset;
uint32 _resourceTableOffset;
};

} // End of namespace Aurora

#endif // AURORA_ERFWRITER_H
2 changes: 2 additions & 0 deletions src/aurora/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ src_aurora_libaurora_la_SOURCES += \
src/aurora/biffile.h \
src/aurora/bzffile.h \
src/aurora/erffile.h \
src/aurora/erfwriter.h \
src/aurora/rimfile.h \
src/aurora/ndsrom.h \
src/aurora/zipfile.h \
Expand Down Expand Up @@ -74,6 +75,7 @@ src_aurora_libaurora_la_SOURCES += \
src/aurora/biffile.cpp \
src/aurora/bzffile.cpp \
src/aurora/erffile.cpp \
src/aurora/erfwriter.cpp \
src/aurora/rimfile.cpp \
src/aurora/ndsrom.cpp \
src/aurora/zipfile.cpp \
Expand Down

0 comments on commit b100c48

Please sign in to comment.