123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- //
- // Created by Иван_Архипов on 07.11.2017.
- //
- #include "SubDirectory.h"
- #include "DatFile.h"
- #include "Common/DatException.h"
- #include "Subfile.h"
- #include "BinaryData.h"
- #include "Subfiles/TextSubfile.h"
- #include "Subfiles/DdsSubfile.h"
- #include "Subfiles/FontSubFile.h"
- #include "Subfiles/JpgSubfile.h"
- #include "Subfiles/OggSubfile.h"
- #include "Subfiles/WavSubfile.h"
- #include "Subfiles/UnknownSubfile.h"
- namespace LOTRO_DAT {
- SubDirectory::SubDirectory() {
- offset_ = 0;
- }
- SubDirectory::SubDirectory(long long offset, DatFile *dat, long long max_subdirs) :
- dat_(dat), offset_(offset), max_subdirs_(max_subdirs) {
- try {
- MakeSubDirectories();
- }catch (...) {
- fprintf(stderr, "Unable to initialize directory at offset %lld. Initializing it as empty directory...\n",
- offset);
- subdirs_.clear();
- subfiles_.clear();
- return;
- }
- try {
- MakeSubFiles();
- }catch (...) {
- fprintf(stderr, "Unable to initialize directory at offset %lld. Initializing it as empty directory...\n",
- offset);
- subdirs_.clear();
- subfiles_.clear();
- return;
- }
- }
- void SubDirectory::MakeSubDirectories() {
- BinaryData data(1024);
- dat_->ReadData(data, 63 * 8, offset_);
- if (data.ToNumber<4>(0) != 0 || data.ToNumber<4>(4) != 0) {
- std::string err =
- std::string("Bad SubDirectory::MakeSubDirectories - first 8 bytes are not equal to 0 at offset ")
- + std::to_string(offset_);
- throw DatException(err.c_str(), SUBDIR_EXCEPTION);
- }
- for (unsigned int i = 8; i < 63 * 8; i += 8) {
- if (data.ToNumber<4>(i) == 0 || data.ToNumber<4>(i + 4) == 0)
- break;
- try {
- subdirs_.emplace_back(
- SubDirectory(
- data.ToNumber<4>(i + 4),
- dat_
- )
- );
- }catch (...) {
- fprintf(stderr, "Making SubDirectory at offset %lld failed, continuing\n", data.ToNumber<4>(i + 4));
- }
- }
- }
- void SubDirectory::MakeSubFiles() {
- BinaryData data(64 * 32);
- dat_->ReadData(data, 64 * 32, offset_ + 63 * 8);
- for (unsigned int i = 0; i < 61 * 32; i += 32) {
- if (data.ToNumber<4>(i + 8) < 0x32 || data.ToNumber<4>(i + 12) < 0x32)
- continue;
- subfiles_.emplace_back(
- MakeSubfile(
- data.ToNumber<4>(i), // fragments_count
- data.ToNumber<4>(i + 4), // unknown1
- data.ToNumber<4>(i + 8), // file_id
- data.ToNumber<4>(i + 12), // file_offset
- data.ToNumber<4>(i + 16), // block_size
- data.ToNumber<4>(i + 20), // timestamp
- data.ToNumber<4>(i + 24), // version
- data.ToNumber<4>(i + 28) // block_size
- )
- );
- }
- }
- void SubDirectory::MakeDictionary(std::unordered_map<long long, Subfile *> &dict) {
- for (Subfile *i : subfiles_) {
- if (dict.count(i->file_id()) == 0 || dict[i->file_id()]->timestamp() < i->timestamp())
- dict[i->file_id()] = i;
- }
- for (SubDirectory &i : subdirs_)
- i.MakeDictionary(dict);
- }
- void SubDirectory::UpdateDirectories() {
- for (unsigned i = 0; i < subfiles_.size(); i++) {
- dat_->WriteData(subfiles_[i]->MakeHeaderData(), 32, offset_ + 63ll * 8ll + i * 32);
- }
- for (unsigned i = 0; i < subdirs_.size(); i++)
- subdirs_[i].UpdateDirectories();
- }
- Subfile *SubDirectory::MakeSubfile(long long fragments_count, long long unknown1, long long file_id,
- long long file_offset, long long file_size, long long timestamp,
- long long version, long long block_size) {
- FILE_TYPE type = GetSubfileType(file_id, file_offset);
- switch (type) {
- case TEXT:
- return dynamic_cast<Subfile *>(new TextSubfile(dat_, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
- case JPG:
- return dynamic_cast<Subfile *>(new JpgSubfile(dat_, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
- case DDS:
- return dynamic_cast<Subfile *>(new DdsSubfile(dat_, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
- case WAV:
- return dynamic_cast<Subfile *>(new WavSubfile(dat_, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
- case OGG:
- return dynamic_cast<Subfile *>(new OggSubfile(dat_, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
- case FONT:
- return dynamic_cast<Subfile *>(new FontSubfile(dat_, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
- case UNKNOWN:
- return dynamic_cast<Subfile *>(new UnknownSubfile(dat_, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size));
- }
- throw DatException("Bad SubDirectory::MakeSubfile() - unable to recognize file_type!", SUBFILE_EXCEPTION);
- }
- FILE_TYPE SubDirectory::GetSubfileType(long long file_id, long long file_offset) const {
- // Text check based on file_id
- if ((file_id >> 24ll) == 0x25ll)
- return TEXT;
- // Font check based on file_id
- if ((file_id >> 24ll) == 0x42ll)
- return FONT;
- BinaryData header(64);
- try {
- dat_->ReadData(header, 64, (unsigned) file_offset + 8);
- }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;
- }
- // 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;
- }
- };
|