123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- //
- // Created by Иван_Архипов on 01.11.2017.
- //
- #include "Subfile.h"
- #include "BinaryData.h"
- #include "DatFile.h"
- #include "DatException.h"
- #include <iostream>
- const long long MAXSIZE = 10000000ll;
- LOTRO_DAT::Subfile::Subfile() {
- ext_ = UNKNOWN;
- }
- LOTRO_DAT::Subfile::Subfile(DatFile *dat, long long unknown0, long long unknown1, long long file_id, long long file_offset, long long size1,
- long long timestamp, long long version, long long size2) :
- dat_(dat), unknown0_(unknown0), unknown1_(unknown1), file_id_(file_id), file_offset_(file_offset + 8),
- size1_(size1), timestamp_(timestamp), version_(version), size2_(size2) {
- if (size1_ > MAXSIZE)
- throw DatException("Bad Subfile::Subfile() - File size is too much... Maybe it's incorrect..?", SUBFILE_EXCEPTION);
- ext_ = GetExtension();
- }
- long long LOTRO_DAT::Subfile::unknown0() const {
- return unknown0_;
- }
- long long LOTRO_DAT::Subfile::unknown1() const {
- return unknown1_;
- }
- long long LOTRO_DAT::Subfile::file_id() const {
- return file_id_;
- }
- long long LOTRO_DAT::Subfile::file_offset() const {
- return file_offset_;
- }
- long long LOTRO_DAT::Subfile::size1() const {
- return size1_;
- }
- long long LOTRO_DAT::Subfile::timestamp() const {
- return timestamp_;
- }
- long long LOTRO_DAT::Subfile::version() const {
- return version_;
- }
- long long LOTRO_DAT::Subfile::size2() const {
- return size2_;
- }
- LOTRO_DAT::EXTENSION LOTRO_DAT::Subfile::GetExtension() const {
- // Text check based on file_id
- if ((file_id_ >> 24ll) == 0x25ll)
- return TXT;
- // Font check based on file_id
- if ((file_id_ >> 24ll) == 0x42ll)
- return FONT;
- BinaryData header(64);
- try {
- dat_->ReadData(header, 64, (unsigned) file_offset_);
- } catch (DatException &e) {
- if (e.type() == READ_EXCEPTION) {
- std::string err =
- "Bad Subfile::getExtension() - unable to read header of file with id = " + std::to_string(file_id()) +
- " and offset = " + std::to_string(file_offset());
- throw DatException(err.c_str(), SUBFILE_EXCEPTION);
- } else
- throw e;
- }
- // jpeg / dds check
- if ((file_id_ >> 24ll) == 0x41ll) {
- long long soi = header.ToNumber<2>(24);
- long long marker = header.ToNumber<2>(26);
- //auto markerSize = header.ToNumber<short>(28);
- //auto four = header.ToNumber<int>(30);
- if (soi == 0xD8FFll && marker == 0xE0FFll || marker == 0xE1FFll)
- return JPG;
- return DDS;
- }
- if (header[8] == 0x4F && header[9] == 0x67 && header[10] == 0x67 && header[11] == 0x53)
- return OGG;
- if (header[8] == 0x52 && header[9] == 0x49 && header[10] == 0x46 && header[11] == 0x46)
- return WAV;
- return UNKNOWN;
- }
- LOTRO_DAT::EXTENSION LOTRO_DAT::Subfile::ext() const {
- return ext_;
- }
- bool LOTRO_DAT::Subfile::ExportFile(const char *filename) const {
- try {
- if (ext() == TXT)
- return ExportAsTXT(filename);
- if (ext() == JPG)
- return ExportAsJPG(filename);
- if (ext() == DDS)
- return ExportAsDDS(filename);
- if (ext() == OGG)
- return ExportAsOGG(filename);
- if (ext() == WAV)
- return ExportAsWAV(filename);
- if (ext() == FONT)
- return ExportAsFont(filename);
- if (ext() == UNKNOWN)
- return ExportAsUnk(filename);
- }
- catch (...) {
- fprintf(stderr, "Caught exception while unpacking the file with id %lld and offset %lld. Continuing...\n", file_id(), file_offset());
- return false;
- }
- throw DatException("Bad Subfile::ExportFile() - got incorrect extension", EXPORT_EXCEPTION);
- }
- bool LOTRO_DAT::Subfile::ExportAsTXT(const char *filename) const {
- throw DatException("ExportAsTXT function is not implemented yet", EXPORT_EXCEPTION);
- }
- bool LOTRO_DAT::Subfile::ExportAsJPG(const char *filename) const {
- BinaryData data((unsigned) size1_ - 24);
- dat_->ReadData(data, (unsigned) size1_ - 24, (unsigned) file_offset_ + 24);
- return data.WriteToFile(std::string(filename) + ".jpg");
- }
- bool LOTRO_DAT::Subfile::ExportAsDDS(const char *filename) const {
- BinaryData data((unsigned) size1_);
- dat_->ReadData(data, (unsigned) size1_, (unsigned) file_offset_);
- BinaryData ddsData((unsigned) size1_ - 24 + 128);
- for (int i = 0; i < 128; i++)
- ddsData[i] = 0;
- memcpy(ddsData.data() + 128, data.data() + 24, (size_t) size1_ - 24);
- ddsData[0] = 0x44; // D
- ddsData[1] = 0x44; // D
- ddsData[2] = 0x53; // S
- ddsData[3] = 0x20;
- ddsData[4] = 0x7C;
- ddsData[8] = 7;
- ddsData[9] = 0x10;
- // width, height
- ddsData[12] = data[12];
- ddsData[13] = data[13];
- ddsData[14] = data[14];
- ddsData[15] = data[15];
- ddsData[16] = data[8];
- ddsData[17] = data[9];
- ddsData[18] = data[10];
- ddsData[19] = data[11];
- long long compression = data.ToNumber<4>(0x10);
- switch (compression) {
- case 20: // 14 00 00 00 - 888 (R8G8B8)
- ddsData[0x4C] = 0x20; // ?
- ddsData[0x50] = 0x40; // compressed or not
- ddsData[0x58] = 0x18; // bytes per pixel
- ddsData[0x5E] = 0xFF;
- ddsData[0x61] = 0xFF;
- ddsData[0x64] = 0xFF;
- break;
- case 21: // 15 00 00 00 - 8888 (R8G8B8A8)
- ddsData[0x4C] = 0x20; // ?
- ddsData[0x50] = 0x40; // compressed or not
- ddsData[0x58] = 0x20; // bytes per pixel
- ddsData[0x5E] = 0xFF;
- ddsData[0x61] = 0xFF;
- ddsData[0x64] = 0xFF;
- ddsData[0x6B] = 0xFF;
- break;
- case 28: // 1C 00 00 00 - 332 (?)
- ddsData[0x4C] = 0x20; // ?
- ddsData[0x50] = 0x40; // compressed or not
- ddsData[0x58] = 0x08; // bytes per pixel
- ddsData[0x5E] = 0xFF;
- ddsData[0x61] = 0xFF;
- ddsData[0x64] = 0xFF;
- break;
- case 827611204: // 44 58 54 31 - DXT1
- ddsData[76] = 32;
- ddsData[80] = 4;
- ddsData[84] = 68;
- ddsData[85] = 88;
- ddsData[86] = 84;
- ddsData[87] = 49;
- break;
- case 861165636: // 44 58 54 33 - DXT3
- ddsData[22] = 1;
- ddsData[76] = 32;
- ddsData[80] = 4;
- ddsData[84] = 68;
- ddsData[85] = 88;
- ddsData[86] = 84;
- ddsData[87] = 51;
- ddsData[108] = 8;
- ddsData[109] = 16;
- ddsData[110] = 64;
- break;
- case 894720068: // 44 58 54 35 - DXT5
- ddsData[10] = 8;
- ddsData[22] = 1;
- ddsData[28] = 1;
- ddsData[76] = 32;
- ddsData[80] = 4;
- ddsData[84] = 68;
- ddsData[85] = 88;
- ddsData[86] = 84;
- ddsData[87] = 53;
- ddsData[88] = 32;
- ddsData[94] = 255;
- ddsData[97] = 255;
- ddsData[100] = 255;
- ddsData[107] = 255;
- ddsData[109] = 16;
- break;
- }
- return ddsData.WriteToFile(std::string(filename) + ".dds");
- }
- bool LOTRO_DAT::Subfile::ExportAsOGG(const char *filename) const {
- BinaryData data((unsigned) size1_ - 8);
- dat_->ReadData(data, (unsigned) size1_ - 8, (unsigned) file_offset_ + 8);
- return data.WriteToFile(std::string(filename) + ".ogg");
- }
- bool LOTRO_DAT::Subfile::ExportAsFont(const char *filename) const {
- throw DatException("Bad Subfile::ExportAsFont() - this function hasn't been implemented yet", EXPORT_EXCEPTION);
- }
- bool LOTRO_DAT::Subfile::ExportAsWAV(const char *filename) const {
- //throw DatException("Bad Subfile::ExportAsWAV() - this function hasn't been implemented yet", EXPORT_EXCEPTION);
- BinaryData data((unsigned) size1_ - 8);
- dat_->ReadData(data, (unsigned) size1_ - 8, (unsigned) file_offset_ + 8);
-
- return data.WriteToFile(std::string(filename) + ".wav");
- }
- bool LOTRO_DAT::Subfile::ExportAsUnk(const char *filename) const {
- throw DatException("Bad Subfile::ExportAsUnk() - this function hasn't been implemented yet", EXPORT_EXCEPTION);
- }
|