quake-tools/source/quam/wad2.cc

86 lines
1.9 KiB
C++

#include "quam/wad2.h"
namespace Wad2 {
enum Compression {
CompressNone,
CompressLZSS,
};
}
static Arc::FileType getFileType(int n) {
switch(n) {
case 0: return Arc::FileType::Normal;
case 1: return Arc::FileType::Label;
case 64: return Arc::FileType::Palette;
case 65: return Arc::FileType::Texture;
case 66: return Arc::FileType::Picture;
case 67: return Arc::FileType::Sound;
case 68: return Arc::FileType::MipTexture;
default: throw EnumError("invalid file type");
}
}
static std::pair<quint32, quint32> readWad2Header(std::istream &st) {
auto magic = readBytes<4>(st);
if(magic != std::array{'W', 'A', 'D', '2'}) {
throw FileFormatError("not a wad2 file (invalid magic number)");
}
auto dirNum = readLE<quint32>(st);
auto dirOffset = readLE<quint32>(st);
return std::make_pair(dirOffset, dirNum);
}
static Arc::File readWad2Entry(std::istream &st) {
auto entOffset = readLE<quint32>(st);
auto entSize = readLE<quint32>(st);
auto entCSize = readLE<quint32>(st);
auto entType = readByte(st);
auto entCompr = readByte(st);
/* padding */ readBytes<2>(st);
auto entName = readBytes<16>(st);
if(entSize != entCSize || entCompr != Wad2::CompressNone) {
throw UnimplementedError("compressed files not supported");
}
auto pos = st.tellg();
st.seekg(entOffset);
QByteArray data;
data.resize(entSize);
st.read(data.data(), entSize);
st.seekg(pos);
Arc::File file{ntbsToString(entName), std::move(data)};
file.type = getFileType(entType);
return file;
}
Arc::Dir readWad2(std::istream &st) {
auto hdr = readWad2Header(st);
st.seekg(hdr.first);
Arc::Dir root{std::string{}};
for(quint32 i = 0; i < hdr.second; i++) {
root.emplaceBack(new Arc::File(readWad2Entry(st)));
}
return root;
}
Wad2Validator::Wad2Validator(QObject *parent) :
QValidator{parent}
{
}
QValidator::State Wad2Validator::validate(QString &input, int &pos) const {
return QValidator::Acceptable;
}
// EOF