// // Created by Иван_Архипов on 01.11.2017. // #include "Subfile.h" #include "BinaryData.h" #include "DatFile.h" #include "DatException.h" #include "Database.h" #include const long long MAXSIZE = 50ll * 1024ll * 1024ll; // Setting maximal file size 50 MB; Files with size more than 50 mb // will be recognized as incorrect and passed. LOTRO_DAT::Subfile::Subfile() { ext_ = UNKNOWN; compressed_ = false; } 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(); BinaryData header(20); dat_->ReadData(header, 10, file_offset_); compressed_ = header.CheckCompression(); } 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(28); //auto four = header.ToNumber(30); if (soi == 0xD8FFll && marker == 0xE0FFll || marker == 0xE1FFll) return JPG; return DDS; } // Ogg and Wav check 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_; } std::string LOTRO_DAT::Subfile::ExtensionToString(LOTRO_DAT::EXTENSION ext) const { switch (ext) { case LOTRO_DAT::TXT: return ".txt"; case LOTRO_DAT::JPG: return ".jpg"; case LOTRO_DAT::DDS: return ".dds"; case LOTRO_DAT::WAV: return ".wav"; case LOTRO_DAT::OGG: return ".ogg"; case LOTRO_DAT::FONT: return ".fontbin"; case LOTRO_DAT::UNKNOWN: return ".unk"; default: return ""; } } bool LOTRO_DAT::Subfile::ExportFile(const char *filename, Database *db) const { try { BinaryData data; switch (ext_) { case LOTRO_DAT::TXT: return ExportFileAsTXT(filename, db); case LOTRO_DAT::JPG: data = PrepareAsJPG(); break; case LOTRO_DAT::DDS: data = PrepareAsDDS(); break; case LOTRO_DAT::WAV: data = PrepareAsWAV(); break; case LOTRO_DAT::OGG: data = PrepareAsOGG(); break; case LOTRO_DAT::FONT: data = PrepareAsFont(); break; case LOTRO_DAT::UNKNOWN: data = PrepareAsUnk(); break; default: break; } if (db == nullptr) { return data.WriteToFile(std::string(filename) + std::string(ExtensionToString(ext_))); } else { db->PushBinaryFile(file_id_, data); return true; } } catch (DatException &e) { fprintf(stderr, "Caught exception while unpacking the file with id %lld and offset %lld. Continuing without this file...\n", file_id(), file_offset()); fprintf(stderr, "%s\n", e.what()); return false; } } bool LOTRO_DAT::Subfile::ExportFileAsTXT(const char *filename, Database *db) const { throw DatException("ExportAsTXT function is not implemented yet", EXPORT_EXCEPTION); } const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsJPG() const { BinaryData data((unsigned) size1_ - 24); dat_->ReadData(data, (unsigned) size1_ - 24, (unsigned) file_offset_ + 24); return data; } const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsDDS() const { BinaryData data((unsigned) size1_); dat_->ReadData(data, (unsigned) size1_, file_offset_); if (compressed_) data.DecompressData(4); BinaryData ddsData(data.size() - 24 + 128); for (int i = 0; i < 128; i++) ddsData[i] = 0; memcpy(ddsData.data() + 128, data.data() + 24, data.size() - 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; } const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsOGG() const { BinaryData data((unsigned) size1_ - 8); dat_->ReadData(data, (unsigned) size1_ - 8, (unsigned) file_offset_ + 8); return data; } const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsFont() const { throw DatException("Bad Subfile::ExportAsFont() - this function hasn't been implemented yet", EXPORT_EXCEPTION); } const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsWAV() const { BinaryData data((unsigned) size1_ - 8); dat_->ReadData(data, (unsigned) size1_ - 8, (unsigned) file_offset_ + 8); return data; } const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsUnk() const { throw DatException("Bad Subfile::ExportAsUnk() - this function hasn't been implemented yet", EXPORT_EXCEPTION); }